-
-
Notifications
You must be signed in to change notification settings - Fork 843
/
Copy pathcore.py
1237 lines (943 loc) · 42.7 KB
/
core.py
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
import contextlib
from typing import Generator
from vyper import ast as vy_ast
from vyper.codegen.ir_node import Encoding, IRnode
from vyper.compiler.settings import OptimizationLevel
from vyper.evm.address_space import CALLDATA, DATA, IMMUTABLES, MEMORY, STORAGE, TRANSIENT
from vyper.evm.opcodes import version_check
from vyper.exceptions import CompilerPanic, StructureException, TypeCheckFailure, TypeMismatch
from vyper.semantics.types import (
AddressT,
BoolT,
BytesM_T,
BytesT,
DArrayT,
DecimalT,
HashMapT,
IntegerT,
InterfaceT,
StructT,
TupleT,
_BytestringT,
)
from vyper.semantics.types.shortcuts import BYTES32_T, INT256_T, UINT256_T
from vyper.semantics.types.subscriptable import SArrayT
from vyper.semantics.types.user import FlagT
from vyper.utils import GAS_COPY_WORD, GAS_IDENTITY, GAS_IDENTITYWORD, ceil32
DYNAMIC_ARRAY_OVERHEAD = 1
def is_bytes_m_type(typ):
return isinstance(typ, BytesM_T)
def is_numeric_type(typ):
return isinstance(typ, (IntegerT, DecimalT))
def is_integer_type(typ):
return isinstance(typ, IntegerT)
def is_decimal_type(typ):
return isinstance(typ, DecimalT)
def is_flag_type(typ):
return isinstance(typ, FlagT)
def is_tuple_like(typ):
# A lot of code paths treat tuples and structs similarly
# so we have a convenience function to detect it
ret = isinstance(typ, (TupleT, StructT))
assert ret == hasattr(typ, "tuple_items")
return ret
def is_array_like(typ):
# For convenience static and dynamic arrays share some code paths
ret = isinstance(typ, (DArrayT, SArrayT))
assert ret == typ._is_array_type
return ret
def get_type_for_exact_size(n_bytes):
"""Create a type which will take up exactly n_bytes. Used for allocating internal buffers.
Parameters:
n_bytes: the number of bytes to allocate
Returns:
type: A type which can be passed to context.new_variable
"""
return BytesT(n_bytes - 32 * DYNAMIC_ARRAY_OVERHEAD)
# propagate revert message when calls to external contracts fail
def check_external_call(call_ir):
copy_revertdata = ["returndatacopy", 0, 0, "returndatasize"]
revert = IRnode.from_list(["revert", 0, "returndatasize"], error_msg="external call failed")
propagate_revert_ir = ["seq", copy_revertdata, revert]
return ["if", ["iszero", call_ir], propagate_revert_ir]
# cost per byte of the identity precompile
def _identity_gas_bound(num_bytes):
return GAS_IDENTITY + GAS_IDENTITYWORD * (ceil32(num_bytes) // 32)
def _mcopy_gas_bound(num_bytes):
return GAS_COPY_WORD * ceil32(num_bytes) // 32
def _calldatacopy_gas_bound(num_bytes):
return GAS_COPY_WORD * ceil32(num_bytes) // 32
def _codecopy_gas_bound(num_bytes):
return GAS_COPY_WORD * ceil32(num_bytes) // 32
# Copy byte array word-for-word (including layout)
# TODO make this a private function
def make_byte_array_copier(dst, src):
assert isinstance(src.typ, _BytestringT)
assert isinstance(dst.typ, _BytestringT)
_check_assign_bytes(dst, src)
# TODO: remove this branch, copy_bytes and get_bytearray_length should handle
if src.value == "~empty" or src.typ.maxlen == 0:
# set length word to 0.
return STORE(dst, 0)
with src.cache_when_complex("src") as (b1, src):
has_storage = STORAGE in (src.location, dst.location)
is_memory_copy = dst.location == src.location == MEMORY
batch_uses_identity = is_memory_copy and not version_check(begin="cancun")
if src.typ.maxlen <= 32 and (has_storage or batch_uses_identity):
# it's cheaper to run two load/stores instead of copy_bytes
ret = ["seq"]
# store length word
len_ = get_bytearray_length(src)
ret.append(STORE(dst, len_))
# store the single data word.
dst_data_ptr = bytes_data_ptr(dst)
src_data_ptr = bytes_data_ptr(src)
ret.append(STORE(dst_data_ptr, LOAD(src_data_ptr)))
return b1.resolve(ret)
# batch copy the bytearray (including length word) using copy_bytes
len_ = add_ofst(get_bytearray_length(src), 32)
max_bytes = src.typ.maxlen + 32
ret = copy_bytes(dst, src, len_, max_bytes)
return b1.resolve(ret)
def bytes_data_ptr(ptr):
if ptr.location is None:
raise CompilerPanic("tried to modify non-pointer type")
assert isinstance(ptr.typ, _BytestringT)
return add_ofst(ptr, ptr.location.word_scale)
def dynarray_data_ptr(ptr):
if ptr.location is None:
raise CompilerPanic("tried to modify non-pointer type")
assert isinstance(ptr.typ, DArrayT)
return add_ofst(ptr, ptr.location.word_scale)
def _dynarray_make_setter(dst, src):
assert isinstance(src.typ, DArrayT)
assert isinstance(dst.typ, DArrayT)
if src.value == "~empty":
return IRnode.from_list(STORE(dst, 0))
# copy contents of src dynarray to dst.
# note that in case src and dst refer to the same dynarray,
# in order for get_element_ptr oob checks on the src dynarray
# to work, we need to wait until after the data is copied
# before we clobber the length word.
if src.value == "multi":
ret = ["seq"]
# handle literals
# copy each item
n_items = len(src.args)
for i in range(n_items):
k = IRnode.from_list(i, typ=UINT256_T)
dst_i = get_element_ptr(dst, k, array_bounds_check=False)
src_i = get_element_ptr(src, k, array_bounds_check=False)
ret.append(make_setter(dst_i, src_i))
# write the length word after data is copied
store_length = STORE(dst, n_items)
ann = None
if src.annotation is not None:
ann = f"len({src.annotation})"
store_length = IRnode.from_list(store_length, annotation=ann)
ret.append(store_length)
return ret
with src.cache_when_complex("darray_src") as (b1, src):
# for ABI-encoded dynamic data, we must loop to unpack, since
# the layout does not match our memory layout
should_loop = src.encoding == Encoding.ABI and src.typ.value_type.abi_type.is_dynamic()
# if the data is not validated, we must loop to unpack
should_loop |= needs_clamp(src.typ.value_type, src.encoding)
# performance: if the subtype is dynamic, there might be a lot
# of unused space inside of each element. for instance
# DynArray[DynArray[uint256, 100], 5] where all the child
# arrays are empty - for this case, we recursively call
# into make_setter instead of straight bytes copy
# TODO we can make this heuristic more precise, e.g.
# loop when subtype.is_dynamic AND location == storage
# OR array_size <= /bound where loop is cheaper than memcpy/
should_loop |= src.typ.value_type.abi_type.is_dynamic()
with get_dyn_array_count(src).cache_when_complex("darray_count") as (b2, count):
ret = ["seq"]
if should_loop:
i = IRnode.from_list(_freshname("copy_darray_ix"), typ=UINT256_T)
loop_body = make_setter(
get_element_ptr(dst, i, array_bounds_check=False),
get_element_ptr(src, i, array_bounds_check=False),
)
loop_body.annotation = f"{dst}[i] = {src}[i]"
ret.append(["repeat", i, 0, count, src.typ.count, loop_body])
# write the length word after data is copied
ret.append(STORE(dst, count))
else:
element_size = src.typ.value_type.memory_bytes_required
# number of elements * size of element in bytes + length word
n_bytes = add_ofst(_mul(count, element_size), 32)
max_bytes = 32 + src.typ.count * element_size
# batch copy the entire dynarray, including length word
ret.append(copy_bytes(dst, src, n_bytes, max_bytes))
return b1.resolve(b2.resolve(ret))
# Copy bytes
# Accepts 4 arguments:
# (i) an IR node for the start position of the source
# (ii) an IR node for the start position of the destination
# (iii) an IR node for the length (in bytes)
# (iv) a constant for the max length (in bytes)
# NOTE: may pad to ceil32 of `length`! If you ask to copy 1 byte, it may
# copy an entire (32-byte) word, depending on the copy routine chosen.
# TODO maybe always pad to ceil32, to reduce dirty bytes bugs
def copy_bytes(dst, src, length, length_bound):
annotation = f"copy up to {length_bound} bytes from {src} to {dst}"
src = IRnode.from_list(src)
dst = IRnode.from_list(dst)
length = IRnode.from_list(length)
with src.cache_when_complex("src") as (b1, src), length.cache_when_complex(
"copy_bytes_count"
) as (b2, length), dst.cache_when_complex("dst") as (b3, dst):
assert isinstance(length_bound, int) and length_bound >= 0
# correctness: do not clobber dst
if length_bound == 0:
return IRnode.from_list(["seq"], annotation=annotation)
# performance: if we know that length is 0, do not copy anything
if length.value == 0:
return IRnode.from_list(["seq"], annotation=annotation)
assert src.is_pointer and dst.is_pointer
# fast code for common case where num bytes is small
if length_bound <= 32:
copy_op = STORE(dst, LOAD(src))
ret = IRnode.from_list(copy_op, annotation=annotation)
return b1.resolve(b2.resolve(b3.resolve(ret)))
if dst.location == MEMORY and src.location in (MEMORY, CALLDATA, DATA):
# special cases: batch copy to memory
# TODO: iloadbytes
if src.location == MEMORY:
if version_check(begin="cancun"):
copy_op = ["mcopy", dst, src, length]
gas_bound = _mcopy_gas_bound(length_bound)
else:
copy_op = ["staticcall", "gas", 4, src, length, dst, length]
gas_bound = _identity_gas_bound(length_bound)
elif src.location == CALLDATA:
copy_op = ["calldatacopy", dst, src, length]
gas_bound = _calldatacopy_gas_bound(length_bound)
elif src.location == DATA:
copy_op = ["dloadbytes", dst, src, length]
# note: dloadbytes compiles to CODECOPY
gas_bound = _codecopy_gas_bound(length_bound)
ret = IRnode.from_list(copy_op, annotation=annotation, add_gas_estimate=gas_bound)
return b1.resolve(b2.resolve(b3.resolve(ret)))
if dst.location == IMMUTABLES and src.location in (MEMORY, DATA):
# TODO istorebytes-from-mem, istorebytes-from-calldata(?)
# compile to identity, CODECOPY respectively.
pass
# general case, copy word-for-word
# pseudocode for our approach (memory-storage as example):
# for i in range(len, bound=MAX_LEN):
# sstore(_dst + i, mload(src + i * 32))
i = IRnode.from_list(_freshname("copy_bytes_ix"), typ=UINT256_T)
# optimized form of (div (ceil32 len) 32)
n = ["div", ["add", 31, length], 32]
n_bound = ceil32(length_bound) // 32
dst_i = add_ofst(dst, _mul(i, dst.location.word_scale))
src_i = add_ofst(src, _mul(i, src.location.word_scale))
copy_one_word = STORE(dst_i, LOAD(src_i))
main_loop = ["repeat", i, 0, n, n_bound, copy_one_word]
return b1.resolve(
b2.resolve(b3.resolve(IRnode.from_list(main_loop, annotation=annotation)))
)
# get the number of bytes at runtime
def get_bytearray_length(arg):
typ = UINT256_T
# TODO: it would be nice to merge the implementations of get_bytearray_length and
# get_dynarray_count
if arg.value == "~empty":
return IRnode.from_list(0, typ=typ)
return IRnode.from_list(LOAD(arg), typ=typ)
# get the number of elements at runtime
def get_dyn_array_count(arg):
assert isinstance(arg.typ, DArrayT)
typ = UINT256_T
if arg.value == "multi":
return IRnode.from_list(len(arg.args), typ=typ)
if arg.value == "~empty":
# empty(DynArray[...])
return IRnode.from_list(0, typ=typ)
return IRnode.from_list(LOAD(arg), typ=typ)
def append_dyn_array(darray_node, elem_node):
assert isinstance(darray_node.typ, DArrayT)
assert darray_node.typ.count > 0, "jerk boy u r out"
ret = ["seq"]
with darray_node.cache_when_complex("darray") as (b1, darray_node):
len_ = get_dyn_array_count(darray_node)
with len_.cache_when_complex("old_darray_len") as (b2, len_):
assertion = ["assert", ["lt", len_, darray_node.typ.count]]
ret.append(IRnode.from_list(assertion, error_msg=f"{darray_node.typ} bounds check"))
# NOTE: typechecks elem_node
# NOTE skip array bounds check bc we already asserted len two lines up
ret.append(
make_setter(get_element_ptr(darray_node, len_, array_bounds_check=False), elem_node)
)
# store new length
ret.append(STORE(darray_node, ["add", len_, 1]))
return IRnode.from_list(b1.resolve(b2.resolve(ret)))
def pop_dyn_array(darray_node, return_popped_item):
assert isinstance(darray_node.typ, DArrayT)
assert darray_node.encoding == Encoding.VYPER
ret = ["seq"]
with darray_node.cache_when_complex("darray") as (b1, darray_node):
old_len = clamp("gt", get_dyn_array_count(darray_node), 0)
new_len = IRnode.from_list(["sub", old_len, 1], typ=UINT256_T)
with new_len.cache_when_complex("new_len") as (b2, new_len):
# store new length
ret.append(STORE(darray_node, new_len))
# NOTE skip array bounds check bc we already asserted len two lines up
if return_popped_item:
popped_item = get_element_ptr(darray_node, new_len, array_bounds_check=False)
ret.append(popped_item)
typ = popped_item.typ
location = popped_item.location
else:
typ, location = None, None
return IRnode.from_list(b1.resolve(b2.resolve(ret)), typ=typ, location=location)
def getpos(node):
return (
node.lineno,
node.col_offset,
getattr(node, "end_lineno", None),
getattr(node, "end_col_offset", None),
)
# add an offset to a pointer, keeping location and encoding info
def add_ofst(ptr, ofst):
ret = ["add", ptr, ofst]
return IRnode.from_list(ret, location=ptr.location, encoding=ptr.encoding)
# shorthand util
def _mul(x, y):
ret = ["mul", x, y]
return IRnode.from_list(ret)
# Resolve pointer locations for ABI-encoded data
def _getelemptr_abi_helper(parent, member_t, ofst, clamp=True):
member_abi_t = member_t.abi_type
# ABI encoding has length word and then pretends length is not there
# e.g. [[1,2]] is encoded as 0x01 <len> 0x20 <inner array ofst> <encode(inner array)>
# note that inner array ofst is 0x20, not 0x40.
if has_length_word(parent.typ):
parent = add_ofst(parent, parent.location.word_scale * DYNAMIC_ARRAY_OVERHEAD)
ofst_ir = add_ofst(parent, ofst)
if member_abi_t.is_dynamic():
# double dereference, according to ABI spec
# TODO optimize special case: first dynamic item
# offset is statically known.
ofst_ir = add_ofst(parent, unwrap_location(ofst_ir))
return IRnode.from_list(
ofst_ir,
typ=member_t,
location=parent.location,
encoding=parent.encoding,
annotation=f"{parent}{ofst}",
)
# TODO simplify this code, especially the ABI decoding
def _get_element_ptr_tuplelike(parent, key):
typ = parent.typ
assert is_tuple_like(typ)
if isinstance(typ, StructT):
assert isinstance(key, str)
subtype = typ.member_types[key]
attrs = list(typ.tuple_keys())
index = attrs.index(key)
annotation = key
else:
# TupleT
assert isinstance(key, int)
subtype = typ.member_types[key]
attrs = list(typ.tuple_keys())
index = key
annotation = None
# generated by empty() + make_setter
if parent.value == "~empty":
return IRnode.from_list("~empty", typ=subtype)
if parent.value == "multi":
assert parent.encoding != Encoding.ABI, "no abi-encoded literals"
return parent.args[index]
ofst = 0 # offset from parent start
if parent.encoding == Encoding.ABI:
if parent.location == STORAGE:
raise CompilerPanic("storage variables should not be abi encoded") # pragma: notest
member_t = typ.member_types[attrs[index]]
for i in range(index):
member_abi_t = typ.member_types[attrs[i]].abi_type
ofst += member_abi_t.embedded_static_size()
return _getelemptr_abi_helper(parent, member_t, ofst)
if parent.location.word_addressable:
for i in range(index):
ofst += typ.member_types[attrs[i]].storage_size_in_words
elif parent.location.byte_addressable:
for i in range(index):
ofst += typ.member_types[attrs[i]].memory_bytes_required
else:
raise CompilerPanic(f"bad location {parent.location}") # pragma: notest
return IRnode.from_list(
add_ofst(parent, ofst),
typ=subtype,
location=parent.location,
encoding=parent.encoding,
annotation=annotation,
)
def has_length_word(typ):
# Consider moving this to an attribute on typ
return isinstance(typ, (DArrayT, _BytestringT))
# TODO simplify this code, especially the ABI decoding
def _get_element_ptr_array(parent, key, array_bounds_check):
assert is_array_like(parent.typ)
if not is_integer_type(key.typ):
raise TypeCheckFailure(f"{key.typ} used as array index")
subtype = parent.typ.value_type
if parent.value == "~empty":
if array_bounds_check:
# this case was previously missing a bounds check. codegen
# is a bit complicated when bounds check is required, so
# block it. there is no reason to index into a literal empty
# array anyways!
raise TypeCheckFailure("indexing into zero array not allowed")
return IRnode.from_list("~empty", subtype)
if parent.value == "multi":
assert isinstance(key.value, int)
return parent.args[key.value]
ix = unwrap_location(key)
if array_bounds_check:
is_darray = isinstance(parent.typ, DArrayT)
bound = get_dyn_array_count(parent) if is_darray else parent.typ.count
# uclamplt works, even for signed ints. since two's-complement
# is used, if the index is negative, (unsigned) LT will interpret
# it as a very large number, larger than any practical value for
# an array index, and the clamp will throw an error.
# NOTE: there are optimization rules for this when ix or bound is literal
ix = clamp("lt", ix, bound)
ix.set_error_msg(f"{parent.typ} bounds check")
if parent.encoding == Encoding.ABI:
if parent.location == STORAGE:
raise CompilerPanic("storage variables should not be abi encoded") # pragma: notest
member_abi_t = subtype.abi_type
ofst = _mul(ix, member_abi_t.embedded_static_size())
return _getelemptr_abi_helper(parent, subtype, ofst)
if parent.location.word_addressable:
element_size = subtype.storage_size_in_words
elif parent.location.byte_addressable:
element_size = subtype.memory_bytes_required
else:
raise CompilerPanic("unreachable") # pragma: notest
ofst = _mul(ix, element_size)
if has_length_word(parent.typ):
data_ptr = add_ofst(parent, parent.location.word_scale * DYNAMIC_ARRAY_OVERHEAD)
else:
data_ptr = parent
return IRnode.from_list(add_ofst(data_ptr, ofst), typ=subtype, location=parent.location)
def _get_element_ptr_mapping(parent, key):
assert isinstance(parent.typ, HashMapT)
subtype = parent.typ.value_type
key = unwrap_location(key)
# TODO when is key None?
if key is None or parent.location not in (STORAGE, TRANSIENT):
raise TypeCheckFailure("bad dereference on mapping {parent}[{key}]")
return IRnode.from_list(["sha3_64", parent, key], typ=subtype, location=parent.location)
# Take a value representing a memory or storage location, and descend down to
# an element or member variable
# This is analogous (but not necessarily equivalent to) getelementptr in LLVM.
def get_element_ptr(parent, key, array_bounds_check=True):
with parent.cache_when_complex("val") as (b, parent):
typ = parent.typ
if is_tuple_like(typ):
ret = _get_element_ptr_tuplelike(parent, key)
elif isinstance(typ, HashMapT):
ret = _get_element_ptr_mapping(parent, key)
elif is_array_like(typ):
ret = _get_element_ptr_array(parent, key, array_bounds_check)
else:
raise CompilerPanic(f"get_element_ptr cannot be called on {typ}") # pragma: notest
return b.resolve(ret)
def LOAD(ptr: IRnode) -> IRnode:
if ptr.location is None:
raise CompilerPanic("cannot dereference non-pointer type")
op = ptr.location.load_op
if op is None:
raise CompilerPanic(f"unreachable {ptr.location}") # pragma: notest
return IRnode.from_list([op, ptr])
def eval_once_check(name):
# an IRnode which enforces uniqueness. include with a side-effecting
# operation to sanity check that the codegen pipeline only generates
# the side-effecting operation once (otherwise, IR-to-assembly will
# throw a duplicate label exception). there is no runtime overhead
# since the jumpdest gets optimized out in the final stage of assembly.
return IRnode.from_list(["unique_symbol", name])
def STORE(ptr: IRnode, val: IRnode) -> IRnode:
if ptr.location is None:
raise CompilerPanic("cannot dereference non-pointer type")
op = ptr.location.store_op
if op is None:
raise CompilerPanic(f"unreachable {ptr.location}") # pragma: notest
_check = _freshname(f"{op}_")
store = [op, ptr, val]
# don't use eval_once_check for memory, immutables because it interferes
# with optimizer
if ptr.location in (MEMORY, IMMUTABLES):
return IRnode.from_list(store)
return IRnode.from_list(["seq", eval_once_check(_check), store])
# Unwrap location
def unwrap_location(orig):
if orig.location is not None:
return IRnode.from_list(LOAD(orig), typ=orig.typ)
else:
# CMC 2022-03-24 TODO refactor so this branch can be removed
if orig.value == "~empty":
# must be word type
return IRnode.from_list(0, typ=orig.typ)
return orig
# utility function, constructs an IR tuple out of a list of IR nodes
def ir_tuple_from_args(args):
typ = TupleT([x.typ for x in args])
return IRnode.from_list(["multi"] + [x for x in args], typ=typ)
def needs_external_call_wrap(typ):
# for calls to ABI conforming contracts.
# according to the ABI spec, return types are ALWAYS tuples even
# if only one element is being returned.
# https://solidity.readthedocs.io/en/latest/abi-spec.html#function-selector-and-argument-encoding
# "and the return values v_1, ..., v_k of f are encoded as
#
# enc((v_1, ..., v_k))
# i.e. the values are combined into a tuple and encoded.
# "
# therefore, wrap it in a tuple if it's not already a tuple.
# for example, `bytes` is returned as abi-encoded (bytes,)
# and `(bytes,)` is returned as abi-encoded ((bytes,),)
# In general `-> X` gets returned as (X,)
# including structs. MyStruct is returned as abi-encoded (MyStruct,).
# (Sorry this is so confusing. I didn't make these rules.)
return not (isinstance(typ, TupleT) and typ.length > 1)
def calculate_type_for_external_return(typ):
if needs_external_call_wrap(typ):
return TupleT([typ])
return typ
def wrap_value_for_external_return(ir_val):
# used for LHS promotion
if needs_external_call_wrap(ir_val.typ):
return ir_tuple_from_args([ir_val])
else:
return ir_val
def set_type_for_external_return(ir_val):
# used for RHS promotion
ir_val.typ = calculate_type_for_external_return(ir_val.typ)
# return a dummy IRnode with the given type
def dummy_node_for_type(typ):
return IRnode("fake_node", typ=typ)
def _check_assign_bytes(left, right):
if right.typ.maxlen > left.typ.maxlen:
raise TypeMismatch(f"Cannot cast from {right.typ} to {left.typ}") # pragma: notest
# stricter check for zeroing a byte array.
if right.value == "~empty" and right.typ.maxlen != left.typ.maxlen:
raise TypeMismatch(f"Cannot cast from empty({right.typ}) to {left.typ}") # pragma: notest
def _check_assign_list(left, right):
def FAIL(): # pragma: no cover
raise TypeCheckFailure(f"assigning {right.typ} to {left.typ}")
if left.value == "multi":
# Cannot do something like [a, b, c] = [1, 2, 3]
FAIL() # pragma: notest
if isinstance(left.typ, SArrayT):
if not is_array_like(right.typ):
FAIL() # pragma: notest
if left.typ.count != right.typ.count:
FAIL() # pragma: notest
# TODO recurse into left, right if literals?
check_assign(
dummy_node_for_type(left.typ.value_type), dummy_node_for_type(right.typ.value_type)
)
if isinstance(left.typ, DArrayT):
if not isinstance(right.typ, DArrayT):
FAIL() # pragma: notest
if left.typ.count < right.typ.count:
FAIL() # pragma: notest
# stricter check for zeroing
if right.value == "~empty" and right.typ.count != left.typ.count:
raise TypeCheckFailure(
f"Bad type for clearing bytes: expected {left.typ} but got {right.typ}"
) # pragma: notest
# TODO recurse into left, right if literals?
check_assign(
dummy_node_for_type(left.typ.value_type), dummy_node_for_type(right.typ.value_type)
)
def _check_assign_tuple(left, right):
def FAIL(): # pragma: no cover
raise TypeCheckFailure(f"assigning {right.typ} to {left.typ}")
if not isinstance(right.typ, left.typ.__class__):
FAIL() # pragma: notest
if isinstance(left.typ, StructT):
for k in left.typ.member_types:
if k not in right.typ.member_types:
FAIL() # pragma: notest
# TODO recurse into left, right if literals?
check_assign(
dummy_node_for_type(left.typ.member_types[k]),
dummy_node_for_type(right.typ.member_types[k]),
)
for k in right.typ.member_types:
if k not in left.typ.member_types:
FAIL() # pragma: notest
if left.typ.name != right.typ.name:
FAIL() # pragma: notest
else:
if len(left.typ.member_types) != len(right.typ.member_types):
FAIL() # pragma: notest
for left_, right_ in zip(left.typ.member_types, right.typ.member_types):
# TODO recurse into left, right if literals?
check_assign(dummy_node_for_type(left_), dummy_node_for_type(right_))
# sanity check an assignment
# typechecking source code is done at an earlier phase
# this function is more of a sanity check for typechecking internally
# generated assignments
# TODO: do we still need this?
def check_assign(left, right):
def FAIL(): # pragma: no cover
raise TypeCheckFailure(f"assigning {right.typ} to {left.typ} {left} {right}")
if isinstance(left.typ, _BytestringT):
_check_assign_bytes(left, right)
elif is_array_like(left.typ):
_check_assign_list(left, right)
elif is_tuple_like(left.typ):
_check_assign_tuple(left, right)
elif left.typ._is_prim_word:
# TODO once we propagate types from typechecker, introduce this check:
# if left.typ != right.typ:
# FAIL() # pragma: notest
pass
else: # pragma: no cover
FAIL()
_label = 0
# TODO might want to coalesce with Context.fresh_varname and compile_ir.mksymbol
def _freshname(name):
global _label
_label += 1
return f"{name}{_label}"
def reset_names():
global _label
_label = 0
# returns True if t is ABI encoded and is a type that needs any kind of
# validation
def needs_clamp(t, encoding):
if encoding == Encoding.VYPER:
return False
if encoding != Encoding.ABI:
raise CompilerPanic("unreachable") # pragma: notest
if isinstance(t, (_BytestringT, DArrayT)):
return True
if isinstance(t, FlagT):
return len(t._enum_members) < 256
if isinstance(t, SArrayT):
return needs_clamp(t.value_type, encoding)
if is_tuple_like(t):
return any(needs_clamp(m, encoding) for m in t.tuple_members())
if t._is_prim_word:
return t not in (INT256_T, UINT256_T, BYTES32_T)
raise CompilerPanic("unreachable") # pragma: notest
# Create an x=y statement, where the types may be compound
def make_setter(left, right):
check_assign(left, right)
# For types which occupy just one word we can use single load/store
if left.typ._is_prim_word:
enc = right.encoding # unwrap_location butchers encoding
right = unwrap_location(right)
# TODO rethink/streamline the clamp_basetype logic
if needs_clamp(right.typ, enc):
right = clamp_basetype(right)
return STORE(left, right)
# Byte arrays
elif isinstance(left.typ, _BytestringT):
# TODO rethink/streamline the clamp_basetype logic
if needs_clamp(right.typ, right.encoding):
with right.cache_when_complex("bs_ptr") as (b, right):
copier = make_byte_array_copier(left, right)
ret = b.resolve(["seq", clamp_bytestring(right), copier])
else:
ret = make_byte_array_copier(left, right)
return IRnode.from_list(ret)
elif isinstance(left.typ, DArrayT):
# TODO should we enable this?
# implicit conversion from sarray to darray
# if isinstance(right.typ, SArrayType):
# return _complex_make_setter(left, right)
# TODO rethink/streamline the clamp_basetype logic
if needs_clamp(right.typ, right.encoding):
with right.cache_when_complex("arr_ptr") as (b, right):
copier = _dynarray_make_setter(left, right)
ret = b.resolve(["seq", clamp_dyn_array(right), copier])
else:
ret = _dynarray_make_setter(left, right)
return IRnode.from_list(ret)
# Complex Types
assert isinstance(left.typ, (SArrayT, TupleT, StructT))
return _complex_make_setter(left, right)
_opt_level = OptimizationLevel.GAS
# FIXME: this is to get around the fact that we don't have a
# proper context object in the IR generation phase.
@contextlib.contextmanager
def anchor_opt_level(new_level: OptimizationLevel) -> Generator:
"""
Set the global optimization level variable for the duration of this
context manager.
"""
assert isinstance(new_level, OptimizationLevel)
global _opt_level
try:
tmp = _opt_level
_opt_level = new_level
yield
finally:
_opt_level = tmp
def _opt_codesize():
return _opt_level == OptimizationLevel.CODESIZE
def _opt_gas():
return _opt_level == OptimizationLevel.GAS
def _opt_none():
return _opt_level == OptimizationLevel.NONE
def _complex_make_setter(left, right):
if right.value == "~empty" and left.location == MEMORY:
# optimized memzero
return mzero(left, left.typ.memory_bytes_required)
ret = ["seq"]
if isinstance(left.typ, SArrayT):
n_items = right.typ.count
keys = [IRnode.from_list(i, typ=UINT256_T) for i in range(n_items)]
else:
assert is_tuple_like(left.typ)
keys = left.typ.tuple_keys()
if left.is_pointer and right.is_pointer and right.encoding == Encoding.VYPER:
# both left and right are pointers, see if we want to batch copy
# instead of unrolling the loop.
assert left.encoding == Encoding.VYPER
len_ = left.typ.memory_bytes_required
has_storage = STORAGE in (left.location, right.location)
if has_storage:
if _opt_codesize():
# assuming PUSH2, a single sstore(dst (sload src)) is 8 bytes,
# sstore(add (dst ofst), (sload (add (src ofst)))) is 16 bytes,
# whereas loop overhead is 16-17 bytes.
base_cost = 3
if left._optimized.is_literal:
# code size is smaller since add is performed at compile-time
base_cost += 1
if right._optimized.is_literal:
base_cost += 1
# the formula is a heuristic, but it works.
# (CMC 2023-07-14 could get more detailed for PUSH1 vs
# PUSH2 etc but not worried about that too much now,
# it's probably better to add a proper unroll rule in the
# optimizer.)
should_batch_copy = len_ >= 32 * base_cost
elif _opt_gas():
# kind of arbitrary, but cut off when code used > ~160 bytes
should_batch_copy = len_ >= 32 * 10
else:
assert _opt_none()
# don't care, just generate the most readable version
should_batch_copy = True
else:
# find a cutoff for memory copy where identity is cheaper
# than unrolled mloads/mstores
# if MCOPY is available, mcopy is *always* better (except in
# the 1 word case, but that is already handled by copy_bytes).
if right.location == MEMORY and _opt_gas() and not version_check(begin="cancun"):
# cost for 0th word - (mstore dst (mload src))
base_unroll_cost = 12
nth_word_cost = base_unroll_cost
if not left._optimized.is_literal:
# (mstore (add N dst) (mload src))
nth_word_cost += 6
if not right._optimized.is_literal:
# (mstore dst (mload (add N src)))
nth_word_cost += 6
identity_base_cost = 115 # staticcall 4 gas dst len src len
n_words = ceil32(len_) // 32
should_batch_copy = (
base_unroll_cost + (nth_word_cost * (n_words - 1)) >= identity_base_cost
)
# calldata to memory, code to memory, cancun, or codesize -
# batch copy is always better.
else:
should_batch_copy = True
if should_batch_copy: