@@ -31,7 +31,8 @@ unsigned ConstantOptimisationMethod::optimiseConstants(
31
31
bool _isCreation,
32
32
size_t _runs,
33
33
langutil::EVMVersion _evmVersion,
34
- Assembly& _assembly
34
+ Assembly& _assembly,
35
+ std::optional<uint8_t > _eofVersion
35
36
)
36
37
{
37
38
// TODO: design the optimiser in a way this is not needed
@@ -55,6 +56,7 @@ unsigned ConstantOptimisationMethod::optimiseConstants(
55
56
params.isCreation = _isCreation;
56
57
params.runs = _runs;
57
58
params.evmVersion = _evmVersion;
59
+ params.eofVersion = _eofVersion;
58
60
LiteralMethod lit (params, item.data ());
59
61
bigint literalGas = lit.gasNeeded ();
60
62
CodeCopyMethod copy (params, item.data ());
@@ -161,73 +163,92 @@ AssemblyItems CodeCopyMethod::execute(Assembly& _assembly) const
161
163
{
162
164
bytes data = toBigEndian (m_value);
163
165
assertThrow (data.size () == 32 , OptimizerException, " Invalid number encoding." );
164
- AssemblyItem newPushData = _assembly.newData (data);
165
- return copyRoutine (&newPushData);
166
+
167
+ if (m_params.eofVersion .has_value ())
168
+ return AssemblyItems {_assembly.newDataLoadN (data)};
169
+ else
170
+ {
171
+ AssemblyItem newPushData = _assembly.newData (data);
172
+ return copyRoutine (&newPushData);
173
+ }
166
174
}
167
175
168
- AssemblyItems CodeCopyMethod::copyRoutine (AssemblyItem* _pushData ) const
176
+ AssemblyItems CodeCopyMethod::copyRoutine (AssemblyItem* _dataItem ) const
169
177
{
170
- if (_pushData)
171
- assertThrow (_pushData->type () == PushData, OptimizerException, " Invalid Assembly Item." );
172
-
173
- AssemblyItem dataUsed = _pushData ? *_pushData : AssemblyItem (PushData, u256 (1 ) << 16 );
174
-
175
- // PUSH0 is cheaper than PUSHn/DUP/SWAP.
176
- if (m_params.evmVersion .hasPush0 ())
178
+ if (m_params.eofVersion .has_value ())
177
179
{
178
- // This costs ~29 gas.
180
+ // This case is used only for gas calculation.
181
+ solAssert (_dataItem == nullptr );
179
182
AssemblyItems copyRoutine{
180
- // back up memory
181
- // mload(0)
182
- u256 (0 ),
183
- Instruction::MLOAD,
184
-
185
- // codecopy(0, <offset>, 32)
186
- u256 (32 ),
187
- dataUsed,
188
- u256 (0 ),
189
- Instruction::CODECOPY,
190
-
191
- // mload(0)
192
- u256 (0 ),
193
- Instruction::MLOAD,
194
-
195
- // restore original memory
196
- // mstore(0, x)
197
- Instruction::SWAP1,
198
- u256 (0 ),
199
- Instruction::MSTORE
183
+ AssemblyItem (DataLoadN, Instruction::DATALOADN, u256 (1 ) << 16 )
200
184
};
201
185
return copyRoutine;
202
186
}
203
187
else
204
188
{
205
- // This costs ~33 gas.
206
- AssemblyItems copyRoutine{
207
- // constant to be reused 3+ times
208
- u256 (0 ),
209
-
210
- // back up memory
211
- // mload(0)
212
- Instruction::DUP1,
213
- Instruction::MLOAD,
214
-
215
- // codecopy(0, <offset>, 32)
216
- u256 (32 ),
217
- dataUsed,
218
- Instruction::DUP4,
219
- Instruction::CODECOPY,
220
-
221
- // mload(0)
222
- Instruction::DUP2,
223
- Instruction::MLOAD,
224
-
225
- // restore original memory
226
- // mstore(0, x)
227
- Instruction::SWAP2,
228
- Instruction::MSTORE
229
- };
230
- return copyRoutine;
189
+ AssemblyItem* _pushData = _dataItem;
190
+ if (_pushData)
191
+ assertThrow (_pushData->type () == PushData, OptimizerException, " Invalid Assembly Item." );
192
+
193
+ AssemblyItem dataUsed = _pushData ? *_pushData : AssemblyItem (PushData, u256 (1 ) << 16 );
194
+
195
+ // PUSH0 is cheaper than PUSHn/DUP/SWAP.
196
+ if (m_params.evmVersion .hasPush0 ())
197
+ {
198
+ // This costs ~29 gas.
199
+ AssemblyItems copyRoutine{
200
+ // back up memory
201
+ // mload(0)
202
+ u256 (0 ),
203
+ Instruction::MLOAD,
204
+
205
+ // codecopy(0, <offset>, 32)
206
+ u256 (32 ),
207
+ dataUsed,
208
+ u256 (0 ),
209
+ Instruction::CODECOPY,
210
+
211
+ // mload(0)
212
+ u256 (0 ),
213
+ Instruction::MLOAD,
214
+
215
+ // restore original memory
216
+ // mstore(0, x)
217
+ Instruction::SWAP1,
218
+ u256 (0 ),
219
+ Instruction::MSTORE
220
+ };
221
+ return copyRoutine;
222
+ }
223
+ else
224
+ {
225
+ // This costs ~33 gas.
226
+ AssemblyItems copyRoutine{
227
+ // constant to be reused 3+ times
228
+ u256 (0 ),
229
+
230
+ // back up memory
231
+ // mload(0)
232
+ Instruction::DUP1,
233
+ Instruction::MLOAD,
234
+
235
+ // codecopy(0, <offset>, 32)
236
+ u256 (32 ),
237
+ dataUsed,
238
+ Instruction::DUP4,
239
+ Instruction::CODECOPY,
240
+
241
+ // mload(0)
242
+ Instruction::DUP2,
243
+ Instruction::MLOAD,
244
+
245
+ // restore original memory
246
+ // mstore(0, x)
247
+ Instruction::SWAP2,
248
+ Instruction::MSTORE
249
+ };
250
+ return copyRoutine;
251
+ }
231
252
}
232
253
}
233
254
0 commit comments