Documente Academic
Documente Profesional
Documente Cultură
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?
3/24/2004 COSC4301 assignment 3 1
Motivation continued
Strategy: o machine code VS high level language o scratch VS improve existing code We introduce some basic concepts and give some examples to make stuff clear.
3/24/2004 COSC4301 assignment 3 2
Terminology
Q: Which programming languages play a role in this picture?
input
translator
output
target language
Tombstone Diagram
Program P implemented in L P L Translator implemented in L S -> T L
Language interpreter in L M L
3/24/2004
COSC4301 assignment 3
P M M
P S
ok
P L M
S -> T M M
P T
ok
P L
Wrong!
COSC4301 assignment 3
S -> T M
Wrong!
5
3/24/2004
Compiler Basics
Host compiler and Cross compiler:
3/24/2004
COSC4301 assignment 3
3/24/2004
COSC4301 assignment 3
Interpreter
3/24/2004
COSC4301 assignment 3
Interpretive Compilers
Hybrid of compilation and interpretation Pascal/Java 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
3/24/2004
Machine dependent
COSC4301 assignment 3 10
Portable Compilers
k2
Java->JVM JVM
JVM M
3/24/2004
COSC4301 assignment 3
12
re-implement
We can expect a fairly easy implantation using high level language implementation
3/24/2004 COSC4301 assignment 3 13
Note here interpreter plays a dual role in both compiling and execution.
3/24/2004 COSC4301 assignment 3 14
Bootstrapping
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
3/24/2004
COSC4301 assignment 3
15
Full 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)
3/24/2004 COSC4301 assignment 3 16
3/24/2004
COSC4301 assignment 3
17
3/24/2004
COSC4301 assignment 3
18
v3 Ada->M
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.
20
COSC4301 assignment 3
Half 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
Ada->HM HM Ada->HM Ada
We want
Ada->TM TM
Step 2: Compile on HM
Ada->TM Ada->TM HM Ada Ada->HM Cross compiler: running on HM but emits TM code
HM HM
3/24/2004 COSC4301 assignment 3 22
DONE!
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.
3/24/2004 COSC4301 assignment 3 24
3/24/2004
COSC4301 assignment 3
25
Step 2
3/24/2004
COSC4301 assignment 3
26
Conclusion
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
3/24/2004 COSC4301 assignment 3 27