Cse322, Programming Languages and Compilers 1 6/22/2015 Lecture #4, April 12, 2007 Strings (representation, byte operation, copying), Structures (representation,

Slides:



Advertisements
Similar presentations
Instruction Set Design
Advertisements

Goal: Write Programs in Assembly
Intermediate Code Generation
The University of Adelaide, School of Computer Science
Programming Languages and Paradigms
1 CS 201 Compiler Construction Machine Code Generation.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Compiler construction in4020 – lecture 10 Koen Langendoen Delft University of Technology The Netherlands.
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
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
The University of Adelaide, School of Computer Science
Chapter 9 Imperative and object-oriented languages 1.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
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.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
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/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent.
Run time vs. Compile time
Run-time Environment and Program Organization
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
Cse322, Programming Languages and Compilers 1 7/13/2015 Lecture #2, April 5, 2007 Overview of backend issues (storage locations, registers, aliasing),
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
Imperative Programming
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Cosc 2150: Computer Organization
Compiler Construction
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Compiler Chapter# 5 Intermediate code generation.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Memory: Relocation.
Execution of an instruction
Arithmetic Expressions
CSC 212 Object-Oriented Programming and Java Part 2.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
EGR 2261 Unit 11 Pointers and Dynamic Variables
Week 3 - Friday CS222.
Java Primer 1: Types, Classes and Operators
Conditional Branches What distinguishes a computer from a simple calculator is its ability to make decisions Decisions are made using the if statement,
CS 11 C track: lecture 8 Last week: hash tables, C preprocessor
Intermediate Representations
Code Shape III Booleans, Relationals, & Control flow
Intermediate Representations
Lecture 15: Code Generation - Instruction Selection
Procedure Linkages Standard procedure linkage Procedure has
Presentation transcript:

Cse322, Programming Languages and Compilers 1 6/22/2015 Lecture #4, April 12, 2007 Strings (representation, byte operation, copying), Structures (representation, anonymous value, field information, layout), Control Flow (basic blocks, generating code, loops).

Cse322, Programming Languages and Compilers 2 6/22/2015 Assignments Reading –Read chapter 7 sections and 7.11 –Possible Quiz Monday on the reading.

Cse322, Programming Languages and Compilers 3 6/22/2015 Strings Strings are usually represented as byte sequences Operations on strings do not generally map onto hardware operations. –Load instructions load whole words –Strings are composed of bytes –Shifting and masking are often necessary String representations are often both language and machine dependent. –In C strings are null terminated adjacent arrays of char –In Java strings are byte arrays with their length stored explicitly.

Cse322, Programming Languages and Compilers 4 6/22/2015 Representation abc\0abc3 abc 3 Null terminated Length Prefixed Length plus pointer 2 Note that sharing is possible with the length plus pointer

Cse322, Programming Languages and Compilers 5 6/22/2015 Assignment of individual characters A[ 1 ] = b[ 2 ] => r b cloadAI r b,2 => r 2 => r a cstoreAI r 2 => r a,1 This is only possible if the machine has byte level load and store. Many machines do not.

Cse322, Programming Languages and Compilers 6 6/22/2015 Without byte oriented operations Masking and shifting are necessary without byte oriented operations. Masking –A mask is a word where “1”s are in the important positions and “0”s are in other positions. –For example in a 32 bit word, the mask for the second byte » » Ox0000FF00 in hex »Anding a mask with a word “zeros” out the unmasked bits andI > Shifting –Shifting moves the bits over Shift ,8 ->

Cse322, Programming Languages and Compilers 7 6/22/2015 a[ 1 ] = b[ 2 ] Load source word ( b ) – Mask away unwanted characters (every thing but 2) – Shift to byte position in word of target (position 1) – Load target word (a) – Mask away the position of the target character – Or with shifted & masked source with masked target Or > Store result in target address

Cse322, Programming Languages and Compilers 8 6/22/2015 Longer words If a and b are longer strings (longer than 4 characters) then we need to select the right word from the longer string. A[n] = B[m] The correct source word is ( n `div` 4 ) The correct source position in that word is ( n `mod` 4 ) Similar for target string A

Cse322, Programming Languages and Compilers 9 6/22/2015 Copying Strings. To copy a string we need to copy all the component characters. With byte oriented load and store this is easy With word oriented load and store again need to load and move words. –How many words must we move? –When do we need to mask? »How is this affected by length? »Word alignment of the two strings? Error conditions. –Since strings are generally allocated once. –A := B could cause an error if B is longer than A –Test for lengths, first.

Cse322, Programming Languages and Compilers 10 6/22/2015 String Concatenation A^B Compute lengths of A and B len A and len B Allocate (len A + len B ) bytes plus room for length and any alignment necessary. Copy A to target Copy B to target Set the length convention appropriately.

Cse322, Programming Languages and Compilers 11 6/22/2015 String Length Here we use the explicit information stored with the string. Null terminated –Loop and count until 0 is encountered Length Prefixed –Address of string stores the length Length plus pointer –Address of string stores length abc\0abc3 abc 3

Cse322, Programming Languages and Compilers 12 6/22/2015 Structures Structures are heterogeneous aggregates with statically known accessors. Statically known means we know their “offset” at compile time. Sometimes these are named. X.age Sometimes the names are implicit as in pattern matching in ML fun f (Node(x,y,z)) = … Positions of x, y, and z, are statically known Examples include C - struct struct node { Int value; Struct node *next; } Java - Objects with instance variables ML - datatypes with constructors with more than one field

Cse322, Programming Languages and Compilers 13 6/22/2015 Problems Anonymous values struct node { Int value; Struct node *next; } Node x f( *(X.next) ) Note that (X.next) is an anonymous value. A value without a name. Structure Layout –Layout requires alignment –Computing offsets for each field. –Offset depends on size of preceeding fields in the structure

Cse322, Programming Languages and Compilers 14 6/22/2015 Anonymous values Aliasing is a problem with anonymous values. –Pointers int a, *b; b = &a; –Array References Are x[i] and x[j-n] different? p1 = (node *) malloc(sizeof(node)); p2 = (node *) malloc(sizeof(node)); If (...) then p3 = p1; else p3 = p2; p1->value =... p2->value =... w := p1->value; It is clear that p1->value is stored in a register. But what register? It depends upon the path through the if then else. Anonymous values are often stored in memory because we can’t tell when they might change because of aliasing

Cse322, Programming Languages and Compilers 15 6/22/2015 Recording and using field information Structure name Field namelengthoffsettype node2 fields value4 bytes0int next4 bytes4node * struct node { int value; struct node *next; } p1->next loadI 4 => r 1 // offset of next loadAI r p1,r 1 => r 2 // value of p1->next

Cse322, Programming Languages and Compilers 16 6/22/2015 Layout of structures When laying out structures –Meet all alignment rules –Minimize the amount of space used –Statically know the offset of each field. Struct example { int fee; double fie; int foe; double fum; } e1; Structure name Field name lengthNaïve offset type example4 fields fee4 bytes0int fie8 bytes4double foe4 bytes12int fum8 bytes16double fee…fiefoe …fum Note that the alignment of fie on double word boundaries makes naïve offset be incorrect

Cse322, Programming Languages and Compilers 17 6/22/2015 Alternate structure We can reorder the layout of the fields As long as the table is correct, the programmer cannot observe this change. This also save space as we don’t use unnecessary padding Structure name Field name lengthoffsettype example4 fields fee4 bytes16int fie8 bytes0double foe4 bytes20int fum8 bytes8double fee fiefoe fum

Cse322, Programming Languages and Compilers 18 6/22/2015 Arrays of structures struct node { int value; int age; } node x[4]; Value = 5 Age = 34 Value = 2 Age = 18 Value = 0 Age = 3 Value = 9 Age = Value Age We can represent these in at least two ways. Performance may vary.

Cse322, Programming Languages and Compilers 19 6/22/2015 Unions and run-time tags Unions can have several different layouts at runtime. In order to distinguish at runtime, the user must add a tag field that can be tested at runtime to distinguish. struct two { int tag; union choice { struct { char * name } A struct { int age } B } field } u2; In ML the tags are the constructor names!

Cse322, Programming Languages and Compilers 20 6/22/2015 Basic Blocks A (maximal length) straight- line code segment. Any jump or label (because it is the target of a jump) ends a basic block. => r2 loadAO rA,r2 => r3 => r4 loadAO rA,r4 => r5 L1: comp r3,r5 => cc1 cbr_Lt cc1 -> L2,L5 L5: => r6 loadAO rA,r6 => r7 => r8 loadAO rA,r8 => r9 comp r7,r9 => cc2 cbr_Lt cc2 -> L6,L3 L6: => r10 loadAO rA,r10 => r11 => r12 loadAO rA,r12 => r13 comp r11,r13 => cc3 cbr_Lt cc3 -> L2,L3 L2: loadI true => r1 jumpI -> L4 L3: loadI false => r1 jumpI -> L4 L4: nop

Cse322, Programming Languages and Compilers 21 6/22/2015 Sources Basic blocks are produced by –Control Flow constructs in the language »If-then-else »Loops –Positional evaluation of booleans –Short circuit evaluation

Cse322, Programming Languages and Compilers 22 6/22/2015 Predication vs jumps Recall the predicated move Mov_GT cc,r1,r2, => r3 Mostly we use these to avoid branching or jumps –if x<y then a <- c+d else a <- e+f –comp rx,ry => cc1 –add rc,rd => r1 –add re,rf => r2 –mov_LT cc1,r1,r2 => ra If the branches to the else and then are large, we may do too much speculative execution, so using jumps may be better. Other considerations –Expected frequency of one path over another –Complicated control flow (other if-then-else) inside the then or else

Cse322, Programming Languages and Compilers 23 6/22/2015 Generating code Because of our experience with short-circuit evaluation we have all the tools to generate code with control flow. We will need one more IR instruction datatype IR = LoadI of (string * Reg) | LoadAO of (Reg * Reg * Reg) | Arith of (Op * Reg * Reg * Reg) | Comp of (Reg * Reg * CC) | Neg of (Reg * Reg) | Cmp of (Op * Reg * Reg * Reg) | Cbr of (Op * CC * Label * Label) | JumpI of Label | Lab of (Label * IR) | Nop | StoreAO of (Reg * Reg * Reg)

Cse322, Programming Languages and Compilers 24 6/22/2015 Translating statements fun stmt dict x = case x of Assign (NONE,v,NONE,exp) => let val result = expr dict exp val b = base (Var(NONE,v)) val delta = offset (Var(NONE,v)) in emit (StoreAO(result,b,delta)); result end

Cse322, Programming Languages and Compilers 25 6/22/2015 z := x – (2 * y) => r1 loadAO rA,r1 => r2 loadI 2 => r3 => r4 loadAO rA,r4 => r5 Mul r3,r5 => r6 Sub r2,r6 => r7 => r8 storeAO r7 => rA,r8

Cse322, Programming Languages and Compilers 26 6/22/2015 If then else fun stmt dict x = case x of If (tst,thenS,elseS) => let val [start,thenL,elseL,endL] = NextLabel 4 in short dict tst start thenL elseL; emitAt thenL Nop; stmt dict thenS; emit (JumpI,endL); emitAt elseL Nop; stmt dict elseS; emitAt endL Nop; end; Note how we take advantage of the short circuit evaluation mechanism

Cse322, Programming Languages and Compilers 27 6/22/2015 Loops Loops have multiple parts –Initialization –Tests for termination –Body –Jump to continue loop Your homework on tuesday will be to extend S04code.sml to include translation of the while statement.