Unformatted text preview:

Topic 3 a Calling Convention 01 14 19 1 Calling Convention Calling convention is a standardized method for a program to pass parameters to a procedure and receive result values back from it 01 14 19 course cpeg421 08s Topic 3 a ppt 2 Consideration of Call Convention Where to place parameters and return values in registers on the activation stack or a mix of both The order in which parameters are passed Responsibility for setting up and cleaning up a function call distributed between the calling and the called code Different platforms use different call conventions and so can different programming languages even on the same platform 01 14 19 course cpeg421 08s Topic 3 a ppt 3 Issues in Calling Convention Register usage convention Calling sequence Parameter passing Local data layout 01 14 19 course cpeg421 08s Topic 3 a ppt 4 Register Usage Convention Special registers stack register frame register return address register return value registers Reserved registers Temporary registers not preserved across a procedure call Saved Registers preserved across a procedure call 01 14 19 course cpeg421 08s Topic 3 a ppt 5 Context Switching Register Saving Register contents are saved before context switching into another procedure Caller save versus Callee save disciplines Contents saved in frame of saver Often choose to keep values in registers for efficiency 01 14 19 course cpeg421 08s Topic 3 a ppt 6 Register Saving Con t Caller save The calling procedure caller is responsible for saving and restoring any registers that must be preserved across the call The called procedure callee can then modify any register without constraint Callee save The callee is responsible for saving and restoring any registers that it might use The caller uses registers without worrying about restoring them after a call 01 14 19 course cpeg421 08s Topic 3 a ppt 7 Without Register Saving Assume register set saved registers temporary registers main subA can use saved registers temporary registers subA subB can t use saved registers but can use temporary registers subB can t use saved registers but can use temporary registers can t save any value in saved registers 01 14 19 course cpeg421 08s Topic 3 a ppt 8 Caller Save Callee Save Trade Of If all caller save Even callee doesn t kill any of the saved registers waste of cycles and memory resource If all callee save Callee has to save all the register which will be used by callee even caller doesn t use them 01 14 19 course cpeg421 08s Topic 3 a ppt 9 Caller Save Callee Save Trade Of caller save temporary registers callee save saved registers 01 14 19 course cpeg421 08s Topic 3 a ppt 10 Caller save example Function A Function B Add 10 11 12 Save 10 12 13 Add 2 4 5 Jal B Br 31 Restore 10 12 13 Sub 11 2 12 Mul 12 10 13 How to save 01 14 19 course cpeg421 08s Topic 3 a ppt 11 Callee Save example Function A Function B Add 10 11 12 Save 10 11 12 Jal B if they are used in B Add 11 2 12 Mul 12 10 13 Lw 10 4 sp Sub 11 8 9 Sub 12 11 10 Sub 2 12 11 Restore 10 11 12 Br 31 01 14 19 course cpeg421 08s Topic 3 a ppt 12 Callee Save example Function A Function B Add 10 11 12 Jal B Add 2 4 5 Sub 11 2 12 Br 31 Mul 12 10 13 No save 01 14 19 course cpeg421 08s Topic 3 a ppt 13 Calling Sequences A calling sequence is code statements to create activation records on the stack and enter data in them It consists of call sequence and return sequence 01 14 19 course cpeg421 08s Topic 3 a ppt 14 Calling Sequences Con t A call sequence allocates an activation record and enters information into its fields parameters return address old stack top saving registers local data etc A return sequence restores the state of the machine return value restore registers restore old stack top branch to return address The code in calling sequence is often divided between the caller and the callee 01 14 19 course cpeg421 08s Topic 3 a ppt 15 Calling Sequence Implementation Approach I More responsibilities are in caller side Call sequence Caller evaluates actual parameters Caller stores a return address and the old stack top into callee s AR 01 14 19 Caller pushes the AR i e increment top sp Callee saves register values and other status information Callee initializes local data and begin execution course cpeg421 08s Topic 3 a ppt 16 Calling Sequence Implementation Approach I Con t Return sequence Callee places a return value next to the AR of the caller Callee restores top sp and other registers Callee branches to the return address Caller can copy return value into its own AR 01 14 19 course cpeg421 08s Topic 3 a ppt 17 Calling Sequence Implementation Approach I Con t Actual parameters Return values Optional control link Optional access link Caller s AR Save machine status top sp Local Data Temporaries Caller put actual parameters Control link access link Caller s responsibility Return address top sp 01 14 19 course cpeg421 08s Topic 3 a ppt 18 Calling Sequence Implementation Approach I Con t Actual parameters Return values Optional control link Optional access link Caller s AR Save machine status Local Data Temporaries Caller put actual parameters Control link access link Callee s AR Return address top sp Local Data 01 14 19 Temporaries course cpeg421 08s Topic 3 a ppt Caller s responsibility top sp Callee s responsibility19 Calling Sequence Implementation Approach I Con t Actual parameters Return values Optional control link Optional access link Caller s AR Save machine status Local Data Temporaries Caller put actual parameters Return values Control link access link Callee s AR Return address top sp top sp Local Data 01 14 19 Temporaries course cpeg421 08s Topic 3 a ppt 20 Calling Sequence Implementation Approach II MIPS More responsibilities are in callee side Call sequence 1 Push onto the register save field for any temporary registers t0 t9 that contain values that must be saved The callee procedure might change these registers 2 Put first 4 argument values into a0 a3 and others into incoming argument field 3 Call the subroutine 01 14 19 course cpeg421 08s Topic 3 a ppt 21 Calling Sequence Implementation Approach II MIPS In callee Prolog done by the callee at its beginning 1 If this procedure might call other procedures save return address onto the stack 2 Push onto the stack any saved registers s0 s7 that this procedure might alter Callee Body 1 The procedure may alter any register that it saved in the prolog 2 If the procedure calls another procedure then it does so by following above rules 01 14 19


View Full Document

UD CPEG 421 - Calling Convention

Loading Unlocking...
Login

Join to view Calling Convention 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 Calling Convention 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?