Target Code Generation

Slides:



Advertisements
Similar presentations
Part IV: Memory Management
Advertisements

Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
Target code Generation Made by – Siddharth Rakesh 11CS30036 Date – 12/11/2013.
Instruction Set Design
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
1 CS 201 Compiler Construction Machine Code Generation.
1 Chapter 8: Code Generation. 2 Generating Instructions from Three-address Code Example: D = (A*B)+C =* A B T1 =+ T1 C T2 = T2 D.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
Loaders and Linkers CS 230 이준원. 2 Overview assembler –generates an object code in a predefined format »COFF (common object file format) »ELF (executable.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Chapter 6: Machine dependent Assembler Features
Lecture 11 – Code Generation Eran Yahav 1 Reference: Dragon 8. MCD
Execution of an instruction
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Part II: Addressing Modes
1 Code Generation Part II Chapter 8 (1 st ed. Ch.9) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CSc 453 Final Code Generation Saumya Debray The University of Arizona Tucson.
1 Code Generation Part II Chapter 9 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Execution of an instruction
Module : Algorithmic state machines. Machine language Machine language is built up from discrete statements or instructions. On the processing architecture,
Computer Science 210 Computer Organization More on Assembler.
1 CS 201 Compiler Construction Introduction. 2 Instructor Information Rajiv Gupta Office: WCH Room Tel: (951) Office.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
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.
Code Generation Ⅰ CS308 Compiler Theory1. 2 Background The final phase in our compiler model Requirements imposed on a code generator –Preserving the.
Chapter 7 Object Code Generation. Chapter 7 -- Object Code Generation2  Statements in 3AC are simple enough that it is usually no great problem to map.
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.
Compilers Modern Compiler Design
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Linking Loader untuk SIC/XE Machine. Lebih Lanjut mengenai Absolute Loader Shortcoming of an absolute loader –Programmer needs to specify the actual address.
Hello world !!! ASCII representation of hello.c.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
©SoftMoore ConsultingSlide 1 Code Optimization. ©SoftMoore ConsultingSlide 2 Code Optimization Code generation techniques and transformations that result.
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.
Chapter 8 Code Generation
Compilation (Semester A, 2013/14)
Machine dependent Assembler Features
Computer Science 210 Computer Organization
Optimization Code Optimization ©SoftMoore Consulting.
Compiler Construction
Computer Science 210 Computer Organization
Code Generation Part I Chapter 9
Code Generation.
Computer Programming Machine and Assembly.
Unit IV Code Generation
Lesson Objectives Aims Understand how machine code is generated
Code Generation Part I Chapter 8 (1st ed. Ch.9)
Code Generation Part I Chapter 9
Memory Management Tasks
Computer Architecture and the Fetch-Execute Cycle
TARGET CODE -Next Usage
Chapter 12 Pipelining and RISC
8 Code Generation Topics A simple code generator algorithm
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Code Generation Part II
Target Code Generation
TARGET CODE GENERATION
CSc 453 Final Code Generation
Presentation transcript:

Target Code Generation Utkarsh Jaiswal 11CS30038

Code Generation Module Intermediate Code Symbol Table Code Generation Module Target Machine Code

Pre-requisites Instruction set of target machine. Instruction addressing modes. No. of registers. Configuration of ALU

Instruction set Load/Store operations: op dest, src Eg. : Load R0, X Store X, R0 Arithmetic/ Logical operations: op dest, src1, src2 Eg.: ADD R0, R1, R2 ADD R0, R1, M where M corresponds to a memory location. Control operations: Unconditional Branch: brn L Conditional branch: BGTZ x L

Addressing Modes Register addressing mode Indirect addressing mode Index addressing mode Eg.: a[i] Immediate addressing mode Eg,: add R1, R2, 100

Issues in the design of a code generator Memory management Target programs Instruction selection Register allocation Evaluation order

Memory Management Mapping names in the source program to addresses of data objects in run-time memory is done cooperatively by the front end and the code generator. A name in a three- address statement refers to a symbol-table entry for the name. From the symbol-table information, a relative address can be determined for the name in a data area for the procedure.

Target Programs Absolute machine language: Producing an absolute machine language program as output has the advantage that it can be placed in a fixed location in memory and immediately executed. Relocatable machine language: Producing a relocatable machine language program as output allows subprograms to be compiled separately. A set of relocatable object modules can be linked together and loaded for execution by a linking loader. If the target machine does not handle relocation automatically, the compiler must provide explicit relocation information to the loader, to link the separately compiled program segments. Assembly language: Producing an assembly language program as output makes the process of code generation some what easier.

Instruction Selection The factors to be considered during instruction selection are: The uniformity and completeness of the instruction set. Instruction speed and machine idioms. Size of the instruction set. Eg., for the following address code is:                     a := b + c                     d := a + e inefficient assembly code is: MOV b, R0          R0 ← b ADD  c, R0              R0 ← c + R0 MOV R0, a           a   ← R0 MOV a, R0          R0 ← a ADD  e, R0          R0 ← e + R0 MOV R0 , d         d   ← R0 Here the fourth statement is redundant, and so is the third statement if 'a' is not subsequently used.

Register Allocation Instructions involving register operands are usually shorter and faster than those involving operands in memory. Therefore efficient utilization of registers is particularly important in generating good code. During register allocation we select the set of variables that will reside in registers at each point in the program. During a subsequent register assignment phase, we pick the specific register that a variable will reside in.

Evaluation Order The order in which computations are performed can affect the efficiency of the target code. Some computation orders require fewer registers to hold intermediate results than others.

Basic Blocks and Control Flow Graphs A basic block is the longest sequence of three-address codes with the following properties. The control flows to the block only through the first three-address code. The flow goes out of the block only through the last three-address code. A control-flow graph is a directed graph G = (V,E), where the nodes are the basic blocks and the edges correspond to the flow of control from one basic block to another. As an example the edge eij = (vi , vj) corresponds to the transfer of flow from the basic block vi to the basic block vj.

Code Generation Algorithm Computing Next-Use Information Knowing when the value of a variable will be used next is essential for generating good code. If there is a three-address instruction sequence of the form i: x = y + z . . no assignments to x between instructions i and j j: a = x + b then we say statement j uses the value of x computed at i. We also say that variable x is live at statement i. A simple way to find next uses is to scan backward from the end of a basic block keeping track for each name x whether x has a next use in the block and if not whether x is live on exit from that block.

Code Generation Algorithm Here we describe an algorithm for generating code for a basic block that keeps track of what values are in registers so it can avoid generating unnecessary loads and stores. It uses a register descriptor to keep track of what variable values are in each available register. It uses an address descriptor to keep track of the location or locations where the current value of each variable can be found. For the instruction x = y + z it generates code as follows: It calls a function getReg(x = y + z) to select registers Rx, Ry, and Rz for variables x, y, and z. If y is not in Ry, it issues the load instruction LD Ry, My where My is one of the memory locations for y in the address descriptor. Similarly, if z is not in Rz, it issues a load instruction LD Rz, Mz. It then issues the instruction ADD Rx, Ry, Rz.

Code Generation Algorithm For the instruction x = y it generates code as follows: It calls a function getReg(x = y) to select a register Ry for both x and y. We assume retReg will always choose the same register for both x and y. If y is not in Ry, issue the load instruction LD Ry, My where My is one of the memory locations for y in the address descriptor. If y is already in Ry, we issue no instruction. At the end of the basic block, it issues a store instruction ST x, R for every variable x that is live on exit from the block and whose current value resides only in a register R. The register and address descriptors are updated appropriately as each machine instruction is issued. If there are no empty registers and a register is needed, the function getReg generates a store instruction ST v, R to store the value of the variable v in some occupied register R. Such a store is called a spill. There are a number of heuristics to choose the register to spill.