1 / 27

Compiler, Interpreter, and Bootstrapping

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?. Motivation continued. Strategy:

kiana
Download Presentation

Compiler, Interpreter, and Bootstrapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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? COSC4301 assignment 3

  2. Motivation 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. COSC4301 assignment 3

  3. 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 COSC4301 assignment 3

  4. Program P implemented in L Translator implemented in L S -> T L P Machine implemented in hardware Language interpreter in L L M M L Tombstone Diagram COSC4301 assignment 3

  5. M M P T S -> T S -> T M M M P P P P S L M L Tombstone diagram: Combination rules ok ok Wrong! Wrong! COSC4301 assignment 3

  6. Compiler Basics Host compiler and Cross compiler: COSC4301 assignment 3

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

  8. Interpreter • A language process accept source code and executes it directly COSC4301 assignment 3

  9. 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 COSC4301 assignment 3

  10. 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 Machine dependent COSC4301 assignment 3

  11. Portable Compilers Java->JVM Java->JVM JVM M • 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. COSC4301 assignment 3

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

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

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

  15. 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 COSC4301 assignment 3

  16. 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) COSC4301 assignment 3

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

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

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

  20. v3 Ada->M Ada->M Ada-S ->M Ada->M M M Ada-S Ada-S v3 v3 v2 Full 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 COSC4301 assignment 3

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

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

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

  24. 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. COSC4301 assignment 3

  25. Step 1 Ada->Mfast Ada->Mslow Ada->Mfast Ada Ada Ada M Ada->Mfast Mslow Ada-> Mslow Ada-> Mslow Mslow Mslow Bootstrap to improve efficiency We have We want COSC4301 assignment 3

  26. Ada->Mfast Fast compiler that emits fast code! Ada Ada->Mfast Mfast Ada-> Mfast Step 2 Mslow M Bootstrap to improve efficiency COSC4301 assignment 3

  27. 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 … COSC4301 assignment 3

More Related