|
2 | 2 | * Block-level code generation |
3 | 3 | */ |
4 | 4 |
|
| 5 | +import type * as Ast from "#ast"; |
5 | 6 | import type * as Format from "@ethdebug/format"; |
6 | 7 | import * as Ir from "#ir"; |
7 | 8 | import type { Stack } from "#evm"; |
@@ -47,9 +48,13 @@ export function generate<S extends Stack>( |
47 | 48 |
|
48 | 49 | // Initialize memory for first block |
49 | 50 | if (isFirstBlock) { |
50 | | - // Always initialize the free memory pointer for consistency |
51 | | - // This ensures dynamic allocations start after static ones |
52 | | - result = result.then(initializeMemory(state.memory.nextStaticOffset)); |
| 51 | + const sourceInfo = |
| 52 | + func?.sourceId && func?.loc |
| 53 | + ? { sourceId: func.sourceId, loc: func.loc } |
| 54 | + : undefined; |
| 55 | + result = result.then( |
| 56 | + initializeMemory(state.memory.nextStaticOffset, sourceInfo), |
| 57 | + ); |
53 | 58 | } |
54 | 59 |
|
55 | 60 | // Set JUMPDEST for non-first blocks |
@@ -215,18 +220,34 @@ function generatePhi<S extends Stack>( |
215 | 220 |
|
216 | 221 | /** |
217 | 222 | * Initialize the free memory pointer at runtime |
218 | | - * Sets the value at 0x40 to the next available memory location after static allocations |
| 223 | + * Sets the value at 0x40 to the next available memory location |
| 224 | + * after static allocations |
219 | 225 | */ |
220 | 226 | function initializeMemory<S extends Stack>( |
221 | 227 | nextStaticOffset: number, |
| 228 | + sourceInfo?: { sourceId: string; loc: Ast.SourceLocation }, |
222 | 229 | ): Transition<S, S> { |
223 | 230 | const { PUSHn, MSTORE } = operations; |
224 | 231 |
|
225 | | - const debug = { |
226 | | - context: { |
227 | | - remark: "initialize free memory pointer", |
228 | | - }, |
229 | | - }; |
| 232 | + const debug = sourceInfo |
| 233 | + ? { |
| 234 | + context: { |
| 235 | + gather: [ |
| 236 | + { remark: "initialize free memory pointer" }, |
| 237 | + { |
| 238 | + code: { |
| 239 | + source: { id: sourceInfo.sourceId }, |
| 240 | + range: sourceInfo.loc, |
| 241 | + }, |
| 242 | + }, |
| 243 | + ], |
| 244 | + } as Format.Program.Context, |
| 245 | + } |
| 246 | + : { |
| 247 | + context: { |
| 248 | + remark: "initialize free memory pointer", |
| 249 | + } as Format.Program.Context, |
| 250 | + }; |
230 | 251 |
|
231 | 252 | return pipe<S>() |
232 | 253 | .then(PUSHn(BigInt(nextStaticOffset), { debug }), { |
|
0 commit comments