|
| 1 | +/** |
| 2 | + * Inlining preserves the call site's source range. |
| 3 | + * |
| 4 | + * When a call is inlined, the real call instruction (which mapped to |
| 5 | + * the call expression, e.g. `square(a)`) is gone and the entry jump |
| 6 | + * that carried its range is folded by jump-optimization. Without care |
| 7 | + * the call site maps to no instruction. The pass gathers the call-site |
| 8 | + * range onto the entry inlined instruction alongside the callee-body |
| 9 | + * range it collides with, so the call expression stays mapped. |
| 10 | + */ |
| 11 | +import { describe, it, expect } from "vitest"; |
| 12 | + |
| 13 | +import { compile } from "#compiler"; |
| 14 | +import { executeProgram } from "#test/evm/behavioral"; |
| 15 | +import type * as Format from "@ethdebug/format"; |
| 16 | + |
| 17 | +// A storage-read argument keeps the inlined body from being folded |
| 18 | +// away at O3, so the inline markers survive. |
| 19 | +const source = `name Inline; |
| 20 | +define { |
| 21 | + function square(x: uint256) -> uint256 { return x * x; }; |
| 22 | +} |
| 23 | +storage { [0] r: uint256; [1] a: uint256; } |
| 24 | +create { a = 3; } |
| 25 | +code { r = square(a); }`; |
| 26 | + |
| 27 | +async function runtimeProgram(level: 0 | 2 | 3): Promise<Format.Program> { |
| 28 | + const result = await compile({ |
| 29 | + to: "bytecode", |
| 30 | + source, |
| 31 | + optimizer: { level }, |
| 32 | + }); |
| 33 | + if (!result.success) throw new Error("compile failed"); |
| 34 | + return result.value.bytecode.runtimeProgram; |
| 35 | +} |
| 36 | + |
| 37 | +/** All `code` leaves reachable through gather/pick, recursively. */ |
| 38 | +function codeLeaves(ctx: unknown): Array<{ offset: number; length: number }> { |
| 39 | + if (!ctx || typeof ctx !== "object") return []; |
| 40 | + const c = ctx as Record<string, unknown>; |
| 41 | + if (Array.isArray(c.gather)) return c.gather.flatMap(codeLeaves); |
| 42 | + if (Array.isArray(c.pick)) return c.pick.flatMap(codeLeaves); |
| 43 | + if (c.code && typeof c.code === "object") { |
| 44 | + const range = (c.code as { range?: { offset: number; length: number } }) |
| 45 | + .range; |
| 46 | + return range ? [range] : []; |
| 47 | + } |
| 48 | + return []; |
| 49 | +} |
| 50 | + |
| 51 | +/** True if an invoke discriminator is present at any leaf. */ |
| 52 | +function carriesInvoke(ctx: unknown): boolean { |
| 53 | + if (!ctx || typeof ctx !== "object") return false; |
| 54 | + const c = ctx as Record<string, unknown>; |
| 55 | + if (Array.isArray(c.gather)) return c.gather.some(carriesInvoke); |
| 56 | + if (Array.isArray(c.pick)) return c.pick.some(carriesInvoke); |
| 57 | + return "invoke" in c; |
| 58 | +} |
| 59 | + |
| 60 | +describe("inlining preserves the call-site source range", () => { |
| 61 | + for (const level of [2, 3] as const) { |
| 62 | + it(`gathers call-site + callee-body ranges on the inlined instruction at O${level}`, async () => { |
| 63 | + const program = await runtimeProgram(level); |
| 64 | + |
| 65 | + // The virtual invoke marks the inlined activation; that |
| 66 | + // instruction should now carry two distinct code ranges. |
| 67 | + const inlined = program.instructions.filter( |
| 68 | + (i) => i.context && carriesInvoke(i.context), |
| 69 | + ); |
| 70 | + expect(inlined.length).toBeGreaterThan(0); |
| 71 | + |
| 72 | + const withTwoRanges = inlined.find((i) => { |
| 73 | + const ranges = codeLeaves(i.context); |
| 74 | + const distinct = new Set(ranges.map((r) => `${r.offset}:${r.length}`)); |
| 75 | + return distinct.size >= 2; |
| 76 | + }); |
| 77 | + expect( |
| 78 | + withTwoRanges, |
| 79 | + "an inlined instruction carrying both the call-site and callee-body ranges", |
| 80 | + ).toBeDefined(); |
| 81 | + |
| 82 | + // One of the ranges must be the call expression `square(a)`. |
| 83 | + const callSite = source.indexOf("square(a)"); |
| 84 | + const ranges = codeLeaves(withTwoRanges!.context); |
| 85 | + expect(ranges.some((r) => r.offset === callSite)).toBe(true); |
| 86 | + }); |
| 87 | + } |
| 88 | + |
| 89 | + it("does not inline (and needs no gather) at O0", async () => { |
| 90 | + const program = await runtimeProgram(0); |
| 91 | + const inlined = program.instructions.filter( |
| 92 | + (i) => |
| 93 | + i.context && |
| 94 | + carriesInvoke(i.context) && |
| 95 | + i.operation?.mnemonic !== "JUMP" && |
| 96 | + i.operation?.mnemonic !== "JUMPDEST", |
| 97 | + ); |
| 98 | + // At O0 the invoke rides real call JUMP/JUMPDESTs, not spliced |
| 99 | + // body instructions — so no non-jump instruction carries it. |
| 100 | + expect(inlined.length).toBe(0); |
| 101 | + }); |
| 102 | + |
| 103 | + it("keeps runtime behavior correct at every level", async () => { |
| 104 | + for (const level of [0, 2, 3] as const) { |
| 105 | + const res = await executeProgram(source, { |
| 106 | + calldata: "", |
| 107 | + optimizationLevel: level, |
| 108 | + }); |
| 109 | + expect(res.callSuccess).toBe(true); |
| 110 | + expect(await res.getStorage(0n)).toBe(9n); // square(3) = 9 |
| 111 | + } |
| 112 | + }); |
| 113 | +}); |
0 commit comments