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

WASURO

A WebAssembly virtual machine for the Asuro operating system, written in Standard Pascal.

WASURO parses and executes WASM binaries directly in a bare-metal kernel environment — no runtime library, no classes, no heap manager beyond what the OS provides.

Architecture

src/
├── emu/               # OS emulation layer (types, console, memory, LEB128)
├── wasm/              # WASM Virtual Machine
│   ├── parser/        # WASM binary parser
│   │   └── sections/  # Section-specific parsers (type, function, export,
│   │                  #   code, global, memory, start, data, etc.)
│   ├── types/         # WASM type system, stack, and linear memory (heap)
│   ├── vm/            # VM tick loop and opcode jump table
│   └── test/          # Test suite
│       ├── infra/     # Stack, heap, LEB128 tests
│       ├── opcodes/   # Per-opcode test units
│       └── parsers/   # Per-section parser tests
└── project/           # Lazarus project files

Key components

  • VM (wasm.vm) — Tick-based execution loop dispatching opcodes through a 256-entry jump table.
  • Parser (wasm.parser) — Validates the WASM magic/version header, then routes each binary section to its handler.
  • Stack (wasm.types.stack) — Operand and control stacks supporting i32, i64, f32, f64 push/pop.
  • Heap (wasm.types.heap) — Paged linear memory with read/write at 8/16/32/64-bit widths.
  • Emulation layer (src/emu/) — Provides the same types, console, lmemorymanager, and leb128 interfaces that exist in the real Asuro kernel, allowing development and testing on a host OS.

Implemented opcodes

Category Opcodes
Control nop, unreachable, drop, select, return
Variables local.get/set/tee, global.get/set
Constants i32.const, i64.const, f32.const, f64.const
i32 Arithmetic add, sub, mul, div_s/u, rem_s/u, clz, ctz, popcnt
i32 Bitwise and, or, xor, shl, shr_s/u, rotl, rotr
i32 Comparison eqz, eq, ne, lt_s/u, gt_s/u, le_s/u, ge_s/u
i64 Arithmetic add, sub, mul, div_s/u, rem_s/u, clz, ctz, popcnt
i64 Bitwise and, or, xor, shl, shr_s/u, rotl, rotr
i64 Comparison eqz, eq, ne, lt_s/u, gt_s/u, le_s/u, ge_s/u
f32/f64 Comparison eq, ne, lt, gt, le, ge
Memory Loads i32/i64/f32/f64.load, sign/zero-extending variants (8/16/32-bit)
Memory Stores i32/i64/f32/f64.store, narrowing variants (8/16/32-bit)
Memory Mgmt memory.size, memory.grow

Parsed sections

Type, Function, Export, Code, Global, Memory, Start, Data, Import (stub), Table (stub), Element (stub), Custom (stub).

Constraints

  • Standard Pascal only — no runtime library, no classes.
  • Kernel-compatible — all arithmetic (including shr_s) uses manual implementations, no RTL intrinsics.
  • Emulation layer — src/emu/ mirrors the real Asuro kernel API; only these interfaces are available.

Requirements

Building

# Normal build
build.bat

# Build with test suite
build.bat test

Output: bin\WASURO.exe

Testing

build.bat test
bin\WASURO.exe

Runs 374 tests across 108 test units. Exit code equals the number of failed tests (0 on success), making it CI/CD-friendly.

License

See repository for license details.

S
Description
No description provided
Readme
751 KiB
0 Stars 4 Watchers 0 Forks
Languages
Pascal 98%
PowerShell 1.1%
WebAssembly 0.9%