Issues in Code Generation CPSC 388 Ellen Walker Hiram College.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Chapter 6 Intermediate Code Generation
Intermediate Code Generation
Cosc 2150 Arrays in assembly code. Variables and addresses Uncompiled ld [a], %r1 addcc %r1, 2, %r3 ARC has three addressing modes —immediate, direct,
Programming Languages and Paradigms The C Programming Language.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Intermediate Code Generation. 2 Intermediate languages Declarations Expressions Statements.
There are two types of addressing schemes:
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Computer Architecture CSCE 350
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
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)
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Assembly Language Part 5. Reference parameter/global variable model C++ reference parameters are references to the actual arguments (as opposed to copies.
COMPILER CONSTRUCTION
Compiler Principle and Technology Prof. Dongming LU Apr. 29th, 2015.
Copyright © 2012 Pearson Education, Inc. Chapter 9: Pointers.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Review: –What is an activation record? –What are the typical fields in an activation record? –What are the storage allocation strategies? Which program.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
CSC 8505 Compiler Construction Runtime Environments.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Code Generation CPSC 388 Ellen Walker Hiram College.
Intermediate Language  Compiler Model Front-End− language dependant part Back-End− machine dependant part [1/34]
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Code Generation How to produce intermediate or target code.
Computer Organization Instructions Language of The Computer (MIPS) 2.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9-1 Pointer Variables Pointer variable : Often just called a pointer, it's.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Windows Programming Lecture 03. Pointers and Arrays.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
COMPILER CONSTRUCTION
CS2100 Computer Organisation
Chapter 11 Instruction Sets
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 12 Variables and Operators
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Programming Languages (CS 550) Mini Language Compiler
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
Chapter 6 Intermediate-Code Generation
Lecture 18 Arrays and Pointer Arithmetic
PZ09A - Activation records
Lecture 8 Data structures
Compiler Design 21. Intermediate Code Generation
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Compiler Design 21. Intermediate Code Generation
Code generation and data types
Programming Languages (CS 360) Mini Language Compiler
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Issues in Code Generation CPSC 388 Ellen Walker Hiram College

Address Calculation Variables –Look up the “address” in the symbol table Arrays & Structures –Look up base address, compute and add offset Pointers –Generate code for indirect reference

Address Calculation Tools in 3- Address Code Operators from C –&xaddress of x –*xvalue contained in location x Addressing Modes –None data as before (x) –Address address of data (&x) –Indirect pointer to data (*x)

3-Address Example: x=A[i] Assume: int A[i], sizeof(int) = 2 Allow the operator t = a[x] (array ref op) t1 = i * 2compute index t2 = &A + t1compute array addr t3 = *t2get the value x = t3assign to x

Address Calculation Tools in PCode IND x –Add x to top of stack, use result as an address of item to push onto the stack IXA x –Calculate address as (top)*x+(top-1), push address onto stack

Pcode Example: x=A[i] Assume: int A[i], sizeof(int) = 2 LDA xload address of x (for later LDA Aload address of A LOD iload value of i IXA 2calculate (&A+2*i) IND 0Load item at that address STOstore it in x

Pcode Generation for Arrays Add subscript ([]) operation to syntax tree (OpKind = subs) Add a parameter (isAddr) to the code generation function: –falsereturn the value of the expression –true return the address of the expression You get C-style multi-D arrays for free (arrays of arrays…)

New Case in GenCode case ‘[]’: //Array index cout first->name; Gen_code(t->first->next, false); cout first->name << “)”; if (!isAddr) cout << “IND 0” break;

Revised to allow Multi- dimensional Arrays case ‘[]’: //Array index //push address gen_code(t->first, true); //push index gen_code(t->first->next, false); cout first->name << “)”; if (!isAddr) cout << “IND 0” break;

Write Pcode for... A[i+1] = 2*A[i] x = A[y][z] Assume: –double A –sizeof(double) =4 –sizeof (double *) = 1

Class/Struct References Address of element is base + offset Base is address of class Offset is sum of sizes of all elements previous (offset of 1st item is 0) Pointer can be treated as struct with base 0, offset = pointer value!

Address computation Assume sizeof int=2, sizeof double=4 –class st{ int A1, double A2, double A3}; –St x; Addresses: –&(x.A1) = &x –&(x.A2) = &x + 2 –&(x.A3) = &x + 6

Offset computation Add function: field_offset(st,field) returns the integer offset of field from the beginning of st Computing the address of x.j –t1 = &x + field_offset (x,j)(3 addr) –LDA x; LOD field_offset(x,j); IXA 1 (Pcode)

Example (pointer & class) class tN{ int val; class tN *left; class tN *right; }; tN *p; p = p->right;

Pcode (p = p->right) LDA p//for storing result LOD p//push p (an address) IND fieldoffset(tN,right); //*(p + offset) STN// store in p

Generating Control Code All control statements can be constructed from IF and WHILE Two additional instructions are needed –FJPjump if falseconditional jump –UJPjump unconditional jump Need to be able to generate and assign labels to statements

Code for IF if else In pcode: –Code to compute and push E –FJP Label1 –Code to execute S1 –UJP Label 2 –Label1: code to execute S2 –Label2:

Code for While while In pcode: –Label1: Code to compute and push E –FJP Label2 –Code to execute S1 –UJP Label 1 –Label2: