Intermediate Code Generation

Size: px
Start display at page:

Download "Intermediate Code Generation"

Transcription

1 Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2011, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University of outhern California have explicit permission to make copies of these materials for their personal use.

2 Intermediate Code Generation IR Parse tree AT Intermediate Code Generation O(n) IR Three-Address Instructions regs Direct Translation Using DT scheme Parse tree to Three-Address Instructions Can be done while parsing in a single pass Needs to be able to deal with yntactic rrors and Recovery Indirect Translation First validate parsing constructing of AT Uses DT scheme to build AT Traverse the AT and generate Three Address Instructions

3 Control Flow tatements: Code Layout Attributes:.true: the label to which control flows if is true.false: the label to which control flows if is false.next: an inherited attribute with the symbolic label of the code following if then 1.true:.false:.code 1.code to.true to.false if then 1 else 2.true:.code 1.code to.true to.false.false:.next: goto.next 2.code

4 Code Layout while do 1.begin:.true:.code 1.code to.true to.false goto.begin.false: Difficulty: Need to know where to jump to Introduce a symbolic labels using the newlabel function Use inherited attributes Back-patch it later with the actual value (later )

5 Grammar and Actions if then 1.true:.true = newlabel().false =.next.false: 1.next =.next.code = append(.code,gen(.true:), 1.code) if then 1 else 2.true = newlabel().false = newlabelv 1.next =.next 2.next =.next.true:.false :.next:.code 1.code.code 1.code goto.next 2.code.code = append(.code,gen(.true:), 1.code, gen(goto.next),gen(.false :), 2.code) to.true to.false to.true to.false while do 1.begin:.code to.true to.false.begin = newlabel().true:.true = newlabel() 1.code.false =.next goto.begin 1.next =.begin.false.code = append(gen(.begin:),.code,gen(.true:), : 1.code, gen(goto.begin)

6 Control Flow Translation of Boolean xpressions hort-circuit valuation No Need to valuate portions of the expression if the outcome is already determined xamples: 1 or 2 need not evaluate 2 if 1 is known to be true. 1 and 2 need not evaluate 2 if 1 is known to be false. Use Control Flow Jump over code that evaluates boolean terms of the expression Use Inherited.false and.true attributes and link evaluation of

7 Control Flow for Boolean xpressions id 1 relop id 2.code = append(gen(if id 1.place relop id 2.place goto.true), gen(goto.false)) true.code = gen(goto.true) false.code = gen(goto.false) not 1 ( 1 ) 1 or 2 1.true =.true 1.false = newlabel 2.true =.true 2.false =.false.code = append( 1.code,gen( 1.false:), 2.code) 1.true =.false 1.false =.true.code = 1.code 1.true =.true 1.false =.false.code = 1.code 1 and 2 1.false =.false 1.true = newlabel 2.true =.true 2.false =.false.code = append( 1.code,gen( 1.true:), 2.code)

8 hort Circuit valuation a < b or c < d and e < f or and a < b c < d e < f

9 a < b or c < d and e < f hort Circuit valuation id 1 relop id 2.code = append( gen(if id 1.place relop id 2.place goto.true), gen(goto.false)) 1.true = Ltrue 1.false = L1 or.true = Ltrue.false = Lfalse and 1 or 2 1.true =.true 1.false = newlabel 2.true = Ltrue 2.false = Lfalse 2.true =.true 2.false =.false.code = append( 1.code, gen( 1.false:), 2.code) a < b c < d e < f L1: if a < b goto Ltrue goto L1

10 a < b or c < d and e < f hort Circuit valuation id 1 relop id 2.code = append( gen(if id 1.place relop id 2.place goto.true), gen(goto.false)) 1.true = Ltrue 1.false = L1 or.true = Ltrue.false = Lfalse 1.true = L2 1.false = Lfalse and 1 and 2 1.false =.false 1.true = newlabel 2.true = Ltrue 2.false = Lfalse 2.true =.true 2.false =.false.code = append( 1.code, 2.true = Ltrue 2.false = Lfalse gen( 1.true:), 2.code) a < b c < d e < f if a < b goto Ltrue goto L1 L1: if c < d goto L2 goto Lfalse L2:

11 a < b or c < d and e < f hort Circuit valuation id 1 relop id 2.code = append( gen(if id 1.place relop id 2.place goto.true), gen(goto.false)) 1.true = Ltrue 1.false = L1 or.true = Ltrue.false = Lfalse 1.true = L2 1.false = Lfalse and 1 and 2 1.false =.false 1.true = newlabel 2.true = Ltrue 2.false = Lfalse 2.true =.true 2.false =.false.code = append( 1.code, 2.true = Ltrue 2.false = Lfalse gen( 1.true:), 2.code) a < b c < d e < f if a < b goto Ltrue goto L1 L1: if c < d goto L2 goto Lfalse L2: if e < f goto Ltrue goto Lfalse

12 hort Circuit valuation a < b or c < d and e < f or and a < b c < d e < f if a < b goto Ltrue goto L1 L1: if c < d goto L2 goto Lfalse L2: if e < f goto Ltrue goto Lfalse

13 Combining Boolean and Control Flow tatements while a < b do if c < d then else x = y + z x = y - z while do 1.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin) while do if then then a < b c < d

14 while a < b do if c < d then else x = y + z CCI Compiler Design Combining Boolean and Control Flow tatements x = y - z while do 1.next = Lnext.begin = L1.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin)) while.true = L2 do.false = Lnext.next = L1 if then then a < b c < d

15 while while a < b do if c < d then else x = y + z x = y - z CCI Compiler Design Combining Boolean and Control Flow tatements.true = L2 do.false = Lnext while do 1.next = Lnext.begin = L1.next = L1.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin)) L1: if a < b goto L2 L2: goto Lnext if then then a < b c < d

16 while while a < b do if c < d then else x = y + z x = y - z CCI Compiler Design Combining Boolean and Control Flow tatements.true = L2 do.false = Lnext if while do 1.next = Lnext.begin = L1 then.true = L3.false = L4.next = L1 then.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin)) 1.next = L1 2.next = L1 L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 L3: goto L4 a < b c < d

17 while while a < b do if c < d then else x = y + z x = y - z a < CCI Compiler Design Combining Boolean and Control Flow tatements.true = L2 b do.false = Lnext if while do 1 c.next = Lnext.begin = L1 < d then.true = L3.false = L4.next = L1 then.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin)) 1.next = L1 2.next = L1 L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 goto L4 L3: t1 = x + z L4: x = t1 goto L1

18 while while a < b do if c < d then else x = y + z x = y - z a < CCI Compiler Design Combining Boolean and Control Flow tatements.true = L2 b do.false = Lnext if while do 1 c.next = Lnext.begin = L1 < d then.true = L3.false = L4.next = L1 then.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin)) 1.next = L1 2.next = L1 L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 goto L4 L3: t1 = x + z x = t1 goto L1 L4: t2 = x - z Lnext: x = t2 goto L1

19 while a < b do if c < d then else x = y + z x = y - z CCI Compiler Design Combining Boolean and Control Flow tatements while do 1.begin = newlabel.true = newlabel.false =.next 1.next =.begin.code = append(gen(.begin:),.code, gen(.true:), 1.code, gen(goto.begin)) L1: if a < b goto L2 goto Lnext while do if then then L2: if c < d goto L3 goto L4 L3: t1 = x + z a < b x = t1 goto L1 L4: t2 = x - z c < d x = t2 goto L1 Lnext:

20 Loop Constructs Loops valuate condition before loop (if needed) valuate condition after loop Branch back to the top (if needed) Pre-test Why this structure? Merges test with last block of loop body Pre-test block to hold loop-invariant code Post-test for increment instructions and test Loop head B1 B2 while, for, do, & until all fit this basic model Post-test Next block

21 Break & kip tatements Many modern programming languages include a break xits from the innermost control-flow statement Out of the innermost loop Out of a case statement Pre-test Translates into a jump Targets statement outside control- flow construct Creates multiple-exit construct skip in loop goes to next iteration Only make sense if loop has > 1 block Break in B1 Loop head B1 B2 Post-test Next block kip in B2

22 Break and kip tatements Need to Keep Track of nclosing Control-Flow Constructs Harder to have clean DT scheme Keep a tack of control-flow constructs Using.next as in the stack as the target for the break statement For skip statements need to keep track of the label of the code of the post-test block to advance to the next iteration. This is harder since the code has not been generated yet. Back-Patching helps Use a breaklist and a skiplist to be patched later.

23 Case tatements case or switch tatements emantics 1 valuate the controlling expression 2 Branch to the selected case 3 xecute the code for that case 4 Branch to the statement after the case Parts 1, 3, & 4 are well understood, part 2 is the key trategies Linear search (nested if-then-else constructs) Build a table of case expressions & binary search it Directly compute an address (requires dense case set) urprisingly many compilers do this for all cases!

24 switch begin case V 1 : 1 Case tatement: Code Layout code to valuate into t goto Ltest L 1 : code for 1 goto Lnext case V 2 : 2 case V n-1 : n-1 default: n end L 2 : code for 2 goto Lnext L n-1 : code for n-1 goto Lnext L n : code for n goto Lnext Ltest: if t = V 1 goto L 1 if t = V 2 goto L 2 Linear earch Lnext: if t = V n-1 goto L n-1 goto L n

25 DT scheme for Case tatements Issue: Need to ave the Labels and Values for the various cases for the test code at the end Use a Right-Recursive Grammar Use queue to save pairs (value, label) for generation of search code In the end pop values from queue to generate the linear search Use a Left-Recursive Grammar Cleaner; No queue is needed Use of the parsing stack to accumulate the non-terminals and corresponding attribute

26 Grammar and Actions switch List end.code = append(.code,gen( goto Ltest ),List.code,gen( Ltest: ) while(queue not empty) do { (vi,li) = pop.queue; if (vi = default).code = append(.code,gen( goto Li )); else.code = append(.code,gen( if t = vi goto Li )); Case case Value : Case.code = append(gen( Li: ),.code,gen( goto Lnext ); queue.push((value.val,li)); List Case ; List 1 List.code = append(case.code,list 1.code); List default : List.code = append(gen( Li: ),.code,gen( goto Lnext ); queue.push((default,li)) List ε List.code = append(gen( Li: ),gen( goto Lnext ); queue.push((default,li))

27 Other tatements Declarations Just save information in ymbol Table For tructures, Unions compute offsets for each field Function Calls Generate code to evaluate each argument in order into temporaries mit the call instruction using the temporary variables tructures, Variants Records and Unions Use the offsets from symbol table for address generation Unstructured Control-Flow Breaks the DT scheme, just use a global table and backpatch it.

28 Back- Patching ingle Pass olution to Code Generation? No more symbolic labels - symbolic addresses instead mit code directly into an array of instructions Actions associated with Productions xecuted when Bottom-Up Parser Reduces a production Problem Need to know the labels for target branches before actually generating the code for them. olution Leave Branches undefined and patch them later Requires: carrying around a list of the places that need to be patched until the value to be patched with is known.

29 Boolean xpressions Revisited Use Additional ε-production Just a Marker M Label Value M.addr Attributes:.truelist: code places that need to be filled-in corresponding to the evaluation of as true..falselist: same for false (1) 1 or M 2 (2) 1 and M 2 (3) not 1 (4) ( 1 ) (5) id 1 relop id 2 (6) true (7) false (8) M ε

30 Boolean xpressions: Code Outline 1 or 2 1 and 2 false 1.code true true 1.code false false 2.code true false 2.code true???

31 Auxiliary Functions Functions: makelist(i): make a list with the label i merge(p1,p2): creates a new list of labels with lists p1 and p2 backpatch(p,i): fills the locations in p with the address i newaddr() : returns a new symbolic address in sequence and increments the value for the next call Array of Instructions Linearly sequence of instructions Function emit to generate actual instructions in the array ymbolic Addresses

32 Using the Actions & List Attributes (5) id 1 relop id 2 {.truelist := makelist(newlabel);.falselist := makelist(newlabel); emit( if id 1.place relop id 2.place goto _ ); emit( goto _ ); } 100: if a < b then goto _ 101: goto _ 102:.truelist = {100}.falselist = {101} a < b

33 More Actions (3) not 1 {.truelist := 1.falselist;.falselist := 1.truelist; } (4) ( 1 ) {.truelist := 1.truelist;.falselist := 1.falselist; } (5) id 1 relop id 2 {.truelist := makelist(nextaddr());.falselist := makelist(nextaddr()); emit( if id 1.place relop.op id 2.place goto _ ); emit( goto _ ); } (6) true {.truelist := makelist(nextaddr()); emit( goto _ ); } (7) false {.falselist := makelist(nextaddr()); emit( goto _ ); }

34 Actions (1) 1 or M 2 { backpatch( 1.falselist,M.Addr);.truelist := merge( 1.truelist, 2.truelist);.falselist := 2.falselist; } (2) 1 and M 2 { backpatch( 1.truelist,M.Addr);.truelist := 2.truelist;.falselist := merge( 1.falselist, 2.falselist); } (8) M ε { M.Addr := nextaddr; }

35 Back-Patching xample xecuting Action Generated Code.truelist.falselist M M.addr.truelist =.falselist = a < or.truelist =.falselist = b M ε M.addr = and.truelist =.falselist =.truelist =.falselist = M ε M.addr = e <.truelist =.falselist = f c < d

36 .truelist.falselist M M.addr CCI Compiler Design Back-Patching xample xecuting Action {.truelist := makelist(nextquad());.falselist := makelist(nextquad()); emit( if id1.place relop.op id2.place goto _ ); emit( goto _ ); } Generated Code 100: if a < b goto _ 101: goto _.truelist =.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = and.truelist =.falselist =.truelist =.falselist = M ε M.addr = e <.truelist =.falselist = f c < d

37 .truelist.falselist M M.addr CCI Compiler Design Back-Patching xample xecuting Action { M.quad = nextquad(); } Generated Code 100: if a < b goto _ 101: goto _.truelist =.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist =.truelist =.falselist = M ε M.addr = e <.truelist =.falselist = f c < d

38 .truelist.falselist M M.addr CCI Compiler Design Back-Patching xample xecuting Action {.truelist := makelist(nextquad());.falselist := makelist(nextquad()); emit( if id1.place relop.op id2.place goto _ ); emit( goto _ ); } Generated Code 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _.truelist =.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist =.falselist = M ε M.addr = e <.truelist =.falselist = f c < d

39 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code { M.quad = nextquad(); } 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _.truelist =.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist =.falselist = M ε M.addr = 104 e <.truelist =.falselist = f c < d

40 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code {.truelist := makelist(nextquad()); 100: if a < b goto _.falselist := makelist(nextquad()); 101: goto _ emit( if id1.place relop.op id2.place goto _ ); 102: if c < d goto _ emit( goto _ ); } 103: goto _ 104: if e < f goto _.truelist = 105: goto _.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist =.falselist = M ε M.addr = 104 e <.truelist =.falselist = f {104} {105} c < d

41 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code { backpatch( 1.truelist,M.quad); 100: if a < b goto _.truelist := 2.truelist; 101: goto _.falselist := merge( 1.falselist, 2.falselist; } 102: if c < d goto _ 103: goto _ 104: if e < f goto _.truelist = 105: goto _.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist =.falselist = M ε M.addr = 104 e <.truelist =.falselist = f {104} {105} c < d

42 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code { backpatch( 1.truelist,M.quad); 100: if a < b goto _.truelist := 2.truelist; 101: goto _.falselist := merge( 1.falselist, 2.falselist; } 102: if c < d goto _ 103: goto _ 104: if e < f goto _.truelist = 105: goto _.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist =.falselist = M ε M.addr = 104 e <.truelist =.falselist = f {104} {105} c < d

43 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code { backpatch( 1.truelist,M.quad); 100: if a < b goto _.truelist := 2.truelist; 101: goto _.falselist := merge( 1.falselist, 2.falselist; } 102: if c < d goto : goto _ 104: if e < f goto _.truelist = 105: goto _.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist =.falselist = M ε M.addr = 104 e <.truelist =.falselist = f {104} {105} c < d

44 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code { backpatch( 1.truelist,M.quad); 100: if a < b goto _.truelist := 2.truelist; 101: goto _.falselist := merge( 1.falselist, 2.falselist; } 102: if c < d goto : goto _ 104: if e < f goto _.truelist = 105: goto _.falselist = a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist = {104}.falselist = {103, 105} M ε M.addr = 104 e <.truelist =.falselist = f {104} {105} c < d

45 .truelist.falselist M M.addr Back-Patching xample xecuting Action Generated Code { backpatch( 1.truelist,M.quad); 100: if a < b goto _.truelist := 2.truelist; 101: goto 102.falselist := merge( 1.falselist, 2.falselist; } 102: if c < d goto : goto _ 104: if e < f goto _.truelist = {100, 104} 105: goto _.falselist = {103, 105} a < or.truelist =.falselist = b {100} {101} M ε M.addr = 102 and.truelist =.falselist = {102} {103}.truelist = {104}.falselist = {103, 105} M ε M.addr = 104 e <.truelist =.falselist = f {104} {105} c < d

46 Back-Patching xample 100: if a < b goto _ 101: goto : if c < d goto : goto _ 104: if e < f goto _ 105: goto _ while M 1 do M 2 1 { backpatch( 1.nextlist, M 1.addr); backpatch(.truelist,m 2.addr);.nextlist :=.falselist; emit( goto M 1.addr ); }.truelist = {100, 104}.falselist = {103, 105}

47 Back-Patching xample 100: if a < b goto _ 101: goto : if c < d goto : goto _ 104: if e < f goto _ 105: goto _ while M 1 do M 2 1 { backpatch( 1.nextlist, M 1.addr); backpatch(.truelist,m 2.addr);.nextlist :=.falselist; emit( goto M 1.addr ); }.truelist = {100, 104}.falselist = {103, 105} 1

48 Back-Patching xample 100: if a < b goto _ 101: goto : if c < d goto : goto _ 104: if e < f goto _ 105: goto _ while M 1 do M 2 1 { backpatch( 1.nextlist, M 1.addr); backpatch(.truelist,m 2.addr);.nextlist :=.falselist; emit( goto M 1.addr ); }.truelist = {100, 104}.falselist = {103, 105} 1

49 Control Flow Code tructures if then 1 if then 1 else 2 while do 1.code.code.begin:.code.true: 1.code.true: 1.code.true: 1.code.false:....false: goto.next 2.code.false: goto.begin...next:....

50 Control Flow Constructs: Conditionals Add the nextlist attribute to and N denotes the set of locations in the code to be patched with the address that follows the execution of Can be either due to control flow or fall-through (1) if then M 1 1 N else M 2 2 { backpatch(.truelist, M 1.addr); backpatch(.falselist,m 2.addr);.nextlist := merge( 1.nextlist,merge(N.nextlist, 2.nextlist)); } (2) N ε { N.nextlist := makelist(nextaddr(); emit( goto _ ); } (3) M ε { M.quad := nextaddr; } (4) if then M 1 { backpatch(.truelist, M.addr);.nextlist := merge(.falselist, 1.nextlist); }

51 Control Flow Constructs: Loops (5) while M 1 do M 2 1 { backpatch( 1.nextlist, M 1.addr); backpatch(.truelist,m 2.addr);.nextlist :=.falselist; emit( goto M 1.addr ); }

52 equencing: List of tatements Additional ymbols L for list of statements for Assignment statement (6) begin L end {.nextlist = L.nextlist; } (7) A {.nextlist = nil; } (8) L L 1 ; M { backpatch(l 1.nextlist, M.addr); L.nextlist =.nextlist; } (9) L { L.nextlist =.nextlist; }

53 xtended xample L i = 0; L M 1 while (i < n) do begin if(a <= k) then a = a + 1; A ε while M 1 do M 2 1 ε ( ) ε begin L end id rel id i++; L 1 M 1 1 end ε A if then M 1 1 ( ) ε A id rel id

54 ummary Intermediate Code Generation Using yntax-directed Translation chemes Conditional Boolean using hort-circuit valuation Control-Flow Back-Patching Allows Code Generation in a ingle Pass

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2015, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

Module 26 Backpatching and Procedures

Module 26 Backpatching and Procedures Module 26 Backpatching and Procedures In this module, we would learn to generate three-address code for control flow statements using backpatching. We shall also discuss to combine Boolean expressions

More information

Intermediate Code Generation Part II

Intermediate Code Generation Part II 1 Intermediate Code Generation Part II Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 Advanced Intermediate Code Generation

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation 1 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting

More information

Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching

Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching Theory of Compilation 236360 rez Petrank Lecture 7: Intermediate Representation Part 2: ackpatching 1 You are here Compiler Source text txt Lexical Analysis Syntax Analysis Semantic Analysis Inter. Rep.

More information

Module 25 Control Flow statements and Boolean Expressions

Module 25 Control Flow statements and Boolean Expressions Module 25 Control Flow statements and Boolean Expressions In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 11a Intermediate Code Generation Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Declarations For each local name Creation of symbol-table entry Add type

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Announcements Weekly team meetings starting today Wednesday (4/4) will be a workshop Wednesday - Post questions you'd like addressed

More information

CMPSC 160 Translation of Programming Languages. Three-Address Code

CMPSC 160 Translation of Programming Languages. Three-Address Code CMPSC 160 Translation of Programming Languages Lectures 16: Code Generation: Three- Address Code Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Spring 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [25 points] Conser the grammar fragment below. It allows for the declaration of

More information

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation. UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Fall 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [30 points] In this problem you ned to develop a grammar for array variable expressions

More information

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

CMPT 379 Compilers. Anoop Sarkar.  11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code

More information

CS2210: Compiler Construction. Code Generation

CS2210: Compiler Construction. Code Generation Modern Compiler Project Fortran program Fortran s Lexer, Parser, and Static Checker Intermediate Code Generator IR MIPS Code Generator MIPS code C program C s Lexer, Parser, and Static Checker Intermediate

More information

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1 SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different

More information

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018 Compilerconstructie najaar 2018 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 7, vrijdag 2 november 2018 + werkcollege

More information

Intermediate Representa.on

Intermediate Representa.on IR Intermediate Representa.on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Intermediate Representation Language Specific Language + Machine Independent Machine Dependent

More information

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum IR Lowering CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 7 March 07 Use temporary variables for the translation Temporary variables in the Low IR store intermediate

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 5 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 IR Lowering Use temporary variables for the translation

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Midterm Exam - Spring 26 CSCI 565 - Compiler Design Spring 26 Midterm Exam March, 26 at : PM in class (KAP 47) Duration: 2h 3 min. Please label

More information

Chapter 6 Intermediate Code Generation

Chapter 6 Intermediate Code Generation Chapter 6 Intermediate Code Generation Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching Introduction Intermediate

More information

2 Intermediate Representation. HIR (high level IR) preserves loop structure and. More of a wizardry rather than science. in a set of source languages

2 Intermediate Representation. HIR (high level IR) preserves loop structure and. More of a wizardry rather than science. in a set of source languages Acknowledgements The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal Intermediate Code Generation... Front translates a source program into an intermediate representation

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

COP5621 Exam 3 - Spring 2005

COP5621 Exam 3 - Spring 2005 COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred

More information

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7 CS322 Languages and Compiler Design II Spring 2012 Lecture 7 1 USES OF BOOLEAN EXPRESSIONS Used to drive conditional execution of program sections, e.g. IF (a < 17) OR (b = 12) THEN... ELSE...; WHILE NOT

More information

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free

More information

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES

SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES UNIT 3 SYNTAX DIRECTED TRANSLATION FOR CONTROL STRUCTURES M.B.Chandak www.mbchandak.com hodcs@rknec.edu CONTROL STRUCTURES 1. If (condition) then Statement 2. If (condition) then Statement 1 else statement

More information

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl Compilerconstructie najaar 2013 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs(dot)nl werkcollege 6, dinsdag 22 oktober 2013 Intermediate Code

More information

Module 27 Switch-case statements and Run-time storage management

Module 27 Switch-case statements and Run-time storage management Module 27 Switch-case statements and Run-time storage management In this module we will discuss the pending constructs in generating three-address code namely switch-case statements. We will also discuss

More information

Formal Languages and Compilers Lecture X Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Semantic Actions and 3-Address Code Generation

Semantic Actions and 3-Address Code Generation Compiler Design 1 Semantic Actions and 3-Address Code Generation Compiler Design 2 Introduction We start with different constructs of the given grammar (laboratory assignment-5) and discuss semantic actions

More information

CS 432 Fall Mike Lam, Professor. Code Generation

CS 432 Fall Mike Lam, Professor. Code Generation CS 432 Fall 2015 Mike Lam, Professor Code Generation Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00

More information

Languages and Compiler Design II IR Code Generation I

Languages and Compiler Design II IR Code Generation I Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

Implementing Control Flow Constructs Comp 412

Implementing Control Flow Constructs Comp 412 COMP 412 FALL 2018 Implementing Control Flow Constructs Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Mterm Exam Solution - Spring 2014 CSCI 565 - Compiler Design Spring 2014 Mterm Exam - Solution Problem 1: Context-Free-Grammars and Parsing Algorithms

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Intermediate-Code Genera=on Intermediate representa=ons

More information

Alternatives for semantic processing

Alternatives for semantic processing Semantic Processing Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all of this work for personal or classroom use is granted without fee provided that copies

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

Exercises II. Exercise: Lexical Analysis

Exercises II. Exercise: Lexical Analysis xercises II Text adapted from : Alessandro Artale, Free University of Bolzano les adapted from : nrico Cimitan, Università di Padova xercise: Lexical Analysis Describe the notions of token, token name,

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer

More information

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015 Boolean Expressions Lecture 31 Sections 6.6, 6.7 Robb T. Koether Hampden-Sydney College Wed, Apr 8, 2015 Robb T. Koether (Hampden-Sydney College) Boolean Expressions Wed, Apr 8, 2015 1 / 22 1 Relational

More information

Lecture 15 CIS 341: COMPILERS

Lecture 15 CIS 341: COMPILERS Lecture 15 CIS 341: COMPILERS Announcements HW4: OAT v. 1.0 Parsing & basic code generation Due: March 28 th No lecture on Thursday, March 22 Dr. Z will be away Zdancewic CIS 341: Compilers 2 Adding Integers

More information

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design PSD3A Principles of Compiler Design Unit : I-V 1 UNIT I - SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler -

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009 Lecture 25 Sections 8.4, 8.6 Hampden-Sydney College Fri, Apr 24, 2009 Outline 1 2 3 Definition (Equality and ) The equality operators are the operators == and!=. The relational operators are the operators

More information

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

& Simulator. Three-Address Instructions. Three-Address Instructions. Three-Address Instructions. Structure of an Assembly Program.

& Simulator. Three-Address Instructions. Three-Address Instructions. Three-Address Instructions. Structure of an Assembly Program. & Simulator Copyright 2011, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California (USC) have explicit permission to make copies of these

More information

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

Syntactic Directed Translation

Syntactic Directed Translation Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei shift/reduce conflict with

More information

Three-address code (TAC) TDT4205 Lecture 16

Three-address code (TAC) TDT4205 Lecture 16 1 Three-address code (TAC) TDT4205 Lecture 16 2 On our way toward the bottom We have a gap to bridge: Words Grammar Source program Semantics Program should do the same thing on all of these, but they re

More information

Dixita Kagathara Page 1

Dixita Kagathara Page 1 2014 Sem-VII Intermediate Code Generation 1) What is intermediate code? Intermediate code is: The output of the parser and the input to the Code Generator. Relatively machine-independent: allows the compiler

More information

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures

More information

CSCI Compiler Design

CSCI Compiler Design University of Southern California CSCI565 Compiler Design Midterm Exam - Fall 26 CSCI 565 - Compiler Design Fall 26 Midterm Exam Solution Problem : Context-Free-Grammars and Parsing Algorithms [4 points]

More information

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator CS571 Notes 16 Denotational Semantics of a Simple Calculator The Calculator Two functions: + and * Unbounded natural numbers (no negatives Conditional: if-then-else Parentheses One memory register 1of

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

More information

UNIT IV INTERMEDIATE CODE GENERATION

UNIT IV INTERMEDIATE CODE GENERATION UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of

More information

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations Register Allocation Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class

More information

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)

More information

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target. COMP 412 FALL 2017 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

More information

THE CALCULATOR SYNTAX

THE CALCULATOR SYNTAX TH CALCULATOR After looking at the basics of the denotational method with the binary numerals we will take chmidt s example of a calculator to bring us one step closer to looking at the semantics of a

More information

CSc 453 Intermediate Code Generation

CSc 453 Intermediate Code Generation CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson Overview Intermediate representations span the gap between the source and target languages: closer to target language;

More information

CIS 341 Midterm March 2, 2017 SOLUTIONS

CIS 341 Midterm March 2, 2017 SOLUTIONS CIS 341 Midterm March 2, 2017 SOLUTIONS 1 1. True or False (14 points) Mark each statement as either true or false. a. T F The typical compiler consists of several phases, including: lexing, parsing, transformation

More information

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures Control Structures

More information

Computing Inside The Parser Syntax-Directed Translation. Comp 412

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL 2018 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights

More information

Lecture 15-16: Intermediate Code-Generation

Lecture 15-16: Intermediate Code-Generation Lecture 15-16: Intermediate Code-Generation Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (16/11/17) Lecture 15-16: Intermediate Code-Generation 2017-2018 1 /

More information

Oberon2 Compiler CS335: Compiler Project

Oberon2 Compiler CS335: Compiler Project Oberon2 Compiler CS335: Compiler Project Ashish Gupta (Y8140) Shitikanth (Y8480) Anindya Jyoti Roy (Y8078) Rajeev Rathore (Y8398) Sanjay (Y8448) April 16, 2011 1 / 12 Characterization Source Language(S)

More information

Intermediate Representations & Symbol Tables

Intermediate Representations & Symbol Tables Intermediate Representations & Symbol Tables Copyright 2014, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission

More information

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz Compiler Design Fall 2015 Control-Flow Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292

More information

PRINCIPLES OF COMPILER DESIGN

PRINCIPLES OF COMPILER DESIGN PRINCIPLES OF COMPILER DESIGN 2 MARK QUESTIONS WITH ANSWERS UNIT I 1. What is a Complier? A Complier is a program that reads a program written in one language-the source language-and translates it in to

More information

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers. Control Flow COMS W4115 Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science Control Flow Time is Nature s way of preventing everything from happening at once. Scott

More information

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science Control Flow COMS W4115 Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science Control Flow Time is Nature s way of preventing everything from happening at once. Scott identifies

More information

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target. COMP 412 FALL 20167 Computing Inside The Parser Syntax-Directed Translation, II Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all

More information

intermediate-code Generation

intermediate-code Generation intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language

More information

Introduction to Parsing. Lecture 5

Introduction to Parsing. Lecture 5 Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Intermediate codes are machine independent codes, but they are close to machine instructions The given program in a source language is converted to an equivalent program in

More information

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation Prelude COMP 181 Lecture 14 Intermediate representations and code generation October 19, 2006 Who is Seth Lloyd? Professor of mechanical engineering at MIT, pioneer in quantum computing Article in Nature:

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Translating source program into an intermediate language. Simple CPU Indepent, yet, close in spirit to machine language. Three Address Code (quadruples) Two Address Code, etc.

More information

The Software Stack: From Assembly Language to Machine Code

The Software Stack: From Assembly Language to Machine Code COMP 506 Rice University Spring 2018 The Software Stack: From Assembly Language to Machine Code source code IR Front End Optimizer Back End IR target code Somewhere Out Here Copyright 2018, Keith D. Cooper

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

5. Semantic Analysis!

5. Semantic Analysis! 5. Semantic Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!

More information

CSCI 565: Compiler Design and Implementation Spring 2006

CSCI 565: Compiler Design and Implementation Spring 2006 CSCI 565: Compiler Design and Implementation Spring 2006 Midterm Exam Solution Feb. 28, 2006 Problem 1: Attributive Grammar and Syntax Directed Translation [30 points] A language such as C allows for user

More information

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

CPSC 3740 Programming Languages University of Lethbridge. Control Structures Control Structures A control structure is a control statement and the collection of statements whose execution it controls. Common controls: selection iteration branching Control Structures 1 15 Howard

More information

Chapter 6 Control Flow. June 9, 2015

Chapter 6 Control Flow. June 9, 2015 Chapter 6 Control Flow June 9, 2015 Expression evaluation It s common in programming languages to use the idea of an expression, which might be a simple object function invocation over some number of arguments

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum Optimizations CS412/CS413 Introduction to Compilers im eitelbaum Lecture 24: s 24 Mar 08 Code transformations to improve program Mainly: improve execution time Also: reduce program size Can be done at

More information

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012 Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

More information

POLITECNICO DI TORINO. (01JEUHT) Formal Languages and Compilers. Laboratory N 3. Lab 3. Cup Advanced Use

POLITECNICO DI TORINO. (01JEUHT) Formal Languages and Compilers. Laboratory N 3. Lab 3. Cup Advanced Use POLITCNICO DI TORINO (01JUHT) Laboratory N 3 tefano canzio Mail: Web: http://www.skenz.it/compilers 1 Cup Advanced Use Grammars with ambiguities s Operator precedence Handling syntax errors 2 Ambiguous

More information

Compiler Design. Code Shaping. Hwansoo Han

Compiler Design. Code Shaping. Hwansoo Han Compiler Design Code Shaping Hwansoo Han Code Shape Definition All those nebulous properties of the code that impact performance & code quality Includes code, approach for different constructs, cost, storage

More information

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline. The Main Idea of Today s Lecture Code Generation We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

We can emit stack-machine-style code for expressions via recursion

We can emit stack-machine-style code for expressions via recursion Code Generation The Main Idea of Today s Lecture We can emit stack-machine-style code for expressions via recursion (We will use MIPS assembly as our target language) 2 Lecture Outline What are stack machines?

More information

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations. Lecture 20 Intermediate Code & Local Optimizations Lecture 20 Lecture Outline Intermediate code Local optimizations Next time: global optimizations 2 Code Generation Summary We have discussed Runtime organization

More information