1 / 30

CSC 415: Translators and Compilers Spring 2009

CSC 415: Translators and Compilers Spring 2009. Chapter 2 Language Processors. Language Processors. Translators and Compilers Interpreters Real and Abstract Machines Interpretive Compilers Portable Compilers Bootstrapping Case Study: The Triangle Language Processor.

Télécharger la présentation

CSC 415: Translators and Compilers Spring 2009

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. CSC 415: Translators and CompilersSpring 2009 Chapter 2 Language Processors

  2. Language Processors • Translators and Compilers • Interpreters • Real and Abstract Machines • Interpretive Compilers • Portable Compilers • Bootstrapping • Case Study: The Triangle Language Processor

  3. Translators & Compilers • Translator: a program that accepts any text expressed in one language (the translator’s source language), and generates a semantically-equivalent text expressed in another language (its target language) • Chinese-into-English • Java-into-C • Java-into-x86 • X86 assembler

  4. Translators & Compilers • Assembler: translates from an assembly language into the corresponding machine code • Generates one machine code instruction per source instruction • Compiler: translates from a high-level language into a low-level language • Generates several machine-code instructions per source command.

  5. Translators & Compilers • Disassembler: translates a machine code into the corresponding assembly language • Decompiler: translates a low-level language into a high-level language Question: Why would you want a disassembler or decompiler?

  6. Source Program Object Program Generate Object Code Semantic Analysis Translators & Compilers • Source Program: the source language text • Object Program: the target language text Compiler Syntax Check Context Constraints • Object program semantically equivalent to source program • If source program is well-formed

  7. Translators & Compilers • Why would you want to do: • Java-into-C translator • C-into-Java translator • Assembly-language-into-Pascal decompiler

  8. S T P L P L L M M Translators & Compilers P = Program Name L = Implementation Language M = Target Machine For this to work, L must equal M, that is, the implementation language must be the same as the machine language S = Source Language T = Target Language L = Translator’s Implementation Language S-into-T Translator is itself a program that runs on machine L

  9. S T P P M S T M Translators & Compilers • Translating a source program P • Expressed in language T, • Using an S-into-T translator • Running on machine M

  10. x86 x86 Translators & Compilers sort sort sort Java x86 Java x86 x86 x86 • Translating a source program sort • Expressed in language Java, • Using a Java-into-x86 translator • Running on an x86 machine The object program is running on the same machine as the compiler

  11. PPC x86 Translators & Compilers sort sort sort Java PPC Java PPC PPC download x86 • Translating a source program sort • Expressed in language Java, • Using a Java-into-PPC translator • Running on an x86 machine • Downloaded to a PPC machine Cross Compiler: The object program is running on a different machine than the compiler

  12. sort x86 x86 x86 x86 x86 x86 Translators & Compilers sort sort sort Java Java C C C x86 x86 • Translating a source program sort • Expressed in language Java, • Using a Java-into-C translator • Running on an x86 machine • Then translating the C program • Using an C-into x86 compiler • Running on an x86 machine • Into x86 object program Two-stage Compiler: The source program is translated to another language before being translated into the object program

  13. Translators & Compilers • Translator Rules • Can run on machine M only if it is expressed in machine code M • Source program must be expressed in translator’s source language S • Object program is expressed in the translator’s target language T • Object program is semantically equivalent to the source program

  14. Interpreters • Accepts any program (source program) expressed in a particular language (source language) and runs that source program immediately • Does not translate the source program into object code prior to execution

  15. Source Program Interpreters Interpreter Fetch Instruction Analyze Instruction Program Complete Execute Instruction • Source program starts to run as soon as the first instruction is analyzed

  16. Interpreters • When to Use Interpretation • Interactive mode – want to see results of instruction before entering next instruction • Only use program once • Each instruction expected to be executed only once • Instructions have simple formats • Disadvantages • Slow: up to 100 times slower than in machine code

  17. Interpreters • Examples • Basic • Lisp • Unix Command Language (shell) • SQL

  18. graph P Basic S x86 M Basic S S x86 M L Interpreters S interpreter expressed in language L Program P expressed in language S, using Interpreter S, running on machine M Program graph written in Basic running on a Basic interpreter executed on an x86 machine

  19. Real and Abstract Machines • Hardware emulation: Using software to execute one set of machine code on another machine • Can measure everything about the new machine except its speed • Abstract machine: emulator • Real machine: actual hardware An abstract machine is functionally equivalent to a real machine if they both implement the same language L

  20. C M P P M nmi nmi nmi M M nmi nmi nmi nmi M M C C Real and Abstract Machines New Machine Instruction (nmi) interpreter written in C nmi interpreter expressed in machine code M nmi interpreter written in C The nmi interpreter is translated into machine code M using the C compiler Compiler to translate C program into M machine code

  21. Interpretive Compilers • Combination of compiler and interpreter • Translate source program into an intermediate language • It is intermediate in level between the source language and ordinary machine code • Its instructions have simple formats, and therefore can be analyzed easily and quickly • Translation from the source language into the intermediate language is easy and fast An interpretive compiler combines fast compilation with tolerable running speed

  22. Java Java JVM JVM P P P M M JVM JVM Java M M JVM JVM M M Interpretive Compilers Java into JVM translator running on machine M JVM code interpreter running on machine M A Java program P is first translated into JVM-code, and then the JVM-code object program is interpreted

  23. Portable Compilers • A program is portable if it can be compiled and run on any machine, without change • A portable program is more valuable than an unportable one, because its development cost can be spread over more copies • Portability is measured by the proportion of code that remains unchanged when it is moved to a dissimilar machine • Language affects portability • Assembly language: 0% portable • High level language: approaches 100% portability

  24. Portable Compilers • Language Processors • Valuable and widely used programs • Typically written in high-level language • Pascal, C, Java • Part of language processor is machine dependent • Code generation part • Language processor is only about 50% portable • Compiler that generates intermediate code is more portable than a compiler that generates machine code

  25. Java Java C JVM JVM M P P P JVM JVM M JVM JVM Java M M M JVM JVM JVM JVM JVM JVM Java M M M C C Note: C M Compiler exists; rewrite JVM interpreter from Java to C Portable Compilers 1. Start with the following Java JVM Java 2. Rewrite interpreter in C 3. Compile the compiler 4. Java program P is translated into JVM program P and run using ghe JVM intrepreter

  26. Bootstrapping • The language processor is used to process itself • Implementation language is the source language • Bootstrapping a portable compiler • A portable compiler can be bootstrapped to make a true compiler – one that generates machine code – by writing an intermediate-language-into-machine-code translator • Full bootstrap • Writing the compiler in itself • Using the latest version to upgrade the next version • Half bootstrap • Compiler expressed in itself but targeted for another machine • Bootstrapping to improve efficiency • Upgrade the compiler to optimize code generation as well as to improve compile efficiency

  27. JVM JVM JVM JVM JVM JVM JVM JVM Java Java Java Java JVM JVM JVM JVM M M M M M M M M JVM JVM JVM JVM JVM Java Java M M M M M P P P JVM Java M M M M M M JVM JVM M M Bootstrapping Bootstrap an interpretive compiler to generate machine code 1, First, write a JVM-coded-into-M translator in Java 2. Next, compile translator using existing interpreter 3. Use translator to translate itself 5. Two stage Java-into-M compiler 4. Translate Java-into-JVM-code translator into machine code

  28. Ada-S C M M v1 v1 Ada-S M Ada-S M Ada-S M M M C Ada-S C M M M v2 v2 v3 v3 Ada-S M Ada-S M Ada M Ada M Ada-S M M Ada M Ada-S M Ada-S M Ada-S M Ada-S M Bootstrapping Full bootstrap v2 v1 1. Write Ada-S compiler in C 3. Convert the C version of Ada-S into Ada-S version of Ada-S 2. Compile v1 using the C compiler v1 v3 v2 5. Extend Ada-S compiler to (full) Ada compiler 6. Compile full version of Ada using Ada-S compiler 4. Use v1 to compile v2

  29. Ada Ada Ada Ada Ada Ada Ada HM HM HM TM TM TM TM Ada Ada Ada HM HM HM HM P P P Ada TM TM HM TM Ada TM Ada TM Ada TM TM Ada HM HM Bootstrapping Half bootstrap 3.Rewrite Ada compiler that generates HM code to Ada compiler that generates machine code for machine TM 2. Ada compiler that generates machine code for machine H expressed in HM 1. Ada compiler that generates machine code for machine H expressed in Ada 5. Make sure the compiler works properly 4. Use existing Ada compiler fro HM to compile the Ada compiler that run on HM but generates code for TM 6. Use the compiler to compile itself. Now have an Ada compiler that generates TM code that runs on machine TM

  30. Ada Ada Ada Ada Ada Ada Ada Ada Ada Ada Ms Ms Ms Ms Mf Mf Mf Mf Mf Mf v2 v2 v2 v1 v2 v3 v2 v2 v1 v1 Ada Ada Ada Ada Ms Ms Ms Ms Ms Mf P P P Ada Mf Mf M M M Bootstrapping Bootstrap to improve efficiency 2. Rewrite v1 to run on M-fast in Ada 1. Start with v1 targeted to M-slow written in Ada and compiled to run on M-slow 4. Compile a program using v2; it will compile slow but run fast 3. Use v1 to compile v2 5. Use v2 to compile v2 to produce v3 which will be the fast compiler.

More Related