Download presentation
Presentation is loading. Please wait.
Published bySusan Cook Modified over 9 years ago
1
1 October 1, 2015 1 October 1, 2015October 1, 2015October 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS400 Compiler Construction
2
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) October 1, 2015 2 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Building a Simple Compiler
3
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 October 1, 2015 3 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Intermediate Representation
4
4 October 1, 2015 4 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Keep in mind following questions Intermediate Representation –M–Machine-independent –F–Front end of compilation –S–Source of target code (back end) Why we need IRs –T–To delay machine-dependent code –T–To check semantics –T–To optimize performance What further use of IRs –E–Executable IRs –M–Machine-independent optimization –T–Target code generation from IRs
5
5 October 1, 2015 5 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction The Structure of the Compiler
6
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 y which is the same as x := x op y October 1, 2015 6 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Intermediate Representations
7
7 October 1, 2015 7 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Intermediate Representations (AST) E.nptr * a b + * a + bc c () a * (b + c) Pro:easy restructuring of code and/or expressions for intermediate code optimization Cons:memory intensive
8
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 Java TM Virtual Machine Specification, 2nd ed. http://java.sun.com/docs/books/vmspec October 1, 2015 8 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction The JVM
9
9 pc method code operand stack heapconstant poolframe local vars & method args October 1, 2015 9 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Runtime Data Areas (§3.5)
10
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 character float a 32-bit IEEE 754 single-precision float value double a 64-bit IEEE 754 double-precision float value boolean a virtual type only, int is used to represent true (1) false (0) returnAddress the location of the pc after method invocation reference 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 October 1, 2015 10 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Data Types (§3.2, §3.3, §3.4)
11
11 October 1, 2015 11 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Flow of Control: loop
12
12 October 1, 2015 12 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Flow of Control: loop
13
13 October 1, 2015 13 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Flow of Control: loop
14
14 import java.lang.*; public class Hello { public static void main(String[] arg) { System.out.println("Hello World!"); } Compiler javac Hello.java Hello.java Disassembler javap -c Hello JVM java Hello Hello.class October 1, 2015 14 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction javac, 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
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 off 1 off 2 code for stmt code for expr istore id.index iconst_0 pc: backpatch() sets the offsets of the relative branch when the target pc value is known loc: October 1, 2015 15 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Generating Code for the JVM
16
16 October 1, 2015 16 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction Got it with following questions Intermediate Representation –M–Machine-independent –F–Front end of compilation –S–Source of target code (back end) Why we need IRs –T–To delay machine-dependent code –T–To check semantics –T–To optimize performance What further use of IRs –E–Executable IRs –M–Machine-independent optimization –T–Target code generation from IRs
17
17 Thank you very much! Questions? October 1, 2015 17 Azusa Pacific University, Azusa, CA 91702, Tel: (800) 825-5278 Department of Computer Science, http://www.apu.edu/clas/computerscience/http://www.apu.edu/clas/computerscience/ CS@APU: CS400 Compiler Construction A Simple Syntax-Directed Translator
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.