feature: implement control flow opcodes, extract constants & helpers

## Control Flow Opcodes
- Implemented all 10 remaining stub control flow opcodes:
  block, loop, if, else, end, br, br_if, br_table, call, call_indirect
- Rewrote return opcode to walk control stack and restore call frames
- Fixed tick() hang: now halts when IP >= Limit

## Refactoring
- Extracted WASM_HDR_MAGIC and CTRL_FRAME_* constants to wasm.types.constants.pas
- Extracted push_control_frame, scan_forward, do_branch helpers to wasm.vm.control.pas
- Updated wasm.parser.pas to reference constants via wasm.types.constants

## Tests
- Added 9 new test units (25 tests):
  block, loop, if, end, br, br_if, br_table, call, call_indirect
- Added E2E binary test: 115-byte handcrafted WASM binary exercising
  all control flow opcodes (2 functions, expected result=30)
- All 425/425 tests passing
This commit is contained in:
t3hn3rd committed 2026-02-24 14:46:30 +00:00
1 parent 679973c6e9
commit d12ad11746
17 files changed
+1447 -26

No files matched your search

+2 -2
View File
@@ -5,7 +5,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types.enums, wasm.types.sections, wasm.types.context, wasm.types.heap, wasm.types.stack,
wasm.parser.sections;
wasm.parser.sections, wasm.types.constants;
function parse(buffer: TWASMPUInt8; buffer_end: TWASMPUInt8) : PWASMProcessContext;
@@ -66,7 +66,7 @@ begin
writestringln('[wasm.parser] Parsing WASM Binary');
// Check for the WASM Magic
if(TWASMPUInt32(pos)^ = WASM_HDR_MAGIC) then begin
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
writestringln('[wasm.parser] Binary is valid.');
// Set binary to valid
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,61 @@
unit wasm.test.opcode.block;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..15] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.block');
{ Test 1: simple empty block falls through }
{ block $40 ... nop ... end }
code[0] := $02; { block }
code[1] := $40; { blocktype $40 }
code[2] := $01; { nop }
code[3] := $0B; { end }
ctx := make_test_context(@code[0], 4);
while wasm.vm.tick(ctx) do;
assert_true('empty block completes', ctx^.ExecutionState.Running = false);
assert_u32('empty block IP at end', ctx^.ExecutionState.IP, 4);
{ Test 2: block with value on stack }
{ block $40 i32.const 42 end }
code[0] := $02; { block }
code[1] := $40; { blocktype $40 }
code[2] := $41; { i32.const }
code[3] := $2A; { 42 }
code[4] := $0B; { end }
ctx := make_test_context(@code[0], 5);
while wasm.vm.tick(ctx) do;
assert_i32('block preserves stack value', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
{ Test 3: nested blocks }
{ block $40 block $40 i32.const 7 end i32.const 3 i32.add end }
code[0] := $02; { outer block }
code[1] := $40; { blocktype $40 }
code[2] := $02; { inner block }
code[3] := $40;
code[4] := $41; { i32.const 7 }
code[5] := $07;
code[6] := $0B; { end inner }
code[7] := $41; { i32.const 3 }
code[8] := $03;
code[9] := $6A; { i32.add }
code[10] := $0B; { end outer }
ctx := make_test_context(@code[0], 11);
while wasm.vm.tick(ctx) do;
assert_i32('nested blocks result', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
test_end;
end;
end.
@@ -0,0 +1,85 @@
unit wasm.test.opcode.br;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.enums, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..20] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.br');
{ Test 1: br 0 exits innermost block }
{ block $40 i32.const 42 br 0 i32.const 99 end }
code[0] := $02; { block }
code[1] := $40;
code[2] := $41; { i32.const 42 }
code[3] := $2A;
code[4] := $0C; { br 0 }
code[5] := $00;
code[6] := $41; { i32.const 99 (unreachable) }
code[7] := $63;
code[8] := $0B; { end }
ctx := make_test_context(@code[0], 9);
while wasm.vm.tick(ctx) do;
assert_i32('br 0 exits block with value', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
{ Test 2: br 1 exits outer block from nested block }
{ block $40 block $40 i32.const 77 br 1 end i32.const 99 end }
code[0] := $02; { outer block }
code[1] := $40;
code[2] := $02; { inner block }
code[3] := $40;
code[4] := $41; { i32.const 77 }
code[5] := $4D;
code[6] := $0C; { br 1 (exit outer) }
code[7] := $01;
code[8] := $0B; { end inner }
code[9] := $41; { i32.const 99 (unreachable) }
code[10] := $63;
code[11] := $0B; { end outer }
ctx := make_test_context(@code[0], 12);
while wasm.vm.tick(ctx) do;
assert_i32('br 1 exits outer block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 77);
{ Test 3: br 0 in loop re-enters loop (tested via local counter) }
{ i32.const 2 local.set 0
loop $40
local.get 0 i32.const 1 i32.sub local.tee 0
br_if 0
end
local.get 0 }
code[0] := $41; { i32.const 2 }
code[1] := $02;
code[2] := $21; { local.set 0 }
code[3] := $00;
code[4] := $03; { loop }
code[5] := $40;
code[6] := $20; { local.get 0 }
code[7] := $00;
code[8] := $41; { i32.const 1 }
code[9] := $01;
code[10] := $6B; { i32.sub }
code[11] := $22; { local.tee 0 }
code[12] := $00;
code[13] := $0D; { br_if 0 }
code[14] := $00;
code[15] := $0B; { end loop }
code[16] := $20; { local.get 0 }
code[17] := $00;
ctx := make_test_context(@code[0], 18);
setup_test_locals(ctx, 1, vti32);
while wasm.vm.tick(ctx) do;
assert_i32('br loop countdown result', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 0);
test_end;
end;
end.
@@ -0,0 +1,57 @@
unit wasm.test.opcode.brif;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..15] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.br_if');
{ Test 1: br_if with true condition exits block }
{ block $40 i32.const 42 i32.const 1 br_if 0 i32.const 99 end }
code[0] := $02; { block }
code[1] := $40;
code[2] := $41; { i32.const 42 }
code[3] := $2A;
code[4] := $41; { i32.const 1 (condition: true) }
code[5] := $01;
code[6] := $0D; { br_if 0 }
code[7] := $00;
code[8] := $41; { i32.const 99 (unreachable) }
code[9] := $63;
code[10] := $0B; { end }
ctx := make_test_context(@code[0], 11);
while wasm.vm.tick(ctx) do;
assert_i32('br_if true exits block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
{ Test 2: br_if with false condition continues }
{ block $40 i32.const 42 i32.const 0 br_if 0 drop i32.const 7 end }
code[0] := $02; { block }
code[1] := $40;
code[2] := $41; { i32.const 42 }
code[3] := $2A;
code[4] := $41; { i32.const 0 (condition: false) }
code[5] := $00;
code[6] := $0D; { br_if 0 }
code[7] := $00;
code[8] := $1A; { drop (remove 42) }
code[9] := $41; { i32.const 7 }
code[10] := $07;
code[11] := $0B; { end }
ctx := make_test_context(@code[0], 12);
while wasm.vm.tick(ctx) do;
assert_i32('br_if false continues execution', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 7);
test_end;
end;
end.
@@ -0,0 +1,80 @@
unit wasm.test.opcode.brtable;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..31] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.br_table');
{ br_table with 2 labels + default, targeting nested blocks.
Layout:
block $40 ;; label 2
block $40 ;; label 1
block $40 ;; label 0
i32.const <idx>
br_table 2 0 1 2 ;; count=2, labels=[0,1], default=2
end
i32.const 10 ;; label 0 lands here
return
end
i32.const 20 ;; label 1 lands here
return
end
i32.const 30 ;; label 2 lands here
}
{ Test 1: br_table index 0 selects label 0 }
code[0] := $02; { block (label 2) }
code[1] := $40;
code[2] := $02; { block (label 1) }
code[3] := $40;
code[4] := $02; { block (label 0) }
code[5] := $40;
code[6] := $41; { i32.const 0 }
code[7] := $00;
code[8] := $0E; { br_table }
code[9] := $02; { count = 2 labels }
code[10] := $00; { label[0] = depth 0 }
code[11] := $01; { label[1] = depth 1 }
code[12] := $02; { default = depth 2 }
code[13] := $0B; { end (label 0) }
code[14] := $41; { i32.const 10 }
code[15] := $0A;
code[16] := $0F; { return }
code[17] := $0B; { end (label 1) }
code[18] := $41; { i32.const 20 }
code[19] := $14;
code[20] := $0F; { return }
code[21] := $0B; { end (label 2) }
code[22] := $41; { i32.const 30 }
code[23] := $1E;
ctx := make_test_context(@code[0], 24);
while wasm.vm.tick(ctx) do;
assert_i32('br_table idx 0 selects label 0', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
{ Test 2: br_table index 1 selects label 1 }
code[7] := $01; { change index to 1 }
ctx := make_test_context(@code[0], 24);
while wasm.vm.tick(ctx) do;
assert_i32('br_table idx 1 selects label 1', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 20);
{ Test 3: br_table index out of range selects default }
code[7] := $05; { index 5, out of range -> default (depth 2) }
ctx := make_test_context(@code[0], 24);
while wasm.vm.tick(ctx) do;
assert_i32('br_table default for out-of-range', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 30);
test_end;
end;
end.
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,30 @@
unit wasm.test.opcode.callindirect;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.context, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..2] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.call_indirect');
{ Test: call_indirect traps (no table support) }
code[0] := $11; { call_indirect }
code[1] := $00; { type index 0 }
code[2] := $00; { table index 0 }
ctx := make_test_context(@code[0], 3);
wasm.vm.tick(ctx);
assert_true('call_indirect traps without table', ctx^.ExecutionState.Running = false);
test_end;
end;
end.
@@ -0,0 +1,42 @@
unit wasm.test.opcode.endop;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..7] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.end');
{ Test 1: end with empty control stack stops execution }
code[0] := $0B; { end }
ctx := make_test_context(@code[0], 1);
wasm.vm.tick(ctx);
assert_true('end stops at top level', ctx^.ExecutionState.Running = false);
{ Test 2: end pops block frame and continues }
{ block $40 i32.const 5 end i32.const 3 i32.add }
code[0] := $02; { block }
code[1] := $40;
code[2] := $41; { i32.const 5 }
code[3] := $05;
code[4] := $0B; { end block }
code[5] := $41; { i32.const 3 }
code[6] := $03;
code[7] := $6A; { i32.add }
ctx := make_test_context(@code[0], 8);
while wasm.vm.tick(ctx) do;
assert_i32('end continues after block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 8);
test_end;
end;
end.
@@ -0,0 +1,100 @@
unit wasm.test.opcode.ifop;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..20] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.if');
{ Test 1: if with true condition (no else) }
{ i32.const 1 if $40 i32.const 42 end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40; { blocktype $40 }
code[4] := $41; { i32.const 42 }
code[5] := $2A;
code[6] := $0B; { end }
ctx := make_test_context(@code[0], 7);
while wasm.vm.tick(ctx) do;
assert_i32('if true executes body', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 42);
{ Test 2: if with false condition (no else) - body skipped }
{ i32.const 0 if $40 i32.const 99 end i32.const 7 }
code[0] := $41; { i32.const 0 }
code[1] := $00;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 99 (should be skipped) }
code[5] := $63;
code[6] := $0B; { end }
code[7] := $41; { i32.const 7 }
code[8] := $07;
ctx := make_test_context(@code[0], 9);
while wasm.vm.tick(ctx) do;
assert_i32('if false skips body', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 7);
{ Test 3: if-else with true condition (takes if branch) }
{ i32.const 1 if $40 i32.const 10 else i32.const 20 end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 10 }
code[5] := $0A;
code[6] := $05; { else }
code[7] := $41; { i32.const 20 }
code[8] := $14;
code[9] := $0B; { end }
ctx := make_test_context(@code[0], 10);
while wasm.vm.tick(ctx) do;
assert_i32('if-else true takes if branch', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
{ Test 4: if-else with false condition (takes else branch) }
{ i32.const 0 if $40 i32.const 10 else i32.const 20 end }
code[0] := $41; { i32.const 0 }
code[1] := $00;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 10 }
code[5] := $0A;
code[6] := $05; { else }
code[7] := $41; { i32.const 20 }
code[8] := $14;
code[9] := $0B; { end }
ctx := make_test_context(@code[0], 10);
while wasm.vm.tick(ctx) do;
assert_i32('if-else false takes else branch', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 20);
{ Test 5: nested if }
{ i32.const 1 if $40 i32.const 1 if $40 i32.const 55 end end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { outer if }
code[3] := $40;
code[4] := $41; { i32.const 1 }
code[5] := $01;
code[6] := $04; { inner if }
code[7] := $40;
code[8] := $41; { i32.const 55 }
code[9] := $37;
code[10] := $0B; { end inner }
code[11] := $0B; { end outer }
ctx := make_test_context(@code[0], 12);
while wasm.vm.tick(ctx) do;
assert_i32('nested if result', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 55);
test_end;
end;
end.
@@ -0,0 +1,94 @@
unit wasm.test.opcode.loop;
interface
procedure run;
implementation
uses
wasm.types.builtin, wasm.types.enums, wasm.types.context, wasm.types.stack, wasm.vm, wasm.test.framework;
procedure run;
var
code : array[0..31] of TWASMUInt8;
ctx : PWASMProcessContext;
begin
test_begin('opcode.loop');
{ Test 1: loop that executes body once then exits via end }
{ loop $40 nop end }
code[0] := $03; { loop }
code[1] := $40; { blocktype $40 }
code[2] := $01; { nop }
code[3] := $0B; { end }
ctx := make_test_context(@code[0], 4);
while wasm.vm.tick(ctx) do;
assert_true('loop single pass completes', ctx^.ExecutionState.Running = false);
{ Test 2: loop that counts down from 3 to 0 using br_if }
{ Local 0 starts at 3; loop: decrement, br_if 0 if nonzero; end }
{ i32.const 3 local.set 0 loop $40
local.get 0 i32.const 1 i32.sub local.tee 0 br_if 0
end }
code[0] := $41; { i32.const 3 }
code[1] := $03;
code[2] := $21; { local.set 0 }
code[3] := $00;
code[4] := $03; { loop }
code[5] := $40; { blocktype $40 }
code[6] := $20; { local.get 0 }
code[7] := $00;
code[8] := $41; { i32.const 1 }
code[9] := $01;
code[10] := $6B; { i32.sub }
code[11] := $22; { local.tee 0 }
code[12] := $00;
code[13] := $0D; { br_if 0 }
code[14] := $00;
code[15] := $0B; { end loop }
ctx := make_test_context(@code[0], 16);
setup_test_locals(ctx, 1, vti32);
while wasm.vm.tick(ctx) do;
assert_i32('loop countdown final local', ctx^.ExecutionState.Locals^.Locals[0].i32Value, 0);
{ Test 3: loop with accumulator - sum 1+2+3 = 6 }
{ local 0 = counter (starts 3), local 1 = accumulator }
{ i32.const 3 local.set 0
loop $40
local.get 1 local.get 0 i32.add local.set 1
local.get 0 i32.const 1 i32.sub local.tee 0
br_if 0
end }
code[0] := $41; { i32.const 3 }
code[1] := $03;
code[2] := $21; { local.set 0 }
code[3] := $00;
code[4] := $03; { loop }
code[5] := $40; { blocktype $40 }
code[6] := $20; { local.get 1 (accum) }
code[7] := $01;
code[8] := $20; { local.get 0 (counter) }
code[9] := $00;
code[10] := $6A; { i32.add }
code[11] := $21; { local.set 1 }
code[12] := $01;
code[13] := $20; { local.get 0 }
code[14] := $00;
code[15] := $41; { i32.const 1 }
code[16] := $01;
code[17] := $6B; { i32.sub }
code[18] := $22; { local.tee 0 }
code[19] := $00;
code[20] := $0D; { br_if 0 }
code[21] := $00;
code[22] := $0B; { end loop }
ctx := make_test_context(@code[0], 23);
setup_test_locals(ctx, 2, vti32);
while wasm.vm.tick(ctx) do;
assert_i32('loop accumulator result', ctx^.ExecutionState.Locals^.Locals[1].i32Value, 6);
test_end;
end;
end.
+21 -1
View File
@@ -31,6 +31,15 @@ uses
wasm.test.opcode.f32const,
wasm.test.opcode.f64const,
wasm.test.opcode.return,
wasm.test.opcode.block,
wasm.test.opcode.loop,
wasm.test.opcode.ifop,
wasm.test.opcode.endop,
wasm.test.opcode.br,
wasm.test.opcode.brif,
wasm.test.opcode.brtable,
wasm.test.opcode.call,
wasm.test.opcode.callindirect,
{ Variable opcode tests }
wasm.test.opcode.localget,
wasm.test.opcode.localset,
@@ -148,7 +157,8 @@ uses
wasm.test.binary.addnums,
wasm.test.binary.arithmetic,
wasm.test.binary.locals,
wasm.test.binary.memroundtrip;
wasm.test.binary.memroundtrip,
wasm.test.binary.controlflow;
procedure run_all_tests;
begin
@@ -183,6 +193,15 @@ begin
wasm.test.opcode.f32const.run;
wasm.test.opcode.f64const.run;
wasm.test.opcode.return.run;
wasm.test.opcode.block.run;
wasm.test.opcode.loop.run;
wasm.test.opcode.ifop.run;
wasm.test.opcode.endop.run;
wasm.test.opcode.br.run;
wasm.test.opcode.brif.run;
wasm.test.opcode.brtable.run;
wasm.test.opcode.call.run;
wasm.test.opcode.callindirect.run;
{ Variable opcodes }
wasm.test.opcode.localget.run;
@@ -314,6 +333,7 @@ begin
wasm.test.binary.arithmetic.run;
wasm.test.binary.locals.run;
wasm.test.binary.memroundtrip.run;
wasm.test.binary.controlflow.run;
{ Summary }
writestringln('');
+23
View File
@@ -0,0 +1,23 @@
unit wasm.types.constants;
interface
uses
wasm.types.builtin;
const
{ The magic number at the start of a WASM binary file (0x00 0x61 0x73 0x6D) }
WASM_HDR_MAGIC = $6D736100;
const
{ Control frame type markers used on the control stack.
Block frames use 3 entries: target_ip, saved_stack_top, frame_type.
Call frames use 4 entries: saved_locals_ptr (i64), return_ip, saved_stack_top, frame_type. }
CTRL_FRAME_BLOCK : TWASMInt32 = 0;
CTRL_FRAME_LOOP : TWASMInt32 = 1;
CTRL_FRAME_IF : TWASMInt32 = 2;
CTRL_FRAME_CALL : TWASMInt32 = 3;
implementation
end.
-3
View File
@@ -5,9 +5,6 @@ interface
uses
wasm.types.builtin;
const
WASM_HDR_MAGIC = $6D736100;
type
TWasmExportType = (
etFunc = $00,
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+5 -2
View File
@@ -29,9 +29,12 @@ end;
function tick(Context : PWASMProcessContext) : TWASMBoolean;
begin
if Context^.ExecutionState.Running then
if Context^.ExecutionState.Running then begin
if Context^.ExecutionState.IP < Context^.ExecutionState.Limit then
OpcodeJumpTable[Context^.ExecutionState.Code[Context^.ExecutionState.IP]](Context);
OpcodeJumpTable[Context^.ExecutionState.Code[Context^.ExecutionState.IP]](Context)
else
Context^.ExecutionState.Running := false;
end;
tick:= Context^.ExecutionState.Running;
end;