CSCI-GA Compiler Construction Lecture 12: Code Generation I. Hubertus Franke

Similar documents
2) Write in detail the issues in the design of code generator.

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Instruction Set Architecture

Intermediate Code. Intermediate Code Generation

Machine-Code Generation for Functions

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

Stack Allocation. Run-Time Data Structures. Static Structures

Where we are CS 4120 Introduction to Compilers Abstract Assembly Instruction selection mov e1 , e2 jmp e cmp e1 , e2 [jne je jgt ] l push e1 call e

Instruction Set Architecture (ISA)

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

Central Processing Unit (CPU)

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Symbol Tables. Introduction

MACHINE INSTRUCTIONS AND PROGRAMS

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

1 Classical Universal Computer 3

Instruction Set Architecture (ISA) Design. Classification Categories

Lab Experience 17. Programming Language Translation

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

LSN 2 Computer Processors

Processor Architectures

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

PROBLEMS (Cap. 4 - Istruzioni macchina)

(Refer Slide Time: 00:01:16 min)

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Full and Complete Binary Trees

Overview. CISC Developments. RISC Designs. CISC Designs. VAX: Addressing Modes. Digital VAX

X86-64 Architecture Guide

Algorithms and Data Structures

Instruction Set Design

CIS570 Lecture 9 Reuse Optimization II 3

Chapter 7D The Java Virtual Machine

7.1 Our Current Model

Computer Organization and Architecture

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

CIS570 Modern Programming Language Implementation. Office hours: TDB 605 Levine

Optimizing compilers. CS Modern Compilers: Theory and Practise. Optimization. Compiler structure. Overview of different optimizations

Data Structures and Algorithms Written Examination

Notes on Assembly Language


Administrative Issues

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

MICROPROCESSOR AND MICROCOMPUTER BASICS

The PageRank Citation Ranking: Bring Order to the Web

The Design of the Inferno Virtual Machine. Introduction

Click on the links below to jump directly to the relevant section

PROBLEMS. which was discussed in Section

1 The Java Virtual Machine

Q. Consider a dynamic instruction execution (an execution trace, in other words) that consists of repeats of code in this pattern:

l C-Programming l A real computer language l Data Representation l Everything goes down to bits and bytes l Machine representation Language

Obfuscation: know your enemy

Can interpreting be as fast as byte compiling? + Other developments in pqr

Data Structures Fibonacci Heaps, Amortized Analysis

İSTANBUL AYDIN UNIVERSITY

Programming Language Pragmatics

Chapter 5 Names, Bindings, Type Checking, and Scopes

EECS 583 Class 11 Instruction Scheduling Software Pipelining Intro

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

CPU Organization and Assembly Language

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Data Structure [Question Bank]

HOTPATH VM. An Effective JIT Compiler for Resource-constrained Devices

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

Computer Architecture Lecture 3: ISA Tradeoffs. Prof. Onur Mutlu Carnegie Mellon University Spring 2013, 1/18/2013

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

SYSM 6304: Risk and Decision Analysis Lecture 5: Methods of Risk Analysis

OAMulator. Online One Address Machine emulator and OAMPL compiler.

CPU Organisation and Operation

Compilers I - Chapter 4: Generating Better Code

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Module 2 Stacks and Queues: Abstract Data Types

Lumousoft Visual Programming Language and its IDE

Assembly Language: Function Calls" Jennifer Rexford!

Lecture 2 Introduction to Data Flow Analysis

Module: Software Instruction Scheduling Part I

EMSCRIPTEN - COMPILING LLVM BITCODE TO JAVASCRIPT (?!)

Inside the Java Virtual Machine

Habanero Extreme Scale Software Research Project

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Vector storage and access; algorithms in GIS. This is lecture 6

Problem Set 7 Solutions

Introduction to Data-flow analysis

Language Processing Systems

Chapter 5 Instructor's Manual

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Lecture 1: Introduction

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Semester Review. CSC 301, Fall 2015

Algebraic expressions are a combination of numbers and variables. Here are examples of some basic algebraic expressions.

6.3 Conditional Probability and Independence

Transcription:

CSCI-GA.2130-001 Compiler Construction Lecture 12: Code Generation I Hubertus Franke frankeh@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 subproblems 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

Assumptions: c and p start at 100 and 200 activation records for c and p: 300 and 364 The above assumptions mean static allocation What if it is not the case?

Stack Allocation The position of the activation record is not know until runtime Must use relative address to access elements of the activation record We need a register to keep track of the top of the stack Remember: The book assumes, for simplicity, that stack grows toward the high memory. The reality is the opposite. The code we see here is based on the book convention.

Assumptions: First word in each activation is the return address start address of p, q, and m: 100, 200, and 300 stack starts at 600

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.

Loops Most programs spend most of their execution time executing loops It is thus important to generate good code for loops. A set of nodes L in a flow graph is a loop if L contains a node e such that e is not ENTRY Only node e has predecessor outside L Every node in L has a nonempty path, completely within L, to e

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

Optimizations We can often achieve optimizations by first focusing on basic blocks Local common expressions Dead code elimination Statement reordering Algebraic rewriting

Live Variables The use of a name in a three-address statement is defined as follows. Suppose threeaddress statement i assigns a value to x. If statement j has x as an operand, and control can flow from statement i to j along a path that has no intervening assignments to x, then we say statement j uses the value of x computed at statement i. We further say that x is live at statement i.

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

Assumptions: a and b are live but c and e are not.

Algebraic Rewrite Eliminate unnecessary computations such as algebric identities: x+0 = 0+x =x x*1 = 1*x=x x-0=x x/1=x Reduction in strength: replace a more expensive operator by a cheaper one: x 2 = x*x 2*x = x+x x/2= x*0.5 Constant folding: evaluate constant expressions at compile time and replace the constant expressions by their values.

Representation of Array References Array references need special consideration: Can we use common sub expression to optimize? x?

Representation of Array References Introduce special ops ( =[] and []= ) to DAG Kills node

Representation of Array References Introduce special ops ( =[] and []= ) to DAG Sometimes have go beyond children: Kills node

Pointer Assignments Pointer aliasing is a problem: Treat similar to array with new operators: ( *= and =* ) Kills all node constructed in DAG till then Some pointer analysis is possible:

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