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

Size: px
Start display at page:

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

Transcription

1 CMPT 379 Compilers Anoop Sarkar 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code Generator TAC Code Generator Assembly Language Sparc, x86 11/13/07 2 1

2 TAC: 3-Address Code Instructions that operate on named locations and labels: generic assembly Locations Every location is some place to store 4 bytes Pretend we can make infinitely many of them Either on stack frame: You assign offset (plus other information possibly) Or global variable Referred to by global name Labels (you generate as needed) 11/13/07 3 TAC: 3-Address Code Addresses/Locations names/labels: we allow source-program names in TAC, implemented as a pointer to a symbol table entry constants temporaries Instructions: assignments: x = y op z / x = op y copy: x = y unconditional jump: goto L conditional jumps: if x goto L / iffalse x goto L / if x relop y goto L 11/13/07 <, ==, >=, etc. 4 2

3 TAC: 3-Address Code Instructions: Procedure calls: param x1 param x2 param xn call p, n Function calls: y = call p, n return y Instructions: Arrays: x = y[i] x[i] = y Pointers: x = &y x = *y *x = y 11/13/07 5 What TAC doesn t give you Array indexing (bounds check) Two or n-dimensional arrays Relational <=, >=, >, Conditional branches other than if or iffalse Field names in records/structures Use base+offset load/store Object data and method access 11/13/07 6 3

4 Control Flow Consider the statement: while (a[i] < v) { i = i+1; L1: t1 = i t2 = t1 * 8 t3 = a[ t2 ] iffalse t3 < v goto L2 t4 = i t4 = t4 + 1 i = t4 goto L1 L2:... Labels can be implemented using position numbers 100: t1 = i 101: t2 = t1 * 8 102: t3 = a[ t2 ] 103: iffalse t3 < v goto : t4 = i 105: t4 = t : i = t4 107: goto : 11/13/07 7 int gcd(int x, int y) { int d; d = x - y; if (d > 0) return gcd(d, y); else if (d < 0) return gcd(x, -d); else return x; gcd: L0: t0 = x - y d = t0 t1 = d t2 = t1 > 0 iffalse t2 goto L0 param y param d t3 = call gcd, 2 return t3 t4 = d t5 = t4 < 0... Avoiding redundant gotos if t2 goto L1 goto L0 L1:... 11/13/07 8 4

5 Short-circuiting Booleans More complex if statements: if (a or b and not c) { Typical sequence: t1 = not c t2 = b and t1 t3 = a or t2 Short-circuit is possible in this case: if (a and b and c) { Short-circuit sequence: t1 = a if t1 goto L0 /* sckt */ goto L4 L0: t2 = b ifz t2 goto L1 11/13/07 9 void main() { int i; for (i = 0; i < 10; i = i + 1) print(i); More Control Flow: for loops main: t0 = 0 i = t0 L0: t1 = 10 t2 = i < t1 iffalse t2 goto L1 param i, 1 call PrintInt, 1 t3 = 1 t4 = i + t3 i = t4 goto L0 L1: return 11/13/

6 Backpatching in Control-Flow Easiest way to implement the translations is to use two passes In one pass we may not know the target label for a jump statement Backpatching allows one pass code generation Generate branching statements with the targets of the jumps temporarily unspecified Put each of these statements into a list which is then filled in when the proper label is determined 11/13/07 11 Backpatching S while M ( expr ) M block expr true expr false expr expr expr M ε simply returns the current instruction number while (true) { : t0 = true 109: if t0 goto : goto - 111: : goto :... falselist backpatch({110, 123) backpatch is done by rule that uses S 11/13/

7 S while M ( expr ) M block expr true expr false expr expr expr M ε simply returns the current instruction number Backpatching while (true) { break; 108: t0 = true 109: if t0 goto : goto - 111: goto - 122: goto :... backpatch({110, 123) backpatch({111, 123) backpatch is done by while rule continue is similar, generates goto /13/07 13 S while M ( expr ) M block expr true expr false expr expr expr M ε while (true false) {... Backpatching backpatch is done by while rule true false 100: t0 = true 101: if t0 goto - 102: t1 = false 103: if t1 goto : t0 = false 105: goto - 106: t0 = true 107: goto - backpatch({101, 105, 107, 109) nextlist 11/13/

8 Backpatching We maintain a list of statements that need patching by future statements Three lists are maintained: truelist: for targets when evaluation is true falselist: for targets when eval is false nextlist: the statement that ends the block These lists can be implemented as a synthesized attribute Note the use of marker non-terminals 11/13/07 15 Array Elements Array elements are numbered 0,..., n-1 Let w be the width of each array element Let base be the address of the storage allocated for the array Then the i th element A[i] begins in location base+i*w The element A[i][j] with n elements in the 2nd dimension begins at: base+(i*n+j)*w 11/13/

9 foo: t0 = 1 t1 = 4 t2 = t1 * t0 t3 = arr + t2 t4 = *(t3) t5 = 0 t6 = 4 t7 = t6 * t5 t8 = arr + t7 t9 = *(t8) t10 = 2 t11 = t9 * t10 t4 = t11 void foo(int[] arr) { arr[1] = arr[0] * 2 foo: t0 = 1 t1 = 4 t2 = t1 * t0 t3 = arr + t2 t4 = 0 t5 = 4 t6 = t5 * t4 t7 = arr + t6 t8 = *(t7) t9 = 2 t10 = t8 * t9 *(t3) = t10 Array References Wrong Correct 11/13/07 17 Translation of Expressions S id = E E E + E E - E E ( E ) E id $$.code = concat($3.code, $1.lexeme = $3.addr) $$.addr = new Temp(); $$.code = concat($1.code, $3.code, $$.addr = $1.addr + $3.addr) $$.addr = new Temp(); $$.code = concat($2.code, $$.addr = - $2.addr) $$.addr = $2.addr; $$.code = $2.code $$.addr = symtbl($1.lexeme); $$.code = 11/13/

10 Function arguments Compute offsets for all incoming arguments, local variables and temporaries Incoming arguments are at offset x, x+4, x+8, Locals+Temps are at y, -y-4, -y-8, Compute Frame Size More Incoming First Incoming <Saved Regs>... First Local More Locals 11/13/07 19 Computing Location Offsets class A { void f (int a */, int b */, int c x+12 */) { int s if (c > 0) { int t else { int u int t Location offsets for temporaries are ignored on this slide You could here, but okay if you don t 11/13/

11 int factorial(int n) { if (n <=1 ) return 1; return n*factorial(n-1); void main() { print(factorial(6)); factorial: t0 = 1 t1 = n lt t0 t3 = n <= 1 t2 = n eq t0 t3 = t1 or t2 iffalse t3 goto L0 t4 = 1 return t4 L0: t5 = 1 t6 = n - t5 param t6 t7 = call factorial, 1 t8 = n * t7 return t8 11/13/07 21 Implementing TAC Quadruples: t1 = - c t2 = b * t1 t3 = - c t4 = b * t3 t5 = t2 + t4 a = t5 Triples 1. - c 2. b * (1) 3. - c 4. b * (3) 5. (2) + (4) 6. a = (5) 11/13/

12 Indirect Triples 1. - c 2. b * (1) 3. - c 4. b * (3) 5. (2) + (4) 6. a = (5) can be re-ordered by the code optimizer Implementing TAC Instruction List: (1) (2) (3) (4) (5) (6) Static Single Assignment (SSA) instead of: a = t1 b = a + t1 a = b + t1 the SSA form has: a1 = t1 b1 = a1 + t1 a2 = b1 + t1 a variable is never reused 11/13/07 23 Correctness vs. Optimizations When writing backend, correctness is paramount Efficiency and optimizations are secondary concerns at this point Don t try optimizations at this stage 11/13/

13 Basic Blocks Functions transfer control from one place (the caller) to another (the called function) Other examples include any place where there are branch instructions A basic block is a sequence of statements that enters at the start and ends with a branch at the end Remaining task of code generation is to create code for basic blocks and branch them together 11/13/07 25 Summary TAC is one example of an intermediate representation (IR) An IR should be close enough to existing machine code instructions so that subsequent translation into assembly is trivial In an IR we ignore some complexities and differences in computer architectures, such as limited registers, multiple instructions, branch delays, load delays, etc. 11/13/

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

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

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

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

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

Compiler 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 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

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

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

TACi: Three-Address Code Interpreter (version 1.0)

TACi: 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

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

Concepts Introduced in Chapter 6

Concepts 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 information

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18 General issues Section 9.1 Target language: absolute machine language all addresses refer to actual addresses program placed in a fixed location in memory relocatable machine language (object modules)

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

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

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 Announcements Be sure to

More information

Concepts Introduced in Chapter 6

Concepts 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 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

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

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

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Object 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 information

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE INTERMEDIATE REPRESENTATIONS CS 403: Intermediate Representations and Code Generation Stefan D. Bruda Winter 2015 Code generation is typically not done directly The functioning of a compiler is typically

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

IR Generation. May 13, Monday, May 13, 13

IR Generation. May 13, Monday, May 13, 13 IR Generation May 13, 2013 Monday, May 13, 13 Monday, May 13, 13 Monday, May 13, 13 Midterm Results Grades already in Repeal policy Talk to TA by the end of the Tuesday reserve the right to regrade the

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

Three-Address Code IR

Three-Address Code IR Three-Address Code IR Announcements Programming Project 3 due tonight at 11:59PM. OH today after lecture. Ask questions on Piazzza! Ask questions via email! Programming Project 4 out, due Wednesday, August

More information

Compiling Code, Procedures and Stacks

Compiling Code, Procedures and Stacks Compiling Code, Procedures and Stacks L03-1 RISC-V Recap Computational Instructions executed by ALU Register-Register: op dest, src1, src2 Register-Immediate: op dest, src1, const Control flow instructions

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

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

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

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Front-end intermediate code generation source program Lexical Analyzer Syntax Analyzer Semantic Analyzer Int. Code Generator intermediate representation Symbol Table Advantages

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

Intermediate Code Generation (ICG)

Intermediate Code Generation (ICG) Intermediate Code Generation (ICG) Transform AST to lower-level intermediate representation Basic Goals: Separation of Concerns Generate efficient code sequences for individual operations Keep it fast

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

Chapter 2: Instructions:

Chapter 2: Instructions: Chapter 2: Instructions: Language of the Computer Computer Architecture CS-3511-2 1 Instructions: To command a computer s hardware you must speak it s language The computer s language is called instruction

More information

CSc 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 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 information

Computer 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 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 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

CMPT 379 Compilers. Parse trees

CMPT 379 Compilers. Parse trees CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 10/25/07 1 Parse trees Given an input program, we convert the text into a parse tree Moving to the backend of the compiler: we will produce intermediate

More information

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

CMPT Sample Final Exam

CMPT Sample Final Exam CMPT 379 - Sample Final Exam 1. Code Generation: Instead of an if statement what if we had an if expression? Remember that, in procedural programming languages, a statement ends with a semicolon and cannot

More information

CSCE 5610: Computer Architecture

CSCE 5610: Computer Architecture HW #1 1.3, 1.5, 1.9, 1.12 Due: Sept 12, 2018 Review: Execution time of a program Arithmetic Average, Weighted Arithmetic Average Geometric Mean Benchmarks, kernels and synthetic benchmarks Computing CPI

More information

Intermediate Representations

Intermediate Representations Intermediate Representations Intermediate Representations (EaC Chaper 5) Source Code Front End IR Middle End IR Back End Target Code Front end - produces an intermediate representation (IR) Middle end

More information

Code Generation. CS 1622: Code Generation & Register Allocation. Arrays. Why is Code Generation Hard? Multidimensional Arrays. Array Element Address

Code Generation. CS 1622: Code Generation & Register Allocation. Arrays. Why is Code Generation Hard? Multidimensional Arrays. Array Element Address Code Generation CS 1622: Code Generation & Register Allocation Input: Intermediate representation Output: Target code Example: cond_expr if-statement stmt_list L1: slti $t1, 3, $s0 beq $t1, $zero, L1 addi

More information

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine Machine Language Instructions Introduction Instructions Words of a language understood by machine Instruction set Vocabulary of the machine Current goal: to relate a high level language to instruction

More information

Code Generation & Parameter Passing

Code Generation & Parameter Passing Code Generation & Parameter Passing Lecture Outline 1. Allocating temporaries in the activation record Let s optimize our code generator a bit 2. A deeper look into calling sequences Caller/Callee responsibilities

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

Intermediate Code Generation

Intermediate 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 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

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation Comp 204: Computer Systems and Their Implementation Lecture 22: Code Generation and Optimisation 1 Today Code generation Three address code Code optimisation Techniques Classification of optimisations

More information

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

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

Oct 13, 2015 Robert Heckendorn University of Idaho ======================================================================

Oct 13, 2015 Robert Heckendorn University of Idaho ====================================================================== TM 3.5 - A virtual machine for CS445 plus A Description of the Execution Environment for C- Oct 13, 2015 Robert Heckendorn University of Idaho The TM machine is from the original code from the compiler

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

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

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

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1 CSE P 501 Compilers Intermediate Representations Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 G-1 Administrivia Semantics/types/symbol table project due ~2 weeks how goes it? Should be caught up on

More information

Control Flow. Stephen A. Edwards. Fall Columbia University

Control Flow. Stephen A. Edwards. Fall Columbia University Control Flow Stephen A. Edwards Columbia University Fall 2013 Control Flow Time is Nature s way of preventing everything from happening at once. Scott identifies seven manifestations of this: 1. Sequencing

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

Wednesday, October 15, 14. Functions

Wednesday, October 15, 14. Functions Functions Terms void foo() { int a, b;... bar(a, b); void bar(int x, int y) {... foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand:

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

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

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

Intermediate Representations

Intermediate 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 information

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

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 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 information

Computer Architecture

Computer Architecture Computer Architecture Chapter 2 Instructions: Language of the Computer Fall 2005 Department of Computer Science Kent State University Assembly Language Encodes machine instructions using symbols and numbers

More information

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL 6.035 Project 3: Unoptimized Code Generation Jason Ansel MIT - CSAIL Quiz Monday 50 minute quiz Monday Covers everything up to yesterdays lecture Lexical Analysis (REs, DFAs, NFAs) Syntax Analysis (CFGs,

More information

Code Generation. Lecture 12

Code Generation. Lecture 12 Code Generation Lecture 12 1 Lecture Outline Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

Page No 1 (Please look at the next page )

Page No 1 (Please look at the next page ) Salman Bin Abdul Aziz University Collage of Computer Engineering and Sciences Computer Science Department 1433-1434 (2012-2013) First Term CS 4300 Compiler Construction 8 th Level Final Exam 120 Minutes

More information

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

Semantic 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 information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers Intermediate Representations Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 G-1 Agenda Survey of Intermediate Representations Graphical Concrete/Abstract Syntax Trees (ASTs)

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

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 21: Generating Pentium Code 10 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Simple Code Generation Three-address code makes it

More information

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

More information

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

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

More information

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR.

We ve written these as a grammar, but the grammar also stands for an abstract syntax tree representation of the IR. CS 4120 Lecture 14 Syntax-directed translation 26 September 2011 Lecturer: Andrew Myers We want to translate from a high-level programming into an intermediate representation (IR). This lecture introduces

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

The structure of a compiler

The structure of a compiler The structure of a compiler O(n) A compiler is a lot of fast stuff followed by hard problems scanning parsing intermediate code generation Polynomial time Code optimization: local dataflow, global dataflow,

More information

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions Representing Instructions Instructions are encoded in binary Called machine code MIPS instructions Encoded as 32-bit instruction words Small number of formats encoding operation code (opcode), register

More information

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI. CSCI 402: Computer Architectures Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI Recall Big endian, little endian Memory alignment Unsigned

More information

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

COMPILER 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 information

Reduced Instruction Set Computer (RISC)

Reduced Instruction Set Computer (RISC) Reduced Instruction Set Computer (RISC) Focuses on reducing the number and complexity of instructions of the ISA. RISC Goals RISC: Simplify ISA Simplify CPU Design Better CPU Performance Motivated by simplifying

More information

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d

CPSC 213. Introduction to Computer Systems. Static Control Flow. Unit 1d CPSC 213 Introduction to Computer Systems Unit 1d Static Control Flow 1 Reading Companion 2.7.1-2.7.3, 2.7.5-2.7.6 Textbook 3.6.1-3.6.5 2 Control Flow The flow of control is the sequence of instruction

More information

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

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

Review of Activation Frames. FP of caller Y X Return value A B C

Review of Activation Frames. FP of caller Y X Return value A B C Review of Activation Frames In general, activation frames are organized like this: HI LO Bookkeeping/preserved registers I/O parameters Locals and temps RA of caller FP of caller Y X Return value A B C

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

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

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

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

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

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

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine

Lecture Outline. Topic 1: Basic Code Generation. Code Generation. Lecture 12. Topic 2: Code Generation for Objects. Simulating a Stack Machine Lecture Outline Code Generation Lecture 12 Topic 1: Basic Code Generation The MIPS assembly language A simple source language Stack-machine implementation of the simple language Topic 2: Code Generation

More information

CS415 Compilers. Intermediate Represeation & Code Generation

CS415 Compilers. Intermediate Represeation & Code Generation CS415 Compilers Intermediate Represeation & Code Generation These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review - Types of Intermediate Representations

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

Final Exam Review Questions

Final Exam Review Questions EECS 665 Final Exam Review Questions 1. Give the three-address code (like the quadruples in the csem assignment) that could be emitted to translate the following assignment statement. However, you may

More information

Levels of Programming. Registers

Levels of Programming. Registers Levels of Programming COSC 2021: Computer Organization Instructor: Dr. Amir Asif Department of Computer Science York University Handout # 3: MIPS Instruction Set I Topics: 1. Arithmetic Instructions 2.

More information

COMPUTER ORGANIZATION AND DESIGN

COMPUTER ORGANIZATION AND DESIGN COMPUTER ORGANIZATION AND DESIGN 5 th The Hardware/Software Interface Edition Chapter 2 Instructions: Language of the Computer 2.1 Introduction Instruction Set The repertoire of instructions of a computer

More information

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Architecture II. Computer Systems Laboratory Sungkyunkwan University MIPS Instruction ti Set Architecture II Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu Making Decisions (1) Conditional operations Branch to a

More information