DOC PREVIEW
Berkeley COMPSCI 164 - Lecture 22: Registers, Functions, Parameter

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:

Lecture 22: Registers, Functions, ParametersThree-Address Code to ia32A Simple Strategy: Local Register AllocationSimple Algorithm for Local Register AllocationFunction Prologue and Epilogue for the ia32Code Generation for Local VariablesPassing Static Links (I)Accessing Non-Local VariablesPassing Static Links to Known FunctionsPassing Static Links to Known Functions: ExampleA Note on PushingParameter Passing SemanticsParameter Passing Semantics: Call by NameCall By Name: Jensen's DeviceCall By Name: ImplementationLecture 22: Registers, Functions, ParametersAdministrivia• Test on Tuesday.• Review session on Sunday at 5, place TBA.• Project #3 should be up Friday night.Last modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 1Three-Address Code to ia32• The problem is that in reality, the ia32 architecture has very fewregisters, and example from last lecture used registers profligately.•Register allocationis the general term for assigning virtual regis-ters to real registers or memory locations.• When we run out of real registers, wespillvalues into memory loca-tions reserved for them.• We keep a register or two around ascompiler temporariesfor caseswhere the instruction set doesn’t let us just combine operands di-rectly.Last modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 2A Simple Strategy: Local Register Allocation• It’s convenient to handle register allocation withinbasic blocks—sequences of code with one entry point at the top and (at most) onebranch at the end.• At the end of each such block, spill any registers needed.• To do this efficiently, need to know when a register isdead—thatis, when its value is no longer needed.• We’ll talk about how to compute that in a later lecture. Let’s assumewe know it for now.• Let’s also assume that each virtual register representing a localvariable or intermediate result has a memory location suitable forspilling.Last modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 3Simple Algorithm for Local Register Allocation• We execute the following for each three-address instruction in abasic block (in turn).• Initially, the set availReg contains all usable physical registers.# Allocate registers to an instruction x := y op z# [Adopted from Aho, Sethi, Ullman]regAlloc(x := y op z):if x has an assigned register already or dies here:returnif y is a virtual register and dies here:reassign y’s physical register to xelif availReg is not empty:remove a register from availReg and assign to xelif op requires a register:spill another virtual register (which could be y or z),and reassign its physical register to xelse:just leave x in memoryLast modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 4Function Prologue and Epilogue for the ia32• Consider a function that needs K bytes of local variables and othercompiler temporary storage for expression evaluation.• We’ll consider the case where we keep a frame pointer.• Overall, the code for a function, F , looks like this:F :pushl %ebp # Save dynamic link (caller’s frame pointer)movl %esp,%ebp # Set new frame pointersubl K,%esp # Reserve space for localscode for body of function, leaving value in%eaxleave # Sets %ebp to 0(%ebp), popping old frame pointerret # Pop return address and returnLast modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 5Code Generation for Local Variables• Local variables are stored on the stack (thus not at fixed location).• One possibility: access relative to the stack pointer, but– Sometimes convenient for stack pointer to change during execu-tion of of function, sometimes by unknown amounts.– Debuggers, unwinders, and stack tracers would like simple way tocompute stack-frame boundaries.• Solution: use frame pointer, which is constant over execution offunction.• For simple language, use fact that parameter i is at locationframe pointer + K1(i + K2). If parameters are 32-bit integers (orpointers) on the ia32, K1= 4 and K2= 2 [why?].• Local variables other than parameters are at negative offsets fromthe frame pointer on the ia32.Last modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 6Passing Static Links (I)• When using static links, the link can be treated as a parameter.• As we’ve seen, a function value consists of a code address and astatic link (let’s assume code address comes first).• So, if we translate a functiondef caller(f):f(42)so that function parameter f is at offset 8 from the frame pointer,then the call f(42) gets translated topushl $42pushl 12(%ebp) # Push static linkmovl 8(%ebp),%eax # Get code addresscall *%eax # GNU assembler for call to address in eaxLast modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 7Accessing Non-Local Variables• In program on left, how does f3 access x1?• f3 will have been passed a static link as its first parameter.• The static link passed to f3 will be f2’s frame pointerdef f1 (x1):def f2 (x2):def f3 (x3):... x1 ......f3 (12)...f2 (9)movl 8(%ebp),%ebx # Fetch FP for f2movl 8(%ebx),%ebx # Fetch FP for f1movl 12(%ebx),%eax # Fetch x1• In general, for a function at nesting level n to access a variable atnesting level m < n, perform n − m loads of static links.Last modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 8Passing Static Links to Known Functions• For a call F (. . .) to a fixed, known function F , we could use the samestrategy:– Create a closure for F containing address of F ’s code and valueof its static link.– Call F using the same code sequence as on previous slide.• But can do better. Functions and their nesting levels are known.• Inside a function at nesting level n, to call another at known nestinglevel m ≤ n + 1, get correct static link in register R with:– movl– Do ‘movl 8(R),R’ n − m + 1 times.• When calling outer-level functions, it doesn’t matter what you useas the static link.Last modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 9Passing Static Links to Known Functions: Exampledef f1 (x1):def f2 (x2):def f3 (x3):... f2 (9) ......f3 (12)f2 (10) # (recursively)...# To call f2(9) (in f3):pushl $9movl 8(%ebp),%ebx # Fetch FP for f2movl 8(%ebx),%ebx # Fetch FP for f1pushl %ebx # Push static linkcall f2addl $8,%esp# To call f3(12) (in f2):pushl $12pushl %ebp # f2’s FP is static linkcall f3addl $8,%ebp# To call f2(10) (in f2):pushl $10pushl 8(%ebp) # Pass down same static linkcall f2addl $8,%ebpLast modified: Thu Apr 9 14:06:10 2009 CS164: Lecture #22 10A Note on Pushing• Don’t really


View Full Document

Berkeley COMPSCI 164 - Lecture 22: Registers, Functions, Parameter

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture 22: Registers, Functions, Parameter
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 22: Registers, Functions, Parameter 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 22: Registers, Functions, Parameter 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?