1 / 58

Code Generation

No variables or registersStack storing values for intermediate results. Stack Machines. PopPopAddPush. Addition in Stack Machine. Instructionspush iaddCompute 5 3push 5push 3add. Addition in Stack Machine. Example: Addition. push 3push 5add2 reads1 write. add 3 5. Top of the s

xenia
Télécharger la présentation

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

    2. No variables or registers Stack storing values for intermediate results Stack Machines

    3. Pop Pop Add Push Addition in Stack Machine

    4. Instructions push i add Compute 5+3 push 5 push 3 add Addition in Stack Machine

    5. Example: Addition push 3 push 5 add 2 reads 1 write add 3 5

    6. Top of the stack is accessed frequently. Idea: Keep the top of the stack in the register (accumulator) Hence, the addition becomes acc <- acc + top_of_stack Improve the Performance?

    7. 5 + 3 acc <- 5 push acc acc <- 3 acc <- acc+ top_of_stack Stack Machine with Accumulator

    8. Example: 2 + (5 + 4)

    9. Simulate stack machine instructions using MIPS instructions and registers Stack Machines to MIPS

    10. Accumulator is kept in MIPS register $a0. Stack is kept in the memory. Stack grows towards lower (smaller) addresses. The address of the next location on the stack is kept in MIPS register $sp. The top of the stack $sp + 4 The Basics

    11. RISC architecture Arithmetic operations use registers for operands and results Load instructions are used to read operands Store instructions are used to store results in memory 32 general purpose registers (32 bits each) MIPS Assembly

    12. Examples of MIPS Instructions (1)

    13. P -> D; P | D D -> def id(ARGS) = E; ARGS -> id, ARGS | id E -> int | id | if E1 = E2 then E3 else E4 | E1 + E2 | E1 - E2 | id(E1, , En) A Simple Language

    14. For each expression expr, we generate MIPS code that Computes the value of expr in $a0 Preserves $sp and the contents of the stack We do so by Defining a code generation function cgen(expr) that returns the generated code for expr. In other words, cgen(expr) will generate the MIPS code for expr. Since Cool is an expression language (i.e., every construct in cool is an expression), we could adopt the same concept. More on this issue later. Code Generation: Expression

    15. cgen(expr) The value of expr is stored in $a0 $sp and the stack contents are preserved. It is very important that the stack is preserved across the evaluation of a sub-expression. Code Generation: Expression

    16. For each constant i, we put its value into the accumulator. cgen(i ) = li $a0 i Note that this preserves the stack. Code Generation: Constant

    17. Stack machine code generation is recursive. For instance, Generating the code for expr1 + expr2 requires The code for expr1 The code for expr2 Notes on Code Generation

    18. cgen(expr1 + expr2) = cgen(expr1) sw $a0 0($sp) addiu $sp $sp-4 cgen(expr2) lw $t1 4($sp) add $a0 $t1 $a0 addiu $sp $sp 4 Code Generation: Addition

    19. Example: 5 + 3 cgen(expr1 + expr2) cgen(5+3) cgen(expr1) sw $a0 0($sp) addiu $sp $sp-4 cgen(expr2) lw $t1 4($sp) add $a0 $t1 $a0 addiu $sp $sp 4 li $a0 5 sw $a0 0($sp) addiu $sp $sp-4 li $a0 3 lw $t1 4($sp) add $a0 $t1 $a0 addiu $sp $sp 4

    20. Code Generated for 5 + 3

More Related