|
1 | 1 | contract Assembly { |
2 | | - function ifAssembly() { |
3 | | - assembly { |
4 | | - if returndatasize { |
5 | | - success := 0 |
6 | | - } |
7 | | - } |
8 | | - } |
9 | | - |
10 | | - function caseAssembly() { |
11 | | - assembly { |
12 | | - switch value |
13 | | - case 0 { |
14 | | - mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) |
15 | | - } |
16 | | - case 1 { |
17 | | - mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) |
18 | | - } |
19 | | - } |
20 | | - } |
21 | | - |
22 | | - function forAssembly() { |
23 | | - assembly { |
24 | | - for { |
25 | | - let i := 0 |
26 | | - } lt(i, x) { |
27 | | - i := add(i, 1) |
28 | | - } { |
29 | | - y := mul(2, y) |
30 | | - } |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - function callWithComments() { |
35 | | - assembly { |
36 | | - f( |
37 | | - 1, // one |
38 | | - 2, // two |
39 | | - 3 // three |
40 | | - ) |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - function assemblyFunctionNoParameters() { |
45 | | - assembly { |
46 | | - function getAnswer() -> answer { |
47 | | - answer := 42 |
48 | | - } |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - function assemblyFunctionOneParameter() { |
53 | | - assembly { |
54 | | - function inc(x) -> result { |
55 | | - result := add(x, 1) |
56 | | - } |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - function assemblyFunctionThreeParameters() { |
61 | | - assembly { |
62 | | - function sum(a, b, c) -> result { |
63 | | - result := add(a, add(b, c)) |
64 | | - } |
65 | | - } |
66 | | - } |
67 | | - |
68 | | - function assemblyFunctionLongParameters() { |
69 | | - assembly { |
70 | | - function sum( |
71 | | - thisIs, |
72 | | - aFunctionWithVery, |
73 | | - veryLongParameterNames, |
74 | | - andItAlsoHasALotOfParameters, |
75 | | - soItShouldBeSplitInMultipleLines |
76 | | - ) -> result { |
77 | | - result := 0 |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - function assemblyFunctionLongReturnParameters() { |
83 | | - assembly { |
84 | | - function sum(a, b, c, d, e) |
85 | | - -> |
86 | | - thisIs, |
87 | | - aFunctionWithVery, |
88 | | - veryLongParameterNames, |
89 | | - andItAlsoHasALotOfParameters, |
90 | | - soItShouldBeSplitInMultipleLines |
91 | | - { |
92 | | - thisIs := 0 |
93 | | - aFunctionWithVery := 0 |
94 | | - veryLongParameterNames := 0 |
95 | | - andItAlsoHasALotOfParameters := 0 |
96 | | - soItShouldBeSplitInMultipleLines := 0 |
97 | | - } |
98 | | - } |
99 | | - } |
100 | | - |
101 | | - function assemblyFunctionLongParametersAndReturnParameters() { |
102 | | - assembly { |
103 | | - function sum(a, veryLong, listof, parameters, thatShould, splitForSure) |
104 | | - -> |
105 | | - thisIs, |
106 | | - aFunctionWithVery, |
107 | | - veryLongParameterNames, |
108 | | - andItAlsoHasALotOfParameters, |
109 | | - soItShouldBeSplitInMultipleLines |
110 | | - { |
111 | | - thisIs := 0 |
112 | | - aFunctionWithVery := 0 |
113 | | - veryLongParameterNames := 0 |
114 | | - andItAlsoHasALotOfParameters := 0 |
115 | | - soItShouldBeSplitInMultipleLines := 0 |
116 | | - } |
117 | | - } |
118 | | - } |
119 | | - |
120 | | - function assemblyNoParameterCalls() { |
121 | | - assembly { |
122 | | - call(gas(), to, value, inputData, inputDataSize, 0, 0) |
123 | | - } |
124 | | - } |
125 | | - |
126 | | - function assemblyLabels() { |
127 | | - assembly { |
128 | | - let n := calldataload(4) |
129 | | - let a := 1 |
130 | | - let b := a |
131 | | - |
132 | | - |
133 | | - loop: |
134 | | - jumpi(loopend, eq(n, 0)) |
135 | | - a |
136 | | - add |
137 | | - swap1 |
138 | | - n := sub(n, 1) |
139 | | - jump(loop) |
140 | | - |
141 | | - |
142 | | - loopend: |
143 | | - mstore(0, a) |
144 | | - return(0, 0x20) |
145 | | - } |
146 | | - |
147 | | - assembly { |
148 | | - let x := 8 |
149 | | - jump(two) |
150 | | - |
151 | | - |
152 | | - one: |
153 | | - // Here the stack height is 2 (because we pushed x and 7), |
154 | | - // but the assembler thinks it is 1 because it reads |
155 | | - // from top to bottom. |
156 | | - // Accessing the stack variable x here will lead to errors. |
157 | | - x := 9 |
158 | | - jump(three) |
159 | | - |
160 | | - |
161 | | - two: |
162 | | - 7 // push something onto the stack |
163 | | - jump(one) |
164 | | - |
165 | | - |
166 | | - three: |
167 | | - } |
168 | | - } |
169 | | - |
170 | | - function assemblyFunctionNoReturn() { |
171 | | - assembly { |
172 | | - function $somefn(somearg) { |
173 | | - |
174 | | - } |
| 2 | + function ifAssembly() { |
| 3 | + assembly { |
| 4 | + if returndatasize { |
| 5 | + success := 0 |
| 6 | + } |
| 7 | + } |
| 8 | + } |
| 9 | + |
| 10 | + function caseAssembly() { |
| 11 | + assembly { |
| 12 | + switch value |
| 13 | + case 0 { |
| 14 | + mstore(0, 0x0000000000000000000000000000000000000000000000000000000000000000) |
| 15 | + } |
| 16 | + case 1 { |
| 17 | + mstore(0, 0x1111111111111111111111111111111111111111111111111111111111111111) |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + function forAssembly() { |
| 23 | + assembly { |
| 24 | + for { |
| 25 | + let i := 0 |
| 26 | + } lt(i, x) { |
| 27 | + i := add(i, 1) |
| 28 | + } { |
| 29 | + y := mul(2, y) |
| 30 | + } |
| 31 | + } |
175 | 32 | } |
176 | | - } |
177 | 33 |
|
178 | | - function letWithoutValue() { |
179 | | - assembly { |
180 | | - let result |
| 34 | + function callWithComments() { |
| 35 | + assembly { |
| 36 | + f( |
| 37 | + 1, // one |
| 38 | + 2, // two |
| 39 | + 3 // three |
| 40 | + ) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + function assemblyFunctionNoParameters() { |
| 45 | + assembly { |
| 46 | + function getAnswer() -> answer { |
| 47 | + answer := 42 |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + function assemblyFunctionOneParameter() { |
| 53 | + assembly { |
| 54 | + function inc(x) -> result { |
| 55 | + result := add(x, 1) |
| 56 | + } |
| 57 | + } |
181 | 58 | } |
182 | | - } |
183 | 59 |
|
184 | | - function memberAccess() { |
185 | | - assembly { |
186 | | - ds.slot := position |
187 | | - offset := x.offset |
| 60 | + function assemblyFunctionThreeParameters() { |
| 61 | + assembly { |
| 62 | + function sum(a, b, c) -> result { |
| 63 | + result := add(a, add(b, c)) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + function assemblyFunctionLongParameters() { |
| 69 | + assembly { |
| 70 | + function sum( |
| 71 | + thisIs, |
| 72 | + aFunctionWithVery, |
| 73 | + veryLongParameterNames, |
| 74 | + andItAlsoHasALotOfParameters, |
| 75 | + soItShouldBeSplitInMultipleLines |
| 76 | + ) -> result { |
| 77 | + result := 0 |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + function assemblyFunctionLongReturnParameters() { |
| 83 | + assembly { |
| 84 | + function sum(a, b, c, d, e) |
| 85 | + -> |
| 86 | + thisIs, |
| 87 | + aFunctionWithVery, |
| 88 | + veryLongParameterNames, |
| 89 | + andItAlsoHasALotOfParameters, |
| 90 | + soItShouldBeSplitInMultipleLines |
| 91 | + { |
| 92 | + thisIs := 0 |
| 93 | + aFunctionWithVery := 0 |
| 94 | + veryLongParameterNames := 0 |
| 95 | + andItAlsoHasALotOfParameters := 0 |
| 96 | + soItShouldBeSplitInMultipleLines := 0 |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + function assemblyFunctionLongParametersAndReturnParameters() { |
| 102 | + assembly { |
| 103 | + function sum(a, veryLong, listof, parameters, thatShould, splitForSure) |
| 104 | + -> |
| 105 | + thisIs, |
| 106 | + aFunctionWithVery, |
| 107 | + veryLongParameterNames, |
| 108 | + andItAlsoHasALotOfParameters, |
| 109 | + soItShouldBeSplitInMultipleLines |
| 110 | + { |
| 111 | + thisIs := 0 |
| 112 | + aFunctionWithVery := 0 |
| 113 | + veryLongParameterNames := 0 |
| 114 | + andItAlsoHasALotOfParameters := 0 |
| 115 | + soItShouldBeSplitInMultipleLines := 0 |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + function assemblyNoParameterCalls() { |
| 121 | + assembly { |
| 122 | + call(gas(), to, value, inputData, inputDataSize, 0, 0) |
| 123 | + } |
188 | 124 | } |
189 | | - } |
190 | 125 |
|
191 | | - function commentsInAssemblyBlock() { |
192 | | - assembly { |
193 | | - /* foo bar baz */ |
194 | | - /* foobbbbbb */ |
| 126 | + function assemblyLabels() { |
| 127 | + assembly { |
| 128 | + let n := calldataload(4) |
| 129 | + let a := 1 |
| 130 | + let b := a |
| 131 | + |
| 132 | + |
| 133 | + loop: |
| 134 | + jumpi(loopend, eq(n, 0)) |
| 135 | + a |
| 136 | + add |
| 137 | + swap1 |
| 138 | + n := sub(n, 1) |
| 139 | + jump(loop) |
| 140 | + |
| 141 | + |
| 142 | + loopend: |
| 143 | + mstore(0, a) |
| 144 | + return(0, 0x20) |
| 145 | + } |
| 146 | + |
| 147 | + assembly { |
| 148 | + let x := 8 |
| 149 | + jump(two) |
| 150 | + |
| 151 | + |
| 152 | + one: |
| 153 | + // Here the stack height is 2 (because we pushed x and 7), |
| 154 | + // but the assembler thinks it is 1 because it reads |
| 155 | + // from top to bottom. |
| 156 | + // Accessing the stack variable x here will lead to errors. |
| 157 | + x := 9 |
| 158 | + jump(three) |
| 159 | + |
| 160 | + |
| 161 | + two: |
| 162 | + 7 // push something onto the stack |
| 163 | + jump(one) |
| 164 | + |
| 165 | + |
| 166 | + three: |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + function assemblyFunctionNoReturn() { |
| 171 | + assembly { |
| 172 | + function $somefn(somearg) { |
| 173 | + |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + function letWithoutValue() { |
| 179 | + assembly { |
| 180 | + let result |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + function memberAccess() { |
| 185 | + assembly { |
| 186 | + ds.slot := position |
| 187 | + offset := x.offset |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + function commentsInAssemblyBlock() { |
| 192 | + assembly { |
| 193 | + /* foo bar baz */ |
| 194 | + /* foobbbbbb */ |
| 195 | + } |
195 | 196 | } |
196 | | - } |
197 | 197 | } |
0 commit comments