Compare commits

..
Author SHA1 Message Date
t3hn3rd 93dfff358d refactor: Split monolithic wasm.vm.opcodes.pas into 119 individual opcode units
- Extract each opcode procedure from wasm.vm.opcodes.pas into its own
unit file under src/wasm/vm/opcodes/ following the naming convention
wasm.vm.opcode.<name>.pas. The hub file now only imports the individual
units and contains initializeOpcodeJumpTable with fully qualified
procedure references.
- Add missing test coverage for the else opcode (4 tests), bringing the
total from 425 to 429 with all tests passing.

Files:
- 119 new opcode units under src/wasm/vm/opcodes/
- 1 new test: wasm.test.opcode.elseop.pas
- Modified: wasm.vm.opcodes.pas (hub-only), wasm.test.pas, build.bat
2026-02-24 15:38:36 +00:00
t3hn3rd 3ed5c0a847 Merge branch 'feature/build-clean' into develop 2026-02-24 14:57:43 +00:00
t3hn3rd ff65a0d5f0 refactor: build.bat flexible argument parsing, add run & help commands
- Replaced positional `%1` checks with a loop-based argument parser
  so flags (`test`, `clean`, `run`, `help`) work in any order/combination
- Added `run` flag: executes `bin\WASURO.exe` after a successful build
- Added `help` flag: prints usage info and exits
- Fixed `clean` early-exit to also check `DO_RUN` (not just `TESTFLAGS`),
  so `build.bat clean run` correctly continues to build+run
- Added section comment headers (Build, Run) for consistency
2026-02-24 14:57:30 +00:00
t3hn3rd 76d7d737c0 Merge branch 'feature/missing-opcodes' into develop 2026-02-24 14:47:01 +00:00
t3hn3rd d12ad11746 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
2026-02-24 14:46:30 +00:00
t3hn3rd 679973c6e9 Merge branch 'feature/type-refactoring' into develop 2026-02-24 13:24:15 +00:00
138 changed files with 4756 additions and 1596 deletions

No files matched your search

+35
View File
@@ -8,7 +8,34 @@ set LIB=%ROOT%lib
set FPC=C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
set TESTFLAGS=
set DO_CLEAN=0
set DO_RUN=0
:parse_args
if "%1"=="" goto args_done
if "%1"=="test" set TESTFLAGS=-dRUN_TESTS
if "%1"=="clean" set DO_CLEAN=1
if "%1"=="run" set DO_RUN=1
if "%1"=="help" (
echo Usage: build.bat [options]
echo Options:
echo test - Build with tests enabled
echo clean - Clean build outputs
echo run - Run the built executable after building
echo help - Show this help message
exit /b 0
)
shift
goto parse_args
:args_done
if %DO_CLEAN%==1 (
echo Cleaning build outputs...
if exist "%BIN%" rd /s /q "%BIN%"
if exist "%LIB%" rd /s /q "%LIB%"
echo Clean complete.
if "%TESTFLAGS%"=="" if %DO_RUN%==0 exit /b 0
)
if not exist "%BIN%" mkdir "%BIN%"
if not exist "%LIB%" mkdir "%LIB%"
@@ -17,6 +44,7 @@ REM ── Lint ─────────────────────
powershell -NoProfile -ExecutionPolicy Bypass -File "%ROOT%lint.ps1" -SrcRoot "%SRC%"
if %ERRORLEVEL% NEQ 0 exit /b 1
REM ── Build ────────────────────────────────────────────────────────────
"%FPC%" ^
%TESTFLAGS% ^
-FE"%BIN%" ^
@@ -32,6 +60,7 @@ if %ERRORLEVEL% NEQ 0 exit /b 1
-Fu"%SRC%\wasm\test\infra" ^
-Fu"%SRC%\wasm\types" ^
-Fu"%SRC%\wasm\vm" ^
-Fu"%SRC%\wasm\vm\opcodes" ^
-gw3 ^
-o"%BIN%\WASURO.exe" ^
"%SRC%\project\WASURO.lpr"
@@ -42,3 +71,9 @@ if %ERRORLEVEL% NEQ 0 (
)
echo Build succeeded: %BIN%\WASURO.exe
REM ── Run ────────────────────────────────────────────────────────────
if %DO_RUN%==1 (
echo Running WASURO...
"%BIN%\WASURO.exe"
)
+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,92 @@
unit wasm.test.opcode.elseop;
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.else');
{ Test 1: else skips else-body when if-true path taken }
{ i32.const 1 if $40 i32.const 10 else i32.const 20 end }
{ When condition is true the if-true body runs and else skips to past end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40; { blocktype empty }
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('true path skips else body', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 10);
{ Test 2: else-body executes when condition is false }
{ 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('false path executes else body', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 20);
{ Test 3: else skips nested block in else-body }
{ i32.const 1 if $40 i32.const 5 else block $40 i32.const 99 end end }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 5 }
code[5] := $05;
code[6] := $05; { else }
code[7] := $02; { block }
code[8] := $40;
code[9] := $41; { i32.const 99 }
code[10] := $63;
code[11] := $0B; { end block }
code[12] := $0B; { end if }
ctx := make_test_context(@code[0], 13);
while wasm.vm.tick(ctx) do;
assert_i32('else skips nested block', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 5);
{ Test 4: execution continues after end when else is skipped }
{ i32.const 1 if $40 i32.const 3 else i32.const 4 end i32.const 7 }
code[0] := $41; { i32.const 1 }
code[1] := $01;
code[2] := $04; { if }
code[3] := $40;
code[4] := $41; { i32.const 3 }
code[5] := $03;
code[6] := $05; { else }
code[7] := $41; { i32.const 4 }
code[8] := $04;
code[9] := $0B; { end }
code[10] := $41; { i32.const 7 }
code[11] := $07;
ctx := make_test_context(@code[0], 12);
while wasm.vm.tick(ctx) do;
assert_i32('continues after else skip', wasm.types.stack.popi32(ctx^.ExecutionState.Operand_Stack), 7);
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.
+23 -1
View File
@@ -31,6 +31,16 @@ 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.elseop,
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 +158,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 +194,16 @@ 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.elseop.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 +335,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,
@@ -0,0 +1,30 @@
unit wasm.vm.opcode.block;
interface
uses wasm.types.context;
procedure _WASM_opcode_BlockOp(Context : PWASMProcessContext);
implementation
uses wasm.types.builtin, wasm.types.constants, wasm.vm.control;
procedure _WASM_opcode_BlockOp(Context : PWASMProcessContext);
var
end_ip: TWASMUInt32;
begin
Inc(Context^.ExecutionState.IP); { past opcode $02 }
Inc(Context^.ExecutionState.IP); { past blocktype byte }
{ Find the matching end byte }
end_ip := scan_forward(Context^.ExecutionState.Code,
Context^.ExecutionState.IP,
Context^.ExecutionState.Limit, false);
{ Push block frame: br target = past the end byte }
push_control_frame(Context^.ExecutionState.Control_Stack,
CTRL_FRAME_BLOCK,
TWASMInt32(end_ip + 1),
TWASMInt32(Context^.ExecutionState.Operand_Stack^.Top));
end;
end.
+27
View File
@@ -0,0 +1,27 @@
unit wasm.vm.opcode.br;
interface
uses wasm.types.context;
procedure _WASM_opcode_BrOp(Context : PWASMProcessContext);
implementation
uses leb128, wasm.types.builtin, wasm.vm.control;
procedure _WASM_opcode_BrOp(Context : PWASMProcessContext);
var
label_depth: TWASMUInt32;
bytesRead: TWASMUInt8;
begin
Inc(Context^.ExecutionState.IP); { past opcode $0C }
bytesRead := read_leb128_to_uint32(
@Context^.ExecutionState.Code[Context^.ExecutionState.IP],
@Context^.ExecutionState.Code[Context^.ExecutionState.Limit],
@label_depth);
{ do_branch sets IP; no need to advance past immediate }
do_branch(Context, label_depth);
end;
end.
@@ -0,0 +1,31 @@
unit wasm.vm.opcode.brif;
interface
uses wasm.types.context;
procedure _WASM_opcode_BrIfOp(Context : PWASMProcessContext);
implementation
uses leb128, wasm.types.builtin, wasm.types.stack, wasm.vm.control;
procedure _WASM_opcode_BrIfOp(Context : PWASMProcessContext);
var
label_depth: TWASMUInt32;
bytesRead: TWASMUInt8;
cond: TWASMInt32;
begin
Inc(Context^.ExecutionState.IP); { past opcode $0D }
bytesRead := read_leb128_to_uint32(
@Context^.ExecutionState.Code[Context^.ExecutionState.IP],
@Context^.ExecutionState.Code[Context^.ExecutionState.Limit],
@label_depth);
Inc(Context^.ExecutionState.IP, bytesRead); { past label depth }
cond := wasm.types.stack.popi32(Context^.ExecutionState.Operand_Stack);
if cond <> 0 then
do_branch(Context, label_depth);
{ else: continue at current IP }
end;
end.
@@ -0,0 +1,50 @@
unit wasm.vm.opcode.brtable;
interface
uses wasm.types.context;
procedure _WASM_opcode_BrTableOp(Context : PWASMProcessContext);
implementation
uses leb128, wasm.types.builtin, wasm.types.stack, wasm.vm.control;
procedure _WASM_opcode_BrTableOp(Context : PWASMProcessContext);
var
count, idx, selected, dummy: TWASMUInt32;
skip_count, i: TWASMUInt32;
bytesRead: TWASMUInt8;
begin
Inc(Context^.ExecutionState.IP); { past opcode $0E }
{ Read label count }
bytesRead := read_leb128_to_uint32(
@Context^.ExecutionState.Code[Context^.ExecutionState.IP],
@Context^.ExecutionState.Code[Context^.ExecutionState.Limit],
@count);
Inc(Context^.ExecutionState.IP, bytesRead);
{ Pop index from operand stack }
idx := TWASMUInt32(wasm.types.stack.popi32(Context^.ExecutionState.Operand_Stack));
{ Determine how many labels to skip to reach our target }
if idx >= count then
skip_count := count
else
skip_count := idx;
{ Skip labels we don't need }
for i := 1 to skip_count do begin
bytesRead := read_leb128_to_uint32(
@Context^.ExecutionState.Code[Context^.ExecutionState.IP],
@Context^.ExecutionState.Code[Context^.ExecutionState.Limit],
@dummy);
Inc(Context^.ExecutionState.IP, bytesRead);
end;
{ Read the selected label (or default if idx >= count) }
bytesRead := read_leb128_to_uint32(
@Context^.ExecutionState.Code[Context^.ExecutionState.IP],
@Context^.ExecutionState.Code[Context^.ExecutionState.Limit],
@selected);
{ Branch to selected depth }
do_branch(Context, selected);
end;
end.
Loaded 20 of 138 files, more files were not shown because too many files have changed in this diff. Show more