Docsity

Calling Conventions, Exercises of Computer Architecture and Organization

Partial preview of the text

Download Calling Conventions and more Exercises Computer Architecture and Organization in PDF only on Docsity!

Calling Conventions

Hakim Weatherspoon

CS 3410

Computer Science

Cornell University

[Weatherspoon, Bala, Bracy, McKee and Sirer]

Write- Memory Back

Instruction Fetch

Instruction Execute Decode

extend

register file

control

alu

memory

din dout

addr PC

memory

new pc

inst

IF/ID (^) ID/EX EX/MEM MEM/WB

imm

B
A

ctrl ctrl ctrl

B
D D
M

jump/branch^ compute targets

Forward unit Detect hazard

Big Picture: Where are we going?

SandyBridge Motherboard, 2011 http://news.softpedia.com 4

CPU

Main Memory (DRAM)

Big Picture: Where are we going?

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

Write- Memory Back

Instruction Fetch

Instruction Execute Decode

extend

register file

control

alu

memory

din dout

addr PC

memory

new pc

inst

IF/ID (^) ID/EX EX/MEM MEM/WB

imm

B
A

ctrl ctrl ctrl

B
D D
M

jump/branch^ compute targets

Cheat Sheet and Mental Model for Today

Forward unit Detect hazard

How do we share registers and use memory when making procedure calls?

Cheat Sheet and Mental Model for Today

  • first eight arg words passed in a0, a1, … , a
  • remaining arg words passed in parent’s stack frame
  • return value (if any) in a0, a
  • stack frame at sp
    • contains ra (clobbered on JAL to sub-functions)
    • contains local vars (possibly clobbered by sub-functions)
    • contains space for incoming args
  • callee save regs are preserved
  • caller save regs are not
  • Global data accessed via $gp

saved ra saved fp saved regs (s1 ... s11)

locals

incoming args

fp 

sp 

RISC-V Register Conventions

x0 zero zero x1 ra return address x2 sp stack pointer x3 gp global data pointer x4 tp thread pointer x5 t0 (^) temps x6 x7 t1t2 (caller save)

x8 s0/fp frame pointer x9 s1 saved (callee save) x10 a0 function args or x11 a1 return values x12 a function x13 a3 arguments x14 a

x15 a function arguments

x16 a x17 a x18 s

saved (callee save)

x19 s x20 s x21 s x22 s x23 s x24 s x25 s x26 s x27 s x28 t temps (caller save)

x29 t x30 t

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

Calling Convention for Procedure Calls

11

What is a Convention?

Warning: There is no one true RISC-V calling convention.

lecture != book != gcc != spim != web

Jumps are not enough

main: j myfn after1: add x1,x2,x

myfn: …

j after

Jumps to the callee

Jumps back

Jumps are not enough

main: j myfn after1: add x1,x2,x

j myfn after2: sub x3,x4,x

myfn: …

j after

Jumps to the callee

Jumps back

What about multiple sites?

??? Change target on the fly ???

j after

Jump-and-Link / Jump Register

main: jal myfn after1: add x1,x2,x

jal myfn after2: sub x3,x4,x

myfn: …

jr x

JAL saves the PC in register $

Subroutine returns by jumping to $

First call x1 (^) after

after

Jump-and-Link / Jump Register

main: jal myfn after1: add x1,x2,x

jal myfn after2: sub x3,x4,x

myfn: …

jr x

JAL saves the PC in register x

Subroutine returns by jumping to x

What happens for recursive invocations?

Second call x1 (^) after

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; } }

JAL / JR for Recursion?

JAL / JR for Recursion?

main: jal myfn after1: add x1,x2,x

myfn: if ( test ) jal myfn after2:

jr x

Problems with recursion:

  • overwrites contents of x

First call x1 (^) after