-
Notifications
You must be signed in to change notification settings - Fork 187
/
abi.rs
675 lines (612 loc) · 25.1 KB
/
abi.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
use crate::{
db::CodegenDb,
yul::{
runtime::{error_revert_numeric, make_ptr},
slot_size::{yul_primitive_type, SLOT_SIZE},
YulVariable,
},
};
use super::{AbiSrcLocation, DefaultRuntimeProvider, RuntimeFunction, RuntimeProvider};
use fe_mir::ir::{self, types::ArrayDef, TypeId, TypeKind};
use yultsur::*;
pub(super) fn make_abi_encode_primitive_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
legalized_ty: TypeId,
is_dst_storage: bool,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let dst = YulVariable::new("dst");
let enc_size = YulVariable::new("enc_size");
let func_def = function_definition! {
function [func_name.ident()]([src.ident()], [dst.ident()]) -> [enc_size.ident()] {
([src.ident()] := [provider.primitive_cast(db, src.expr(), legalized_ty)])
([yul::Statement::Expression(provider.ptr_store(
db,
dst.expr(),
src.expr(),
make_ptr(db, yul_primitive_type(db), is_dst_storage),
))])
([enc_size.ident()] := 32)
}
};
RuntimeFunction::from_statement(func_def)
}
pub(super) fn make_abi_encode_static_array_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
legalized_ty: TypeId,
) -> RuntimeFunction {
let is_dst_storage = legalized_ty.is_sptr(db.upcast());
let deref_ty = legalized_ty.deref(db.upcast());
let (elem_ty, len) = match &deref_ty.data(db.upcast()).kind {
ir::TypeKind::Array(def) => (def.elem_ty, def.len),
_ => unreachable!(),
};
let elem_abi_ty = db.codegen_abi_type(elem_ty);
let elem_ptr_ty = make_ptr(db, elem_ty, false);
let elem_ty_size = deref_ty.array_elem_size(db.upcast(), SLOT_SIZE);
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let dst = YulVariable::new("dst");
let enc_size = YulVariable::new("enc_size");
let header_size = elem_abi_ty.header_size();
let iter_count = literal_expression! {(len)};
let func = function_definition! {
function [func_name.ident()]([src.ident()], [dst.ident()]) -> [enc_size.ident()] {
(for {(let i := 0)} (lt(i, [iter_count])) {(i := (add(i, 1)))}
{
(pop([provider.abi_encode(db, src.expr(), dst.expr(), elem_ptr_ty, is_dst_storage)]))
([src.ident()] := add([src.expr()], [literal_expression!{(elem_ty_size)}]))
([dst.ident()] := add([dst.expr()], [literal_expression!{(header_size)}]))
})
([enc_size.ident()] := [literal_expression! {(header_size * len)}])
}
};
RuntimeFunction::from_statement(func)
}
pub(super) fn make_abi_encode_static_aggregate_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
legalized_ty: TypeId,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let is_dst_storage = legalized_ty.is_sptr(db.upcast());
let deref_ty = legalized_ty.deref(db.upcast());
let src = YulVariable::new("src");
let dst = YulVariable::new("dst");
let enc_size = YulVariable::new("enc_size");
let field_enc_size = YulVariable::new("field_enc_size");
let mut body = vec![
statement! {[enc_size.ident()] := 0 },
statement! {let [field_enc_size.ident()] := 0 },
];
let field_num = deref_ty.aggregate_field_num(db.upcast());
for idx in 0..field_num {
let field_ty = deref_ty.projection_ty_imm(db.upcast(), idx);
let field_ty_ptr = make_ptr(db, field_ty, false);
let field_offset = deref_ty.aggregate_elem_offset(db.upcast(), idx, SLOT_SIZE);
let src_offset = expression! { add([src.expr()], [literal_expression!{(field_offset)}]) };
body.push(statement!{
[field_enc_size.ident()] := [provider.abi_encode(db, src_offset, dst.expr(), field_ty_ptr, is_dst_storage)]
});
body.push(statement! {
[enc_size.ident()] := add([enc_size.expr()], [field_enc_size.expr()])
});
if idx < field_num - 1 {
body.push(assignment! {[dst.ident()] := add([dst.expr()], [field_enc_size.expr()])});
}
}
let func_def = yul::FunctionDefinition {
name: func_name.ident(),
parameters: vec![src.ident(), dst.ident()],
returns: vec![enc_size.ident()],
block: yul::Block { statements: body },
};
RuntimeFunction(func_def)
}
pub(super) fn make_abi_encode_string_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
is_dst_storage: bool,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let dst = YulVariable::new("dst");
let string_len = YulVariable::new("string_len");
let enc_size = YulVariable::new("enc_size");
let func_def = function_definition! {
function [func_name.ident()]([src.ident()], [dst.ident()]) -> [enc_size.ident()] {
(let [string_len.ident()] := mload([src.expr()]))
([enc_size.ident()] := add(32, (mul([string_len.expr()], 32))))
(let data_size := add(32, [string_len.expr()]))
([yul::Statement::Expression(provider.ptr_copy(db, src.expr(), dst.expr(), literal_expression!{data_size}, false, is_dst_storage))])
([enc_size.ident()] := mul((div((add(data_size, 31)), 32)), 32))
}
};
RuntimeFunction::from_statement(func_def)
}
pub(super) fn make_abi_encode_bytes_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
len: usize,
is_dst_storage: bool,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let dst = YulVariable::new("dst");
let enc_size = YulVariable::new("enc_size");
let dst_len_ty = make_ptr(db, yul_primitive_type(db), is_dst_storage);
let func_def = function_definition! {
function [func_name.ident()]([src.ident()], [dst.ident()]) -> [enc_size.ident()] {
([enc_size.ident()] := [literal_expression!{ (ceil_32(32 + len)) }])
([yul::Statement::Expression(provider.ptr_store(db, dst.expr(), literal_expression!{ (len) }, dst_len_ty))])
([dst.ident()] := add(32, [dst.expr()]))
([yul::Statement::Expression(provider.ptr_copy(db, src.expr(), dst.expr(), literal_expression!{(len)}, false, is_dst_storage))])
}
};
RuntimeFunction::from_statement(func_def)
}
pub(super) fn make_abi_encode_value_seq(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
value_tys: &[TypeId],
is_dst_storage: bool,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let value_num = value_tys.len();
let abi_tys: Vec<_> = value_tys
.iter()
.map(|ty| db.codegen_abi_type(ty.deref(db.upcast())))
.collect();
let dst = YulVariable::new("dst");
let header_ptr = YulVariable::new("header_ptr");
let enc_size = YulVariable::new("enc_size");
let data_ptr = YulVariable::new("data_ptr");
let values: Vec<_> = (0..value_num)
.map(|idx| YulVariable::new(format!("value{}", idx)))
.collect();
let total_header_size =
literal_expression! { (abi_tys.iter().fold(0, |acc, ty| acc + ty.header_size())) };
let mut body = statements! {
(let [header_ptr.ident()] := [dst.expr()])
([enc_size.ident()] := [total_header_size])
(let [data_ptr.ident()] := add([dst.expr()], [enc_size.expr()]))
};
for i in 0..value_num {
let ty = value_tys[i];
let abi_ty = &abi_tys[i];
let value = &values[i];
let header_size = literal_expression! { (abi_ty.header_size()) };
let stmts = if abi_ty.is_static() {
statements! {
(pop([provider.abi_encode(db, value.expr(), header_ptr.expr(), ty, is_dst_storage)]))
([header_ptr.ident()] := add([header_ptr.expr()], [header_size]))
}
} else {
let header_ty = make_ptr(db, yul_primitive_type(db), is_dst_storage);
statements! {
([yul::Statement::Expression(provider.ptr_store(db, header_ptr.expr(), enc_size.expr(), header_ty))])
([enc_size.ident()] := add([provider.abi_encode(db, value.expr(), data_ptr.expr(), ty, is_dst_storage)], [enc_size.expr()]))
([header_ptr.ident()] := add([header_ptr.expr()], [header_size]))
([data_ptr.ident()] := add([dst.expr()], [enc_size.expr()]))
}
};
body.extend_from_slice(&stmts);
}
let mut parameters = vec![dst.ident()];
for value in values {
parameters.push(value.ident());
}
let func_def = yul::FunctionDefinition {
name: func_name.ident(),
parameters,
returns: vec![enc_size.ident()],
block: yul::Block { statements: body },
};
RuntimeFunction(func_def)
}
pub(super) fn make_abi_decode(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
types: &[TypeId],
abi_loc: AbiSrcLocation,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let header_size = types
.iter()
.fold(0, |acc, ty| acc + db.codegen_abi_type(*ty).header_size());
let src = YulVariable::new("$src");
let enc_size = YulVariable::new("$enc_size");
let header_ptr = YulVariable::new("header_ptr");
let data_offset = YulVariable::new("data_offset");
let tmp_offset = YulVariable::new("tmp_offset");
let returns: Vec<_> = (0..types.len())
.map(|i| YulVariable::new(format!("$ret{}", i)))
.collect();
let abi_enc_size = abi_enc_size(db, types);
let size_check = match abi_enc_size {
AbiEncodingSize::Static(size) => statements! {
(if (iszero((eq([enc_size.expr()], [literal_expression!{(size)}]))))
{ [revert_with_invalid_abi_data(provider, db)]
})
},
AbiEncodingSize::Bounded { min, max } => statements! {
(if (or(
(lt([enc_size.expr()], [literal_expression!{(min)}])),
(gt([enc_size.expr()], [literal_expression!{(max)}]))
)) {
[revert_with_invalid_abi_data(provider, db)]
})
},
};
let mut body = statements! {
(let [header_ptr.ident()] := [src.expr()])
(let [data_offset.ident()] := [literal_expression!{ (header_size) }])
(let [tmp_offset.ident()] := 0)
};
for i in 0..returns.len() {
let ret_value = &returns[i];
let field_ty = types[i];
let field_abi_ty = db.codegen_abi_type(field_ty.deref(db.upcast()));
if field_abi_ty.is_static() {
body.push(statement!{ [ret_value.ident()] := [provider.abi_decode_static(db, header_ptr.expr(), field_ty, abi_loc)] });
} else {
let identifiers = identifiers! {
[ret_value.ident()]
[tmp_offset.ident()]
};
body.push(yul::Statement::Assignment(yul::Assignment {
identifiers,
expression: provider.abi_decode_dynamic(
db,
expression! {add([src.expr()], [data_offset.expr()])},
field_ty,
abi_loc,
),
}));
body.push(statement! { ([data_offset.ident()] := add([data_offset.expr()], [tmp_offset.expr()])) });
};
let field_header_size = literal_expression! { (field_abi_ty.header_size()) };
body.push(
statement! { [header_ptr.ident()] := add([header_ptr.expr()], [field_header_size]) },
);
}
let offset_check = match abi_enc_size {
AbiEncodingSize::Static(_) => vec![],
AbiEncodingSize::Bounded { .. } => statements! {
(if (iszero((eq([enc_size.expr()], [data_offset.expr()])))) { [revert_with_invalid_abi_data(provider, db)] })
},
};
let returns: Vec<_> = returns.iter().map(YulVariable::ident).collect();
let func_def = function_definition! {
function [func_name.ident()]([src.ident()], [enc_size.ident()]) -> [returns...] {
[size_check...]
[body...]
[offset_check...]
}
};
RuntimeFunction::from_statement(func_def)
}
impl DefaultRuntimeProvider {
fn abi_decode_static(
&mut self,
db: &dyn CodegenDb,
src: yul::Expression,
ty: TypeId,
abi_loc: AbiSrcLocation,
) -> yul::Expression {
let ty = db.codegen_legalized_type(ty).deref(db.upcast());
let abi_ty = db.codegen_abi_type(ty.deref(db.upcast()));
debug_assert!(abi_ty.is_static());
let func_name_postfix = match abi_loc {
AbiSrcLocation::CallData => "call_data",
AbiSrcLocation::Memory => "memory",
};
let args = vec![src];
if ty.is_primitive(db.upcast()) {
let name = format! {
"$abi_decode_primitive_type_{}_from_{}",
ty.0, func_name_postfix,
};
return self.create_then_call(&name, args, |provider| {
make_abi_decode_primitive_type(provider, db, &name, ty, abi_loc)
});
}
let name = format! {
"$abi_decode_static_aggregate_type_{}_from_{}",
ty.0, func_name_postfix,
};
self.create_then_call(&name, args, |provider| {
make_abi_decode_static_aggregate_type(provider, db, &name, ty, abi_loc)
})
}
fn abi_decode_dynamic(
&mut self,
db: &dyn CodegenDb,
src: yul::Expression,
ty: TypeId,
abi_loc: AbiSrcLocation,
) -> yul::Expression {
let ty = db.codegen_legalized_type(ty).deref(db.upcast());
let abi_ty = db.codegen_abi_type(ty.deref(db.upcast()));
debug_assert!(!abi_ty.is_static());
let func_name_postfix = match abi_loc {
AbiSrcLocation::CallData => "call_data",
AbiSrcLocation::Memory => "memory",
};
let mut args = vec![src];
if abi_ty.is_string() {
let len = match &ty.data(db.upcast()).kind {
TypeKind::String(len) => *len,
_ => unreachable!(),
};
args.push(literal_expression! {(len)});
let name = format! {"$abi_decode_string_from_{}", func_name_postfix};
self.create_then_call(&name, args, |provider| {
make_abi_decode_string_type(provider, db, &name, abi_loc)
})
} else if abi_ty.is_bytes() {
let len = match &ty.data(db.upcast()).kind {
TypeKind::Array(ArrayDef { len, .. }) => *len,
_ => unreachable!(),
};
args.push(literal_expression! {(len)});
let name = format! {"$abi_decode_bytes_from_{}", func_name_postfix};
self.create_then_call(&name, args, |provider| {
make_abi_decode_bytes_type(provider, db, &name, abi_loc)
})
} else {
todo! {}
}
}
}
fn make_abi_decode_primitive_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
ty: TypeId,
abi_loc: AbiSrcLocation,
) -> RuntimeFunction {
debug_assert! {ty.is_primitive(db.upcast())}
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let ret = YulVariable::new("ret");
let decode = match abi_loc {
AbiSrcLocation::CallData => {
statement! { [ret.ident()] := calldataload([src.expr()]) }
}
AbiSrcLocation::Memory => {
statement! { [ret.ident()] := mload([src.expr()]) }
}
};
let ty_size_bits = ty.size_of(db.upcast(), SLOT_SIZE) * 8;
let validation = if ty_size_bits == 256 {
statements! {}
} else if ty.is_signed(db.upcast()) {
let shift_num = literal_expression! { ( ty_size_bits - 1) };
let tmp1 = YulVariable::new("tmp1");
let tmp2 = YulVariable::new("tmp2");
statements! {
(let [tmp1.ident()] := iszero((shr([shift_num.clone()], [ret.expr()]))))
(let [tmp2.ident()] := iszero((shr([shift_num], (not([ret.expr()]))))))
(if (iszero((or([tmp1.expr()], [tmp2.expr()])))) {
[revert_with_invalid_abi_data(provider, db)]
})
}
} else {
let shift_num = literal_expression! { ( ty_size_bits) };
let tmp = YulVariable::new("tmp");
statements! {
(let [tmp.ident()] := iszero((shr([shift_num], [ret.expr()]))))
(if (iszero([tmp.expr()])) {
[revert_with_invalid_abi_data(provider, db)]
})
}
};
let func = function_definition! {
function [func_name.ident()]([src.ident()]) -> [ret.ident()] {
([decode])
[validation...]
}
};
RuntimeFunction::from_statement(func)
}
fn make_abi_decode_static_aggregate_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
ty: TypeId,
abi_loc: AbiSrcLocation,
) -> RuntimeFunction {
debug_assert!(ty.is_aggregate(db.upcast()));
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let ret = YulVariable::new("ret");
let field_data = YulVariable::new("field_data");
let type_size = literal_expression! { (ty.size_of(db.upcast(), SLOT_SIZE)) };
let mut body = statements! {
(let [field_data.ident()] := 0)
([ret.ident()] := [provider.alloc(db, type_size)])
};
let field_num = ty.aggregate_field_num(db.upcast());
for idx in 0..field_num {
let field_ty = ty.projection_ty_imm(db.upcast(), idx);
let field_ty_size = field_ty.size_of(db.upcast(), SLOT_SIZE);
body.push(statement! { [field_data.ident()] := [provider.abi_decode_static(db, src.expr(), field_ty, abi_loc)] });
let dst_offset =
literal_expression! { (ty.aggregate_elem_offset(db.upcast(), idx, SLOT_SIZE)) };
let field_ty_ptr = make_ptr(db, field_ty, false);
if field_ty.is_primitive(db.upcast()) {
body.push(yul::Statement::Expression(provider.ptr_store(
db,
expression! {add([ret.expr()], [dst_offset])},
field_data.expr(),
field_ty_ptr,
)));
} else {
body.push(yul::Statement::Expression(provider.ptr_copy(
db,
expression! {add([ret.expr()], [dst_offset])},
field_data.expr(),
literal_expression! { (field_ty_size) },
false,
false,
)));
}
if idx < field_num - 1 {
let abi_field_ty = db.codegen_abi_type(field_ty);
let field_abi_ty_size = literal_expression! { (abi_field_ty.header_size()) };
body.push(assignment! {[src.ident()] := add([src.expr()],
[field_abi_ty_size])});
}
}
let func_def = yul::FunctionDefinition {
name: func_name.ident(),
parameters: vec![src.ident()],
returns: vec![ret.ident()],
block: yul::Block { statements: body },
};
RuntimeFunction(func_def)
}
fn make_abi_decode_string_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
abi_loc: AbiSrcLocation,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let decoded_data = YulVariable::new("decoded_data");
let decoded_size = YulVariable::new("decoded_size");
let max_len = YulVariable::new("max_len");
let string_size = YulVariable::new("string_size");
let dst_size = YulVariable::new("dst_size");
let end_word = YulVariable::new("end_word");
let end_word_ptr = YulVariable::new("end_word_ptr");
let padding_size_bits = YulVariable::new("padding_size_bits");
let primitive_ty_ptr = make_ptr(db, yul_primitive_type(db), false);
let func = function_definition! {
function [func_name.ident()]([src.ident()], [max_len.ident()]) -> [(vec![decoded_data.ident(), decoded_size.ident()])...] {
(let string_len := [provider.abi_decode_static(db, src.expr(), primitive_ty_ptr, abi_loc)])
(if (gt(string_len, [max_len.expr()])) { [revert_with_invalid_abi_data(provider, db)] } )
(let [string_size.ident()] := add(string_len, 32))
([decoded_size.ident()] := mul((div((add([string_size.expr()], 31)), 32)), 32))
(let [end_word_ptr.ident()] := sub((add([src.expr()], [decoded_size.expr()])), 32))
(let [end_word.ident()] := [provider.abi_decode_static(db, end_word_ptr.expr(), primitive_ty_ptr, abi_loc)])
(let [padding_size_bits.ident()] := mul((sub([decoded_size.expr()], [string_size.expr()])), 8))
[(check_right_padding(provider, db, end_word.expr(), padding_size_bits.expr()))...]
(let [dst_size.ident()] := add([max_len.expr()], 32))
([decoded_data.ident()] := [provider.alloc(db, dst_size.expr())])
([ptr_copy_decode(provider, db, src.expr(), decoded_data.expr(), string_size.expr(), abi_loc)])
}
};
RuntimeFunction::from_statement(func)
}
fn make_abi_decode_bytes_type(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
func_name: &str,
abi_loc: AbiSrcLocation,
) -> RuntimeFunction {
let func_name = YulVariable::new(func_name);
let src = YulVariable::new("src");
let decoded_data = YulVariable::new("decoded_data");
let decoded_size = YulVariable::new("decoded_size");
let max_len = YulVariable::new("max_len");
let bytes_size = YulVariable::new("bytes_size");
let end_word = YulVariable::new("end_word");
let end_word_ptr = YulVariable::new("end_word_ptr");
let padding_size_bits = YulVariable::new("padding_size_bits");
let primitive_ty_ptr = make_ptr(db, yul_primitive_type(db), false);
let func = function_definition! {
function [func_name.ident()]([src.ident()], [max_len.ident()]) -> [(vec![decoded_data.ident(),decoded_size.ident()])...] {
(let [bytes_size.ident()] := [provider.abi_decode_static(db, src.expr(), primitive_ty_ptr, abi_loc)])
(if (iszero((eq([bytes_size.expr()], [max_len.expr()])))) { [revert_with_invalid_abi_data(provider, db)] } )
([src.ident()] := add([src.expr()], 32))
(let padded_data_size := mul((div((add([bytes_size.expr()], 31)), 32)), 32))
([decoded_size.ident()] := add(padded_data_size, 32))
(let [end_word_ptr.ident()] := sub((add([src.expr()], padded_data_size)), 32))
(let [end_word.ident()] := [provider.abi_decode_static(db, end_word_ptr.expr(), primitive_ty_ptr, abi_loc)])
(let [padding_size_bits.ident()] := mul((sub(padded_data_size, [bytes_size.expr()])), 8))
[(check_right_padding(provider, db, end_word.expr(), padding_size_bits.expr()))...]
([decoded_data.ident()] := [provider.alloc(db, max_len.expr())])
([ptr_copy_decode(provider, db, src.expr(), decoded_data.expr(), bytes_size.expr(), abi_loc)])
}
};
RuntimeFunction::from_statement(func)
}
enum AbiEncodingSize {
Static(usize),
Bounded { min: usize, max: usize },
}
fn abi_enc_size(db: &dyn CodegenDb, types: &[TypeId]) -> AbiEncodingSize {
let mut min = 0;
let mut max = 0;
for &ty in types {
let legalized_ty = db.codegen_legalized_type(ty);
let abi_ty = db.codegen_abi_type(legalized_ty);
let maximum_size = db.codegen_abi_type_maximum_size(ty);
if abi_ty.is_string() {
min += 64;
} else {
min += maximum_size
};
max += maximum_size;
}
if min == max {
AbiEncodingSize::Static(min)
} else {
AbiEncodingSize::Bounded { min, max }
}
}
fn revert_with_invalid_abi_data(
provider: &mut dyn RuntimeProvider,
db: &dyn CodegenDb,
) -> yul::Statement {
const ERROR_INVALID_ABI_DATA: usize = 0x103;
let error_code = literal_expression! { (ERROR_INVALID_ABI_DATA) };
error_revert_numeric(provider, db, error_code)
}
fn check_right_padding(
provider: &mut dyn RuntimeProvider,
db: &dyn CodegenDb,
val: yul::Expression,
size_bits: yul::Expression,
) -> Vec<yul::Statement> {
statements! {
(let bits_shifted := sub(256, [size_bits]))
(let is_ok := iszero((shl(bits_shifted, [val]))))
(if (iszero((is_ok))) {
[revert_with_invalid_abi_data(provider, db)]
})
}
}
fn ptr_copy_decode(
provider: &mut DefaultRuntimeProvider,
db: &dyn CodegenDb,
src: yul::Expression,
dst: yul::Expression,
len: yul::Expression,
loc: AbiSrcLocation,
) -> yul::Statement {
match loc {
AbiSrcLocation::CallData => {
statement! { calldatacopy([dst], [src], [len]) }
}
AbiSrcLocation::Memory => {
yul::Statement::Expression(provider.ptr_copy(db, src, dst, len, false, false))
}
}
}
fn ceil_32(len: usize) -> usize {
((len + 31) / 32) * 32
}