CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak www.cs.sjsu.edu/~mak.

Slides:



Advertisements
Similar presentations
Mohamed M. Saad & Mohamed A. Mohamedin.  Static Execution Graph  Nodes; can be either ▪ Basic Blocks; a sequence of non-branching instructions ▪ Variables.
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 174: Web Programming April 28 Class Meeting
Lab 9 Java Bytecode & The Jasmin Assembler
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)
ISBN Chapter 6 Data Types: Structured types.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
CS 157B: Database Management Systems II March 18 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron Mak.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
The Java profiler based on byte code analysis and instrumentation for many-core hardware accelerators Marcin Pietroń 1,2, Michał Karwatowski 1,2, Kazimierz.
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 149: Operating Systems April 7 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CS 153: Concepts of Compiler Design September 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 7 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 10 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CMPE 226 Database Systems November 18 Class Meeting Department of Computer Engineering San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 25 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
#include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
CS 153: Concepts of Compiler Design November 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 4 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 151: Object-Oriented Design October 29 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Code generation exercises. Function body Transform the following code into java bytecode: def middle(small: Int, big: Int): Int = { val mid = small +
CS 235: User Interface Design April 28 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Review on Program Challenge CSc3210 Yuan Long.
CS 154 Formal Languages and Computability April 19 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 160 and CMPE/SE 131 Software Engineering May 12 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
RealTimeSystems Lab Jong-Koo, Lim
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS216: Program and Data Representation
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 153: Concepts of Compiler Design November 28 Class Meeting
CS 153: Concepts of Compiler Design November 2 Class Meeting
CMPE Database Systems Exercise #1 Solutions
CMPE 152: Compiler Design September 25 Class Meeting
Java Virtual Machine (JVM)
CMPE 152: Compiler Design September 18 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 135: Object-Oriented Analysis and Design December 6 Class Meeting
CMPE 152: Compiler Design December 6 Class Meeting
CMPE 152: Compiler Design November 29 Class Meeting
CMPE 135: Object-Oriented Analysis and Design November 29 Class Meeting Department of Computer Engineering San Jose State University Fall 2018 Instructor:
CS 153: Concepts of Compiler Design November 6 Class Meeting
CS 432: Compiler Construction Lecture 16
CMPE 152: Compiler Design February 28 Class Meeting
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
CMPE 152: Compiler Design February 28 / March 5 Lab
CMPE 152: Compiler Design April 18 – 30 Labs
CS 144 Advanced C++ Programming April 25 Class Meeting
CMPE 152: Compiler Design April 25 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CS 144 Advanced C++ Programming April 30 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE 152: Compiler Design April 18 Class Meeting
CMPE 152: Compiler Design April 30 Class Meeting
CMPE 152: Compiler Design May 2 Class Meeting
Presentation transcript:

CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak 1

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 2 Unofficial Field Trip  Computer History Museum in Mt. View Provide your own transportation to the museum.  Saturday, November 21, 11:30 – closing time Special free admission. Do a self-guided tour of the Revolution exhibit. See a life-size working model of Charles Babbage’s Difference Engine in operation, a hand-cranked mechanical computer designed in the early 1800s. Experience a fully restored IBM 1401 mainframe computer from the early 1960s in operation.

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 3 Code Generation for Arrays and Subscripts  Code to allocate memory for an array variable.  Code to allocate memory for each non-scalar array element.  Code for a subscripted variable in an expression.  Code for a subscripted variable that is an assignment target.

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 4 Arrays and Subscripts, cont’d  Allocate memory for single-dimensional arrays: Instruction newarray for scalar elements. Instruction anewarray for non-scalar elements.  Allocate memory for multidimensional arrays: Instruction multianewarray.

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 5 Allocating Memory for Arrays  Recall the code template for a Jasmin method. Code to allocate arrays here!  Pascal automatically allocates memory for arrays declared in the main program or locally in a procedure or function. The memory allocation occurs whenever the routine is called. This is separate from dynamically allocated data using pointers and new. Therefore, our generated Jasmin code must implement this automatic runtime behavior.

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 6 Example: Allocate Memory for Scalar Arrays PROGRAM ArrayTest; TYPE vector = ARRAY[0..9] OF integer; matrix = ARRAY[0..4, 0..4] OF integer; cube = ARRAY[0..1, 0..2, 0..3] OF integer; VAR i, j, k, n : integer; a1 : vector; a2 : matrix; a3 : cube; BEGIN... END. bipush10 newarrayint putstaticarraytest/a1 [I iconst_5 multianewarray[[I 2 putstaticarraytest/a2 [[I iconst_2 iconst_3 iconst_4 multianewarray[[[I 3 putstaticarraytest/a3 [[[I

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 7 7 Access an Array Element of a 2-D Array PROGRAM ArrayTest; TYPE matrix = ARRAY[0..2, 0..3] OF integer; VAR i, j, k : integer; a2 : matrix; BEGIN... i := 1; j := 2; k := a2[i, j];... END getstatic arraytest/a2 [[I getstatic arraytest/i I aaload getstatic arraytest/j I iaload putstatic arraytest/k I k a2

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 8 Subscripted Variables in Expressions  iaload : push a scalar value from an array element value  aaload : push an array element address PROGRAM ArrayTest; TYPE vector = ARRAY[0..9] OF integer; matrix = ARRAY[0..4, 0..4] OF integer; cube = ARRAY[0..1, 0..2, 0..3] OF integer; VAR i, j, k, n : integer; a1 : vector; a2 : matrix; a3 : cube; BEGIN... j := a1[i]; k := a2[i, j]; n := a3[i, j, k];... END. getstatic arraytest/a1 [I getstatic arraytest/i I iaload putstatic arraytest/j I getstatic arraytest/a2 [[I getstatic arraytest/i I aaload getstatic arraytest/j I iaload putstatic arraytest/k I getstatic arraytest/a3 [[[I getstatic arraytest/i I aaload getstatic arraytest/j I aaload getstatic arraytest/k I iaload putstatic arraytest/n I

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 9 7 Set an Array Element of a 2-D Array PROGRAM ArrayTest; TYPE matrix = ARRAY[0..2, 0..3] OF integer; VAR i, j, k : integer; a2 : matrix; BEGIN... i := 1; j := 2; k := 0; a2[i, j] := k;... END getstatic arraytest/a2 [[I getstatic arraytest/i I aaload getstatic arraytest/j I getstatic arraytest/k I iastore k 0 a2

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 10 More Subscripted Variables PROGRAM ArrayTest; TYPE vector = ARRAY[0..9] OF integer; matrix = ARRAY[0..4, 0..4] OF integer; cube = ARRAY[0..1, 0..2, 0..3] OF integer; VAR i, j, k, n : integer; a1 : vector; a2 : matrix; a3 : cube; BEGIN... a3[i][a1[j]][k] := a2[i][j] - a3[k, 2*n][k+1];... END. getstaticarraytest/a3 [[[I getstaticarraytest/i I aaload getstaticarraytest/a1 [I getstaticarraytest/j I iaload aaload getstaticarraytest/k I getstaticarraytest/a2 [[I getstaticarraytest/i I aaload getstaticarraytest/j I iaload getstaticarraytest/a3 [[[I getstaticarraytest/k I aaload iconst_2 getstaticarraytest/n I imul aaload getstaticarraytest/k I iconst_1 iadd iaload isub iastore Instruction aaload pushes the address of one dimension of an array. Instruction iaload pushes the integer value of an array element. What’s on the stack after this instruction?

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 11 Allocate Memory for Non-Scalar Arrays  For a non-scalar array, we must generate code to : Allocate memory for the array itself. Similar to a scalar array, except that each element will contain a reference to its data. Allocate memory for the data of each array element and initialize each element.

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 12  Each array element should contain a reference to a string object. PROGRAM AllocArrayTest2; TYPE string = ARRAY[1..5] OF char; vector = ARRAY[0..9] OF string; VAR a1 : vector; BEGIN END. Allocate Memory for a 1-D String Array " a1

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 13 Memory for a 1-D String Array, cont’d PROGRAM AllocArrayTest2; TYPE string = ARRAY[1..5] OF char; vector = ARRAY[0..9] OF string; VAR a1 : vector; BEGIN END. bipush 10 anewarray java/lang/StringBuilder iconst_0 istore_1 L001: iload_1 bipush 10 if_icmpge L002 dup iload_1 iconst_5 invokestatic PaddedString.create(I) Ljava/lang/StringBuilder; aastore iinc 1 1 goto L001 L002: putstatic allocarraytest2/a1 [Ljava/lang/StringBuilder; Like the Java code: for (int i = 0; i < 10; ++i) { a1[i] = PaddedString.create(5); } Allocate slot #1 as the temporary variable i. " a1 " PaddedString is a class in the Pascal Runtime Library. What are we duplicating and why?

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 14 Code Template: 1-D Non-Scalar Array bipush 10 anewarray java/lang/StringBuilder iconst_0 istore_1 L001: iload_1 bipush 10 if_icmpge L002 dup iload_1 iconst_5 invokestatic PaddedString.create(I)Ljava/lang/StringBuilder; aastore iinc 1 1 goto L001 L002: putstatic allocarraytest2/a1 [Ljava/lang/StringBuilder;

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 15 Allocate Memory for a 2-D String Array PROGRAM AllocArrayTest2; TYPE string = ARRAY[1..5] OF char; matrix = ARRAY[0..2, 0..3] OF string; VAR a2 : matrix; BEGIN END. a2 "

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 16 iconst_3 iconst_4 multianewarray [[Ljava/lang/StringBuilder; 2 iconst_0 istore_1 L003: iload_1 iconst_3 if_icmpge L004 dup iload_1 aaload iconst_0 istore_2 L005: iload_2 iconst_4 if_icmpge L006 dup iload_2 iconst_5 invokestatic PaddedString.create(I)Ljava/lang/StringBuilder; aastore iinc2 1 gotoL005 L006: pop iinc1 1 gotoL003 L004: putstatic allocarraytest2/a2 [[Ljava/lang/StringBuilder; Memory for a 2-D String Array, cont’d PROGRAM AllocArrayTest2; TYPE string = ARRAY[1..5] OF char; matrix = ARRAY[0..2, 0..3] OF string; VAR a2 : matrix; BEGIN END. Like the Java code: for (int i = 0; i < 3; ++i) { for (int j = 0; j < 4; ++j) { a2[i][j] = PaddedString.create(5); } Allocate slots #1 and #2 as the temporary variables i and j. a2 " ditto

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 17 Code Template: n -D Non-Scalar Array iconst_5 iconst_4 multianewarray [[Ljava/lang/StringBuilder; 2 iconst_0 istore_1 L003: iload_1 iconst_3 if_icmpge L004 dup iload_1 aaload iconst_0 istore_2 L005: iload_2 iconst_4 if_icmpge L006 dup iload_2 iconst_5 invokestatic PaddedString.create(I)Ljava/lang/StringBuilder; aastore iinc2 1 gotoL005 L006: pop iinc1 1 gotoL003 L004: putstatic allocarraytest2/a2 [[Ljava/lang/StringBuilder;

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 18 Records and Fields  Implement the value of each Pascal record variable as a java.util.HashMap object. Keys: Field names (as strings) Values: Field values (as objects) Code to allocate records here!  Recall the code template for a Jasmin method.

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 19 Pascal Records in the JVM PROGRAM RecordTest2; TYPE String16 = ARRAY [1..16] OF char; PersonRec = RECORD firstName : String16; age : integer; END; VAR john : PersonRec; BEGIN... END. new java/util/HashMap dup invokenonvirtual java/util/HashMap/ ()V dup ldc "age" iconst_0 invokestatic java/lang/Integer.valueOf(I)Ljava/lang/Integer; invokevirtual java/util/HashMap.put(Ljava/lang/Object; Ljava/lang/Object;)Ljava/lang/Object; pop dup ldc "firstname" bipush 16 invokestatic PaddedString.create(I)Ljava/lang/StringBuilder; invokevirtual java/util/HashMap.put(Ljava/lang/Object; Ljava/lang/Object;)Ljava/lang/Object; pop putstatic recordtest2/john Ljava/util/HashMap; "age"0 "firstname"" john Convert the int value 0 to an Integer object.  Each record value is a separate hash table. Keys: field names Values: field values  Allocate and initialize each value. Why pop ?

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 20 Set the Values of Record Fields PROGRAM RecordTest2; TYPE String16 = ARRAY [1..16] OF char; PersonRec = RECORD firstName : String16; age : integer; END; VAR john : PersonRec; age : integer; BEGIN john.age := 24; john.firstName := 'John'; age := john.age; END. getstatic recordtest2/john Ljava/util/HashMap; ldc "age" bipush 24 invokestatic java/lang/Integer.valueOf(I)Ljava/lang/Integer; invokevirtual java/util/HashMap.put(Ljava/lang/Object; Ljava/lang/Object;)Ljava/lang/Object; pop getstatic recordtest2/john Ljava/util/HashMap; ldc "firstname" invokevirtual java/util/HashMap.get(Ljava/lang/Object;)Ljava/lang/Object; checkcast java/lang/StringBuilder dup iconst_0 invokevirtual java/lang/StringBuilder.setLength(I)V ldc "John" invokevirtual java/lang/StringBuilder.append( Ljava/lang/String;)Ljava/lang/StringBuilder; bipush 16 iconst_4 invokestatic PaddedString.blanks(II)Ljava/lang/StringBuilder; invokevirtual java/lang/StringBuilder.append( Ljava/lang/CharSequence;)Ljava/lang/StringBuilder; pop "age" 0 "firstname"" john 24 "" "John"

Computer Science Dept. Fall 2015: November 18 CS 153: Concepts of Compiler Design © R. Mak 21 Access Values of Record Fields PROGRAM RecordTest2; TYPE String16 = ARRAY [1..16] OF char; PersonRec = RECORD firstName : String16; age : integer; END; VAR john : PersonRec; age : integer; BEGIN john.age := 24; john.firstName := 'John'; age := john.age; END. getstatic recordtest2/john Ljava/util/HashMap; ldc "age" invokevirtual java/util/HashMap.get(Ljava/lang/Object;) Ljava/lang/Object; checkcast java/lang/Integer invokevirtual java/lang/Integer.intValue()I putstatic recordtest2/age I "age" 24 "firstname""John " john age 24