r/ProgrammingLanguages • u/DTCreeperMCL6 • 4h 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...)
0
Upvotes
4
u/konacurrents 3h 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.
10
u/MCWizardYT 3h 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?
3
u/tabbekavalkade 3h ago
Compiling to C looks simpler to people who already know C, but don't know assembly. With all the UBs and odd behaviours of C, it is NOT simpler. If you know how you would like your language to work, and C just doesn't work that way, you have a problem.
Pros of compiling to C:
Pros of compiling to assembly:
7
u/mooglinux 3h 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.
3
u/coderpants 3h 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.
3
u/SeaAnalyst8680 3h ago
Quite a lot has been written about how C accidentally became the de facto language of interfaces. E.g., if you're using Rust or C# and want to call out to some other component - even one written in, say, Python - the bindings are written in terms of C "things" (raw pointers, null terminated strings).
Obviously there are lots of other better ways to interface two specific ecosystems, but C is the lowest common denominator that is always an option (including when you don't know how the other component was created, e.g. a closed source library).
The answer to why languages often transpile to C is the same as to why C is used for interfaces, but if you research the interfaces aspect, you'll probably learn a lot of useful stuff along the way. Including why, if you want your programming language to ever be broadly adopted, you need to provide the functionality for it to make use of and behave as a C component.
And of course, if your language is built on C, it's straightforward to make it interact as though it were C.
3
u/GunpowderGuy 3h ago
Advantages :
-You can output c that will run on any compiler
-compiling to c makes interop with c libraries, easier
Disadvantages:
-C might not support features your language may need . Such tail call optimization.
-Hooking rts such as garbage collections might be more difficult than with a custom backend
2
u/an_unreal Lidl-Jai 3h 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.
2
u/Key_River7180 Nain. 3h ago
C is very portable and can do a lot of the heavy lifting for you. Cons: harder, much less flexibility
2
u/Ifeee001 3h ago
One con that I often see mentioned is that it'll be difficult to express certain things in C. But I personally wouldn't say it's difficult. It'll probably be more difficult in assembly.
An example is if expressions. C doesn't have that. But you could just desugar it into an uninitialised temp variable declaration and update the values at the appropriate place inside the if block. Every path inside the if block must assign to the temp variable. The else block will/must contain an initialization too so you don't get garbage memory.
E.g. let x = 23 if true else 24;
Hypothetical C:
Ignore any errors. You could also take it a step further to "keep the scope clean" and take advantage of existing inclining optimization and make every if expression emit a function that'll return the result. That's probably over kill
Pretty much any common feature can be expressed in C if you're choosing it as your backend.
C also has a lot of hidden foot guns and undefined behavior.
2
u/vmcrash 3h ago
The advantage is that you can eliminate all the hard work done in the middle and backend. No need for lower-level IR, no need for register allocator, no need for creating optimal assembly code, no need for multiple backends for different targets.
1
u/mauriciocap 3h ago
Think of C as a virtual machine better than the one accepting e.g. x86 assembly. Notice x86 assembly can also be seen as a VM running over RISC assembly, etc.
Some advantages of generating C:
1
u/dreamingforward 3m ago
One advantage is that you can now compile to nearly any processor.
1
u/honey-pony 0m ago
For context, my project is a compiled game scripting language.
The main reason I am currently compiling to C for my it is because having someone else write the backend saves a lot of trouble. I'm able to build for Linux, Windows, and WASM without writing basically any target-specific code. I can also use tcc for faster debug builds.
But the reason to choose C here over LLVM is not actually for any of those reasons (and in fact, I think I will have to swap to LLVM due to some semantic requirements of my language that aren't expressible in C). Instead, my main reason to prefer C as a backend over LLVM is because it makes it trivial to call C functions. LLVM still does not have a built-in way to handle the C ABI, meaning that you have to do a bunch of target-specific and very error-prone work just to get function calls to work, which is super annoying.
0
u/phovos 3h ago edited 2h ago
What "C-like" ultimately portends is like Assembly + the "Abstract machine" + "Call by Reference/Call by value semantics".
In other words it is [pre-compiled] assembly but made with respect to each platforms abstract machine while dealing with any memory/bus hardware/bandwidth/etc. [2s compliment? etc.] (so writing the instructions per the platform, and changing the elements of the program that change with respect to those changes) changes.
(this is for C99 and onward, the 'abstract machine' was not fully parameterized in early years of C... actually I think its C89 and forward lol but I just always use C99)
edit: downvoter fight me you can't be more accurate or descriptive than the above without investing actual time or money in it (Oh, it must be; C89 fans not withstanding, probably just old-hats, any new young adherent will devote themselves to the C99 catechism, or newer).