Cse322, Programming Languages and Compilers 1 6/14/2015 Lecture #3, April 11, 2007 Boolean expressions Positional encoding Short circuit evaluation Conditional.

Slides:



Advertisements
Similar presentations
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
Advertisements

Code Generation for Control Flow Mooly Sagiv Ohad Shacham Chapter 6.4.
Goal: Write Programs in Assembly
Intermediate Code Generation
CS/COE0447 Computer Organization & Assembly Language
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.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
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.
Intermediate Representation I High-Level to Low-Level IR Translation EECS 483 – Lecture 17 University of Michigan Monday, November 6, 2006.
CS412/413 Introduction to Compilers Radu Rugina Lecture 16: Efficient Translation to Low IR 25 Feb 02.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
Code Shape II Expressions & Arrays Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
CS 61C L09 Introduction to MIPS: Data Transfer & Decisions I (1) Garcia, Fall 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Review Binary –Each digit place is a power of 2 –Any two state phenomenon can encode a binary number –The number of bits (digits) required directly relates.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Cse322, Programming Languages and Compilers 1 6/21/2015 Lecture #5, April 17, 2007 Array Access Case stmt Jump tables Procedure call Machine dependent.
Cse322, Programming Languages and Compilers 1 6/22/2015 Lecture #4, April 12, 2007 Strings (representation, byte operation, copying), Structures (representation,
Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
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.
CS61C L09 Introduction to MIPS : Data Transfer and Decisions (1) Garcia, Spring 2007 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS1Q Computer Systems Lecture 3 Simon Gay. Lecture 3CS1Q Computer Systems - Simon Gay2 Where we are Global computing: the Internet Networks and distributed.
CS412/413 Introduction to Compilers Radu Rugina Lecture 15: Translating High IR to Low IR 22 Feb 02.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
CS 147 June 13, 2001 Levels of Programming Languages Svetlana Velyutina.
Compiler Chapter# 5 Intermediate code generation.
Arithmetic Expressions
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Computer Organization and Architecture Instructions: Language of the Machine Hennessy Patterson 2/E chapter 3. Notes are available with photocopier 24.
Computer Architecture CSE 3322 Lecture 2 NO CLASS MON Sept 1 Course WEB SITE crystal.uta.edu/~jpatters.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
F28PL1 Programming Languages Lecture 4: Assembly Language 3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Science I Storing data. Binary numbers. Classwork/homework: Catch up. Do analysis of image types.
Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Chapter 2 — Instructions: Language of the Computer — 1 Memory Operands Main memory used for composite data – Arrays, structures, dynamic data To apply.
Code Generation How to produce intermediate or target code.
Three Address Code Generation of Control Statements continued..
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.
The Machine Model Memory
Programming in Machine Language
Computing Fundamentals
Morgan Kaufmann Publishers
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
CSCE Fall 2013 Prof. Jennifer L. Welch.
Computer Architecture & Operations I
MIPS Instruction Encoding
ECE232: Hardware Organization and Design
Code Shape III Booleans, Relationals, & Control flow
Instruction Selection, II Tree-pattern matching
MIPS Instruction Encoding
Chapter-3 Operators.
Fields in the FALCON-A Instruction
Lecture 8 Data structures
Differences between Java and C
Computer Instructions
CSCE Fall 2012 Prof. Jennifer L. Welch.
Lecture 15: Code Generation - Instruction Selection
Comp Org & Assembly Lang
Overheads for Computers as Components 2nd ed.
Herbert G. Mayer, PSU CS Status 7/19/2015
CS501 Advanced Computer Architecture
Code generation and data types
Presentation transcript:

Cse322, Programming Languages and Compilers 1 6/14/2015 Lecture #3, April 11, 2007 Boolean expressions Positional encoding Short circuit evaluation Conditional move Array expressions

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

Cse322, Programming Languages and Compilers 3 6/14/2015 Boolean Expressions Boolean expressions can be treated just like arithmetic expressions. –Need to represent True and False, some possibilities »False = 0000 n True = 0001 n »False = 0000 n True = 1111 n for a n-bit representations Some machines have special instructions for manipulating booleans in addition to boolean operations –Special hardware called the condition code »One to several bits (pattern encodes the conditions LT, GT etc) »Set by arithmetic and boolean operations »Special instructions that interrogate the condition code Some compilers use an implicit representation using the location of the program counter to encode True or False.

Cse322, Programming Languages and Compilers 4 6/14/2015 By example We will illustrate this by example 1.Add new instructions to our IR 2.Translate code from CS321 expressions into the new IR instructions 3.Illustrate the approaches by defining a different translation for each approach. 1.Approaches 1.Numerical encoding 1.Register based boolean operators 1.cmp_LT rx,ry => ra 2.Conditional Branches and condition codes 1.Comp rx,ry => cc1 2.Positional Encoding 3.Short Circuit evaluation

Cse322, Programming Languages and Compilers 5 6/14/2015 New IR instruction type Reg = int; type Label = int; type CC = int; datatype IR = LoadI of (string * Reg) | LoadAO of (Reg * Reg * Reg) | Arith of (BINOP * Reg * Reg * Reg) | Comp of (Reg * Reg * CC)

Cse322, Programming Languages and Compilers 6 6/14/2015 Register based boolean encodings fun expr dict node = case node of | Binop(m,x,y) => let val t1 = expr dict x val t2 = expr dict y val result = NextRegister() in emit (Arith(m,t1,t2,result)); result end | Relop(m,x,y) => let val rx = expr dict x val ry = expr dict y val r2 = NextRegister() in emit (Cmp(m,rx,ry,r2)) Notice the similarity between arithmetic and relational operators, Depends upon machine operations that leave boolean values in registers

Cse322, Programming Languages and Compilers 7 6/14/2015 New Concepts 1.Condition codes –type CC = int; –fun showCC n = "cc"^Int.toString n; 2.Labels –type Label = int; –fun showLab n = "L"^Int.toString n;

Cse322, Programming Languages and Compilers 8 6/14/2015 Managing Condition Codes val firstCC = 1; val CCCount = ref firstCC; fun resetCC () = CCCount := firstCC; fun NextCC() = let val n = !CCCount in (CCCount := n+1; n) end;

Cse322, Programming Languages and Compilers 9 6/14/2015 Managing Labels We often generate multiple labels all at once. val firstLabel = 1; val LabelCount = ref firstLabel; fun resetLabel () = LabelCount := firstLabel; fun NextLabel m = let val n = !LabelCount fun f n 0 = [] | f n m = n :: (f (n+1) (m-1)) in (LabelCount := n+m; f n m) end; - NextLabel 4; val it = [10,11,12,13] : int list

Cse322, Programming Languages and Compilers 10 6/14/2015 Emitting labeled code fun emitAt l x = emit(Lab(l,x)); emit (Comp(rx,ry,cc)); emit (Cbr(LT,cc,l1,l2); emitAt l1 (LoadI("true",r2)); emit (JumpI l3); emitAt l2 (LoadI("false",r2)); emit (JumpI l3); emitAt l3 Nop

Cse322, Programming Languages and Compilers 11 6/14/2015 Additions to the IR datatype IR = LoadI of (string * Reg) | LoadAO of (Reg * Reg * Reg) | Arith of (Op * Reg * Reg * Reg) | Cmp of (Op * Reg * Reg * Reg) | Comp of (Reg * Reg * CC) | Neg of (Reg * Reg) | Cbr of ( RELOP * CC * Label * Label) | JumpI of Label | Lab of (Label * IR) | Nop;

Cse322, Programming Languages and Compilers 12 6/14/2015 Condition codes Operations set special hardware called condition codes. Some operations depend upon condition codes. => r1 loadAO rA,r1 => r2 => r3 loadAO rA,r3 => r4 comp r2,r4 => cc1 cbr_Lt cc1 -> L1,L2 L1: loadI true => r5 jumpI -> L3 L2: loadI false => r5 jumpI -> L3 L3: nop Conditional branch. L1 and L2 are labels

Cse322, Programming Languages and Compilers 13 6/14/2015 | Relop(m,x,y) => let val rx = expr dict x val ry = expr dict y val r2 = NextRegister() val cc = NextCC() val [l1,l2,l3] = NextLabel 3 in emit (Comp(rx,ry,cc)); emit (Cbr(m,cc,l1,l2)); emitAt l1 (LoadI("true",r2)); emit (JumpI l3); emitAt l2 (LoadI("false",r2)); emit (JumpI l3); emitAt l3 Nop r2 end => r1 loadAO rA,r1 => r2 => r3 loadAO rA,r3 => r4 comp r2,r4 => cc1 cbr_Lt cc1 -> L1,L2 L1: loadI true => r5 jumpI -> L3 L2: loadI false => r5 jumpI -> L3 L3: nop

Cse322, Programming Languages and Compilers 14 6/14/2015 Choosing between a style | Relop(m,x,y) => let val rx = expr dict x val ry = expr dict y val r2 = NextRegister() in (case !style of Numerical => emit (Cmp(m,rx,ry,r2)) | CondCode => let val cc = NextCC() val [l1,l2,l3] = NextLabel 3 in emit (Comp(rx,ry,cc)); emit (Cbr(m,cc,l1,l2)); emitAt l1 (LoadI("true",r2)); emit (JumpI l3); emitAt l2 (LoadI("false",r2)); emit (JumpI l3); emitAt l3 Nop end); r2 end

Cse322, Programming Languages and Compilers 15 6/14/2015 => r1 loadAO rA,r1 => r2 // r2=a => r3 loadAO rA,r3 => r4 // r4=b comp r2,r4 => cc1 // a<b cbr_Lt cc1 -> L1,L2 L1: loadI true => r5 jumpI -> L3 L2: loadI false => r5 jumpI -> L3 L3: nop => r6 loadAO rA,r6 => r7 // r7=c => r8 loadAO rA,r8 => r9 // r9=d comp r7,r9 => cc2 // c<d cbr_Lt cc2 -> L4,L5 L4: loadI true => r10 jumpI -> L6 L5: loadI false => r10 jumpI -> L6 L6: nop => r11 loadAO rA,r11 => r12 // r11=e => r13 loadAO rA,r13 => r14 // r14=f comp r12,r14 => cc3 // e<f cbr_Lt cc3 -> L7,L8 L7: loadI true => r15 jumpI -> L9 L8: loadI false => r15 jumpI -> L9 L9: nop And r10,r15 => r16 Or r5,r16 => r17 a<b or c<d and e<f

Cse322, Programming Languages and Compilers 16 6/14/2015 Positional Rather than load a boolean into a register use the position in the code to indicate the result of the test. => r1 loadAO rA,r1 => r2 => r3 loadAO rA,r3 => r4 comp r2,r4 => cc1 cbr_Lt cc1 -> L1,L2 L1: ____cc1 is true here__ jumpI -> L3 L2: ___cc1 is false here__ jumpI -> L3 L3: nop

Cse322, Programming Languages and Compilers 17 6/14/2015 Use functions as parameters Suppose we were translating –if x<y then z := 0 else z := z+1 => r1 loadAO rA,r1 => r2 => r3 loadAO rA,r3 => r4 comp r2,r4 => cc1 cbr_Lt cc1 -> L1,L2 L1: ____ z := 0 here__ jumpI -> L3 L2: ___ z := z+1 here__ jumpI -> L3 L3: nop

Cse322, Programming Languages and Compilers 18 6/14/2015 Break translation into 2 parts | Relop(m,x,y) => let val rx = expr dict x val ry = expr dict y val r2 = NextRegister() in (case !style of Positional => let val [trueL,falseL] = NextLabel 2 in compare m rx ry trueL falseL; fillRelSlot trueL (fn () => emit (LoadI("true",r2))) falseL (fn () => emit (LoadI("false",r2))) end ); emits comparison code

Cse322, Programming Languages and Compilers 19 6/14/2015 compare & fillRelSlots fun compare oper rx ry trueL falseL = let val cc = NextCC() in emit (Comp(rx,ry,cc)); emit (Cbr(oper,cc,trueL,falseL)) end; fun fillRelSlot trueL truef falseL falsef = let val [resultL] = NextLabel 1 in tag trueL truef; emit (JumpI resultL); tag falseL falsef; emit (JumpI resultL); emitAt resultL Nop end;

Cse322, Programming Languages and Compilers 20 6/14/2015 Short Circuit Evaluation => 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 a<b or c<d and e<f

Cse322, Programming Languages and Compilers 21 6/14/2015 Coding it up fun short dict (Relop(m,x,y)) start trueL falseL = let val _ = emitAt start Nop val rx = expr dict x val ry = expr dict y val cc = NextCC() in emit (Comp(rx,ry,cc)); emit (Cbr(m,cc,trueL,falseL)) end | short dict (Binop(AND,r1,r2)) start trueL falseL = let val [start2] = NextLabel 1 in short dict r1 start start2 falseL; short dict r2 start2 trueL falseL end | short dict (Binop(OR,r1,r2)) start trueL falseL = let val [start2] = NextLabel 1 in short dict r1 start trueL start2; short dict r2 start2 trueL falseL end

Cse322, Programming Languages and Compilers 22 6/14/2015 Driving short fun shortCircuit dict exp = let val [start,l1,l2,l3] = NextLabel 4 val r2 = NextRegister() in short dict exp start l1 l2; emitAt l1 (LoadI("true",r2)); emit (JumpI l3); emitAt l2 (LoadI("false",r2)); emit (JumpI l3); emitAt l3 Nop; r2 end;

Cse322, Programming Languages and Compilers 23 6/14/2015 Incorporation into expr fun expr dict node = case node of Binop(AND,_,_) => shortCircuit dict node | Binop(OR,_,_) => shortCircuit dict node | Binop(m,x,y) => let val t1 = expr dict x val t2 = expr dict y val result = NextRegister() in emit (Arith(m,t1,t2,result)); result end

Cse322, Programming Languages and Compilers 24 6/14/2015 Conditional Move Some machines have conditional move instructions –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 Note the speculative evaluation (we need only one of the branches)

Cse322, Programming Languages and Compilers 25 6/14/2015 Array Access Arrays addresses have two components –Base –Offset For 1-dimensional, zero-based arrays, with elements of 1 byte things are straight forward. X[3] X = 2341 X[0] = 2341 x[3] =

Cse322, Programming Languages and Compilers 26 6/14/ dimensional address calculation X[y] Address of x[y] = address of x + y => r1 loadA0 ra,r1 => r1 => r2 add r1,r2 => r3 load r3 => r4

Cse322, Programming Languages and Compilers 27 6/14/ dimensional Non zero indexing Array [ low.. High ] Address x[y] = address x + y - low

Cse322, Programming Languages and Compilers 28 6/14/ dimensional non unit size Array [ 0.. n ] of Float // where float = size bytes Address x[y] = address x + y * size

Cse322, Programming Languages and Compilers 29 6/14/2015 Combined Array [ low.. High ] of Float // where float = size bytes Address x[y] = address x + (y – low) * size Optimization Address x[y] = address x + (y * size) – (low * size) Address x[y] = address x – (low * size) + (y * size) Perhaps this is known at compile-time? Performed with a shift if size is a power of 2

Cse322, Programming Languages and Compilers 30 6/14/ dimensional arrays Array[1..2; 2..3] Row major Column major (1,2)(1,3)(2,2)(2,3)(1,2)(2,2)(1,3)(2,3) (1,2)(1,3) (2,2)(2,3)

Cse322, Programming Languages and Compilers 31 6/14/2015 Lets work out the formula Array[ l1..h1; l2..h2 ]

Cse322, Programming Languages and Compilers 32 6/14/2015 Assignment #2 CS322 Prog Lang & Compilers Prog Assignment #2 Assigned Monday April 10, Due Wednesday, April 12, 2006 This assignment is to extend the expr program discussed in class (and available for download) so that it can translate array accesses, where the array is a variable: i.e. x[34+j] fun expr dict (ArrayElm(Var(loc,nm),index,SOME typ)) =... See the assignment directory for for details.