-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathfunction.ts
More file actions
398 lines (360 loc) · 11.3 KB
/
function.ts
File metadata and controls
398 lines (360 loc) · 11.3 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
/**
* Function-level code generation
*/
import type * as Format from "@ethdebug/format";
import * as Ir from "#ir";
import type * as Evm from "#evm";
import type { Stack } from "#evm";
import type { State } from "#evmgen/state";
import type { Layout, Memory } from "#evmgen/analysis";
import type { Error as EvmgenError } from "#evmgen/errors";
import * as Block from "./block.js";
import { serialize } from "../serialize.js";
import { type Transition } from "../operations.js";
/**
* Generate prologue for user-defined functions
* Stack on entry: [arg0] [arg1] ... [argN] [return_pc]
* After prologue: empty stack, args stored in memory, return_pc at 0x60
*/
function generatePrologue<S extends Stack>(
func: Ir.Function,
): Transition<S, readonly []> {
const params = func.parameters || [];
return ((state: State<S>): State<readonly []> => {
let currentState = state;
// Add JUMPDEST with function entry annotation.
// After this JUMPDEST executes, the callee's args are
// on the stack (first arg deepest).
const argPointers = params.map((_p, i) => ({
location: "stack" as const,
slot: params.length - 1 - i,
}));
const entryInvoke: Format.Program.Context.Invoke = {
invoke: {
jump: true as const,
identifier: func.name || "anonymous",
target: {
pointer: {
location: "stack" as const,
slot: 0,
},
},
...(argPointers.length > 0 && {
arguments: {
pointer: {
group: argPointers,
},
},
}),
},
};
const entryDebug = {
context: entryInvoke as Format.Program.Context,
};
currentState = {
...currentState,
instructions: [
...currentState.instructions,
{ mnemonic: "JUMPDEST", opcode: 0x5b, debug: entryDebug },
],
};
// Store each parameter to memory and pop from stack
// Stack layout on entry: [arg0, arg1, ..., argN]
// Return PC is already in memory at 0x60 (stored by caller)
// Pop and store each arg from argN down to arg0
const prologueDebug =
func.sourceId && func.loc
? {
context: {
gather: [
{
remark: `prologue: store ${params.length} parameter(s) to memory`,
},
{
code: {
source: { id: func.sourceId },
range: func.loc,
},
},
],
} as Format.Program.Context,
}
: {
context: {
remark: `prologue: store ${params.length} parameter(s) to memory`,
} as Format.Program.Context,
};
for (let i = params.length - 1; i >= 0; i--) {
const param = params[i];
const allocation = currentState.memory.allocations[param.tempId];
if (!allocation) continue;
// Push memory offset
const highByte = (allocation.offset >> 8) & 0xff;
const lowByte = allocation.offset & 0xff;
currentState = {
...currentState,
instructions: [
...currentState.instructions,
{
mnemonic: "PUSH2",
opcode: 0x61,
immediates: [highByte, lowByte],
debug: prologueDebug,
},
],
};
// MSTORE pops arg and offset
currentState = {
...currentState,
instructions: [
...currentState.instructions,
{
mnemonic: "MSTORE",
opcode: 0x52,
debug: prologueDebug,
},
],
};
}
// Save the return PC from 0x60 to a dedicated slot
// so nested function calls don't clobber it.
const savedPcOffset = currentState.memory.savedReturnPcOffset;
if (savedPcOffset !== undefined) {
const savePcDebug =
func.sourceId && func.loc
? {
context: {
gather: [
{
remark: `prologue: save return PC to 0x${savedPcOffset.toString(16)}`,
},
{
code: {
source: { id: func.sourceId },
range: func.loc,
},
},
],
} as Format.Program.Context,
}
: {
context: {
remark: `prologue: save return PC to 0x${savedPcOffset.toString(16)}`,
} as Format.Program.Context,
};
const highByte = (savedPcOffset >> 8) & 0xff;
const lowByte = savedPcOffset & 0xff;
currentState = {
...currentState,
instructions: [
...currentState.instructions,
{
mnemonic: "PUSH1",
opcode: 0x60,
immediates: [0x60],
debug: savePcDebug,
},
{
mnemonic: "MLOAD",
opcode: 0x51,
debug: savePcDebug,
},
{
mnemonic: "PUSH2",
opcode: 0x61,
immediates: [highByte, lowByte],
debug: savePcDebug,
},
{
mnemonic: "MSTORE",
opcode: 0x52,
debug: savePcDebug,
},
],
};
}
// Return with empty stack
return {
...currentState,
stack: [],
brands: [],
} as State<readonly []>;
}) as Transition<S, readonly []>;
}
/**
* Generate bytecode for a function
*/
export function generate(
func: Ir.Function,
memory: Memory.Function.Info,
layout: Layout.Function.Info,
options: { isUserFunction?: boolean } = {},
): {
instructions: Evm.Instruction[];
bytecode: number[];
warnings: EvmgenError[];
patches: State<Stack>["patches"];
blockOffsets: Record<string, number>;
} {
const initialState: State<readonly []> = {
brands: [],
stack: [],
instructions: [],
memory,
nextId: 0,
patches: [],
blockOffsets: {},
warnings: [],
functionRegistry: {},
callStackPointer: 0x60,
};
// Add prologue for user functions (not main/create)
let stateAfterPrologue = initialState;
if (options.isUserFunction) {
const prologueTransition = generatePrologue(func);
stateAfterPrologue = prologueTransition(initialState);
}
const finalState = layout.order.reduce(
(state: State<Stack>, blockId: string, index: number) => {
const block = func.blocks.get(blockId);
if (!block) return state;
// Determine predecessor for phi resolution
// This is simplified - real implementation would track actual control flow
const predecessor = index > 0 ? layout.order[index - 1] : undefined;
// Check if this is the first or last block
const isFirstBlock = index === 0;
const isLastBlock = index === layout.order.length - 1;
return Block.generate(
block,
predecessor,
isLastBlock,
isFirstBlock,
options.isUserFunction || false,
func,
)(state);
},
stateAfterPrologue,
);
// For user functions, defer block/continuation patching
// to module level where the function's base offset is known.
// Block offsets computed here are relative to the function
// start; EVM JUMP needs absolute program counter values.
if (options.isUserFunction) {
const bytecode = serialize(finalState.instructions);
return {
instructions: finalState.instructions,
bytecode,
warnings: finalState.warnings,
patches: finalState.patches,
blockOffsets: finalState.blockOffsets,
};
}
// Patch block jump targets (not function calls yet)
const patchedState = patchJumps(finalState);
// Serialize to bytecode
const bytecode = serialize(patchedState.instructions);
return {
instructions: patchedState.instructions,
bytecode,
warnings: patchedState.warnings,
patches: finalState.patches, // Return patches for module-level patching
blockOffsets: finalState.blockOffsets,
};
}
/**
* Patch jump targets after all blocks have been generated
*/
function patchJumps<S extends Stack>(state: State<S>): State<S> {
const patchedInstructions = [...state.instructions];
for (const patch of state.patches) {
// Skip function patches - they'll be handled at module level
if (patch.type === "function") {
continue;
}
// Both block jumps and continuation patches use blockOffsets
const targetOffset = state.blockOffsets[patch.target];
if (targetOffset === undefined) {
throw new Error(`Jump target ${patch.target} not found`);
}
// Convert offset to bytes for PUSH2 (2 bytes, big-endian)
const highByte = (targetOffset >> 8) & 0xff;
const lowByte = targetOffset & 0xff;
// Update the PUSH2 instruction at the patch index
const instruction = patchedInstructions[patch.index];
if (instruction && instruction.immediates) {
instruction.immediates = [highByte, lowByte];
}
}
return {
...state,
instructions: patchedInstructions,
};
}
/**
* Patch function call addresses in bytecode.
*
* When `baseOffset` is provided (for user-defined functions),
* block/continuation patches are also resolved by adding
* `baseOffset` to each block-relative target. This is
* necessary because block offsets are computed relative to
* the function's instruction stream, but EVM JUMP needs
* absolute program counter values.
*/
export function patchFunctionCalls(
bytecode: number[],
instructions: Evm.Instruction[],
patches: State<Stack>["patches"],
functionRegistry: Record<string, number>,
options: {
baseOffset?: number;
blockOffsets?: Record<string, number>;
} = {},
): { bytecode: number[]; instructions: Evm.Instruction[] } {
const patchedInstructions = [...instructions];
const patchedBytecode = [...bytecode];
for (const patch of patches) {
let targetOffset: number | undefined;
if (patch.type === "function") {
targetOffset = functionRegistry[patch.target];
if (targetOffset === undefined) {
throw new Error(`Function ${patch.target} not found in registry`);
}
} else if (options.baseOffset !== undefined && options.blockOffsets) {
// Block or continuation patch with base offset
const blockOffset = options.blockOffsets[patch.target];
if (blockOffset === undefined) {
throw new Error(`Jump target ${patch.target} not found`);
}
targetOffset = blockOffset + options.baseOffset;
} else {
// Skip non-function patches when no base offset
continue;
}
// Convert offset to bytes for PUSH2 (2 bytes, big-endian)
const highByte = (targetOffset >> 8) & 0xff;
const lowByte = targetOffset & 0xff;
// Update the PUSH2 instruction
const instruction = patchedInstructions[patch.index];
if (instruction && instruction.immediates) {
instruction.immediates = [highByte, lowByte];
}
// Also patch in the bytecode
// Find the instruction's position in bytecode
let bytePos = 0;
for (let i = 0; i < patch.index; i++) {
const inst = instructions[i];
bytePos += 1; // opcode
if (inst.immediates) {
bytePos += inst.immediates.length;
}
}
// Skip opcode byte
bytePos += 1;
patchedBytecode[bytePos] = highByte;
patchedBytecode[bytePos + 1] = lowByte;
}
return {
bytecode: patchedBytecode,
instructions: patchedInstructions,
};
}