Intermediate Code Generation
|
|
- Kerry Perry
- 6 years ago
- Views:
Transcription
1 Intermediate Code Generation 1
2 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting is facilitated Machine independent code optimization can be applied 2
3 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 an intermediate language by the intermediate code generator Intermediate language can be many different languages, and the designer of the compiler decides this intermediate language Syntax trees can be used as an intermediate language Postfix notation can be used as an intermediate language Three-address code (Quadruples) can be used as an intermediate language we will use quadruples to discuss intermediate code generation quadruples are close to machine instructions, but they are not actual machine instructions Some programming languages have well defined intermediate languages java java virtual machine prolog Warren abstract machine In fact, there are byte-code emulators to execute instructions in these intermediate languages 3
4 A Tree and A DAG as Intermediate Languages assign assign a + a + b uminus b uminus b uminus Pro: Con: c c easy restructuring of code and/or expressions for intermediate code optimization memory intensive c 4
5 Postfix Notation a := b -c + b -c a b c uminus b c uminus + assign Pro: Con: Postfix notation represents operations on a stack easy to generate stack operations are more difficult to optimize Bytecode (for example) iload 2 // push b iload 3 // push c ineg // uminus imul // iload 2 // push b iload 3 // push c ineg // uminus imul // iadd // + istore 1 // store a 5
6 Two Representations of A Syntax Tree id b uminus id c assign id a + id b uminus id c 0 id b 1 id c 2 uminus id b 5 id c 6 uminus id a 10 assign
7 Three-Address Code assign x:= x:= y y op op z z assign a + Tree Dag a + b uminus b uminus t t 1 :=-c 1 :=-c t t 2 :=bt 2 :=bt 1 1 t t 3 :=-c 3 :=-c t t 4 :=bt 4 :=bt 3 3 t t 1 :=-c 1 :=-c t t 2 :=bt 2 :=bt 1 1 t t 5 :=t 5 :=t 2 +t 2 +t 2 2 a:=t a:=t 5 5 b uminus t t 5 :=t 5 :=t 2 +t 2 +t 4 4 c c a:=t a:=t 5 5 c Three-address code is a linearization of the tree/dag 7
8 Types of Three-Address Code Instructions Binary operations: x:=y op z Unary operations: x:=op y Copy instructions: x:=y Conditional jumps: if x relop y goto L Procedure calls: param x 1 param x 2 param x n call p,n Index assignments: x:=y[i],x[i]:=y Address and pointer assignments: x:=&y, x:=y, x:=y 8
9 Syntax-Directed Translation Into 3-address Code First deal with assignments and simple expressions Use attributes E.place: the name that will hold the value of E Identifier will be assumed to already have the place attribute defined E.code: hold the three address code statements that evaluate E Use function newtemp that returns a new temporary variable that we can use Use function gen to generate a single three address statement given the necessary information (variable names and operations) 9
10 The gen Function gen(e.place := E 1.place + E 2.place) Code generation t3 := t1 + t2 10
11 Code Generation for Expressions Production S id:=e E E 1 +E 2 E E 1 E 2 E -E 1 E ( E 1 ) E id Semantic Rules S.code:=E.code gen(id.place ':=' E.place) E.place:=newtemp; E.code:=E 1.code E 2.code gen(e.place ':=' E 1.place '+' E 2.place) E.place:=newtemp; E.code:=E 1.code E 2.code gen(e.place ':=' E 1.place '' E 2.place) E.place:=newtemp; E.code:=E 1.code gen(e.place ':=' 'uminus' E 1.place) E.place:=E 1.place; E.code:=E 1.code E.place := id.place E.code:='' 11
12 Code Generation for while Loops Production S while E do S 1 Semantic Rules S.begin:=newlabel; S.after:=newlabel; S.code:=gen(S.begin ':') E.code gen('if' E.place '=' '0' 'goto' S.after) S 1.code gen('goto' S.begin) gen(s.after ':') S.begin : E.code if E.place = 0 goto S.after S 1.code S.after : goto S.begin 12
13 Example i := 2 n + k while i do i := i - k t1 t1 := := 2 2 t2 t2 := := t1 t1 n n t3 t3 := := t2 t2 + + k k i i := := t3 t3 L1: L1: if if i i = = 0 0 goto goto L2 L2 t4 t4 := := i i - - k k i i := := t4 t4 goto goto L1 L1 L2: L2: 13
14 Quadruple Representation of Three-Address Statements (0) op uminus arg1 c arg2 result t 1 t 1 t :=- 1 :=c c (1) (2) (3) (4) uminus + b c b t 2 t 1 t 3 t 4 t 2 t 3 t 4 t 5 t 2 t :=b 2 :=b t 1 t 1 t 3 t :=- 3 :=c c t 4 t :=b 4 :=b t 3 t 3 t 5 t :=t 5 :=t t 4 t 4 a:=t a:=t 5 5 (5) := t 5 a Temporary names must be entered into the symbol table as they are created 14
15 Triple Representation of Three-Address Statements (0) op uminus arg1 c arg2 t 1 t :=- 1 :=c c (1) (2) (3) uminus b c b (0) (2) t 2 t :=b 2 :=b t 1 t 1 t 3 t :=- 3 :=c c t 4 t :=b 4 :=b t 3 t 3 t 5 t :=t 5 :=t t 4 t 4 (4) + (1) (3) a:=t a:=t 5 5 (5) assign a (4) Temporary names are not entered into the symbol table 15
16 Other Types of 3-address Statements Ternary operations like x[i]:=y x:=y[i] require two or more entries: op arg1 arg2 op arg1 arg2 (0) []= x i (0) =[] y i (1) assign (0) y (1) assign x (0) x[i] := y x := y[i] 16
17 Indirect Triples Representation of Three-Address Statements Instruction op arg1 arg2 (0) (14) (14) uminus c (1) (15) (15) b (14) (2) (16) (16) uminus c (3) (17) (17) b (16) (4) (18) (18) + (15) (17) (5) (19) (19) assign a (18) 17
18 Declarations P { offset := 0 } D D D ; D D id : T { enter(id.name, T.type, offset); offset := offset + T.width } T char { T.type := char; T.width := 1 } T integer { T.type := integer; T.width := 4 } T real { T.type := real; T.width := 8 } T ^T 1 { T.type := pointer(t 1.type); T.width := 4 } T array [ num ] of T 1 { T.type := array(1..num.val, T 1.type) ; T.width := num.val T 1.width } name a b c type char pointer(real) real offset
19 Nested Procedure Declarations For each procedure we should create a symbol table. mktable(previous) create a new symbol table where previous is the parent symbol table of this new symbol table enter(symtable,name,type,offset) create a new entry for a variable in the given symbol table. enterproc(symtable,name,newsymbtable) create a new entry for the procedure in the symbol table of its parent. addwidth(symtable,width) puts the total width of all entries in the symbol table into the header of that table. We will have two stacks: tblptr to hold the pointers to the symbol tables offset to hold the current offsets in the symbol tables in tblptr stack. 19
20 Symbol Table for Nested Procedures sort null a x header readarray exchange to readarray's symbol table to exchange's symbol table quicksort readarray exchange quicksort i header header k v partition header i j header partition 20
21 Nested Procedure Declarations Production P MD M D D 1 ;D 2 D proc id; ND 1 ; S D id: T N Semantic Rules addwidth(top(tblptr), top(offset)); pop(tblptr); pop(offset) t:=mktable(nil); push(t, tblptr); push(0,offset) t:=top(tblptr); addwidth(t,top(offset)); pop(tblptr); pop(offset); enterproc(top(tblptr), id.name, t) enter(top(tblptr), id.name, T.type, top(offset)); top(offset) := top(offset)+t.width t:=mktable(top(tblptr)); push(t,tblptr); push(0,offset) 21
22 Code Generation for Expressions Production S id:=e E E 1 +E 2 E E 1 E 2 E - E 1 E ( E 1 ) E id Semantic Rules p:=lookup(id.name); if p<> nil then emit(p ':=' E.place) else error E.place:=newtemp; emit(e.place ':=' E 1.place '+' E 2.place) E.place:=newtemp; emit(e.place ':=' E 1.place '' E 2.place) E.place:=newtemp; emit(e.place ':=' 'uminus' E 1.place) E.place := E 1.place p:= lookup(id.name); if p<> nil then E.place:=p else error 22
23 Addresing of Array Elements A[low..high] A[i] base + (i - low) w i w + (base - loww) A[low 1..high 1, low 2..high 2 ] A[i 1,i 2 ] n 2 = high 2 - low base + ( (i 1 - low 1 ) n 2 + i 2 - low 2 ) w ( (i 1 n 2 + i 2 ) w + (base - ( (low 1 n 2 ) + low 2 ) w) 23
24 Conversion of Types E E 1 + E 2 { E.place := newtemp; if E 1.type = integer and E 2.type = integer then begin emit (E.place ':=' E 1.place 'int+' E 2.place); E.type := integer; end else if E 1.type = real and E 2.type = real then begin emit (E.place ':=' E 1.place 'real+' E 2.place); E.type := real; end else if E 1.type = integer and E 2.type = real then begin u := newtemp; emit (u ':=' 'inttoreal' E 1.place); emit (E.place ':=' u 'real+' E 2.place ); E.type := real; end else if E 1.type = real and E 2.type = integer then begin u := newtemp; emit (u ':=' 'inttoreal' E 2.place); emit (E.place ':=' E 1.place 'real+' u); E.type := real; end else E.type := type_error } x := y + i j t 1 := i int j t 3 := inttoreal t 1 t 2 := y real+ t 3 x := t 2 24
25 Boolean Expressions E E or E E and E not E ( E ) id relop id true false a or b and not c t 1 := not c t 2 := b and t 1 t 3 := a or t 2 a < b if a < b then 1 else 0 100: if a < b goto : t := 0 102: goto : t := 1 104: 25
26 Boolean Expressions E E 1 or E 2 { E.place := newtemp; emit (E.place ':=' E 1.place 'or' E 2.place) } E E 1 and E 2 { E.place := newtemp; emit (E.place ':=' E 1.place 'and' E 2.place) } E not E 1 { E.place := newtemp; emit (E.place ':=' 'not' E 1.place) } E ( E 1 ) { E.place := E 1.place } E id 1 relop id 2 { E.place := newtemp; emit ('if' id 1.place relop.op id 2.place 'goto' nextstat + 3); emit (E.place ':=' '0') emit ('goto' nextstat + 2) emit (E.place ':=' '1') } E true { E.place := newtemp; emit (E.place ':=' '1') } E false { E.place := newtemp; emit (E.place ':=' '0') } a < b or c < d and e < f 100: if a < b goto : t 1 := 0 102: goto : t 1 := 1 104: if c < d goto : t 2 := 0 106: goto : t 2 := 1 108: if e < f goto : t 3 := 0 110: goto : t 3 := 1 112: t 4 := t 2 and t 3 113: t 5 := t 1 or t 4 26
27 Control Instructions S if E then S 1 S if E then S 1 else S 2 do E.true E.code do E.false do E.true E.true : E.false : S 1.code E.true : E.code S 1.code do E.false S.begin : S while E do S 1 E.code do E.true do E.false E.false : S.next : goto S.next S 2.code E.true : S 1.code E.false : goto S.begin 27
28 S if E then S 1 Dyntax-Directed Definition for Flow-of-Control Statements E.true := newlabel; E.false := S.next; S 1.next := S.next; S.code := E.code gen(e.true ':') S 1.code E.true : E.false : E.code S 1.code do E.true do E.false S if E then S 1 else S 2 E.true := newlabel; E.false := newlabel; S 1.next := S.next; S 2.next := S.next; S.code := E.code gen(e.true ':') S 1.code gen('goto' S.next) gen(e.false ':') S 2.code E.true : E.false : E.code S 1.code goto S.next S 2.code do E.true do E.false S while E do S 1 S.begin := newlabel; E.true := newlabel; E.false := S.next S 1.next := S.begin; S.code := gen(s.begin ':') E.code gen(e.true ':') S 1.code gen('goto' S.begin) S.next : S.begin : E.code E.true : S 1.code do E.true do E.false E.false : goto S.begin 28
29 Boolean Expression - "Short-Circuit" Translation E E 1 or E 2 E E 1 and E 2 E not E 1 E ( E 1 ) E 1.true := E.true; E 1.false := newlabel; E 2.true := E.true; E 2.false := E.false; E.code := E 1.code gen(e 1.false ':') E 2.code E 1.true := newlabel; E 1.false := E.false; E 2.true := E.true; E 2.false := E.false; E.code := E 1.code gen(e 1.true ':') E 2.code E 1.true := E.false; E 1.false := E.true; E.code := E 1.code; E 1.true := E.true; E 1.false := E.false; E.code := E 1.code; E id 1 relop id 2 E.code := gen( 'if' id 1.place relop.op id 2.place 'goto' E.true) gen( 'goto' E.false ) Code for E= a<b: if a < b goto E.true goto E.false E true E.code := gen( 'goto' E.true ) E false E.code := gen( 'goto' E.false ) 29
30 Boolean Expression - "Short-Circuit" Translation a < b or c < d and 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 while a < b do if c < d then x := y + z else x := y - z L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 goto L4 L3: t 1 := y + z x := t 1 goto L 1 L4: t 2 := y - z x := t 2 goto L 1 Lnext: 30
31 Boolean Expressions - Mixed-Mode (a + b) < c (a < b) + (b < a) E E + E E and E E relop E id E E 1 + E 2 E.type := arith; if E 1.type = arith and E 2.type = arith then begin / arithmetic addition / E.place := newtemp; E.code := E 1.code E 2.code gen(e.place ':=' E 1.place '+' E 2.place) end else if E 1.type = arith and E 2.type = bool then begin E.place := newtemp; E 2.true := newlabel; E 2.false := newlabel; E.code := E 1.code E 2.code gen(e 2.true ':' E.place ':=' E 1.place '+' 1) gen('goto' nextstat + 1) gen(e 2.false ':' E.place ':=' E 1.place) else if... E 2.true : E.place := E 1.place + 1 goto nextstat + 1 E 2.false : E.place := E 1.place 31
32 Translating Short-Circuit Expressions Using Backpatching For the examples of the previous lectures for implementing syntax-directed definitions, the easiest way is to use two passes. First syntax tree is constructed and is then traversed in depth-first order to compute the translations given in the definition The main problem in generating three address codes in a single pass for Boolean expressions and flow of control statements is that we may not know the labels that control must go to at the time jump statements are generated 32
33 Translating Short-Circuit Expressions Using Backpatching This problem is solved by generating a series of branch statements with the targets of the jumps temporarily left unspecified. Each such statement will be put on a list of goto statements whose labels will be filled in when the proper label can be determined. This subsequent filling of addresses for the determined labels is called BACKPATCHING. 33
34 Syntax-Directed Definition for Backpatching E E or M E E and M E not E ( E ) id relop id true false M ε Synthesized attributes: E.code three-address code E.truelist backpatch list for jumps on true E.falselist backpatch list for jumps on false M.quad location of current three-address quad 34
35 Backpatch Operations with Lists makelist(i) creates a new list containing threeaddress location i, returns a pointer to the list merge(p 1, p 2 ) concatenates lists pointed to by p 1 and p 2, returns a pointer to the concatenated list backpatch(p, i) inserts i as the target label for each of the statements in the list pointed to by p 35
36 Backpatching with Lists: Example a < b or c < d and e < f 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _ backpatch 100: if a < b goto TRUE 101: goto : if c < d goto : goto FALSE 104: if e < f goto TRUE 105: goto FALSE 36
37 Backpatching with Lists: Translation Scheme M ε { M.quad := nextquad } E E 1 or M E 2 { backpatch(e 1.falselist, M.quad); E.truelist := merge(e 1.truelist, E 2.truelist); E.falselist := E 2.falselist } E E 1 and M E 2 { backpatch(e 1.truelist, M.quad); E.truelist := E 2.truelist; E.falselist := merge(e 1.falselist, E 2.falselist); } E not E 1 { E.truelist := E 1.falselist; E.falselist := E 1.truelist } E ( E 1 ) { E.truelist := E 1.truelist; E.falselist := E 1.falselist } E id 1 relop id 2 E true E false { E.truelist := makelist(nextquad); E.falselist := makelist(nextquad + 1); emit( if id 1.place relop.op id 2.place goto _ ); emit( goto _ ) } { E.truelist := makelist(nextquad); E.falselist := nil; emit( goto _ ) } { E.falselist := makelist(nextquad); E.truelist := nil; emit( goto _ ) } 37
38 Backpatching Example E.t = {100} E.f = {101} a < b E.t = {100,104} E.f = {103,105} or M.q = {102} ε E.t = {102} E.f = {103} E.t = {104} E.f = {103,105} and M.q = {104} ε 100: if a < b goto _ 101: goto _ 102: if c < d goto _ 103: goto _ 104: if e < f goto _ 105: goto _ E.t = {104} E.f = {105} c < d e < f 38
39 Flow-of-Control Statements and Backpatching: Grammar S if E then S if E then S else S while E do S begin L end A L L ; S S Synthesized attributes: S.nextlist backpatch list for jumps to the next statement after S (or nil) L.nextlist backpatch list for jumps to the next statement after L (or nil) Jumps out of S 1 100: Code for S1 S 1 ; S 2 ; S 3 ; S 4 ; S 4 backpatch(s 1.nextlist, 200) 200: Code for S2 300: Code for S3 backpatch(s 2.nextlist, 300) 400: Code for S4 backpatch(s 3.nextlist, 400) 500: Code for S5 backpatch(s 4.nextlist, 500) 39
40 Flow-of-Control Statements and Backpatching S A { S.nextlist := nil } S begin L end { S.nextlist := L.nextlist } S if E then M S 1 { backpatch(e.truelist, M.quad); S.nextlist := merge(e.falselist, S 1.nextlist) } L L 1 ; M S { backpatch(l 1.nextlist, M.quad); L.nextlist := S.nextlist; } L S { L.nextlist := S.nextlist; } M ε { M.quad := nextquad } S if E then M 1 S 1 N else M 2 S 2 { backpatch(e.truelist, M 1.quad); backpatch(e.falselist, M 2.quad); S.nextlist := merge(s 1.nextlist, merge(n.nextlist, S 2.nextlist)) } S while M 1 E do M 2 S 1 { backpatch(s 1,nextlist, M 1.quad); backpatch(e.truelist, M 2.quad); S.nextlist := E.falselist; emit( goto M 1.quad ) } N ε { N.nextlist := makelist(nextquad); emit( goto _ ) } 40
ΕΠΛ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 informationModule 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 informationCMPSC 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 informationIntermediate 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 information2 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 informationUNIT-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 informationFormal 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 informationCS2210: 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 informationPrinciples 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 informationDixita 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 informationModule 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 informationPrinciple 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 informationPART 6 - RUN-TIME ENVIRONMENT. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 6 - RUN-TIME ENVIRONMENT F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 188 / 309 Objectives/Tasks Relate static source code to actions at program runtime. Names in the source code
More informationNARESHKUMAR.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 informationFormal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table
Formal Languages and Compilers Lecture IX Semantic Analysis: Type Checking & Symbol Table Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationSyntax-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 informationModule 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 informationIntermediate 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 informationIntermediate 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 informationIntermediate Code Generation
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
More informationTHEORY 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 informationCompilerconstructie. 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 informationCSE443 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 informationTheory 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 informationConcepts Introduced in Chapter 6
Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler
More informationIntermediate 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 informationintermediate-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 informationIntermediate 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 informationChapter 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 informationCMPT 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 informationIntermediate 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 informationCOP5621 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 informationConcepts Introduced in Chapter 6
Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler
More informationIntermediate 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 informationComputer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres
Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Compiler Architecture Source language Scanner (lexical analysis) tokens Parser (syntax
More informationIntermediate 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 informationUNIT 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 informationCSCI 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 informationTime : 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 informationCSc 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 informationIntermediate 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 informationGenerating 3-Address Code from an Attribute Grammar
Generating 3-Address Code from an Attribute Grammar Here is a small but realistic programming language with integer and boolean data, loops and conditionals, procedures with no arguments that compute but
More informationIntermediate 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 informationCSCI565 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 informationControl 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 informationCSE443 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 informationSYNTAX 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 informationPRINCIPLES 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 informationComputer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres
Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Introduction He am a driver might be syntactically correct but semantically wrong. Semantic
More informationSemantic 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 informationSemantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.
SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens
More informationCOMPILER CONSTRUCTION (Intermediate Code: three address, control flow)
COMPILER CONSTRUCTION (Intermediate Code: three address, control flow) Prof. K R Chowdhary Email: kr.chowdhary@jietjodhpur.ac.in Campus Director, JIET, Jodhpur Thursday 18 th October, 2018 kr chowdhary
More informationProf. 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 informationCompilerconstructie. 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 informationCS322 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 informationCSc 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 informationLanguages 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 informationAcknowledgement. 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 informationCSCI565 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 informationCSE443 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 Announcements Be sure to
More informationCompiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)
Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while
More informationIntermediate 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 informationCMSC430 Spring 2014 Midterm 2 Solutions
CMSC430 Spring 2014 Midterm 2 Solutions 1. (12 pts) Syntax directed translation & type checking Consider the following grammar fragment for an expression for C--: exp CONST IDENT 1 IDENT 2 [ exp 1 ] Assume
More informationIntermediate 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 informationType Checking. Chapter 6, Section 6.3, 6.5
Type Checking Chapter 6, Section 6.3, 6.5 Inside the Compiler: Front End Lexical analyzer (aka scanner) Converts ASCII or Unicode to a stream of tokens Syntax analyzer (aka parser) Creates a parse tree
More informationIntermediate Representations
Compiler Design 1 Intermediate Representations Compiler Design 2 Front End & Back End The portion of the compiler that does scanning, parsing and static semantic analysis is called the front-end. The translation
More informationCSc 453. Compilers and Systems Software. 13 : Intermediate Code I. Department of Computer Science University of Arizona
CSc 453 Compilers and Systems Software 13 : Intermediate Code I Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Introduction Compiler Phases
More informationECS 142 Project: Code generation hints (2)
ECS 142 Project: Code generation hints (2) Winter 2011 This document describes the nature of final code generation for your project. I have made a number of assumptions here, mostly in terms of availability
More informationObject Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code
Code Generation Intermediate Code? Assembly Code Object Code (Machine Code) 1 Intermediate Code P-Code Three Address Code 2 Compiler Construction F6S 1 Intermediate Representation Abstract Syntax Tree
More informationIntermediate-Code Generat ion
Chapter 6 Intermediate-Code Generat ion 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
More informationPrinciple 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 informationopt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into
Intermediate representations source code front end ir opt. ir back end target code front end produce an intermediate representation (IR) for the program. optimizer transforms the code in IR form into an
More informationAlternatives 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 informationIR 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 informationCS412/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 informationCompilers. 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 informationCS 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 informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More informationSemantic 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 informationCompilers. 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 informationPSD3A 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 informationThe analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.
COMPILER DESIGN 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the target
More informationSemantic Analysis and Intermediate Code Generation
DDD55 Compilers and Interpreters DDB44 Compiler Construction Semantic Analysis and Intermediate Code Generation Semantic Analysis and Intermediate Code Generation able management source program Lexical
More informationSyntax-Directed Translation Part II
Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed
More informationCompiler. --- Intermediate Code Generation. Zhang Zhizheng.
Compiler --- Intermediate Code Generation Zhang Zhizheng seu_zzz@seu.edu.cn School of Computer Science and Engineering, Software College Southeast University 2013/12/12 Zhang Zhizheng, Southeast University
More informationType Checking. Error Checking
Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationCompiler Construction Assignment 4 Spring 2017
Compiler Construction Assignment 4 Spring 2017 Robert van Engelen µc Part 2 In this programming assignment we complete our µc compiler. We adopt the common C scoping rules for local variable declarations
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 24, 2007 Outline Recap Three
More informationCSCI 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 informationSyntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?
Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the
More informationVIVA QUESTIONS WITH ANSWERS
VIVA QUESTIONS WITH ANSWERS 1. What is a compiler? A compiler is a program that reads a program written in one language the source language and translates it into an equivalent program in another language-the
More informationContext-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis
Semantic Processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure
More informationBoolean 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 informationLecture 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 informationJava: framework overview and in-the-small features
Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: framework overview and in-the-small features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer
More informationIntermediate representation
Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing HIR semantic analysis HIR intermediate code gen.
More informationIntermediate Code Generation Part II
Intermediate Code Generation Part II Chapter 6: Type checking, Control Flow Slides adapted from : Robert van Engelen, Florida State University Static versus Dynamic Checking Static checking: the compiler
More informationSyntax Directed Translation
Syntax Directed Translation 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 informationTACi: Three-Address Code Interpreter (version 1.0)
TACi: Three-Address Code Interpreter (version 1.0) David Sinclair September 23, 2018 1 Introduction TACi is an interpreter for Three-Address Code, the common intermediate representation (IR) used in compilers.
More information