Download presentation
Presentation is loading. Please wait.
Published byLiani Setiawan Modified over 5 years ago
1
CMPE 152: Compiler Design April 25 Class Meeting
Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak
2
A Free Compiler Book! A more “traditional” compiler textbook first published in 1979: A bit dated, but free to download. 2.6 MB PDF.
3
Unofficial Field Trip Computer History Museum in Mt. View
Provide your own transportation to the museum. Saturday, May 4, 11:30 – closing time Special free admission. We will meet in the lobby. No backpacks. Experience a fully restored IBM 1401 mainframe computer from the early 1960s in operation. Do a self-guided tour of the Revolution exhibit.
4
Unofficial Field Trip, cont’d
IBM 1401 computer, fully restored and operational. A small transistor-based mainframe computer. Extremely popular with small businesses in the late 1950s through the mid 1960s Maximum of 16K bytes of memory 800 card/minute punched card reader (reads holes with wire brushes) 600 line/minute line printer (impact) 6 magnetic tape drives, no disk drives
5
Unofficial Field Trip, cont’d
Information on the IBM 1401: General info: My summer seminar: Restoration:
6
Unofficial Field Trip, cont’d
See the extensive Revolution exhibit! Walk through a timeline of the First 2000 Years of Computing History. Historic computer systems, data processing equipment, and other artifacts. Small theater presentations. Hollerith Census Machine Atanasoff-Berry Computer
7
Code to allocate records here!
Records and Fields Recall the code template for a Jasmin method. Code to allocate records here! Implement the value of each Pascal record variable as a java.util.HashMap object. Keys: Field names (as strings) Values: Field values (as objects)
8
Pascal Records in the JVM
Each record value is a separate hash table. Keys: field names Values: field values Allocate and initialize each value. john "age" "firstname" " " 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/<init>()V 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 ldc "firstname" bipush 16 invokestatic PaddedString.create(I)Ljava/lang/StringBuilder; putstatic recordtest2/john Ljava/util/HashMap; Convert the int value 0 to an Integer object. Why pop?
9
Set the Values of Record Fields
john "age" 24 "firstname" " " "" "John " "John" 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 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 iconst_4 invokestatic PaddedString.blanks(II)Ljava/lang/StringBuilder; invokevirtual java/lang/StringBuilder.append( Ljava/lang/CharSequence;)Ljava/lang/StringBuilder;
10
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. john "age" 24 "firstname" "John " age 24 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
11
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. You don’t have to know Java to use this library. Just follow the examples of how to instantiate the classes and call their methods (member functions).
12
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.
13
The Pascal Runtime Library, cont’d
The Pascal Runtime Library reuses 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.
14
The Pascal Runtime Library, cont’d
To allow your compiled code to call routines in the runtime library, include it on your class path with -cp option when you run your program. Mac and Linux: Windows: The -cp option is not necessary if PascalRTL.jar already on your system CLASSPATH. java –cp .:PascalRTL.jar MyProgram java –cp .;PascalRTL.jar MyProgram
15
System Class Path Good jar files to include in your system class path are antlr-4.7-complete.jar and PascalRTL.jar Be sure to include “.” in order to search whatever directory you are currently in. Example: Linux and Mac Linux: In your home directory .bashrc Mac: In your home directory .bash_profile Of course, use the file paths on your machine. CLASSPATH=.:/Users/rmak/CMPE152/PascalRTL.jar:/Users/rmak/antlr-4.7/antlr-4.7-complete.jar
16
Mac and Linux Class Path
After updating .bashrc or .bash_profile: or The next time you start a terminal window, those files are automatically read and your class path will be set. source .bashrc source .bash_profile
17
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
18
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
19
Pascal Parameter Passing
Pass by value Pass by reference (“VAR parameters”)
20
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, first wrap the value inside an object. Pass the object reference (by value) to the routine. The routine can use the copy of the reference to modify the wrapped value. Upon return, the caller must unwrap the modified value.
21
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.
22
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.
23
Example: Passing Scalars by Reference
PROGRAM parmswrap; VAR i, j : integer; PROCEDURE swap(VAR parm1, parm : integer); temp : integer; BEGIN temp := parm1; parm1 := parm2; parm2 := temp; END; 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 astore_1 new IWrap getstatic parmswrap/j I astore_2 invokestatic parmswrap/swap(LIWrap;LIWrap;)V aload_1 getfield IWrap/value I putstatic parmswrap/i I aload_2 putstatic parmswrap/j I Wrap i. Allocate slots #1 and #2 as temporaries to store the wrapped i and j. Wrap j. Call function. Unwrap i. Unwrap j.
24
Example: Passing Scalars by Reference, cont’d
PROGRAM parmswrap; VAR i, j : integer; PROCEDURE swap(VAR parm1, parm : integer); temp : integer; BEGIN temp := parm1; parm1 := parm2; parm2 := temp; END; 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_1 putfield IWrap/value I iload_2 return .limit locals 3 .limit stack 2 .end method #0 #1 #2 Access the wrapped values of parm1 and parm2 and swap them.
25
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); 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); PROCEDURE doCubeRef(VAR c : cube); ... BEGIN c[i,j][k] := 100*i + 10*j +k; END; doCubeRef(vvv); writeln('In main:'); printCube(vvv); doCubeValue(vvv); END. getstatic parmsclone/vvv [[[I invokestatic Cloner.deepClone(Ljava/lang/Object;) Ljava/lang/Object; checkcast [[[I invokestatic parmsclone/docubevalue([[[I)V
26
Class Cloner In the Pascal Runtime Library: Write the original
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.
27
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”. RefTest
28
Wrapping is not a Perfect Solution, cont’d
PROGRAM parmsbad; VAR i : integer; PROCEDURE proc(VAR v : integer); BEGIN writeln('In proc: i = ', i:0); writeln('In proc: v = ', v:0); v := 999; END; BEGIN i := 10; writeln('In main: i = ', i:0); writeln; proc(i); END. In main: i = 10 In proc: i = 10 In proc: v = 10 In proc: v = 999 In main: i = 999
29
Compilation Demos Wolf Island Hilbert Matrix
Compare execution speeds: interpreter vs. compiler How much faster is the compiled code?
30
Java Parameter Passing
Java passes scalar arguments by value. True or false? A copy of the argument value is passed. True!
31
Java Parameter Passing
Java passes object arguments by reference. True or false? Java passes all arguments by value, including object references. A copy of the reference is passed. Therefore, it is not possible for a function to change what the caller’s argument points to. The function can use the copy of the object reference to change the contents of the caller’s argument object. False!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.