Skip to content

Commit 0a0ce4a

Browse files
committed
bugc: use proper typed invoke/return context construction
Replace `as unknown as Format.Program.Context` casts with properly typed construction using Context.Invoke and Context.Return interfaces. Add required `target` pointer to invoke contexts and always provide `data` pointer in return contexts. Update tests accordingly.
1 parent b83972f commit 0a0ce4a

4 files changed

Lines changed: 55 additions & 49 deletions

File tree

packages/bugc/src/evmgen/call-contexts.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ code {
7373
expect(invoke.jump).toBe(true);
7474
expect(invoke.identifier).toBe("add");
7575

76+
// Should have target pointer
77+
const target = invoke.target as Record<string, unknown>;
78+
expect(target.pointer).toBeDefined();
79+
7680
// Should have argument pointers
7781
const args = invoke.arguments as Record<string, unknown>;
7882
const pointer = args.pointer as Record<string, unknown>;
@@ -107,7 +111,6 @@ code {
107111
const ctx = (returnJumpdests[0].context as Record<string, unknown>)!;
108112
const ret = ctx.return as Record<string, unknown>;
109113

110-
expect(ret.jump).toBe(true);
111114
expect(ret.identifier).toBe("add");
112115

113116
// Should have data pointer to return value at

packages/bugc/src/evmgen/generation/block.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,23 @@ export function generate<S extends Stack>(
7070

7171
// Add JUMPDEST with continuation annotation if applicable
7272
if (isContinuation) {
73-
// Check if the call has a return value
74-
const predBlock = func!.blocks.get(predecessor!);
75-
const hasReturnValue =
76-
predBlock?.terminator.kind === "call" &&
77-
!!predBlock.terminator.dest;
78-
7973
// Return context describes state after JUMPDEST
8074
// executes: TOS is the return value (if any).
81-
const continuationDebug = {
82-
context: {
83-
return: {
84-
jump: true as const,
85-
identifier: calledFunction,
86-
...(hasReturnValue && {
87-
data: {
88-
pointer: {
89-
location: "stack" as const,
90-
slot: 0,
91-
},
92-
},
93-
}),
75+
// data pointer is required by the schema; for
76+
// void returns, slot 0 is still valid (empty).
77+
const returnCtx: Format.Program.Context.Return = {
78+
return: {
79+
identifier: calledFunction,
80+
data: {
81+
pointer: {
82+
location: "stack" as const,
83+
slot: 0,
84+
},
9485
},
95-
} as unknown as Format.Program.Context,
86+
},
87+
};
88+
const continuationDebug = {
89+
context: returnCtx as Format.Program.Context,
9690
};
9791
result = result.then(JUMPDEST({ debug: continuationDebug }));
9892
} else {

packages/bugc/src/evmgen/generation/control-flow/terminator.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,25 @@ export function generateCallTerminator<S extends Stack>(
245245

246246
// Invoke context describes state after JUMP executes:
247247
// the callee has been entered with args on the stack.
248-
// Cast needed because invoke context TS types don't
249-
// exist yet (only YAML schemas on call-return branch).
250-
const invokeContext = {
251-
context: {
252-
invoke: {
253-
jump: true as const,
254-
identifier: funcName,
255-
...(argPointers.length > 0 && {
256-
arguments: {
257-
pointer: {
258-
group: argPointers,
259-
},
260-
},
261-
}),
248+
// target points to the function address at stack slot 0
249+
// (consumed by JUMP, but describes the call target).
250+
const invoke: Format.Program.Context.Invoke = {
251+
invoke: {
252+
jump: true as const,
253+
identifier: funcName,
254+
target: {
255+
pointer: { location: "stack" as const, slot: 0 },
262256
},
263-
} as unknown as Format.Program.Context,
257+
...(argPointers.length > 0 && {
258+
arguments: {
259+
pointer: {
260+
group: argPointers,
261+
},
262+
},
263+
}),
264+
},
264265
};
266+
const invokeContext = { context: invoke as Format.Program.Context };
265267

266268
currentState = {
267269
...currentState,

packages/bugc/src/evmgen/generation/function.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,27 @@ function generatePrologue<S extends Stack>(
3636
slot: params.length - 1 - i,
3737
}));
3838

39-
const entryDebug = {
40-
context: {
41-
invoke: {
42-
jump: true as const,
43-
identifier: func.name || "anonymous",
44-
...(argPointers.length > 0 && {
45-
arguments: {
46-
pointer: {
47-
group: argPointers,
48-
},
49-
},
50-
}),
39+
const entryInvoke: Format.Program.Context.Invoke = {
40+
invoke: {
41+
jump: true as const,
42+
identifier: func.name || "anonymous",
43+
target: {
44+
pointer: {
45+
location: "stack" as const,
46+
slot: 0,
47+
},
5148
},
52-
} as unknown as Format.Program.Context,
49+
...(argPointers.length > 0 && {
50+
arguments: {
51+
pointer: {
52+
group: argPointers,
53+
},
54+
},
55+
}),
56+
},
57+
};
58+
const entryDebug = {
59+
context: entryInvoke as Format.Program.Context,
5360
};
5461
currentState = {
5562
...currentState,

0 commit comments

Comments
 (0)