Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
The University of Adelaide, School of Computer Science
Lecture 6 Programming the TMS320C6x Family of DSPs.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
ECE 232 L6.Assemb.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 6 MIPS Assembly.
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
1 Compiler Construction Intermediate Code Generation.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
COMPILERS Basic Blocks and Traces hussein suleman uct csc3005h 2006.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
The University of Adelaide, School of Computer Science
Program Representations. Representing programs Goals.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
CPSC Compiler Tutorial 8 Code Generator (unoptimized)
Lecture 26 Epilogue: Or Everything else you Wanted to Know about Compilers (more accurately Everything else I wanted you to Know) Topics Getreg – Error.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
PSUCS322 HM 1 Languages and Compiler Design II Re-Introduction from CS 321 Material provided by Prof. Jingke Li Stolen with pride and modified by Herb.
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)
1 Handling nested procedures Method 1 : static (access) links –Reference to the frame of the lexically enclosing procedure –Static chains of such links.
Cse322, Programming Languages and Compilers 1 6/14/2015 Lecture #3, April 11, 2007 Boolean expressions Positional encoding Short circuit evaluation Conditional.
CS 536 Spring Code generation I Lecture 20.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Cse322, Programming Languages and Compilers 1 6/22/2015 Lecture #4, April 12, 2007 Strings (representation, byte operation, copying), Structures (representation,
Cse321, Programming Languages and Compilers 1 6/23/2015 Lecture #15, March. 5, 2007 Judgments for mini-Java Multiple type environments Class Hierarchy.
Run time vs. Compile time
PSUCS322 HM 1 Languages and Compiler Design II Project 4 Hints Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Code Generation Compiler Baojian Hua
Cse322, Programming Languages and Compilers 1 7/13/2015 Lecture #2, April 5, 2007 Overview of backend issues (storage locations, registers, aliasing),
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Lesson 3 Two topics 1. Semantic Analysis 2. Translation to intermediate code Presented as separated topics but can be implemented together.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
1 CIS 461 Compiler Design and Construction Winter 2012 Lecture-Module #16 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon.
Simple Code Generation CS153: Compilers. Code Generation Next PS: Map Fish code to MIPS code Issues: –eliminating compound expressions –eliminating variables.
Run-Time Storage Organization Compiler Design Lecture (03/23/98) Computer Science Rensselaer Polytechnic.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Code Generation CPSC 388 Ellen Walker Hiram College.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Intermediate Code Generation CS 671 February 14, 2008.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
CS 404 Introduction to Compiler Design
6.001 SICP Compilation Context: special purpose vs. universal machines
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Instructions - Type and Format
Programming Languages (CS 550) Mini Language Compiler
Procedures – Overview Lecture 19 Mon, Mar 28, 2005.
CSE401 Introduction to Compiler Construction
Three-address code A more common representation is THREE-ADDRESS CODE . Three address code is close to assembly language, making machine code generation.
PZ09A - Activation records
Topic 3-a Calling Convention 1/10/2019.
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Yan Shi CS/SE 2630 Lecture Notes
Assignments and Procs w/Params
Procedure Linkages Standard procedure linkage Procedure has
Review: What is an activation record?
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:

Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent strategy OO-langauages

Cse322, Programming Languages and Compilers 2 6/21/2015 Assignments Reading –Read chapter 8 sections 8.1 – 8.3 –Possible Quiz Wednesday on the reading. Programming assignment #3 is now available on the class website under the assignments link.

Cse322, Programming Languages and Compilers 3 6/21/2015 Array Access Array access is a lot like variable access in a block structured or OO-language We need to compute an address and an offset Y[0] = 6 Y[1] = 8 Y[2] = 12 X = 23 rA  x= 1  y= 3 => r1 loadAO rA,r1 => =  x = 1

Cse322, Programming Languages and Compilers 4 6/21/2015 Array Access #2 The instruction LoadAO takes a base address and an offset. In an array the offset includes the index of the array expression, as well as the delta from the rA Y[0] = 6 Y[1] = 8 Y[2] = 12 X = 23 rA  x= 1  y= 3 => r1... => r exp Add r1,r exp => r off loadAO rA,r off => r2 y[exp]

Cse322, Programming Languages and Compilers 5 6/21/2015 Alternate approach Another way is to approach this is to include both the rA and the  y into the base and just use the index as the offset. Compare => r1... => r exp Add r1,r exp => r off loadAO rA,r off => r2 => r1 Add rA,r1 => r base... => r exp loadAO r base,r exp => r2

Cse322, Programming Languages and Compilers 6 6/21/2015 ML program To add this to our ML program we can proceed either way. We need to handle element size and 1 based indexing as well. Consider y[2*x] loadI 2 => r1 => r2 loadAO rA,r2 => r3 Mul r1,r3 => r4 ! 2*x => r5 loadI 1 => r7 Sub r4,r7 => r4 ! 2*x – 1 handle 1-based loadI 4 => r6 Mul r4,r6 => r4 ! (2*x – 1) * 4 handle size Add r5,r4 => r4 ! Add the offset of y loadAO rA,r4 => r8 ! Access the correct word

Cse322, Programming Languages and Compilers 7 6/21/2015 fun expr dict node = case node of ArrayElm(array as(Var(NONE,y)),index,SOME Type) => let val size = (case Type of Bool => 1 | Int => 4 | Real => 8) val nbased = 1 val rindex = expr dict index val t1 = base array val t2 = offset array val rsize = NextRegister() val rbase = NextRegister() val result = NextRegister() in emit (LoadI(Int.toString nbased,rbase)); emit (Arith(SUB,rindex,rbase,rindex)); emit (LoadI(Int.toString size,rsize)); emit (Arith(MUL,rindex,rsize,rindex)); emit (Arith(ADD,t2,rindex,rindex)); emit (LoadAO(t1,rindex,result)); result end

Cse322, Programming Languages and Compilers 8 6/21/2015 Case stmt Consider a case statement over integers case exp of 0 => exp0 | 1 => exp1 | 2 => exp2 | 3 => exp3 How can we efficiently translate this? –Nested if-then-else –Binary search –Jump table Compact set of contiguous integers. Like Tags in a datatype

Cse322, Programming Languages and Compilers 9 6/21/2015 Jump Table JumpI Top Table: JumpI L0 JumpI L1 JumpI L2 JumpI L3 Top:... => r exp LoadI Table => r 0 Add r 0,r exp => r target JumpR r target LO: L1:

Cse322, Programming Languages and Compilers 10 6/21/2015 Add two new IR instructions datatype IR = LoadI of (string * Reg) | LoadAO of (Reg * Reg * Reg) | Arith of (Op * Reg * Reg * Reg) | JumpR of Reg | Move of Reg *Reg

Cse322, Programming Languages and Compilers 11 6/21/2015 fun caseExp dict exp arms = let val ns = map fst arms val [Table,Top,Bottom] = NextLabel 3 fun target (n,expr) = (n,hd (NextLabel 1),expr) val labels = map target arms fun emitJump (0,lab,exp) = emitAt Table (JumpI lab) | emitJump (_,lab,exp) = emit (JumpI lab) val result = NextRegister() val index = NextRegister () fun emitArm (_,lab,exp) = let val _ = emitAt lab Nop val r = expr dict exp in emit (Move(r,result)); emit (JumpI Bottom) end val _ = emit (JumpI Top) val _ = map emitJump labels val _ = emitAt Top Nop val rexp = expr dict exp in emit (LoadI(“L”^Int.toString Table,index)); emit (Arith(ADD,index,rexp,index)); emit (JumpR index); map emitArm labels; emitAt Bottom Nop; result end

Cse322, Programming Languages and Compilers 12 6/21/2015 jumpI -> L2 L1: jumpI -> L4 jumpI -> L5 jumpI -> L6 L2: nop => r3 loadAO rA,r3 => r4 loadI L1 => r2 Add r2,r4 => r2 jumpR r2 L4: nop => r5 loadAO rA,r5 => r6 Move r6 => r1 jumpI -> L3 L5: nop loadI 5 => r7 Move r7 => r1 jumpI -> L3 L6: nop loadI 2 => r8 Mul r8,r4 => r9 Move r9 => r1 jumpI -> L3 L3: nop case y of 0 => x | 1 => 5 | 2 => 2 * y

Cse322, Programming Languages and Compilers 13 6/21/2015 Procedure Call AR creation is a cooperative effort Shared by the caller and the callee Has 4 parts –Precall –Postreturn –Prolog –Epilog Precall & Postcall can be split prolog precall postcall epilog prolog epilog Call Return

Cse322, Programming Languages and Compilers 14 6/21/2015 Setting up parameters –On stack –In memory –In registers Setting up the call –The address to point to –The return address –Special registers like the activation record or object pointer Handling returned values

Cse322, Programming Languages and Compilers 15 6/21/2015 Machine dependencies Many of these set up operations will be machine dependent To avoid machine dependencies at the IR level we define some abstract IR instructions Precall - param Call - call PostCall - retval For each machine these will be implemented in a different way

Cse322, Programming Languages and Compilers 16 6/21/2015 Example f(3,z,2*x) loadI 3 => r1 => r2 loadAO rA,r2 => r3 loadI 2 => r4 => r5 loadAO rA,r5 => r6 Mul r4,r6 => r7 param r1 param r3 param r7 => r8 call r8 retval r9

Cse322, Programming Languages and Compilers 17 6/21/2015 fun expr dict node = case node of Call(_,f,_,args) => let val rargs = map (expr dict) args fun emitParam r = emit (Param r) val rf = NextRegister() val result = NextRegister() in map emitParam rargs; emit (LoadI(f,rf)); emit (Callx rf); emit (Retval result); result end

Cse322, Programming Languages and Compilers 18 6/21/2015 OO languages The code shape for OO languages is different from other languages in several places –Instance variables –Method calls These must use the same code no matter where they are used, since we generate code from its abstract syntax. In different instances, objects may different numbers of instance variables and methods. –Uniform way of laying out these things is necessary Consider two different ways of laying out objects

Cse322, Programming Languages and Compilers 19 6/21/2015 class three fee fum class two fee foe class one fee fie Object  Obj a X=2.0 Y=0.1 Z = N =1 Obj c X=5.0 Y=3.1 N =1 Obj b X=5.0 Y=3.0 Z = N =1 #1

Cse322, Programming Languages and Compilers 20 6/21/2015 class three fee fum Object  class two fee fum foe class one fee fum foe fie fum … fee … fee … foe … fee … fie … Obj c X=5.0 Y=3.1 N =1 Obj a X=2.0 Y=0.1 Z = N =1 Obj b X=5.0 Y=3.0 Z = N =1 #2

Cse322, Programming Languages and Compilers 21 6/21/2015 Project #1 Project #1 will be assigned Thursday –It will be due in two weeks time, May 3 rd –There will be no daily assignments next week, so get started as soon as you get the project #1 description Project 1 will translate MiniJava through a sequence of IR languages. These languages will be different (but also similar) to the IR we have seen in class. The first IR is located on the notes web page. Also see the Slides from Jenke Li about templates for MiniJava Translation (available on the notes web page).

Cse322, Programming Languages and Compilers 22 6/21/2015 IR #1 datatype BINOP = ADD | SUB | MUL | DIV | AND | OR datatype RELOP = EQ | NE | LT | LE | GT | GE datatype EXP = BINOP of BINOP * EXP * EXP | CALL of EXP * EXP list | MEM of EXP | NAME of string | TEMP of int | PARAM of int | VAR of int | CONST of string | STRING of string | ESEQ of STMT * EXP | EXPLIST of EXP list

Cse322, Programming Languages and Compilers 23 6/21/2015 IR #1 continued and STMT = MOVE of EXP * EXP | JUMP of EXP | CJUMP of RELOP * EXP * EXP * EXP | LABEL of EXP | CALLST of EXP * EXP list | RETURN of EXP | STMTlist of STMT list; datatype FUNC = FUNC of string * int * int * STMT list

Cse322, Programming Languages and Compilers 24 6/21/2015 Assignment #3 CS322 Prog Lang & Compilers Prog Assignment #3 Assigned Wednesday April 12, Due Monday, April 17, 2006 This assignment is to extend the stmt program discussed in class (and available for download) so that it can translate while loops fun stmt dict x = case x of (While(tst,body)) => Base your solution on the if-then-else statement and the discussion in the text book about while-loops. You don’t need to add any new IR instructions. Be sure and test your code, print out the tests, and hand them in.