CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Chapter 6 Intermediate Code Generation
Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
8 Intermediate code generation
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules 17 and 18 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
CH4.1 CSE244 L-Attributed Definitions Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Syntax Directed Translation
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CH4.1 CSE244 Bottom Up Translation (revisited) Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road,
CSC 8505 Compiler Construction Intermediate Representations.
CH4.1 CSE244 Syntax Directed Translation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
Compiler Chapter# 5 Intermediate code generation.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
國立台灣大學 資訊工程學系 薛智文 98 Spring Intermediate Code Generation (textbook ch# 6.1–6.4, 6.5.1–6.5.3,
Intermediate Code Generation
Intermediate Code Generation
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Topic #7: Intermediate Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Intermediate Code Generation Abstraction at the source level identifiers, operators, expressions, statements, conditionals, iteration, functions (user.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Code Generation CPSC 388 Ellen Walker Hiram College.
Code Generation How to produce intermediate or target code.
Chap. 4, Intermediate Code Generation
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Three Address Code Generation of Control Statements continued..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Compiler Design Lecture 10 Semantic Analysis. int aintegers int a[2][3]array(2, array(3, integer)) int f(int, float, char) int x float x char  int Int.
CS 404 Introduction to Compiler Design
Syntax-Directed Translation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Compiler Construction
Intermediate Code Generation
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
Chapter 6 Intermediate-Code Generation
Intermediate Code Generation Part I
Intermediate code generation
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
Syntax-Directed Translation Part II
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
THREE ADDRESS CODE GENERATION
Intermediate Code Generation
Syntax-Directed Translation Part II
Intermediate Code Generation Part I
Compiler Construction
Compiler Design 21. Intermediate Code Generation
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
Presentation transcript:

CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs, CT

CH4.2 CSE244 Intermediate Code Generation  Translating source program into an “intermediate language.”  Simple  CPU Independent,  …yet, close in spirit to machine language.  Or, depending on the application other intermediate languages may be used, but in general, we opt for simple, well structured intermediate forms.  (and this completes the “Front-End” of Compilation).

CH4.3 CSE244 Types of Intermediate Languages  Graphical Representations.  Consider the assignment a:=b*-c+b*-c: assign a+ ** uminus b cc b assign a + * uminus b c

CH4.4 CSE244 Syntax Dir. Definition for Gr. Reps. PRODUCTIONSemantic Rule S  id := E{ S.nptr = mknode (‘assign’, mkleaf(id, id.entry), E.nptr) } E  E 1 + E 2 {E.nptr = mknode(‘+’, E 1.nptr,E 2.nptr) } E  E 1 * E 2 {E.nptr = mknode(‘*’, E 1.nptr,E 2.nptr) } E  - E 1 {E.nptr = mknode(‘uminus’, E 1.nptr) } E  ( E 1 ) {E.nptr = E 1.nptr } E  id {E.nptr = mkleaf(id, id.entry) }

CH4.5 CSE244 Three Address Code  Statements of general form x:=y op z  No built-up arithmetic expressions are allowed.  As a result, x:=y + z * w should be represented as t 1 :=z * w t 2 :=y + t 1 x:=t 2  Observe that given the syntax-tree or the dag of the graphical representation we can easily derive a three address code for assignments as above.  In fact three-address code is a linearization of the tree.  Three-address code is useful: related to machine- language/ simple/ optimizable.

CH4.6 CSE244 Example of 3-address code  t 1 :=- c t 2 :=b * t 1 t 3 :=- c t 4 :=b * t 3 t 5 :=t 2 + t 4 a:=t 5  t 1 :=- c t 2 :=b * t 1 t 5 :=t 2 + t 2 a:=t 5

CH4.7 CSE244 Types of Three-Address Statements.  x:=y op z  x:=op z  x:=z  goto L  if x relop y goto L  Push/pop (stack) more advanced:  param x 1 param x 2 … param x n call p,n  x:=y[i]x[i]:=y  x:=&yx:=*y*x:=y

CH4.8 CSE244 Syntax-Directed Translation into 3- address code.  First deal with assignments.  Use attributes  E.place to hold the name of the “place” that will hold the value of E  Identifier will be assumed to already have the place attribute defined.  For example, the place attribute will be of the form t0, t1, t2, … for identifiers and v0,v1,v2 etc. for the rest.  E.code to hold the three address code statements that evaluate E (this is the `translation’ attribute).  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).

CH4.9 CSE244 Syntax-Dir. Definition for 3-address code PRODUCTIONSemantic Rule S  id := E{ S.code = E.code||gen(id.place ‘=’ E.place ‘;’) } E  E 1 + E 2 {E.place= newtemp ; E.code = E 1.code || E 2.code || || gen(E.place‘:=’E 1.place‘+’E 2.place) } E  E 1 * E 2 {E.place= newtemp ; E.code = E 1.code || E 2.code || || gen(E.place‘=’E 1.place‘*’E 2.place) } E  - E 1 {E.place= newtemp ; E.code = E 1.code || || gen(E.place ‘=’ ‘uminus’ E 1.place) } E  ( E 1 ) {E.place= E 1.place ; E.code = E 1.code} E  id {E.place = id.entry ; E.code = ‘’ } e.g. e.g. a := b * - (c+d)

CH4.10 CSE244 What about things that are not assignments?  E.g. while statements of the form “while E do S” (intepreted as while the value of E is not 0 do S) Extension to the previous syntax-dir. Def. PRODUCTION S  while E do S 1 Semantic Rule begin = newlabel; after = newlabel ; S.code = gen(begin ‘:’) || E.code || gen(‘if’ E.place ‘=’ ‘0’ ‘goto’ after) || || S 1.code || gen(‘goto’ begin) || gen(after ‘:’)

CH4.11 CSE244 Dealing with Procedures P  procedure ident ‘;’ block ‘;’ Semantic Rule begin = newlabel; Enter into symbol-table in the entry of the procedure name the begin label. P.code = gen(begin ‘:’) || block.code || gen(‘pop’ return_address) || gen(“goto return_address”) S  call ident Semantic Rule Look up symbol table to find procedure name. Find its begin label called proc_begin return = newlabel; S.code = gen(‘push’return); gen(goto proc_begin) || gen(return “:”)

CH4.12 CSE244 Implementations of 3-address statements  Quadruples t 1 :=- c t 2 :=b * t 1 t 3 :=- c t 4 :=b * t 3 t 5 :=t 2 + t 4 a:=t 5 oparg1arg2result (0)uminusc t1t1t1t1 (1)*b t1t1t1t1 t2t2t2t2 (2)uminusc (3)*b t3t3t3t3 t4t4t4t4 (4)+ t2t2t2t2 t4t4t4t4 t5t5t5t5 (5):= t5t5t5t5a

CH4.13 CSE244 Implementations of 3-address statements, II  Triples t 1 :=- c t 2 :=b * t 1 t 3 :=- c t 4 :=b * t 3 t 5 :=t 2 + t 4 a:=t 5 oparg1arg2 (0)uminusc (1)*b(0) (2)uminusc (3)*b(2) (4)+(1)(3) (5)assigna(4)

CH4.14 CSE244 Other types of 3-address statements  e.g. ternary operations like x[i]:=yx:=y[i]  require two or more entries. e.g. oparg1arg2 (0) [ ] = xi (1)assign(0)y oparg1arg2(0) yi (1)assignx(0)

CH4.15 CSE244 Implementations of 3-address statements, III  Indirect Triples oparg1arg2 (14)uminusc (15)*b(14) (16)uminusc (17)*b(16) (18)+(15)(17) (19)assigna(18)op(0)(14) (1)(15) (2)(16) (3)(17) (4)(18) (5)(19)

CH4.16 CSE244Declarations Using a global variable offset PRODUCTIONSemantic Rule P  M D { } M   {offset:=0 } D  id : T{ addtype(id.entry, T.type, offset) offset:=offset + T.width } T  char{T.type = char; T.width = 4; } T  integer {T.type = integer ; 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} T  ^T 1 {T.type = pointer(T 1.type); T 1.width = 4}

CH4.17 CSE244 Keeping Track of Scope Information Consider the grammar fraction: P  D D  D ; D | id : T | proc id ; D ; S Each procedure should be allowed to use independent names. Nested procedures are allowed.

CH4.18 CSE244 Keeping Track of Scope Information (a translation scheme) P  M D{ addwidth(top(tblptr), top(offset)); pop(tblptr); pop(offset) } M   { t:=mktable(null); push(t, tblptr); push(0, offset)} D  D 1 ; D 2... D  proc id ; N D ; S{ t:=top(tblpr); addwidth(t,top(offset)); pop(tblptr); pop(offset); enterproc(top(tblptr), id.name, t)} N   {t:=mktable(top(tblptr)); push(t,tblptr); push(0,offset);} D  id : T {enter(top(tblptr), id.name, T.type, top(offset); top(offset):=top(offset) + T.width Example: proc func1; D; proc func2 D; S; S