|
| 1 | +/** |
| 2 | + * Every instruction has a source range, so a debugger stopped at any |
| 3 | + * of them has something to show: the instructions the compiler adds |
| 4 | + * (a called function's entry, a block's JUMPDEST, a return the source |
| 5 | + * does not write) get the range of what they belong to. |
| 6 | + */ |
| 7 | +import { describe, it, expect } from "vitest"; |
| 8 | +import type * as Format from "@ethdebug/format"; |
| 9 | +import { Program } from "@ethdebug/format"; |
| 10 | +import { compile } from "#compiler"; |
| 11 | +import { exampleSources } from "../examples/index.js"; |
| 12 | + |
| 13 | +const { Context } = Program; |
| 14 | + |
| 15 | +/** Whether a context gives a source range, in a gather or a pick too */ |
| 16 | +function hasCode(context: Format.Program.Context | undefined): boolean { |
| 17 | + if (!context) return false; |
| 18 | + if (Context.isCode(context)) return true; |
| 19 | + if (Context.isGather(context)) return context.gather.some(hasCode); |
| 20 | + if (Context.isPick(context)) return context.pick.some(hasCode); |
| 21 | + return false; |
| 22 | +} |
| 23 | + |
| 24 | +/** Whether a context starts a call, in a gather too */ |
| 25 | +function hasInvoke(context: Format.Program.Context | undefined): boolean { |
| 26 | + if (!context) return false; |
| 27 | + if (Context.isInvoke(context)) return true; |
| 28 | + return Context.isGather(context) && context.gather.some(hasInvoke); |
| 29 | +} |
| 30 | + |
| 31 | +/** Why an instruction has no source range, if it may have none */ |
| 32 | +function exception( |
| 33 | + instructions: Format.Program.Instruction[], |
| 34 | + index: number, |
| 35 | +): string | undefined { |
| 36 | + const { operation, context } = instructions[index]; |
| 37 | + const remark = context && Context.isRemark(context) ? context.remark : ""; |
| 38 | + // The source has no `code` block: the runtime is the compiler's own |
| 39 | + if (remark === "initialize free memory pointer") return "no code block"; |
| 40 | + // The create program's end: it returns the runtime code |
| 41 | + if (remark.startsWith("deployment:")) return "deployment"; |
| 42 | + // A block that starts with an inlined call: the JUMPDEST is at the |
| 43 | + // call site, which the call's first instruction does not always give |
| 44 | + if ( |
| 45 | + operation?.mnemonic === "JUMPDEST" && |
| 46 | + hasInvoke(instructions[index + 1]?.context) |
| 47 | + ) { |
| 48 | + return "before an inlined call"; |
| 49 | + } |
| 50 | + return undefined; |
| 51 | +} |
| 52 | + |
| 53 | +describe("source ranges", () => { |
| 54 | + it("are on every instruction of every example", async () => { |
| 55 | + const missing: string[] = []; |
| 56 | + for (const [name, source] of Object.entries(exampleSources)) { |
| 57 | + for (const level of [0, 1, 2, 3] as const) { |
| 58 | + const result = await compile({ |
| 59 | + to: "bytecode", |
| 60 | + source, |
| 61 | + optimizer: { level }, |
| 62 | + }); |
| 63 | + if (!result.success) continue; |
| 64 | + const { runtimeProgram, createProgram } = result.value.bytecode; |
| 65 | + for (const program of [runtimeProgram, createProgram]) { |
| 66 | + const { instructions } = program ?? { instructions: [] }; |
| 67 | + instructions.forEach(({ offset, context }, index) => { |
| 68 | + if (hasCode(context) || exception(instructions, index)) return; |
| 69 | + missing.push(`${name} at O${level}, offset ${offset}`); |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + expect(missing).toEqual([]); |
| 75 | + }, 120_000); |
| 76 | + |
| 77 | + it("give a called function's entry its declaration", async () => { |
| 78 | + const source = `name Entry; |
| 79 | +define { |
| 80 | + function f(n: uint256) -> uint256 { |
| 81 | + if (n > 0) { return n; } |
| 82 | + return f(n + 1); |
| 83 | + }; |
| 84 | +} |
| 85 | +storage { [0] r: uint256; } |
| 86 | +create {} |
| 87 | +code { r = f(0); }`; |
| 88 | + for (const level of [0, 1, 2, 3] as const) { |
| 89 | + const result = await compile({ |
| 90 | + to: "bytecode", |
| 91 | + source, |
| 92 | + optimizer: { level }, |
| 93 | + }); |
| 94 | + if (!result.success) throw new Error("compilation failed"); |
| 95 | + const entries = result.value.bytecode.runtimeProgram.instructions.filter( |
| 96 | + ({ operation, context }) => |
| 97 | + operation?.mnemonic === "JUMPDEST" && |
| 98 | + context && |
| 99 | + Context.isInvoke(context), |
| 100 | + ); |
| 101 | + expect(entries.length).toBeGreaterThan(0); |
| 102 | + for (const { context } of entries) { |
| 103 | + const { code, invoke } = context as Format.Program.Context.Code & |
| 104 | + Format.Program.Context.Invoke; |
| 105 | + expect(code).toEqual((invoke as { declaration?: unknown }).declaration); |
| 106 | + } |
| 107 | + } |
| 108 | + }); |
| 109 | +}); |
0 commit comments