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

But there is another possibility:

complex semantics
      ↓
express the semantics as normal C code
      ↓
inject it into the existing frontend
      ↓
TREE → GIMPLE → RTL

For 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
          ↓
         RTL

The 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.

6 Upvotes

46 comments sorted by

View all comments

Show parent comment

-1

u/General_Purple3060 Sep 09 '26

I'm using C because complex semantics are easier to express, reason about, and maintain in normal C code than through low-level TREE construction APIs (such as build_modify_expr, COMPONENT_REF, etc.).

Even though the generated C code contains internal naming noise like _aet_magic$_123 (which I can still optimize for better readability), it remains infinitely more human-readable to a compiler developer than chains of raw GCC Tree API calls. For me, it functions as a clear, high-level blueprint of the actual memory and initialization logic.

4

u/awoocent Sep 09 '26

it remains infinitely more human-readable to a compiler developer

I mean. I mean...

1

u/General_Purple3060 Sep 09 '26

Fair enough — “infinitely” was definitely hyperbole. :)

I’m not claiming the generated C is a work of art. My point is purely pragmatic: expressing semantic lowering as regular C operations is much easier for me to reason about and maintain than manually chaining low-level APIs like build_modify_expr and COMPONENT_REF.

The generated C is essentially a raw semantic blueprint, and there is definitely room to make the naming cleaner.

If a bug appears, would you rather debug a flattened C string or the raw GCC TREE?

3

u/Jwosty Sep 09 '26

If a bug appears, would you rather debug a flattened C string or the raw GCC TREE?

Neither -- false dichotomy. I'd rather debug my own internal AST structure, which presumably I wrote nice pretty printing for

1

u/General_Purple3060 Sep 09 '26

That’s fair — I’d debug my own AET IR first as well.

My point is about the next stage: once the AET IR has resolved the semantics, I find flattened C a convenient way to express the lowering into GCC, rather than manually constructing GCC TREE nodes.

So the question is really how to lower AET IR into GCC’s representation.

1

u/Jwosty Sep 09 '26

I mean yeah if you must compile to C, it sounds like GCC's internal representation is the wrong tool for the job. I do wonder what your original reason was for using another compiler's internal syntax tree structures.

Also - there's always the option of creating your own backend that lowers to assembly or machine code. Nobody said you have to go via C. That's a constraint you put on yourself and baked in as an assumption in your post. It is a valid approach and could be the correct one (depending on your situation and goals), but it does have its own tradeoffs.

1

u/General_Purple3060 Sep 09 '26

Yes, building a custom backend is certainly a valid approach. But my goal with AET is specifically to extend C with three things I think C programmers need: OO, generics, and heterogeneous programming.

GCC’s C frontend is already mature, stable, and widely used, so I’d rather spend my effort implementing those three major features instead of rebuilding a whole C compiler infrastructure from scratch.

That doesn’t mean AET has no backend work. In the process of implementing heterogeneous programming, I also built a backend for the target architecture.

So C is a deliberate design choice for AET, not just a constraint I happened to impose on myself. I want to keep the mature parts of C/GCC and put my effort where I think the missing capabilities are.

1

u/Jwosty Sep 09 '26

OK there's a useful distinction to make here.

Is the goal to make AET - the language - a superset of C - the language?

Or is the goal one of practicality: to reuse an existing mature compiler backend to save on effort?

Or both?

In other words -- is this for users of your language, or yourself as the compiler implementer?

These are actually two independent goals that have differing impacts on your design and implementation

1

u/General_Purple3060 Sep 09 '26

Both, but they are at different levels.

For users, the goal is to make AET a practical extension of C. I sometimes describe it as “2C — the second-generation C,” introducing features such as object-oriented programming (OO), generics, and heterogeneous programming — things I believe can benefit C programmers.

For me as the compiler implementer, reusing GCC’s mature C frontend and existing backend infrastructure saves development time and lets me focus my effort on these features.

So targeting C is both a language-design choice and an implementation strategy, but the user-facing goal is always the primary one.

1

u/Jwosty Sep 09 '26 edited Sep 09 '26

Do you really need to make AET a strict superset of C? Or do you just want it to be "like" C? The former has its benefits but also has real costs. You are automatically forced to inherit all the bad decisions C made. And you have to deal with the notorious can of worms that is "parsing C" (https://faultlore.com/blah/c-isnt-a-language/).

You only need the former if you truly do need to be able to make sure any valid C program is also a valid AET program (the C++ and Objective-C approach). That you can take an existing C codebase as-is and slowly start sprinkling in AES features.

Otherwise, if your target audience would just write brand new programs anyway, and all you really care about is making a C programmer feel at home -- you don't actually need a strict superset. Take this opportunity to improve the design (especially on the no-brainers). You'd be in good company with this wave of modern C-like spiritual successors: Zig, Odin, C3, Jai, etc.

Only marry yourself to C if you know you need to, as a hard baseline requirement!

On the implementer side: definitely a valid and pretty standard approach - a lot of language start this way (compiling to C) for exactly this reason. Eventually you may want to move off of that but you can stay that way for quite a while. You just have to be very aware of what semantics you may be implicitly tying to your language by essentially delegating parts of your language implementation to a C compiler. There are definitely footguns here

→ More replies

2

u/Jwosty Sep 09 '26

It sounds like you might be missing the steps of having your own AST and IR layers and/or conflating the very idea of those with GCC's API. Do you have your own AST and IR?