Compare commits

..
Author SHA1 Message Date
t3hn3rd 0a4bd589ee feature: Compiler flag added for verbose debug output
VM will now only output verbose debugging information if -dDEBUG_OUTPUT is supplied.
2026-02-24 23:07:58 +00:00
t3hn3rd eda3669ab2 Merge branch 'feature/build-script-conversion-ps' into develop 2026-02-24 21:52:51 +00:00
27 changed files with 185 additions and 75 deletions

No files matched your search

+8 -3
View File
@@ -3,7 +3,8 @@ param(
[switch]$Test,
[switch]$Clean,
[switch]$Run,
[switch]$Help
[switch]$Help,
[switch]$Debug
)
# Also accept positional string args (e.g. .\build.ps1 clean test run)
@@ -12,6 +13,7 @@ foreach ($a in $args) {
'test' { $Test = $true }
'clean' { $Clean = $true }
'run' { $Run = $true }
'debug' { $Debug = $true }
'help' { $Help = $true }
}
}
@@ -22,6 +24,7 @@ if ($Help) {
Write-Host ' test - Build with tests enabled'
Write-Host ' clean - Clean build outputs'
Write-Host ' run - Run the built executable after building'
Write-Host ' debug - Build with debug info and assertions enabled'
Write-Host ' help - Show this help message'
exit 0
}
@@ -38,7 +41,6 @@ if ($Clean) {
if (Test-Path $Bin) { Remove-Item $Bin -Recurse -Force }
if (Test-Path $Lib) { Remove-Item $Lib -Recurse -Force }
Write-Host 'Clean complete.'
if (-not $Test -and -not $Run) { exit 0 }
}
if (-not (Test-Path $Bin)) { New-Item $Bin -ItemType Directory | Out-Null }
@@ -50,7 +52,8 @@ if ($LASTEXITCODE -ne 0) { exit 1 }
# ── Build ────────────────────────────────────────────────────────────
$TestFlags = @()
if ($Test) { $TestFlags = @('-dRUN_TESTS') }
if ($Test) { $TestFlags += '-dRUN_TESTS' }
if ($Debug) { $TestFlags += '-dDEBUG_OUTPUT' }
$fpcArgs = @(
$TestFlags
@@ -73,6 +76,8 @@ $fpcArgs = @(
"$Src\project\WASURO.lpr"
)
Write-Output $fpcArgs
& $FPC @fpcArgs
if ($LASTEXITCODE -ne 0) {
Write-Host "Build failed with error code $LASTEXITCODE"
+1
View File
@@ -45,3 +45,4 @@ if ($ruleHits -gt 0) {
# ── Add more rules here ─────────────────────────────────────────────
if ($fail) { exit 1 }
Write-Host 'Lint passed.'
exit 0
+43 -18
View File
@@ -5,12 +5,11 @@ uses
console,
types,
wasm.types.builtin,
wasm.types.context,
wasm.types.stack,
wasm.parser,
wasm.vm, wasm.test.binary.return42,
wasm.types.stack
wasm.vm
{$IFDEF RUN_TESTS}
, wasm.test
, wasm.test.framework
@@ -19,6 +18,9 @@ uses
var
Context : PWASMProcessContext;
ModuleSize : Int64;
ModuleBuffer : TWASMPUInt8;
ModuleFile : File;
begin
{$IFDEF RUN_TESTS}
@@ -26,22 +28,45 @@ begin
wasm.test.run_all_tests;
halt(wasm.test.framework.FailedTests);
{$ELSE}
writestringln('[main] Initializing VM');
console.writestringln('WASURO - WebAssembly Runtime in Object Pascal');
wasm.vm.init();
writestringln('[main] Parsing WASM Binary');
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;
while (wasm.vm.tick(Context)) do;
writestringln('[main] Finished Execution');
writestring('[main] Returns: ');
while Context^.ExecutionState.Operand_Stack^.Top > 0 do begin
write(wasm.types.stack.popi32(Context^.ExecutionState.Operand_Stack),', ');
if ParamCount > 0 then begin
if not FileExists(ParamStr(1)) then begin
console.writestringln('File not found.');
halt(1);
end;
Assign(ModuleFile, ParamStr(1));
Reset(ModuleFile, 1);
ModuleSize := FileSize(ModuleFile);
GetMem(ModuleBuffer, ModuleSize);
BlockRead(ModuleFile, ModuleBuffer^, ModuleSize);
Close(ModuleFile);
console.writestringln('Module loaded.');
Context := wasm.parser.parse(ModuleBuffer, TWASMPUInt8(ModuleBuffer + ModuleSize));
while wasm.vm.tick(Context) do ;
console.writestringln('Execution finished.');
console.writestringln('');
console.writestringln('--- VM State ---');
writeln('IP: ', Context^.ExecutionState.IP);
writeln('Running: ', Context^.ExecutionState.Running);
writeln('');
writeln('Operand Stack (', Context^.ExecutionState.Operand_Stack^.Top, ' entries):');
if Context^.ExecutionState.Operand_Stack^.Top > 0 then
wasm.types.stack.walk(Context^.ExecutionState.Operand_Stack)
else
console.writestringln(' (empty)');
writeln('');
writeln('Control Stack (', Context^.ExecutionState.Control_Stack^.Top, ' entries):');
if Context^.ExecutionState.Control_Stack^.Top > 0 then
wasm.types.stack.walk(Context^.ExecutionState.Control_Stack)
else
console.writestringln(' (empty)');
console.writestringln('----------------');
FreeMem(ModuleBuffer);
end else begin
console.writestringln('Usage: WASURO <module.wasm>');
halt(1);
end;
writeln();
while true do sleep(1000);
{$ENDIF}
end.
@@ -72,8 +72,10 @@ var
totalSize: TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: Code - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Code - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
// Initialize the read/end pointers
pos:= buffer;
@@ -183,8 +185,9 @@ begin
end;
end;
end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
@@ -12,8 +12,12 @@ implementation
procedure handle(buffer: TWASMPUInt8; section_length: TWASMUInt32; ctx: PWASMProcessContext);
begin
writestring('[wasm.parser] Handle Section: Custom - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Custom - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
{ Custom sections are ignored in this implementation }
console.writestringln('[wasm.parser] TODO: Custom section skipped.');
end;
end.
@@ -20,8 +20,10 @@ var
bytesRead : TWASMUInt8;
dataCount : TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: DataCount - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: DataCount - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
bend := TWASMPUInt8(buffer + section_length);
@@ -29,8 +31,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataCount);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Count: ');
writeintlnWND(dataCount, 0);
{$ENDIF}
{ Pre-allocate data segment storage with correct count.
The actual segment data will be filled when the Data section is parsed. }
@@ -25,8 +25,10 @@ var
segData : TWASMPUInt8;
begin
writestring('[wasm.parser] Handle Section: Data - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Data - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
bend := TWASMPUInt8(buffer + section_length);
@@ -74,12 +76,14 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Segment ');
writeintWND(i, 0);
writestring(' - Mode: 0 Offset: ');
writeintWND(offset, 0);
writestring(' Size: ');
writeintlnWND(dataSize, 0);
{$ENDIF}
{ Store segment data for bulk memory ops }
segData := TWASMPUInt8(kalloc(dataSize));
@@ -100,10 +104,12 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Segment ');
writeintWND(i, 0);
writestring(' - Mode: 1 (passive) Size: ');
writeintlnWND(dataSize, 0);
{$ENDIF}
{ Store segment data for bulk memory ops }
segData := TWASMPUInt8(kalloc(dataSize));
@@ -140,6 +146,7 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @dataSize);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Data Segment ');
writeintWND(i, 0);
writestring(' - Mode: 2 MemIdx: ');
@@ -148,6 +155,7 @@ begin
writeintWND(offset, 0);
writestring(' Size: ');
writeintlnWND(dataSize, 0);
{$ENDIF}
{ Store segment data for bulk memory ops }
segData := TWASMPUInt8(kalloc(dataSize));
@@ -23,8 +23,10 @@ var
tables: PWASMTables;
indices: TWASMPUInt32;
begin
writestring('[wasm.parser] Handle Section: Element - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Element - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
buf_end := buffer;
@@ -33,8 +35,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @seg_count);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Element Segments: ');
writeintlnWND(seg_count, 0);
{$ENDIF}
tables := ctx^.ExecutionState.Tables;
@@ -75,12 +79,14 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Segment ');
writeintWND(i, 0);
writestring(' - Mode: 0 Offset: ');
writeintWND(offset, 0);
writestring(' Functions: ');
writeintlnWND(func_count, 0);
{$ENDIF}
{ Store segment data }
indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32)));
@@ -112,12 +118,14 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Segment ');
writeintWND(i, 0);
writestring(' - Mode: 1 (passive) Kind: ');
writeintWND(elem_kind, 0);
writestring(' Functions: ');
writeintlnWND(func_count, 0);
{$ENDIF}
indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32)));
for j := 0 to func_count - 1 do begin
@@ -153,6 +161,7 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @func_count);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Segment ');
writeintWND(i, 0);
writestring(' - Mode: 2 Table: ');
@@ -161,6 +170,7 @@ begin
writeintWND(offset, 0);
writestring(' Functions: ');
writeintlnWND(func_count, 0);
{$ENDIF}
indices := TWASMPUInt32(kalloc(func_count * sizeof(TWASMUInt32)));
for j := 0 to func_count - 1 do begin
@@ -36,8 +36,10 @@ var
i,j : TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: Export - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Export - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
// Initialize the read/end pointers
pos:= buffer;
@@ -85,8 +87,9 @@ begin
inc(pos, bytesRead);
end;
end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
@@ -32,8 +32,10 @@ var
i : TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: Function - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Function - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
// Initialize the read/end pointers
pos:= buffer;
@@ -64,8 +66,9 @@ begin
inc(pos, bytesRead);
end;
end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
@@ -49,8 +49,10 @@ var
tmpU64 : TWASMUInt64;
begin
writestring('[wasm.parser] Handle Section: Global - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Global - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
bend := TWASMPUInt8(buffer + section_length);
@@ -115,8 +117,9 @@ begin
if (pos < bend) and (pos^ = $0B) then
Inc(pos);
end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
@@ -71,8 +71,10 @@ var
i, j : TWASMUInt32;
limitsFlag : TWASMUInt8;
begin
writestring('[wasm.parser] Handle Section: Import - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Import - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
bend := TWASMPUInt8(pos + section_length);
@@ -84,8 +86,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, bend, @ctx^.Sections.ImportSection^.ImportCount);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Import Count: ');
writeintlnWND(ctx^.Sections.ImportSection^.ImportCount, 0);
{$ENDIF}
if ctx^.Sections.ImportSection^.ImportCount > 0 then begin
{ Allocate entries }
@@ -165,8 +169,9 @@ begin
end;
end;
end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
@@ -39,8 +39,10 @@ var
flags : TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: Memory - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Memory - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
bend := TWASMPUInt8(buffer + section_length);
@@ -77,8 +79,9 @@ begin
while ctx^.ExecutionState.Memory^.PageCount < mem^.InitialPages do
wasm.types.heap.expand_heap(ctx^.ExecutionState.Memory);
end;
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
@@ -16,15 +16,19 @@ var
funcIdx : TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: Start - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Start - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
{ Read the start function index }
bytesRead := read_leb128_to_uint32(buffer, TWASMPUInt8(buffer + section_length), @funcIdx);
ctx^.Sections.StartIndex := TWASMInt32(funcIdx);
writestring('[wasm.parser] Start Function Index: ');
writeintlnWND(funcIdx, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Start Function Index: ');
console.writeintlnWND(funcIdx, 0);
{$ENDIF}
end;
end.
@@ -19,8 +19,10 @@ var
elem_type, limits_flag: TWASMUInt8;
tables: PWASMTables;
begin
writestring('[wasm.parser] Handle Section: Table - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Table - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos := buffer;
buf_end := buffer;
@@ -29,8 +31,10 @@ begin
bytesRead := read_leb128_to_uint32(pos, buf_end, @table_count);
Inc(pos, bytesRead);
{$IFDEF DEBUG_OUTPUT}
writestring('[wasm.parser] Tables: ');
writeintlnWND(table_count, 0);
{$ENDIF}
tables := ctx^.ExecutionState.Tables;
tables^.TableCount := table_count;
@@ -55,17 +59,19 @@ begin
Inc(pos, bytesRead);
end;
writestring('[wasm.parser] Table ');
writeintWND(i, 0);
writestring(' - ElemType: $');
writeintWND(elem_type, 0);
writestring(' Initial: ');
writeintWND(min_size, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Table ');
console.writeintWND(i, 0);
console.writestring(' - ElemType: $');
console.writeintWND(elem_type, 0);
console.writestring(' Initial: ');
console.writeintWND(min_size, 0);
if limits_flag = 1 then begin
writestring(' Max: ');
writeintWND(max_size, 0);
console.writestring(' Max: ');
console.writeintWND(max_size, 0);
end;
writestringln('');
console.writestringln('');
{$ENDIF}
tables^.Tables[i].ElementType := elem_type;
tables^.Tables[i].Size := min_size;
@@ -59,8 +59,11 @@ var
i, parsedTypes : TWASMUInt32;
begin
writestring('[wasm.parser] Handle Section: Type - Size: ');
writeintlnWND(section_length, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Handle Section: Type - Size: ');
console.writeintlnWND(section_length, 0);
{$ENDIF}
pos:= buffer;
// Initialize the type section
@@ -118,7 +121,9 @@ begin
end;
// Walk the wasm.types.builtin
{$IFDEF DEBUG_OUTPUT}
walk(ctx);
{$ENDIF}
end;
end.
+10 -4
View File
@@ -79,11 +79,15 @@ begin
ctx:= newContext();
pos:= buffer;
writestringln('[wasm.parser] Parsing WASM Binary');
{$IFDEF DEBUG_OUTPUT}
console.writestringln('[wasm.parser] Starting parse of WASM binary.');
{$ENDIF}
// Check for the WASM Magic
if(TWASMPUInt32(pos)^ = wasm.types.constants.WASM_HDR_MAGIC) then begin
writestringln('[wasm.parser] Binary is valid.');
{$IFDEF DEBUG_OUTPUT}
console.writestringln('[wasm.parser] Binary is valid.');
{$ENDIF}
// Set binary to valid
ctx^.ValidBinary:= true;
@@ -92,8 +96,10 @@ begin
// Read the version
ctx^.Version:= TWASMPUInt32(pos)^;
inc(pos, 4);
writestring('[wasm.parser] Version: ');
writeintlnWND(ctx^.Version, 0);
{$IFDEF DEBUG_OUTPUT}
console.writestring('[wasm.parser] Version: ');
console.writeintlnWND(ctx^.Version, 0);
{$ENDIF}
// Read the sections
while (pos < buffer_end) do begin
@@ -66,7 +66,7 @@ begin
code[0] := $A0;
ctx := make_test_context(@code[0], 1);
pushf64(ctx^.ExecutionState.Operand_Stack, TWASMPDouble(@nanBits)^);
nanBits := $FFF0000000000000;
nanBits := TWASMUInt64($FFF0000000000000);
pushf64(ctx^.ExecutionState.Operand_Stack, TWASMPDouble(@nanBits)^);
wasm.vm.tick(ctx);
resultVal := popf64(ctx^.ExecutionState.Operand_Stack);
@@ -57,7 +57,7 @@ begin
assert_true('ceil(+Inf)=+Inf', resultBits = $7FF0000000000000);
{ Infinity: ceil(-Inf) = -Inf }
nanBits := $FFF0000000000000;
nanBits := TWASMUInt64($FFF0000000000000);
code[0] := $9B;
ctx := make_test_context(@code[0], 1);
pushf64(ctx^.ExecutionState.Operand_Stack, TWASMPDouble(@nanBits)^);
@@ -53,7 +53,7 @@ begin
assert_u64('max(NaN,1.0) is NaN', resultBits and $7FF8000000000000, $7FF8000000000000);
{ max(-0.0, +0.0) = +0.0 }
negZero := $8000000000000000;
negZero := TWASMUInt64($8000000000000000);
code[0] := $A5;
ctx := make_test_context(@code[0], 1);
pushf64(ctx^.ExecutionState.Operand_Stack, TWASMPDouble(@negZero)^);
Loaded 20 of 27 files, more files were not shown because too many files have changed in this diff. Show more