Code Generation. M.B.Chandak Lecture notes on Language Processing

Similar documents
MODULE 32 DAG based Code Generation and Dynamic Programming

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

Principles of Compiler Design

More Code Generation and Optimization. Pat Morin COMP 3002

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

G Compiler Construction Lecture 12: Code Generation I. Mohamed Zahran (aka Z)

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

Compiler Optimization Techniques

CODE GENERATION Monday, May 31, 2010

Code Generation: Integrated Instruction Selection and Register Allocation Algorithms

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

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

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

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Goals of Program Optimization (1 of 2)

Three address representation

UNIT-V. Symbol Table & Run-Time Environments Symbol Table

CSE 504: Compiler Design. Code Generation

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Code Generation (#')! *+%,-,.)" !"#$%! &$' (#')! 20"3)% +"#3"0- /)$)"0%#"

Goal is to produce more ecient code for expressions

Intermediate Code Generation

COMPILER DESIGN - CODE OPTIMIZATION

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations

Register Allocation. CS 502 Lecture 14 11/25/08

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

Multi-dimensional Arrays

Compiler construction in4303 lecture 9

Running class Timing on Java HotSpot VM, 1

CSE 504: Compiler Design. Instruction Selection

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

Compiler Code Generation COMP360

THEORY OF COMPILATION

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Figure 28.1 Position of the Code generator

Compiler construction 2009

Compiler Optimization and Code Generation

Intermediate Representations

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

MODEL ANSWERS COMP36512, May 2016

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Compiler Optimization

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Compilers and Code Optimization EDOARDO FUSELLA

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Instruction Selection. Problems. DAG Tiling. Pentium ISA. Example Tiling CS412/CS413. Introduction to Compilers Tim Teitelbaum

Compiler Construction 2016/2017 Loop Optimizations

Optimization Prof. James L. Frankel Harvard University

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Agenda. CSE P 501 Compilers. Big Picture. Compiler Organization. Intermediate Representations. IR for Code Generation. CSE P 501 Au05 N-1

PRINCIPLES OF COMPILER DESIGN

Control-Flow Graph and. Local Optimizations

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

Lecture 12: Compiler Backend

Compiler Design and Construction Optimization

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Intermediate representation

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Dr. D.M. Akbar Hussain

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

Transform & Conquer. Presorting

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Register Allocation via Hierarchical Graph Coloring

Addressing Modes. Immediate Direct Indirect Register Register Indirect Displacement (Indexed) Stack

Programming Languages: Lecture 12

CSL 730: Parallel Programming

register allocation saves energy register allocation reduces memory accesses.

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

Register allocation. TDT4205 Lecture 31

Tour of common optimizations

Compiler Construction 2010/2011 Loop Optimizations

MIT Spring 2011 Quiz 3

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

PART 6 - RUN-TIME ENVIRONMENT. F. Wotawa TU Graz) Compiler Construction Summer term / 309

VIVA QUESTIONS WITH ANSWERS

Review. Pat Morin COMP 3002

Concurrent Processing in Client-Server Software

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

CS 406/534 Compiler Construction Putting It All Together

COMPILER CONSTRUCTION Seminar 03 TDDB

2. ADDRESSING METHODS

Lecture 25: Register Allocation

A Sparse Algorithm for Predicated Global Value Numbering

Transcription:

Code Generation M.B.Chandak Lecture notes on Language Processing

Code Generation It is final phase of compilation. Input from ICG and output in the form of machine code of target machine. Major issues Input to code generator Target program Memory management Instruction selection Allocation of registers Choice of Evaluation order

Major Issues: Input to Code Generator Output of Intermediate code generator and code optimizer. Entries of symbol tables used to generate run time addresses of data objects generated by ICG. Type of representation of ICG, generally TAC, Trees and DAG are suitable for most of code generators. Assumption: Code is free of errors.

Major Issues: Target Programs Target program may be in the form of Absolute machine code, re-locatable machine code or assembly language. Absolute machine code can be placed at any fixed location in memory and executed. Re-locatable machine code (object module) allows subprograms to be complied separately and linked and loaded in memory before execution. Advantage: Reusability Assembly language version makes process of code generation simple. //Refer class material for remaining points

Code Generation: Arrays The assembly code is used for code generation to simplify the process of code generation. The instruction like: MOV, ADD, SUB are used for implementation purpose. The code generation process of array statements like: a = b[i] and a[i] = b is as follows: The process depends upon index variable i which can be stored at different places in memory. The possible memory representation of index variable are: Register Ri Memory location Mi On Stack at offset Si from top and register A holds the address of activation record containing details of stack.

Code Generation: Arrays The code generation for arrays will be: Statement i in Register Ri i in memory Mi i in stack a = b[i] MOV b[ri], R1 MOV Mi, R MOV b(r), R1 a[i] = b MOV b, a[ri] MOV Mi, R MOV b, a(r) Example: Generate code for : Assuming i is present in memory. sum = sum + a[i] + b[j] The TAC for above code will be: T1 = sum + a[i] T2 = T1 + b[j] Sum = T2 MOV Si(A), R MOV b(r), R MOV Si(A), R MOV b, a(r)

Code Generation: Arrays The code generation for the TAC will be Statement Code Sequence Register T1 = sum + a[i] T2 = T1 + b[j] MOV X, R0 MOV A[R0], R1 ADD SUM, R1 MOV Y, R0 MOV B[R0], R2 Descriptor RO contains index i R1 contains A[i] R1 contains t1 RO contains index j R2 contains A[j] R2 contains t2 Address Descriptor i in R0 A[i] in R1 T1 in R1 j in R0 B[j] in R2 T2 in R2 ADD R1, R2 SUM = T2 MOV R2, SUM Memory location sum contains result.

Code Generation: Conditional Statements The conditions statements or loops in the program segments are implemented using if construct in TAC. For code generation phase, it is necessary to have implementation of if construct in assembly format. The two constructs: CMP and CJ are used for implementation. For example: if (x > y) goto z can be implemented as CMP X, Y //Compare X and Y CJ> Z // Conditional jump to statement Z if > condition is satisfied.

Labelling Algorithm Purpose: To determine number of registers required for executing the generated code. The algorithm makes use of DAG generated from TAC Algorithm: Operation starts with left most leaf node Algorithm label() { if [node==leaf] and [node==left child] label(n) = 1 else label(n) = 0 else label (n) = max label (n1, n2) If label(n1)!= label(n2) L1+1 Otherwise }

Code Generation: Using DAG This algorithm is also called as Heuristic Ordering Algorithm for code generation. In nodes of DAG are ordered and listed to generate the code. The root node represents operator and leaf nodes represents operands in the DAG.

Algorithm H_DAG() { While (unlisted interior node remains) do begin select (an unlisted node n all of whose parents are listed) list node n //root node will have no parents, hence will be always listed// While (left child m of node n has no unlisted parent) and (is not a leaf node) do begin list m n = m end End }

Code Generation: Using DAG Example always move left first Execution: Nodes number is already provided, if not given then assume suitable sequence a 9 + 2-5 * 1 * 4 + 6 c 7 b 10 d 11-3 + 8 e 12 Step 1: Select the parent node: as it is root, list the node 1. Left child of node 1 is node 2 and parent of node 2 is node 1. Since node 1 is already listed, list node 2. Left child of node 2 is node 6. The parent of node 6 are node 2 and node 5, since node 5 is not listed, it is necessary to find new opening. This opening should be a node with listed parent. This node is node 3, hence list node 3 and continue with node 4. Node 4, both the parents node 2 and 3 are listed, hence list node 4 and move left. Node 5, parent node 4 is listed, hence list node 5. Node 6 parent node 2 and node 5 are listed, hence list node 6. Node 6, left child is node 9, hence list node 9 but it is leaf node so ignore and check another node. In the similar fashion, control reaches to node 4 and its right child node 8, so list node 8. All the nodes are now listed. Select the last listed node for execution. 1,2,3,4,5,6,8 8, 6, 5, 4, 3, 2, 1

Peephole Optimization The theory contents of the topic are available in the study material with like Code Optimization at www.mbchandak.com (* available) It is an approach to improve the efficiency of target code. It is performed on output of code optimization phase, in order to generate efficient target code. It is considered as process carried out by code generator before finagling the target code. The peephole is small moving window, applied on code before generating target code. Peephole optimization is performed using various techniques like: Elimination of redundant instructions Flow of control optimization (Elimination of un-reachable code)* Algebraic simplifications* Use of machine Idioms.

Peephole Optimization Elimination of Redundant Instructions: Consider the following sequence: MOV R0, a MOV a, R0 Instruction 2, can be deleted for further optimization, if both the instructions are present in same block in PFG. Elimination of Unreachable code: (Refer CO notes) Flow of control optimization This process involves optimization using controlling the flow across the code. The intermediate code generator frequently produces jumps to jumps. These jumps can be eliminated in peephole optimization process

Peephole Optimization Flow of control optimization This process involves optimization using controlling the flow across the code. The intermediate code generator frequently produces jumps to jumps. These jumps can be eliminated in peephole optimization process Example 1: Goto L1 L1: Goto L2 Above code requires statement L2 to be executed. It can be replaced by Goto L2 Example 2: if (a<b) goto L1 L1: goto L2 Above code can be reduced to if(a<b) goto L2

Peephole Optimization Flow of control optimization Example 3 Goto L1 L1 if (a<b) goto L2 goto L3 Above code can be reduced to if(a<b) goto L2 Goto L3 Algebraic Simplification Code optimization notes Reduction of Strength Code optimization notes Use of Machine Idioms: This process allows to use standard machine architecture constructs instead of regular instructions. For example: i=i+1 can be replaced by i++ and i=i-1 can be replaced by i--.

Sample Code Generator: Register and Address Descriptors Refer Class notes

Sample Code Generator: getreg() algorithm Refer Class notes

Sample Code Generator: Final code generation algorithm Refer Class notes