Presentation is loading. Please wait.

Presentation is loading. Please wait.

3/24/2004COSC4301 assignment 31 Compiler, Interpreter, and Bootstrapping Motivation: When we are asked to write a  Compiler for a complex source language.

Similar presentations


Presentation on theme: "3/24/2004COSC4301 assignment 31 Compiler, Interpreter, and Bootstrapping Motivation: When we are asked to write a  Compiler for a complex source language."— Presentation transcript:

1 3/24/2004COSC4301 assignment 31 Compiler, 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?

2 3/24/2004COSC4301 assignment 32 Motivation continued Strategy: omachine code VS high level language oscratch VS improve existing code We introduce some basic concepts and give some examples to make stuff clear.

3 3/24/2004COSC4301 assignment 33 Terminology 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

4 3/24/2004COSC4301 assignment 34 Tombstone Diagram Program P implemented in L L P S -> T L Translator implemented in L M Machine implemented in hardware MLML Language interpreter in L

5 3/24/2004COSC4301 assignment 35 Tombstone diagram: Combination rules M M P S PP T S -> T M M L P M M L P ok Wrong!

6 3/24/2004COSC4301 assignment 36 Compiler Basics Host compiler and Cross compiler:

7 3/24/2004COSC4301 assignment 37 Compiler Basics continued Two stage compiler: Compile a translator implemented in high level language: Output of the first compiler is the input of the second one

8 3/24/2004COSC4301 assignment 38 Interpreter A language process accept source code and executes it directly

9 3/24/2004COSC4301 assignment 39 Interpreters 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 => easier to compile to  more low-level than source language => easy to implement as an interpreter

10 3/24/2004COSC4301 assignment 310 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 independentMachine dependent

11 3/24/2004COSC4301 assignment 311 Portable Compilers JVM M Java->JVM JVM M Java->JVM M For ideal portability, compilers are. developed in modules for easy exchange.. developed in modules for easy exchange.. implemented in high level language.. implemented in high level language. Given 2 kits, which one is more portable? Given 2 kits, which one is more portable? k1 k2

12 3/24/2004COSC4301 assignment 312 Portable Compilers continued Suppose we have following modules (high level). We have no way to run Pascal program, why?

13 3/24/2004COSC4301 assignment 313 Portable Compilers continue We need a interpreter re-implement We can expect a fairly easy implantation using high level language implementation

14 3/24/2004COSC4301 assignment 314 Portable Compilers continued Now, we can run our Pascal program smoothly Note here interpreter plays a dual role in both compiling and execution.

15 3/24/2004COSC4301 assignment 315 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

16 3/24/2004COSC4301 assignment 316 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 don’t 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)

17 3/24/2004COSC4301 assignment 317 Full Bootstrapping continued Step 1: build a compiler for Ada-S in another language Ada-S ->M C v1 Ada-S ->M C v1 Step 1a: build a compiler (v1) for Ada-S in another language.

18 3/24/2004COSC4301 assignment 318 Full bootstrapping continued Ada-S ->M C v1 M Ada-S->M v1 Step 1b: Compile v1 compiler on M M C->M M This compiler can be used for bootstrapping on machine M but we do not want to rely on it permanently!

19 3/24/2004COSC4301 assignment 319 Full Bootstrapping continued Ada-S ->M Ada-S v2 Step 2a: Implement v2 of Ada-S compiler in Ada-S Ada-S ->M Ada-S v2 M M Ada-S->M v2 Step 2b: Compile v2 compiler with v1 compiler Ada-S ->M M v1 We are now no longer dependent on the availability of a C compiler!

20 3/24/2004COSC4301 assignment 320 Full Bootstrapping continued Step 3a: Build a full Ada compiler in Ada-S Ada->M Ada-S v3 Step 3b: Compile with v2 compiler M Ada->M v3 Ada-S ->M M v2 Ada->M Ada-S v3 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.

21 3/24/2004COSC4301 assignment 321 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 ? Ada->HM HM Ada->HM Ada Ada->TM TM We haveWe want Idea: we can use cross compilation from HM to TM to bootstrap the TM compiler

22 3/24/2004COSC4301 assignment 322 Half Bootstrapping continued Step 1: Implement Ada->TM compiler in Ada Ada->TM Ada Step 2: Compile on HM Ada->TM Ada HM Ada->HM HM Ada->TM Cross compiler: running on HM but emits TM code HM

23 3/24/2004COSC4301 assignment 323 Half Bootstrapping continued Step 3: Cross compile our TM compiler. Ada->TM Ada Ada->TM HM TM DONE! From now on we can develop subsequent versions of the compiler completely on TM Ada->TM

24 3/24/2004COSC4301 assignment 324 Bootstrap to improve efficiency The efficiency of programs and compilers: Efficiency 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.

25 3/24/2004COSC4301 assignment 325 Bootstrap to improve efficiency Ada->M slow Ada Ada-> M slow M slow Ada->M fast Ada Ada->M fast Ada M Ada->M fast M slow Step 1 Ada-> M slow M slow We haveWe want

26 3/24/2004COSC4301 assignment 326 Bootstrap to improve efficiency Step 2 Ada->M fast Ada M Ada->M fast M fast Ada-> M fast M slow Fast compiler that emits fast code!

27 3/24/2004COSC4301 assignment 327 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 …


Download ppt "3/24/2004COSC4301 assignment 31 Compiler, Interpreter, and Bootstrapping Motivation: When we are asked to write a  Compiler for a complex source language."

Similar presentations


Ads by Google