Carnegie Mellon MachineLevel Programming II Control Slides adapted

  • Slides: 25
Download presentation
Carnegie Mellon Machine-Level Programming II: Control Slides adapted from Bryant and O’Hallaron, Computer Systems:

Carnegie Mellon Machine-Level Programming II: Control Slides adapted from Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 1

Carnegie Mellon Today: How control flow is done Condition codes ¢ Conditional branches ¢

Carnegie Mellon Today: How control flow is done Condition codes ¢ Conditional branches ¢ Loops ¢ Switch Statements ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 2

Carnegie Mellon Processor State (x 86 -64, Partial) ¢ Registers %rax %r 8 %rbx

Carnegie Mellon Processor State (x 86 -64, Partial) ¢ Registers %rax %r 8 %rbx %r 9 %rcx %r 10 %rdx %r 11 %rsi %r 12 %rdi %r 13 %rsp %r 14 %rbp %r 15 %rip ¢ Condition codes (single bit registers) CF ZF SF OF Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 3

Carnegie Mellon Condition Codes (implicit setting) ¢ Implicitly set by arithmetic operations Example: addq

Carnegie Mellon Condition Codes (implicit setting) ¢ Implicitly set by arithmetic operations Example: addq Src, Dest t = a + b CF (Carry Flag) set if unsigned overflow ZF (Zero Flag) set if t == 0 SF (Sign Flag) set if t < 0 (as signed) OF (Over. Flow Flag) set if signed overflow (a>0 && b>0 && t<0) || (a<0 && b<0 && t>=0) ¢ Not set by leaq Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 4

Carnegie Mellon Condition Codes (Explicit Setting: Compare) ¢ Explicit set by Compare Instruction §cmpq

Carnegie Mellon Condition Codes (Explicit Setting: Compare) ¢ Explicit set by Compare Instruction §cmpq b, a like computing a-b without setting destination §CF set if unsigned overflow §ZF set if a == b §SF set if (a-b) < 0 (as signed) §OF set if two’s-complement (signed) overflow (a>0 && b<0 && (a-b)<0) || (a<0 && b>0 && (a-b)>0) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 5

Carnegie Mellon Condition Codes (Explicit Setting: Test) ¢ Explicit set by Test instruction §testq

Carnegie Mellon Condition Codes (Explicit Setting: Test) ¢ Explicit set by Test instruction §testq b, a (like computing a&b without setting destination) §ZF set when a&b == 0 §SF set when a&b < 0 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 6

Carnegie Mellon Reading Condition Codes ¢ Set. X Instructions § Set low-order byte of

Carnegie Mellon Reading Condition Codes ¢ Set. X Instructions § Set low-order byte of destination register to 0/1 based on condition codes § Does not alter remaining 7 bytes Set. X sete setne sets setns setge setle seta setb Condition ZF ~ZF SF ~(SF^OF)&~ZF ~(SF^OF)|ZF ~CF&~ZF CF Description Equal / Zero Not Equal / Not Zero Negative Nonnegative Greater (Signed) Greater or Equal (Signed) Less or Equal (Signed) Above (unsigned) Below (unsigned) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 7

x 86 -64 Integer Registers %rax %al %r 8 b %rbx %bl %r 9

x 86 -64 Integer Registers %rax %al %r 8 b %rbx %bl %r 9 b %rcx %cl %r 10 b %rdx %dl %r 11 b %rsi %sil %r 12 b %rdi %dil %r 13 b %rsp %spl %r 14 b %rbp %bpl %r 15 b Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 8

Carnegie Mellon Reading Condition Codes (Cont. ) ¢ Set. X Example: int gt (long

Carnegie Mellon Reading Condition Codes (Cont. ) ¢ Set. X Example: int gt (long x, long y) { return x > y; } cmpq %rsi, %rdi setg %al movzbl %al, %eax ret Register Use(s) %rdi Argument x %rsi Argument y %eax Return value # Compare x: y # Set when > # Zero rest of %eax Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 9

Carnegie Mellon Today Control: Condition codes ¢ Conditional branches ¢ Loops ¢ Switch Statements

Carnegie Mellon Today Control: Condition codes ¢ Conditional branches ¢ Loops ¢ Switch Statements ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 10

Carnegie Mellon Jumping ¢ j. X Instructions: jump to different part of code j.

Carnegie Mellon Jumping ¢ j. X Instructions: jump to different part of code j. X Condition Description jmp 1 Unconditional je ZF Equal / Zero jne ~ZF Not Equal / Not Zero js SF Negative jns ~SF Nonnegative jg ~(SF^OF)&~ZF Greater (Signed) jge ~(SF^OF) Greater or Equal (Signed) jl (SF^OF) Less (Signed) jle (SF^OF)|ZF Less or Equal (Signed) ja ~CF&~ZF Above (unsigned) jb CF Below (unsigned) Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 11

Carnegie Mellon Conditional Branch Example (Old Style) ¢ gcc –Og -S –fno-if-conversion control. c

Carnegie Mellon Conditional Branch Example (Old Style) ¢ gcc –Og -S –fno-if-conversion control. c long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } absdiff: cmpq jle movq subq ret. L 4: movq subq ret Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition %rsi, %rdi. L 4 %rdi, %rax %rsi, %rax # x: y # x <= y %rsi, %rax %rdi, %rax Register Use(s) %rdi Argument x %rsi Argument y %rax Return value 12

Carnegie Mellon Equivalent goto code ¢ Machine code is essentially doing control flow with

Carnegie Mellon Equivalent goto code ¢ Machine code is essentially doing control flow with goto statement long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } long absdiff_j (long x, long y) { long result; int ntest = x <= y; if (ntest) goto Else; result = x-y; goto Done; Else: result = y-x; Done: return result; } Do not write this normally Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 13

Carnegie Mellon Another way: Using Conditional Moves ¢ Conditional Move Instructions § cmov. X

Carnegie Mellon Another way: Using Conditional Moves ¢ Conditional Move Instructions § cmov. X instruction if (Test) Dest Src § Supported in post-1995 x 86 processors ¢ Why? § Branches (control transfer) can slow down execution § Conditional moves do not require control transfer Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 14

Carnegie Mellon Conditional Move Example long absdiff (long x, long y) { long result;

Carnegie Mellon Conditional Move Example long absdiff (long x, long y) { long result; if (x > y) result = x-y; else result = y-x; return result; } absdiff: movq subq cmpq cmovle ret %rdi, %rsi, %rdx, Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Register Use(s) %rdi Argument x %rsi Argument y %rax Return value %rax %rdx %rdi %rax # result = x-y # rdx = y-x # x: y # if <=, result = rdx 15

Carnegie Mellon Bad Cases for Conditional Move Expensive Computations val = Test(x) ? Hard

Carnegie Mellon Bad Cases for Conditional Move Expensive Computations val = Test(x) ? Hard 1(x) : Hard 2(x); ¢ ¢ Both values get computed Only makes sense when computations are very simple Risky Computations val = p ? *p : 0; ¢ ¢ Both values get computed May have undesirable effects Computations with side effects val = x > 0 ? x*=7 : x+=3; Both values get computed ¢ Must be side-effect free Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition ¢ 16

Carnegie Mellon Today Control: Condition codes ¢ Conditional branches ¢ Loops ¢ Switch Statements

Carnegie Mellon Today Control: Condition codes ¢ Conditional branches ¢ Loops ¢ Switch Statements ¢ Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 17

Carnegie Mellon “Do-While” Loop Example C Code long pcount_do (unsigned long x) { long

Carnegie Mellon “Do-While” Loop Example C Code long pcount_do (unsigned long x) { long result = 0; do { result += x & 0 x 1; x >>= 1; } while (x); return result; } ¢ ¢ Goto Version long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0 x 1; x >>= 1; if(x) goto loop; return result; } Count number of 1’s in argument x (“popcount”) Use conditional branch to either continue looping or to exit loop Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 18

Carnegie Mellon “Do-While” Loop Compilation Goto Version long pcount_goto (unsigned long x) { long

Carnegie Mellon “Do-While” Loop Compilation Goto Version long pcount_goto (unsigned long x) { long result = 0; loop: result += x & 0 x 1; x >>= 1; if(x) goto loop; return result; } movl. L 2: movq andl addq shrq jne ret Register Use(s) %rdi Argument x %rax result $0, %eax %rdi, %rdx $1, %edx %rdx, %rax %rdi. L 2 Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition # result = 0 # loop: # # t = x & 0 x 1 result += t x >>= 1 if (x) goto loop 19

Carnegie Mellon General “Do-While” Translation C Code do … while (Test); Goto Version loop:

Carnegie Mellon General “Do-While” Translation C Code do … while (Test); Goto Version loop: … if (Test) goto loop Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 20

Carnegie Mellon General “While” Translation #1 ¢ ¢ “Jump-to-middle” translation Used with -Og While

Carnegie Mellon General “While” Translation #1 ¢ ¢ “Jump-to-middle” translation Used with -Og While version while (Test) Body Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition Goto Version goto test; loop: Body test: if (Test) goto loop; done: 21

Carnegie Mellon General “While” Translation #2 While version while (Test) Body ¢ ¢ Do-While

Carnegie Mellon General “While” Translation #2 While version while (Test) Body ¢ ¢ Do-While Version if (!Test) goto done; do Body while(Test); done: Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition “Do-while” conversion Used with –O 1 Goto Version if (!Test) goto done; loop: Body if (Test) goto loop; done: 22

Carnegie Mellon “For” Loop for (Init; Test; Update ) Body long pcount_for(unsigned long x)

Carnegie Mellon “For” Loop for (Init; Test; Update ) Body long pcount_for(unsigned long x) { long result = 0; for (int i = 0; i < 8*sizeof(int); i++) { unsigned bit = (x >> i) & 0 x 1; result += bit; } return result; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 23

Carnegie Mellon “For” Loop While Loop For Version for (Init; Test; Update ) Body

Carnegie Mellon “For” Loop While Loop For Version for (Init; Test; Update ) Body While Version Init; while (Test ) { Body Update; } Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 24

Carnegie Mellon Summary ¢ Today § § Control: Condition codes Conditional branches & conditional

Carnegie Mellon Summary ¢ Today § § Control: Condition codes Conditional branches & conditional moves Loops Switch statements Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition 25