Top Banner
Code Generation for Control Flow Mooly Sagiv html://www.math.tau.ac.il/~msagiv/ courses/wcc03.html Chapter 6.4
47

Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Code Generation forControl FlowMooly Sagiv

html://www.math.tau.ac.il/~msagiv/courses/wcc03.html

Chapter 6.4

Page 2: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Outline• Local flow of control

– Conditionals– Switch– Loops

• Routine Invocation

• Non-local gotos

• Runtime errors

• Handling Exceptions

• Summary

Page 3: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Machine Code AssumptionsInstruction Meaning

GOTO Label Jump to Label

GOTO label register Indirect jump

IF condition register then GOTO Label Conditional

Jump

IF not condition register then GOTO Label

Page 4: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Boolean Expressions

• In principle behave like arithmetic expressions

• But are treated specially– Different machine instructions– Shortcut computations

Code for a < b yielding a condition value

Conversion condition value into Boolean

Conversion from Boolean in condition value

Jump to l on condition value

if (a < b) goto l

Page 5: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Shortcut computations

• Languages such as C define shortcut computation rules for Boolean

• Incorrect translation of e1 && e2

Code to compute e1 in loc1 Code to compute e2 in loc2 Code for && operator on loc1 and loc2

Page 6: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Code for Booleans(Location Computation)

• Top-Down tree traversal• Generate code sequences instructions • Jump to a designated ‘true’ label when the

Boolean expression evaluates to 1• Jump to a designated ‘false’ label when the

Boolean expression evaluates to 0• The true and the false labels are passed as

parameters

Page 7: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Exampleif ((a==0) && (b > 5))

x = ((7 * a) + b) if

&& :=

x +

* b

7 a

==

a 0

>

b 5

Page 8: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

if

&& :=

x +

* b

7 a

==

a 0

>

b 5

No label Lf

Lt Lf

Load_Local -8(FP), R0

Cmp_Constant R0, 0

IF NOT EQ THEN GOTO Lf

Lt Lf

Load_Local -12(FP), R0

Cmp_Constant R0, 5

IF GT THEN GOTO Lt

GOTO Lf

Lt:

Code for :=

Lf:

Page 9: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Location Computation for Booleans

Page 10: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Code generation for IF

if

Boolean expression true sequence false sequence

Allocate two new labels Lf, Lend

Generate code for Boolean(left, 0, Lf)

Code for Boolean with jumps to Lf

Code for

true sequence

Code for

false sequence

GOTO Lend

Lf:

Lend:

Page 11: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Code generation for IF (no-else)

if

Boolean expression true sequence

Allocate new label Lend

Generate code for Boolean(left, 0, Lend)

Code for Boolean with jumps to Lend

Code for

true sequence

Lend:

Page 12: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Coercions into value computations

:=

x a > b

Generate new label Lf

Load_Constant R0, 0;Generate code for Boolean(right, 0, Lf)

Load_Local -8(FP), R1;CMP R1, -12(FP) ;

IF <= GOTO Lf;

Load_Constant R0, 1Lf: Store_Local R0, -20(FP)

Page 13: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Effects on performance

• Number of executed instructions

• Unconditional vs. conditional branches

• Instruction cache

• Branch prediction

• Target look-ahead

Page 14: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Code for case statements

• Three possibilities– Sequence of IFs

• O(n) comparisons

– Jump table • O(1) comparisons

– Balanced binary tree • O(log n) comparisons

• Performance depends on n• Need to handle runtime errors

Page 15: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Simple Translation

Page 16: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Jump Table

• Generate a table of Lhigh-Llow+1 entries– Filled at ?time

• Each entry contains the start location of the corresponding case or a special label

• Generated codetmp_case_value:= case expression;

if tmp_case_value <Llow GOTO label_else; if tmp_case_value>Lhigh GOTO label_else; GOTO table[tmp_case_value –Llow];

Page 17: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Balanced trees

• The jump table may be inefficient– Space consumption– Cache performance

• Organize the case labels in a balanced tree– Left subtrees smaller labels– Right subtrees larger labels

• Code generated for node_k

label_k: IF tmp_case_value < lk THEN GOTO label of left branch ; IF tmp_case_value >lk THEN GOTO label of right branch; code for statement sequence; GOTO label_next;

Page 18: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Repetition Statements (loops)

• Similar to IFs• Preserve language semantics• Performance can be affected by different

instruction orderings• Some work can be shifted to compile-time

– Loop invariant– Strength reduction– Loop unrolling

Page 19: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

while statements

Boolean expression

while

statement

Sequence

test_label:

Generate new labels test_label, Lend

Generate code for Boolean(left, 0, Lend)

Code for Boolean with jumps to Lend

Code for

statement sequence

GOTO test_label;

Lend:

Page 20: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

while statements(2)

Boolean expression

while

statement

Sequence

GOTO test_label:Ls:

Generate labels test_label, Ls

Generate code for Boolean(left, Ls, 0)

Code for Boolean with jumps to LS

Code for

statement sequencetest_label:

Page 21: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

For-Statements

• Special case of while

• Tricky semantics– Number of executions– Effect on induction variables– Overflow

Page 22: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Simple-minded translation

FOR i in lower bound .. upper bound DO statement sequence

END for

i := lower_bound;

tmp_ub := upper_bound;

WHILE I <= tmp_ub DO code for statement sequence

i := i + 1;END WHILE

Page 23: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Correct TranslationFOR i in lower bound .. upper bound DO statement sequence

END for

i := lower_bound;

tmp_ub := upper_bound; IF i >tmp_ub THEN GOTO end_label;

loop_label: code for statement sequence if (i==tmp_ub) GOTO end_label;

i := i + 1;

GOTO loop_label;

end_label:

Page 24: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Tricky question

Page 25: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Loop unrolling

FOR i := 1 to n DO

sum := sum + a[i];

END FOR;

Page 26: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Summary• Handling control flow statements is usually

simple

• Complicated aspects– Routine invocation– Non local gotos– Runtime errors

• Runtime profiling can help

Page 27: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Routine Invocation

• Identify the called routine• Generate calling sequence

– Some instructions are executed by the callee

• Filling the activation record– Actual parameters (caller)– Administrative part – The local variable area– The working stack

Page 28: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Parameter Passing Mechanisms

• By value

• By reference

• By result

• By value-result

• Pass the R-value of the parameter

• Pass the L-value of the parameter

• Pass the L-value of the parameter

• The callee creates a temporary

• Stores the temporary upon return

• Can use registers

• Pass the L-value of the parameter

• The callee creates a temporary

• Store the temporary upon return

Page 29: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Caller Sequence

• Save caller-save registers• Pass actual parameters

– In stack– In register

• Pass lexical pointer• Generate code for the call

– Store return address– Pass flow of control

Page 30: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Callee Sequence

• Allocate the frame

• Store callee-save registers

• Perform the procedure code

• Return function result

• Restore callee-save registers

• Deallocate the frame

• Transfer the control back to the caller

Page 31: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Two activation records on the stack

Page 32: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Non-Local goto in C syntax

Page 33: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Non-local gotos

• Close activation records

• Restore callee-save registers

Main p:

l:

q:

goto l;

code Memory before

p

r

q

Main

Memory After

p

Page 34: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Runtime errors

• The smartest compiler cannot catch all potential errors at compile-time– Missing information– Undecidability

• Compiler need to generate code to identify runtime errors– Non-trivial

Page 35: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Common runtime errors

• Overflow– Integers

– Stack frame

• Limited resources• Division by zero• Null pointer dereferences• Dangling pointer dereferences• Buffer overrun (array out of bound)

Page 36: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Runtime Errors

• C– No support for runtime errors

– Situations with “undefined” ANSI C semantics• Leads to security vulnerabilities

• Pascal– Runtime errors abort execution

• Java– Runtime errors result in raised exceptions

– Can be handled by the programmer code

Page 37: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Detecting a runtime error

• Array bound-checkfoo() { int a[100], i, j; scanf(“%d%d”, &i, &j); while (i < j) { a[i] = i ; i++;}

if (“i” < 0 || “i” >=100)

THROW range_error;

Page 38: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Detecting a runtime error(2)

• Array bound-checkfoo() { int a[100], i, j; scanf(“%d%d”, &i, &j); if ((i >=0) && j <=100) { while (i < j) { a[i] = i ; i++; } else …}

Page 39: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Handling Runtime Errors

• Abort

• Statically assigned error handlers (signal)

• Exceptions

Page 40: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Signal Handlers

• Binds errors to handler routines

• Invoke a specific routine when runtime error occurs

• Report an error– Close open resources and exit– Resume immediately after the error– Resume in some “synchronization” point

Page 41: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Signal Example

Page 42: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Exceptions

• Flexible mechanism for handling runtime errors• Available in modern programming languages• Useful programming paradigm• But are hard to compile

void f() {

{ … g() …

catch(error1) {

… }

}

}

void g() {

… h() …

}

void h() {

… throw error1 …

}

Page 43: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Why are exceptions hard to compile?

• Dynamic addresses– Not always known when at compile time– Non local goto– Register state

• The handler may change in the execution of a routine

• The handler code assumes sequential execution

Page 44: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Handling Exceptions

• At compile time store mappings from exceptions to handlers

• Store a pointer to the table in the activation record

• When an exception is raised scan the stack frames to locate the most recent handler

• Perform a non-goto

Page 45: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

• Generate code for handler

• Terminate with a jump to the end of block/routine

• A unique label where the handler code begins

• Generate a table

• Store a pointer to the table at the activation record

Handler Code Block/Routine

exception handler

Page 46: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Code for raising exception

• Extract the pointer to the table from the activation record

• Search for a handler for the exception raised

• If not found pop the stack and repeat

• If found perform a non-local goto

• Usually combine the search and the goto

Page 47: Code Generation for Control Flow Mooly Sagiv html://msagiv/courses/wcc03.html Chapter 6.4.

Summary

• Non local transfer of control can be expensive– Hard to understand = Hard to implement

• But are necessary

• Challenging optimization problems