r/Compilers • u/Key_Detail15 • 4d ago
An Intermediate Language Case Study
In this post I just want to present the API for my own IR, as an example of what one might look like at a vastly smaller scale (compared to LLVM). I am not selling it, and I'm not giving any links (none exist anyway). It's just something different.
Only the high-level functions for managing an entire program are listed. 'PCL' is the IL name.
Possibly, it might be simpler than QBE, however QBE deals with only a small part of the pipeline (it turns SSA text into ASM text).
proc pcl_start (int nunits = 0) # (Syntax is my systems language.)
proc pcl_end
func pcl_runpcl :int
proc pcl_genmcl
proc pcl_genss (int obj = 0)
proc pcl_exec
proc pcl_writeexe (ichar filename) # ('ichar' is like char* in C)
proc pcl_writedll (ichar filename)
proc pcl_writeobj (ichar filename)
proc pcl_writemx (ichar filename)
func pcl_writeasm (ichar filename = nil, int atype = 'AA')ichar
func pcl_writepcl (ichar filename = nil)ichar
func pcl_writepst (ichar filename = nil)ichar
func pcl_writess (ichar filename = nil, int obj = 0)ichar
proc pcl_cmdskip (int cmdskip, psymbol dcmdskip = nil)pcl_start Start a new IL program and ST. The optional 'nunits' argument gives an estimate of the program size.
At this point, there is set of 40 lower-level functions, starting with 'pc_' and 'gen_', used to build IL code, data and symbols. This is done for an entire program as this is designed for whole-program compilation.
This library uses a single global context so no context handles need to be returned or passed.
pcl_end Write an end-of-program marker. This IL program is now complete.
pcl_runpcl Run the program via interpretation of the IL. This is not fast or efficient, but it is useful for debugging, experiments, profiling or for fun. The return value is the program exit code.
pcl_genmcl Turn the IL into a native code representation. Mainly this is for x64/Windows. Experimental backends have been done for 8-bit Z80, with a brief foray into ARM64/SYS V.
pcl_genss Turn the native code representation into binary machine code.
pcl_exec Fix up the binary machine code and run it in memory.
pcl_writeexe pcl_writedll Write Windows EXE or DLL files.
pcl_writeobj Write an object file in PE+ format (this is the only time a linker is needed)
pcl_writemx Write the program in my private executable binary format
pcl_writeasm Dump as ASM source to a file or string. Supports four formats: AA (mine), NASM, GAS, and MASM, although usually only one is configured in a particular build.
pcl_writepcl Dump as IL source code to file or string. This is for debugging mainly but the result can represent an entire program (see below).
pcl_writepst pcl_writess Write the IL symbol table or dump the generated binary to a file or string.
pcl_cmdskip This is to do with moving the command line parameters when invocation is complex (see example below).
The call-pattern to generate an EXE file say might be:
pcl_start()
genpcl() ...
pcl_end()
pcl_writeexe(outfile) # calls pcl_genmcl/pcl_genss if neededThe above library is normally built-in to a compiler. The library can also be built into a standalone DLL, a dynamically linked library. I might do that if I wanted to use it from my scripting language, say.
What I don't have in this API is a way to read textual IL from a file. That is done in a standalone application that contains the necessary front-end.
Some stats for this library that includes all the above:
Line count: 17 Kloc
Build time: 60 msec
EXE size: 180 KBJIT This is not needed since compilation is so fast.
Optimisation That is not used, but generated code is decent: typically within a factor of two of optimised code.
(Shortened and edited.)
2
u/Discordbothost 4d ago
Do you run the same PCL fixture through pcl_runpcl and pcl_writeexe in tests? I'd compare stdout as well as the exit code. A little program that prints the quotient and remainder of -7 divided by 3 could catch the interpreter and codegen disagreeing on signs even when both exit cleanly.
2
u/gio_vs 4d ago
Uma biblioteca de 17Kloc que compila em 60ms e cabe em 180KB é impressionante. Fiquei com duas dúvidas: como você mede esse "dentro de um fator de dois" em relação ao código otimizado? E os backends de Z80 e ARM64 chegaram a ser funcionais ou foram apenas experimentos?