{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

18-Code-Generation-Examples

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

Info icon This preview shows pages 1–4. 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:
Image of page 1

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

View Full Document Right 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 num M[SP] = 100 ; assign num constant value 100 SP = SP + 4 ; clean up storage for locals RET ; return, no value stored in RV since fn has no return
Image of page 2
3 Although the caller makes space for parameters, it does not allocate space for the locals.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern