Compilers Modern Compiler Design

Slides:



Advertisements
Similar presentations
1 Code Generation The target machine Instruction selection and register allocation Basic blocks and flow graphs A simple code generator Peephole optimization.
Advertisements

Compiler Construction
Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
Target Code Generation
Target code Generation Made by – Siddharth Rakesh 11CS30036 Date – 12/11/2013.
Intermediate Code Generation
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.
1 CS 201 Compiler Construction Machine Code Generation.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Intermediate Representations Saumya Debray Dept. of Computer Science The University of Arizona Tucson, AZ
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt.
1 Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Execution of an instruction
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.
Lecture 23 Basic Blocks Topics Code Generation Readings: 9 April 17, 2006 CSCE 531 Compiler Construction.
Intermediate Code CS 471 October 29, CS 471 – Fall Intermediate Code Generation Source code Lexical Analysis Syntactic Analysis Semantic.
Code Generation Professor Yihjia Tsai Tamkang University.
1 CS 201 Compiler Construction Lecture 1 Introduction.
CSC 8505 Compiler Construction Intermediate Representations.
Lecture 25 Generating Code for Basic Blocks Topics Code Generation Readings: April 19, 2006 CSCE 531 Compiler Construction.
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.
Introduction For some compiler, the intermediate code is a pseudo code of a virtual machine. Interpreter of the virtual machine is invoked to execute the.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
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.
Execution of an instruction
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
1 Code Generation. 2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Computer Organization Instructions Language of The Computer (MIPS) 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
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.
Code Generation The target machine
Chapter 8 Code Generation
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
High-level optimization Jakub Yaghob
Assembly Language Assembly Language
Unit IV Code Generation
Chapter 6 Intermediate-Code Generation
CS 201 Compiler Construction
CS 201 Compiler Construction
Introduction to Micro Controllers & Embedded System Design
Interval Partitioning of a Flow Graph
TARGET CODE -Next Usage
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Intermediate Code Generation
Compiler Construction
Taken largely from University of Delaware Compiler Notes
Code Generation Part II
Target Code Generation
TARGET CODE GENERATION
Intermediate Code Generating machine-independent intermediate form.
Bubble Sort begin; int A[10]; main(){ int i,j; Do 10 i = 0, 9, 1
CS 201 Compiler Construction
Presentation transcript:

Compilers Modern Compiler Design Supplementary Note 1 Code Generation (ASU88: Simplified Introduction) NCYU C. H. Wang

The Target Machine The target machine is a byte-addressable machine with four bytes to a word and n general purpose registers. It has two address instruction of the form: op source, destination

Op-codes MOV (move source to destination) ADD (add source to destination) SUB (subtract source from destination)

Addressing Mode Literal #c constant c 1

Examples MOV R0, M MOV 4(R0), M MOV *4(R0), M MOV #1, R0 Store the contents of register R0 into memory location M. MOV 4(R0), M Store the value contents(4+contents(R0)) into memory location M. MOV *4(R0), M Store the value contents(contents(4+contrnts(R0))) in to memory location M. MOV #1, R0 Load the constant 1 into register R0

Instruction Costs The cost of an instruction to be one plus the costs associated with the source and destination address modes. MOV R0, R1 (1) MOV R0, M (2) MOV #1, R0 (2) SUB 4 (R0), *12 (R1) (3) contents(contents(12+contents(R1)))- contents(4+contents(R0))

Example a := b + c 1. MOV b, R0 2. MOV b, a ADD c, R0 ADD c, a MOV R0, a (cost=6) (cost=6) 3. R0, R1, R2 contain 4. R1, R2 contain the addresses of a, b and c the values of b and c MOV *R1, *R0 ADD R2, R1 ADD *R2, *R0 MOV R1, a (cost=2) (cost=3)

Basic Blocks and Flow Graphs A basic block is a sequence of consecutive statements in which control enters at the beginning and leaves at the end without halt or possibility of branching except at the end.

Partition into Basic Blocks First determine the set of leaders The first statement is a leader. Any statement that is the target of a conditional or unconditional goto is a leader. Any statement that immediately follows a goto or conditional goto statement is a leader. For each leader, its basic block consists of the leader and all statements up to but not including the next leader or the end of the program.

Example (1/2) Source code begin prod:=0; i=1; do begin prod :=prod + a[i] * b[i]; i:=i+1 end while i<=20

Example (2/2) Three-address code (the partition) B1 B2

Flow Graphs

A Simple Code Generation (1) For each three-address statement of the form x:=y op z we perform Invoke a function getreg to determine the location L where the result of the computation y op z should be stored. Consult the address descriptor for y to determine y’, the current location y. If the value of y is not already in L, generate the instruction MOV y’ L to place a copy of y in L.

A Simple Code Generation (2) Generate the instruction OP z’, L where z’ is a current location of z. Update the address descriptor of x to indicate that x is in location L. If L is a register, update its descriptor to indicate that it contains the value of x, and remove x from all other register descriptor. If the current values of y and/or z have no next uses, are not live on exit from the block, and are in registers, alter the register descriptor to indicate that, after execution of x:=y op z, those registers no longer will contain y and/or z, respectively.

Example d:=(a-b)+(a-c)+(a-c) Three-address code t:=a-b u:=a-c v:=t+u d:=v+u

Code sequence

Generating code for indexed assignments

Generating code for pointer assignments

Conditional Statements CMP x,y CJ< z Jump to z if the condition code is negative or zero x:=y+z if x<0 goto z MOV y, R0 ADD z, R0 MOV R0, x

The DAG Representation of Basic Blocks DAG (Directed Acyclic Graphs) A DAG for a basic block Leaves labeled by unique identifiers. Interior nodes labeled by operator symbols Interior nodes optionally given a sequence of identifiers. The Interior nodes represent computed values, and the identifiers labeling a node are deemed to have that value.

Example (1/2)

Example (2/2)

Constructing a DAG (1/2) Suppose the current three-address statement is either (i) x:=y op z, (ii) x:=op y or (iii) x:=y. We treat a relational operator like if i<=20 goto as case (i), with x undefined. If node(y) is undefined, create a leaf labeled y, and let node(y) be this node. In case (i), if node(z) is undefined, create a leaf labeled z and let that leaf be node(z).

Constructing a DAG (2/2) In case (i), determine if there is a node labeled op, whose left child is node(y) and whose right child is node(z). If not, create such node. In either event, let n be the node found or created. In case (ii), determine if there is a node labeled op, whose lone child is node(y). If not, create such node, and let n be the node found or created. In case (iii), let n be the node(y). Delete x from the list of attached identifiers for node(x). Append x to the list of attached identifiers for the node n found in (2) and set node(x) to n.

Example

Generating Code from DAGS

Code sequence

Rearrange the Order of the Statements

Other Topics Code Optimization Dynamic Programming Code Generation Algorithm