Top Banner
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
26

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

Jan 18, 2016

Download

Documents

Magdalene Mason
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS 153: Concepts of Compiler Design November 23 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak mak.

1

CS 153: Concepts of Compiler DesignNovember 23 Class Meeting

Department of Computer ScienceSan Jose State University

Fall 2015Instructor: Ron Mak

www.cs.sjsu.edu/~mak

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

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

A bit dated, but free to download. 2.6 MB PDF.

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

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 arandom drawing this Wednesday.

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

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

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

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);}{ <IF> boolexp(syncSet1) <THEN> statement(syncSet2)}

pclError.jjt

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

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

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

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);}{ <BEGIN> [ statement(syncSet) ( <SEMICOLON> statement(syncSet) )* ] <END> { return jjtThis; }}

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

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

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

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.

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

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

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

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.

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

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 …

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

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

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

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

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

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

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

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.

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

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.

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

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.

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

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 canchange the value of the object (such as bymodifying the values of the object fields). But the method cannot change theparameter’s value to refer to another object and have the method’s caller see that change.

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

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.

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

Computer Science Dept.Fall 2015: November 23

CS 153: Concepts of Compiler Design© R. Mak

21

Example: Passing Scalars by ReferencePROGRAM 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/<init>(I)V dup astore_1 new IWrap dup getstatic parmswrap/j I invokenonvirtual IWrap/<init>(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 #2as temporaries to store the

wrapped i and j.

Unwrap i.

Unwrap j.

Call method.

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

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

#2Access the wrapped values of

parm1 and parm2 and swap them.

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

Computer Science Dept.Fall 2015: November 23

CS 153: Concepts of Compiler Design© R. Mak

23

Example: Passing an Array by ValuePROGRAM 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; END; 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 [[[Iinvokestatic Cloner.deepClone(Ljava/lang/Object;) Ljava/lang/Object;checkcast [[[Iinvokestatic parmsclone/docubevalue([[[I)V

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

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 originalobject to a bytearray stream.

Construct a copyfrom the stream.

Return the copy as the deep clone, too.

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

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

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

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?