Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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 1

2 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: http://www.eis.mdx.ac.uk/staffpages/r_bornat/books/compiling.pdf http://www.eis.mdx.ac.uk/staffpages/r_bornat/books/compiling.pdf  A bit dated, but free to download. 2.6 MB PDF.

3 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.

4 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

5 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

6 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

7 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; }

8 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

9 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.

10 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

11 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.

12 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 …

13 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

14 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

15 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”)

16 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.

17 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.

18 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.

19 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.

20 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.

21 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.

22 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.

23 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

24 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.

25 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

26 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?


Download ppt "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."

Similar presentations


Ads by Google