Eli5: If a compiler is a program that converts your code into binary form for the computer, unless my understanding is incorrect and it isn’t just a program, wouldn’t the compiler also need a compiler to run it since how do you run a program without a compiler?

58 views
0

Eli5: If a compiler is a program that converts your code into binary form for the computer, unless my understanding is incorrect and it isn’t just a program, wouldn’t the compiler also need a compiler to run it since how do you run a program without a compiler?

In: 360

Compiler needed a compiler to compile – a lot of compilers can actually self-compile. But once that’s done, they’re now in binary, and no longer need a compiler.

Essentially, your code does get turned into binary, but unless you change the code, you can keep reusing the same binary. That’s what you do with most anything that runs on your computer, including compilers.

A compiler compiles your program into machine code (or any other language, but let’s ignore that for now). The compiled program can then be run on your machine (computer) because it’s already in the machine code the machine can run. So yes, someone had to compile the compiler at some point, but then it’s already compiled, so it doesn’t need the compiler anymore.

What you’re thinking about is probably the interpreter, which is a program that takes a program in some language, and actually performs the actions the program was supposed to perform. Therefore it needs to be present any time you want to run the interpreted program. You could say that the processor is an interpreter for machine code.

Compilers don’t run code, they just convert it into a form that processors can work with.

Processors are fundamentally circuits that are hard wired to do certain things when they are given a certain combination of 1’s and 0’s, and if you string enough of these small functions together you can do any computational task. Compilers make this code out of more human readable code, though it is possible to just program something directly in machine language if you really wanted to which is how things worked before compilers were invented and how the first compiler was made.

This is the compiler bootstrapping problem. Nowadays the compilers are juste written in another language where a compiler already exist. Once the program have compiled there is no need for a compiler any longer so you can run this program without having the original compiler. This is how you can create an independent compiler. Some procramming languages can also be interpreted, where you need the interpreter to run it. So you can write a compiler, run it in the already existing interpreter, and have it compile itself.

In the old days, when compilers were still quite rare. You could write machine code directly, or partially helped by existing tools. There are several such known projects where a compiler were first compiled to machine code by hand. Once the first hand written copy was down you could use this to compile the code properly.

The code that the computer actually runs is called machine code. And if you have a very simple computer, you can program directly in machine code.

But for anything other than the simplest kind of computer or the simplest kind of program, it’s simply not efficient.

So you can create a language that makes it easier. And this is what we did, and it was called Assembly. ~~Assembly basically lumps common operations and sets of operations and makes them more comprehensible at a higher level.~~ It basically created what are known as “mnemonics” which are more easily intelligible by people, that map to machine code instructions.

To take a set of assembly instructions and convert them into machine code you need a program called an “assembler.” But, the first assembler had to have been built with machine code.

But even Assembly is fairly low level and only really suitable for simple programs. The more complex a program we want to make, the higher level language we want.

So people started making higher level languages. But this languages need to be converted into machine code which requires a compiler. The first compilers were built using the low level languages (such as Assembly) at the time. Once they were made, you could use existing higher level languages and compilers to make new ones.