
Calling Conventions Hakim Weatherspoon CS 3410 Computer Science Cornell University [Weatherspoon, Bala, Bracy, McKee and Sirer] Big Picture: Where are we going? compute jump/branch targets A memory register alu D D file B +4 addr PC control B d d in out M inst memory extend new Forward unit pc Detect hazard imm Instruction Instruction Execute Write- ctrl ctrl Fetch Decode ctrl Memory Back IF/ID ID/EX EX/MEM MEM/WB 2 Big Picture: Where are we going? C int x = 10; compiler x = 2 * x + 15; x0 = 0 x5 = x0 + 10 RISC‐V addi x5, x0, 10 muli x5, x5, 2 x5 = x5<<1 #x5 = x5 * 2 assembly addi x5, x5, 15 x5 = x15 + 15 assembler 10 r0 r5 op = addi 00000000101000000000001010010011 machine 00000000001000101000001010000000 code 00000000111100101000001010010011 15 r5 r5 op = addi CPU op = r-type x5 shamt=1 x5 func=sll Circuits Gates Transistors Silicon 3 Big Picture: Where are we going? CPU Main Memory (DRAM) SandyBridge Motherboard, 2011 4 http://news.softpedia.com Goals for this week Calling Convention for Procedure Calls Enable code to be reused by allowing code snippets to be invoked Will need a way to • call the routine (i.e. transfer control to procedure) • pass arguments - fixed length, variable length, recursively • return to the caller - Putting results in a place where caller can find them • Manage register 5 Calling Convention for Procedure Calls Transfer Control • Caller Routine • Routine Caller Pass Arguments to and from the routine • fixed length, variable length, recursively • Get return value back to the caller Manage Registers • Allow each routine to use registers • Prevent routines from clobbering each others’ data What is a Convention? Warning: There is no one true RISC-V calling convention. lecture != book != gcc != spim != web 6 Cheat Sheet and Mental Model for Today compute jump/branch targets A memory register alu D D file B +4 addr PC control B d d in out M inst memory extend new Forward unit pc Detect hazard imm Instruction Instruction Execute Write- ctrl ctrl Fetch Decode ctrl Memory Back IF/ID ID/EX EX/MEM MEM/WB How do we share registers and use memory when making procedure calls? 7 Cheat Sheet and Mental Model for Today • first eight arg words passed in a0, a1, … , a7 • remaining arg words passed in parent’s stack frame • return value (if any) in a0, a1 • stack frame at sp - contains ra (clobbered on JAL fp to sub-functions) incoming - contains local vars (possibly args clobbered by sub-functions) saved ra - contains space for incoming args saved fp • callee save regs are preserved saved regs • caller save regs are not (s1 ... s11) • Global data accessed via $gp locals sp 8 RISC-V Register • Return address: x1 (ra) • Stack pointer: x2 (sp) • Frame pointer: x8 (fp/s0) • First eight arguments: x10-x17 (a0-a7) • Return result: x10-x11 (a0-a1) • Callee-save free regs: x18-x27 (s2-s11) • Caller-save free regs: x5-x7,x28-x31 (t0-t6) • Global pointer: x3 (gp) • Thread pointer: x4 (tp) 9 RISC-V Register Conventions x0 zero zero x15 a5 function x16 a6 x1 ra return address arguments x2 sp stack pointer x17 a7 x3 gp global data pointer x18 s2 x4 tp thread pointer x19 s3 x5 t0 x20 s4 temps x6 t1 x21 s5 (caller save) x7 t2 x22 s6 saved x8 s0/fp frame pointer x23 s7 (callee save) saved x24 s7 x9 s1 (callee save) x25 s9 x10 a0 function args or x26 s10 x11 a1 return values x27 s11 x12 a2 x28 t3 function x13 a3 x29 t4 temps arguments x14 a4 x30 t5 (caller save) 10 x31 t6 Calling Convention for Procedure Calls Transfer Control • Caller Routine • Routine Caller Pass Arguments to and from the routine • fixed length, variable length, recursively • Get return value back to the caller Manage Registers • Allow each routine to use registers • Prevent routines from clobbering each others’ data What is a Convention? Warning: There is no one true RISC-V calling convention. lecture != book != gcc != spim != web 11 How does a function call work? int main (int argc, char* argv[ ]) { int n = 9; int result = myfn(n); } int myfn(int n) { int f = 1; int i = 1; int j = n –1; while(j >= 0) { f *= i; i++; j = n ‐ i; } return f; } 12 Jumps are not enough 1 main: myfn: j myfn … after1: 2 add x1,x2,x3 … j after1 Jumps to the callee Jumps back 13 Jumps are not enough 1 main: myfn: j myfn 3 … after1: 2 add x1,x2,x3 … j myfn j after1 j after2 after2: 4 ??? Change target sub x3,x4,x5 on the fly ??? Jumps to the callee Jumps back What about multiple sites? 14 Takeaway1: Need Jump And Link JAL (Jump And Link) instruction moves a new value into the PC, and simultaneously saves the old value in register x1 (aka $ra or return address) Thus, can get back from the subroutine to the instruction immediately following the jump by transferring control back to PC in register x1 15 Jump-and-Link / Jump Register First call x1 after1 1 main: myfn: jal myfn … after1: 2 add x1,x2,x3 … jal myfn jr x1 after2: sub x3,x4,x5 JAL saves the PC in register $31 Subroutine returns by jumping to $31 16 Jump-and-Link / Jump Register Second call x1 after1after2 1 main: myfn: jal myfn 3 … after1: 2 add x1,x2,x3 … jal myfn jr x1 after2: 4 sub x3,x4,x5 JAL saves the PC in register x1 Subroutine returns by jumping to x1 What happens for recursive invocations? 17 JAL / JR for Recursion? int main (int argc, char* argv[ ]) { int n = 9; int result = myfn(n); } int myfn(int n) { int f = 1; int i = 1; int j = n –1; while(j >= 0) { f *= i; i++; j = n ‐ i; } return f; } 18 JAL / JR for Recursion? int main (int argc, char* argv[ ]) { int n = 9; int result = myfn(n); } int myfn(int n) { if(n > 0) { return n * myfn(n ‐ 1); } else { return 1; } } 19 JAL / JR for Recursion? First call x1 after1 1 main: myfn: jal myfn if (test) after1: jal myfn add x1,x2,x3 after2: jr x1 Problems with recursion: • overwrites contents of x1 20 JAL / JR for Recursion? Recursive Call x1 after1after2 1 2 main: myfn: jal myfn if (test) after1: jal myfn add x1,x2,x3 after2: jr x1 Problems with recursion: • overwrites contents of x1 21 JAL / JR for Recursion? Return from Recursive Call x1 after2 1 2 main: myfn: jal myfn if (test) after1: jal myfn add x1,x2,x3 after2: 3 jr x1 Problems with recursion: • overwrites contents of x1 22 JAL / JR for Recursion? Return from Original Call??? x1 after2 1 2 main: myfn: jal myfn if (test) after1: jal myfn add x1,x2,x3 after2: Stuck! 3 4 jr x1 Problems with recursion: • overwrites contents of x1 23 JAL / JR for Recursion? Return from Original Call??? x1 after2 1 2 main: myfn: jal myfn if (test) after1: jal myfn add x1,x2,x3 after2: Stuck! 3 4 jr x1 Problems with recursion: • overwrites contents of x1 • Need a way to save and restore register contents 24 Need a “Call Stack” Call stack • contains activation records high mem (aka stack frames) Each activation record contains • the return address for that invocation x1 = after1 • the local variables for that procedure sp A stack pointer (sp) keeps track of the top of the stack • dedicated register (x2) on the RISC-V Manipulated by push/pop operations • push: move sp down, store • pop: load, move sp up low mem 25 Cheat Sheet and Mental Model for Today compute jump/branch targets A memory register alu D D file B +4 addr PC control B d d in out M inst memory extend new Forward unit pc Detect hazard imm Instruction Instruction Execute Write- ctrl ctrl Fetch Decode ctrl Memory Back IF/ID ID/EX EX/MEM MEM/WB 26 Need a “Call Stack” Call stack • contains activation records high mem (aka stack frames) Each activation record contains • the return address for that invocation x1 = after1 • the local variables for that procedure sp A stack pointer (sp) keeps track of x1 = after2 the top of the stack sp • dedicated register (x2) on the RISC-V Manipulated by push/pop operations • push: move sp down, store Push: ADDI sp, sp, -4 • pop: load, move sp up SW x1, 0 (sp) low mem 27 Need a “Call Stack” Call stack • contains activation records high mem (aka stack frames) Each activation record contains • the return address for that invocation x1 = after1 • the local variables for that procedure sp A stack pointer (sp) keeps track of x1 = after2 the top of the stack sp • dedicated register (x2) on the RISC-V Manipulated by push/pop operations • push: move sp down, store Push: ADDI sp, sp, -4 SW x1, 0 (sp) • pop: load, move sp up Pop: LW x1, 0 (sp) ADDI sp, sp, 4 JR x1 low mem 28 Need a “Call Stack” high mem 2 main: myfn: jal myfn addi sp,sp,-4 after1: sw x1, 0(sp) after1 add x1,x2,x3 if (test) after2 jal myfn sp after2: after2 sp lw x1, 0(sp) after2 addi sp,sp,4 sp jr x1 low mem Stack used to save and restore contents of x1 29 Need a “Call Stack” high mem 2 main: myfn: jal myfn addi sp,sp,-4 after1 after1: sw x1, 0(sp) sp add x1,x2,x3 if (test) after2 jal myfn sp after2: after2 sp lw x1, 0(sp) after2 addi sp,sp,4 sp jr x1 low mem Stack used to save and restore contents of x1 30 Stack Growth (Call) Stacks start at a high address in memory Stacks grow down as frames are pushed on • Note: data region starts at a low address and grows up • The growth potential of stacks and data region are not artificially limited 31 An executing program in memory 0xfffffffc top system reserved 0x80000000 0x7ffffffc stack dynamic data (heap) 0x10000000 static data .data code (text) .text 0x00400000 0x00000000 system reserved bottom 32 An executing program in memory 0xfffffffc top system reserved 0x80000000 0x7ffffffc stack “Data Memory” dynamic data (heap) 0x10000000 static data code (text) “Program Memory” 0x00400000 0x00000000 system reserved bottom 33 Anatomy of an executing program compute jump/branch targets A memory register alu D D file x2 ($sp) B +4 x1 ($ra) addr Stack,
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages101 Page
-
File Size-