DOC PREVIEW
CSU CS 453 - LECTURE NOTES

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

CS453 Intro and PA1 1CS453 Lecture Offsets and Frame Class 1Plan for Today General stack frame concept– agreement amongst programmers, procedure call convention Stack frame the MiniJava compiler will generate– Need to match the Wisconsin C-- compiler to implement garbage collection Building the symbol table– determining the memory locations for each local and class member variable– the Frame class– specifics for inMethodDecl Suggested Exercise for figuring out stack frame information in x86 codeCS453 Lecture Offsets and Frame Class 2Structure of the MiniJava Compiler“sentences”SynthesisoptimizationAssem (MIPS)IR code generationAssem (MIPS)Analysischaracter streamlexical analysis“words”tokenssemantic analysissyntactic analysisASTAST and symbol tablecode genMIPSPA3PA4PA5PA6553CS453 Lecture Offsets and Frame Class 3Questions a calling convention must answer Contract between caller and callee– Where is the return value?– Where is the stack pointer pointing upon entry to a function?– Where are the parameters?– Is the caller or callee responsible for popping the parameters?– Does the stack pointer point at the top of the stack or the next empty slot? Decisions needed for manipulating a frame/activation record– Layout of callee-saved registers, caller-saved registers, locals, and temps– Are parameters pushed by moving the stack pointer or is enough space setaside initially?CS453 Lecture Offsets and Frame Class 4Mapping 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 Intro and PA1 2CS453 Lecture Offsets and Frame Class 5Wisconsin 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 PA2 (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 Lecture Offsets and Frame Class 6Another 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; }}CS453 Lecture Offsets and Frame Class 7Determining 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 variableCS453 Lecture Offsets and Frame Class 8Interface to Frame Three main responsibilities– provide a factory interface for generating machine-specific frames– Frame newFrame(Label name, List<Boolean> formals)– answer queries that are machine-specific, but not method specific– int wordSize()– Temp FP(), coming to an interface near you in PA7– store method-specific information about frame layout– Label name– List<Access> formals– Access allocLocal(boolean escape)CS453 Intro and PA1 3CS453 Lecture Offsets and Frame Class 9When do parameters and/or locals escape? Nesting of classes and methods When the variable may have its address taken When the language uses pass-by-referenceint foo(int x){ int baz(int y) { return x+y; } int bar() { return baz(2); } return bar();}int main() { printf(“%f\n”, foo(3));}int addressFunc(int x){ int z; return blah(&x, &z);}FORTRANsubroutinefoo(x,y)double precision xdouble precision yy=x*2end subroutinesubroutine head()double precision a, bcall foo(a,b)end subroutineCS453 Lecture Offsets and Frame Class 10Frame class implements Factory design patternFrameFrame newFrame()MipsFrameFrame newFrame()MipsFrameCS453 Lecture Offsets and Frame Class 11What, why, and where? What?– one instance of the MipsFrame will generate other instances Why?– need a Frame instance for each function and want to avoid calling theMipsFrame constructor everywhere Where?// FrameLayout.java! Frame.Frame frame = new Mips.MipsFrame();! BuildSymTable buildSTvisitor = new BuildSymTable(linesToNodes, frame);! // BuildSymTable::inAMethodDecl...Frame.Frame methodFrame = mFrame.newFrame(! new Temp.Label(mCurrentClass.getName()+”_"+node.getName().getText()), formalEscapeList);CS453 Lecture Offsets and Frame Class 12inMethodDecl for BuildSymTable visitor Steps needed in the inMethodDecl– does the method name conflict– create a formal escape list– add an entry into the formal escape list for the implicit “this” parameter– create a list of types for explicit formals– for each explicit parameter add entry to formal escape list– create method Signature( return type, formal types list)– create Frame with newFrame– create MethodSTE and insert it into current ST– push method


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?