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

Similar documents
CSE 504: Compiler Design. Code Generation

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

CODE GENERATION Monday, May 31, 2010

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

Figure 28.1 Position of the Code generator

Principles of Compiler Design

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

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

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Intermediate representation

Compiler Optimization Techniques

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

Instructions: Language of the Computer

Lecture 4: Instruction Set Design/Pipelining

Intermediate Representations

Tour of common optimizations

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

Intermediate Representations

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

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

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

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

CSc 453. Code Generation I Christian Collberg. Compiler Phases. Code Generation Issues. Slide Compilers and Systems Software.

Architectures. Code Generation Issues III. Code Generation Issues II. Machine Architectures I

Introduction. CSc 453. Compilers and Systems Software. 19 : Code Generation I. Department of Computer Science University of Arizona.

Lecture 2: The Art of Emulation

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

Intermediate Code Generation

Intermediate Code & Local Optimizations

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

Principles of Compiler Design

Chapter 04: Instruction Sets and the Processor organizations. Lesson 20: RISC and converged Architecture

CS 24: INTRODUCTION TO. Spring 2018 Lecture 3 COMPUTING SYSTEMS

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

55:132/22C:160, HPCA Spring 2011

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

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

We can emit stack-machine-style code for expressions via recursion

Front End. Hwansoo Han

CS415 Compilers. Intermediate Represeation & Code Generation

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

Where we are. Instruction selection. Abstract Assembly. CS 4120 Introduction to Compilers

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

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

Lecture 04: Machine Instructions

Intermediate Representations

CSE 401/M501 Compilers

COMPILER CONSTRUCTION Seminar 03 TDDB

Intermediate Representations

Register Allocation (via graph coloring) Lecture 25. CS 536 Spring

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

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Announcements. Class 7: Intro to SRC Simulator Procedure Calls HLL -> Assembly. Agenda. SRC Procedure Calls. SRC Memory Layout. High Level Program

THEORY OF COMPILATION

Code Generation: Integrated Instruction Selection and Register Allocation Algorithms

CS 406/534 Compiler Construction Putting It All Together

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

Compiler Architecture

Intermediate Representation (IR)

Compiler Construction D7011E

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Lecture 21 CIS 341: COMPILERS

Global Register Allocation

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

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

Compilation /17a Lecture 10. Register Allocation Noam Rinetzky

register allocation saves energy register allocation reduces memory accesses.

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

CSC D70: Compiler Optimization

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Optimization Prof. James L. Frankel Harvard University

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

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

CS 61c: Great Ideas in Computer Architecture

Programming Language Processor Theory

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

More Code Generation and Optimization. Pat Morin COMP 3002

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

UNIT IV INTERMEDIATE CODE GENERATION

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

PRINCIPLES OF COMPILER DESIGN

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

Lecture 4: Instruction Set Architecture

Chapter 11. Instruction Sets: Addressing Modes and Formats. Yonsei University

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

Where we are. What makes a good IR? Intermediate Code. CS 4120 Introduction to Compilers

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

CSc 453 Interpreters & Interpretation

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

Instructions: Language of the Computer

Intermediate Code & Local Optimizations. Lecture 20

Lecture Outline. Code Generation. Lecture 30. Example of a Stack Machine Program. Stack Machines

Compilers and Code Optimization EDOARDO FUSELLA

Intermediate Representations & Symbol Tables

Instruction Set Architecture

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

Lecture 3 Local Optimizations, Intro to SSA

Transcription:

G22.2130-001 Compiler Construction Lecture 12: Code Generation I Mohamed Zahran (aka Z) mzahran@cs.nyu.edu

semantically equivalent + symbol table Requirements Preserve semantic meaning of source program Make effective use of available resources of target machine Code generator itself must run efficiently Challenges Problem of generating optimal target program is undecidable Many subprogroblems encountered in code generation are computationally intractable

Main Tasks of Code Generator Instruction selection: choosing appropriate target-machine instructions to implement the IR statements Registers allocation and assignment: deciding what values to keep in which registers Instruction ordering: deciding in what order to schedule the execution of instructions

Design Issues of a Code Generator Input three-address presentations (quadruples, triples, ) Virtual machine presentations (bytecode, stack-machine, ) Linear presentation (postfix, ) Graphical presentation (syntax trees, DAGs, )

Design Issues of a Code Generator Target program Instruction set architecture (RISC, CISC) Producing absolute machine-language program Producing relocatable machine-language program Producing assembly language programs

Design Issues of a Code Generator Instruction Selection The complexity of mapping IR program into code-sequence for target machine depends on: Level of IR (high-level or low-level) Nature of instruction set (data type support) Desired quality of generated code (speed and size)

Design Issues of a Code Generator Register Allocation Selecting the set of variables that will reside in registers at each point in the program Register Assignment Picking the specific register that a variable will reside in

Design Issues of a Code Generator Evaluation Order Selecting the order in which computations are performed Affects the efficiency of the target code Picking a best order is NP-complete Some orders require fewer registers than others

Simple Target-Machine Load/store operations LD dst, addr ST x, r Computation operations OP dst, src1, src2 Jump operations BR L Conditional jumps Bcond r, L Byte addressable n registers: R0, R1, Rn-1

Simple Target-Machine Addressing modes variable name a(r) means contents(a + contents(r)) *a(r) means: contents(contents(a + contents(r))) immediate: #constant (e.g. LD R1, #100)

Simple Target-Machine Cost cost of an instruction = 1 + cost of operands cost of register operand = 0 cost involving memory and constants = 1 cost of a program = sum of instruction costs

Examples X = Y - Z b = a[i] (8-byte elements) x = *p

More Examples a[j] = c *p = y if X<Y goto L

Generating Code for Handling the Stack Size and layout of activation records are determined by the code generator using information from symbol table saves return address at beginning of activation record of callee constants giving address of beginning of activation record of callee transfers control to target code of procedure callee CALL callee RETURN

Basic Blocks and Flow Graphs Graph presentation of intermediate code Nodes of the graph are called basic blocks Edges indicate which block follows which other block. The graph is useful for doing better job in: Register allocation Instruction selection

Basic Blocks Definition: maximal sequence of consecutive instructions such that Flow of control can only enter the basic block from the first instruction Control leaves the block only at the last instruction Each instruction is assigned to exactly one basic block

Fist we determine leader instructions:

First we determine leader instructions:

Fist we determine leader instructions: Basic block starts with a leader instruction and stops before the following leader instruction.

DAG Representation of Basic Blocks Leaves for initial values of variables (we may not know the values so we use a0, b0, ) Node for each expression Node label is the expression operation Next to the node we put the variable(s) for which the node produced last definition Children of a node consist of nodes producing last definition of operands

Finding Local Common Subexpressions

Dead Code Elimination From the basic block DAG: Remove any root node that has no live variables Repeat until no nodes can be removed

So Skim: 8.3.3, 8.5.4, 8.5.5, 8.5.6, and 8.5.7 Read: 8.1 -> 8.5