Chapter 8: Intermediate Code Generation

Slides:



Advertisements
Similar presentations
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Advertisements

Intermediate Code Generation
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
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.
Compiler Construction Sohail Aslam Lecture IR Taxonomy IRs fall into three organizational categories 1.Graphical IRs encode the compiler’s knowledge.
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.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
1 Intermediate Code generation. 2 Intermediate Code Generation l Intermediate languages l Declarations l Expressions l Statements l Reference: »Chapter.
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
CSC 8505 Compiler Construction Intermediate Representations.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
CH4.1 CSE244 Intermediate Code Generation Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit.
7/15/2015\course\cpeg621-10F\Topic-1a.ppt1 Intermediate Code Generation Reading List: Aho-Sethi-Ullman: Chapter 2.3 Chapter 6.1 ~ 6.2 Chapter 6.3 ~ 6.10.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
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.
CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson.
1 October 1, October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
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.
Compilers: IC/10 1 Compiler Structures Objective – –describe intermediate code generation – –explain a stack-based intermediate code for the expression.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
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,
Introduction to Code Generation and Intermediate Representations
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.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Code Generation CPSC 388 Ellen Walker Hiram College.
Chap. 4, Intermediate Code Generation
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Theory of Compilation Erez Petrank Lecture 6: Intermediate Representation and Attribute Grammars 1.
Three Address Code Generation of Control Statements continued..
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
1 Syntax-Directed Translation Part II Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
CS 404 Introduction to Compiler Design
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Compiler Construction
Compiler Construction
Subject Name:COMPILER DESIGN Subject Code:10CS63
Compiler Optimization and Code Generation
Intermediate Code Generation Part I
Intermediate Code Generation
פרק 5 תרגום מונחה תחביר תורת הקומפילציה איתן אביאור.
Intermediate Code Generation
Chapter 6 Intermediate-Code Generation
Three Address Code Generation - Control Statements
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.
7.4 Boolean Expression and Control Flow Statements
Syntax-Directed Translation Part II
Compiler Design 21. Intermediate Code Generation
Intermediate Code Generation Part I
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:

Chapter 8: Intermediate Code Generation Lecture # 23 Chapter 8: Intermediate Code Generation

Intermediate Code Generation Facilitates retargeting: enables attaching a back end for the new machine to an existing front end Enables machine-independent code optimization Intermediate code Target machine code Front end Back end

Intermediate Representations Graphical representations (e.g. AST) Postfix notation: operations on values stored on operand stack (similar to JVM bytecode) Three-address code: (e.g. triples and quads) x := y op z Two-address code: x := op y which is the same as x := x op y

Syntax-Directed Translation of Abstract Syntax Trees Production S  id := E E  E1 + E2 E  E1 * E2 E  - E1 E  ( E1 ) E  id Semantic Rule S.nptr := mknode(‘:=’, mkleaf(id, id.entry), E.nptr) E.nptr := mknode(‘+’, E1.nptr, E2.nptr) E.nptr := mknode(‘*’, E1.nptr, E2.nptr) E.nptr := mknode(‘uminus’, E1.nptr) E.nptr := E1.nptr E.nptr := mkleaf(id, id.entry)

Abstract Syntax Trees E.nptr a * (b + c) E.nptr * E.nptr a ( E.nptr ) Pro: easy restructuring of code and/or expressions for intermediate code optimization Cons: memory intensive a + b c

Abstract Syntax Trees versus DAGs a := b * -c + b * -c := := a + a + * * * b uminus b uminus b uminus c c c Tree DAG

Postfix Notation a := b * -c + b * -c a b c uminus * b c uminus * + assign 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 Postfix notation represents operations on a stack Pro: easy to generate Cons: stack operations are more difficult to optimize

Three-Address Code a := b * -c + b * -c t1 := - c t2 := b * t1 t3 := - c t4 := b * t3 t5 := t2 + t4 a := t5 t1 := - c t2 := b * t1 t5 := t2 + t2 a := t5 Linearized representation of a syntax tree Linearized representation of a syntax DAG

Three-Address Statements Assignment statements: x := y op z, x := op y Indexed assignments: x := y[i], x[i] := y Pointer assignments: x := &y, x := *y, *x := y Copy statements: x := y Unconditional jumps: goto lab Conditional jumps: if x relop y goto lab Function calls: param x… call p, n return y

Syntax-Directed Translation into Three-Address Code Productions S  id := E | while E do S E  E + E | E * E | - E | ( E ) | id | num Synthesized attributes: S.code three-address code for S S.begin label to start of S or nil S.after label to end of S or nil E.code three-address code for E E.place a name holding the value of E gen(E.place ‘:=’ E1.place ‘+’ E2.place) Code generation t3 := t1 + t2

Syntax-Directed Translation into Three-Address Code (cont’d) Productions S  id := E S  while E do S1 E  E1 + E2 E  E1 * E2 E  - E1 E  ( E1 ) E  id E  num Semantic rules S.code := E.code || gen(id.place ‘:=’ E.place); S.begin := S.after := nil (see next slide) E.place := newtemp(); E.code := E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘+’ E2.place) E.place := newtemp(); E.code := E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘*’ E2.place) E.place := newtemp(); E.code := E1.code || gen(E.place ‘:=’ ‘uminus’ E1.place) E.place := E1.place E.code := E1.code E.place := id.name E.code := ‘’ E.place := newtemp(); E.code := gen(E.place ‘:=’ num.value)

Syntax-Directed Translation into Three-Address Code (cont’d) Production S  while E do S1 S.begin: E.code if E.place = 0 goto S.after S.code Semantic rule S.begin := newlabel() S.after := newlabel() S.code := gen(S.begin ‘:’) || E.code || gen(‘if’ E.place ‘=‘ ‘0’ ‘goto’ S.after) || S1.code || gen(‘goto’ S.begin) || gen(S.after ‘:’) goto S.begin S.after: …

Example i := 2 * n + k while i do i := i - k t1 := 2 t2 := t1 * n t3 := t2 + k i := t3 L1: if i = 0 goto L2 t4 := i - k i := t4 goto L1 L2: