r/ProgrammingLanguages • • 19m ago

Discussion Compiling to C

Ive found a lot of languages on this sub compile to c code instead of assembly or a backend. I'm curious what the advantages / disadvantages are to this. I know c is pretty optimized so is this an easy way to make a performant language or does it just make it simpler. I dont know c but I know assembly and some QBE so what are the pros and cons to directly compiling to assembly ( is it just more flexibility? etc...)

1 Upvotes

7 comments sorted by

1

u/konacurrents 11m ago

C is the portable "assembly". Otherwise, what computer is the Assembly Code written for? Also you have to deal with the linker, etc. Lots of challenges (which original compilers had to deal with). What is "backend" in your statement? Is that the AST that a GNU code generator would use? That would be valid approach too.

2

u/MCWizardYT 9m ago

The advantage is that C compilers exist already that have been optimized to death over decades. Take Clang/LLVM and GCC as examples.

If you can generate decent C code, these compilers will optimize the shit out of it for you.

You also get the bonus of automatically supporting multiple architectures instead of having to learn the syntax and optimization tricks of the assembly for every platform you want to support.

It's a bit similar to why someone would want to target something like CIL or JVM bytecode instead of writing their own virtual machine: most of the work has already been done, why reinvent the wheel?

1

u/mooglinux 9m ago

Compiling to C is a far simpler task than compiling to assembly, and lets you lean on the immense amount of human effort that has gone into making C compilers. The benefits are similar if you compiled to llvm bytecode, you get all of the optimizations and device support of llvm.

1

u/an_unreal Lidl-Jai 7m ago

Compiling to C is just really straightforward. You don't need any third-party library (like LLVM) with a massive API and a steep learning curve, or dive too deep into the low-level details of calling conventions, register allocation, object files, etc for rolling your own machine code generator.

Additionally, compiling to C gives you full tooling support out-of-the-box (e.g. for debugging your code), and it's pretty easy to cross-compile and port to other platforms.

Finally, debugging your generated code is also a lot easier than some of the other solutions. You can always look at the C file that you generated and debug that. The C compiler will also catch some of the more obvious mistakes that you make.

1

u/coderpants 6m ago

The main advantages to me are inspectability, interoperability and cross-platform capabilities.

ie it's harder to read assembly, it's more painful to drop out to native code, and you need to handle different assembler/calling semantics for different platforms.

The main disadvantages Is that likely you are buying into the C execution model - ie stack based rather than something a bit more exotic - if you try to escape then you likely lose the inspectability and interoperability.

1

u/phovos 1m ago

What "C-like" ultimately portends is like Assembly + the "Abstract machine" + "Call by Reference/Call by value semantics".

In other words it is assembly but made with respect to each platforms abstract machine while dealing with any memory/bus hardware/bandwidth/etc. (so writing the instructions for the platform, and changing the elements of the program that change with respect to those changes).

1

u/Key_River7180 Nain. 0m ago

C is very portable and can do a lot of the heavy lifting for you. Cons: harder, much less flexibility