Compiler Construction Chapter 11 1 Compiler Construction Compiler Construction
A New Compiler • Perhaps a new source language • Perhaps a new target for an existing compiler • Perhaps both 2 Compiler Construction Compiler Construction
Source Language • Larger, more complex languages generally require larger, more complex compilers • Is the source language expected to evolve? – E.g., Java 1.0 → Java 1.1 → . . . – A brand new language may undergo considerable change early on – A small working prototype may be in order – Compiler writers must anticipate some amount of change and their design must therefore be flexible – Lexer and parser generators (like Lex and Yacc) are therefore better than hand- coding the lexer and parser when change is inevitable 3 Compiler Construction Compiler Construction
Target Language • The nature of the target language and run-time environment influence compiler construction considerably • A new processor and/or its assembler may be buggy Buggy targets make it difficult to debug compilers for that target! • A successful source language will persist over several target generations – E.g., 386 → 486 → Pentium → . . . – Thus the design of the IR is important – Modularization of machine-specific details is also important 4 Compiler Construction Compiler Construction
Compiler Performance Issues • Compiler speed • Generated code quality • Error diagnostics • Portability • Maintainability 5 Compiler Construction Compiler Construction
Compiler Speed • Reduce the number of modules • Reduce the number of passes Perhaps generate machine code on the first pass • Disadvantages: – Target code may not be high quality – The compiler may be difficult to maintain 6 Compiler Construction Compiler Construction
Compiler Portability • Retargetability Easily modified to generate code for a different target language • Rehostability Easily modified to run on a different machine • A portable compiler may not be as effi cient as a compiler designed for a specifi c machine A specific machine compiler can be tuned to the specific target language 7 Compiler Construction Compiler Construction
Bootstrapping • Illustrated by remarks such as “A C compiler for a new platform written in C” • The process is: 1. Devise a minimal subset of language. 2. Write a compiler for that minimal subset in the language of that minimal subset 3. Hand-translate this minimal subset source code into assembly language and assemble it—this produces a working compiler for a subset of the target language 4. Write a new compiler in the language of the working compiler to accept more source features that are missing from the language of the working compiler 5. Use the working compiler to compile this new compiler—this produces a new working compiler which accepts a superset of the language under which it was compiled 6. Repeat the last two steps until the working compiler realizes all features in the source language 8 Compiler Construction Compiler Construction
Bootstrapping History • The concept was developed in the mid-1950s • The fi rst L ISP interpreter was built using bootstrapping 9 Compiler Construction Compiler Construction
T-Diagrams Useful for describing the bootstrapping process A compiler can be characterized by three languages: • Source language: S S T • Target language: T I • Implementation language: I A T-diagram is also called a SIT diagram 10 Compiler Construction Compiler Construction
Our Decaf T-Diagram MIPS Decaf Asm. C++ 11 Compiler Construction Compiler Construction
Early C++ Translator Cfront translated C++ into C to be compiled by a standard C compiler C++ C C 12 Compiler Construction Compiler Construction
Cross-compilation A cross-compiler produces target code for a machine different from the one on which it is run For example, running gcc on an Pentium Linux platform and generating code for a 68000 PalmOS platform 68000 C ML C 13 Compiler Construction Compiler Construction
Compiling a Compiler 1. Suppose we have cross-compiler for a new language L in implementation language S generating code for machine N . LSN 2. Suppose we also have an existing S compiler running on machine M implementing code for machine M : SMM 3. Run LSN through SMM to produce LMN 14 Compiler Construction Compiler Construction
Composing T-Diagrams L L N N S S M M M LSN + SMM = LMN 15 Compiler Construction Compiler Construction
Composition Example 1. Suppose we have cross-compiler for a new language, Decaf, implemented in C++: DecafC++MIPS 2. Suppose we also have an existing C++ compiler for a PowerPC machine (e.g., Mac) C++PowerPCPowerPC 3. Run DecafC++MIPS through C++PowerPCPowerPC to produce DecafPowerPCMIPS 16 Compiler Construction Compiler Construction
Bootstrapping Let’s create a compiler for a new language L that runs on machine M 1. Write a compiler for S , a subset of L : SMM Here, M is assembly language 2. Write the compiler for L using language S : LSM 3. Compile LSM under SMM to make LMM LMM is a compiler for language L that produces code for machine M L L M M S S M M M 17 Compiler Construction Compiler Construction
Retargeting and Rehosting the Compiler Let’s make an L compiler for a different machine, N 1. Write LLN 2. Compile LLN with LMM to produce LMB This make a cross-compiler for N that runs on machine M 3. Compile LLN with the cross-compiler to produce LNN This makes a compiler for language L that runs on machine N L N L N L L L N N N L L L M M M 18 Compiler Construction Compiler Construction
Recommend
More recommend