Glulxtoc and IF-Decompiler

I am proud to announce the first public release of Glulxtoc, a Glulx to C decompiler!

Glulxtoc is a Rust project (how to install Rust), and you can install it easily using cargo: cargo install glulxtoc

Glulxtoc will output a folder of C code and a CMake script for compiling it against a Linux Glk library. In theory the code should be cross-platform, and I’d welcome help adding support for other build systems.

I’m happy with how stable it is currently, but there will be bugs. Don’t hesitate to report any issues you have.

FAQ:

  1. Why?
    1. Fun!
    2. I had been intending to use the decompiler core as the foundation for a new version of ZVM (and to write my GVM interpreter.) Glulxtoc combines the if-decompiler with a C code generator, and it wouldn’t be hard to switch the code generator for a JS or WASM code generator. However I’m not sure how feasible it will be - compiling big Glulx files is both slow and takes a lot of memory.
    3. When decompiled to C and then compiled with an optimising C compiler, large Glulx games should run considerably more efficiently. This could help with publishing large games for mobile or other restricted platforms. (However note that the final binary will not be compact file-size-wise.)
  2. What makes Glulxtoc efficient?
    There are two major optimisations:
    1. Firstly, by tracking which functions call which others, we are able to turn many Glulx VM function calls into direct C function calls. In effect it expands the idea of accelerated functions from the dozen or so in the Glulx spec to any safe function in your code, potentially 10s of thousands.
    2. Glulxtoc implements a ‘relooper’ algorithm, like those in Emscripten and Cheerp. A relooper algorithm takes a list of unstructured jumps and branches, and turns them into loops. So rather than being a knotty messy of goto statements, the output will be well structured code, and then able to receive all the optimisations of any other well-written C code.
  3. Why Rust?
    This is my first Rust project, and I’ve really enjoyed learning the language. The big advantages I found in Rust are its algebraic data types, its ability to compile to WASM, and the Petgraph graph library. There are graph libraries for other languages of course, and Petgraph isn’t without some rough edges, but I found it fairly easy to use, and when I ran into trouble I found answers quickly on Stack Overflow. Thank you Rust community!
10 Likes

Very cool.

You may also want to check out the Rust user forums. I find them more helpful and friendly than Stack Overflow.

Ah, the secret project which has been foretold…

(That is, Dannii mentioned the name at some point.)

Neat!

How much guessing does it have to do to identify function code? Can it handle “going off the rails” and interpreting opcodes on the fly?

Too much. I think it’s pretty reliable now, as long as the file was compiled by Inform 6, so that all the functions are at the beginning with nothing interleaved between them. But if you have a debug file then it will use it, no guessing required!

Can it handle “going off the rails” and interpreting opcodes on the fly?

I’m not too sure what exactly you mean… but probably not. It doesn’t support functions in RAM, only ROM. It does support some inter-function branches, but you have to manually instruct it. If it’s in glulxercise, then it’s supported.

PS, if you have time to delve into the code, I hope you’ll be simultaneously pleased and horrified at how I got function calls in strings to work :wink:

2 Likes