Explore the concepts of recursive and iterative interpretation, self-identifying data, and code generation in compilers. Learn about object code production, code selection, register allocation, and optimal code generation techniques.
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
More Related Content
Presentation Transcript
Compilers 6. Code Generation Chih-Hung Wang References 1. C. N. Fischer and R. J. LeBlanc. Crafting a Compiler with C. Pearson Education Inc., 2009. 2. D. Grune, H. Bal, C. Jacobs, and K. Langendoen. Modern Compiler Design. John Wiley & Sons, 2000. 3. Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman. Compilers: Principles, Techniques, and Tools. Addison-Wesley, 1986. (2nd Ed. 2006)
Interpretation • An interpreter is a program that consider the nodes of the AST in the correct order and performs the actions prescribed for those nodes by the semantics of the language. • Two varieties • Recursive • Iterative
Interpretation • Recursive interpretation • operates directly on the AST [attribute grammar] • simple to write • thorough error checks • very slow: 1000x speed of compiled code • Iterative interpretation • operates on intermediate code • good error checking • slow: 100x speed of compiled code
Self-identifying data • must handle user-defined data types • value = pointer to type descriptor + array of subvalues • example: complex number 3.0 re: 4.0 im:
Code Generation • Compilation produces object code from the intermediate code tree through a process called code generation • Tree rewriting • Replace nodes and subtrees of the AST by target code segments • Produce a linear sequence of instructions from the rewritten AST
Machine instructions • Load_Addr M[Ri], C, Rd • Loads the address of the Ri-th element of the array at M into Rd, where the size of the elements of M is C bytes • Load_Byte (M+Ro)[Ri], C, Rd • Loads the byte contents of the Ri-th element of the array at M plus offset Ro into Rd, where the other parameters have the same meanings as above
Code generation Main issues: • Code selection – which template? • Register allocation – too few! • Instruction ordering Optimal code generation is NP-complete • Consider small parts of the AST • Simplify target machine • Use conventions
frame Simple code generation • Consider one AST node at a time • Two simplistic target machines • Pure register machine • Pure stack machine stack SP vars BP
Weighted register allocation • It is advantageous to generate the code for the child that requires the most registers first • Weight: • The number of registers required by a node
- * * T1 1 b b 4 * a c Register spilling Too few registers? • Spill registers in memory, to be retrieved later • Heuristic: select subtree that uses all registers, and replace it by a temporary example: • b*b – 4*a*c • 2 registers 3 2 2 2 2 1 1 1 1 1