|
| 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 | +import { hasCode } from "./generation/control-flow/terminator.js"; |
| 13 | + |
| 14 | +const { Context } = Program; |
| 15 | + |
| 16 | +/** Whether a context starts an inlined call */ |
| 17 | +function invokesInline(context: Format.Program.Context | undefined): boolean { |
| 18 | + return !!context && invokes(context) && inlined(context); |
| 19 | +} |
| 20 | + |
| 21 | +/** Whether a context starts a call with no jump target (in a gather |
| 22 | + * too), as an inlined call's invoke has none */ |
| 23 | +function invokes(context: Format.Program.Context): boolean { |
| 24 | + return ( |
| 25 | + (Context.isInvoke(context) && !("target" in context.invoke)) || |
| 26 | + (Context.isGather(context) && context.gather.some(invokes)) |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +/** Whether a context is in an inlined body (in a gather too) */ |
| 31 | +function inlined(context: Format.Program.Context): boolean { |
| 32 | + return ( |
| 33 | + (Context.isTransform(context) && context.transform.includes("inline")) || |
| 34 | + (Context.isGather(context) && context.gather.some(inlined)) |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +/** Why an instruction has no source range, if it may have none */ |
| 39 | +function exception( |
| 40 | + instructions: Format.Program.Instruction[], |
| 41 | + index: number, |
| 42 | +): string | undefined { |
| 43 | + const { operation, context } = instructions[index]; |
| 44 | + const remark = context && Context.isRemark(context) ? context.remark : ""; |
| 45 | + // The source has no `code` block: the runtime is the compiler's own |
| 46 | + if (remark === "initialize free memory pointer") return "no code block"; |
| 47 | + // The create program's end: it returns the runtime code |
| 48 | + if (remark.startsWith("deployment:")) return "deployment"; |
| 49 | + // A block that starts with an inlined call: the JUMPDEST is at the |
| 50 | + // call site, which the call's first instruction does not always give |
| 51 | + if ( |
| 52 | + operation?.mnemonic === "JUMPDEST" && |
| 53 | + invokesInline(instructions[index + 1]?.context) |
| 54 | + ) { |
| 55 | + return "before an inlined call"; |
| 56 | + } |
| 57 | + return undefined; |
| 58 | +} |
| 59 | + |
| 60 | +describe("source ranges", () => { |
| 61 | + it("are on every instruction of every example", async () => { |
| 62 | + const missing: string[] = []; |
| 63 | + for (const [name, source] of Object.entries(exampleSources)) { |
| 64 | + for (const level of [0, 1, 2, 3] as const) { |
| 65 | + const result = await compile({ |
| 66 | + to: "bytecode", |
| 67 | + source, |
| 68 | + optimizer: { level }, |
| 69 | + }); |
| 70 | + if (!result.success) continue; |
| 71 | + const { runtimeProgram, createProgram } = result.value.bytecode; |
| 72 | + for (const program of [runtimeProgram, createProgram]) { |
| 73 | + const { instructions } = program ?? { instructions: [] }; |
| 74 | + instructions.forEach(({ offset, context }, index) => { |
| 75 | + if (hasCode(context) || exception(instructions, index)) return; |
| 76 | + missing.push(`${name} at O${level}, offset ${offset}`); |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + expect(missing).toEqual([]); |
| 82 | + }, 120_000); |
| 83 | + |
| 84 | + it("give a called function's entry its declaration", async () => { |
| 85 | + const source = `name Entry; |
| 86 | +define { |
| 87 | + function f(n: uint256) -> uint256 { |
| 88 | + if (n > 0) { return n; } |
| 89 | + return f(n + 1); |
| 90 | + }; |
| 91 | +} |
| 92 | +storage { [0] r: uint256; } |
| 93 | +create {} |
| 94 | +code { r = f(0); }`; |
| 95 | + for (const level of [0, 1, 2, 3] as const) { |
| 96 | + const result = await compile({ |
| 97 | + to: "bytecode", |
| 98 | + source, |
| 99 | + optimizer: { level }, |
| 100 | + }); |
| 101 | + if (!result.success) throw new Error("compilation failed"); |
| 102 | + const entries = result.value.bytecode.runtimeProgram.instructions.filter( |
| 103 | + ({ operation, context }) => |
| 104 | + operation?.mnemonic === "JUMPDEST" && |
| 105 | + context && |
| 106 | + Context.isInvoke(context), |
| 107 | + ); |
| 108 | + expect(entries.length).toBeGreaterThan(0); |
| 109 | + for (const { context } of entries) { |
| 110 | + const { code, invoke } = context as Format.Program.Context.Code & |
| 111 | + Format.Program.Context.Invoke; |
| 112 | + expect(code).toEqual((invoke as { declaration?: unknown }).declaration); |
| 113 | + } |
| 114 | + } |
| 115 | + }); |
| 116 | + |
| 117 | + it("give a block's JUMPDEST the range of what runs next", async () => { |
| 118 | + const source = `name Labels; |
| 119 | +storage { [0] r: uint256; } |
| 120 | +create {} |
| 121 | +code { |
| 122 | + let n = r; |
| 123 | + if (n > 3) { r = n - 1; } else { r = n + 2; } |
| 124 | + for (let i = 0; i < n; i = i + 1) { r = r + i; } |
| 125 | +}`; |
| 126 | + const result = await compile({ |
| 127 | + to: "bytecode", |
| 128 | + source, |
| 129 | + optimizer: { level: 0 }, |
| 130 | + }); |
| 131 | + if (!result.success) throw new Error("compilation failed"); |
| 132 | + const { instructions } = result.value.bytecode.runtimeProgram; |
| 133 | + const labels = instructions.flatMap(({ operation, context }, index) => |
| 134 | + operation?.mnemonic === "JUMPDEST" ? [{ context, index }] : [], |
| 135 | + ); |
| 136 | + expect(labels.length).toBeGreaterThan(2); |
| 137 | + for (const { context, index } of labels) { |
| 138 | + const next = instructions[index + 1].context; |
| 139 | + expect(hasCode(context)).toBe(true); |
| 140 | + expect((context as Format.Program.Context.Code).code).toEqual( |
| 141 | + (next as Format.Program.Context.Code).code, |
| 142 | + ); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + it("give the end of a body with no return the function's", async () => { |
| 147 | + const source = `name End; |
| 148 | +define { |
| 149 | + function g(n: uint256) { r = n; }; |
| 150 | +} |
| 151 | +storage { [0] r: uint256; } |
| 152 | +create {} |
| 153 | +code { g(3); g(4); }`; |
| 154 | + for (const level of [0, 1] as const) { |
| 155 | + const result = await compile({ |
| 156 | + to: "bytecode", |
| 157 | + source, |
| 158 | + optimizer: { level }, |
| 159 | + }); |
| 160 | + if (!result.success) throw new Error("compilation failed"); |
| 161 | + const exits = result.value.bytecode.runtimeProgram.instructions.filter( |
| 162 | + ({ operation, context }) => |
| 163 | + operation?.mnemonic === "JUMP" && |
| 164 | + context && |
| 165 | + Context.isReturn(context), |
| 166 | + ); |
| 167 | + expect(exits.length).toBe(1); |
| 168 | + const { code, return: info } = exits[0] |
| 169 | + .context as Format.Program.Context.Code & Format.Program.Context.Return; |
| 170 | + expect(info.declaration).toBeDefined(); |
| 171 | + expect(code).toEqual(info.declaration); |
| 172 | + } |
| 173 | + }); |
| 174 | +}); |
0 commit comments