/** * An instruction list under construction, with its source positions. * * Two things live here rather than in the passes that emit code, because both * are easy to get subtly wrong in a hundred places and right in one. * * **Positions.** `compiled-program.md` 2.15 stores one triple per change of * position, and the position of any instruction is the triple with the greatest * index at or below it. So the builder is told where it is and works out which * triples to keep; a pass that appended a triple per instruction would produce a * correct program and a debug table four times the size of the code. * * **Stack depth.** Section 3.5 check 5 is the verifier's, and a compiler that * cannot compute the same number cannot know it emitted a loadable program. The * builder walks its own list on demand, so an emitter can assert the depth is * zero where the language says a statement has finished. */ import type { Span } from '../span/index.js'; import type { Instruction, Position } from './program.js'; import type { Opcode } from './opcodes.js'; export declare class CodeBuilder { private readonly instructions; private readonly positions; private line; private column; /** Where the instructions pushed after this call came from. */ at(span: Span): void; /** The index the next instruction will take, which is what a jump targets. */ get here(): number; /** * Appends one instruction and returns its index. * * The operand count is checked here because an instruction with the wrong * number of operands is a program an engine refuses at load with one code and * no line (3.5 check 2), and this is the last place that still knows the line. */ push(opcode: Opcode, ...operands: number[]): number; /** Fills in a jump whose target was not known when it was emitted. */ patch(index: number, operand: number, target: number): void; get code(): readonly Instruction[]; get pos(): readonly Position[]; } /** What walking an instruction list found, or the first place it stopped making sense. */ export interface Walk { /** The depth at each instruction, before it executes. */ readonly depths: readonly number[]; /** The first instruction whose depth two paths disagreed about, or nothing. */ readonly conflict: number | undefined; /** The first instruction the walk reached with a negative depth, or nothing. */ readonly underflow: number | undefined; /** * The first `RET` or `HALT` the stack is not empty at, or nothing. * * Section 3.5 check 5 is three sentences and this is the third: the depth * agrees on every path, it never goes below zero, and **it is zero at the * terminator**. Only the first two were walked here, and the difference is * not academic. A `RET` reaches nothing after it, so an expression that left * the stack one short took it to minus one exactly at the `RET`, where * nothing was ever asked; the walk finished clean and a body whose stack does * not add up was emitted, for an engine to refuse at load with a code about * the compiler. Every `RET` is checked rather than the last one, because an * early `return` is a terminator too and owes the same debt. */ readonly terminal: number | undefined; } /** * The stack depth at every instruction, computed exactly as section 3.5 does. * * A forward pass is enough because every backward jump in a program this * compiler emits targets a `TICK` at a loop header, and a loop body leaves the * stack as it found it. The walk still records a disagreement rather than * assuming one, so a bug in a pass shows up here and not in an engine. */ export declare function walkDepths(code: readonly Instruction[], argcOf: (site: number) => number): Walk; //# sourceMappingURL=code.d.ts.map