Author Topic: [UPDATE] SedaiBasic - SSA pipeline, native AOT/JIT backends, and what changed  (Read 725 times)

mau.camma

  • New member
  • *
  • Posts: 7
I posted about SedaiBasic in this forum (https://forum.lazarus.freepascal.org/index.php/topic,72593.msg568516.html) some time ago.
Enough has changed since then that a new thread makes more sense than reviving the old one.

What's new, in short: the interpreter is no longer the only execution mode. There is now a full compilation pipeline, lexer -> parser -> AST -> SSA intermediate representation -> optimization passes (GVN, LICM, algebraic simplification, register allocation via backward dataflow liveness) -> register-based bytecode, and two native backends emitting x86-64 directly with no external assembler or linker. AOT compiles the whole program at load; the JIT compiles loop regions. They can be combined.

The code generator uses typed register banks (int/float/string) resolved statically in SSA, so there is no tagging, boxing or runtime type checking in the hot path. Machine registers are allocated over GPR r9-r15 and xmm2-7.

Where that lands, n-body from the Computer Language Benchmarks Game, 1M iterations, on an old i7-3630QM: bytecode interpreter 9.8s, AOT 0.86s, JIT 0.80s, AOT+JIT 0.77s. CPython 3.14 runs the same program in 16.8s. FreeBASIC -O2 is 0.196s for execution alone and 0.466s including its compilation step; compared over the full pipeline, which is the fair comparison since SedaiBasic compiles at load, the gap is currently under 2x.

Language coverage has also moved: roughly 90% compatibility with BASIC v7, 84% with the FreeBASIC dialect. The graphical console now abstracts over SDL2 (including KMSDRM) and there's a clean-room SID emulator verified against VICE-generated reference audio.

Two FPC-specific findings from the recent work, which may be more useful here than the benchmark table:

Managed types in hot recursive functions are brutal. My SSA value record carried three string fields, and the code generation entry point had several dozen locals. FPC's prologue/epilogue zeroing and RTTI finalization came to roughly 24 µs per call against 0.25 µs of actual work in the function body. Two orders of magnitude of pure frame overhead, invisible to any reasoning about the algorithm, found only by bisection profiling. A fast path with no managed locals cut SSA generation from 16.1ms to 5.2ms on a const-heavy module.

On the other side, the strictness of the type system caught a class of IR construction errors at compile time that would have been silent memory corruption elsewhere. Writing a compiler backend in Pascal turned out to be considerably more comfortable than I expected.

Questions welcome on any part of it, particularly the SSA lowering and register allocation, which is where most of the work went.

Released under GNU GPL v3.

Develop branch: https://github.com/camauri/SedaiBasic2/tree/develop


440bx

  • Hero Member
  • *****
  • Posts: 6578
I haven't looked at it in detail which is something I eventually intend to do but, it looks very interesting, particularly its mixed architectural output (VM and full binary.)

Thank you for sharing.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.


LemonParty

  • Hero Member
  • *****
  • Posts: 620
A big bunch of technologies used in this project.
Was it hard to implement JIT? And how many lines of code take JIT?
Nice work.
Lazarus v. 4.99. FPC v. 3.3.1. Windows 11

mau.camma

  • New member
  • *
  • Posts: 7
Was it hard to implement JIT? And how many lines of code take JIT?

Not many lines, a lot of weeks. And the most expensive lines are the invisible ones.  ;)

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1309
This looks fun :-)  Many thanks
Lazarus Trunk/FPC latest fixes on Windows 11
  How to use the forum:  https://wiki.lazarus.freepascal.org/Forum

mau.camma

  • New member
  • *
  • Posts: 7
SedaiBasic compiled to WebAssembly

I'm building a WebAssembly target for SedaiBasic. The compiler emits WASM straight from its own SSA. No external toolchain, no interpreter shipped to the browser.
In the screenshot: the voxel landscape demo, written in BASIC, running inside Firefox.
HTML+JS+WASM is about 32 KB.

It's not perfect, but it's a good start.


Online demo: https://www.sedai.it/demo/voxel_landscape.html

Demo sources: https://github.com/camauri/SedaiBasic2/tree/main/bas/demo

Repository: https://github.com/camauri/SedaiBasic2

Gigatron

  • Sr. Member
  • ****
  • Posts: 431
  • Amiga Rulez !!
    • Gigatron Shader Network Demo
I'ts really awesome , will try it soon , thank you very much !
Coding faster than Light ! Finishing 5 Java Projects , will come back soon ;
https://www.youtube.com/@gtrgtr9505

 

TinyPortal © 2005-2018