1 october 1, 2015 1 october 1, 2015october 1, 2015october 1, 2015 azusa, ca sheldon x. liang ph. d....
TRANSCRIPT
1
April 19, 20231
April 19, 2023April 19, 2023 Azusa, CAAzusa, CA
Sheldon X. Liang Ph. D.
Computer Science at Computer Science at Azusa Pacific UniversityAzusa Pacific University
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS400 Compiler ConstructionCS400 Compiler Construction
2
• Building a compiler involves:– Defining the syntax of a programming language
– Develop a source code parser: for our compiler we will use predictive parsing (greedy eater)
– Implementing syntax directed translation to generate intermediate code: our target is the JVM abstract stack machine
– Generating Java bytecode for the JVM
– Optimize the Java bytecode (optional)
April 19, 20232
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Building a Simple CompilerBuilding a Simple Compiler
3
• Intermediate Representation (IR) is the product of front end compilation:– The front end of a compiler constructs IR– From which the back end generates the target– Machine-independent optimization– There are two kinds of IRs
April 19, 20233
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Intermediate RepresentationIntermediate Representation
4
April 19, 20234
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Keep in mind following questionsKeep in mind following questions
• Intermediate Representation– Machine-independent– Front end of compilation– Source of target code (back end)
• Why we need IRs– To delay machine-dependent code– To check semantics– To optimize performance
• What further use of IRs– Executable IRs– Machine-independent optimization– Target code generation from IRs
5
April 19, 20235
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
The Structure of the CompilerThe Structure of the Compiler
6
Graphical• Graphical representations (e.g. AST-->abstract syntax tree)
Linear• Postfix notation: operations on values stored on operand
stack (similar to JVM bytecode)• Three-address code: (e.g. triples and quads)
x := y op z • Two-address code:
x := op ywhich is the same as x := x op y
April 19, 20236
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Intermediate RepresentationsIntermediate Representations
7
April 19, 20237
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Intermediate Representations (AST)Intermediate Representations (AST)E.nptr
*E.nptr
E.nptra
b
+E.nptr
*
a +
b c
E.nptr
c
E.nptr
( )
a * (b + c)
Pro: easy restructuring of codeand/or expressions forintermediate code optimization
Cons: memory intensive
8
• Abstract stack machine architecture– Emulated in software with JVM interpreter
– Just-In-Time (JIT) compilers
– Hardware implementations available
• Java bytecode– Platform independent
– Small
– Safe
• The JavaTM Virtual Machine Specification, 2nd ed.http://java.sun.com/docs/books/vmspec
April 19, 20238
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
The JVMThe JVM
9
pc
method code
operand stack
heapconstant pool frame
local vars &method args
April 19, 20239
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Runtime Data Areas (§3.5)Runtime Data Areas (§3.5)
10
byte a 8-bit signed two’s complement integer short a 16-bit signed two’s complement integer int a 32-bit signed two’s complement integer long a 64-bit signed two’s complement integer char a 16-bit Unicode characterfloat a 32-bit IEEE 754 single-precision float valuedouble a 64-bit IEEE 754 double-precision float valueboolean a virtual type only, int is used to represent true (1) false (0)returnAddress the location of the pc after method invocationreference a 32-bit address reference to an object of class type, array type, or interface type (value can be NULL)
Operand stack has 32-bit slots, thus long and double occupy two slots
April 19, 202310
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Data Types (§3.2, §3.3, §3.4)Data Types (§3.2, §3.3, §3.4)
11
April 19, 202311
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Flow of Control: loopFlow of Control: loop
12
April 19, 202312
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Flow of Control: loopFlow of Control: loop
13
April 19, 202313
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Flow of Control: loopFlow of Control: loop
14
import java.lang.*;public class Hello{ public static void main(String[] arg) { System.out.println("Hello World!"); }}
Compilerjavac Hello.java
Hello.java
Disassemblerjavap -c Hello
JVMjava Hello
Hello.class
April 19, 202314
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
javac, javap, javajavac, javap, java#javac – The compiler, which converts source code into Java bytecode #java –Interpreter, which interprets the class files generated by the javac compiler. #javap – The class file disassembler, disassembles compiled Java files and prints out a representation of the Java program.
15
stmt id := expr { emit2(istore, id.index) }
stmt if expr { emit(iconst_0); loc := pc; emit3(if_icmpeq, 0) } then stmt { backpatch(loc, pc-loc) }
code for expr
if_icmpeq off1 off2
code for stmt
code for expr
istore id.index
iconst_0
pc:backpatch() sets the offsets of the relative branchwhen the target pc value is known
loc:
April 19, 202315
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Generating Code for the JVMGenerating Code for the JVM
16
April 19, 202316
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
Got it with following questionsGot it with following questions
• Intermediate Representation– Machine-independent– Front end of compilation– Source of target code (back end)
• Why we need IRs– To delay machine-dependent code– To check semantics– To optimize performance
• What further use of IRs– Executable IRs– Machine-independent optimization– Target code generation from IRs
17
Thank you very much!
Questions?
April 19, 202317
Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/
CS@APU: CS400 Compiler ConstructionCS@APU: CS400 Compiler Construction
A Simple Syntax-Directed TranslatorA Simple Syntax-Directed Translator