tailieunhanh - Ebook Basics of compiler design: Part 2

Part 2 ebook "Basics of compiler design" presentation of content: Intermediate-Code generation, machine code generation, register allocation, function calls, analysis and optimisation, memory management, bootstrapping a compiler, set notation and concepts. . | Chapter 7 Intermediate-Code Generation Introduction The final goal of a compiler is to get programs written in a high-level language to run on a computer. This means that eventually the program will have to be expressed as machine code which can run on the computer. This does not mean that we need to translate directly from the high-level abstract syntax to machine code. Many compilers use a medium-level language as a stepping-stone between the high-level language and the very low-level machine code. Such stepping-stone languages are called intermediate code. Apart from structuring the compiler into smaller jobs using an intermediate language has other advantages If the compiler needs to generate code for several different machine-architectures only one translation to intermediate code is needed. Only the translation from intermediate code to machine language . the back-end needs to be written in several versions. If several high-level languages need to be compiled only the translation to intermediate code need to be written for each language. They can all share the back-end . the translation from intermediate code to machine code. Instead of translating the intermediate language to machine code it can be interpreted by a small program written in machine code or a language for which a compiler or interpreter already exists. The advantage of using an intermediate language is most obvious if many languages are to be compiled to many machines. If translation is done directly the number of compilers is equal to the product of the number of languages and the number of machines. If a common intermediate language is used one front-end . compiler 147 148 CHAPTER 7. INTERMEDIATE-CODE GENERATION to intermediate code is needed for every language and one back-end interpreter or code generator is needed for each machine making the total number of frontends and back-ends equal to the sum of the number of languages and the number of machines. If an interpreter for an