Carnegie Mellon MachineLevel Programming III How to implement

  • Slides: 36
Download presentation
Carnegie Mellon Machine-Level Programming III: How to implement procedure calls Slides adapted from Bryant

Carnegie Mellon Machine-Level Programming III: How to implement procedure calls Slides adapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition

Requirements of procedure calls? Passing control ¢ Passing Arguments & return value ¢ Allocate

Requirements of procedure calls? Passing control ¢ Passing Arguments & return value ¢ Allocate / deallocate local variables ¢ P(…) { • • y = Q(x); y++; • } int Q(int i) { int t, z; . . return z; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

Carnegie Mellon How to transfer control for procedure calls? void main(){. . f(. .

Carnegie Mellon How to transfer control for procedure calls? void main(){. . f(. . ) L 1: . . } void f(){. . g(. . ) L 2: . . } void g(){. . h(. . ) L 3: . . } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition • Jump to f() • Remember where to come back L 1 L 2 L 1 • Jump to f() • Remember where to come back L 2 L 3 3

Carnegie Mellon How to transfer control for procedure calls? void main(){. . f(. .

Carnegie Mellon How to transfer control for procedure calls? void main(){. . f(. . ) L 1: . . } void f(){. . g(. . ) L 2: . . } void g(){. . h(. . ) L 3: . . } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition • Jump to L 1 • Forget L 1 • Jump to L 2 • Forget L 2 L 1 L 2 L 3 • Jump to L 3 • Forget L 3 4

x 86 -64 Stack Region of memory managed like a stack ¢ Grows toward

x 86 -64 Stack Region of memory managed like a stack ¢ Grows toward lower addresses Stack “Bottom” ¢ ¢ Increasing Addresses Register %rsp contains lowest stack address Stack Grows Down § address of “top” element Stack Pointer: %rsp Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5

Carnegie Mellon Push instruction ¢ Stack “Bottom” pushq Src § Fetch operand at Src

Carnegie Mellon Push instruction ¢ Stack “Bottom” pushq Src § Fetch operand at Src § Decrement %rsp by 8 § Write operand at address given by %rsp Stack Pointer: %rsp Increasing Addresses Stack Grows Down -8 Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6

Carnegie Mellon Pop instruction ¢ Stack “Bottom” popq Dest § Read value at address

Carnegie Mellon Pop instruction ¢ Stack “Bottom” popq Dest § Read value at address given by %rsp § Increment %rsp by 8 § Store value at Dest (must be register) Stack Pointer: %rsp Increasing Addresses +8 Stack Grows Down Stack “Top” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7

Carnegie Mellon Call and Ret instructions ¢ call label § Push return address on

Carnegie Mellon Call and Ret instructions ¢ call label § Push return address on stack § Jump to label Next instrution after the ¢ ret call instruction § Pop 8 bytes (address) from stack § Jump to address Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8

Carnegie Mellon Using the stack void main(){. . f(. . ) Call L 3

Carnegie Mellon Using the stack void main(){. . f(. . ) Call L 3 L 1: . . } void f(){ L 3: . . g(. . ) Call L 5 L 2: . . } void g(){ L 5: . . } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition • Jump to L 3 • Remember where to come back L 1 L 2 • Jump to L 5 • Remember where to come back 9

Carnegie Mellon Using the stack void main(){. . f(. . ) Call L 3

Carnegie Mellon Using the stack void main(){. . f(. . ) Call L 3 L 1: . . } • Pop L 1 from stack • Jump to L 1 void f(){ L 3: . . g(. . ) Call L 5 L 2: . . } ret L 1 L 2 • Pop L 2 from stack • Jump to L 2 void g(){ L 5: . . } ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10

Control Flow Example #1 00000400540 <main>: • • 400544: callq 400550 <func> 400549: mov

Control Flow Example #1 00000400540 <main>: • • 400544: callq 400550 <func> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 %rsp 0 x 120 %rip 0 x 400544 00000400550 <func>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11

Control Flow Example #2 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549:

Control Flow Example #2 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 0 x 118 0 x 400549 %rsp 0 x 118 %rip 0 x 400550 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 12

Control Flow Example #3 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549:

Control Flow Example #3 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 0 x 118 0 x 400549 %rsp 0 x 118 %rip 0 x 400557 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13

Control Flow Example #4 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549:

Control Flow Example #4 00000400540 <multstore>: • • 400544: callq 400550 <mult 2> 400549: mov %rax, (%rbx) • • 0 x 130 0 x 128 • • • 0 x 120 %rsp 0 x 120 %rip 0 x 400549 00000400550 <mult 2>: 400550: mov %rdi, %rax • • 400557: retq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14

How to allocate/deallocate local variables? ¢ Allocate local variables on the stack § subq

How to allocate/deallocate local variables? ¢ Allocate local variables on the stack § subq $0 x 8, %rsp //allocate 8 bytes § movq $1, 8(%rsp) //store 1 in the allocated 8 bytes ¢ De-allocate then from the stack before returning § addq $0 x 8, %rsp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 15

How to pass arguments and return values? ¢ We could store arguments/return values on

How to pass arguments and return values? ¢ We could store arguments/return values on the stack § Not very efficient ¢ C tries to pass arguments and return values using registers § C’s calling convention Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 16

C’s calling convention: args/return values Registers ¢ First 6 arguments Stack • • •

C’s calling convention: args/return values Registers ¢ First 6 arguments Stack • • • %rdi %rsi Arg n %rdx • • • %rcx ¢ %r 8 Arg 8 %r 9 Arg 7 Return value %rax Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ¢ Only allocate stack space when needed 17

Carnegie Mellon What does mystery function do? … leaq call … 8(%rsp), %rdi <mystery>:

Carnegie Mellon What does mystery function do? … leaq call … 8(%rsp), %rdi <mystery>: movq (%rdi), %rax addq $1, %rax movq %rax, (%rdi) ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition void mystery(long *x) { (*x)++; } 18

Carnegie Mellon Calling convention: Register Saving ¢ When procedure f calls g: § f

Carnegie Mellon Calling convention: Register Saving ¢ When procedure f calls g: § f is the caller, g is the callee Can caller assume register values do not change when callee returns? ¢ If not, caller must save all register values (in memory) that it needs to use later ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 19

Carnegie Mellon Calling convention: register saving ¢ Some registers are “caller saved”, others are

Carnegie Mellon Calling convention: register saving ¢ Some registers are “caller saved”, others are “callee saved” § Caller saved Caller saves “caller saved” registers on stack before the call § Callee saved § Callee saves “callee saved” registers on stack before using § Callee restores them before returning to caller § Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20

C’ calling convention: Register Usage Return value Arguments Caller-saved %rax %rdi %rsi %rdx %rcx

C’ calling convention: Register Usage Return value Arguments Caller-saved %rax %rdi %rsi %rdx %rcx %r 8 %r 9 %r 10 %r 11 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Callee-saved Temporaries %rbx %r 12 %r 13 %r 14 %rbp %rsp 21

Carnegie Mellon Why caller vs. callee saved registers? Why not make all registers caller

Carnegie Mellon Why caller vs. callee saved registers? Why not make all registers caller saved? ¢ Why not make all registers callee saved? ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 22

Carnegie Mellon Register save Example long func(long x) { long v 1 = 15213;

Carnegie Mellon Register save Example long func(long x) { long v 1 = 15213; long v 2 = incr(&v 1); return x+v 2; } Initial Stack Structure. . . Rtn address func: pushq subq movq leaq call addq popq ret %rbx $16, %rsp %rdi, %rbx $15213, 8(%rsp), %rdi incr %rbx, %rax $16, %rsp %rbx Resulting Stack Structure. . . Rtn address Saved %rbx 15213 Unused Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsp+8 %rsp 23

Carnegie Mellon Register save example long func(long x) { long v 1 = 15213;

Carnegie Mellon Register save example long func(long x) { long v 1 = 15213; long v 2 = incr(&v 1); return x+v 2; } func: pushq subq movq leaq call addq popq ret %rbx $16, %rsp %rdi, %rbx $15213, 8(%rsp), %rdi incr %rbx, %rax $16, %rsp %rbx Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Resulting Stack Structure. . . Rtn address Saved %rbx 15213 Unused %rsp+8 %rsp Pre-return Stack Structure. . . Rtn address %rsp 24

Stack Frames ¢ We view the part of stack pertaining to each function invocation

Stack Frames ¢ We view the part of stack pertaining to each function invocation as a “stack frame” Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25

Carnegie Mellon x 86 -64/C Stack Frame ¢ Current Stack Frame (“Top” to Bottom)

Carnegie Mellon x 86 -64/C Stack Frame ¢ Current Stack Frame (“Top” to Bottom) § “Argument build: ” Parameters for function about to call § Local variables (if can’t be kept in registers) § Saved register context ¢ Caller Frame Saved Registers + Local Variables Caller Stack Frame § Return address Pushed by call instruction § Arguments for this call § Stack pointer %rsp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Arguments 7+ Return Addr Argument Build (Optional) 26

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo yoo

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo yoo who am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 27

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who();

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %rsp who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 28

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 29

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); am. I(…) • • { • • • am. I(); • • } • am. I(); • } yoo yoo who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition am. I %rsp 30

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition am. I %rsp 31

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who();

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %rsp who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 32

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • {

Carnegie Mellon Stack Example yoo(…) { who(…) • { am. I(…) • • { am. I(); who(); • • am. I(); • } • } yoo yoo who am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 33

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who();

Carnegie Mellon Stack Example yoo(…) { who(…) • { • • am. I(); who(); • • • am. I(); • • • } } yoo yoo who am. I %rsp who am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 34

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo yoo

Carnegie Mellon Stack Example yoo(…) { • • who(); • • } yoo yoo who am. I %rsp am. I Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 35

Carnegie Mellon C/x 86 -64 Procedure Summary Uses stack for procedure call / return

Carnegie Mellon C/x 86 -64 Procedure Summary Uses stack for procedure call / return ¢ Content of stack frame: ¢ § § Local variables Saved registers Arguments Return address Caller Frame %rbp (Optional) %rsp Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Arguments 7+ Return Addr Old %rbp Saved Registers + Local Variables Argument Build 36