1 / 120

Compilers

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.

jspinks
Télécharger la présentation

Compilers

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. 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)

  2. Overview

  3. 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

  4. 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

  5. Recursive Interpretation

  6. 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:

  7. Complex number representation

  8. IF condition THEN ELSE FI Iterative interpretation • Operates on threaded AST • Active node pointer • Flat loop over a case statement

  9. Sketch of the main loop

  10. Example for demo compiler

  11. 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

  12. Example of code generation a:=(b[4*c+d]*2)+9;

  13. 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

  14. Two sample instructions with their ASTs

  15. 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

  16. Object code sequence Load_Byte (b+Rd)[Rc], 4, Rt Load_Addr 9[Rt], 2, Ra

  17. Trivial code generation

  18. Code for (7*(1+5))

  19. Partial evaluation

  20. New Code

  21. 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

  22. Pure stack machine • Instructions

  23. Example of p:=p+5 Push_Local #p Push_Const 5 Add_Top2 Store_Local #p

  24. Pure register machine • Instructions

  25. Example of p:=p+5 Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, p

  26. Simple code generation for a stack machine • The AST for b*b – 4 *(a*c)

  27. The ASTs for the stack machine instructions

  28. The AST for b*b - 4*(a*c) rewritten

  29. Simple code generationfor a stack machine (demo) • example: b*b – 4*a*c • threaded AST - * * b b 4 * a c

  30. Simple code generationfor a stack machine (demo) • example: b*b – 4*a*c • threaded AST Sub_Top2 - Mul_Top2 Mul_Top2 * * Push_Local #b b Push_Local #b b Push_Const 4 4 Mul_Top2 * Push_Local #a a Push_Local #c c

  31. Sub_Top2 - Mul_Top2 Mul_Top2 * * Push_Local #b b Push_Local #b b Push_Const 4 4 Mul_Top2 * Push_Local #a a Push_Local #c c Simple code generationfor a stack machine (demo) Push_Local #b Push_Local #b Mul_Top2 Push_Const 4 Push_Local #a Push_Local #c Mul_Top2 Mul_Top2 Sub_Top2 • example: b*b – 4*a*c • rewritten AST

  32. Depth-first code generation

  33. Stack configurations

  34. Simple code generation for a register machine • The ASTs for the register machine instructions

  35. Code generation with register allocation

  36. Code generation with register numbering

  37. Register machine code for b*b - 4*(a*c)

  38. Register contents

  39. 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

  40. Register weight of a node

  41. AST for b*b-4*(a*c) with register weights

  42. Weighted register machine code

  43. Example Parameter number N 2 3 1 Stored weight 4 2 1 Registers occupied when 0 1 2 starting parameter N Maximum per parameter 4 3 3 Overall maximum 4

  44. Example: Tree representation

  45. - * * 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

  46. - 3 2 T1 * * 2 2 1 b b 4 * 2 1 1 1 a c 1 1 Register spilling Load_Mem b, R1 Load_Mem b, R2 Mul_Reg R2, R1 Store_Mem R1, T1 Load_Mem a, R1 Load_Mem c, R2 Mul_Reg R2, R1 Load_Const 4, R2 Mul_Reg R1, R2 Load_Mem T1, R1 Sub_Reg R2, R1

  47. - * * T1 1 b b 4 * a c Another example 3 2 2 2 2 1 1 1

  48. Algorithm

  49. Machines with register-memory operations • An instruction: • Add_Mem X, R1 • Adding the contents of memory location X to R1

  50. Register-weighted tree for a memory-register machine

More Related