Bootstrapping in Compiler

2,261 views 24 slides Aug 17, 2020
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

This is the 4th tutorial in the series... its about bootstrapping a compiler... how a compiler compiles itself?


Slide Content

Bootstrapping

Translator

Compiling a Compiler

Tombstone Diagrams D iagrams consist of a set of “puzzle pieces” that can be used to reason about language processors and programs. Different kinds of pieces. Combination rules (not all diagrams are “well formed ”).

Tombstone Diagrams The rules for T-diagrams are very simple. A compiler written in some language “C” that translates programs in language A to language B looks like this:-

Tombstone Diagrams

Tombstone Diagrams The combination rules of Tombstone diagrams can vary:

Tombstone Diagrams The combination rules of Tombstone diagrams can vary:

Tombstone Diagrams Compilation of C programs on an x86 machine:

Bootstrapping It refers to the seemingly impossible task of pulling oneself up by the bootstraps.

Bootstrapping The idea of bootstrapping is quite simple:- You write your compiler in language A (but still let it target B) and then let it compile itself. The result is a compiler from A to B written in B . In order to use the compiler to compile a program, we must already have compiled it, and to do this we must use the compiler. In a way, it is a bit like the chicken-and-egg paradox.

Bootstrapping Note that the languages match at every connection and that the source and target program aren't standing on anything , as they aren't executed in this diagram.

Bootstrapping The basic idea in bootstrapping is to use compilers to compile themselves or other compilers . Target -> we want a compiler for ML to Pentium machine code and want this to run on a Pentium . We have -> an ML-compiler that generates HP machine code and runs on an HP machine.

Bootstrapping One way of obtaining the desired compiler would be to do binary translation, i.e., Write a compiler from HP machine code to Pentium machine code . This is a very tedious and herculean task and some efficiency will be lost in it.

Bootstrapping A better solution is to write an ML-to-Pentium compiler in ML (a high-level language). We can compile this using the ML compiler on the HP :

Bootstrapping The output is: we get a program that converts ML code into P code and now written in HP machine code ( a new compiler ).

Bootstrapping Now, we can run the ML-to-Pentium compiler on the HP and let it compile itself i.e. – We input our original program to the newly generated compiler.

Bootstrapping Finally, we get the required compiler which converts ML code into P code and also written in P language (a low-level language).

Bootstrapping The whole process can be combined as follows:

Bootstrapping

Bootstrapping for Cross-compiler Cross-compiler: It’s a compiler which runs on one machine (the host machine) but emits code for another machine (the target machine ). T-diagrams can help generating cross-compilers. For example, a compiler that runs on a Windows 10  PC but generates code that runs on Android smartphone is a cross compiler.

Bootstrapping for Cross-compiler C “cross compiler” from x86 to PPC

Bootstrapping for Cross-compiler Two Stage Compilation: It is a combination of 2 translators. The output of the first translator is provided as input to the second translator.