r/Compilers • u/ReverseAI • Oct 20 '26
A C/C++ compiler to readable JavaScript, held to gcc: exact i386 and 16-bit DOS ABIs, C++98 lowered to C, deterministic builds
PRISM is a C and C++ compiler written in JavaScript, targeting JavaScript (not Wasm): one JS function per C function, run by the engine's JIT. Its purpose is to compile unmodified real-world code (MAME, Quake I–III, ScummVM, Lua, SQLite, PuTTY) and run it in a browser.
Disclosure up front: the code was written with Claude Code (Anthropic's coding agent) under my direction. Because of that, the whole project is built around not trusting it: every claim is checked by a gate against an independent implementation, and each gate carries a deliberate mutation it must catch.
Some design points that might interest this sub:
- Two exact ABIs: i386 System V and 16-bit real-mode DOS (small/medium/large, far/huge pointers). Struct layout is held to gcc 12 i386 on ~105,000 generated checks, bitfields included.
- Preprocessor token-identical to gcc -E, including 289 whole translation units.
- C++98 subset lowered to C (classes, virtual and multiple inheritance, templates, exceptions on setjmp/longjmp), enough for ScummVM and Enemy Territory.
- Assembly translated, not emulated: Borland inline asm, TASM modules, Watcom #pragma aux, NASM.
- Testing: c-testsuite and gcc.c-torture/execute with 0 wrong answers (a construct it can't compile is a named refusal, never a silent miscompile), csmith against gcc -O0/-O2, and whole programs bit-identical to native builds (MAME 0.34: 15/15 games over 1,500 frames).
- Deterministic: fixed __DATE__, a virtual clock when headless, so every comparison is exact and re-checkable.
A small example of what exact comparison turns up: on one float-only Rosetta Code program, x87 gcc, SSE gcc, clang (which fuses a*b+c into an FMA by default) and PRISM gave three different characters for one pixel; PRISM matched SSE gcc and -ffloat-store, i.e. IEEE single precision rounded per operation.
No optimiser yet: about 9.5× slower than clang -O2 on a Z80-core benchmark.
You can type C or C++ in the page and see the emitted JS: https://prism-compiler.pages.dev/web/live/
Source (MIT): https://github.com/jfrelat-lab/prism
1
u/roderla Oct 21 '26
Excuse me, what?
All real-world code will always go down and somehow somewhere include library calls. That eventually map to syscalls. The operating system then handles the request - be that opening a file, reading from the network, creating a process, a thread, (some kind of) synchronization device and much more.
Now, a browser cannot offer these syscalls. If it did, you'd break the browser sandbox and opening a website would be just as dangerous as executing a binary downloaded from the web. So, must fundamentally, how on earth is this purpose ever supposed to - you know, work?
1
u/morglod Oct 20 '26 edited Oct 20 '26
How it handles pointers and raw memory stuff? TypedArrayBuffers?