Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.

Slides:



Advertisements
Similar presentations
Target Code Generation
Advertisements

Target code Generation Made by – Siddharth Rakesh 11CS30036 Date – 12/11/2013.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Intermediate Code Generation
Course Outline Traditional Static Program Analysis Software Testing
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
PSUCS322 HM 1 Languages and Compiler Design II Basic Blocks Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
1 Intermediate representation Goals: encode knowledge about the program facilitate analysis facilitate retargeting facilitate optimization scanning parsing.
1 CS 201 Compiler Construction Lecture 1 Introduction.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CSC 8505 Compiler Construction Intermediate Representations.
Intermediate Code. Local Optimizations
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
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.
Lexical Analysis Uses formalism of Regular Languages – Regular Expressions – Deterministic Finite Automata (DFA) – Non-deterministic Finite Automata (NDFA)
Compiler Chapter# 5 Intermediate code generation.
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
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,
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Compilers Modern Compiler Design
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Code Generation CPSC 388 Ellen Walker Hiram College.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Topic #9: Target Code EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Chapter10: Code generator. 2 Code Generator Source Program Target Program Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator.
CS 404 Introduction to Compiler Design
High-level optimization Jakub Yaghob
Intermediate code Jakub Yaghob
Intermediate Code Generation
Intermediate Representations Hal Perkins Autumn 2011
Three-Address Implementation
Unit IV Code Generation
Chapter 6 Intermediate-Code Generation
CS 201 Compiler Construction
Data Flow Analysis Compiler Design
8 Code Generation Topics A simple code generator algorithm
Compiler Construction
Compiler Construction
Target Code Generation
Lexical Analysis Uses formalism of Regular Languages
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1
CS 201 Compiler Construction
Presentation transcript:

Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building LR parse tables Compiling Expressions Compiling Expressions Build control flow intermediates Build control flow intermediates Generate target code Generate target code Optimize intermediate Optimize intermediate

Implementation “Done” to date Symbol Table Symbol Table Generic List routines Generic List routines Expressions using lex, yacc input Expressions using lex, yacc input

Three-Address Code section (new), pp 467 (old) Assembler for generic computer Assembler for generic computer Types of statements 3-address (Dragon) Types of statements 3-address (Dragon) Assignment statement x = y op z Assignment statement x = y op z Unconditional jump br label Unconditional jump br label Conditional jump if( cond ) goto label Conditional jump if( cond ) goto label Parameter x Parameter x Call statement call f Call statement call f

Example “Source” a = ((c-1) * b) + (-c * b)

Example 3-Address t1 = c - 1 t2 = b * t1 t3 = -c t4 = t3 * b t5 = t2 + t4 a = t5

Three-Address Implementation (Quadruples, sec 6.2.2; pp 470-2) oparg1arg2result 0-c1t1 1*bt1t2 2uminusct3 3*bt3t4 4+t2t4t5 5=t5a

Three-Address Implementation (Triples, section 6.2.3) oparg1arg2 0-c1 1*b(0) 2uminusc 3*b(2) 4+(1)(3) 5=a(4)

Three-Address Implementation N-tuples (my choice – and yours ??) N-tuples (my choice – and yours ??) Lhs = oper(op 1, op 2, …, op n ) Lhs = oper(op 1, op 2, …, op n ) Lhs = call(func, arg 1, arg 2, … arg n ) Lhs = call(func, arg 1, arg 2, … arg n ) If condOper(op 1, op 2, Label) If condOper(op 1, op 2, Label) br Label br Label

Three-Address Code 3-address operands 3-address operands Variable Variable Constant Constant Array Array Pointer Pointer

Control Flow Graph sec 8.4 (new); sec 9.4 (old) Nodes are Basic Blocks Nodes are Basic Blocks Single entry, single exit Single entry, single exit No branch exempt (possibly) at bottom No branch exempt (possibly) at bottom Edges represent one possible flow of execution between two basic blocks Edges represent one possible flow of execution between two basic blocks Whole CFG represents a function Whole CFG represents a function

Bubble Sort begin; int A[10]; main(){ int i,j; int i,j; Do 10 i = 0, 9, 1 Do 10 i = 0, 9, 1 10 A[i] = random(); Do 20 i = 1, 9, 1 Do 20 i = 1, 9, 1 Do 20 j = 1, 9, 1 Do 20 j = 1, 9, 1 20 if( A[j] > A[j+1]) swap(j); }

Bubble Sort (cont.) int swap(int i) { int temp; int temp; temp = A[i]; temp = A[i]; A[i] = A[i+1]; A[i] = A[i+1]; A[i+1] = temp; A[i+1] = temp;}end;

Example Generate 3-addr code for BubbleSort

Building CFG alg 8.5 (pp 526-7); alg 9.1(p 529) Starting with 3-addr code Starting with 3-addr code Each leader starts a basic block which ends immediately before the next leader Each leader starts a basic block which ends immediately before the next leader ID “leaders” (heads of basic blocks) ID “leaders” (heads of basic blocks) First statement is a leader First statement is a leader Any statement that is the target of a conditional of unconditional goto is a leader Any statement that is the target of a conditional of unconditional goto is a leader Any statement immediately following a goto or conditional goto is a leader Any statement immediately following a goto or conditional goto is a leader Each leader starts a basic block which ends immediately before the next leader Each leader starts a basic block which ends immediately before the next leader

Example Build control flow graphs for BubbleSort

Code Generation Pick three registers to be used throughout Pick three registers to be used throughout Assuming stmt of form dest = s1 op s2 Assuming stmt of form dest = s1 op s2 Generate code by: Generate code by: Load source 1 into r5 Load source 1 into r5 Load source 2 into r6 Load source 2 into r6 R7 = r5 op r6 R7 = r5 op r6 Store r7 into destination Store r7 into destination