Compare commits

...
Author SHA1 Message Date
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
t3hn3rd 68df3565cb feature: types refactored from one monolithic file to seperation of duties
- wasm.types.context.pas
- wasm.types.enums.pas
- wasm.types.sections.pas
- wasm.types.values.pas
2026-02-24 13:23:46 +00:00
t3hn3rd 862e3254df Merge branch 'hotfix/binary-test-builtins' into develop 2026-02-24 13:05:42 +00:00
t3hn3rd 6794c52a77 patch: binary-test patched to use builtin types 2026-02-24 13:05:33 +00:00
t3hn3rd 7cc4459dfd Merge branch 'feature/builtin-types' into develop 2026-02-24 13:04:25 +00:00
t3hn3rd ae344e7495 Merge branch 'feature/binary-tests' into develop 2026-02-24 13:04:20 +00:00
t3hn3rd 835f6f86cc feature: Binary E2E tests
Implemented the following E2E tests:
- src/wasm/test/wasm.test.binary.addnums.pas
- src/wasm/test/wasm.test.binary.arithmetic.pas
- src/wasm/test/wasm.test.binary.locals.pas
- src/wasm/test/wasm.test.binary.memroundtrip.pas
- src/wasm/test/wasm.test.binary.return42.pas
2026-02-24 12:26:43 +00:00
161 changed files with 2138 additions and 356 deletions

No files matched your search

+1
View File
@@ -28,6 +28,7 @@ if %ERRORLEVEL% NEQ 0 exit /b 1
-Fu"%SRC%\wasm\test" ^
-Fu"%SRC%\wasm\test\opcodes" ^
-Fu"%SRC%\wasm\test\parsers" ^
-Fu"%SRC%\wasm\test\binaries" ^
-Fu"%SRC%\wasm\test\infra" ^
-Fu"%SRC%\wasm\types" ^
-Fu"%SRC%\wasm\vm" ^
+3 -3
View File
@@ -7,9 +7,9 @@ uses
types,
wasm.types,
wasm.types.context,
wasm.parser,
wasm.vm, wasm.test.binary,
wasm.vm, wasm.test.binary.return42,
wasm.types.stack
{$IFDEF RUN_TESTS}
, wasm.test
@@ -30,7 +30,7 @@ begin
wasm.vm.init();
writestringln('[main] Parsing WASM Binary');
Context:= wasm.parser.parse(@wasm.test.binary.TEST_BINARY_1[0], puint8(@wasm.test.binary.TEST_BINARY_1[0] + $26));
Context:= wasm.parser.parse(@wasm.test.binary.return42.BINARY[0], puint8(@wasm.test.binary.return42.BINARY[0] + wasm.test.binary.return42.BINARY_SIZE));
writestringln('[main] Running...');
Context^.ExecutionState.Running:= true;
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types, wasm.types.heap;
wasm.types.context, wasm.types.heap;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.enums, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.enums, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types, wasm.types.heap;
wasm.types.sections, wasm.types.context, wasm.types.heap;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types;
wasm.types.enums, wasm.types.values, wasm.types.sections, wasm.types.context;
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
+3 -3
View File
@@ -4,8 +4,8 @@ interface
uses
wasm.types.builtin, lmemorymanager, console, leb128,
wasm.types, wasm.types.heap, wasm.types.stack,
wasm.parser.sections;
wasm.types.enums, wasm.types.sections, wasm.types.context, wasm.types.heap, wasm.types.stack,
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.types.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
+1 -1
View File
@@ -4,7 +4,7 @@ interface
uses
wasm.types.builtin,
wasm.types,
wasm.types.enums, wasm.types.context,
wasm.parser.sections.customSection,
wasm.parser.sections.typeSection,
wasm.parser.sections.importSection,
@@ -0,0 +1,62 @@
{ E2E test: i32.const 10 + i32.const 32 = 42
Equivalent WAT:
(module
(func (export "main") (result i32)
i32.const 10
i32.const 32
i32.add
return
)
)
}
unit wasm.test.binary.addnums;
interface
procedure run;
implementation
uses
wasm.types.builtin, console, wasm.types.context, wasm.types.stack,
wasm.parser, wasm.vm, wasm.test.framework;
const
BINARY_SIZE = 41;
BINARY : Array[$00..$28] of TWASMUInt8 = (
$00, $61, $73, $6D, { magic }
$01, $00, $00, $00, { version 1 }
$01, $05, $01, $60, { type section: 1 func type }
$00, $01, $7F, { () -> (i32) }
$03, $02, $01, $00, { function section: 1 func, type 0 }
$07, $08, $01, $04, { export section: 1 export, name len 4 }
$6D, $61, $69, $6E, { "main" }
$00, $00, { export type=func, index=0 }
$0A, $0A, $01, $08, { code section: len=10, count=1, body=8 }
$00, { 0 locals }
$41, $0A, { i32.const 10 }
$41, $20, { i32.const 32 }
$6A, { i32.add }
$0F, $0B { return, end }
);
procedure run;
var
ctx : PWASMProcessContext;
begin
test_begin('binary.addnums');
ctx := wasm.parser.parse(@BINARY[0], TWASMPUInt8(@BINARY[0] + BINARY_SIZE));
assert_true('valid binary', ctx^.ValidBinary);
ctx^.ExecutionState.Running := true;
while wasm.vm.tick(ctx) do;
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
assert_i32('10+32=42', popi32(ctx^.ExecutionState.Operand_Stack), 42);
test_end;
end;
end.
@@ -0,0 +1,66 @@
{ E2E test: (2 + 3) * 4 = 20
Equivalent WAT:
(module
(func (export "main") (result i32)
i32.const 2
i32.const 3
i32.add
i32.const 4
i32.mul
return
)
)
}
unit wasm.test.binary.arithmetic;
interface
procedure run;
implementation
uses
wasm.types.builtin, console, wasm.types.context, wasm.types.stack,
wasm.parser, wasm.vm, wasm.test.framework;
const
BINARY_SIZE = 44;
BINARY : Array[$00..$2B] of TWASMUInt8 = (
$00, $61, $73, $6D, { magic }
$01, $00, $00, $00, { version 1 }
$01, $05, $01, $60, { type section: 1 func type }
$00, $01, $7F, { () -> (i32) }
$03, $02, $01, $00, { function section: 1 func, type 0 }
$07, $08, $01, $04, { export section: 1 export, name len 4 }
$6D, $61, $69, $6E, { "main" }
$00, $00, { export type=func, index=0 }
$0A, $0D, $01, $0B, { code section: len=13, count=1, body=11 }
$00, { 0 locals }
$41, $02, { i32.const 2 }
$41, $03, { i32.const 3 }
$6A, { i32.add }
$41, $04, { i32.const 4 }
$6C, { i32.mul }
$0F, $0B { return, end }
);
procedure run;
var
ctx : PWASMProcessContext;
begin
test_begin('binary.arithmetic');
ctx := wasm.parser.parse(@BINARY[0], TWASMPUInt8(@BINARY[0] + BINARY_SIZE));
assert_true('valid binary', ctx^.ValidBinary);
ctx^.ExecutionState.Running := true;
while wasm.vm.tick(ctx) do;
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
assert_i32('(2+3)*4=20', popi32(ctx^.ExecutionState.Operand_Stack), 20);
test_end;
end;
end.
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,74 @@
{ E2E test: use local variables, local.set 0 := 10, local.set 1 := 20,
return local.get 0 + local.get 1 = 30
Equivalent WAT:
(module
(func (export "main") (result i32) (local i32 i32)
i32.const 10
local.set 0
i32.const 20
local.set 1
local.get 0
local.get 1
i32.add
return
)
)
}
unit wasm.test.binary.locals;
interface
procedure run;
implementation
uses
wasm.types.builtin, console, wasm.types.context, wasm.types.stack,
wasm.parser, wasm.vm, wasm.test.framework;
const
BINARY_SIZE = 51;
BINARY : Array[$00..$32] of TWASMUInt8 = (
$00, $61, $73, $6D, { magic }
$01, $00, $00, $00, { version 1 }
$01, $05, $01, $60, { type section: 1 func type }
$00, $01, $7F, { () -> (i32) }
$03, $02, $01, $00, { function section: 1 func, type 0 }
$07, $08, $01, $04, { export section: 1 export, name len 4 }
$6D, $61, $69, $6E, { "main" }
$00, $00, { export type=func, index=0 }
$0A, $14, $01, $12, { code section: len=20, count=1, body=18 }
$01, $02, $7F, { 1 local type entry: 2 x i32 }
$41, $0A, { i32.const 10 }
$21, $00, { local.set 0 }
$41, $14, { i32.const 20 }
$21, $01, { local.set 1 }
$20, $00, { local.get 0 }
$20, $01, { local.get 1 }
$6A, { i32.add }
$0F, $0B { return, end }
);
procedure run;
var
ctx : PWASMProcessContext;
begin
test_begin('binary.locals');
ctx := wasm.parser.parse(@BINARY[0], TWASMPUInt8(@BINARY[0] + BINARY_SIZE));
assert_true('valid binary', ctx^.ValidBinary);
{ Set up locals from the parsed code entry for the single function }
ctx^.ExecutionState.Locals := @ctx^.Sections.CodeSection^.Entries[0].Locals;
ctx^.ExecutionState.Running := true;
while wasm.vm.tick(ctx) do;
assert_bool('execution stopped', ctx^.ExecutionState.Running, false);
assert_true('stack has result', ctx^.ExecutionState.Operand_Stack^.Top > 0);
assert_i32('10+20=30', popi32(ctx^.ExecutionState.Operand_Stack), 30);
test_end;
end;
end.
Loaded 20 of 161 files, more files were not shown because too many files have changed in this diff. Show more