DOC PREVIEW
CSU CS 453 - LECTURE NOTES

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS453 Intro and PA1 1CS453 Lecture C-- Stack Frame Layout 1Plan for Learning about Stack Frames Typical C stack frame– gcc calling convention described in A.5 and A.6 in appendix on assemblycode and MIPS Stack frame the MiniJava compiler will generate– Need to match the Wisconsin C-- compiler to implement garbage collection General stack frame concept– agreement amongst programmers, procedure call convention– what about gcc for x86?– handling nested procedures– handling first class functionsCS453 Lecture C-- Stack Frame Layout 2Structure of the MiniJava Compiler“sentences”Synthesisinstruction selectionIR code generationTreeAnalysischaracter streamlexical analysis“words”tokenssemantic analysissyntactic analysisASTAST and symbol tablecode generationMIPSPA2PA3 and PA4PA5 and PA6PA7PA8PA8Assem/MIPSoptimizationCS453 Lecture C-- Stack Frame Layout 3Mapping out the stack frame for the funcCall1 exampleint foo(int x,int y,int *z) { int a; a = x * y - *z; return a;}void main() { int x; x = 2; cout << foo(4,5,&x); cout << "\n";} .text_foo: sw $ra, 0($sp) #PUSH subu $sp, $sp, 4 sw $fp, 0($sp) #PUSH subu $sp, $sp, 4 addu $fp, $sp, 20 subu $sp, $fp, 24 ... lw $t0, -20($fp) move $v0, $t0 lw $ra, -12($fp) move $t0, $fp lw $fp, -16($fp) move $sp, $t0 jr $ra .text .globl mainmain: sw $ra, 0($sp) #PUSH subu $sp, $sp, 4 sw $fp, 0($sp) #PUSH subu $sp, $sp, 4 addu $fp, $sp, 8 subu $sp, $fp, 12 li $t0, 2 sw $t0, -8($fp) li $t0, 4 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 li $t0, 5 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 subu $t0, $fp, 8 sw $t0, 0($sp) #PUSH subu $sp, $sp, 4 jal _foo move $a0, $v0 ... lw $ra, 0($fp) move $t0, $fp lw $fp, -4($fp) move $sp, $t0 jr $raCS453 Lecture C-- Stack Frame Layout 4Wisconsin C-- calling convention Calling convention (contract between caller and callee)– $sp must be divisible by 4– caller should pass parameters in order on the stack– upon callee entry, the stack pointer $sp should be pointing at the firstempty slot past the last parameter– upon callee exit, the stack pointer $sp should be pointing at the firstparameter– upon callee exit, return value should be in $v0 Rules to follow for PA6 (to standardize frame usage)– $sp should always be pointing at next empty slot on the stack– $ra and $fp should be stored right after the parameters on stack, you can’tuse any other callee-saved registers– $fp should be made to point at the first parameter, so that the address forthe first parameter is $fp-0, the address for the second parameter is $fp-4,...– locals should be stored in order, right after $ra and $fpCS453 Intro and PA1 2CS453 Lecture C-- Stack Frame Layout 5Another example: where does each variable go?class A { public static void main(String[] a){System.out.println(42); }}class B { int [] x; boolean mBool; public int foo(boolean p1, int p2, B b, int [] y) { boolean v1; int i; int j; return 0; } public B bar() { B b; b = new B; return b; } public boolean baz() { return mBool; }}CS453 Lecture C-- Stack Frame Layout 6Determining locations for vars Local vars– maintain counter for method that is initialized to 0– store counter in a temporary variable– decrement current counter by size of the local variable– return the value in the temporary variable Class members– maintain counter for method that is initialized to 0– store counter in a temporary variable– increment current counter by size of the local variable– return the value in the temporary


View Full Document

CSU CS 453 - LECTURE NOTES

Download LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?