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

Slides:



Advertisements
Similar presentations
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
Advertisements

CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CS 206 Introduction to Computer Science II 01 / 21 / 2009 Instructor: Michael Eckmann.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
CS 153: Concepts of Compiler Design October 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 206 Introduction to Computer Science II 01 / 23 / 2009 Instructor: Michael Eckmann.
CS 46B: Introduction to Data Structures July 7 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 153: Concepts of Compiler Design September 2 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 235: User Interface Design September 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 160: Software Engineering October 22 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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
CS 206 Introduction to Computer Science II 09 / 11 / 2009 Instructor: Michael Eckmann.
CS 153: Concepts of Compiler Design October 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 30 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
CS 46B: Introduction to Data Structures June 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CS 146: Data Structures and Algorithms June 11 Class Meeting Department of Computer Science San Jose State University Summer 2015 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 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 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
CS 152: Programming Language Paradigms April 16 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 154 Formal Languages and Computability March 8 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 28 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
CS 153: Concepts of Compiler Design November 9 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 14 Class Meeting
Pascal Runtime Library
CS 153: Concepts of Compiler Design August 29 Class Meeting
CS 153: Concepts of Compiler Design October 17 Class Meeting
CS 153: Concepts of Compiler Design October 5 Class Meeting
CS 153: Concepts of Compiler Design October 3 Class Meeting
CS 153: Concepts of Compiler Design November 28 Class Meeting
CS 153: Concepts of Compiler Design November 30 Class Meeting
CS 153: Concepts of Compiler Design November 2 Class Meeting
CMPE 152: Compiler Design December 5 Class Meeting
CMPE 152: Compiler Design February 6 Class Meeting
CMPE 152: Compiler Design October 2 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design October 4 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CS 153: Concepts of Compiler Design November 6 Class Meeting
CS 432: Compiler Construction Lecture 16
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
CMPE 152: Compiler Design January 29 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
CMPE 152: Compiler Design February 28 / March 5 Lab
CMPE 152: Compiler Design March 7 Class Meeting
CMPE 152: Compiler Design April 25 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CMPE 152: Compiler Design March 19 Class Meeting
CMPE 152: Compiler Design March 7/12 Lab
CMPE 152: Compiler Design September 3 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
CMPE 152: Compiler Design February 7 Class Meeting
Presentation transcript:

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

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 2 A Free Compiler Book!  A “traditional” (i.e., more theoretical) compiler textbook first published in 1979:  A bit dated, but free to download. 2.6 MB PDF.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 3 Short Team Presentations  Maximum 15 minutes. Describe your language. Any compilation challenges you had to overcome? Demo as much as you can.  Which teams to present next Monday and Wednesday to be determined by a random drawing this Wednesday.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 4 JavaCC Error Handling with Synchronization String handleError(ParseException ex, HashSet syncSet, boolean doPop) #void { Token token = ex.currentToken; System.out.println(ex.getMessage()); // Consume tokens up to but not including // any token in the synchronization set. while (!syncSet.contains(getToken(1).kind)) { token = getNextToken(); } if (doPop) jjtree.popNode(); return token.image; } pclError.jjt

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 5 JavaCC Error Handling, cont’d void ifStatement() : { HashSet syncSet1 = new HashSet(); syncSet1.add(THEN); syncSet1.add(SEMICOLON); syncSet1.add(EOF); HashSet syncSet2 = new HashSet(); syncSet2.add(SEMICOLON); syncSet2.add(EOF); } { boolexp(syncSet1) statement(syncSet2) } pclError.jjt

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 6 JavaCC Error Handling, cont’d void boolexp(HashSet syncSet) #void : {} { try { variable() } catch (ParseException ex) { handleError(ex, syncSet, true); } pclError.jjt

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 7 JavaCC Error Handling, cont’d SimpleNode compoundStatement() : { HashSet syncSet = new HashSet(); syncSet.add(SEMICOLON); syncSet.add(EOF); } { [ statement(syncSet) ( statement(syncSet) )* ] { return jjtThis; }

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 8 JavaCC Error Handling, cont’d void statement(HashSet syncSet) #void : {} { try { assignmentStatement() | compoundStatement() | ifStatement() | Error() { throw new ParseException(); } } catch (ParseException ex) { handleError(ex, syncSet, false); } Demo

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 9 The Pascal Runtime Library  Useful utility classes written in Java that contain methods your compiled Jasmin code can call. Create an archive file PascalRTL.jar. See Chapters 16 and 17.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 10 The Pascal Runtime Library Utility Classes  PascalTextIn Runtime text input based on the scanner.  RunTimer Time the execution of compiled programs and print the elapsed run time.  RangeChecker Perform runtime range checking.  PascalRuntimeException Error exception thrown while executing compiled code.  PaddedString Pascal string implementation with blank-padding

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 11 The Pascal Runtime Library, cont’d  The Pascal Runtime Library can reuse some classes from the front end.  When a Pascal program calls the standard procedure read to input values at run time, read must scan the input text for values of various data types (integer, real, string, etc.).  Therefore, reuse the scanner and token classes by including them in the library.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 12 The Pascal Runtime Library, cont’d  Include the runtime library on your class path when you run your compiled program.  Your generated code can call routines in the library. java –cp.:PascalRTL.jar …

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 13 The Pascal Runtime Library, cont’d  Cloner.class  PaddedString.class  PascalRuntimeException.class  PascalTextIn.class  RangeChecker.class  RunTimer.class  BWrap.class  CWrap.class  IWrap.class  RWrap.class

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 14 The Pascal Runtime Library, cont’d  wci/frontend/EofToken.class  wci/frontend/Scanner.class  wci/frontend/Source.class  wci/frontend/Token.class  wci/frontend/TokenType.class  wci/frontend/pascal/PascalScanner.class  wci/frontend/pascal/PascalToken.class  wci/frontend/pascal/PascalTokenType.class  wci/frontend/pascal/tokens/PascalErrorToken.class  wci/frontend/pascal/tokens/PascalNumberToken.class  wci/frontend/pascal/tokens/PascalSpecialSymbolToken.class  wci/frontend/pascal/tokens/PascalStringToken.class  wci/frontend/pascal/tokens/PascalWordToken.class  wci/message/Message.class  wci/message/MessageHandler.class  wci/message/MessageListener.class  wci/message/MessageProducer.class  wci/message/MessageType.class

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 15 Pascal Parameter Passing  Pass by value  Pass by reference (“VAR parameters”)

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 16 True or False?  Java passes scalar parameters by value.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 17 Passing Parameters  Pascal can pass parameters by value and by reference. VAR parameters = pass by reference  Java and Jasmin pass scalar values by value.  To pass a scalar value by reference, you must first wrap the value inside an object. Pass the object reference (by value) to the routine. The routine can modify the wrapped value. Upon return, the caller must unwrap the changed value.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 18 True or False?  Java passes object parameters by reference.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 19 Passing Parameters, cont’d  Java and Jasmin pass references to objects by value.  To pass an array or record by value as in Pascal, first clone the array or record value and then pass the reference to the clone. When a formal parameter to a method is a reference to an object, the method can change the value of the object (such as by modifying the values of the object fields). But the method cannot change the parameter’s value to refer to another object and have the method’s caller see that change.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 20 Passing Parameters, cont’d  The Pascal Runtime Library contains classes for passing parameters by value or by reference.  Classes BWrap, CWrap, IWrap, and RWrap wrap a boolean, character, integer, and real scalar value, respectively, to be passed by reference.  Class Cloner clones an array or record to be passed by value. public class IWrap { public int value; public IWrap(int value) { this.value = value; } See WCI Chapter 17 for the details.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 21 Example: Passing Scalars by Reference PROGRAM parmswrap; VAR i, j : integer; PROCEDURE swap(VAR parm1, parm2 : integer); VAR temp : integer; BEGIN temp := parm1; parm1 := parm2; parm2 := temp; END; BEGIN i := 10; j := 20; swap(i, j); writeln('Result: i = ', i:0, ', j = ', j:0); END..method public static main([Ljava/lang/String;)V... new IWrap dup getstatic parmswrap/i I invokenonvirtual IWrap/ (I)V dup astore_1 newIWrap dup getstatic parmswrap/j I invokenonvirtual IWrap/ (I)V dup astore_2 invokestatic parmswrap/swap(LIWrap;LIWrap;)V aload_1 getfield IWrap/value I putstatic parmswrap/i I aload_2 getfield IWrap/value I putstatic parmswrap/j I Wrap i. Wrap j. Allocate slots #1 and #2 as temporaries to store the wrapped i and j. Unwrap i. Unwrap j. Call method.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 22 Example: Passing Scalars by Reference, cont’d PROGRAM parmswrap; VAR i, j : integer; PROCEDURE swap(VAR parm1, parm2 : integer); VAR temp : integer; BEGIN temp := parm1; parm1 := parm2; parm2 := temp; END; BEGIN i := 10; j := 20; swap(i, j); writeln('Result: i = ', i:0, ', j = ', j:0); END..method private static swap(LIWrap;LIWrap;)V.var 2 is temp I.var 0 is parm1 LIWrap;.var 1 is parm2 LIWrap; aload_0 getfield IWrap/value I istore_2 aload_0 aload_1 getfield IWrap/value I putfield IWrap/value I aload_1 iload_2 putfield IWrap/value I return.limit locals 3.limit stack 2.end method #0#1 #2 Access the wrapped values of parm1 and parm2 and swap them.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 23 Example: Passing an Array by Value PROGRAM parmsclone; TYPE cube = ARRAY [0..1, 0..2, 0..3] OF integer; VAR vvv : cube; PROCEDURE printCube(VAR c : cube);... PROCEDURE doCubeValue(c : cube); VAR i, j, k : integer; BEGIN FOR i := 0 TO 1 DO BEGIN FOR j := 0 TO 2 DO BEGIN FOR k := 0 TO 3 DO BEGIN c[i,j][k] := 200*i + 10*j +k; END; writeln('In doCubeValue:'); printCube(c); END; PROCEDURE doCubeRef(VAR c : cube);... BEGIN... c[i,j][k] := 100*i + 10*j +k;... END; BEGIN doCubeRef(vvv); writeln('In main:'); printCube(vvv); doCubeValue(vvv); writeln('In main:'); printCube(vvv); END. getstatic parmsclone/vvv [[[I invokestatic Cloner.deepClone(Ljava/lang/Object;) Ljava/lang/Object; checkcast [[[I invokestatic parmsclone/docubevalue([[[I)V

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 24 Class Cloner  In the Pascal Runtime Library: public class Cloner { public static Object deepClone(Object original) throws PascalRuntimeException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(original); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } catch (Exception ex) { throw new PascalRuntimeException("Deep clone failed."); } } } Write the original object to a byte array stream. Construct a copy from the stream. Return the copy as the deep clone, too.

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 25 Wrapping is not a Perfect Solution  Wrapping a scalar value as an object to be passed by reference is not a perfect solution.  Wrapping is actually closer to “passing by value-result”. Demo

Computer Science Dept. Fall 2015: November 23 CS 153: Concepts of Compiler Design © R. Mak 26 Compilation Demos  Wolf Island  Hilbert Matrix Compare execution speeds: interpreter vs. compiler How much faster is the compiled code?