-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathmod.rs
More file actions
1974 lines (1856 loc) · 66.1 KB
/
Copy pathmod.rs
File metadata and controls
1974 lines (1856 loc) · 66.1 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
mod array_boundary;
pub mod cfg;
mod constant_folding;
mod constructor;
mod dead_storage;
pub(crate) mod dispatch;
pub(crate) mod encoding;
mod events;
mod expression;
pub(super) mod polkadot;
mod reaching_definitions;
pub mod revert;
mod solana_accounts;
mod solana_deploy;
mod soroban;
mod statements;
mod storage;
mod strength_reduce;
pub(crate) mod subexpression_elimination;
mod tests;
mod undefined_variable;
mod unused_variable;
pub(crate) mod vartable;
mod vector_to_slice;
mod yul;
use self::{
cfg::{optimize_and_check_cfg, ControlFlowGraph, Instr},
dispatch::function_dispatch,
expression::expression,
solana_accounts::account_collection::collect_accounts_from_contract,
vartable::Vartable,
};
use crate::sema::ast::{
ArrayLength, FormatArg, Function, Layout, Namespace, RetrieveType, StringLocation, Type,
};
use crate::{sema::ast, Target};
use std::cmp::Ordering;
use crate::codegen::cfg::ASTFunction;
use crate::codegen::solana_accounts::account_management::manage_contract_accounts;
use crate::codegen::yul::generate_yul_function_cfg;
use crate::sema::diagnostics::Diagnostics;
use crate::sema::eval::eval_const_number;
use crate::sema::Recurse;
#[cfg(feature = "wasm_opt")]
use contract_build::OptimizationPasses;
use encoding::soroban_encoding::soroban_encode_arg;
use num_bigint::{BigInt, Sign};
use num_rational::BigRational;
use num_traits::{FromPrimitive, Zero};
use solang_parser::diagnostics::Diagnostic;
use solang_parser::{pt, pt::CodeLocation};
// The sizeof(struct account_data_header)
pub const SOLANA_FIRST_OFFSET: u64 = 16;
/// Name of the storage initializer function
pub const STORAGE_INITIALIZER: &str = "storage_initializer";
/// Maximum permitted size of account data (10 MiB).
/// https://github.com/solana-labs/solana/blob/08aba38d3507c8cb66f85074d8f1249d43e64a75/sdk/program/src/system_instruction.rs#L85
pub const MAXIMUM_ACCOUNT_SIZE: u64 = 10 * 1024 * 1024;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum OptimizationLevel {
None = 0,
Less = 1,
Default = 2,
Aggressive = 3,
}
#[cfg(feature = "llvm")]
impl From<OptimizationLevel> for inkwell::OptimizationLevel {
fn from(level: OptimizationLevel) -> Self {
match level {
OptimizationLevel::None => inkwell::OptimizationLevel::None,
OptimizationLevel::Less => inkwell::OptimizationLevel::Less,
OptimizationLevel::Default => inkwell::OptimizationLevel::Default,
OptimizationLevel::Aggressive => inkwell::OptimizationLevel::Aggressive,
}
}
}
#[cfg(feature = "llvm")]
impl From<inkwell::OptimizationLevel> for OptimizationLevel {
fn from(level: inkwell::OptimizationLevel) -> Self {
match level {
inkwell::OptimizationLevel::None => OptimizationLevel::None,
inkwell::OptimizationLevel::Less => OptimizationLevel::Less,
inkwell::OptimizationLevel::Default => OptimizationLevel::Default,
inkwell::OptimizationLevel::Aggressive => OptimizationLevel::Aggressive,
}
}
}
pub enum HostFunctions {
PutContractData,
GetContractData,
HasContractData,
DeleteContractData,
ExtendContractDataTtl,
ExtendCurrentContractInstanceAndCodeTtl,
LogFromLinearMemory,
SymbolNewFromLinearMemory,
VectorNew,
VectorNewFromLinearMemory,
VecUnpackToLinearMemory,
VecLen,
MapNewFromLinearMemory,
Call,
ObjToU64,
ObjFromU64,
ObjToI128Lo64,
ObjToI128Hi64,
ObjToU128Lo64,
ObjToU128Hi64,
ObjFromI128Pieces,
ObjFromU128Pieces,
ObjToU256LoLo,
ObjToU256LoHi,
ObjToU256HiLo,
ObjToU256HiHi,
ObjFromU256Pieces,
ObjToI256LoLo,
ObjToI256LoHi,
ObjToI256HiLo,
ObjToI256HiHi,
ObjFromI256Pieces,
RequireAuth,
AuthAsCurrContract,
MapNew,
MapPut,
VecPushBack,
VecPopBack,
VecGet,
VecPut,
StringNewFromLinearMemory,
StrKeyToAddr,
GetLedgerTimestamp,
GetCurrentContractAddress,
BytesNewFromLinearMemory,
BytesLen,
BytesCopyToLinearMemory,
BytesGet,
BytesPut,
}
impl HostFunctions {
pub fn name(&self) -> &str {
match self {
HostFunctions::PutContractData => "l._",
HostFunctions::GetContractData => "l.1",
HostFunctions::HasContractData => "l.0",
HostFunctions::DeleteContractData => "l.2",
HostFunctions::ExtendContractDataTtl => "l.7",
HostFunctions::ExtendCurrentContractInstanceAndCodeTtl => "l.8",
HostFunctions::LogFromLinearMemory => "x._",
HostFunctions::SymbolNewFromLinearMemory => "b.j",
HostFunctions::VectorNew => "v._",
HostFunctions::VectorNewFromLinearMemory => "v.g",
HostFunctions::VecUnpackToLinearMemory => "v.h",
HostFunctions::Call => "d._",
HostFunctions::ObjToU64 => "i.0",
HostFunctions::ObjFromU64 => "i._",
HostFunctions::ObjToI128Lo64 => "i.7",
HostFunctions::ObjToI128Hi64 => "i.8",
HostFunctions::ObjToU128Lo64 => "i.4",
HostFunctions::ObjToU128Hi64 => "i.5",
HostFunctions::ObjFromI128Pieces => "i.6",
HostFunctions::ObjFromU128Pieces => "i.3",
HostFunctions::ObjToU256LoLo => "i.f",
HostFunctions::ObjToU256LoHi => "i.e",
HostFunctions::ObjToU256HiLo => "i.d",
HostFunctions::ObjToU256HiHi => "i.c",
HostFunctions::ObjFromU256Pieces => "i.9",
HostFunctions::ObjToI256LoLo => "i.m",
HostFunctions::ObjToI256LoHi => "i.l",
HostFunctions::ObjToI256HiLo => "i.k",
HostFunctions::ObjToI256HiHi => "i.j",
HostFunctions::ObjFromI256Pieces => "i.g",
HostFunctions::RequireAuth => "a.0",
HostFunctions::AuthAsCurrContract => "a.3",
HostFunctions::MapNewFromLinearMemory => "m.9",
HostFunctions::MapNew => "m._",
HostFunctions::MapPut => "m.0",
HostFunctions::VecPushBack => "v.6",
HostFunctions::StringNewFromLinearMemory => "b.i",
HostFunctions::StrKeyToAddr => "a.1",
HostFunctions::GetLedgerTimestamp => "x.4",
HostFunctions::GetCurrentContractAddress => "x.7",
HostFunctions::BytesNewFromLinearMemory => "b.3",
HostFunctions::BytesLen => "b.8",
HostFunctions::BytesCopyToLinearMemory => "b.1",
HostFunctions::BytesGet => "b.6",
HostFunctions::BytesPut => "b.5",
HostFunctions::VecLen => "v.3",
HostFunctions::VecPopBack => "v.7",
HostFunctions::VecGet => "v.1",
HostFunctions::VecPut => "v.0",
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Options {
pub dead_storage: bool,
pub constant_folding: bool,
pub strength_reduce: bool,
pub vector_to_slice: bool,
pub common_subexpression_elimination: bool,
pub generate_debug_information: bool,
pub opt_level: OptimizationLevel,
pub log_runtime_errors: bool,
pub log_prints: bool,
pub strict_soroban_types: bool,
#[cfg(feature = "wasm_opt")]
pub wasm_opt: Option<OptimizationPasses>,
pub soroban_version: Option<u64>,
}
impl Default for Options {
fn default() -> Self {
Options {
dead_storage: true,
constant_folding: true,
strength_reduce: true,
vector_to_slice: true,
common_subexpression_elimination: true,
generate_debug_information: false,
opt_level: OptimizationLevel::Default,
log_runtime_errors: false,
log_prints: true,
strict_soroban_types: false,
#[cfg(feature = "wasm_opt")]
wasm_opt: None,
soroban_version: None,
}
}
}
/// The contracts are fully resolved but they do not have any CFGs which is needed for
/// the llvm code emitter. This will also do additional code checks.
pub fn codegen(ns: &mut Namespace, opt: &Options) {
if ns.diagnostics.any_errors() {
return;
}
let mut contracts_done = Vec::new();
contracts_done.resize(ns.contracts.len(), false);
// codegen all the contracts; some additional errors/warnings will be detected here
while contracts_done.iter().any(|e| !*e) {
for contract_no in 0..ns.contracts.len() {
if contracts_done[contract_no] {
continue;
}
if !ns.contracts[contract_no].instantiable {
contracts_done[contract_no] = true;
continue;
}
// does this contract create any contract which are not done
if ns.contracts[contract_no]
.creates
.iter()
.any(|c| !contracts_done[*c])
{
continue;
}
contract(contract_no, ns, opt);
if ns.diagnostics.any_errors() {
return;
}
contracts_done[contract_no] = true;
}
}
if ns.target == Target::Solana {
for contract_no in 0..ns.contracts.len() {
if ns.contracts[contract_no].instantiable {
let diag = collect_accounts_from_contract(contract_no, ns);
ns.diagnostics.extend(diag);
}
}
for contract_no in 0..ns.contracts.len() {
if ns.contracts[contract_no].instantiable {
manage_contract_accounts(contract_no, ns);
}
}
}
ns.diagnostics.sort_and_dedup();
}
fn contract(contract_no: usize, ns: &mut Namespace, opt: &Options) {
if !ns.diagnostics.any_errors() && ns.contracts[contract_no].instantiable {
layout(contract_no, ns);
let mut cfg_no = 0;
let mut all_cfg = Vec::new();
// all the functions should have a cfg_no assigned, so we can generate call instructions to the correct function
for (_, func_cfg) in ns.contracts[contract_no].all_functions.iter_mut() {
*func_cfg = cfg_no;
cfg_no += 1;
}
// create a cfg number for yul functions
for yul_fn_no in &ns.contracts[contract_no].yul_functions {
ns.yul_functions[*yul_fn_no].cfg_no = cfg_no;
cfg_no += 1;
}
all_cfg.resize(cfg_no, ControlFlowGraph::placeholder());
// clone all_functions so we can pass a mutable reference to generate_cfg
for (function_no, cfg_no) in ns.contracts[contract_no]
.all_functions
.iter()
.map(|(function_no, cfg_no)| (*function_no, *cfg_no))
.collect::<Vec<(usize, usize)>>()
.into_iter()
{
cfg::generate_cfg(
contract_no,
Some(function_no),
cfg_no,
&mut all_cfg,
ns,
opt,
)
}
// generate the cfg for yul functions
for yul_func_no in ns.contracts[contract_no].yul_functions.clone() {
generate_yul_function_cfg(contract_no, yul_func_no, &mut all_cfg, ns, opt);
}
// Generate cfg for storage initializers
let cfg = storage_initializer(contract_no, ns, opt);
let pos = all_cfg.len();
all_cfg.push(cfg);
ns.contracts[contract_no].initializer = Some(pos);
if ns.contracts[contract_no].constructors(ns).is_empty() {
// generate the default constructor
let func = ns.default_constructor(contract_no);
let cfg_no = all_cfg.len();
all_cfg.push(ControlFlowGraph::placeholder());
cfg::generate_cfg(contract_no, None, cfg_no, &mut all_cfg, ns, opt);
ns.contracts[contract_no].default_constructor = Some((func, cfg_no));
}
for mut dispatch_cfg in function_dispatch(contract_no, &mut all_cfg, ns, opt) {
optimize_and_check_cfg(&mut dispatch_cfg, ns, ASTFunction::None, opt);
all_cfg.push(dispatch_cfg);
}
ns.contracts[contract_no].cfg = all_cfg;
}
}
/// This function will set all contract storage initializers and should be called from the constructor
fn storage_initializer(contract_no: usize, ns: &mut Namespace, opt: &Options) -> ControlFlowGraph {
// note the single `:` to prevent a name clash with user-declared functions
let mut cfg = ControlFlowGraph::new(STORAGE_INITIALIZER.to_string(), ASTFunction::None);
let mut vartab = Vartable::new(ns.next_id);
for layout in &ns.contracts[contract_no].layout {
let var = &ns.contracts[layout.contract_no].variables[layout.var_no];
let soroban_init_with_vec = ns.target == Target::Soroban
&& match &var.ty {
Type::String | Type::DynamicBytes | Type::Slice(_) => true,
Type::Array(elem_ty, dims) if dims.last() == Some(&ArrayLength::Dynamic) => {
!elem_ty.is_reference_type(ns)
}
_ => false,
};
let mut value = if let Some(init) = &var.initializer {
expression(init, &mut cfg, contract_no, None, ns, &mut vartab, opt)
} else if soroban_init_with_vec {
soroban::soroban_vec_new(&var.loc, &var.ty, &mut cfg, &mut vartab)
} else {
continue;
};
let storage = ns.contracts[contract_no].get_storage_slot(
pt::Loc::Codegen,
layout.contract_no,
layout.var_no,
ns,
None,
);
//let mut value = expression(init, &mut cfg, contract_no, None, ns, &mut vartab, opt);
if ns.target == Target::Soroban {
value = soroban_encode_arg(value, &mut cfg, &mut vartab, ns);
}
cfg.add(
&mut vartab,
Instr::SetStorage {
value,
ty: var.ty.clone(),
storage,
storage_type: var.storage_type.clone(),
},
);
}
cfg.add(&mut vartab, Instr::Return { value: Vec::new() });
vartab.finalize(ns, &mut cfg);
optimize_and_check_cfg(&mut cfg, ns, ASTFunction::None, opt);
cfg
}
/// Layout the contract. We determine the layout of variables and deal with overriding variables
fn layout(contract_no: usize, ns: &mut Namespace) {
let mut slot = if ns.target == Target::Solana {
BigInt::from(SOLANA_FIRST_OFFSET)
} else {
BigInt::zero()
};
for base_contract_no in ns.contract_bases(contract_no) {
for var_no in 0..ns.contracts[base_contract_no].variables.len() {
if !ns.contracts[base_contract_no].variables[var_no].constant {
let ty = ns.contracts[base_contract_no].variables[var_no].ty.clone();
if ns.target == Target::Solana {
// elements need to be aligned on solana
let alignment = ty.align_of(ns);
let offset = slot.clone() % alignment;
if offset > BigInt::zero() {
slot += alignment - offset;
}
}
ns.contracts[contract_no].layout.push(Layout {
slot: slot.clone(),
contract_no: base_contract_no,
var_no,
ty: ty.clone(),
});
slot += ty.storage_slots(ns);
}
}
}
let constructors = ns.contracts[contract_no].constructors(ns);
if !constructors.is_empty() {
if let Some((_, exp)) = &ns.functions[constructors[0]].annotations.space {
// This code path is only reachable on Solana
assert_eq!(ns.target, Target::Solana);
if let Ok((_, value)) = eval_const_number(exp, ns, &mut Diagnostics::default()) {
if slot > value {
ns.diagnostics.push(Diagnostic::error(
exp.loc(),
format!("contract requires at least {slot} bytes of space"),
));
} else if value > BigInt::from(MAXIMUM_ACCOUNT_SIZE) {
ns.diagnostics.push(Diagnostic::error(
exp.loc(),
"Solana's runtime does not permit accounts larger than 10 MB".to_string(),
));
}
}
}
}
ns.contracts[contract_no].fixed_layout_size = slot;
}
trait LLVMName {
fn llvm_symbol(&self, ns: &Namespace) -> String;
}
impl LLVMName for Function {
/// Return a unique string for this function which is a valid llvm symbol
fn llvm_symbol(&self, ns: &Namespace) -> String {
let mut sig = self.id.name.to_owned();
if !self.params.is_empty() {
sig.push_str("__");
for (i, p) in self.params.iter().enumerate() {
if i > 0 {
sig.push('_');
}
sig.push_str(&p.ty.to_llvm_string(ns));
}
}
sig
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expression {
Add {
loc: pt::Loc,
ty: Type,
overflowing: bool,
left: Box<Expression>,
right: Box<Expression>,
},
AllocDynamicBytes {
loc: pt::Loc,
ty: Type,
size: Box<Expression>,
initializer: Option<Vec<u8>>,
},
ArrayLiteral {
loc: pt::Loc,
ty: Type,
dimensions: Vec<u32>,
values: Vec<Expression>,
},
BitwiseAnd {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
BitwiseOr {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
BitwiseXor {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
BoolLiteral {
loc: pt::Loc,
value: bool,
},
Builtin {
loc: pt::Loc,
tys: Vec<Type>,
kind: Builtin,
args: Vec<Expression>,
},
BytesCast {
loc: pt::Loc,
ty: Type,
from: Type,
expr: Box<Expression>,
},
BytesLiteral {
loc: pt::Loc,
ty: Type,
value: Vec<u8>,
},
Cast {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
BitwiseNot {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
ConstArrayLiteral {
loc: pt::Loc,
ty: Type,
dimensions: Vec<u32>,
values: Vec<Expression>,
},
UnsignedDivide {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
SignedDivide {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
Equal {
loc: pt::Loc,
left: Box<Expression>,
right: Box<Expression>,
},
FormatString {
loc: pt::Loc,
args: Vec<(FormatArg, Expression)>,
},
FunctionArg {
loc: pt::Loc,
ty: Type,
arg_no: usize,
},
GetRef {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
InternalFunctionCfg {
ty: Type,
cfg_no: usize,
},
Keccak256 {
loc: pt::Loc,
ty: Type,
exprs: Vec<Expression>,
},
Less {
loc: pt::Loc,
signed: bool,
left: Box<Expression>,
right: Box<Expression>,
},
LessEqual {
loc: pt::Loc,
signed: bool,
left: Box<Expression>,
right: Box<Expression>,
},
Load {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
UnsignedModulo {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
SignedModulo {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
More {
loc: pt::Loc,
signed: bool,
left: Box<Expression>,
right: Box<Expression>,
},
MoreEqual {
loc: pt::Loc,
signed: bool,
left: Box<Expression>,
right: Box<Expression>,
},
Multiply {
loc: pt::Loc,
ty: Type,
overflowing: bool,
left: Box<Expression>,
right: Box<Expression>,
},
Not {
loc: pt::Loc,
expr: Box<Expression>,
},
NotEqual {
loc: pt::Loc,
left: Box<Expression>,
right: Box<Expression>,
},
NumberLiteral {
loc: pt::Loc,
ty: Type,
value: BigInt,
},
Poison,
Power {
loc: pt::Loc,
ty: Type,
overflowing: bool,
base: Box<Expression>,
exp: Box<Expression>,
},
RationalNumberLiteral {
loc: pt::Loc,
ty: Type,
rational: BigRational,
},
ReturnData {
loc: pt::Loc,
},
SignExt {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
ShiftLeft {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
},
ShiftRight {
loc: pt::Loc,
ty: Type,
left: Box<Expression>,
right: Box<Expression>,
signed: bool,
},
StorageArrayLength {
loc: pt::Loc,
ty: Type,
array: Box<Expression>,
elem_ty: Type,
},
StringCompare {
loc: pt::Loc,
left: StringLocation<Expression>,
right: StringLocation<Expression>,
},
StructLiteral {
loc: pt::Loc,
ty: Type,
values: Vec<Expression>,
},
StructMember {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
member: usize,
},
Subscript {
loc: pt::Loc,
ty: Type,
array_ty: Type,
expr: Box<Expression>,
index: Box<Expression>,
},
Subtract {
loc: pt::Loc,
ty: Type,
overflowing: bool,
left: Box<Expression>,
right: Box<Expression>,
},
Trunc {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
Negate {
loc: pt::Loc,
ty: Type,
overflowing: bool,
expr: Box<Expression>,
},
Undefined {
ty: Type,
},
Variable {
loc: pt::Loc,
ty: Type,
var_no: usize,
},
ZeroExt {
loc: pt::Loc,
ty: Type,
expr: Box<Expression>,
},
AdvancePointer {
pointer: Box<Expression>,
bytes_offset: Box<Expression>,
},
VectorData {
pointer: Box<Expression>,
},
}
impl CodeLocation for Expression {
fn loc(&self) -> pt::Loc {
match self {
Expression::StorageArrayLength { loc, .. }
| Expression::Builtin { loc, .. }
| Expression::Cast { loc, .. }
| Expression::NumberLiteral { loc, .. }
| Expression::Keccak256 { loc, .. }
| Expression::MoreEqual { loc, .. }
| Expression::ReturnData { loc }
| Expression::Subscript { loc, .. }
| Expression::Trunc { loc, .. }
| Expression::Variable { loc, .. }
| Expression::SignExt { loc, .. }
| Expression::GetRef { loc, .. }
| Expression::Load { loc, .. }
| Expression::BytesLiteral { loc, .. }
| Expression::Add { loc, .. }
| Expression::Multiply { loc, .. }
| Expression::Subtract { loc, .. }
| Expression::FormatString { loc, .. }
| Expression::LessEqual { loc, .. }
| Expression::BoolLiteral { loc, .. }
| Expression::UnsignedDivide { loc, .. }
| Expression::SignedDivide { loc, .. }
| Expression::UnsignedModulo { loc, .. }
| Expression::SignedModulo { loc, .. }
| Expression::Power { loc, .. }
| Expression::BitwiseOr { loc, .. }
| Expression::BitwiseAnd { loc, .. }
| Expression::BitwiseXor { loc, .. }
| Expression::Equal { loc, .. }
| Expression::NotEqual { loc, .. }
| Expression::BitwiseNot { loc, .. }
| Expression::Negate { loc, .. }
| Expression::Less { loc, .. }
| Expression::Not { loc, .. }
| Expression::StructLiteral { loc, .. }
| Expression::ArrayLiteral { loc, .. }
| Expression::ConstArrayLiteral { loc, .. }
| Expression::StructMember { loc, .. }
| Expression::StringCompare { loc, .. }
| Expression::FunctionArg { loc, .. }
| Expression::ShiftRight { loc, .. }
| Expression::ShiftLeft { loc, .. }
| Expression::RationalNumberLiteral { loc, .. }
| Expression::AllocDynamicBytes { loc, .. }
| Expression::BytesCast { loc, .. }
| Expression::More { loc, .. }
| Expression::ZeroExt { loc, .. } => *loc,
Expression::InternalFunctionCfg { .. }
| Expression::Poison
| Expression::Undefined { .. }
| Expression::AdvancePointer { .. }
| Expression::VectorData { .. } => pt::Loc::Codegen,
}
}
}
impl Recurse for Expression {
type ArgType = Expression;
fn recurse<T>(&self, cx: &mut T, f: fn(expr: &Expression, ctx: &mut T) -> bool) {
if !f(self, cx) {
return;
}
match self {
Expression::BitwiseAnd { left, right, .. }
| Expression::BitwiseOr { left, right, .. }
| Expression::UnsignedDivide { left, right, .. }
| Expression::SignedDivide { left, right, .. }
| Expression::Equal { left, right, .. }
| Expression::Less { left, right, .. }
| Expression::LessEqual { left, right, .. }
| Expression::BitwiseXor { left, right, .. }
| Expression::More { left, right, .. }
| Expression::MoreEqual { left, right, .. }
| Expression::Multiply { left, right, .. }
| Expression::NotEqual { left, right, .. }
| Expression::ShiftLeft { left, right, .. }
| Expression::ShiftRight { left, right, .. }
| Expression::Power {
base: left,
exp: right,
..
}
| Expression::Subscript {
expr: left,
index: right,
..
}
| Expression::Subtract { left, right, .. }
| Expression::AdvancePointer {
pointer: left,
bytes_offset: right,
..
}
| Expression::Add { left, right, .. } => {
left.recurse(cx, f);
right.recurse(cx, f);
}
Expression::BytesCast { expr, .. }
| Expression::Cast { expr, .. }
| Expression::GetRef { expr, .. }
| Expression::Not { expr, .. }
| Expression::Trunc { expr, .. }
| Expression::Negate { expr, .. }
| Expression::ZeroExt { expr, .. }
| Expression::SignExt { expr, .. }
| Expression::BitwiseNot { expr, .. }
| Expression::Load { expr, .. }
| Expression::StorageArrayLength { array: expr, .. }
| Expression::StructMember { expr, .. }
| Expression::AllocDynamicBytes { size: expr, .. } => {
expr.recurse(cx, f);
}
Expression::Builtin { args, .. }
| Expression::ConstArrayLiteral { values: args, .. }
| Expression::Keccak256 { exprs: args, .. }
| Expression::StructLiteral { values: args, .. }
| Expression::ArrayLiteral { values: args, .. } => {
for item in args {
item.recurse(cx, f);
}
}
Expression::FormatString { args, .. } => {
for item in args {
item.1.recurse(cx, f);
}
}
Expression::StringCompare { left, right, .. } => {
if let StringLocation::RunTime(exp) = left {
exp.recurse(cx, f);
}
if let StringLocation::RunTime(exp) = right {
exp.recurse(cx, f);
}
}
_ => (),
}
}
}
impl RetrieveType for Expression {
fn ty(&self) -> Type {
match self {
Expression::ReturnData { loc: _ } => Type::DynamicBytes,
Expression::Builtin { tys, .. } => {
assert_eq!(tys.len(), 1);
tys[0].clone()
}
Expression::Keccak256 { ty, .. }
| Expression::Undefined { ty }
| Expression::Variable { ty, .. }
| Expression::Trunc { ty, .. }
| Expression::ZeroExt { ty, .. }
| Expression::Cast { ty, .. }
| Expression::SignExt { ty, .. }
| Expression::GetRef { ty, .. }
| Expression::Load { ty, .. }
| Expression::BytesLiteral { ty, .. }
| Expression::Add { ty, .. }
| Expression::NumberLiteral { ty, .. }
| Expression::Multiply { ty, .. }
| Expression::Subtract { ty, .. }
| Expression::SignedDivide { ty, .. }
| Expression::UnsignedDivide { ty, .. }
| Expression::SignedModulo { ty, .. }
| Expression::UnsignedModulo { ty, .. }
| Expression::Power { ty, .. }
| Expression::BitwiseOr { ty, .. }
| Expression::BitwiseAnd { ty, .. }
| Expression::BitwiseXor { ty, .. }
| Expression::ShiftLeft { ty, .. }
| Expression::ShiftRight { ty, .. }
| Expression::BitwiseNot { ty, .. }
| Expression::StorageArrayLength { ty, .. }
| Expression::Negate { ty, .. }
| Expression::StructLiteral { ty, .. }
| Expression::ArrayLiteral { ty, .. }
| Expression::ConstArrayLiteral { ty, .. }
| Expression::StructMember { ty, .. }
| Expression::FunctionArg { ty, .. }
| Expression::AllocDynamicBytes { ty, .. }
| Expression::BytesCast { ty, .. }
| Expression::RationalNumberLiteral { ty, .. }
| Expression::Subscript { ty, .. }
| Expression::InternalFunctionCfg { ty, .. } => ty.clone(),
Expression::BoolLiteral { .. }
| Expression::MoreEqual { .. }
| Expression::More { .. }
| Expression::Not { .. }
| Expression::NotEqual { .. }
| Expression::Less { .. }
| Expression::Equal { .. }