Commit Graph

68 Commits

Author SHA1 Message Date
ok2 f7a8bf1d24 Implement startup batching: 12x faster boot
Batch-compile all ~64 IR primitives into a single WASM module at startup.
Replaces 64 separate Module::new + Instance::new with 1 of each.
Reuses compile_consolidated_module() directly, removed compile_core_module() stub.

Boot time: 7.7ms -> 0.6ms (release), test suite: 5.1s -> 1.5s (debug).
13 of 14 optimizations now implemented. 392 tests passing.
2026-04-02 13:05:53 +02:00
ok2 94f6cb6941 Add switchable optimization config and benchmark framework
WaferConfig: unified config controlling all optimizations individually.
ForthVM::new_with_config(config) to create VMs with custom optimization settings.
All 8 switchable optimizations: peephole, constant_fold, strength_reduce, dce,
tail_call, inline (IR passes) + stack_to_local_promotion (codegen).

Benchmark framework (crates/core/tests/benchmark_report.rs):
- 7 Forth benchmarks: Fibonacci, Factorial, SumRecurse, NestedLoops, GCD, MemFill, Collatz
- Correctness verification across all configs (runs in CI)
- Full report with 128 optimization combinations (cargo test --ignored)
- Measures execution time, compilation time, WASM module bytes
- CONSOLIDATE impact comparison

Key findings from benchmark report:
- Inlining: -77% exec time on Fibonacci, -92% on Collatz
- Stack-to-local promotion: -5.5% WASM module size
- CONSOLIDATE: -72% exec time on Fibonacci (call_indirect -> direct call)
- All optimizations combined: best overall performance
2026-04-02 12:24:57 +02:00
ok2 83585d1890 Add stack-to-local promotion, verify all optimizations end-to-end
Stack-to-local promotion (Phase 1):
- is_promotable() identifies straight-line words (no control flow/calls/I/O)
- StackSim maps stack slots to WASM locals
- Stack manipulation (Swap, Rot, Nip, Tuck, Dup, Drop) emits ZERO instructions
- Prologue loads items from memory, epilogue writes back
- ~7x instruction reduction for DUP * and similar patterns

End-to-end verification (16 tests proving each optimization is active):
- verify_peephole_active: 0+ elimination
- verify_constant_folding_active: 3 4 + folded to 7
- verify_strength_reduction_active: 4* becomes shift
- verify_dce_active: code after EXIT eliminated
- verify_tail_call_active: recursive RECURSE works
- verify_inlining_active: small word inlined and folded
- verify_compound_ops_active: 2DUP works
- verify_dsp_caching_active: factorial via RECURSE
- verify_consolidation_active: CONSOLIDATE word
- verify_stack_promotion_*: 7 tests for promoted codegen

22 additional codegen promotion tests (wasmtime execution).
Fix F~ stack overflow panic (checked_sub instead of unchecked).
380 unit tests + 11 compliance tests, all passing.
2026-04-01 23:51:15 +02:00
ok2 4f59ffa19e Implement stack-to-local promotion and consolidation recompiler
Stack-to-local promotion (Phase 1: straight-line code):
- Words with no control flow/calls use WASM locals instead of memory stack
- Stack manipulation (Swap, Rot, Nip, Tuck, Dup, Drop) emits ZERO instructions
- ~7x instruction reduction for arithmetic-heavy words like DUP *
- Pre-loads consumed items from memory, writes results back at exit

Consolidation recompiler (CONSOLIDATE word):
- Recompiles all IR-based words into single WASM module
- Direct call instructions instead of call_indirect through function table
- Cranelift can inline and optimize across word boundaries
- All control flow variants support consolidated calls

342 unit tests + 11 compliance, all passing.
2026-04-01 22:56:00 +02:00
ok2 af1cf425f3 Add inlining, DSP caching, fix TailCall-in-inline bug
Inlining: store IR bodies for all words, inline Call(id) when body <= 8 ops
and non-recursive. Convert TailCall back to Call when inlining (tail position
in callee is not tail position in caller -- found via compliance test failure
where inlined TailCall caused unreachable code after the call site).

DSP global caching: cache $dsp in WASM local 0 at function entry, use
local.get/set throughout, writeback before calls and at function exit.
Reduces global access instructions by ~30-40%.

323 unit tests + 11 compliance, all passing.
2026-04-01 22:34:51 +02:00
ok2 180982576e Implement optimization pipeline: peephole, constant folding, strength reduction, DCE, tail calls
IR optimizer with 6 composable passes:
- Peephole: PushI32+Drop, Dup+Drop, Swap+Swap, Swap+Drop→Nip, identity ops
- Constant folding: binary (Add/Sub/Mul/And/Or/Xor/shifts/comparisons) + unary (Negate/Abs/Invert/ZeroEq/ZeroLt)
- Strength reduction: power-of-2 multiply→shift, PushI32(0)+Eq→ZeroEq
- Dead code elimination: truncate after Exit, constant-conditional If
- Tail call detection: last Call→TailCall when return stack balanced
- Compound ops: Over+Over→TwoDup, Drop+Drop→TwoDrop with optimized codegen

Dictionary hash index for O(1) word lookup during compilation.
wasmtime config: disable NaN canonicalization, enable module caching.
319 unit tests + 11 compliance, all passing.
2026-04-01 21:50:08 +02:00
ok2 d02b5b4437 Implement complete Floating-Point word set, 70+ float words
Separate float stack with fsp global, IEEE 754 double precision.
Stack ops: FDROP FDUP FSWAP FOVER FROT FDEPTH
Arithmetic: F+ F- F* F/ FNEGATE FABS FMAX FMIN FSQRT FLOOR FROUND F**
Comparisons: F0= F0< F= F< F~
Memory: F@ F! SF@ SF! DF@ DF! FLOAT+ FLOATS FALIGNED FALIGN
Conversions: D>F F>D S>F F>S
Trig: FSIN FCOS FTAN FASIN FACOS FATAN FATAN2 FSINCOS
Exp/Log: FEXP FEXPM1 FLN FLNP1 FLOG FALOG
Hyperbolic: FSINH FCOSH FTANH FASINH FACOSH FATANH
I/O: F. FE. FS. REPRESENT >FLOAT PRECISION SET-PRECISION
Defining: FVARIABLE FCONSTANT FVALUE FLITERAL
Float literal parsing (1E, 1.5E2, -3.14E0 format)
299 unit tests + 11 compliance tests, 0 errors on float test suite
2026-04-01 20:38:48 +02:00
ok2 37c583f8d7 Add working compliance test harness, 11 word sets at 100%
Replace placeholder compliance tests with real harness that boots WAFER,
loads Gerry Jackson's test suite, and asserts 0 errors per word set.

Passing word sets (11/13):
  Core, Core Plus, Core Ext, Exception, Double-Number, String,
  Search-Order, Memory-Allocation, Programming-Tools, Facility, Locals

Not yet: File-Access (needs WASI), Floating-Point, Extended-Character
272 total tests (261 unit + 11 compliance)
2026-03-31 15:25:02 +02:00
ok2 dd389c6a3d Implement Double-Number and String word sets, fix memory panics
Double-Number (19 words): D+ D- DNEGATE DABS D2* D2/ D0= D0< D= D< DU<
  DMAX DMIN D>S M+ M*/ D. D.R 2ROT 2CONSTANT 2VARIABLE 2VALUE 2LITERAL
  Double-number literal parsing (tokens ending with '.')
String (5 words): COMPARE SEARCH /STRING BLANK -TRAILING SLITERAL
Fix all memory access panics with bounds checking throughout host functions.

8 word sets at 100%: Core, Core Ext, Exception, Double, String,
  Search-Order, Memory-Allocation, Programming-Tools
2026-03-31 14:43:30 +02:00
ok2 193ad7ec5a Add optimization docs, workspace lints, and pre-commit hooks
- Add docs/OPTIMIZATIONS.md: catalog of 14 optimization passes with
  status tracking and implementation roadmap
- Configure workspace-level clippy and rustc lints in Cargo.toml
- Add clippy.toml and deny.toml for clippy thresholds and dependency
  auditing (licenses, advisories, bans)
- Set up pre-commit hook: cargo fmt, dprint, clippy, cargo deny,
  cargo machete
- Update Justfile with deny/machete targets, dprint in fmt checks
2026-03-30 23:01:35 +02:00
ok2 7507b1f164 Achieve 100% Core Extensions compliance, 261 tests
Implement 25+ Core Extension words:
- VALUE/TO, DEFER/IS/ACTION-OF, :NONAME
- CASE/OF/ENDOF/ENDCASE, ?DO, AGAIN
- PARSE, PARSE-NAME, S\", C", HOLDS, BUFFER:
- 2>R, 2R>, 2R@, U>, .R, U.R, PAD, ERASE, UNUSED
- REFILL, SOURCE-ID, MARKER (stub)

Fix panic on invalid memory access (bounds check in FIND).
Rewrite FIND/WORD host functions for inline operation.
Add BeginAgain IR variant and codegen.

Three word sets at 100%: Core, Core Extensions, Exception.
2026-03-30 22:19:49 +02:00
ok2 b52b4a79ce Achieve 100% Core compliance, implement CATCH/THROW
Core word set: 0 errors on Gerry Jackson's forth2012-test-suite/core.fr
- Fix POSTPONE for non-immediate words via COMPILE, mechanism
- Fix double-DOES> (WEIRD: pattern) with does-body scanning and
  runtime patching via _DOES_PATCH_
- Implement CATCH/THROW exception handling using wasmtime trap
  mechanism with stack pointer save/restore
- 232 tests passing
2026-03-30 21:26:21 +02:00
ok2 8aaffad6b5 Reach 97% Core compliance: 58 errors down to 3
- Fix HERE corruption: sync user_here before writing to shared cell
- Fix DOES> without CREATE: patch most-recent word, not read new name
- Implement >BODY via word_pfa_map tracking parameter field addresses
- Nested BEGIN...WHILE...WHILE...REPEAT...ELSE...THEN support
- DEPTH overflow protection
- Forth 2012 core.fr: 3 errors remaining (POSTPONE edge case,
  double-DOES>, NOP meta-programming)
2026-03-30 21:02:00 +02:00
ok2 84bfe5c581 Fix Core test suite compliance: >IN sync, RSHIFT, +LOOP, pictured output
Major compliance fixes for running Gerry Jackson's core.fr tests:
- >IN synchronization: outer interpreter reads >IN back from WASM memory
  after each word, enabling TESTING and other >IN-manipulating words
- RSHIFT changed to logical (unsigned) shift per Forth 2012 spec
- +LOOP uses boundary-crossing termination check for negative steps
- HEX/DECIMAL compile as WASM primitives (work inside definitions)
- BASE read from WASM memory for all number formatting
- Pictured numeric output: <# # #S #> HOLD SIGN
- New words: 2@ 2! .( ] ArithRshift
- Error recovery resets compile state on failure
- FIND reads counted strings from WASM memory
- Forth 2012 core.fr: 58 errors remaining (from unable-to-load)
2026-03-30 18:17:59 +02:00
ok2 d9cd6f2b10 Add DOES>, EVALUATE, double-cell arithmetic, and 20+ more Core words
- DOES> with split-compilation for defining words (CREATE , DOES> @ pattern)
- EVALUATE for string interpretation
- Double-cell: M* UM* UM/MOD FM/MOD SM/REM S>D */ */MOD
- Parsing: WORD FIND COUNT >NUMBER >IN STATE
- Memory: CMOVE CMOVE>
- Compile-time: ABORT" S" (compile mode)
- 219 tests passing, ~90% Core word set coverage
- Update docs to reflect current implementation
2026-03-29 23:40:37 +02:00
ok2 5e08f7a11f Add 50+ Core words: loops, defining words, memory, system primitives
- Loop support: I, J, UNLOOP, LEAVE
- Defining words: VARIABLE, CONSTANT, CREATE
- Memory: HERE, ALLOT, comma, C-comma, CELLS, CELL+, CHARS, CHAR+,
  ALIGNED, ALIGN, MOVE, FILL
- Stack: 2DUP, 2DROP, 2SWAP, 2OVER, ?DUP, PICK, MIN, MAX, WITHIN
- Comparison: 0<>, 0>
- System: EXECUTE, IMMEDIATE, DECIMAL, HEX, TYPE, SPACES, tick,
  CHAR, [CHAR], ['], >BODY, ENVIRONMENT?, SOURCE, ABORT
- Number output now respects BASE (HEX FF DECIMAL . prints 255)
- 185 tests passing
2026-03-29 23:10:51 +02:00
ok2 c548f17f1e Implement core Forth runtime: dictionary, codegen, outer interpreter, REPL
- Dictionary: linked-list word headers in simulated linear memory with
  create/find/reveal, case-insensitive lookup, IMMEDIATE flag support
- WASM codegen: IR-to-WASM translation via wasm-encoder with full
  validation; all stack, arithmetic, comparison, logic, memory, control
  flow, and return stack operations; wasmtime execution tests
- Outer interpreter: tokenizer, number parsing (decimal/$hex/#dec/%bin),
  interpret/compile dispatch, control structures (IF/ELSE/THEN,
  BEGIN/UNTIL, BEGIN/WHILE/REPEAT), RECURSE, comments, string output
- 40+ primitive words registered via JIT-compiled WASM modules linked
  to shared memory/globals/table
- Interactive REPL with rustyline, piped input, and file execution
- 145 tests passing across dictionary, codegen, and runtime
2026-03-29 22:48:37 +02:00
ok2 7d9937d0d8 Initial commit: WAFER (WebAssembly Forth Engine in Rust)
Optimizing Forth 2012 compiler targeting WebAssembly with IR-based
compilation pipeline, multi-typed stack inference, subroutine threading,
and JIT/consolidation modes. Rust kernel with ~35 primitives and Forth
standard library for core/core-ext word sets.
2026-03-29 22:30:18 +02:00