Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b937e60838 | ||
|
|
255ad4806e | ||
|
|
93dfff358d | ||
|
|
3ed5c0a847 |
No files matched your search
@@ -1,3 +1,4 @@
|
|||||||
bin
|
bin
|
||||||
lib
|
lib
|
||||||
AGENTS.md
|
AGENTS.md
|
||||||
|
GAP_ANALYSIS.md
|
||||||
@@ -60,6 +60,7 @@ REM ── Build ─────────────────────
|
|||||||
-Fu"%SRC%\wasm\test\infra" ^
|
-Fu"%SRC%\wasm\test\infra" ^
|
||||||
-Fu"%SRC%\wasm\types" ^
|
-Fu"%SRC%\wasm\types" ^
|
||||||
-Fu"%SRC%\wasm\vm" ^
|
-Fu"%SRC%\wasm\vm" ^
|
||||||
|
-Fu"%SRC%\wasm\vm\opcodes" ^
|
||||||
-gw3 ^
|
-gw3 ^
|
||||||
-o"%BIN%\WASURO.exe" ^
|
-o"%BIN%\WASURO.exe" ^
|
||||||
"%SRC%\project\WASURO.lpr"
|
"%SRC%\project\WASURO.lpr"
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
unit wasm.parser.sections.dataCountSection;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses
|
||||||
|
wasm.types.builtin, wasm.types.context;
|
||||||
|
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses console, leb128, lmemorymanager, wasm.types.sections;
|
||||||
|
|
||||||
|
{ DataCount section (ID 12): contains a single u32 count of data segments.
|
||||||
|
Pre-allocates the DataSegments array so that bulk memory opcodes
|
||||||
|
can reference segments by index during validation. }
|
||||||
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos, bend : TWASMPUInt8;
|
||||||
|
bytesRead : TWASMUInt8;
|
||||||
|
dataCount : TWASMUInt32;
|
||||||
|
begin
|
||||||
|
writestring('[wasm.parser] Handle Section: DataCount - Size: ');
|
||||||
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
bend := TWASMPUInt8(buffer + section_length);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Data Count: ');
|
||||||
|
writeintlnWND(dataCount, 0);
|
||||||
|
|
||||||
|
{ Pre-allocate data segment storage with correct count.
|
||||||
|
The actual segment data will be filled when the Data section is parsed. }
|
||||||
|
ctx^.ExecutionState.DataSegments^.SegmentCount := dataCount;
|
||||||
|
if dataCount > 0 then
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := PWASMDataSegment(kalloc(dataCount * sizeof(TWASMDataSegment)))
|
||||||
|
else
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -4,16 +4,85 @@ interface
|
|||||||
|
|
||||||
uses
|
uses
|
||||||
wasm.types.builtin, lmemorymanager, console, leb128,
|
wasm.types.builtin, lmemorymanager, console, leb128,
|
||||||
wasm.types.context;
|
wasm.types.sections, wasm.types.context;
|
||||||
|
|
||||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
|
||||||
|
var
|
||||||
|
pos: TWASMPUInt8;
|
||||||
|
buf_end: TWASMPUInt8;
|
||||||
|
bytesRead: TWASMUInt8;
|
||||||
|
table_count, i, j, min_size, max_size: TWASMUInt32;
|
||||||
|
elem_type, limits_flag: TWASMUInt8;
|
||||||
|
tables: PWASMTables;
|
||||||
begin
|
begin
|
||||||
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
writestring('[wasm.parser] Handle Section: Table - Size: ');
|
||||||
writeintlnWND(section_length, 0);
|
writeintlnWND(section_length, 0);
|
||||||
|
|
||||||
|
pos := buffer;
|
||||||
|
buf_end := buffer;
|
||||||
|
Inc(buf_end, section_length);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Tables: ');
|
||||||
|
writeintlnWND(table_count, 0);
|
||||||
|
|
||||||
|
tables := ctx^.ExecutionState.Tables;
|
||||||
|
tables^.TableCount := table_count;
|
||||||
|
if table_count > 0 then
|
||||||
|
tables^.Tables := PWASMTableInstance(kalloc(sizeof(TWASMTableInstance) * table_count))
|
||||||
|
else
|
||||||
|
tables^.Tables := nil;
|
||||||
|
|
||||||
|
for i := 0 to table_count - 1 do begin
|
||||||
|
elem_type := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
|
||||||
|
limits_flag := pos^;
|
||||||
|
Inc(pos);
|
||||||
|
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, buf_end, @min_size);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
|
||||||
|
max_size := 0;
|
||||||
|
if limits_flag = 1 then begin
|
||||||
|
bytesRead := read_leb128_to_uint32(pos, buf_end, @max_size);
|
||||||
|
Inc(pos, bytesRead);
|
||||||
|
end;
|
||||||
|
|
||||||
|
writestring('[wasm.parser] Table ');
|
||||||
|
writeintWND(i, 0);
|
||||||
|
writestring(' - ElemType: $');
|
||||||
|
writeintWND(elem_type, 0);
|
||||||
|
writestring(' Initial: ');
|
||||||
|
writeintWND(min_size, 0);
|
||||||
|
if limits_flag = 1 then begin
|
||||||
|
writestring(' Max: ');
|
||||||
|
writeintWND(max_size, 0);
|
||||||
|
end;
|
||||||
|
writestringln('');
|
||||||
|
|
||||||
|
tables^.Tables[i].ElementType := elem_type;
|
||||||
|
tables^.Tables[i].Size := min_size;
|
||||||
|
tables^.Tables[i].HasMax := (limits_flag = 1);
|
||||||
|
if limits_flag = 1 then
|
||||||
|
tables^.Tables[i].MaxSize := max_size
|
||||||
|
else
|
||||||
|
tables^.Tables[i].MaxSize := 0;
|
||||||
|
|
||||||
|
{ Allocate elements and initialize to $FFFFFFFF (uninitialized) }
|
||||||
|
if min_size > 0 then begin
|
||||||
|
tables^.Tables[i].Elements := TWASMPUInt32(kalloc(sizeof(TWASMUInt32) * min_size));
|
||||||
|
for j := 0 to min_size - 1 do
|
||||||
|
tables^.Tables[i].Elements[j] := $FFFFFFFF;
|
||||||
|
end else
|
||||||
|
tables^.Tables[i].Elements := nil;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
end.
|
end.
|
||||||
@@ -36,6 +36,7 @@ begin
|
|||||||
|
|
||||||
// Initialize the sections
|
// Initialize the sections
|
||||||
ctx^.Sections.TypeSection:= nil;
|
ctx^.Sections.TypeSection:= nil;
|
||||||
|
ctx^.Sections.ImportSection:= nil;
|
||||||
ctx^.Sections.FunctionSection:= nil;
|
ctx^.Sections.FunctionSection:= nil;
|
||||||
ctx^.Sections.ExportSection:= nil;
|
ctx^.Sections.ExportSection:= nil;
|
||||||
ctx^.Sections.CodeSection:= nil;
|
ctx^.Sections.CodeSection:= nil;
|
||||||
@@ -47,6 +48,21 @@ begin
|
|||||||
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
ctx^.ExecutionState.Globals^.GlobalCount:= 0;
|
||||||
ctx^.ExecutionState.Globals^.Globals:= nil;
|
ctx^.ExecutionState.Globals^.Globals:= nil;
|
||||||
|
|
||||||
|
// Initialize tables (empty until table section is parsed)
|
||||||
|
ctx^.ExecutionState.Tables:= PWASMTables(kalloc(sizeof(TWASMTables)));
|
||||||
|
ctx^.ExecutionState.Tables^.TableCount:= 0;
|
||||||
|
ctx^.ExecutionState.Tables^.Tables:= nil;
|
||||||
|
|
||||||
|
// Initialize data segments (empty until data section is parsed)
|
||||||
|
ctx^.ExecutionState.DataSegments:= PWASMDataSegments(kalloc(sizeof(TWASMDataSegments)));
|
||||||
|
ctx^.ExecutionState.DataSegments^.SegmentCount:= 0;
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments:= nil;
|
||||||
|
|
||||||
|
// Initialize element segments (empty until element section is parsed)
|
||||||
|
ctx^.ExecutionState.ElementSegments:= PWASMElementSegments(kalloc(sizeof(TWASMElementSegments)));
|
||||||
|
ctx^.ExecutionState.ElementSegments^.SegmentCount:= 0;
|
||||||
|
ctx^.ExecutionState.ElementSegments^.Segments:= nil;
|
||||||
|
|
||||||
newContext:= ctx;
|
newContext:= ctx;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,8 @@ uses
|
|||||||
wasm.parser.sections.startSection,
|
wasm.parser.sections.startSection,
|
||||||
wasm.parser.sections.elementSection,
|
wasm.parser.sections.elementSection,
|
||||||
wasm.parser.sections.codeSection,
|
wasm.parser.sections.codeSection,
|
||||||
wasm.parser.sections.dataSection;
|
wasm.parser.sections.dataSection,
|
||||||
|
wasm.parser.sections.dataCountSection;
|
||||||
|
|
||||||
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
procedure handle(sectionId : TWASMUInt8; buffer : TWASMPUInt8; section_length : TWASMUInt32; ctx : PWASMProcessContext);
|
||||||
|
|
||||||
@@ -50,6 +51,8 @@ begin
|
|||||||
ord(sidCode): wasm.parser.sections.codeSection.handle (buffer, section_length, ctx);
|
ord(sidCode): wasm.parser.sections.codeSection.handle (buffer, section_length, ctx);
|
||||||
// Data Section
|
// Data Section
|
||||||
ord(sidData): wasm.parser.sections.dataSection.handle (buffer, section_length, ctx);
|
ord(sidData): wasm.parser.sections.dataSection.handle (buffer, section_length, ctx);
|
||||||
|
// DataCount Section
|
||||||
|
ord(sidDataCount): wasm.parser.sections.dataCountSection.handle (buffer, section_length, ctx);
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
unit wasm.parser.validate;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
uses wasm.types.builtin, wasm.types.context;
|
||||||
|
|
||||||
|
{ Validates a parsed WASM module for structural consistency.
|
||||||
|
Returns true if valid, false otherwise.
|
||||||
|
Sets errorMsg to a description of the first error found. }
|
||||||
|
function validate_module(ctx : PWASMProcessContext; var errorMsg : TWASMPChar) : TWASMBoolean;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses console;
|
||||||
|
|
||||||
|
function validate_module(ctx : PWASMProcessContext; var errorMsg : TWASMPChar) : TWASMBoolean;
|
||||||
|
var i : TWASMUInt32;
|
||||||
|
begin
|
||||||
|
validate_module := true;
|
||||||
|
|
||||||
|
{ Check 1: Binary must be valid (magic number was correct) }
|
||||||
|
if not ctx^.ValidBinary then begin
|
||||||
|
errorMsg := 'Invalid WASM binary: bad magic number';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Check 2: Version must be 1 (MVP) }
|
||||||
|
if ctx^.Version <> 1 then begin
|
||||||
|
errorMsg := 'Unsupported WASM version (expected 1)';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Check 3: If function section exists, code section must exist and counts must match }
|
||||||
|
if (ctx^.Sections.FunctionSection <> nil) then begin
|
||||||
|
if (ctx^.Sections.CodeSection = nil) then begin
|
||||||
|
errorMsg := 'Function section present but code section missing';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
if ctx^.Sections.FunctionSection^.FunctionCount <> ctx^.Sections.CodeSection^.CodeCount then begin
|
||||||
|
errorMsg := 'Function count does not match code count';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Check 4: If code section exists, function section must exist }
|
||||||
|
if (ctx^.Sections.CodeSection <> nil) then begin
|
||||||
|
if (ctx^.Sections.FunctionSection = nil) then begin
|
||||||
|
errorMsg := 'Code section present but function section missing';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Check 5: Function type indices must be within range of type section }
|
||||||
|
if (ctx^.Sections.FunctionSection <> nil) and (ctx^.Sections.TypeSection <> nil) then begin
|
||||||
|
for i := 0 to ctx^.Sections.FunctionSection^.FunctionCount - 1 do begin
|
||||||
|
if ctx^.Sections.FunctionSection^.Functions[i].Index >= ctx^.Sections.TypeSection^.TypeCount then begin
|
||||||
|
errorMsg := 'Function type index out of range';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
{ Check 6: Start function index must be within range }
|
||||||
|
if ctx^.Sections.StartIndex >= 0 then begin
|
||||||
|
if (ctx^.Sections.FunctionSection = nil) then begin
|
||||||
|
errorMsg := 'Start function specified but no function section';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
if TWASMUInt32(ctx^.Sections.StartIndex) >= ctx^.Sections.FunctionSection^.FunctionCount then begin
|
||||||
|
errorMsg := 'Start function index out of range';
|
||||||
|
validate_module := false;
|
||||||
|
exit;
|
||||||
|
end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
errorMsg := nil;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,45 @@
|
|||||||
|
unit wasm.test.opcode.datadrop;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
wasm.types.builtin, wasm.types.sections, wasm.types.context,
|
||||||
|
wasm.vm, wasm.test.framework, lmemorymanager;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
{ FC $09, data_idx=0 }
|
||||||
|
code : array[0..2] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
segData : array[0..1] of TWASMUInt8;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.data.drop');
|
||||||
|
|
||||||
|
code[0] := $FC; code[1] := $09; code[2] := $00;
|
||||||
|
|
||||||
|
{ Drop a data segment }
|
||||||
|
segData[0] := $11; segData[1] := $22;
|
||||||
|
ctx := make_test_context(@code[0], 3);
|
||||||
|
ctx^.ExecutionState.DataSegments^.SegmentCount := 1;
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments := PWASMDataSegment(kalloc(sizeof(TWASMDataSegment)));
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments[0].Data := @segData[0];
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments[0].Size := 2;
|
||||||
|
ctx^.ExecutionState.DataSegments^.Segments[0].Dropped := false;
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_true('segment dropped', ctx^.ExecutionState.DataSegments^.Segments[0].Dropped);
|
||||||
|
assert_true('still running', ctx^.ExecutionState.Running);
|
||||||
|
|
||||||
|
{ Drop invalid index traps }
|
||||||
|
ctx := make_test_context(@code[0], 3);
|
||||||
|
{ SegmentCount = 0 (default), so data_idx=0 is out of range }
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_true('invalid_idx traps', not ctx^.ExecutionState.Running);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
unit wasm.test.opcode.elemdrop;
|
||||||
|
|
||||||
|
interface
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
|
||||||
|
implementation
|
||||||
|
|
||||||
|
uses
|
||||||
|
wasm.types.builtin, wasm.types.sections, wasm.types.context,
|
||||||
|
wasm.vm, wasm.test.framework, lmemorymanager;
|
||||||
|
|
||||||
|
procedure run;
|
||||||
|
var
|
||||||
|
{ FC $0D, elem_idx=0 }
|
||||||
|
code : array[0..2] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.elem.drop');
|
||||||
|
|
||||||
|
code[0] := $FC; code[1] := $0D; code[2] := $00;
|
||||||
|
|
||||||
|
{ Drop an element segment }
|
||||||
|
ctx := make_test_context(@code[0], 3);
|
||||||
|
ctx^.ExecutionState.ElementSegments^.SegmentCount := 1;
|
||||||
|
ctx^.ExecutionState.ElementSegments^.Segments := PWASMElementSegment(kalloc(sizeof(TWASMElementSegment)));
|
||||||
|
ctx^.ExecutionState.ElementSegments^.Segments[0].FuncCount := 2;
|
||||||
|
ctx^.ExecutionState.ElementSegments^.Segments[0].FuncIndices := TWASMPUInt32(kalloc(2 * sizeof(TWASMUInt32)));
|
||||||
|
ctx^.ExecutionState.ElementSegments^.Segments[0].Dropped := false;
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_true('segment dropped', ctx^.ExecutionState.ElementSegments^.Segments[0].Dropped);
|
||||||
|
assert_true('still running', ctx^.ExecutionState.Running);
|
||||||
|
|
||||||
|
{ Drop invalid index traps }
|
||||||
|
ctx := make_test_context(@code[0], 3);
|
||||||
|
{ SegmentCount = 0 (default), so elem_idx=0 is out of range }
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_true('invalid_idx traps', not ctx^.ExecutionState.Running);
|
||||||
|
|
||||||
|
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,52 @@
|
|||||||
|
unit wasm.test.opcode.f32abs;
|
||||||
|
|
||||||
|
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..0] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
nanBits, resultBits : TWASMUInt32;
|
||||||
|
resultVal : TWASMFloat;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.abs');
|
||||||
|
|
||||||
|
code[0] := $8B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 3.5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('abs(3.5)=3.5', popf32(ctx^.ExecutionState.Operand_Stack), 3.5);
|
||||||
|
|
||||||
|
code[0] := $8B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, -3.5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('abs(-3.5)=3.5', popf32(ctx^.ExecutionState.Operand_Stack), 3.5);
|
||||||
|
|
||||||
|
code[0] := $8B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 0.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('abs(0.0)=0.0', popf32(ctx^.ExecutionState.Operand_Stack), 0.0);
|
||||||
|
|
||||||
|
{ NaN propagation }
|
||||||
|
nanBits := $7FC00000;
|
||||||
|
code[0] := $8B;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('abs(NaN)=NaN', ((resultBits and $7F800000) = $7F800000) and ((resultBits and $007FFFFF) <> 0));
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
unit wasm.test.opcode.f32add;
|
||||||
|
|
||||||
|
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..0] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
nanBits, resultBits : TWASMUInt32;
|
||||||
|
resultVal : TWASMFloat;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.add');
|
||||||
|
|
||||||
|
code[0] := $92;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 2.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('1.0+2.0=3.0', popf32(ctx^.ExecutionState.Operand_Stack), 3.0);
|
||||||
|
|
||||||
|
code[0] := $92;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, -1.0);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('-1.0+1.0=0.0', popf32(ctx^.ExecutionState.Operand_Stack), 0.0);
|
||||||
|
|
||||||
|
code[0] := $92;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 0.5);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 0.25);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('0.5+0.25=0.75', popf32(ctx^.ExecutionState.Operand_Stack), 0.75);
|
||||||
|
|
||||||
|
{ NaN propagation }
|
||||||
|
nanBits := $7FC00000;
|
||||||
|
code[0] := $92;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('NaN+1.0=NaN', ((resultBits and $7F800000) = $7F800000) and ((resultBits and $007FFFFF) <> 0));
|
||||||
|
|
||||||
|
{ Infinity: +Inf + 1 = +Inf }
|
||||||
|
nanBits := $7F800000; { +Inf }
|
||||||
|
code[0] := $92;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('+Inf+1=+Inf', resultBits = $7F800000);
|
||||||
|
|
||||||
|
{ Infinity: +Inf + (-Inf) = NaN }
|
||||||
|
nanBits := $7F800000;
|
||||||
|
code[0] := $92;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
nanBits := $FF800000; { -Inf }
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('+Inf+(-Inf)=NaN', ((resultBits and $7F800000) = $7F800000) and ((resultBits and $007FFFFF) <> 0));
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
unit wasm.test.opcode.f32ceil;
|
||||||
|
|
||||||
|
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..0] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
nanBits, resultBits : TWASMUInt32;
|
||||||
|
resultVal : TWASMFloat;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.ceil');
|
||||||
|
|
||||||
|
code[0] := $8D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 1.5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('ceil(1.5)=2.0', popf32(ctx^.ExecutionState.Operand_Stack), 2.0);
|
||||||
|
|
||||||
|
code[0] := $8D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, -1.5);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('ceil(-1.5)=-1.0', popf32(ctx^.ExecutionState.Operand_Stack), -1.0);
|
||||||
|
|
||||||
|
code[0] := $8D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, 3.0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('ceil(3.0)=3.0', popf32(ctx^.ExecutionState.Operand_Stack), 3.0);
|
||||||
|
|
||||||
|
{ NaN propagation }
|
||||||
|
nanBits := $7FC00000;
|
||||||
|
code[0] := $8D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('ceil(NaN)=NaN', ((resultBits and $7F800000) = $7F800000) and ((resultBits and $007FFFFF) <> 0));
|
||||||
|
|
||||||
|
{ Infinity: ceil(+Inf) = +Inf }
|
||||||
|
nanBits := $7F800000;
|
||||||
|
code[0] := $8D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('ceil(+Inf)=+Inf', resultBits = $7F800000);
|
||||||
|
|
||||||
|
{ Infinity: ceil(-Inf) = -Inf }
|
||||||
|
nanBits := $FF800000;
|
||||||
|
code[0] := $8D;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushf32(ctx^.ExecutionState.Operand_Stack, TWASMPFloat(@nanBits)^);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
resultVal := popf32(ctx^.ExecutionState.Operand_Stack);
|
||||||
|
resultBits := TWASMPUInt32(@resultVal)^;
|
||||||
|
assert_true('ceil(-Inf)=-Inf', resultBits = $FF800000);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
unit wasm.test.opcode.f32converti32s;
|
||||||
|
|
||||||
|
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..0] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.convert_i32_s');
|
||||||
|
|
||||||
|
code[0] := $B2;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 42);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(42)=42.0', popf32(ctx^.ExecutionState.Operand_Stack), 42.0);
|
||||||
|
|
||||||
|
code[0] := $B2;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, -1);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(-1)=-1.0', popf32(ctx^.ExecutionState.Operand_Stack), -1.0);
|
||||||
|
|
||||||
|
code[0] := $B2;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(0)=0.0', popf32(ctx^.ExecutionState.Operand_Stack), 0.0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
unit wasm.test.opcode.f32converti32u;
|
||||||
|
|
||||||
|
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..0] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.convert_i32_u');
|
||||||
|
|
||||||
|
code[0] := $B3;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 42);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(42)=42.0', popf32(ctx^.ExecutionState.Operand_Stack), 42.0);
|
||||||
|
|
||||||
|
code[0] := $B3;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(0)=0.0', popf32(ctx^.ExecutionState.Operand_Stack), 0.0);
|
||||||
|
|
||||||
|
code[0] := $B3;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi32(ctx^.ExecutionState.Operand_Stack, 1);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(1)=1.0', popf32(ctx^.ExecutionState.Operand_Stack), 1.0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
unit wasm.test.opcode.f32converti64s;
|
||||||
|
|
||||||
|
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..0] of TWASMUInt8;
|
||||||
|
ctx : PWASMProcessContext;
|
||||||
|
begin
|
||||||
|
test_begin('opcode.f32.convert_i64_s');
|
||||||
|
|
||||||
|
code[0] := $B4;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi64(ctx^.ExecutionState.Operand_Stack, 42);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(42)=42.0', popf32(ctx^.ExecutionState.Operand_Stack), 42.0);
|
||||||
|
|
||||||
|
code[0] := $B4;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi64(ctx^.ExecutionState.Operand_Stack, -1);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(-1)=-1.0', popf32(ctx^.ExecutionState.Operand_Stack), -1.0);
|
||||||
|
|
||||||
|
code[0] := $B4;
|
||||||
|
ctx := make_test_context(@code[0], 1);
|
||||||
|
pushi64(ctx^.ExecutionState.Operand_Stack, 0);
|
||||||
|
wasm.vm.tick(ctx);
|
||||||
|
assert_f32('convert(0)=0.0', popf32(ctx^.ExecutionState.Operand_Stack), 0.0);
|
||||||
|
|
||||||
|
test_end;
|
||||||
|
end;
|
||||||
|
|
||||||
|
end.
|
||||||
Loaded 20 of 323 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user