r/Compilers • • 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 Upvotes

4 comments sorted by

1

u/roderla Oct 21 '26

Its purpose is to compile unmodified real-world code (MAME, Quake I–III, ScummVM, Lua, SQLite, PuTTY) and run it in a browser.

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/ReverseAI Oct 21 '26

The program never gets to a syscall. "Unmodified" means its source isn't edited; everything below it is replaced. Code calls libc, Xlib, SDL, Win32 or DOS interrupts, and PRISM provides those libraries itself, on top of what a page can do: an in-memory filesystem holding the game's data, canvas/WebGL for the display, an AudioWorklet for sound, DOM events for input. The program runs in a Web Worker over a SharedArrayBuffer, so blocking calls really block (Atomics.wait). Same idea as Emscripten's libc/SDL ports, across more platforms.

Whatever a page can't offer just fails the way it would on a real system: socket() returns an error, so no networking yet. The sandbox stays intact; the program's "OS" is a set of libraries running with the page's permissions.

Your syscall picture is exactly the approach of a sibling project, MIRAGE (not public yet, soon): it recompiles x86 binaries to JS, and its Linux mode loads an i386 ELF with no kernel at all and answers every int 0x80 in JS, checked against a real kernel byte for byte. Debian 12's bash and perl run that way, and so does id software's 1990s Linux Doom. Source when you have it (PRISM), binary when you don't (MIRAGE).

I’ll post MIRAGE here when it’s out.

1

u/morglod Oct 20 '26 edited Oct 20 '26

How it handles pointers and raw memory stuff? TypedArrayBuffers?