Code Generation Part I Chapter 9

Slides:



Advertisements
Similar presentations
Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
Advertisements

Target Code Generation
8. Code Generation. Generate executable code for a target machine that is a faithful representation of the semantics of the source code Depends not only.
1 Compiler Construction Intermediate Code Generation.
Lec 9Systems Architecture1 Systems Architecture Lecture 9: Assemblers, Linkers, and Loaders Jeremy R. Johnson Anatole D. Ruslanov William M. Mongan Some.
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
6.S078 - Computer Architecture: A Constructive Approach Introduction to SMIPS Li-Shiuan Peh Computer Science & Artificial Intelligence Lab. Massachusetts.
April 23, 2001Systems Architecture I1 Systems Architecture I (CS ) Lecture 9: Assemblers, Linkers, and Loaders * Jeremy R. Johnson Mon. April 23,
Execution of an instruction
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
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.
M. Mateen Yaqoob The University of Lahore Spring 2014.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Chapter# 6 Code generation.  The final phase in our compiler model is the code generator.  It takes as input the intermediate representation(IR) produced.
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Computer Organization Instructions Language of The Computer (MIPS) 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
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 Part I Chapter 8 (1st ed. Ch.9)
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
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.
Advanced Computer Systems
Computer Architecture & Operations I
Overview of Instruction Set Architectures
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Computer Architecture Instruction Set Architecture
Language Translation Compilation vs. interpretation.
Chapter 11 Instruction Sets
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Computer Architecture Instruction Set Architecture
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
Introduction to Compilers Tim Teitelbaum
The University of Adelaide, School of Computer Science
Chapter 7 Subroutines Dr. A.P. Preethy
An Overview to Compiler Design
Introduction to Compiler Construction
Code Generation Part I Chapter 9
Instructions - Type and Format
Programming Languages (CS 550) Mini Language Compiler
Code Generation.
Stack Frame Linkage.
Unit IV Code Generation
Code Generation Part I Chapter 8 (1st ed. Ch.9)
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
Introduction to Micro Controllers & Embedded System Design
TARGET CODE -Next Usage
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Introduction to Compiler Construction
Compiler Construction
Code Generation Part II
Intermediate Code Generation Part I
Target Code Generation
TARGET CODE GENERATION
Review: What is an activation record?
Intermediate Code Generating machine-independent intermediate form.
CSc 453 Interpreters & Interpretation
Introduction to Compiler Construction
Programming Languages (CS 360) Mini Language Compiler
Presentation transcript:

Code Generation Part I Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007

Position of a Code Generator in the Compiler Model Source program Target program Intermediate code Intermediate code Front-End Code Optimizer Code Generator Lexical error Syntax error Semantic error Symbol Table

Code Generation Code produced by compiler must be correct Source-to-target program transformation should be semantics preserving Code produced by compiler should be of high quality Effective use of target machine resources Heuristic techniques should be used to generate good but suboptimal code, because generating optimal code is undecidable

Target Program Code The back-end code generator of a compiler may generate different forms of code, depending on the requirements: Absolute machine code (executable code) Relocatable machine code (object files for linker) Assembly language (facilitates debugging) Byte code forms for interpreters (e.g. JVM)

The Target Machine Implementing code generation requires thorough understanding of the target machine architecture and its instruction set Our (hypothetical) machine: Byte-addressable (word = 4 bytes) Has n general purpose registers R0, R1, …, Rn-1 Two-address instructions of the form op source, destination

The Target Machine: Op-codes and Address Modes Op-codes (op), for example MOV (move content of source to destination) ADD (add content of source to destination) SUB (subtract content of source from dest.) Address modes Mode Form Address Added Cost Absolute M 1 Register R Indexed c(R) c+contents(R) Indirect register *R contents(R) Indirect indexed *c(R) contents(c+contents(R)) Literal #c N/A

Instruction Costs Machine is a simple, non-super-scalar processor with fixed instruction costs Realistic machines have deep pipelines, I-cache, D-cache, etc. Define the cost of instruction = 1 + cost(source-mode) + cost(destination-mode)

Examples Instruction Operation Cost MOV R0,R1 Store content(R0) into register R1 1 MOV R0,M Store content(R0) into memory location M 2 MOV M,R0 Store content(M) into register R0 2 MOV 4(R0),M Store contents(4+contents(R0)) into M 3 MOV *4(R0),M Store contents(contents(4+contents(R0))) into M 3 MOV #1,R0 Store 1 into R0 2 ADD 4(R0),*12(R1) Add contents(4+contents(R0)) to contents(12+contents(R1)) 3

Instruction Selection Instruction selection is important to obtain efficient code Suppose we translate three-address code x:=y+z to: MOV y,R0 ADD z,R0 MOV R0,x MOV a,R0 ADD #1,R0 MOV R0,a a:=a+1 Cost = 6 Better Best ADD #1,a INC a Cost = 3 Cost = 2

Instruction Selection: Utilizing Addressing Modes Suppose we translate a:=b+c into MOV b,R0 ADD c,R0 MOV R0,a Assuming addresses of a, b, and c are stored in R0, R1, and R2 MOV *R1,*R0 ADD *R2,*R0 Assuming R1 and R2 contain values of b and c ADD R2,R1 MOV R1,a

Need for Global Machine-Specific Code Optimizations Suppose we translate three-address code x:=y+z to: MOV y,R0 ADD z,R0 MOV R0,x Then, we translate a:=b+c d:=a+e to: MOV a,R0 ADD b,R0 MOV R0,a MOV a,R0 ADD e,R0 MOV R0,d Redundant

Register Allocation and Assignment Efficient utilization of the limited set of registers is important to generate good code Registers are assigned by Register allocation to select the set of variables that will reside in registers at a point in the code Register assignment to pick the specific register that a variable will reside in Finding an optimal register assignment in general is NP-complete

Example t:=a*b t:=t+a t:=t/d t:=a*b t:=t+a t:=t/d { R1=t } { R0=a, R1=t } MOV a,R1 MUL b,R1 ADD a,R1 DIV d,R1 MOV R1,t MOV a,R0 MOV R0,R1 MUL b,R1 ADD R0,R1 DIV d,R1 MOV R1,t

Choice of Evaluation Order When instructions are independent, their evaluation order can be changed MOV a,R0 ADD b,R0 MOV R0,t1 MOV c,R1 ADD d,R1 MOV e,R0 MUL R1,R0 MOV t1,R1 SUB R0,R1 MOV R1,t4 t1:=a+b t2:=c+d t3:=e*t2 t4:=t1-t3 a+b-(c+d)*e MOV c,R0 ADD d,R0 MOV e,R1 MUL R0,R1 MOV a,R0 ADD b,R0 SUB R1,R0 MOV R0,t4 reorder t2:=c+d t3:=e*t2 t1:=a+b t4:=t1-t3

Generating Code for Stack Allocation of Activation Records t1 := a + b param t1 param c t2 := call foo,2 … func foo … return t1 100: ADD #16,SP 108: MOV a,R0 116: ADD b,R0 124: MOV R0,4(SP) 132: MOV c,8(SP) 140: MOV #156,*SP 148: GOTO 500 156: MOV 12(SP),R0 164: SUB #16,SP 172: … 500: … 564: MOV R0,12(SP) 572: GOTO *SP Push frame Store a+b Store c Store return address Jump to foo Get return value Remove frame Store return value Return to caller Note: Language and machine dependent Here we assume C-like implementation with SP and no FP