Download presentation
Presentation is loading. Please wait.
Published byPhillip Welch Modified over 8 years ago
1
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building LR parse tables Compiling Expressions Compiling Expressions Build control flow intermediates Build control flow intermediates Generate target code Generate target code Optimize intermediate Optimize intermediate
2
Implementation “Done” to date Symbol Table Symbol Table Generic List routines Generic List routines Expressions using lex, yacc input Expressions using lex, yacc input
3
Three-Address Code section 6.2.1 (new), pp 467 (old) Assembler for generic computer Assembler for generic computer Types of statements 3-address (Dragon) Types of statements 3-address (Dragon) Assignment statement x = y op z Assignment statement x = y op z Unconditional jump br label Unconditional jump br label Conditional jump if( cond ) goto label Conditional jump if( cond ) goto label Parameter x Parameter x Call statement call f Call statement call f
4
Example “Source” a = ((c-1) * b) + (-c * b)
5
Example 3-Address t1 = c - 1 t2 = b * t1 t3 = -c t4 = t3 * b t5 = t2 + t4 a = t5
6
Three-Address Implementation (Quadruples, sec 6.2.2; pp 470-2) oparg1arg2result 0-c1t1 1*bt1t2 2uminusct3 3*bt3t4 4+t2t4t5 5=t5a
7
Three-Address Implementation (Triples, section 6.2.3) oparg1arg2 0-c1 1*b(0) 2uminusc 3*b(2) 4+(1)(3) 5=a(4)
8
Three-Address Implementation N-tuples (my choice – and yours ??) N-tuples (my choice – and yours ??) Lhs = oper(op 1, op 2, …, op n ) Lhs = oper(op 1, op 2, …, op n ) Lhs = call(func, arg 1, arg 2, … arg n ) Lhs = call(func, arg 1, arg 2, … arg n ) If condOper(op 1, op 2, Label) If condOper(op 1, op 2, Label) br Label br Label
9
Three-Address Code 3-address operands 3-address operands Variable Variable Constant Constant Array Array Pointer Pointer
10
Control Flow Graph sec 8.4 (new); sec 9.4 (old) Nodes are Basic Blocks Nodes are Basic Blocks Single entry, single exit Single entry, single exit No branch exempt (possibly) at bottom No branch exempt (possibly) at bottom Edges represent one possible flow of execution between two basic blocks Edges represent one possible flow of execution between two basic blocks Whole CFG represents a function Whole CFG represents a function
11
Bubble Sort begin; int A[10]; main(){ int i,j; int i,j; Do 10 i = 0, 9, 1 Do 10 i = 0, 9, 1 10 A[i] = random(); Do 20 i = 1, 9, 1 Do 20 i = 1, 9, 1 Do 20 j = 1, 9, 1 Do 20 j = 1, 9, 1 20 if( A[j] > A[j+1]) swap(j); }
12
Bubble Sort (cont.) int swap(int i) { int temp; int temp; temp = A[i]; temp = A[i]; A[i] = A[i+1]; A[i] = A[i+1]; A[i+1] = temp; A[i+1] = temp;}end;
13
Example Generate 3-addr code for BubbleSort
14
Building CFG alg 8.5 (pp 526-7); alg 9.1(p 529) Starting with 3-addr code Starting with 3-addr code Each leader starts a basic block which ends immediately before the next leader Each leader starts a basic block which ends immediately before the next leader ID “leaders” (heads of basic blocks) ID “leaders” (heads of basic blocks) First statement is a leader First statement is a leader Any statement that is the target of a conditional of unconditional goto is a leader Any statement that is the target of a conditional of unconditional goto is a leader Any statement immediately following a goto or conditional goto is a leader Any statement immediately following a goto or conditional goto is a leader Each leader starts a basic block which ends immediately before the next leader Each leader starts a basic block which ends immediately before the next leader
15
Example Build control flow graphs for BubbleSort
16
Code Generation Pick three registers to be used throughout Pick three registers to be used throughout Assuming stmt of form dest = s1 op s2 Assuming stmt of form dest = s1 op s2 Generate code by: Generate code by: Load source 1 into r5 Load source 1 into r5 Load source 2 into r6 Load source 2 into r6 R7 = r5 op r6 R7 = r5 op r6 Store r7 into destination Store r7 into destination
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.