1 / 32

Code Generation

Code Generation. Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html. Chapter 4.2. Basic Compiler Phases. Code generation issues. Code selection Register allocation Instruction ordering. Simplifications. Consider small parts of AST at time One expression at the time

Download Presentation

Code Generation

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. Code Generation Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html Chapter 4.2

  2. Basic Compiler Phases

  3. Code generation issues • Code selection • Register allocation • Instruction ordering

  4. Simplifications • Consider small parts of AST at time • One expression at the time • Target machine simplifications • Ignore certain instructions • Use simplifying conventions

  5. Overall Structure

  6. Outline • Partial evaluation in the nutshell • Simple code generation for expressions (4.2.4) • Pure stack machine • Pure register machine • Code generation of basic blocks (4.2.5) • Automatic generation of code generators (4.2.6) • Register Allocation by Graph Coloring (4.2.7) Next week

  7. Partial Evaluator Program’ Program Input 1 Input 2 Partial Evaluation • Partially interpret static parts in a program • Generates an equivalent program

  8. Example int pow4(int n) { return n * n * n *n; } int pow(int n, int e) { if (e==0) return 1; else return n * pow(n, e-1); } e=4

  9. Example2 Bool match(string, regexp) { switch(regexp) { …. } } regexp=a b*

  10. Partial Evaluation Generalizes Compilation Partial Evaluator Program Interpreter AST Program Input

  11. But ….

  12. Simple Code Generation • Fixed translation for each node type • Translates one expression at the time • Local decisions only • Works well for simple machine model • Stack machines (PDP 11, VAX) • Register machines (IBM 360/370) • Can be applied to modern machines

  13. Simple Stack Machine SP Stack BP

  14. Stack Machine Instructions

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

  16. Simple Stack Machine Push_Local #p Push_Const 5 Add_Top2 Store_Local #p SP BP+5 7 BP

  17. Simple Stack Machine Push_Local #p Push_Const 5 Add_Top2 Store_Local #p SP 7 BP+5 7 BP

  18. Simple Stack Machine SP 5 Push_Local #p Push_Const 5 Add_Top2 Store_Local #p 7 BP+5 7 BP

  19. Simple Stack Machine Push_Local #p Push_Const 5 Add_Top2 Store_Local #p SP 12 BP+5 7 BP

  20. Simple Stack Machine Push_Local #p Push_Const 5 Add_Top2 Store_Local #p SP BP+5 12 BP

  21. Register Machine • Fixed set of registers • Load and store from/to memory • Arithmetic operations on register only

  22. Register Machine Instructions

  23. Example Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, P p := p + 5

  24. Simple Register Machine Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, P R1 R2 x770 7 memory

  25. Simple Register Machine 7 Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, P R1 R2 x770 7 memory

  26. Simple Register Machine 7 5 Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, P R1 R2 x770 7 memory

  27. Simple Register Machine 12 5 Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, P R1 R2 x770 7 memory

  28. Simple Register Machine 12 5 Load_Mem p, R1 Load_Const 5, R2 Add_Reg R2, R1 Store_Reg R1, P R1 R2 x770 12 memory

  29. Simple Code Generation for Stack Machine • Tree rewritings • Bottom up AST traversal

  30. Abstract Syntax Trees for Stack Machine Instructions

  31. Example Subt_Top2 - Mult_Top2 Mult_Top2 * * Mult_Top2 Push_Constant 4 Push_Local #b Push_Local #b b b 4 * a c Push_Local #c Push_Local #a

  32. Bottom-Up Code Generation

More Related