C as an intermediate language (2012)
yosefk.com
yosefk.com
> Without doubt, today the answer is C++ and Objective-C – languages whose first compilers emitted C code.
I wrote the first native C++ compiler for the PC. It was a huge win over the cfront based PC compilers, which were nearly unworkable.
Is that only because of the extra compilation step, or are there other things making this more slow ?
1. Compile the code 2. Generate strings for C code (C is much larger than intermediate code). Translating things like integers to strings is slow. 3. Writing the file out, as opposed to just keeping the data in memory.
C compilers are already slow (due to the preprocessor and phases of translation), so add your compiler on top of that, and you're going to have an unfixably slow solution.
I run into these sorts of problems with D's C++ interface. In particular, how "long long" and "long" are mangled when they are the same size, and when "long long" is used vs when "long" is used. It's an endless whack-a-mole problem.
One of the nice things about GNU C is that various GCC-specific extensions can be taken advantage of. For example ISO C does not require a compiler to optimise tail recursive calls, which are heavily used in functional (and logic) programming languages, where recursion is used instead of loops. It also lets you access CPU registers directly, and write inline assembly code.
If you can then insert some asm, that's good, otherwise you hit a brick wall.
EiffelStudio uses a VM like environment for the develop-compile-debug cycle, and then compilation to native code via the platform's system C compiler for deployment.
I still think it was a great language for learning programming.
The book "Object-Oriented Software Construction" by Bertrand Meyer, Eiffel creator, is also very good. I had read most of it some years ago.
RPython includes a jit generator that can be used to speedup new languages written in it. Pixie [0] is an example of another language written in RPython.
That's interesting. How does it work? I think most JIT compilers emit assembly directly and then execute it. Does PyPy generate C code while your program is running?
Pypy consists of a Python interpreter written in the RPython language. RPython is a statically typed language that is easy for an opimizing compilation framework to work with. It also happens to be a valid subset of Python but that isn't actually that important.
The RPython translation framework has three ways to run RPython code:
* The first way is by using an ahead-of-time compiler to convert the RPython to C and then passing that to a C compiler (gcc or clang).
* The second way is by compiling the RPython down to RPython bytecode and running that through an RPython bytecode interpreter. (The RPython bytecode interpreter is written in C)
* The third way is the trace compiler. It takes in a linearized trace of the program execution (as observed by the RPython bytecode interpreter) and generates optimized machine code for this trace.
So, going back to PyPy as a concrete example, this is what is going on:
PyPy is a Python interpreter written in RPython. The pypy executable contains two versions of this python interpreter, both created by the RPython translation framework. The first version is the one where the rpython code for the python interpreter gets compiled to C. The second version is one where the rpython code for the python interpreter is converted to rpython bytecode (which is stored in the data section of the pypy binary) and where this bytecode is in turn executed by the rpython bytecode interpreter.
When you run a python program with pypy, it starts being executed by the first interpreter. When this interpreter detects a hot loop in the python program, it transfers control to the RPython bytecode interpreter. This bytecode interpreter executes for one iteration of the loop, and records a trace of what rpython bytecode instructions were executed in this iteration. Then, it uses the jit-compiler to directly generate machine code for this trace, and transfers execution to that.
If it goes according to plan, the base C interpreter will have speed comparable to CPython (or slightly slower), the tracing interpreter will be very slow (but for just one iteration) and the machine code for the traces will be very fast.
-------
You might be wondering: why bother with so many different steps?
1) Why write a python interpreter in rpython and then run that on an rpython jit compiler instead of just writing a JIT compiler for Python?
The PyPy devs had already tried that with the Psyco JIT compiler.
2) Why generate C code for the first interpreter if you already have a JIT compiler that can generate machine code?
The JIT compiler can only compile linear traces, and can't compile whole functions.
3) Why not use C as an intermediate language for the JIT compiler as well?
While C is a workable intermediate language for method-at-a-time compilation, it is less suited for trace compilers.
And Bigloo Scheme. http://www-sop.inria.fr/indes/fp/Bigloo/doc/bigloo-3.html
And Gambit Scheme. http://gambitscheme.org/
There seems to be a theme here...
If my language is close to c (functions, scope, variable, types), can I take advantage of it so it's less work in my compiler to let the c compiler catch errors, or must I rewrite a full parser?
All I want to do is add pythonic indenting, range loops, maps, and geometric types with their operators (a little similar to shader languages).
If you only play around, using C is fine.
I've only dabbled in Vala, but apparently its "-g" flag lets you debug with GDB. https://wiki.gnome.org/Projects/Vala/Tutorial#Debugging
Still, my simple opinion is: If you are serious, use LLVM. It is not that much harder than outputting C and has clear advantages like debug information.
I believe it's based on #pragmas that tell the C compiler (and/or GDB, presumably) which line of which file to blame.
Serious work has been done in Vala. It's not fair to dismiss the language as a quaint toy.
The solution is to disable optimisation for debug builds, not to avoid C.
From the gnome.org page:
> The -g command line option tells the Vala compiler to include Vala source code line information in the compiled binary
1 - https://nim-lang.org/blog/2017/10/02/documenting-profiling-a...
I can give you an example from C compilers. Consider a simple loop:
int a[] = ...;
for (int i=0; i<max; i++) {
print(a[i]);
}
Something fishy is printed, so I run it in my debugger, break at some point, and print "i". Gdb can usually do that. Now think about the optimizations a compiler probably did: While the index is incremented by 1, we actually step 4 bytes further due to the int array, so the compiler does i+=4 instead to avoid the multiply by 4 instruction. While i starts at 0, we can instead use the "a" pointer and avoid to add an offset at every loop iteration. The code might actually look like this: int *a = ...
int end = a + max*4;
for (char* x=a; x < end; x+=4) {
print(x);
}
How can gdb print "i"? The trick is DWARF [0], basically the compiler inserts the information: If you want to print i here, then compute it by i=(x-a)/4.tldr: #line directives are not enough
You also can't create JIT compiled REPL using C as easily.
BTW this isn't a criticism of LLVM (far from it). It's expected to be used as a backend so any invalid IR is, pretty much by definition, a programmer error.
At the quick and dirty level, IMO C wins here as you can just splat the C you're building to a file and inspect it. With LLVM you need to get through the hurdle of building valid IR before you can serialise it.
In the longer term, I would argue that LLVM is better as you have way more control over what you're generating. Once your lowering works then the issue above is irrelevant.
LLVM does have a verifier pass which should catch every invalid IR. However, optimization passes aren't necessarily robust against weird types (I don't expect <13 x i3> to go over very well), and the backends are liable to flip out if the IR isn't in the subset they expect (e.g., weird types).
Instead of serializing it, directly write as text and process the file with the LLVM IR assembler.
Granted, it is still harder as just using C, but a step closer to LLVM and easier to debug.
I'm kind of surprised that there's not a library for emitting C code, though maybe my Google-fu is failing.
Essentially you generate a C-ast and then pretty print it. It has some limitations though.
The biggest loss is that a lot of error and debug info will be relative to the C rather than your language. This means the user will be exposed to the internals.
Probably the biggest win, is that it’s almost trivial to integrate C, and potentially C++ libraries into your language. This has been hugely beneficial to Nim, for example. They have written a full front end though so they don’t have the error issue above.
Like most things it’s a trade off.
> The biggest loss is that a lot of error and debug info will be relative to the C rather than your language. This means the user will be exposed to the internals.
In other words, sometimes the end user has to debug a bunch of code they didn't write. I wouldn't underestimate how much a pain this can be.
> Probably the biggest win, is that it’s almost trivial to integrate C, and potentially C++ libraries into your language.
Again, subject to adhering to threading models, etc. If you're writing a language as a hobby, this may not be a big concern, but at scale, someone will be using memcpy or memory barriers or thread-local memory somewhere. It is certainly possible to support those use cases, but I'd be careful about overstating how trivial that is.
In the long run you always want to do it specifically for your language, because otherwise giving usable error messages and debugging information becomes impossible.
But for anything more complex you are better off implementing complete parser with your own error handling and reporting.
Edit: one of the clearest signs of language implemented in this way is use of '@' character as part of syntax extensions as it is the only printable ascii character that has no syntactic meaning in C (the other such "unused character" is '$', but its meaning is implementation-defined, for example gcc has option that causes it to be accepted as part of identifiers, which is quite obviously the default on VMS)
The compiler has historically been fairly strongly tied to LLVM. This is changing for several reasons as the compiler is refactored, which should enable alternative backends like directly generating C or even plugging directly into GCC.
There is also an LLVM backend for generating C, at various levels of maintenance, that might be made to work.
However that would not make much sense from engineering point of view, too many years of optimization algorithms, tooling and supported architectures have been spent into gcc and LLVM backends just to thrown them away in name of purity.
For awhile LLVM could target C
https://internals.rust-lang.org/t/possible-alternative-compi...