CSE 504: Compiler Design. Code Generation

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

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

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Principles of Compiler Design

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

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

CODE GENERATION Monday, May 31, 2010

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

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

Figure 28.1 Position of the Code generator

Compiler Optimization Techniques

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

Lecture 4: MIPS Instruction Set

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

Compiler Optimization and Code Generation

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

Intermediate Code Generation

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

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

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

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

CS 432 Fall Mike Lam, Professor. Code Generation

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

Lecture 4: Instruction Set Architecture

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

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

Compiler Architecture

Process Environment. Pradipta De

Chapter 2. Instruction Set Architecture (ISA)

Instructions: Language of the Computer

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

Basic Processor Design

CSE 504: Compiler Design. Instruction Selection

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Compilers and Code Optimization EDOARDO FUSELLA

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

Lecture 4: Instruction Set Design/Pipelining

CSc 453 Interpreters & Interpretation

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

4. An interpreter is a program that

Chapter 2A Instructions: Language of the Computer

Code Generation. CS 540 George Mason University

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Machine Instructions - II. Hwansoo Han

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

Lecture 04: Machine Instructions

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

Code generation and local optimization

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

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

Code generation and local optimization

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

Code Generation. Lecture 30

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

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

CS5363 Final Review. cs5363 1

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Instructions: Language of the Computer

Chapter 2: Instructions:

register allocation saves energy register allocation reduces memory accesses.

6.035 Project 3: Unoptimized Code Generation. Jason Ansel MIT - CSAIL

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

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

Computer Architecture. The Language of the Machine

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

CENG3420 Lecture 03 Review

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CPU Architecture and Instruction Sets Chapter 1

Levels of Programming. Registers

CS 61c: Great Ideas in Computer Architecture

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

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

Instruction Set. Instruction Sets Ch Instruction Representation. Machine Instruction. Instruction Set Design (5) Operation types

Compilers and computer architecture: A realistic compiler to MIPS

Instruction Sets Ch 9-10

Instruction Sets Ch 9-10

Compiler, Assembler, and Linker

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

CS3350B Computer Architecture

Introduction to Computer. Chapter 5 The LC-3. Instruction Set Architecture

CSE 504: Compiler Design. Runtime Environments

CS 211. Project 5 Infix Expression Evaluation

Computer Organization MIPS ISA

MIPS Instruction Set Architecture (2)

Code Generation. Lecture 31 (courtesy R. Bodik) CS164 Lecture14 Fall2004 1

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

CS3350B Computer Architecture MIPS Introduction

Chapter 04: Instruction Sets and the Processor organizations. Lesson 08: Processor Instructions - Part 1

COMPUTER ORGANIZATION AND DESIGN

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

Course Administration

ECE 486/586. Computer Architecture. Lecture # 7

Computer Architecture

Cpu Architectures Using Fixed Length Instruction Formats

Architecture II. Computer Systems Laboratory Sungkyunkwan University

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Transcription:

Code Generation Pradipta De pradipta.de@sunykorea.ac.kr

Current Topic Introducing basic concepts in code generation phase

Code Generation Detailed Steps The problem of generating an optimal target program for a source program is undecidable

Goals of Code Generator Code Generator must produce correct code Preserve the semantic meaning of the source code make effective use of the target resources without changing meaning Inputs to Code Generator IR of source program IR can be 3-address code, stack machine code, bytecode, linear representations Syntactic and semantic errors are eliminated, type checking done, type conversions inserted Information in symbol table Used to determine runtime address of data objects denoted by names in IR

Target Machine Instruction set architecture (ISA) of target machine determines difficulty of CG Types of target machines RISC Many registers, 3-address instructions, simple addressing modes, simple ISA X86 processors CISC Few registers, 2-address instructions, many addressing modes, several register classes, variable length instructions, and instructions with side effects Stack based: Use of stack to execute instructions JVM is a stack based abstract machine

Target machine code Absolute vs. relocatable machine code Absolute machine language program: can be placed in fixed location in memory Relocatable machine language program: set of relocatable object modules are generated Object modules are linked by the linker-loader Output of code generation Assembly language code Assembler converts to target machine code

Instruction Selection IR program into executable code sequence Level of IR High level IR CG may translate each IR into a sequence of machine instructions using code templates Low level IR may produce more efficient code Nature of ISA Uniform ISA makes CG easier less exceptions to handle Desired quality of generated code A naïve translation of IR will produce correct but inefficient code

Example of instruction selection Source code : Target Code x = y + z Source code : a = b + c d = a + e

Register Allocation Register are fast to access, but there are not many of them Values not in register is in memory takes longer to access them Two issues Register allocation: select set of variables that will reside in registers Register assignment: select the specific register in which to store the value Computation order can affect code efficiency Some order may require less registers to store intermediate results

Simple Target Machine Byte addressable machine with n general purpose registers Operations: Load: LD dst, addr, LD r, x (load value in location x into r) Store: ST x, r (store value in register r into x) Computation: OP dst, src1, src2 SUB r1,r2,r3 (r1 = r2 r3) Unconditional Jumps: BR L Conditional Jumps: B cond r, L BLTZ r,l jump to label L if value in register r is less than 0

Simple Target Machine Addressing Modes Variable name x refers to location in memory Location can be indexed a(r), where a is a variable and r is a register LD R1, a(r2) R1 = contents(a + contents(r2)) Memory location can be an integer indexed by a register LD R1, 100(R2) R1 = contents(100 + contents(r2)) Indirect addressing modes LD R1, *100(R2) R1 = contents(contents(100 + contents(r2))) Immediate addressing LD R1, #100 load integer 100 into R1

Examples Source: x = y-z Target Source: b = a[i] Target Source: x = *p Target Source: We can compute instruction cost based based on the addressing modes

Address Computation Addresses are computed based on the program layout Code Static Heap Stack

Basic Blocks The entire code is partitioned into basic blocks before generating the assembly code Basic blocks are transformed into a flow graph Provides context information

Code Generation Rules for register use Some or all operands must be in registers Registers are used to hold temporary values Registers hold global values Values used across blocks Registers are used in runtime storage management Runtime stack pointer Some registers are used to store values within a block Reserved registers: for global variables and stack management

Code Generation Data Structures to manage storage Register descriptor: keeps track of variable names whose values are in the register Initially all register descriptors are empty Address Descriptor: keeps track of location(s) where the current value of the variable can be found Can be stored in the symbol table entry

Code Generation Algorithm getreg(i) : select registers for each memory location for the instruction I Ex: getreg( x=y+z) Machine Instruction for Operations Load y into Ry Load z into Rz Issue ADD, Rx, Ry, Rz Machine Instruction for Copy Statements Adjust the register descriptor of y to include x also Ending Basic Block If temporary value, then nothing to do If variable is live on exiting a block, then ST x, R store x in its memory location Managing Register and Address Descriptors LD R, x change register descriptor, change address descriptor ST R, x change address descriptor Operation ADD Rx,Ry,Rz change register descriptor Rx, change address descriptor of x so that its only location is Rx (memory has old value) Remove Rx from address descriptor

Implementing getreg(i) Source x=y+z Steps: If y is in register, pick Ry no machine instruction required If y is not in register, but there is an empty register, then pick that register to load y load y into Ry y is not in register, and no register empty have to pick a register and make it available Register descriptor says v is in R Address descriptor says v is also somewhere else other than R If v is same as x, that is we can overwrite R since x will not be used before this instruction is executed v is not used later will be recomputed within the block If all conditions fail, then ST v, R (termed as spill) The check is done for all the registers Pick the one that requires least number of store instructions Similar rules hold for the left hand side opearnd, x

Summary Challenges of code generation Simple code generation We will look at Better heuristics for instruction selection Optimization techniques