160 likes | 177 Views
This guide covers Regular Expressions, Context-Free Grammars, LR Parsing Algorithm, Building LR Parse Tables, and Code Generation with Three-Address Code. Includes examples and implementation details.
E N D
Road Map • Regular Exprs, Context-Free Grammars • LR parsing algorithm • Building LR parse tables • Compiling Expressions • Build control flow intermediates • Generate target code • Optimize intermediate
Implementation “Done” to date • Symbol Table • Generic List routines • Expressions using lex, yacc input
Three-Address Codesection 6.2.1 (new), pp 467 (old) • Assembler for generic computer • Types of statements 3-address (Dragon) • Assignment statement x = y op z • Unconditional jump br label • Conditional jump if( cond ) goto label • Parameter x • Call statement call f
Example “Source” a = ((c-1) * b) + (-c * b)
Example 3-Address t1 = c - 1 t2 = b * t1 t3 = -c t4 = t3 * b t5 = t2 + t4 a = t5
Three-Address Implementation(Quadruples, sec 6.2.2; pp 470-2)
Three-Address Implementation • N-tuples (my choice – and yours ??) • Lhs = oper(op1, op2, …, opn) • Lhs = call(func, arg1, arg2, … argn) • If condOper(op1, op2, Label) • br Label
Three-Address Code • 3-address operands • Variable • Constant • Array • Pointer
Control Flow Graphsec 8.4 (new); sec 9.4 (old) • Nodes are Basic Blocks • Single entry, single exit • No branch exempt (possibly) at bottom • Edges represent one possible flow of execution between two basic blocks • Whole CFG represents a function
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1 10 A[i] = random(); Do 20 i = 1, 9, 1 Do 20 j = 1, 9, 1 20 if( A[j] > A[j+1]) swap(j); }
Bubble Sort (cont.) int swap(int i) { int temp; temp = A[i]; A[i] = A[i+1]; A[i+1] = temp; } end;
Example Generate 3-addr code for BubbleSort
Building CFGalg 8.5 (pp 526-7); alg 9.1(p 529) • Starting with 3-addr code • Each leader starts a basic block which ends immediately before the next leader • ID “leaders” (heads of basic blocks) • First statement 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 • Each leader starts a basic block which ends immediately before the next leader
Example Build control flow graphs for BubbleSort
Code Generation • Pick three registers to be used throughout • Assuming stmt of form dest = s1 op s2 • Generate code by: • Load source 1 into r5 • Load source 2 into r6 • R7 = r5 op r6 • Store r7 into destination