CS453 Colorado State University ============================================== Functions and Function Calls ============================================== -> introduce PA5 ------------------------- Key Concepts for Today PA5 grammar: declaring classes, but only implementing functions Calling convention: implicit and explicit Storing off parameters Step through an example on the board and in MJSIM ------------------------- PA5 Grammar -> look at the PA5bluedot.java example Declaring classes to make Java happy. Really have a single scope with functions in it. - Corolary: you can't have a method with the same name in two different classes YET To make Java happy, need a receiver for any user-defined functions called in main. new Foo().myfunc() Since none of the classes will have member variables in PA5, you don't have to allocate object instances and therefore you can ignore "new Foo()". To make MeggyJava grammar happy, you need a receiver for ALL method calls. new Blah().myfunc() this.myfunc() ------------------------- Calling convention: implicit and explicit ---------- Explicit - pass parameters in registers - pass return value(s) in register(s) Arguments - allocated left to right, r25 to r8 r24, r25 parameter 1, only use r24 if just a byte parameter r22, r23 parameter 2 r20, r21 parameter 3 r18, r19 parameter 4 r16, r17 parameter 5 ... r8, r9 parameter 9 Return values 8-bit in r24 (not r25!), 16-bit in r25:r24, up to 32 bits in r22-r25, up to 64 bits in r18-r25. 8-bit return values are zero/sign-extended to 16 bits by the called function (unsigned char is more efficient than signed char - just clr r25). Arguments to functions with variable argument lists (printf etc.) are all passed on stack, and char is extended to int. ---------- Implicit - call and ret instructions push and pop the return address onto the stack -> step through the PA5bluedot.java.s example ------------------------- Storing off parameters Why do we need to store off the incoming parameters? - walk through PA5raindrop.java.s example (1) Could store into registers. Which ones? Who saves what registers caller saved r18-r27, r30-r31 callee saved r2-r17, r28-r29 This brings up a detail wrt the calling convention. How do we have to generate code differently for parameters 5 through 9 versus 1 through 4? store and restore: push and pop (2) Or we could store incoming parameters on the stack. - will eventually need to do this to some extent because we won't have enough registers for everything - need a way to get at the parameter value anywhere in the function and we could be using the run-time stack for expression evaluation - we can (a) allocate space on the stack, (b) set the frame pointer equal to the stack pointer, and then (c) access params at a positive offset from the frame pointer. fp, sp -> | | lo mem | param | | paramlo | | paramhi | | param | | | hi mem (a) allocate space on the stack Just keep pushing any value on the stack. ldi r30, 0 push r30 push r30 push r30 push r30 (b) set the frame pointer equal to the stack pointer Stack pointer, stored in special registers -> show push info in AVR manual # Copy stack pointer to frame pointer in r28,__SP_L__ in r29,__SP_H__ [The manual doesn't use __SP_L__, but the code generated by avr-gcc does.] (c) access params at a positive offset from the frame pointer # store param value in r24 to address fp+1 std Y+1, r24 # load value from address fp+1 into r2 ldd r2, Y + 3 Frame pointer r29:r28 hold the frame pointer and are accessed through Y --------------------------------- Updates to the Calling Convention Interface Explicit - pass parameters in registers - pass return value(s) in register(s) - If callee uses a callee-saved register then it must store and restore it. - If caller uses a caller-saved register, then it must store and restore it around function calls. Implicit - call and ret store and restore the return address Details that can vary per function, but recommend you come up with one way and stick with it. You could do specializations for performance. Order do we store and restore callee-saved registers we are using. Where does callee keep parameters. Where is the frame pointer placed. How is expression evaluation done within a function. --------------------------------- Big Example -> PA5raindrop.java.s -> draw on the board and step through with MJSIM ------------------------ mstrout@cs.colostate.edu, 3/24/11