Control
description
Transcript of Control
-
*Control
Compilers Autumn 2002
-
*OutlineSwitchConditional MoveSuggested readingChap 3.6.6, 3.6.7
-
*case 103result += 11;break ;case 104: case 106:result *= result ;break ;default:result = 0 ;}return result ;}Switch Statements int switch_eg(int x, int n) {int result = x ;switch ( n ) {case 100:result *= 13 ;break ;case 102:result += 10 ;/* fall through */
-
*Switch ConstructProperties of Switch ConstructInteger testingMultiple outcomes (may be a large number)Improve the readability of the source code
-
*case 103result += 11;break ;case 104: case 106:result *= result ;break ;default:result = 0 ;}return result ;}Switch Statements int switch_eg(int x, int n) {int result = x ;switch ( n ) {case 100:result *= 13 ;break ;case 102:result += 10 ;/* fall through */Integer testingMultiple cases
-
*Jump Table Efficient implementationAvoid long sequence of if-else statementCriteriathe number of cases and the sparcity of the case value
-
*Jump Table ImplementationJump Tabletarget = JTab[op];goto *target;Approx. TranslationJump Targets
-
*case 103result += 11;break ;case 104: case 106:result *= result ;break ;default:result = 0 ;}return result ;}Switch Statements int switch_eg(int x, int n) {int result = x ;switch ( n ) {case 100:result *= 13 ;break ;case 102:result += 10 ;/* fall through */
-
*Jump Table Implementationint switch_eg_goto ( int x, int n) {unsigned ni = n - 100;int result = x ;if ( ni >6 ) goto loc_def ; //defaultgoto jt[xi];
loc_a: //100result *= 13 ;goto done ;loc_b: //102result += 10 ; /* fall through*/loc_c: //103result +=11; goto done ; loc_d: //104, 106result *= result ;goto done ; loc_def: //defaultresult = 0 ; done:return result ; }code jt[7] = {loc_a, loc_def, loc_b, loc_c, loc_d, loc_def, loc_d};
-
*Jump Table .section .rodata .align 4 .L7: .long .L3case 100: loc_a .long .L2case 101: loc_def .long .L4case 102: loc_b .long .L5case 103: loc_c .long .L6case 104: loc_d .long .L2case 105: loc_def .long .L6case 106: loc_d
-
*Jump Table Implementation movl8(%ebp), %edxget x movl12(%ebp), %eaxget n subl$100, %eaxcompute index = n 100 cmpl$6, %eaxcompare index:6 ja.L2If > , goto default jmp *.L7(, %eax, 4) .L2:default: mov $0, %eaxresult = 0jmp .L8goto done
-
*Jump Table Implementation .L5:loc_c: // 103 movl%edx, %eaxresult = x jmp.L9goto rest .L3:loc_a: // 100 leal(%edx, %edx, 2), %eaxresult = x * 3 leal(%edx, %eax, 4), %eaxresult = x + 4 * result jmp.L8goto done .L4:loc_b: // 102 leal 10(%edx), %eaxresult = x + 10
-
*Jump Table Implementation .L9:rest: // fall through addl$11, %eaxresult += 11 jmp.L8goto done .L6:loc_d: // 104, 106 movl%edx, %eaxresult = x imull%edx, %eaxresult *= x .L8:done:
-
*Conditional MoveOriginal C code
1 int absdiff(int x, int y) {2 return x < y ? y-x : x-y;3 }(b) Implementation using conditional assignment1 int cmovdiff(int x, int y) {2 int tval = y-x;3 int rval = x-y;4 int test = x < y;5 /* Line below requires6 single instruction: */7 if (test) rval = tval;8 return rval;9 }
-
*Conditional Move(c) Generated assembly code (x at %ebp+8, y at %ebp+12)
movl 8(%ebp), %ecxGet x movl 12(%ebp), %edxGet y movl %edx, %ebx Copy y subl %ecx, %ebxCompute y-x movl %ecx, %eax Copy x subl %edx, %eaxCompute x-y and set as return value cmpl %edx, %ecx Compare x:y cmovl %ebx, %eax If < , replace return value with y-x%ecxx%edxy%ebxy-x%eaxx-y
-
*Invalid SituationConditional Move instructions suppose that there is no side effect
int cread(int *xp) { return (xp ? *xp : 0); }
-
*Procedure Call
Compilers Autumn 2002
-
*OutlineProcedure callStack frameCalling conventionsRecursiveSuggested readingChap 3.7
-
*Execution within Procedure/FunctionData Movement (e.g., movl $-17, (%esp))Arithmetic Operations (e.g., incl 8(%eax))Logical Operations (e.g., xorl 8(%esp), %eax)Condition Codes (e.g., cmpl %eax, %edx)Jump Instructions (e.g., jg .L5)How to execute cross procedures?
-
*Procedure/Function callAnother type of unconditional JUMPSAME:Control from one part to anotherDIFF:ReturnPassing data (arguments, return values)Local variableRegisters
-
*Basic ConceptTerminologyCallerCallee
f()g()call-1h()call-2call-1Caller: fCallee: g
call-2Caller: gCallee: h
-
*Basic ConceptTerminologyCaller: gCallee: f
Control Flowe.g. int g() { return 1 ; } int f() { return g() ; } int main() { g(); return f(); }
g()f()callmain()g()f()g()
-
*Procedure/Function Implementation Invoke callee Return to caller Passing data Registers Local variable
-
*Procedure/Function Implementation Invoke callee: call (new instructions) Return to caller Passing data Registers Local variable
-
*Invoke CalleeInstruction call label (direct)call *operand (indirect)Behavior description (by hardware)Save return address in the stackJump to the entry of callee
call = push + jmppush retaddrjmp callee
-
*Procedure/Function Implementation Invoke callee: call (new instructions) Return to caller: ret (new instructions) Passing data Registers Local variable
-
*Return to CallerInstruction retBehavior description (by hardware)Pop return address from stackJump to return address in caller
ret = pop + jmp
pop retaddrjmp retaddr
-
*Procedure/Function Implementation Invoke callee: call (new instructions) Return to caller: ret (new instructions) Passing data: stack, register Registers Local variable
-
*Stack Frame StructureThe portion of stack allocated for a procedureA stack frame is delimited by The frame pointer %ebpThe stack pointer %espThe stack pointer can move when the procedure is executing (dynamic)The frame pointer is static
Frame
-
*Stack Frame Structurecall: save return address in the stackret: pop return address from stackThe end of callers stack frame
Callee Frame
retaddr
Caller Frame
-
*Frame ChainPointers (%ebp/%esp) only delimit topmost frame
Frames are chained%ebp%esp 1. call callee 2. push %ebp 3. mov %esp, %ebp . . . n-2. mov %ebp, %espn-1. pop %ebp n. ret
old %ebp
old %ebp
old %ebp
-
*Restore Caller %ebpInstruction leaveBehavior description (by hardware)Adjust %esp to callee %ebpPop caller %ebp from stack
leave = mov + pop
mov %ebp, %esppop %ebp
-
*Execution of call and ret//beginning of function sum08048394 : 8048394: 55push%ebp . . . 80483a4:ret . . .//call to sum from main 80483dc: e8 b3 ff ff ff call8048394 80483e1: 83 c4 14add$0x14, %espExecuting callAfter callAfter ret
%eip0x080483dc%esp0xff9bc960ret-
%eip0x08048394%esp0xff9bc95cret0x080483e1
%eip0x080483e1%esp0xff9bc960ret-
-
*Memory Layout
-
*Passing Data: ArgumentsPushed by CallerSaved in caller frameJust upon of return addressFrom Nth to 1stUsed by CalleeRelative to %ebpOffset: 4 + 4*i + %ebp
argument 1
. . . . . .
argument N
Callee Frame
retaddr
Caller Frame
-
*Passing Data: Argumentspush argument N
argument N
Caller Frame
-
*Passing Data: Argumentspush argument N. . . push argument 1
argument 1
. . . . . .
argument N
Caller Frame
-
*Passing Data: Argumentspush argument N. . . push argument 1call callee
argument 1
. . . . . .
argument N
retaddr
Caller Frame
-
*Passing Data: Argumentspush argument N. . . push argument 1call calleepush %ebp
old %ebp
argument 1
. . . . . .
argument N
retaddr
Caller Frame
Callee Frame
-
*Passing Data: Argumentspush argument N. . . push argument 1call calleepush %ebpmov %esp, %ebp. . .
old %ebp
argument 1
. . . . . .
argument N
retaddr
Caller Frame
Callee Frame
-
*Passing Data: Return ValueSpecific register to keep the return value %eax is used to pass the result of callee to caller
-
*Procedure/Function Implementation Invoke callee: call (new instructions) Return to caller: ret (new instructions) Passing data: stack, register Registers: calling convention Local variable
-
*Calling ConventionRegisters act as a single resource shared by all of the proceduresOnly 1 procedure can be activePartition registers between caller and calleeSaller-save registerCallee-save registerOnly consider the registers used by the procedure
-
*Calling ConventionCaller-save registers%eax, %edx, %ecxSaved by callerCallee can use these registers freely
The contents in these registers may be changed after returnCaller must restore them if it tries to use them after calling
-
*Caller-save Registerspush %eaxpush argument N. . . push argument 1call callee
Saved %eax
argument 1
. . . . . .
argument N
retaddr
Caller Frame
-
*Calling ConventionCallee-save registers%ebx, %esi, %ediSaved by calleeCaller can use these registers freely
Callee must save them before usingCallee must restore them before return
-
*Callee-save Registerscall calleepush %ebpmov %esp, %ebppush %ebx. . .
Old %ebp
Saved %ebx
Callee Frame
retaddr
-
*Procedure/Function Implementation Invoke callee: call (new instructions) Return to caller: ret (new instructions) Passing data: stack, register Registers: calling convention Local variable: stack
-
*Local VariableWhy not store local variables in registers ?No enough registersArray and structures (e.g., a[2])Need address (e.g., &a)
-
*Local VariableAllocationBelow saved regs or old %ebpmove/sub %esp, (e.g., subl $4, %esp)De-allocationmove/add %esp, (e.g., addl $4, %esp)UsageRelative to %esp/%ebp, (e.g., movl %eax, 8(%esp))
Old %ebp
Local variable
Saved regs
Callee Frame
retaddr
**************************************************