diff --git a/lib/lua/compiler.ex b/lib/lua/compiler.ex index d3cd230f..728c9479 100644 --- a/lib/lua/compiler.ex +++ b/lib/lua/compiler.ex @@ -8,6 +8,8 @@ defmodule Lua.Compiler do alias Lua.AST.Chunk alias Lua.Compiler.Bytecode alias Lua.Compiler.Codegen + alias Lua.Compiler.GotoResolution + alias Lua.Compiler.GotoValidation alias Lua.Compiler.Prototype alias Lua.Compiler.Scope @@ -27,9 +29,19 @@ defmodule Lua.Compiler do """ @spec compile(Chunk.t(), compile_opts()) :: {:ok, Prototype.t()} | {:error, term()} def compile(%Chunk{} = chunk, opts \\ []) do - with {:ok, scope_state} <- Scope.resolve(chunk, opts), + with :ok <- GotoValidation.validate(chunk), + {:ok, scope_state} <- Scope.resolve(chunk, opts), {:ok, prototype} <- Codegen.generate(chunk, scope_state, opts) do - {:ok, Bytecode.compile(prototype)} + # Encode bytecode first (it reads the raw `:goto` / `:label` stream), + # then resolve gotos for the list interpreter. The two passes are + # independent: the dispatcher runs `bytecode`, the interpreter runs the + # resolved `instructions` plus `goto_targets`. + prototype = + prototype + |> Bytecode.compile() + |> GotoResolution.resolve() + + {:ok, prototype} end end @@ -38,13 +50,13 @@ defmodule Lua.Compiler do """ @spec compile!(Chunk.t(), compile_opts()) :: Prototype.t() def compile!(%Chunk{} = chunk, opts \\ []) do - # `compile/2`'s spec allows `{:error, _}` for forward compatibility, but the - # codegen path doesn't yet have an error-returning code path — codegen - # surfaces unsupported constructs by raising directly. Once codegen is - # converted to thread `{:ok, _} | {:error, _}` through every clause, swap - # this back to a `case` that re-raises `{:error, reason}` as a clear - # compiler exception. - {:ok, prototype} = compile(chunk, opts) - prototype + # Codegen surfaces unsupported constructs by raising directly, but the + # goto legality pass (`Lua.Compiler.GotoValidation`) returns `{:error, + # message}` for programs PUC-Lua rejects at compile time. Re-raise those as + # a clear compiler exception. + case compile(chunk, opts) do + {:ok, prototype} -> prototype + {:error, message} -> raise Lua.CompilerException, message + end end end diff --git a/lib/lua/compiler/bytecode.ex b/lib/lua/compiler/bytecode.ex index 74e08b68..bb8702be 100644 --- a/lib/lua/compiler/bytecode.ex +++ b/lib/lua/compiler/bytecode.ex @@ -68,10 +68,10 @@ defmodule Lua.Compiler.Bytecode do @op_numeric_for 36 # B5c-v2: closures, upvalues, varargs, multi-return, loops, self, concat. - # The bitwise family and the `:set_list` multi-return tail (below) since - # joined the covered set, so the only opcodes the encoder still rejects - # are `:goto` / `:label` (label-resolution semantics not yet ported to the - # dispatcher's flat tuple model). + # The bitwise family, the `:set_list` multi-return tail (below), and + # `:goto` / `:label` (resolved to `@op_goto` / `@op_label` by + # `resolve_gotos/2`) have all joined the covered set — the encoder no + # longer rejects any opcode current codegen emits. @op_closure 37 @op_set_upvalue 38 @op_get_open_upvalue 39 @@ -104,6 +104,14 @@ defmodule Lua.Compiler.Bytecode do @op_bitwise_not 58 @op_set_list_multi 59 + # `:label` is a no-op that anchors a `goto` target PC. `:goto` is resolved at + # encode time to `{@op_goto, depth, target_pc, level}` (see `resolve_gotos/2`): + # `depth` is the `cont` entries the dispatcher drops to reach the target's + # block, `target_pc` the label's index there, `level` the upvalue-close + # threshold. Mirrors `Lua.Compiler.GotoResolution` for the interpreter. + @op_label 60 + @op_goto 61 + @doc """ Compile a prototype, populating its `bytecode` field on success. @@ -123,7 +131,8 @@ defmodule Lua.Compiler.Bytecode do case encode_list(proto.instructions, [], 0) do {:ok, encoded} -> - %{proto_with_children | bytecode: List.to_tuple(encoded)} + bytecode = encoded |> List.to_tuple() |> resolve_gotos([]) + %{proto_with_children | bytecode: bytecode} :fallback -> proto_with_children @@ -437,13 +446,134 @@ defmodule Lua.Compiler.Bytecode do defp encode(:break), do: {:ok, {@op_break}} - # Anything else — `:goto` / `:label` (label-resolution semantics not - # ported to the dispatcher's flat tuple model) — stays on the interpreter. - # - # `:source_line` is stripped upstream in `encode_list/2`, so it never - # reaches this clause table. + # `:label` keeps its name, scope `level`, and lexical block path so + # `resolve_gotos/2` can map goto targets to PCs, close levels, and check + # visibility; the path is dropped from the encoded output and at run time the + # label is a no-op. `:goto` is emitted as a placeholder carrying its own + # block path and rewritten by `resolve_gotos/2` once every block's label PCs + # are known. + defp encode({:label, name, level, block_path}), do: {:ok, {@op_label, name, level, block_path}} + + defp encode({:goto, name, block_path}), do: {:ok, {:pending_goto, name, block_path}} + + # Anything else stays on the interpreter. `:source_line` is stripped + # upstream in `encode_list/2`, so it never reaches this clause table. defp encode(_other), do: :fallback + # ── Goto resolution ───────────────────────────────────────────────────── + # + # Walk the encoded tuple tree, rewriting each `{:pending_goto, name}` to + # `{@op_goto, depth, target_pc, level}`. Mirrors `Lua.Compiler.GotoResolution` + # (interpreter) but over encoded tuples with PCs: `depth` counts the `cont` + # entries the dispatcher drops (a `:test` branch pushes one, a loop body + # two), `target_pc` is the `{@op_label, ...}` index in the destination + # tuple, `level` its scope close threshold. `ancestors` is a stack of + # `{labels, child_pc, weight}` from the innermost enclosing tuple outward. + + # `cont` entries each construct pushes — must match the dispatcher. + @test_weight 1 + @loop_weight 2 + + defp resolve_gotos(code, ancestors) do + entries = Tuple.to_list(code) + labels = collect_labels(entries) + + entries + |> Enum.with_index(1) + |> Enum.map(fn {entry, pc} -> resolve_entry(entry, pc, labels, ancestors) end) + |> List.to_tuple() + end + + defp collect_labels(entries) do + entries + |> Enum.with_index(1) + |> Enum.flat_map(fn + {{@op_label, name, level, block_path}, pc} -> [{name, pc, level, block_path}] + _ -> [] + end) + end + + # Labels carry their block path for goto resolution; the dispatcher only + # needs `{@op_label, name, level}`, so drop the path in the encoded output. + defp resolve_entry({@op_label, name, level, _block_path}, _pc, _labels, _ancestors) do + {@op_label, name, level} + end + + defp resolve_entry({:pending_goto, name, block_path}, pc, labels, ancestors) do + # Illegal gotos (undefined / out-of-scope target) are rejected up front by + # `Lua.Compiler.GotoValidation` as a compile error, so resolution always + # succeeds here. + {:ok, depth, target_pc, level} = resolve_goto_target(name, block_path, labels, pc, ancestors) + {@op_goto, depth, target_pc, level} + end + + defp resolve_entry({@op_test, reg, then_bc, else_bc}, pc, labels, ancestors) do + anc = [{labels, pc, @test_weight} | ancestors] + {@op_test, reg, resolve_gotos(then_bc, anc), resolve_gotos(else_bc, anc)} + end + + defp resolve_entry({@op_numeric_for, base, loop_var, body_bc}, pc, labels, ancestors) do + {@op_numeric_for, base, loop_var, resolve_gotos(body_bc, [{labels, pc, @loop_weight} | ancestors])} + end + + defp resolve_entry({@op_while_loop, test_reg, cond_bc, body_bc}, pc, labels, ancestors) do + {@op_while_loop, test_reg, cond_bc, resolve_gotos(body_bc, [{labels, pc, @loop_weight} | ancestors])} + end + + defp resolve_entry({@op_repeat_loop, test_reg, body_bc, cond_bc}, pc, labels, ancestors) do + {@op_repeat_loop, test_reg, resolve_gotos(body_bc, [{labels, pc, @loop_weight} | ancestors]), cond_bc} + end + + defp resolve_entry({@op_generic_for, base, var_regs, body_bc, line}, pc, labels, ancestors) do + {@op_generic_for, base, var_regs, resolve_gotos(body_bc, [{labels, pc, @loop_weight} | ancestors]), line} + end + + defp resolve_entry(other, _pc, _labels, _ancestors), do: other + + defp resolve_goto_target(name, goto_path, labels, from_pc, ancestors) do + case find_label(labels, name, goto_path, from_pc) do + {:ok, target_pc, level} -> {:ok, 0, target_pc, level} + :error -> resolve_in_ancestors(name, goto_path, ancestors, 0) + end + end + + defp resolve_in_ancestors(_name, _goto_path, [], _depth), do: :error + + defp resolve_in_ancestors(name, goto_path, [{labels, from_pc, weight} | rest], depth) do + depth = depth + weight + + case find_label(labels, name, goto_path, from_pc) do + {:ok, target_pc, level} -> {:ok, depth, target_pc, level} + :error -> resolve_in_ancestors(name, goto_path, rest, depth) + end + end + + # Nearest visible label after `from_pc`, else nearest before — matching the + # interpreter's `GotoResolution`. A label is visible when its block path is + # a prefix of the goto's, excluding nested and sibling `do` blocks flattened + # into the same tuple (Lua 5.3 §3.3.4). + defp find_label(labels, name, goto_path, from_pc) do + matching = + Enum.filter(labels, fn {n, _, _, label_path} -> n == name and visible?(label_path, goto_path) end) + + after_label = + matching |> Enum.filter(fn {_, pc, _, _} -> pc > from_pc end) |> Enum.min_by(&elem(&1, 1), fn -> nil end) + + before_label = + matching |> Enum.filter(fn {_, pc, _, _} -> pc < from_pc end) |> Enum.max_by(&elem(&1, 1), fn -> nil end) + + case after_label || before_label do + {_name, pc, level, _path} -> {:ok, pc, level} + nil -> :error + end + end + + # A label is visible to a goto when the label's block path is a prefix of + # the goto's — the label's block encloses (or equals) the goto's. + defp visible?([], _goto_path), do: true + defp visible?([h | label_rest], [h | goto_rest]), do: visible?(label_rest, goto_rest) + defp visible?(_label_path, _goto_path), do: false + # ── Opcode tag accessors ──────────────────────────────────────────────── # # Exposed for `Lua.VM.Dispatcher` (which mirrors these as its own @@ -510,4 +640,6 @@ defmodule Lua.Compiler.Bytecode do def op_shift_right, do: @op_shift_right def op_bitwise_not, do: @op_bitwise_not def op_set_list_multi, do: @op_set_list_multi + def op_label, do: @op_label + def op_goto, do: @op_goto end diff --git a/lib/lua/compiler/codegen.ex b/lib/lua/compiler/codegen.ex index 4d18bca8..9c9611ec 100644 --- a/lib/lua/compiler/codegen.ex +++ b/lib/lua/compiler/codegen.ex @@ -127,7 +127,7 @@ defmodule Lua.Compiler.Codegen do defp instruction_size({:call, base, _ac, _rc, _hint}), do: base + 1 defp instruction_size({:source_line, _line, _src}), do: 0 defp instruction_size({:close_upvalues, _threshold}), do: 0 - defp instruction_size({:label, _name}), do: 0 + defp instruction_size({:label, _name, _level, _block_path}), do: 0 defp instruction_size({:set_list, _table, start, count, _offset}) when is_integer(count), do: start + count defp instruction_size({:set_list, _table, start, {:multi, init}, _offset}), do: start + init defp instruction_size({:numeric_for, base, _loop_var, body}), do: max(base + 3, instruction_peak(body)) @@ -152,7 +152,7 @@ defmodule Lua.Compiler.Codegen do # are annotations. Reads of an existing register never raise the peak, so # all of these contribute nothing. defp instruction_size(:break), do: 0 - defp instruction_size({:goto, _label}), do: 0 + defp instruction_size({:goto, _label, _block_path}), do: 0 defp instruction_size({:return, _base, _count}), do: 0 defp instruction_size({:return_vararg}), do: 0 defp instruction_size({:set_table, _table, _key, _value, _hint}), do: 0 @@ -851,13 +851,28 @@ defmodule Lua.Compiler.Codegen do end # Goto statement - defp gen_statement(%Statement.Goto{label: label}, ctx) do - {[{:goto, label}], ctx} + # + # Carries the lexical block path recorded by scope resolution so goto + # resolution (both engines) can bind the goto only to a label visible from + # its own block or an enclosing one, never a nested or sibling block + # (Lua 5.3 §3.3.4). + defp gen_statement(%Statement.Goto{label: label} = goto, ctx) do + block_path = Map.fetch!(ctx.scope.var_map, {:goto_block, goto}) + {[{:goto, label, block_path}], ctx} end # Label statement - defp gen_statement(%Statement.Label{name: name}, ctx) do - {[{:label, name}], ctx} + # + # The label carries the live-local register level recorded by scope + # resolution (`next_register` counts locals only, excluding temps) so a + # `goto` targeting it knows the close threshold: jumping to the label closes + # any open upvalue cell for a local allocated beyond this level (a local + # declared deeper in the block being re-entered or exited). See + # `Lua.Compiler.GotoResolution`. + defp gen_statement(%Statement.Label{name: name} = label, ctx) do + level = Map.fetch!(ctx.scope.var_map, {:label_level, label}) + block_path = Map.fetch!(ctx.scope.var_map, {:label_block, label}) + {[{:label, name, level, block_path}], ctx} end # Stub for other statements diff --git a/lib/lua/compiler/goto_resolution.ex b/lib/lua/compiler/goto_resolution.ex new file mode 100644 index 00000000..8f0a83eb --- /dev/null +++ b/lib/lua/compiler/goto_resolution.ex @@ -0,0 +1,217 @@ +defmodule Lua.Compiler.GotoResolution do + @moduledoc """ + Resolves `goto` / `::label::` ahead of interpreter execution. + + The list interpreter walks an instruction tree and, by the time it reaches + a `:goto`, has already consumed the head of the current block — so it can + neither scan backward nor reach an enclosing block at run time. This pass + resolves every goto up front, rewriting `{:goto, name}` to `{:goto, id}` + and recording in `proto.goto_targets` an entry + `id => {depth, level, target_tail}`: + + * `depth` — the number of `cont` entries the interpreter drops to leave + the blocks between the goto and its target. A `:test` branch pushes one + `cont` entry, each loop body pushes two (a CPS marker plus a + `:loop_exit`); `depth` is the sum across the boundaries crossed, so + after dropping it the remaining `cont` is exactly what was active when + the destination block was first entered. + + * `level` — the target label's scope register level (recorded by codegen + on the `{:label, name, level}` instruction). The goto closes any open + upvalue cell at or above this register, so locals in the blocks it + leaves or re-enters get fresh cells (Lua 5.3 §3.3.4 block-exit). + + * `target_tail` — the instruction sublist immediately after the matching + `{:label, name, level}` in its owning block. + + Label scope (Lua 5.3 §3.3.4): a goto targets a label in its own block or an + enclosing one, never inside a nested block or a sibling block. `do` blocks + are flattened by codegen into the parent's instruction list, so several + blocks' labels can share one flat list. Each `{:label, name, level, path}` + and `{:goto, name, path}` carries the lexical block `path` recorded by scope + resolution (innermost-last, unique per `do` block). A label is visible to a + goto only when the label's path is a prefix of the goto's path — i.e. the + label sits in the goto's own block or one enclosing it. Among visible + candidates the nearest after the goto (else the nearest before) is chosen, + matching the forward `continue` / `break` idioms and the backward-jump loop. + + Gotos never cross a function boundary, so each prototype is resolved + independently against its own instruction tree. + """ + + alias Lua.Compiler.Prototype + + # `cont` entries pushed when the interpreter enters a nested block. + @test_weight 1 + @loop_weight 2 + + @doc """ + Resolve gotos in `proto` and every nested prototype, returning the proto + with rewritten `instructions` and a populated `goto_targets` map. + """ + @spec resolve(Prototype.t()) :: Prototype.t() + def resolve(%Prototype{} = proto) do + proto = %{proto | prototypes: Enum.map(proto.prototypes, &resolve/1)} + + {tree, _next, id_to_name} = assign_ids(proto.instructions, 0, %{}) + targets = collect_targets(tree, [], %{}, id_to_name) + + %{proto | instructions: tree, goto_targets: targets} + end + + # ── Pass 1: assign each goto a unique id ──────────────────────────────── + # + # Rewriting `{:goto, name}` to `{:goto, id}` keeps the instruction a single + # element, so block structure and label positions are unchanged — pass 2 + # can compute target tails directly against this tree. + + defp assign_ids(list, counter, names) when is_list(list) do + {acc, counter, names} = + Enum.reduce(list, {[], counter, names}, fn instr, {acc, c, n} -> + {instr, c, n} = assign_instr(instr, c, n) + {[instr | acc], c, n} + end) + + {Enum.reverse(acc), counter, names} + end + + defp assign_instr({:goto, name, block_path}, c, names), do: {{:goto, c}, c + 1, Map.put(names, c, {name, block_path})} + + defp assign_instr({:test, reg, then_body, else_body}, c, names) do + {then_body, c, names} = assign_ids(then_body, c, names) + {else_body, c, names} = assign_ids(else_body, c, names) + {{:test, reg, then_body, else_body}, c, names} + end + + defp assign_instr({:numeric_for, base, loop_var, body}, c, names) do + {body, c, names} = assign_ids(body, c, names) + {{:numeric_for, base, loop_var, body}, c, names} + end + + defp assign_instr({:while_loop, cond_body, test_reg, body}, c, names) do + {body, c, names} = assign_ids(body, c, names) + {{:while_loop, cond_body, test_reg, body}, c, names} + end + + defp assign_instr({:repeat_loop, body, cond_body, test_reg}, c, names) do + {body, c, names} = assign_ids(body, c, names) + {{:repeat_loop, body, cond_body, test_reg}, c, names} + end + + defp assign_instr({:generic_for, base, var_regs, body}, c, names) do + {body, c, names} = assign_ids(body, c, names) + {{:generic_for, base, var_regs, body}, c, names} + end + + defp assign_instr(other, c, names), do: {other, c, names} + + # ── Pass 2: resolve each goto to {depth, target_tail} ─────────────────── + # + # `ancestors` is a stack of `{parent_block, child_index, weight}` from the + # innermost enclosing block outward, where `weight` is the number of `cont` + # entries crossing from the inner block into `parent_block` costs. + + defp collect_targets(block, ancestors, targets, names) do + block + |> Enum.with_index() + |> Enum.reduce(targets, fn {instr, idx}, targets -> + resolve_instr(instr, idx, block, ancestors, targets, names) + end) + end + + defp resolve_instr({:goto, id}, idx, block, ancestors, targets, names) do + {name, block_path} = Map.fetch!(names, id) + + case resolve_target(name, block_path, block, idx, ancestors) do + {:ok, depth, level, tail} -> + Map.put(targets, id, {depth, level, tail}) + + # Unresolved gotos are rejected up front by `Lua.Compiler.GotoValidation` + # as a compile error, so this branch is unreachable in practice; leaving + # the goto unresolved would surface as the interpreter's runtime + # "goto target not found". + :error -> + targets + end + end + + defp resolve_instr({:test, _reg, then_body, else_body}, idx, block, ancestors, targets, names) do + ancestors = [{block, idx, @test_weight} | ancestors] + + targets = collect_targets(then_body, ancestors, targets, names) + collect_targets(else_body, ancestors, targets, names) + end + + defp resolve_instr({:numeric_for, _base, _loop_var, body}, idx, block, ancestors, targets, names) do + collect_targets(body, [{block, idx, @loop_weight} | ancestors], targets, names) + end + + defp resolve_instr({:while_loop, _cond_body, _test_reg, body}, idx, block, ancestors, targets, names) do + collect_targets(body, [{block, idx, @loop_weight} | ancestors], targets, names) + end + + defp resolve_instr({:repeat_loop, body, _cond_body, _test_reg}, idx, block, ancestors, targets, names) do + collect_targets(body, [{block, idx, @loop_weight} | ancestors], targets, names) + end + + defp resolve_instr({:generic_for, _base, _var_regs, body}, idx, block, ancestors, targets, names) do + collect_targets(body, [{block, idx, @loop_weight} | ancestors], targets, names) + end + + defp resolve_instr(_other, _idx, _block, _ancestors, targets, _names), do: targets + + # Search the current block, then each enclosing block, summing `cont` + # weights along the way. Returns `{:ok, depth, level, target_tail}` where + # `level` is the target label's scope register level (the goto's close + # threshold). `goto_path` is the goto's lexical block path; only labels in + # the goto's own block or an enclosing one are visible. + defp resolve_target(name, goto_path, block, idx, ancestors) do + case find_label_pos(block, name, goto_path, idx) do + {:ok, pos, level} -> {:ok, 0, level, Enum.drop(block, pos + 1)} + :error -> resolve_in_ancestors(name, goto_path, ancestors, 0) + end + end + + defp resolve_in_ancestors(_name, _goto_path, [], _depth), do: :error + + defp resolve_in_ancestors(name, goto_path, [{parent_block, child_idx, weight} | rest], depth) do + depth = depth + weight + + case find_label_pos(parent_block, name, goto_path, child_idx) do + {:ok, pos, level} -> {:ok, depth, level, Enum.drop(parent_block, pos + 1)} + :error -> resolve_in_ancestors(name, goto_path, rest, depth) + end + end + + # The visible label nearest after `from`, else the nearest before it. A + # label is visible when its block path is a prefix of the goto's path (the + # label is in the goto's block or one enclosing it) — this excludes nested + # and sibling `do` blocks flattened into the same list (Lua 5.3 §3.3.4). + # Returns the chosen position and its codegen-recorded scope `level`. + defp find_label_pos(block, name, goto_path, from) do + labels = + block + |> Enum.with_index() + |> Enum.flat_map(fn + {{:label, ^name, level, label_path}, i} -> + if visible?(label_path, goto_path), do: [{i, level}], else: [] + + _ -> + [] + end) + + after_label = labels |> Enum.filter(fn {i, _} -> i > from end) |> Enum.min_by(&elem(&1, 0), fn -> nil end) + before_label = labels |> Enum.filter(fn {i, _} -> i < from end) |> Enum.max_by(&elem(&1, 0), fn -> nil end) + + case after_label || before_label do + {pos, level} -> {:ok, pos, level} + nil -> :error + end + end + + # A label is visible to a goto when the label's block path is a prefix of + # the goto's — i.e. the label's block encloses (or equals) the goto's. + defp visible?([], _goto_path), do: true + defp visible?([h | label_rest], [h | goto_rest]), do: visible?(label_rest, goto_rest) + defp visible?(_label_path, _goto_path), do: false +end diff --git a/lib/lua/compiler/goto_validation.ex b/lib/lua/compiler/goto_validation.ex new file mode 100644 index 00000000..394603e6 --- /dev/null +++ b/lib/lua/compiler/goto_validation.ex @@ -0,0 +1,248 @@ +defmodule Lua.Compiler.GotoValidation do + @moduledoc """ + Static legality check for `goto` / `::label::` (Lua 5.3 §3.3.4). + + PUC-Lua rejects illegal gotos at compile time; this pass mirrors that so + both VM engines refuse the same programs before either goto resolver runs. + It walks the AST function by function (gotos never cross a function + boundary) and reports the first violation as `{:error, message}`: + + * a label defined twice in the same block — `label '%s' already defined`; + * a `goto` whose label is not visible from its block or any enclosing one + (undefined, or hidden inside a nested/sibling block) — + `no visible label '%s' for goto`; + * a forward `goto` that jumps into the scope of a local — i.e. it crosses + a local declaration that is still in scope at the target label, and the + label is not at the end of its block — `jumps into the scope of local + '%s'`. + + Active-local counts (`nact`) are tracked function-wide, like PUC's + `nactvar`. When a goto leaves a block unresolved its count is clamped to the + block's entry level (mirroring PUC's `leaveblock`), so the local-scope check + stays sound across block boundaries. + """ + + alias Lua.AST.Block + alias Lua.AST.Chunk + alias Lua.AST.Expr + alias Lua.AST.Statement + + @doc """ + Validate every function in `chunk`. Returns `:ok` or `{:error, message}`. + """ + @spec validate(Chunk.t()) :: :ok | {:error, String.t()} + def validate(%Chunk{block: block}) do + validate_function(block) + end + + # ── Per-function validation ────────────────────────────────────────────── + # + # A function body is the root block. We resolve all of its labels and + # gotos; any goto that escapes the root block unresolved targets a + # non-visible label. + + defp validate_function(%Block{} = block) do + case check_block(block, new_ctx()) do + {:ok, %{pending: [{name, _nact, _depth} | _]}} -> + {:error, "no visible label '#{name}' for goto"} + + {:ok, _ctx} -> + :ok + + {:error, _} = err -> + err + end + end + + defp new_ctx, do: %{nact: 0, pending: [], labels: %{}, locals: [], depth: 0, block_kind: :block} + + # ── Block walk ─────────────────────────────────────────────────────────── + # + # `ctx.nact` is the function-wide active-local count on entry. `ctx.labels` + # accumulates every label still in scope (its own block plus enclosing + # ones), keyed by name to `{nact, end?, depth}`; enclosing labels stay + # visible so a goto can jump backward out to them. `ctx.pending` is the list + # of forward gotos not yet matched. + # + # Returns `{:ok, ctx}` with `nact`, `locals`, `depth`, and `labels` restored + # to the block's entry state (inner locals and labels leave scope), and + # `pending` carrying every goto still unresolved — to be matched by an + # enclosing block's later labels. + + defp check_block(block, ctx), do: check_block(block, ctx, :block) + + defp check_block(%Block{stmts: stmts}, ctx, kind) do + entry_nact = ctx.nact + entry_locals = ctx.locals + entry_labels = ctx.labels + entry_depth = ctx.depth + entry_kind = ctx.block_kind + + with {:ok, ctx} <- walk(stmts, %{ctx | depth: entry_depth + 1, block_kind: kind}) do + # Gotos still pending when this block ends leave it: their locals go out + # of scope, so clamp each one's active-local count down to this block's + # entry level (PUC's `leaveblock` does the same). Without the clamp a + # later enclosing label would compare against an inflated count and miss + # a jump into the scope of an enclosing local. + pending = + Enum.map(ctx.pending, fn {name, goto_nact, goto_depth} -> + {name, min(goto_nact, entry_nact), goto_depth} + end) + + ctx = %{ + ctx + | nact: entry_nact, + locals: entry_locals, + labels: entry_labels, + depth: entry_depth, + block_kind: entry_kind, + pending: pending + } + + {:ok, ctx} + end + end + + # Whether a label sits at the end of its block (only labels follow). Used for + # the "jump over a local to the end of the block is legal" exception. A + # `repeat` body's labels never count as end-of-block: the `until` condition + # extends the body's local scope, so the locals are still live at the label. + defp at_block_end?(_stmts_after, :repeat), do: false + defp at_block_end?(stmts_after, _kind), do: Enum.all?(stmts_after, &match?(%Statement.Label{}, &1)) + + defp walk([], ctx), do: {:ok, ctx} + + defp walk([stmt | rest], ctx) do + with {:ok, ctx} <- check_statement(stmt, rest, ctx) do + walk(rest, ctx) + end + end + + # ── Statements that affect goto legality ───────────────────────────────── + + defp check_statement(%Statement.Label{name: name}, rest, ctx) do + case Map.get(ctx.labels, name) do + {_nact, _end?, depth} when depth == ctx.depth -> + {:error, "label '#{name}' already defined"} + + _ -> + label_end? = at_block_end?(rest, ctx.block_kind) + ctx = %{ctx | labels: Map.put(ctx.labels, name, {ctx.nact, label_end?, ctx.depth})} + resolve_pending(name, ctx.nact, ctx.depth, label_end?, ctx) + end + end + + defp check_statement(%Statement.Goto{label: name}, _rest, ctx) do + case Map.get(ctx.labels, name) do + # Backward jump to a label already in scope (this block or an enclosing + # one). A backward jump lands where its locals were already in scope, so + # it can never jump into a local's scope — accept unconditionally. + {_label_nact, _end?, _depth} -> + {:ok, ctx} + + nil -> + # Forward (or enclosing-but-later) reference: defer. The goto's active + # local count and block depth are captured so the local-scope and + # visibility checks can run when the label appears. + {:ok, %{ctx | pending: [{name, ctx.nact, ctx.depth} | ctx.pending]}} + end + end + + defp check_statement(%Statement.Local{names: names}, _rest, ctx) do + {:ok, %{ctx | nact: ctx.nact + length(names), locals: ctx.locals ++ names}} + end + + defp check_statement(%Statement.LocalFunc{name: name} = lf, _rest, ctx) do + with {:ok, _} <- as_ok(validate_function(lf.body)) do + {:ok, %{ctx | nact: ctx.nact + 1, locals: ctx.locals ++ [name]}} + end + end + + defp check_statement(%Statement.Do{body: body}, _rest, ctx) do + check_block(body, ctx) + end + + defp check_statement(%Statement.If{then_block: then_b, elseifs: elseifs, else_block: else_b}, _rest, ctx) do + blocks = [then_b | Enum.map(elseifs, fn {_cond, blk} -> blk end)] ++ List.wrap(else_b) + reduce_blocks(blocks, ctx) + end + + defp check_statement(%Statement.While{body: body}, _rest, ctx), do: check_block(body, ctx) + + defp check_statement(%Statement.Repeat{body: body}, _rest, ctx), do: check_block(body, ctx, :repeat) + + defp check_statement(%Statement.ForNum{var: var, body: body}, _rest, ctx) do + check_loop_block(body, [var], ctx) + end + + defp check_statement(%Statement.ForIn{vars: vars, body: body}, _rest, ctx) do + check_loop_block(body, vars, ctx) + end + + # Function declarations open their own goto scope. + defp check_statement(%Statement.FuncDecl{body: body}, _rest, ctx) do + with {:ok, _} <- as_ok(validate_function(body)) do + {:ok, ctx} + end + end + + defp check_statement(%Expr.Function{body: body}, _rest, ctx) do + with {:ok, _} <- as_ok(validate_function(body)) do + {:ok, ctx} + end + end + + # Everything else is goto-irrelevant, but it may contain a function literal + # in an expression position — those are validated when scope resolution + # reaches them, so nothing more is needed here. + defp check_statement(_stmt, _rest, ctx), do: {:ok, ctx} + + # ── Helpers ────────────────────────────────────────────────────────────── + + # The loop variable(s) are in scope for the body. Bump `nact` accordingly + # for the body walk, then restore. + defp check_loop_block(body, vars, ctx) do + inner = %{ctx | nact: ctx.nact + length(vars), locals: ctx.locals ++ vars} + + with {:ok, inner} <- check_block(body, inner) do + {:ok, %{inner | nact: ctx.nact, locals: ctx.locals}} + end + end + + defp reduce_blocks(blocks, ctx) do + Enum.reduce_while(blocks, {:ok, ctx}, fn block, {:ok, ctx} -> + case check_block(block, ctx) do + {:ok, ctx} -> {:cont, {:ok, ctx}} + {:error, _} = err -> {:halt, err} + end + end) + end + + # When a label at `label_depth` appears, resolve the pending forward gotos it + # can satisfy. A label can only be the target of a goto in its own block or a + # nested one (`goto_depth >= label_depth`); a goto in an enclosing block + # (`goto_depth < label_depth`) cannot jump into this nested block, so it is + # left pending to bubble outward. A resolved goto is illegal if it jumps into + # the scope of a local: the label sees more active locals than the goto did, + # and the label is not at the end of its block. + defp resolve_pending(name, label_nact, label_depth, label_end?, ctx) do + {matched, rest} = + Enum.split_with(ctx.pending, fn {n, _nact, goto_depth} -> + n == name and goto_depth >= label_depth + end) + + case Enum.find(matched, fn {_n, goto_nact, _depth} -> label_nact > goto_nact and not label_end? end) do + {_n, goto_nact, _depth} -> + # The crossed local is the first one in scope at the label that was not + # in scope at the goto — index `goto_nact` in the label's live locals. + crossed = Enum.at(ctx.locals, goto_nact, "?") + {:error, "jumps into the scope of local '#{crossed}'"} + + nil -> + {:ok, %{ctx | pending: rest}} + end + end + + defp as_ok(:ok), do: {:ok, :ok} + defp as_ok({:error, _} = err), do: err +end diff --git a/lib/lua/compiler/prototype.ex b/lib/lua/compiler/prototype.ex index ec1ce22d..a6d0933d 100644 --- a/lib/lua/compiler/prototype.ex +++ b/lib/lua/compiler/prototype.ex @@ -22,7 +22,10 @@ defmodule Lua.Compiler.Prototype do max_registers: non_neg_integer(), source: binary(), lines: {non_neg_integer(), non_neg_integer()}, - bytecode: tuple() | nil + bytecode: tuple() | nil, + goto_targets: %{ + non_neg_integer() => {non_neg_integer(), non_neg_integer(), [instruction()]} + } } # `bytecode` is an optional dense encoding produced by `Lua.Compiler.Bytecode`. @@ -49,7 +52,8 @@ defmodule Lua.Compiler.Prototype do source: <<"-no-source-">>, lines: {0, 0}, varargs: [], - bytecode: nil + bytecode: nil, + goto_targets: %{} @doc """ Creates a new prototype with the given options. diff --git a/lib/lua/compiler/scope.ex b/lib/lua/compiler/scope.ex index 3badd9ef..05a1efa5 100644 --- a/lib/lua/compiler/scope.ex +++ b/lib/lua/compiler/scope.ex @@ -48,7 +48,9 @@ defmodule Lua.Compiler.Scope do next_register: 0, locals: %{}, parent_scopes: [], - captured_locals: MapSet.new() + captured_locals: MapSet.new(), + block_path: [], + block_counter: 0 @type t :: %__MODULE__{ var_map: %{optional(term()) => Lua.Compiler.Scope.var_ref()}, @@ -57,7 +59,9 @@ defmodule Lua.Compiler.Scope do next_register: non_neg_integer(), locals: %{optional(binary()) => non_neg_integer()}, parent_scopes: [%{locals: map(), function: term()}], - captured_locals: MapSet.t() + captured_locals: MapSet.t(), + block_path: [non_neg_integer()], + block_counter: non_neg_integer() } end @@ -220,6 +224,7 @@ defmodule Lua.Compiler.Scope do saved_next_register = state.next_register state = %{state | var_map: Map.put(state.var_map, {:block_close_threshold, body}, saved_next_register)} + state = resolve_block(body, state) state = resolve_expr(condition, state) @@ -382,7 +387,18 @@ defmodule Lua.Compiler.Scope do saved_locals = state.locals saved_next_register = state.next_register + # Codegen flat-inlines a `do` block into its parent's instruction list, so + # its labels and gotos share that list with the enclosing block's. Tag the + # block with a unique id pushed onto `block_path` so goto resolution can + # tell a label in this nested block apart from one in the enclosing or a + # sibling block (Lua 5.3 §3.3.4). The path is innermost-last; a label is + # visible to a goto only when the label's path is a prefix of the goto's. + saved_block_path = state.block_path + block_id = state.block_counter + state = %{state | block_counter: block_id + 1, block_path: saved_block_path ++ [block_id]} + state = resolve_block(body, state) + state = %{state | block_path: saved_block_path} # Stash the pre-block register watermark so codegen can emit a # `:close_upvalues` at block exit. Per Lua 5.3 §3.4.10 any open-upvalue @@ -395,6 +411,28 @@ defmodule Lua.Compiler.Scope do %{state | locals: saved_locals, next_register: saved_next_register} end + # Record the live-local register watermark at each label. A `goto` to this + # label closes any open-upvalue cell at or above this register, so locals + # declared deeper in the blocks it leaves or re-enters get fresh cells + # (Lua 5.3 §3.3.4). `next_register` counts locals only (not codegen temps), + # which is exactly the close threshold. Keyed by the label node, which is + # unique per position, so reused names across sibling blocks stay distinct. + defp resolve_statement(%Statement.Label{} = label, state) do + var_map = + state.var_map + |> Map.put({:label_level, label}, state.next_register) + |> Map.put({:label_block, label}, state.block_path) + + %{state | var_map: var_map} + end + + # Record the lexical block path of each `goto` so codegen can tag the + # emitted instruction and goto resolution can match it only against labels + # visible from this block or an enclosing one (Lua 5.3 §3.3.4). + defp resolve_statement(%Statement.Goto{} = goto, state) do + %{state | var_map: Map.put(state.var_map, {:goto_block, goto}, state.block_path)} + end + # For now, stub out other statement types - we'll implement them incrementally defp resolve_statement(_stmt, state), do: state @@ -651,13 +689,19 @@ defmodule Lua.Compiler.Scope do {Map.put(locals, param, index), index + 1} end) + # A nested function body is its own instruction list; gotos never cross a + # function boundary, so its `block_path` starts fresh. `block_counter` + # stays monotonic across the boundary for global uniqueness. + saved_block_path = state.block_path + state = %{ state | locals: param_locals, next_register: next_param_reg, current_function: func_key, parent_scopes: [parent_scope_entry | state.parent_scopes], - captured_locals: MapSet.new() + captured_locals: MapSet.new(), + block_path: [] } func_scope = %FunctionScope{ @@ -720,7 +764,8 @@ defmodule Lua.Compiler.Scope do next_register: saved_next_register, current_function: saved_function, parent_scopes: saved_parent_scopes, - captured_locals: MapSet.union(saved_captured_locals, newly_captured) + captured_locals: MapSet.union(saved_captured_locals, newly_captured), + block_path: saved_block_path } end end diff --git a/lib/lua/vm/dispatcher.ex b/lib/lua/vm/dispatcher.ex index d7c1f204..ff4dcf55 100644 --- a/lib/lua/vm/dispatcher.ex +++ b/lib/lua/vm/dispatcher.ex @@ -117,6 +117,13 @@ defmodule Lua.VM.Dispatcher do @op_bitwise_not 58 @op_set_list_multi 59 + # `@op_label` is a no-op anchoring a goto target. `@op_goto` carries + # `{depth, target_pc, level}`: close open upvalues at or above `level`, drop + # `depth` `cont` entries to leave the intervening blocks, then dispatch the + # destination tuple (recovered from the unwound markers) at `target_pc`. + @op_label 60 + @op_goto 61 + @doc """ Execute a compiled prototype against `args` and `state`. """ @@ -1034,6 +1041,27 @@ defmodule Lua.VM.Dispatcher do {exit_code, exit_pc, rest_cont} = find_loop_exit(cont) dispatch(exit_code, exit_pc, regs, upvalues, proto, state, rest_cont, frames) + # ── label / goto ────────────────────────────────────────────── + # + # `:label` is a no-op. `:goto` closes upvalue cells at or above the + # target's scope `level`, drops `depth` `cont` entries (a `:test` + # branch pushed one, a loop body two), and resumes the destination + # tuple at `target_pc`. `depth == 0` is a jump within the current + # tuple (forward or backward); otherwise the destination tuple is the + # enclosing `code` recorded on the unwound markers. + + {@op_label, _name, _level} -> + dispatch(code, pc + 1, regs, upvalues, proto, state, cont, frames) + + {@op_goto, 0, target_pc, level} -> + state = Executor.dispatcher_close_open_upvalues_at_or_above(state, level) + dispatch(code, target_pc, regs, upvalues, proto, state, cont, frames) + + {@op_goto, depth, target_pc, level} -> + state = Executor.dispatcher_close_open_upvalues_at_or_above(state, level) + {dest_code, rest_cont} = unwind_goto(cont, depth) + dispatch(dest_code, target_pc, regs, upvalues, proto, state, rest_cont, frames) + # ── Closure construction ────────────────────────────────────── # # Walks `nested_proto.upvalue_descriptors`, allocating a new cell @@ -1585,6 +1613,29 @@ defmodule Lua.VM.Dispatcher do defp find_loop_exit([]), do: raise(InternalError, value: "break outside loop") + # `:goto` unwind. Drops exactly `depth` `cont` entries (counted to match the + # encoder: a `:test` branch is one, a loop body two) and returns the + # enclosing `code` tuple recorded on the last entry dropped, into which the + # goto's `target_pc` indexes. `depth >= 1` here (`depth == 0` is handled + # without unwinding). + defp unwind_goto(cont, depth), do: unwind_goto(cont, depth, nil) + + defp unwind_goto(cont, 0, dest_code), do: {dest_code, cont} + + defp unwind_goto([entry | rest], depth, _dest_code) do + unwind_goto(rest, depth - 1, cont_entry_code(entry)) + end + + # The enclosing `code` tuple carried by a `cont` entry: a `:test` resume is + # `{code, pc}` (code first); every loop marker (`:loop_exit`, `:cps_*`) is a + # tagged tuple carrying `code` as its second-to-last element. + defp cont_entry_code(entry) do + case :erlang.element(1, entry) do + tag when is_atom(tag) -> :erlang.element(tuple_size(entry) - 1, entry) + _ -> :erlang.element(1, entry) + end + end + # Vararg setup at the call boundary. Mirrors the executor's per-call # behaviour: when calling a vararg function, regs[param_count..total_args) # become the varargs list carried on `%{proto | varargs: ...}`. diff --git a/lib/lua/vm/executor.ex b/lib/lua/vm/executor.ex index 645f1a26..71f14790 100644 --- a/lib/lua/vm/executor.ex +++ b/lib/lua/vm/executor.ex @@ -652,20 +652,30 @@ defmodule Lua.VM.Executor do end # ── Goto ─────────────────────────────────────────────────────────────────── + # + # `Lua.Compiler.GotoResolution` rewrote each `goto` to `{:goto, id}` and + # recorded `proto.goto_targets[id] = {depth, target_tail}`. Dropping `depth` + # `cont` entries leaves the blocks between here and the target; `target_tail` + # is the destination block's instruction suffix after the `::label::`. + + defp do_execute([{:goto, id} | _rest], regs, upvalues, proto, state, cont, frames, line) do + case proto.goto_targets do + %{^id => {depth, level, target_tail}} -> + # Close upvalue cells for locals allocated beyond the target's scope: + # the blocks the goto leaves (or re-enters, on a backward jump) take + # their captured locals with them, so the next pass through gets fresh + # cells — matching Lua 5.3 §3.3.4 block-exit semantics. + state = close_open_upvalues_at_or_above(state, level) + do_execute(target_tail, regs, upvalues, proto, state, Enum.drop(cont, depth), frames, line) - defp do_execute([{:goto, label} | rest], regs, upvalues, proto, state, cont, frames, line) do - case find_label(rest, label) do - {:found, after_label} -> - do_execute(after_label, regs, upvalues, proto, state, cont, frames, line) - - :not_found -> - raise InternalError, value: "goto target '#{label}' not found" + _ -> + raise InternalError, value: "goto target not found" end end # ── Label ────────────────────────────────────────────────────────────────── - defp do_execute([{:label, _name} | rest], regs, upvalues, proto, state, cont, frames, line) do + defp do_execute([{:label, _name, _level, _block_path} | rest], regs, upvalues, proto, state, cont, frames, line) do do_execute(rest, regs, upvalues, proto, state, cont, frames, line) end @@ -2225,29 +2235,6 @@ defmodule Lua.VM.Executor do defp find_loop_exit([_ | rest_cont]), do: find_loop_exit(rest_cont) defp find_loop_exit([]), do: raise(InternalError, value: "break outside loop") - # ── find_label — scan instruction list for a :label marker ──────────────── - - defp find_label([], _label), do: :not_found - - defp find_label([{:label, name} | rest], label) when name == label do - {:found, rest} - end - - defp find_label([{:test, _reg, then_body, else_body} | rest], label) do - case find_label(then_body, label) do - {:found, _} = found -> - found - - :not_found -> - case find_label(else_body, label) do - {:found, _} = found -> found - :not_found -> find_label(rest, label) - end - end - end - - defp find_label([_ | rest], label), do: find_label(rest, label) - # ── call_value — invoke a function for generic_for iterator ─────────────── defp call_value({:lua_closure, callee_proto, callee_upvalues}, args, _proto, state, _line) do diff --git a/tasks/suite_runner.ex b/tasks/suite_runner.ex index ad89ea73..9fdf7f1e 100644 --- a/tasks/suite_runner.ex +++ b/tasks/suite_runner.ex @@ -154,16 +154,20 @@ defmodule Lua.SuiteRunner do env -> env end - case Lua.Parser.parse(code) do - {:ok, ast} -> - proto = Lua.Compiler.compile!(ast, source: chunk_name) - env_cell = make_ref() - vm = %{vm | upvalue_cells: Map.put(vm.upvalue_cells, env_cell, env)} - closure = {:lua_closure, proto, {env_cell}} - {[closure], %{lua | state: vm}} - - {:error, error} -> - {[nil, "parse error: #{inspect(error)}"], lua} + with {:ok, ast} <- Lua.Parser.parse(code), + {:ok, proto} <- Lua.Compiler.compile(ast, source: chunk_name) do + env_cell = make_ref() + vm = %{vm | upvalue_cells: Map.put(vm.upvalue_cells, env_cell, env)} + closure = {:lua_closure, proto, {env_cell}} + {[closure], %{lua | state: vm}} + else + # Mirror PUC `load`: on a lexer/parser/compile failure return + # `nil, message` rather than raising, so callers can pattern-match the + # error (the suite's `errmsg` helper does `string.find(msg, ...)`). + # Both `Lua.Parser.parse/1` and the compiler's goto-legality pass + # return a plain string message. + {:error, message} -> + {[nil, message], lua} end end) end diff --git a/test/lua/compiler/bytecode_test.exs b/test/lua/compiler/bytecode_test.exs index 750c8e75..b24b2f07 100644 --- a/test/lua/compiler/bytecode_test.exs +++ b/test/lua/compiler/bytecode_test.exs @@ -176,18 +176,13 @@ defmodule Lua.Compiler.BytecodeTest do assert result.bytecode == nil end - test ":goto falls back (label-resolution semantics not ported)" do - # `:goto` / `:label` resolve via a runtime structural scan over the - # remaining instruction list; the dispatcher's flat-tuple model has - # no analogue yet, so the encoder forces fallback. - proto = %Prototype{ - instructions: [{:label, :done}, {:goto, :done}, {:return, 0, 0}], - max_registers: 1, - source: "test-synthetic" - } - - result = Bytecode.compile(proto) - assert result.bytecode == nil + test "short-circuit and/or falls back (test_and/test_or not covered)" do + # `:test_and` / `:test_or` carry a nested continuation body the encoder + # does not lower yet, so a function using short-circuit `and`/`or` keeps + # its prototype on the interpreter. + proto = compile!("function f(a, b) return a and b or 0 end") + [fn_proto] = proto.prototypes + assert fn_proto.bytecode == nil end end @@ -210,15 +205,13 @@ defmodule Lua.Compiler.BytecodeTest do describe "cascade independence" do test "child prototype compiles even when sibling falls back" do - # `pure` is pure arithmetic (covered). `impure` uses a `goto`, which - # is not yet in dispatcher coverage (its own follow-up plan). + # `pure` is pure arithmetic (covered). `impure` uses short-circuit + # `and`/`or` (`:test_and` / `:test_or`), which the encoder does not yet + # cover, so it stays on the interpreter. proto = compile!(""" function pure(a, b) return a + b end - function impure() - ::top:: - goto top - end + function impure(a, b) return a and b or 0 end """) [pure_proto, impure_proto] = proto.prototypes @@ -227,14 +220,13 @@ defmodule Lua.Compiler.BytecodeTest do end test "deeply-nested function compiles even when its parent falls back" do - # The outer `make` uses a `goto` (fallback), but the inner adder is a - # pure-arithmetic single-result function (compiles). + # The outer `make` uses short-circuit `and`/`or` (fallback), but the + # inner adder is a pure-arithmetic single-result function (compiles). proto = compile!(""" - function make() - ::skip:: - goto skip - local function add(a, b) return a + b end + function make(a, b) + local guard = a and b or 0 + local function add(x, y) return x + y + guard end return add end """) @@ -251,8 +243,13 @@ defmodule Lua.Compiler.BytecodeTest do # A representative corpus exercising every covered opcode family. Each # program must compile end-to-end — root chunk and every nested function — # so the dispatcher never silently falls back to the interpreter. The only - # documented exception is `:goto` / `:label` (asserted separately below). + # documented exception is short-circuit `and`/`or` (`:test_and` / + # `:test_or`), asserted separately below. @corpus [ + {"goto forward", "local x = 1 goto skip x = 99 ::skip:: return x"}, + {"goto backward loop", "local i = 0 ::top:: i = i + 1 if i < 5 then goto top end return i"}, + {"goto continue out of if", "local s = 0 for i = 1, 5 do if i == 3 then goto c end s = s + i ::c:: end return s"}, + {"goto break out of loop", "local i = 0 while true do i = i + 1 if i == 3 then goto d end end ::d:: return i"}, {"arithmetic", "function f(a, b) return a + b * 2 - 1 end return f(3, 4)"}, {"comparison + branch", "function f(n) if n < 0 then return -n end return n end return f(-5)"}, {"recursion", "function fib(n) if n < 2 then return n end return fib(n-1)+fib(n-2) end return fib(10)"}, @@ -276,11 +273,12 @@ defmodule Lua.Compiler.BytecodeTest do end end - test "goto/label is the one documented exception (not yet covered)" do - # Until the goto/label port lands, a program using `goto` is expected to - # fall back. This is the only construct codegen emits that the dispatcher - # does not yet cover; when it lands this assertion flips. - refute Bytecode.fully_compiled?(compile!("local i = 0 ::top:: i = i + 1 if i < 3 then goto top end return i")) + test "short-circuit and/or is the remaining documented exception" do + # `:test_and` / `:test_or` (short-circuit `and`/`or`) are the only + # opcodes current codegen emits that the dispatcher does not yet cover, + # so a function using them still falls back to the interpreter. (goto / + # label are now covered — see the corpus above.) + refute Bytecode.fully_compiled?(compile!("local function f(a, b) return a and b or 0 end return f(1, 2)")) end end @@ -337,14 +335,13 @@ defmodule Lua.Compiler.BytecodeTest do end test "fallback returns a Prototype with bytecode: nil, never an error" do - # The encoder must not crash on any well-formed prototype. `goto` - # stays on the interpreter (label-resolution semantics not ported), - # so use one to exercise the fallback path. + # The encoder must not crash on any well-formed prototype. Short-circuit + # `and`/`or` (`:test_and` / `:test_or`) stays on the interpreter, so use + # it to exercise the fallback path. proto = compile!(""" - function f() - ::top:: - goto top + function f(a, b) + return a and b or 0 end """) diff --git a/test/lua/compiler/instruction_size_test.exs b/test/lua/compiler/instruction_size_test.exs index 67e7438c..4f377da6 100644 --- a/test/lua/compiler/instruction_size_test.exs +++ b/test/lua/compiler/instruction_size_test.exs @@ -90,7 +90,7 @@ defmodule Lua.Compiler.InstructionSizeTest do # Stores, control flow, and debug annotations write no fresh register. @non_writers [ {:break, 0}, - {{:goto, :l1}, 0}, + {{:goto, :l1, [0]}, 0}, {{:return, 5, 1}, 0}, {{:return_vararg}, 0}, {{:set_table, 1, 2, 3, nil}, 0}, @@ -99,7 +99,7 @@ defmodule Lua.Compiler.InstructionSizeTest do {{:set_open_upvalue, 2, 3}, 0}, {{:source_line, 10, "f.lua"}, 0}, {{:close_upvalues, 3}, 0}, - {{:label, :l1}, 0} + {{:label, :l1, 0, [0]}, 0} ] describe "instruction_peak classifies every emitted opcode" do diff --git a/test/lua/vm/dispatcher_test.exs b/test/lua/vm/dispatcher_test.exs index cf5a78ad..79507ed2 100644 --- a/test/lua/vm/dispatcher_test.exs +++ b/test/lua/vm/dispatcher_test.exs @@ -63,15 +63,18 @@ defmodule Lua.VM.DispatcherTest do # `proto`. Used to confirm a specific opcode reached bytecode without # assuming which (sub-)prototype owns it. defp encodes_op?(%Prototype{} = proto, op) do - own = - case proto.bytecode do - nil -> false - bc -> bc |> Tuple.to_list() |> Enum.any?(&(:erlang.element(1, &1) == op)) - end + deep_has_op?(proto.bytecode, op) or Enum.any?(proto.prototypes, &encodes_op?(&1, op)) + end - own or Enum.any?(proto.prototypes, &encodes_op?(&1, op)) + # Scans the encoded tuple tree, descending into nested body tuples (`:test` + # branches, loop bodies) so an opcode emitted inside a block is found too. + defp deep_has_op?(t, op) when is_tuple(t) do + (tuple_size(t) > 0 and :erlang.element(1, t) == op) or + Enum.any?(Tuple.to_list(t), &deep_has_op?(&1, op)) end + defp deep_has_op?(_other, _op), do: false + describe "arithmetic opcodes (dispatcher-compiled body)" do test ":add — integer fast path" do {proto, results} = @@ -1310,4 +1313,79 @@ defmodule Lua.VM.DispatcherTest do assert results == [1] end end + + describe ":goto / :label" do + test "forward goto skips statements", %{} do + {proto, results} = + run!(""" + function f() + local x = 1 + goto skip + x = 99 + ::skip:: + return x + end + return f() + """) + + assert encodes_op?(proto, Bytecode.op_goto()) + assert results == [1] + end + + test "backward goto forms a counted loop", %{} do + {proto, results} = + run!(""" + function f() + local i = 0 + ::top:: + i = i + 1 + if i < 5 then goto top end + return i + end + return f() + """) + + assert encodes_op?(proto, Bytecode.op_goto()) + assert results == [5] + end + + test "goto out of a nested loop to a function-level label", %{} do + {proto, results} = + run!(""" + function f() + for a = 1, 3 do + for b = 1, 3 do + if a * b == 6 then goto done end + end + end + ::done:: + return 42 + end + return f() + """) + + assert encodes_op?(proto, Bytecode.op_goto()) + assert results == [42] + end + + test "goto exiting a loop closes captured upvalues", %{} do + {proto, results} = + run!(""" + function f() + local captured + for i = 1, 10 do + local v = i * 2 + captured = function() return v end + if i == 3 then goto stop end + end + ::stop:: + return captured() + end + return f() + """) + + assert encodes_op?(proto, Bytecode.op_goto()) + assert results == [6] + end + end end diff --git a/test/lua/vm/goto_test.exs b/test/lua/vm/goto_test.exs new file mode 100644 index 00000000..818a18bf --- /dev/null +++ b/test/lua/vm/goto_test.exs @@ -0,0 +1,217 @@ +defmodule Lua.VM.GotoTest do + @moduledoc """ + Conformance tests for Lua 5.3 `goto` / `::label::` across both VM engines. + + Each case runs on the interpreter (bytecode stripped) and on the bytecode + dispatcher (bytecode kept) and asserts both produce the same, correct + result. `goto` is non-structured control flow — the interesting cases are + backward jumps, `continue`-style jumps out of an `if`, break-style jumps + out of a loop, jumps across reused label names, and jumps that must close + captured upvalues on the way out of a scope. + """ + + use ExUnit.Case, async: true + + alias Lua.Compiler + alias Lua.Compiler.Prototype + alias Lua.Parser + alias Lua.VM + alias Lua.VM.State + alias Lua.VM.Stdlib + + # Run `src` on whichever engine `mode` selects, returning the result list. + # `:dispatcher` keeps the compiled bytecode; `:interpreter` strips it from + # the whole prototype tree so execution falls back to the list interpreter. + defp run(src, mode) do + {:ok, ast} = Parser.parse(src) + {:ok, proto} = Compiler.compile(ast, source: "goto_test.lua") + proto = if mode == :interpreter, do: strip_proto(proto), else: proto + state = Stdlib.install(State.new()) + {:ok, results, _state} = VM.execute(proto, state) + results + end + + defp strip_proto(%Prototype{} = p) do + %{p | bytecode: nil, prototypes: Enum.map(p.prototypes, &strip_proto/1)} + end + + # Assert both engines agree and match the expected result. + defp assert_both(src, expected) do + assert run(src, :interpreter) == expected, "interpreter mismatch for:\n#{src}" + assert run(src, :dispatcher) == expected, "dispatcher mismatch for:\n#{src}" + end + + # Assert `src` is rejected at compile time with a message matching `pattern`. + defp assert_compile_error(src, pattern) do + {:ok, ast} = Parser.parse(src) + assert {:error, message} = Compiler.compile(ast, source: "goto_test.lua") + assert message =~ pattern, "expected #{inspect(pattern)} in #{inspect(message)}" + end + + describe "goto conformance (both engines)" do + test "forward goto skips statements" do + assert_both("local x = 1; goto skip; x = 99; ::skip:: return x", [1]) + end + + test "backward goto forms a counted loop" do + assert_both( + """ + local i = 0 + ::top:: + i = i + 1 + if i < 5 then goto top end + return i + """, + [5] + ) + end + + test "continue idiom: goto out of an if to a later label in the loop body" do + assert_both( + """ + local sum = 0 + for i = 1, 5 do + if i % 2 == 0 then goto cont end + sum = sum + i + ::cont:: + end + return sum + """, + [9] + ) + end + + test "break-style: goto out of a loop to a label after it" do + assert_both( + """ + local i = 0 + while true do + i = i + 1 + if i == 3 then goto done end + end + ::done:: + return i + """, + [3] + ) + end + + test "goto out of a nested loop to a function-level label" do + assert_both( + """ + local found = nil + for a = 1, 3 do + for b = 1, 3 do + if a * b == 6 then found = a * 10 + b; goto out end + end + end + ::out:: + return found + """, + [23] + ) + end + + test "forward goto jumps over a reused label name to the correct one" do + # Two blocks each define ::l::; the goto must reach the one in scope. + assert_both( + """ + local x = 0 + do goto l; x = 1; ::l:: x = x + 10 end + do goto l; x = x + 100; ::l:: x = x + 1000 end + return x + """, + [1010] + ) + end + + test "goto to a label at end of block falls through to return" do + assert_both( + """ + local function f(n) + if n > 0 then goto positive end + do return "nonpos" end + ::positive:: + return "pos" + end + return f(5), f(-1) + """, + ["pos", "nonpos"] + ) + end + + test "goto exiting a loop closes captured upvalues correctly" do + # The closure captures the loop local `v`; jumping out must close the + # cell so the captured value is the one from the iteration that jumped. + assert_both( + """ + local captured + for i = 1, 10 do + local v = i * 2 + captured = function() return v end + if i == 3 then goto stop end + end + ::stop:: + return captured() + """, + [6] + ) + end + end + + describe "label visibility (Lua 5.3 §3.3.4)" do + test "goto binds to the outer label, never a nested do-block label" do + # The nested `do ::l:: end` must be invisible to the goto; the jump + # reaches the outer ::l::, skipping the `x=100` in the nested block. + assert_both( + "local x=0; goto l; do ::l:: x=100 end; ::l:: x=x+5; return x", + [5] + ) + end + + test "backward goto loops on its own block's label, not a sibling's" do + assert_both( + """ + local out=0 + do ::a:: out=out+1 if out<2 then goto a end end + do goto a out=out+100 ::a:: out=out+1000 end + return out + """, + [1002] + ) + end + + test "sibling blocks reuse a label name independently" do + assert_both( + """ + local a,b=0,0 + do ::l:: a=a+1 if a<2 then goto l end end + do ::l:: b=b+1 if b<3 then goto l end end + return a,b + """, + [2, 3] + ) + end + end + + describe "illegal goto rejection (compile error)" do + test "goto into the scope of a local is rejected" do + assert_compile_error( + "do goto skip; local y = 10; ::skip:: return y end", + "jumps into the scope of local 'y'" + ) + end + + test "duplicate label in the same scope is rejected" do + assert_compile_error("::a:: ::a:: return 1", "label 'a' already defined") + end + + test "undefined label is rejected" do + assert_compile_error("goto nowhere; return 1", "no visible label 'nowhere'") + end + + test "goto into a nested block is rejected" do + assert_compile_error("goto l; do ::l:: end", "no visible label 'l'") + end + end +end diff --git a/test/lua53_skips.exs b/test/lua53_skips.exs index fe4df0f1..d1ce9778 100644 --- a/test/lua53_skips.exs +++ b/test/lua53_skips.exs @@ -158,9 +158,12 @@ ], "goto.lua" => [ %{ - lines: 12..40, + lines: 163..195, category: :stdlib, - reason: "load() parse-error messages do not match PUC-Lua 'label/local' format", + reason: + "asserts upvalue cell identity via debug.upvalueid, which is a stub returning nil " <> + "(upvalue-cell introspection not implemented). The goto control flow these closures " <> + "exercise is still covered: the preceding `assert(#a == 6)` runs.", issue: nil } ],