Compiler_ Interpreter_ and Bootstrapping by rt3463df


									       Compiler, Interpreter, and
  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

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

• Q: Which programming languages play a role in this

     input           translator              output

source program       expressed in the         target
(expressed in the   implementation           language
source language)    language

3/24/2004            COSC4301 assignment 3              3
            Tombstone Diagram

Program P implemented in L        Translator implemented in L
            P                                S -> T
            L                                   L

                              Language interpreter in L
Machine implemented in hardware
                M                               M

3/24/2004            COSC4301 assignment 3                  4
     Tombstone diagram: Combination rules

            P                 P                        P
            M                 S           S -> T       T
            M                               M
                 ok                         M         ok

                                   L         S -> T
                Wrong!                         M
3/24/2004         COSC4301 assignment 3                         5
               Compiler Basics
Host compiler and Cross compiler:

3/24/2004             COSC4301 assignment 3   6
        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:

3/24/2004                      COSC4301 assignment 3                         7
•   A language process accept source
    code and executes it directly

3/24/2004          COSC4301 assignment 3   8
    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
• 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
3/24/2004               COSC4301 assignment 3                9
            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

  Machine independent                    Machine dependent

3/24/2004               COSC4301 assignment 3                10
             Portable Compilers
    Given 2 kits, which one is more portable?

             Java->JVM                  JVM
                 M                       M

             Java->JVM                  JVM
       k2                                M
For ideal portability, compilers are
. developed in modules for easy exchange.
 . implemented in high level language.
 3/24/2004                COSC4301 assignment 3   11
    Portable Compilers continued
Suppose we have following modules (high level).

    We have no way to run Pascal program, why?

3/24/2004          COSC4301 assignment 3          12
     Portable Compilers continue
• We need a interpreter


   We can expect a fairly easy implantation
   using high level language implementation

3/24/2004          COSC4301 assignment 3      13
    Portable Compilers continued
• Now, we can run our Pascal program smoothly

• Note here interpreter plays a dual role in both compiling
  and execution.
3/24/2004             COSC4301 assignment 3                   14
• 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 don’t currently have access to
  any Ada compiler (not on M, nor on any other
• 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
     Full Bootstrapping continued
• Step 1: build a compiler for Ada-S in another language
                    Ada-S ->M

    Step 1a: build a compiler (v1) for Ada-S in another
    language.                v1
                        Ada-S ->M

3/24/2004              COSC4301 assignment 3               17
 Full bootstrapping continued

Step 1b: Compile v1 compiler
on M       v1                v1
       Ada-S ->M          Ada-S->M
           C       C->M      M
                               This compiler can be used for
                     M         bootstrapping on machine M
                                       but we do not want to rely on it

 3/24/2004               COSC4301 assignment 3                            18
       Full Bootstrapping continued
  Step 2a: Implement v2 of Ada-S compiler in Ada-S
     Ada-S ->M
Step 2b: Compile v2 compiler with v1
               v2                 v2
           Ada-S ->M v1 Ada-S->M
             Ada-S Ada-S ->M M
                       M                  We are now no longer
                       M           dependent on the availability of
   3/24/2004              COSC4301 assignment 3
                                                a C compiler!   19
     Full Bootstrapping continued
Step 3a: Build a full Ada compiler in Ada-S
Step 3b: Compile with v2 compiler
      v3              v3
    Ada->M     v2   Ada->M           From this point on we can maintain the
                                     compiler in Ada.
     Ada-S Ada-S ->M M               Subsequent versions v4,v5,... of the
               M                     compiler in Ada and compile each with
                                     the the previous version.
3/24/2004             COSC4301 assignment 3                          20
             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

      Ada->HM     Ada->HM                       Ada->TM
        HM          Ada                           TM

 Idea: we can use cross compilation from
  HM to TM to bootstrap the TM compiler
3/24/2004            COSC4301 assignment 3                21
     Half Bootstrapping continued

Step 1: Implement Ada->TM compiler in Ada

Step 2: Compile on HM
                                               Cross compiler:
   Ada->TM       Ada->TM
                                               running on HM
     Ada Ada->HM    HM                          but emits TM
           HM                                       code

 3/24/2004             COSC4301 assignment 3                 22
     Half Bootstrapping continued
 Step 3: Cross compile our TM compiler.

   Ada->TM       Ada->TM
     Ada Ada->TM    TM

From now on we can develop subsequent versions of the
compiler completely on TM

 3/24/2004           COSC4301 assignment 3              23
 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
3/24/2004              COSC4301 assignment 3               24
      Bootstrap to improve efficiency

               We have                           We want

   Ada->Mslow       Ada-> Mslow                    Ada->Mfast
      Ada             Mslow                          Ada

      Step 1
               Ada->Mfast       Ada->Mfast
                 Ada Ada-> Mslow Mslow

3/24/2004                COSC4301 assignment 3                  25
   Bootstrap to improve efficiency

Step 2 Ada->M
              fast       Ada->Mfast           Fast compiler that
         Ada Ada-> Mfast Mfast                 emits fast code!

 3/24/2004            COSC4301 assignment 3                26
• 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

To top