r/Compilers • u/General_Purple3060 • Sep 09 '26
Why Construct Complex IR When You Can Inject Source?
While developing AET (Active Expandable Translator) on top of GCC, I came across an approach that I found surprisingly useful for implementing complex language semantics.
Normally, when lowering a language feature, we might construct the compiler's AST/IR programmatically:
complex semantics
↓
construct AST / TREE / IRBut there is another possibility:
complex semantics
↓
express the semantics as normal C code
↓
inject it into the existing frontend
↓
TREE → GIMPLE → RTLFor example, AET has an OO new$ construct. Its semantics include object allocation, initialization, MTCS information, constructor invocation, unref, and constructor failure handling.
Instead of manually constructing all the corresponding GCC TREE nodes, AET generates normal C code:
valueObj=({
TFirst *_notv2_6TFirst0;
unsigned int _mtcsPlatType0=0;
int _isMtcs=((AClass *)TFirst.class)->isMtcsClass();
_notv2_6TFirst0=
TFirst.newObject(sizeof(TFirst),
_isMtcs,
_mtcsPlatType0,
"TFirst");
_notv2_6TFirst0->objectSize=sizeof(TFirst);
_notv2_6TFirst0->mtcsPlatformType=_mtcsPlatType0;
_notv2_6TFirst0->_aet_magic$_123=1725348960;
TFirst_init_object_2927145182(_notv2_6TFirst0);
((debug_AObject *)_notv2_6TFirst0)
->_Z7AObject10free_childEPN7AObjectE =
_notv2_6TFirst0->_Z6TFirst22TFirst_unref_290629480EPN6TFirstE;
TFirst *tempObject123=_notv2_6TFirst0->TFirst();
if(tempObject123==NULL){
if(_notv2_6TFirst0->objectSize>0){
_notv2_6TFirst0->unref();
_notv2_6TFirst0=NULL;
}
}
_notv2_6TFirst0;
});The important point is that this is normal C code. The semantics are expressed using the host language that programmers already understand.
AET then injects this generated source directly into the current GCC preprocessing/parsing pipeline instead of writing a .c file and starting another compilation:
cpp_push_buffer(pfile, (uchar *)nbuf, len, true);The generated C goes through GCC's normal C lexer and parser, which constructs the corresponding TREE representation.
This gives me a useful separation:
AET parser / semantic analysis
↓
semantic lowering
↓
normal C code
↓
GCC C frontend
↓
TREE
↓
GIMPLE
↓
RTLThe key idea is not simply "generate C."
It is that source code is the language programmers use to express semantics, while AST/TREE/IR is the language the compiler uses internally.
For sufficiently complex semantics, I think the former can sometimes be a better construction language for the latter.
So why manually construct a large number of IR nodes when the same semantics can be expressed clearly in source code and handed to a mature frontend?
I'm interested in where others would draw the line between direct IR construction and source-level semantic injection.
8
u/ChiveSalad Sep 09 '26
This sounds like a desugaring step before lowering, which is perfectly respectable- but look at where existing projects had great luck using desugaring (local translation of operators to function calls, expanding comprehensions) vs where it exploded (inserting object oriented features with complex intertwined semantics)
4
u/dnpetrov Sep 09 '26
If you don't do anything with the generated C code except than compiling it with GCC (or any other C compiler), then you are just transpiling to C. There are quite a few tools that transpile to C / C++. Not all of them are called "compilers", but essentially they do compiler's job. E.g., hardware simulators compile hardware description languages to native code via C or C++. Some language implementations like Gambit Scheme or Chicken Scheme transpile to C. And so on.
If you actually do something with GIMPLE or with RTL, that is also fine. But keep in mind that GCC IRs are not meant for such processing, and that's kinda intentional.
2
u/awoocent Sep 09 '26
If you need to manually construct such complex IR that something like this is desirable, it's probably a sign your IR is too low-level and you should add a new intermediate stage.
What you describe is probably a suitable choice if you don't want to go to such lengths. But I don't see the benefit of C syntax being a big value-add when the C you're writing is stuff like
_notv2_6TFirst0->_aet_magic$_123=1725348960- I don't know that I agree that this is any better for developer familiarity. And you are also wasting some time running all your intrinsics through a C preprocessor, parser, and typechecker when all you really care about is the IR.2
u/jason-reddit-public Sep 09 '26
Scheme compilers are kind of famous for lowering into easier to compile versions of Scheme to compile. The first Scheme compiler Rabbit did just that (Guy L Steele Jr). Google search "nano pass Scheme compiler" for more recent work along these lines (or source to source transformation in general).
The first C++ "compiler" (cfront) was actually a translator lowering the new parts of C++ to C code (which is very close to a subset of C++).
I recently used "token" injection in a little single pass compiler. If I wanted to tackle "macros" properly, I could have made it prettier.
1
u/jason-reddit-public Sep 09 '26
I lot of the work with C interop aka "FFI" is parsing the header files. Structure layout and calling conventions have settled down quite a bit. gcc was not really conceived as a front-end and backend (potentially on purpose). libclang looks like it might be the secret sauce for that part of the equation.
Dynamic languages like Scheme don't really want to interface with C because precise GC is difficult after a C compiler gets its hand on the code. But C is kind of the lingua franca.
(If this double posts, sorry - Reddit seemed to be having a moment.)
1
u/ratchetfreak Sep 10 '26
So you have written a transpiler? Good for you.
That's not an uncommon thing to do. Plenty of language projects compile to another language to express things that are overly verbose or prone to error to do in pure C. For example C++ started as C with classes transpiled to C.
But the C step can become limited. At which point you start using a backend directly that takes in an IR. Most of those IRs are very C-like but without useful features such as infix expressions with operator priority, structured control flow scopes, type safety, etc.
1
u/kindredseer Sep 09 '26
This is how a lot of new languages work... transpiling/lowering to C. As u/jason-reddit-public mentioned, this is how C++ used to work. It's a good model because it has the bonus of making it easy for your language to interop with C.
I've used this model for madc and it works very well. I maintain it internally in memory (lowering input to a C-shaped AST tree). The exact model I use is:
My specific C-AST maintains the full semantic source information, it is just ignored when lowered to MIR level.
In my humble opinion, if every language worked this way (even just optionally) then they could all easily interop with C, and be a compiled language.
2
u/jason-reddit-public Sep 09 '26
I lot of the work with C interop aka "FFI" is parsing the header files. Structure layout and calling conventions have settled down quite a bit since the 90s.
gcc was not really conceived as a front-end and backend (potentially on purpose). libclang looks like it might be the secret sauce for that part of the equation.
Dynamic languages like Scheme don't really want to interface with C because precise GC is difficult after a C compiler gets its hand on the code. But C is kind of the lingua franca these days so it's smart to consider how to interop with it.