compiler development cmpsc 401
play

Compiler Development (CMPSC 401) Three Address Code Janyl - PowerPoint PPT Presentation

Compiler Development (CMPSC 401) Three Address Code Janyl Jumadinova April 2, 2019 Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 1 / 30 Three Address Code (TAC) High-level assembly where each operation has at most three


  1. Compiler Development (CMPSC 401) Three Address Code Janyl Jumadinova April 2, 2019 Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 1 / 30

  2. Three Address Code (TAC) High-level assembly where each operation has at most three operands (e.g., an add instruction has three operands: two for each argument and one for the result). Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 2 / 30

  3. Three Address Code (TAC) High-level assembly where each operation has at most three operands (e.g., an add instruction has three operands: two for each argument and one for the result). The operands are called addresses and can be held in a virtual register . The compiler back-end will convert TAC instructions and registers into specific machine instructions and hardware registers. Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 2 / 30

  4. Temporary Variables The “three” in “three-address code” refers to the number of operands in any instruction. Evaluating an expression with more than three subexpressions requires the introduction of temporary variables. Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 3 / 30

  5. TAC Sample Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 4 / 30

  6. TAC Sample Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 5 / 30

  7. TAC Sample Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 6 / 30

  8. TAC Sample Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 7 / 30

  9. TAC Sample Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 8 / 30

  10. TAC Sample Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 9 / 30

  11. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  12. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  13. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; var 1 = var 2 op var 3 ; Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  14. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; var 1 = var 2 op var 3 ; var 1 = constant op var 2 ; Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  15. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; var 1 = var 2 op var 3 ; var 1 = constant op var 2 ; var 1 = var 2 op constant; Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  16. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; var 1 = var 2 op var 3 ; var 1 = constant op var 2 ; var 1 = var 2 op constant; var = constant 1 op constant 2 ; Permitted operators are + , - , * , / , % . Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  17. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; var 1 = var 2 op var 3 ; var 1 = constant op var 2 ; var 1 = var 2 op constant; var = constant 1 op constant 2 ; Permitted operators are + , - , * , / , % . How would you compile y = -x; ? Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  18. Simple TAC Instructions Variable assignment allows assignments of the form : var = constant ; var 1 = var 2 ; var 1 = var 2 op var 3 ; var 1 = constant op var 2 ; var 1 = var 2 op constant; var = constant 1 op constant 2 ; Permitted operators are + , - , * , / , % . How would you compile y = -x; ? y = 0 - x; y = -1 * x; Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 10 / 30

  19. TAC with bool s Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 11 / 30

  20. TAC with bool s Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 12 / 30

  21. TAC with bool s Boolean variables are represented as integers that have zero or nonzero values. In addition to the arithmetic operator, TAC supports: < , ==, || , and &&. Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 13 / 30

  22. TAC with bool s Boolean variables are represented as integers that have zero or nonzero values. In addition to the arithmetic operator, TAC supports: < , ==, || , and &&. How might you compile b = ( x ≤ y )? Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 13 / 30

  23. Control Flow Statements Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 14 / 30

  24. Control Flow Statements Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 15 / 30

  25. Labels TAC allows for named labels indicating particular points in the code that can be jumped to. There are two control flow instructions: Goto label; 1 IfZ value Goto label; 2 Note that IfZ is always paired with Goto . Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 16 / 30

  26. Control Flow Statements Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 17 / 30

  27. Control Flow Statements Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 18 / 30

  28. Compiling Functions Decaf functions consist of four pieces: 1 A label identifying the start of the function. 2 A BeginFunc N; instruction reserving N bytes of space for locals and temporaries. 3 The body of the function. 4 An EndFunc; instruction marking the end of the function. – When reached, cleans up stack frame and returns. Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 19 / 30

  29. A Complete Decaf Program Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 20 / 30

  30. A Logical Decaf Stack Frame Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 21 / 30

  31. A Logical Decaf Stack Frame Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 22 / 30

  32. Compiling Function Calls Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 23 / 30

  33. Compiling Function Calls Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 24 / 30

  34. Stack Management in TAC The BeginFunc N; instruction only needs to reserve room for local variables and temporaries. The EndFunc; instruction reclaims the room allocated with BeginFunc N; A single parameter is pushed onto the stack by the caller using the PushParam var instruction. Space for parameters is reclaimed by the caller using the PopParams N; instruction. N is measured in bytes, not number of arguments. Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 25 / 30

  35. Compiling Function Calls Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 26 / 30

  36. Compiling Function Calls Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 27 / 30

  37. Compiling Function Calls Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 28 / 30

  38. Compiling Function Calls Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 29 / 30

  39. Instruction Formats Janyl Jumadinova Compiler Development (CMPSC 401) April 2, 2019 30 / 30

Recommend


More recommend