Title: Compiler, Interpreter, and Bootstrapping
1Compiler, Interpreter, and Bootstrapping
- Motivation
- When we are asked to write a
- Compiler for a complex source language or
- Interpreter for a low level intermediate language
or - Emulator for a new virtual machine
- Our strategy?
2Motivation continued
- Strategy
- machine code VS high level language
- scratch VS improve existing code
- We introduce some basic concepts and give
- some examples to make stuff clear.
3Terminology
- Q Which programming languages play a role in
this picture? - input translator output
- source program expressed in the
target - (expressed in the implementation
language - source language) language
4Tombstone Diagram
5Tombstone diagram Combination rules
ok
ok
Wrong!
Wrong!
6Compiler Basics
Host compiler and Cross compiler
7Compiler Basics continued
Output of the first compiler is the input of the
second one
- Compile a translator implemented in high level
language
8Interpreter
- A language process accept source code and
executes it directly
9Interpreters versus Compilers
- Each faction hold the inverted set of advantages
against the other. - This part covered in previous presentation.
- A comprise exists between two extremes to achieve
reasonable compilation and acceptable run time
performance. - Define an intermediate language
-
- more high-level than machine code
- gt easier to compile to
- more low-level than source language
- gt easy to implement as an interpreter
10Interpretive Compilers
Hybrid of compilation and interpretation Pascal/Ja
va 2-stage compiler is good examples compile
source into machine independent low-level
representation P-code/Byte code interpret P-code
through machine dependent interpreter/JVM
Machine independent
Machine dependent
11Portable Compilers
- Given 2 kits, which one is more portable?
JVM M
k1
JVM M
k2
For ideal portability, compilers are .
developed in modules for easy exchange.
. implemented in high level language.
12Portable Compilers continued
Suppose we have following modules (high level).
We have no way to run Pascal program, why?
13Portable Compilers continue
- We need a interpreter
- re-implement
- We can expect a fairly easy implantation
using high level language implementation
14Portable Compilers continued
- Now, we can run our Pascal program smoothly
- Note here interpreter plays a dual role in both
compiling and execution.
15Bootstrapping
- a number of techniques which rely on
partial/inefficient compiler version to create a
full/better version - often compiling a translator expressed in its own
language
16Full Bootstrapping
- A full bootstrap is necessary when we are
building a new compiler from scratch. - Example
- We want to implement an Ada compiler for machine
M. We dont currently have access to any Ada
compiler (not on M, nor on any other machine). - Idea Ada is very large, we will implement the
compiler in a subset of Ada and bootstrap it from
a subset of Ada compiler in another language.
(e.g. C)
17Full Bootstrapping continued
- Step 1 build a compiler for Ada-S in another
language
18Full bootstrapping continued
19Full Bootstrapping continued
We are now no longer dependent on the
availability of a C compiler!
20Full Bootstrapping continued
Step 3a Build a full Ada compiler in Ada-S
Step 3b Compile with v2 compiler
From this point on we can maintain the compiler
in Ada. Subsequent versions v4,v5,... of the
compiler in Ada and compile each with the the
previous version.
M
21Half Bootstrapping
We discussed full bootstrapping which is required
when we have no access to a compiler for our
language at all. Q What if we have access to an
compiler for our language on a different machine
HM but want to develop one for TM ?
We have
We want
Idea we can use cross compilation from HM to TM
to bootstrap the TM compiler
22Half Bootstrapping continued
Step 1 Implement Ada-gtTM compiler in Ada
Step 2 Compile on HM
Ada-gtTM
HM
HM
23Half Bootstrapping continued
Step 3 Cross compile our TM compiler.
Ada-gtTM
TM
HM
From now on we can develop subsequent versions of
the compiler completely on TM
24Bootstrap to improve efficiency
The efficiency of programs and compilers Efficien
cy of programs - memory usage -
runtime Efficiency of compilers - Efficiency of
the compiler itself - Efficiency of the emitted
code
Idea We start from a simple compiler (generating
inefficient code) and develop more sophisticated
version of it. We can then use bootstrapping to
improve performance of the compiler.
25Bootstrap to improve efficiency
We have
We want
26Bootstrap to improve efficiency
27Conclusion
- To write a good compiler you may be writing
several simpler ones first - You have to think about the source language, the
target language and the implementation language. - The work of a compiler writer is never finished,
there is always version 1.x and version 2.0 and