Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

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

Intermediate Code Generation

Alternatives for semantic processing

Compiler Design (40-414)

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

COMPILER CONSTRUCTION (Intermediate Code: three address, control flow)

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

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

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

UNIT IV INTERMEDIATE CODE GENERATION

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

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

CST-402(T): Language Processors

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

Concepts Introduced in Chapter 6

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Chapter 6 Intermediate Code Generation

Intermediate representation


Concepts Introduced in Chapter 6

Intermediate Code Generation

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

A Simple Syntax-Directed Translator

Intermediate Representations

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

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

Dixita Kagathara Page 1

Introduction to Compiler Design

CS 4201 Compilers 2014/2015 Handout: Lab 1

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

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

Intermediate Representations & Symbol Tables

Time : 1 Hour Max Marks : 30

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

Intermediate Code Generation

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

tokens parser 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address code

Intermediate Representations

Intermediate Code Generation

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

CSE 401/M501 Compilers

Principles of Compiler Design

TACi: Three-Address Code Interpreter (version 1.0)

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

Intermediate Representations

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

Intermediate Representations

Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit

Formal Languages and Compilers Lecture I: Introduction to Compilers

Intermediate Representations

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Intermediate Representa.on

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Semantic Analysis and Intermediate Code Generation

THEORY OF COMPILATION

LECTURE NOTES ON COMPILER DESIGN P a g e 2

PRINCIPLES OF COMPILER DESIGN

LECTURE 3. Compiler Phases

CS415 Compilers. Intermediate Represeation & Code Generation

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain)

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

COMPILER DESIGN. For COMPUTER SCIENCE

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

Compilers. Compiler Construction Tutorial The Front-end

Intermediate Code Generation

CS606- compiler instruction Solved MCQS From Midterm Papers

CSc 453 Intermediate Code Generation

QUESTIONS RELATED TO UNIT I, II And III

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

COMPILER DESIGN LECTURE NOTES

Syntax-Directed Translation

UNIT I- LEXICAL ANALYSIS. 1.Interpreter: It is one of the translators that translate high level language to low level language.

Intermediate Representation (IR)

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

6. Intermediate Representation

Front End. Hwansoo Han

Compiler Design Aug 1996

Intermediate Code Generation

Intermediate Code Generation

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

VIVA QUESTIONS WITH ANSWERS

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

CMPSC 160 Translation of Programming Languages. Three-Address Code

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

CS2210: Compiler Construction. Code Generation

Semantic actions for expressions

6. Intermediate Representation!

intermediate-code Generation

Compiler Design Overview. Compiler Design 1

A programming language requires two major definitions A simple one pass compiler

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

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

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Introduction to Compiler Construction

Principles of Programming Languages COMP251: Syntax and Grammars

Transcription:

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 analysis) Syntactic structure Semantic Analysis (IC generator) Intermediate Code Code Generator Target language Intermediate Code Code Optimizer Intermediate Code Symbol Table

Intermediate Code Advantages: Allows the analysis phase to be machine independent (language - machine independence). Makes optimization easier (not machine-dependent). The same analysis and/or optimizer can be used on the same intermediate code. Facilitates the division in phases of a compiler project.

Intermediate Code Disadvantages: Loss of efficiency (not only one pass). Introduces a new translation phase in the compiler.

Analysis Phase position := initial + rate * 60 lexical analyzer id1 := id2 + id3 * 60 Symbol Table position... initial. rate. syntax analyzer := id1 + id2l * id3 60 semantic analyzer := id1 + id2l * id3 inttoreal 60 intermediate code generator E r r o r s

Synthesis Phase Symbol Table position... initial. rate. intermediate code generator temp1 := inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 code optimizer temp1 := id3 * 60.0 id1 := id2 + temp1 final code generator MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R1, R2 MOVF R1, id1 3 address code E r r o r s

Intermediate Languages Types Depends on the target program (often depends on the machine): Graphical IRs: Abstract Syntax Trees (AST), Direct Acyclic Graphs (DAGs), Control Flow Graphs (CFG). Linear IRs: Stack based (postfix). Three address code (triples, indirect triples, quadruples).

Graphical IRs Abstract Syntax Trees (AST) retain essential structure of the parse tree, eliminating unneeded nodes. Directed Acyclic Graphs (DAG) compacted AST to avoid duplication. Control flow graphs (CFG) explicitly model control flow.

Linearized IC Postfix notation: Operators follow operands S = A + B * C S A B C * + = Benefits Simple notation Disadvantages Difficult to understand code not useful for optimizations Three-address code: quadruples triples Indirect triples

Abstract Syntax Tree Condensed form of a parse tree useful for representing language constructs. Keywords and operators appear as interior nodes. Examples: E T F Parse Tree AST S:=A+B*C IF A<B THEN X:=B A[I]:=B E + := IF-THEN T A B S + < := + F A * A B X B A B B C := [ ] B I A

Abstract Syntax Tree Include semantic information: example: a=b*-c+4 assign id a + * num 4 id b minus id c

Three-Address Code Sequence of statements of the form: x = y op z One operator and up to three addresses. Compiler-generated temporary variables. Directly related to the evaluation of expressions: Example: a = b*(c+d) is translated to: tmp1 = c+d a = b*tmp1

Three-address code Lineal representation of a AST Example 1: a=b*-c+4 3-address code t1=-c t2=b*t1 t3=4 t4=t3+t2 a=t4 Example 2: if cond then then_statements else else_statements; end if; 3-address code t1 = cond if not t1 goto else_label code for the then_statements goto endif_label else_label: code for the else_statements endif_label:

Types of 3-address statements Assignment: x=y op z (op is a binary arithmetic or logical operator) Assignment x=op y (unary operators) copy x:=y Unconditional jump: goto L (L label) Conditional gotoc x L (conditional jump if x relop y goto L)

Types of 3-address statements Calls to routines: param x1... param xn call p,n Indexed assignments x:=y[i] x[i]=y Pointer assignments x:=&y x:=*y *x:=y

3-address code implementation Quadruples Record structure with 4 fields: (op,y,z,x) to represent x=y op z Triples The fourth value is a temporary value. The index is used instead. Example: a = b+(c*d) [quadruples] [triples] 1. (mul,c,d,t1) 1: (mul,c,d) 2. (add,b,t1,t2) 2: (add,b,(1)) 3. (asn,a,t2,_) 3: (asn,a,(2))

3-address statements Quadruples 4 values: (<OPERATOR>,<Operand 1 >,<Operand 2 >,<Result>) Examples Expression Quadruples S:=A+B*C * B C T 1 (*, B, C, T 1 ) + A T 1 T 2 (+, A, T 1, T 2 ) := T 2 S (:=, T 2,, S) IF A<B THEN X:=B < A B E 1 (<, A, B, E 1 ) GOTOC E 1 E 2 (GOTOC, E 1,, E 2 ) := B X (:=, B,, X) LABEL E 2 (LABEL,,, E 2 )

Triples Quadruples are more general but: Need too much space. Too many auxiliary variables to store intermediate results. Triples omitts the last operand, it is implicit and associated to the triple: (<OPERATOR>, <Operand 1 >, <Operand 2 >) Equivalent to AST

Declarations Construction of the symbol table Create an entry for each local name (within a procedure or a block) including information like the type and the relative address of the storage for that name Reserve space Offset: a global variable that keeps track of the next available relative address Synthesized attributes type and width to indicate the type and the number of memory bits taken by objects of that type