LLVM backend for Glulx

Hello folks,

As a hobby project, I’ve written an LLVM backend for Glulx. This allows you to use clang to compile C (and theoretically other languages) to Glulx assembly, which you can then compile into an .ulx file with glasm (thanks @zzo38!). It’s pretty easy to use, if your C program is self-contained – for example, the only changes needed to compile @zarf 's Glk example program “model.c” directly to Glulx are renaming the main function to main and replacing glk.h with a modified version clangglk.h (see my repo above) that provides appropriate wrappers for all the Glk functions.

I’ve attached a somewhat more elaborate demonstration to this post. The “story” needs to be able to read files in the local directory, so I’ve found it easier to run in Glulxe+CheapGlk than Lectrote.

At some point it could be interesting to write an LLVM frontend for Inform 6, allowing I6 and I7 to take advantage of LLVM’s sophisticated optimizer. I have not wanted to dive into the complexities of I6’s parser, but if someone else wants to give it a shot, great! (Although if you can wait long enough, writing a frontend for I7’s new intermediate language would probably be a lot easier, once it comes out.)

In any case, I hope people find this entertaining, if not useful (it was certainly an absorbing hobby for me). If you have difficulty using the backend please feel free to ask me questions here or post issues on GitHub; I certainly don’t have time to maintain this tool properly but will try to check in on it every now and then.

Best,
Daniel

compilation.zip (335.5 KB)

18 Likes

well, we’re started with backends… the frontends WILL follow, I hope… let’s do the ultimate IF compiler, using the GCC/LLVM technology :slight_smile:

Best regards from Italy,
dott. Piergiorgio.

I admit this is not what I was expecting when I started designing the VM. :) Neat!

2 Likes

Would it be possible to use this as a way to make a json importer/exporter operation?

What do you mean?

Very cool.

Does this mean you could compile other interpreters in C into glulx? That would indeed be weird.

1 Like

well i mean possibly using json for save files instead of the serialized format.

It should, though they might need to be modified to use the Glk functions for IO?

This isn’t compatible with Inform. So you could use a JSON library, but you’d have to implement everything else yourself. It would be possible to write a JSON extension in Inform too though, which might be a better option.

obligatory joke comment: can it compile doom

Thanks! You certainly could - did you try running the example I attached to my post? It’s not an interpreter, but it is a compiler…

(To @Dannii’s reply: for the example I just implemented stdio.h using Glk, so that I didn’t have to modify the original program. The hard part isn’t the actual I/O, but rather printf and its cousins, which I cheerfully grabbed from uClibc-ng.)

@jfmherokiller: if you want to plug in some ASCII-art renderer…

1 Like

oh so you mean this Doom Rendered in TEXT - 1337D00M | Nostalgia Nerd - YouTube

Glk’s graphics functions would let you port the full graphics of Doom, but the framerate might suffer. We won’t know how bad though until someone tries! I think it might be playable.

1 Like

tbh I thought of using it more for like BigInt purposes.

I actually wrote an arbitrary-precision integer arithmetic library in I6 but never got around to writing proper documentation for it to be released. It allows up to 508 bytes of precision and supports all the basic operations, plus modular exponentiation and Montgomery arithmetic. I optimized it for Quixe and it’s decently fast, though for serious cryptography it’s hard to be as quick as one would like in pure Glulx (I ended up writing another extension to run such tasks in the background while waiting for user input…). Anyway, if it would be useful I can put it up somewhere.

3 Likes

Something I do wish which could be fun to pair up with this tool is a decompiler/disassembler for i6. I did find GitHub - wertercatt/mrifk: A decompiler and disassembler for the Glulx virtual machine. but it seems to have been forgotten.

The tool was also discussed here but that topic seems to have also dried up Reverse Engineering the Source Code from .gblorb - #23 by aschultz

1 Like

I published the first version of my Glulxtoc decompiler earlier this year: if-decompiler/glulxtoc at master · curiousdannii/if-decompiler · GitHub

3 Likes

thanks I must havent seen that while going over the forum

Btw how did you handle more complex opcodes like simd or such?

ascii doom aside, back in the 8 bit age, there was text/character graphics adventures, esp. for the commodore line of 8b computers.

Thinking along this line, will be rather interesting (and useful…) an ANSI library for glulx.

(I’m wrong, or this was already done for the magnetics interpreter, a version being capable of render the art of Pawn/Guild/Jinxter in ANSI with nice results ?)

Best regards from Italy,
dott. Piergiorgio.

oh question: is the output of the compilation able to be “shunted” into a prexisting story?

Basicly can one compile “hello.c” and then import that into an inform7 project called “world”

Not conveniently, no, since the output is Glulx assembly code that expects to be a standalone program. You can put raw Glulx assembly into an I6 inclusion within an I7 project, but my backend uses glasm’s syntax, which is different from what I6 expects. You could modify the backend to use I6’s syntax, but it would be a nontrivial amount of work: for example global variables would need to be emitted as I6 arrays.