18-Code-Generation-Examples - CS107 Spring 2007 Handout 18...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 18 Spring 2007 April 25, 2007 Computer Architecture: Take IV Examples by Jerry, Nick, and Julie. A simple function The simple Add function takes two integers as parameters, computes their sum in a local variable, and then returns that value. static int Add(int one, int two) { int temp, sum; sum = one + two; return sum; } The activation record is 20 bytes total: 8 bytes for the parameter block, 4 bytes for the return address, and 8 bytes for the local variables. Add AR (total size 20 bytes) type size offset two int 4 16 one int 4 12 Saved PC address 4 8 temp int 4 4 SP -> sum int 4 0 Doing the most straightforward translation (no concern for optimization), the generated code for the body of Add will look like this: SP = SP - 8 ; make space for local variables R1 = M[SP + 12] ; load value of parameter one into R1 R2 = M[SP + 16] ; load value of parameter two into R2 R3 = R1 + R2 ; do addition M[SP] = R3 ; store result in local variable sum RV = M[SP] ; copy sum's value into RV register (return value) SP = SP + 8 ; clean up space used for local variables RET ; return to caller, pick up at saved address You could change the C code to not bother with the local variables ( temp isn't even used, and sum isn't necessary), but in fact, a smart optimizing compiler can already recognize they aren't needed and remove them for you. When a value is used only temporarily, it is likely to exist only in a register and never be written to the stack at all:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 ; eliminate all local variables, no change to SP R1 = M[SP + 4] ; load value of parameter one into R1 R2 = M[SP + 8] ; load value of parameter two into R2 RV = R1 + R2 ; compute sum directly into RV register RET ; no need to clean up locals, there aren't any! The Calling Function What kind of code is generated to make a function call? The caller has responsibility for making space for the parameters and assigning their values, along with saving the return state and transferring control. The function Caller has no parameters, one local integer, and calls the Add function from the previous example: static void Caller(void) { int num = 10; num = Add(num, 45); num = 100; } Caller AR (total size 8 bytes) type size offset Saved PC addres s 4 4 SP -> num int 4 0 Here is the code generated for the Caller function, note how it handles the call to Add . SP = SP - 4 ; make space for local variable num M[SP] = 10 ; assign local variable num constant value 10 R1 = M[SP] ; load up the value of num (before we change SP ; so we don't have to deal with changed offsets) SP = SP - 8 ; push space for parameter block of Add 's AR M[SP + 4] = 45 ; initialize parameters in the activation record M[SP] = R1 CALL <Add> ; the CALL instruction makes space on the stack ; for the return address, saves the PC value there ; and then assigns the PC to the address of the first ; instruction of the Add fn (which transfers control) SP = SP + 8 ; when control returns here, pop the params off stack M[SP] = RV ; read return value from RV and store in
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/14/2010 for the course CS 107 taught by Professor Cain,g during the Spring '08 term at Stanford.

Page1 / 12

18-Code-Generation-Examples - CS107 Spring 2007 Handout 18...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online