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.

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Object Oriented Programming in JAVA
Chapter 14: Building a Runnable Program Chapter 14: Building a runnable program 14.1 Back-End Compiler Structure 14.2 Intermediate Forms 14.3 Code.
Outline Java program structure Basic program elements
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
IB Computer Science II Paul Bui
Programming in Java; Instructor:Moorthy Introduction, Objects, Classes, Libraries1 Programming in Java Introduction.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
DAT602 Database Application Development Lecture 5 JAVA Review.
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
1 October 2, October 2, 2015October 2, 2015October 2, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
Lecture 10 : Introduction to Java Virtual Machine
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
FRST JAVA PROGRAM. Getting Started with Java Programming A Simple Java Application Compiling Programs Executing Applications.
1 Introduction to JVM Based on material produced by Bill Venners.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
A Simple One-Pass Compiler to Generate Bytecode for the JVM Chapter 2 COP5621 Compiler Construction, Fall 2013 Copyright Robert van Engelen, Florida State.
1 A Simple One-Pass Compiler (to Generate Code for the JVM) Chapter 2 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
1 October 16, October 16, 2015October 16, 2015October 16, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Compiler Chapter# 5 Intermediate code generation.
CS591x A very brief introduction to Java. Java Developed by Sun Microsystems was intended a language for embedded applications became a general purpose.
1 Intermediate Code Generation Part I Chapter 8 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
Chapter 8: Intermediate Code Generation
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
1 October 25, October 25, 2015October 25, 2015October 25, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1 June 3, June 3, 2016June 3, 2016June 3, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Programming Languages
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Intermediate Code Representations
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
1 January 18, January 18, 2016January 18, 2016January 18, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
Getting Started With Java September 22, Java Bytecode  Bytecode : is a highly optimized set of instructions designed to be executed by the Java.
1 February 17, February 17, 2016February 17, 2016February 17, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Language Implementation Overview John Keyser Spring 2016.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Recap: Printing Trees into Bytecodes To evaluate e 1 *e 2 interpreter –evaluates e 1 –evaluates e 2 –combines the result using * Compiler for e 1 *e 2.
March 19, March 19, 2016March 19, 2016March 19, 2016 Azusa, CA Sheldon X. Liang Ph. D. Software Engineering in CS at APU Azusa Pacific University,
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
RealTimeSystems Lab Jong-Koo, Lim
Object Oriented Programming Lecture 2: BallWorld.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
INTERMEDIATE LANGUAGES SUNG-DONG KIM DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Chapter 9. Intermediate Languages Sung-Dong Kim Dept. of Computer Engineering, Hansung University.
JAVA MULTIPLE CHOICE QUESTION.
Internet and Java Foundations, Programming and Practice
Compiler Chapter 9. Intermediate Languages
Java package classes Java package classes.
CSE401 Introduction to Compiler Construction
IB Computer Science II Paul Bui
Presentation transcript:

1 October 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) Department of Computer Science, CS400 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) October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Building 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 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Intermediate Representation

4 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction The 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 y which is the same as x := x op y October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Intermediate Representations

7 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 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. October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction The JVM

9 pc method code operand stack heapconstant poolframe local vars & method args October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction 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 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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Data Types (§3.2, §3.3, §3.4)

11 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Flow of Control: loop

12 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Flow of Control: loop

13 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Flow of Control: loop

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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 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, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction Generating Code for the JVM

16 October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, 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 Thank you very much! Questions? October 1, Azusa Pacific University, Azusa, CA 91702, Tel: (800) Department of Computer Science, CS400 Compiler Construction A Simple Syntax-Directed Translator