r/AskComputerScience • u/kovlin • May 27 '18
At a level above the very basic, how does the process of compiling work?
So far as I know, I already know the laymen-level basics of the process...compilers translate the given program into Assembly, and it's assembled from there into machine code.
But as a CS student, I'd love to have a slightly more detailed understanding of how that translation takes place, and how it is that compiled programs can often be run on computers with different CPUs, when the variant of Assembly is dependent upon a given CPU. I'm looking to gain more insight into the process generally, and perhaps a better window into how one might write a compiler (or language), assuming those topics are sufficiently connected.
2
u/cdrootrmdashrfstar May 28 '18
5 part series on how to create a compiler from my favorite Finnish man!
1
4
u/IgnorantPlatypus May 27 '18
LLVM (Clang) compiles source code into an intermediate representation. That's the front end of the compiler. To allow LLVM to work on another programming language, you'd need to write parsing code and translation into the intermediate representation.
From the IR, the back end translates into different assembly flavors for different architectures. So to allow Clang to compile to a new kind of hardware, you'd write a new backend that translates IR into your new assembly primitives.
I don't know how gcc does it but there's going to be some similarities.
1
u/Felicia_Svilling May 28 '18
As a CS student, isn't there a compiler course you can take?
1
u/kovlin May 28 '18
Not as a part of my degree, and I haven't even seen a course offered in the topic at my institution. Too bad.
1
u/kovlin Sep 16 '18
Update: such a course used to be required at my instituition, but today is no longer even offered. Too many students were failing it, and they concluded that the reason was because it was too much to cram into a single term.
1
u/Felicia_Svilling Sep 16 '18
Wow. That sounds weird. Couldn't they just lightened the burden on the course. We had an obligatory compiler course, and two optional courses on advanced compiler stuff.
1
u/kovlin Sep 16 '18
One would think; on the other hand, I heard this information from a prof who was an original architect of the CS program’s cirriculuum, with the most rigorous expectations of any prof I’ve had in 120+ credits of college work (his own class has a 50% failure rate, and he still admirably refuses to ease up on the rigor). He said the issue was that it was really graduate-level.
The undergrad program I’m in is claimed to be well-regarded and rigorous, (though of course it would be), with ABET accredation touted, national recognition, and 75% degree failure rate and 80k starting salary figures mentioned. I can’t say how much of this is hype or reality; even if a good program, neither is it Carnegie Mellon.
I do feel somewhat cheated out of the opportunity to see such material inside a classroom, but also perhaps as if I dodged a bullet, if it really was as hard as made out. I’m an extremely serious student, but wouldn’t relish a nearly impossible class standing in way of my graduation.
2
u/Steve132 May 27 '18
So far as I know, I already know the laymen-level basics of the process...compilers translate the given program into Assembly, and it's assembled from there into machine code
It depends on the language and architecture, but often this is no longer true. Often the compiler compiles into a cross platform intermediate binary format directly, which is either interpreted at runtime (if the language is Java or .NET or Python or other languages) or converted into native machine code binary (C,C++,Fortran,D,Golang)
But as a CS student, I'd love to have a slightly more detailed understanding of how that translation takes place, and how it is that compiled programs can often be run on computers with different CPUs, when the variant of Assembly is dependent upon a given CPU.
Fully compiled native programs cannot be run on computers that are totally different native instruction set architectures (ISA). For example, if your program was compiled for x86_64 (intel/amd PC), it cannot ever run on an ARM processor or RISCV processor.
However, as I said above, many languages only partially compile programs into an intermediate code format before runtime. Those programs can run on ARM or x86 without "recompiling" because they aren't native machine code when they are distributed. A program on the users computer does the final conversion.
I'm looking to gain more insight into the process generally, and perhaps a better window into how one might write a compiler (or language), assuming those topics are sufficiently connected.
A language is typically only a specification of how the computer will behave in response to a certain source code. An implementation is the actual program that realizes that specification. It can be a compiler, but many implementations are interpreters. Many languages have no implementation.
26
u/2opt May 27 '18 edited May 27 '18
In the most basic form, there are usually four steps:
Lexing: take the text of your program and use regular expressions to convert that text into symbols.
input: int x = y + 3; output: IDENTIFIER(INT) IDENTIFIER(x) EQUALS IDENTIFIER(y) PLUS CONSTANT(3) SEMICOLON
Parsing: take this string of symbols and use a grammar to form a syntax tree out of these symbols. Something like this.
Semantic analysis (type-checking / other static analysis): traverse the syntax tree you made and check whether all the types agree with a type system for your language.
Compilation: traverse your syntax tree and convert expressions into blocks of assembly text.
The first three steps are sometimes called the "front-end" of a compiler, and the last step is the "back-end" of the compiler. Real compilers are more complicated with a lot of intermediate representations (e.g. for performance optimizations), but the basic steps are about the same.
Compilers usually have a "target" assembly language (e.g. x86) that will work with architectures that support it. Languages that are cross-platform (such as Java) are compiled into "bytecode" and have a "runtime environment" that can interpret that bytecode into instructions that will work for the target architecture. So for Java you really need just one compiler, but you need a different runtime environment for different platforms.
There is some CS theory around each of these steps.
Lexing -> regular expressions / DFAs
Parsing -> grammars / PDAs
Semantic analysis -> type rules / type systems
Compilation -> operational semantics