forked from hyperledger-solang/solang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoroban_encoding.rs
More file actions
1571 lines (1386 loc) · 46.5 KB
/
soroban_encoding.rs
File metadata and controls
1571 lines (1386 loc) · 46.5 KB
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
use crate::codegen::cfg::InternalCallTy;
use crate::codegen::cfg::{ControlFlowGraph, Instr};
use crate::codegen::encoding::create_encoder;
use crate::codegen::vartable::Vartable;
use crate::codegen::HostFunctions;
use crate::codegen::{Builtin, Expression};
use crate::sema::ast::{ArrayLength, Namespace, RetrieveType, StructType, Type, Type::Uint};
use num_bigint::BigInt;
use num_traits::Zero;
use solang_parser::helpers::CodeLocation;
use solang_parser::pt;
use solang_parser::pt::Loc;
/// Soroban encoder works a little differently than the other encoders.
/// For an external call, Soroban first needs to convert values into Soroban ScVals.
/// Each ScVal is 64 bits long, and encoded either via a host function or shifting bits.
/// For this reason, the soroban encoder is implemented as separate from the other encoders.
pub fn soroban_encode(
loc: &Loc,
args: Vec<Expression>,
ns: &Namespace,
vartab: &mut Vartable,
cfg: &mut ControlFlowGraph,
packed: bool,
) -> (Expression, Expression, Vec<Expression>) {
let mut encoder = create_encoder(ns, packed);
let size = 8 * args.len(); // 8 bytes per argument
let size_expr = Expression::NumberLiteral {
loc: *loc,
ty: Uint(32),
value: size.into(),
};
let encoded_bytes = vartab.temp_name("abi_encoded", &Type::Bytes(size as u8));
let expr = Expression::AllocDynamicBytes {
loc: *loc,
ty: Type::Bytes(size as u8),
size: size_expr.clone().into(),
initializer: None,
};
cfg.add(
vartab,
Instr::Set {
loc: *loc,
res: encoded_bytes,
expr,
},
);
let mut offset = Expression::NumberLiteral {
loc: *loc,
ty: Uint(64),
value: BigInt::zero(),
};
let buffer = Expression::Variable {
loc: *loc,
ty: Type::Bytes(size as u8),
var_no: encoded_bytes,
};
let mut encoded_items = Vec::new();
for (arg_no, item) in args.iter().enumerate() {
let var = soroban_encode_arg(item.clone(), cfg, vartab, ns);
encoded_items.push(var.clone());
let advance = encoder.encode(&var, &buffer, &offset, arg_no, ns, vartab, cfg);
offset = Expression::Add {
loc: *loc,
ty: Uint(64),
overflowing: false,
left: offset.into(),
right: advance.into(),
};
}
(buffer, size_expr, encoded_items)
}
pub fn soroban_decode(
_loc: &Loc,
buffer: &Expression,
_types: &[Type],
ns: &Namespace,
vartab: &mut Vartable,
cfg: &mut ControlFlowGraph,
_buffer_size_expr: Option<Expression>,
) -> Vec<Expression> {
let mut returns = Vec::new();
let loaded_val = Expression::Load {
loc: Loc::Codegen,
ty: Type::Uint(64),
expr: Box::new(buffer.clone()),
};
let decoded_val = soroban_decode_arg(loaded_val, cfg, vartab, ns, None);
returns.push(decoded_val);
returns
}
pub fn soroban_decode_arg(
arg: Expression,
wrapper_cfg: &mut ControlFlowGraph,
vartab: &mut Vartable,
ns: &Namespace,
decode_as: Option<Type>,
) -> Expression {
let ty = match decode_as {
Some(ty) => ty,
None => {
if let Type::Ref(inner_ty) = arg.ty() {
*inner_ty
} else if let Type::StorageRef(_, inner) = arg.ty() {
*inner
} else if let Type::SorobanHandle(inner) = arg.ty() {
*inner
} else {
arg.ty()
}
}
};
match ty {
Type::Bool => Expression::NotEqual {
loc: Loc::Codegen,
left: arg.into(),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: 0u64.into(),
}),
},
Type::Uint(64) => Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Uint(64),
left: arg.into(),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(8_u64),
}),
signed: false,
},
Type::Address(_) | Type::String => arg.clone(),
Type::Int(128) | Type::Uint(128) => decode_i128(wrapper_cfg, vartab, arg),
Type::Int(256) | Type::Uint(256) => decode_i256(wrapper_cfg, vartab, arg),
Type::Uint(32) => {
// get payload out of major bits then truncate to 32‑bit
Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Uint(32),
expr: Box::new(Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Uint(64),
left: arg.into(),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: 32u64.into(),
}),
signed: false,
}),
}
}
Type::Int(32) => Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(32),
expr: Box::new(Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Int(64),
left: arg.into(),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: 32u64.into(),
}),
signed: true,
}),
},
Type::Int(64) => Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Int(64),
left: arg.into(),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(8u64),
}),
signed: true,
},
Type::Struct(StructType::UserDefined(n)) => {
decode_struct(arg, wrapper_cfg, vartab, n, ns, ty)
}
Type::Array(elem_ty, _) => {
if let Type::StorageRef(_, _) = arg.ty() {
arg.clone()
} else {
decode_vector(arg, &elem_ty, ns, wrapper_cfg, vartab)
}
}
_ => unimplemented!("unimplemented ty {:#?} in soroban decoder", ty),
}
}
pub fn soroban_encode_arg(
item: Expression,
cfg: &mut ControlFlowGraph,
vartab: &mut Vartable,
ns: &Namespace,
) -> Expression {
let obj = vartab.temp_name("obj_".to_string().as_str(), &Type::Uint(64));
let ret = match item.ty() {
Type::Bool => {
let encoded = match item {
Expression::BoolLiteral { value, .. } => Expression::NumberLiteral {
loc: item.loc(),
ty: Type::Uint(64),
value: if value { 1u64.into() } else { 0u64.into() },
},
_ => item.cast(&Type::Uint(64), ns),
};
Instr::Set {
loc: item.loc(),
res: obj,
expr: encoded,
}
}
Type::String => {
if let Expression::Variable {
loc: _,
ty: _,
var_no: _,
} = item.clone()
{
Instr::Set {
loc: Loc::Codegen,
res: obj,
expr: item.clone(),
}
} else {
let inp = Expression::VectorData {
pointer: Box::new(item.clone()),
};
let inp_extend = Expression::ZeroExt {
loc: Loc::Codegen,
ty: Type::Uint(64),
expr: Box::new(inp),
};
let encoded = Expression::ShiftLeft {
loc: Loc::Codegen,
ty: Uint(64),
left: Box::new(inp_extend),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(32),
}),
};
let encoded = Expression::Add {
loc: Loc::Codegen,
ty: Type::Uint(64),
overflowing: true,
left: Box::new(encoded),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(4),
}),
};
let len = match item.clone() {
Expression::AllocDynamicBytes { size, .. } => {
let sesa = Expression::ShiftLeft {
loc: Loc::Codegen,
ty: Uint(64),
left: Box::new(size.clone().cast(&Type::Uint(64), ns)),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(32),
}),
};
Expression::Add {
loc: Loc::Codegen,
ty: Type::Uint(64),
overflowing: true,
left: Box::new(sesa),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(4),
}),
}
}
Expression::BytesLiteral { loc, ty: _, value } => {
let len = Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(value.len() as u64),
};
let len = Expression::ShiftLeft {
loc,
ty: Type::Uint(64),
left: Box::new(len),
right: Box::new(Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(32),
}),
};
Expression::Add {
loc,
ty: Type::Uint(64),
left: Box::new(len),
right: Box::new(Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(4),
}),
overflowing: false,
}
}
_ => unreachable!(),
};
Instr::Call {
res: vec![obj],
return_tys: vec![Type::Uint(64)],
call: crate::codegen::cfg::InternalCallTy::HostFunction {
name: HostFunctions::SymbolNewFromLinearMemory.name().to_string(),
},
args: vec![encoded, len],
}
}
}
Type::Uint(32) | Type::Int(32) => {
// widen to 64 bits so we can shift
let widened = match item.ty() {
Type::Uint(32) => Expression::ZeroExt {
loc: item.loc(),
ty: Type::Uint(64),
expr: Box::new(item.clone()),
},
Type::Int(32) => Expression::SignExt {
loc: item.loc(),
ty: Type::Int(64),
expr: Box::new(item.clone()),
},
_ => unreachable!(),
};
// the value goes into the major bits of the 64 bit value
let shifted = Expression::ShiftLeft {
loc: item.loc(),
ty: Type::Uint(64),
left: Box::new(widened.cast(&Type::Uint(64), ns)),
right: Box::new(Expression::NumberLiteral {
loc: item.loc(),
ty: Type::Uint(64),
value: 32u64.into(), // 24 (minor) + 8 (tag)
}),
};
let tag = if matches!(item.ty(), Type::Uint(32)) {
4
} else {
5
};
Instr::Set {
loc: item.loc(),
res: obj,
expr: Expression::Add {
loc: item.loc(),
ty: Type::Uint(64),
left: Box::new(shifted),
right: Box::new(Expression::NumberLiteral {
loc: item.loc(),
ty: Type::Uint(64),
value: tag.into(),
}),
overflowing: false,
},
}
}
Type::Uint(64) | Type::Int(64) => {
let shift_left = Expression::ShiftLeft {
loc: item.loc(),
ty: Type::Uint(64),
left: Box::new(item.clone()),
right: Box::new(Expression::NumberLiteral {
loc: item.loc(),
ty: Type::Uint(64),
value: BigInt::from(8),
}),
};
let tag = match item.ty() {
Type::Uint(64) => 6,
Type::Int(64) => 7,
_ => unreachable!(),
};
let added = Expression::Add {
loc: item.loc(),
ty: Type::Uint(64),
left: Box::new(shift_left),
right: Box::new(Expression::NumberLiteral {
loc: item.loc(),
ty: Type::Uint(64),
value: BigInt::from(tag),
}),
overflowing: false,
};
Instr::Set {
loc: item.loc(),
res: obj,
expr: added,
}
}
Type::Address(_) => {
let instr = if let Expression::Cast { loc, ty: _, expr } = item {
let address_literal = expr;
let pointer = Expression::VectorData {
pointer: address_literal.clone(),
};
let pointer_extend = Expression::ZeroExt {
loc,
ty: Type::Uint(64),
expr: Box::new(pointer),
};
let encoded = Expression::ShiftLeft {
loc,
ty: Uint(64),
left: Box::new(pointer_extend),
right: Box::new(Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(32),
}),
};
let encoded = Expression::Add {
loc,
ty: Type::Uint(64),
overflowing: true,
left: Box::new(encoded),
right: Box::new(Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(4),
}),
};
let len = if let Expression::BytesLiteral { loc, ty: _, value } =
*address_literal.clone()
{
let len = Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(value.len() as u64),
};
let len = Expression::ShiftLeft {
loc,
ty: Type::Uint(64),
left: Box::new(len),
right: Box::new(Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(32),
}),
};
Expression::Add {
loc,
ty: Type::Uint(64),
left: Box::new(len),
right: Box::new(Expression::NumberLiteral {
loc,
ty: Type::Uint(64),
value: BigInt::from(4),
}),
overflowing: false,
}
} else {
todo!()
};
let str_key_temp = vartab.temp_name("str_key", &Type::Uint(64));
let str_key_var = Expression::Variable {
loc,
ty: Type::Uint(64),
var_no: str_key_temp,
};
let soroban_str_key = Instr::Call {
res: vec![str_key_temp],
return_tys: vec![Type::Uint(64)],
call: crate::codegen::cfg::InternalCallTy::HostFunction {
name: HostFunctions::StringNewFromLinearMemory.name().to_string(),
},
args: vec![encoded.clone(), len.clone()],
};
cfg.add(vartab, soroban_str_key);
let address_object = Instr::Call {
res: vec![obj],
return_tys: vec![Type::Uint(64)],
call: crate::codegen::cfg::InternalCallTy::HostFunction {
name: HostFunctions::StrKeyToAddr.name().to_string(),
},
args: vec![str_key_var],
};
address_object
} else {
Instr::Set {
loc: Loc::Codegen,
res: obj,
expr: item.clone(),
}
};
instr
}
Type::Int(128) | Type::Uint(128) => {
let low = Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(64),
expr: Box::new(item.clone()),
};
let high = Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Int(128),
left: Box::new(item.clone()),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Int(128),
value: BigInt::from(64),
}),
signed: false,
};
let high = Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(64),
expr: Box::new(high),
};
let encoded = encode_i128(cfg, vartab, low, high, item.ty());
Instr::Set {
loc: item.loc(),
res: obj,
expr: encoded,
}
}
Type::Int(256) | Type::Uint(256) => {
// For 256-bit integers, we need to split into four 64-bit pieces
// lo_lo: bits 0-63
// lo_hi: bits 64-127
// hi_lo: bits 128-191
// hi_hi: bits 192-255
let is_signed = matches!(item.ty(), Type::Int(256));
// Extract lo_lo (bits 0-63)
let lo_lo = Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(64),
expr: Box::new(item.clone()),
};
// Extract lo_hi (bits 64-127)
let lo_hi_shift = Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Int(256),
left: Box::new(item.clone()),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Int(256),
value: BigInt::from(64),
}),
signed: is_signed,
};
let lo_hi = Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(64),
expr: Box::new(lo_hi_shift),
};
// Extract hi_lo (bits 128-191)
let hi_lo_shift = Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Int(256),
left: Box::new(item.clone()),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Int(256),
value: BigInt::from(128),
}),
signed: is_signed,
};
let hi_lo = Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(64),
expr: Box::new(hi_lo_shift),
};
// Extract hi_hi (bits 192-255)
let hi_hi_shift = Expression::ShiftRight {
loc: Loc::Codegen,
ty: Type::Int(256),
left: Box::new(item.clone()),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Int(256),
value: BigInt::from(192),
}),
signed: is_signed,
};
let hi_hi = Expression::Trunc {
loc: Loc::Codegen,
ty: Type::Int(64),
expr: Box::new(hi_hi_shift),
};
let encoded = encode_i256(cfg, vartab, lo_lo, lo_hi, hi_lo, hi_hi, item.ty());
Instr::Set {
loc: item.loc(),
res: obj,
expr: encoded,
}
}
Type::Struct(StructType::UserDefined(n)) => {
let buf = encode_struct(item.clone(), cfg, vartab, ns, n);
Instr::Set {
loc: Loc::Codegen,
res: obj,
expr: buf,
}
}
Type::SorobanHandle(_) => Instr::Set {
loc: Loc::Codegen,
res: obj,
expr: item.clone(),
},
Type::Array(_, _) => Instr::Set {
loc: Loc::Codegen,
res: obj,
expr: encode_vector(item.clone(), cfg, vartab),
},
_ => todo!("Type not yet supported in soroban encoder: {:?}", item.ty()),
};
cfg.add(vartab, ret);
Expression::Variable {
loc: pt::Loc::Codegen,
ty: Type::Uint(64),
var_no: obj,
}
}
fn encode_i128(
cfg: &mut ControlFlowGraph,
vartab: &mut Vartable,
lo: Expression,
high: Expression,
int128_ty: Type,
) -> Expression {
let ret_var = vartab.temp_anonymous(&lo.ty());
let ret = Expression::Variable {
loc: pt::Loc::Codegen,
ty: lo.ty().clone(),
var_no: ret_var,
};
vartab.new_dirty_tracker();
let fits_in_56_bits = cfg.new_basic_block("fits_in_56_bits".to_string());
let should_be_in_host = cfg.new_basic_block("should_be_in_host".to_string());
let return_block = cfg.new_basic_block("finish".to_string());
let high_8_bits = Expression::ShiftRight {
loc: pt::Loc::Codegen,
ty: Type::Uint(64),
left: lo.clone().into(),
right: Expression::NumberLiteral {
loc: pt::Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(56_u64),
}
.into(),
signed: false,
};
let cond = Expression::Equal {
loc: pt::Loc::Codegen,
left: high_8_bits.clone().into(),
right: Expression::NumberLiteral {
loc: pt::Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(0_u64),
}
.into(),
};
cfg.add(
vartab,
Instr::BranchCond {
cond,
true_block: fits_in_56_bits,
false_block: should_be_in_host,
},
);
cfg.set_basic_block(fits_in_56_bits);
let to_return = Expression::ShiftLeft {
loc: Loc::Codegen,
ty: Type::Uint(64),
left: Box::new(lo.clone()),
right: Box::new(Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(8_u64),
}),
};
let tag = match int128_ty {
Type::Int(128) => 11,
Type::Uint(128) => 10,
_ => unreachable!(),
};
let to_return = Expression::Add {
loc: Loc::Codegen,
ty: Type::Uint(64),
left: to_return.into(),
right: Expression::NumberLiteral {
loc: Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(tag),
}
.into(),
overflowing: false,
};
let set_instr = Instr::Set {
loc: pt::Loc::Codegen,
res: ret_var,
expr: to_return,
};
cfg.add(vartab, set_instr);
cfg.add(
vartab,
Instr::Branch {
block: return_block,
},
);
cfg.set_basic_block(should_be_in_host);
let instr = match int128_ty {
Type::Int(128) => Instr::Call {
res: vec![ret_var],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::ObjFromI128Pieces.name().to_string(),
},
args: vec![high, lo],
},
Type::Uint(128) => Instr::Call {
res: vec![ret_var],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::ObjFromU128Pieces.name().to_string(),
},
args: vec![high, lo],
},
_ => unreachable!(),
};
cfg.add(vartab, instr);
cfg.add(
vartab,
Instr::Branch {
block: return_block,
},
);
cfg.set_basic_block(return_block);
cfg.set_phis(return_block, vartab.pop_dirty_tracker());
ret
}
/// Encodes a 256-bit integer (signed or unsigned) into a Soroban ScVal.
/// This function handles both Int256 and Uint256 types by splitting them into
/// four 64-bit pieces and using the appropriate host functions.
fn encode_i256(
cfg: &mut ControlFlowGraph,
vartab: &mut Vartable,
lo_lo: Expression,
lo_hi: Expression,
hi_lo: Expression,
hi_hi: Expression,
int256_ty: Type,
) -> Expression {
let ret_var = vartab.temp_anonymous(&lo_lo.ty());
let ret = Expression::Variable {
loc: pt::Loc::Codegen,
ty: lo_lo.ty().clone(),
var_no: ret_var,
};
// For 256-bit integers, we always use the host functions since they can't fit in a 64-bit ScVal
let instr = match int256_ty {
Type::Int(256) => Instr::Call {
res: vec![ret_var],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::ObjFromI256Pieces.name().to_string(),
},
args: vec![hi_hi, hi_lo, lo_hi, lo_lo],
},
Type::Uint(256) => Instr::Call {
res: vec![ret_var],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::ObjFromU256Pieces.name().to_string(),
},
args: vec![hi_hi, hi_lo, lo_hi, lo_lo],
},
_ => unreachable!(),
};
cfg.add(vartab, instr);
ret
}
fn decode_i128(cfg: &mut ControlFlowGraph, vartab: &mut Vartable, arg: Expression) -> Expression {
let ty = match arg.ty() {
Type::Ref(inner_ty) => *inner_ty.clone(),
Type::SorobanHandle(inner_ty) => *inner_ty.clone(),
_ => arg.ty(),
};
let ret_var = vartab.temp_anonymous(&ty);
let ret = Expression::Variable {
loc: pt::Loc::Codegen,
ty: ty.clone(),
var_no: ret_var,
};
vartab.new_dirty_tracker();
let tag = extract_tag(arg.clone());
let val_in_host = cfg.new_basic_block("val_is_host".to_string());
let val_in_obj = cfg.new_basic_block("val_is_obj".to_string());
let return_block = cfg.new_basic_block("finish".to_string());
let predicate = match ty {
Type::Int(128) => 11,
Type::Uint(128) => 10,
_ => unreachable!(),
};
let is_in_obj = Expression::Equal {
loc: pt::Loc::Codegen,
left: tag.clone().into(),
right: Expression::NumberLiteral {
loc: pt::Loc::Codegen,
ty: Type::Uint(64),
value: BigInt::from(predicate),
}
.into(),
};
cfg.add(
vartab,
Instr::BranchCond {
cond: is_in_obj,
true_block: val_in_obj,
false_block: val_in_host,
},
);
cfg.set_basic_block(val_in_obj);
let value = Expression::ShiftRight {
loc: pt::Loc::Codegen,
ty: Type::Int(64),
left: arg.clone().into(),
right: Expression::NumberLiteral {
loc: pt::Loc::Codegen,
ty: Type::Int(64),
value: BigInt::from(8_u64),
}
.into(),
signed: false,
};
let extend = Expression::ZeroExt {
loc: Loc::Codegen,
ty: ty.clone(),
expr: Box::new(value.clone()),
};
let set_instr = Instr::Set {
loc: pt::Loc::Codegen,
res: ret_var,
expr: extend,
};
cfg.add(vartab, set_instr);
cfg.add(
vartab,
Instr::Branch {
block: return_block,
},
);
cfg.set_basic_block(val_in_host);
let low_var_no = vartab.temp_anonymous(&Type::Uint(64));
let low_var = Expression::Variable {
loc: pt::Loc::Codegen,
ty: Type::Uint(64),
var_no: low_var_no,
};
let get_lo_instr = match ty {
Type::Int(128) => Instr::Call {
res: vec![low_var_no],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::ObjToI128Lo64.name().to_string(),
},
args: vec![arg.clone()],
},
Type::Uint(128) => Instr::Call {
res: vec![low_var_no],
return_tys: vec![Type::Uint(64)],
call: InternalCallTy::HostFunction {
name: HostFunctions::ObjToU128Lo64.name().to_string(),
},
args: vec![arg.clone()],
},
_ => unreachable!(),
};
cfg.add(vartab, get_lo_instr);
let low_var = Expression::ZeroExt {
loc: Loc::Codegen,
ty: ty.clone(),
expr: Box::new(low_var),
};