Compiling Techniques

Similar documents
CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Compiler construction 2009

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

An Introduction to Multicodes. Ben Stephenson Department of Computer Science University of Western Ontario

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Java: framework overview and in-the-small features

Topics. Structured Computer Organization. Assembly language. IJVM instruction set. Mic-1 simulator programming

JAM 16: The Instruction Set & Sample Programs

Java Instrumentation for Dynamic Analysis

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

CSC 4181 Handout : JVM

Compiler construction 2009

CSc 453 Interpreters & Interpretation

CSCE 314 Programming Languages

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

Plan for Today. Safe Programming Languages. What is a secure programming language?

02 B The Java Virtual Machine

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Static Program Analysis

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Context Threading: A flexible and efficient dispatch technique for virtual machine interpreters

Introduction to Programming Using Java (98-388)

High-Level Language VMs

Recap: Printing Trees into Bytecodes

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

CS 11 java track: lecture 1

The Java Virtual Machine. CSc 553. Principles of Compilation. 3 : The Java VM. Department of Computer Science University of Arizona

Java Class Loading and Bytecode Verification

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Practical Malware Analysis

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Running class Timing on Java HotSpot VM, 1

Chapter 5. A Closer Look at Instruction Set Architectures

CSE 431S Code Generation. Washington University Spring 2013

Tutorial 3: Code Generation

Part VII : Code Generation

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

Exercise 7 Bytecode Verification self-study exercise sheet

Chapter 5. A Closer Look at Instruction Set Architectures

Chapter 5. A Closer Look at Instruction Set Architectures. Chapter 5 Objectives. 5.1 Introduction. 5.2 Instruction Formats

Computational Expression

Just In Time Compilation

Run-time Program Management. Hwansoo Han

Computer Components. Software{ User Programs. Operating System. Hardware

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

Java byte code verification

Code Generation Introduction

Short Notes of CS201

Lecture 1: Overview of Java

CS201 - Introduction to Programming Glossary By

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

CSE 431S Final Review. Washington University Spring 2013

Lecture 3: C Programm

Problem: Too Many Platforms!

The Java Virtual Machine

INTERMEDIATE REPRESENTATIONS RTL EXAMPLE

CMPSC 497: Java Security

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

Translating JVM Code to MIPS Code 1 / 43

Introduction Basic elements of Java

TACi: Three-Address Code Interpreter (version 1.0)

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Program Dynamic Analysis. Overview

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

Improving Java Performance

Digital Forensics Lecture 3 - Reverse Engineering

Declaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:

JoeQ Framework CS243, Winter 20156

Let s make some Marc R. Hoffmann Eclipse Summit Europe

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

Joeq Analysis Framework. CS 243, Winter

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Building a Compiler with. JoeQ. Outline of this lecture. Building a compiler: what pieces we need? AKA, how to solve Homework 2

Parsing Scheme (+ (* 2 3) 1) * 1

CSCE 5610: Computer Architecture

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Pointers II. Class 31

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

A Tour of Language Implementation

Improving Java Code Performance. Make your Java/Dalvik VM happier

YETI. GraduallY Extensible Trace Interpreter VEE Mathew Zaleski, Angela Demke Brown (University of Toronto) Kevin Stoodley (IBM Toronto)

Question. 1 Features of Java.

CS112 Lecture: Primitive Types, Operators, Strings

Computer Components. Software{ User Programs. Operating System. Hardware

Java TM. Multi-Dispatch in the. Virtual Machine: Design and Implementation. Computing Science University of Saskatchewan

301AA - Advanced Programming [AP-2017]

Programming. Syntax and Semantics

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Compiler Construction I

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

B.V. Patel Institute of BMC & IT, UTU 2014

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Transcription:

Lecture 10: Introduction to 10 November 2015

Coursework: Block and Procedure

Table of contents Introduction 1 Introduction Overview Java Virtual Machine Frames and Function Call 2 JVM Types and Mnemonics Arguments and Operands 3 Variables Function Call and Return value Control Flow

From Java source code to the JVM Overview Java Virtual Machine Frames and Function Call Java code (.java) (.class) Java compiler ($javac) Class Loader Interpreter JIT compiler Execution Engine Java Virtual Machine ($java)

Overview Java Virtual Machine Frames and Function Call From SmallC source code to the JVM SmallC code (.c) Java code (.java) SmallC compiler (.class) Java compiler ($javac) Class Loader Interpreter JIT compiler Execution Engine Java Virtual Machine ($java)

Overview Java Virtual Machine Frames and Function Call Java (or SmallC) compiler: compile the source code into Java ByteCode Classloader: loads the bytecode from class files into the Runtime Execution Engine: executes the ByteCode Interpreter: interprets the ByteCode JIT compiler: compiles the ByteCode into native instruction on the fly (JIT = Just-In-Time)

Java Virtual Machine (JVM) Overview Java Virtual Machine Frames and Function Call The Java Virtual Machine is an abstract computing machine. It has an instruction set and manipulates various memory areas at run time The Java Virtual Machine knows nothing of the Java programming language, only of a particular binary format, the class file format. A class file contains Java Virtual Machine instructions (or ) as well as other information.

Frames Introduction Overview Java Virtual Machine Frames and Function Call In the JVM, a frame: stores data and partial results; performs dynamic linking; returns values for methods; and dispatches exceptions.

Overview Java Virtual Machine Frames and Function Call Frame Local Variables Operand Stack... Local Variables: array of variables declared locally in a method (includes parameters) Operand Stack: LIFO (Last-In Last-Out) stack of operands Java Stack!= Operand Stack The operand stack is used to perform computation. The Java stack is used to keep track of function calls.

Overview Java Virtual Machine Frames and Function Call Frame for foo Local Variables a b Operand Stack 13 7 New frame for bar Local Variables x=7 y=13 Example void bar ( i n t x, i n t y ) {... } void f o o ( i n t a ) { i n t b ; bar ( 7, 1 3 ) ; // push 7 // push 13 // c a l l bar... } Operand Stack

JVM Types and Mnemonics Arguments and Operands What is? is the virtual instruction set of the Java virtual machine. One-byte instruction 256 possible opcodes (200+ in use) Stack-based computation Suggested reading: The Java Virtual Machine Specification: http://docs. oracle.com/javase/specs/jvms/se8/html/index.html Instructions listing: https://en.wikipedia.org/wiki/ Java_bytecode_instruction_listings Online tutorial http://blog.jamesdbloom.com/ JavaCodeToByteCode_PartOne.html

JVM Types Introduction JVM Types and Mnemonics Arguments and Operands Java byte code instructions operates on 9 main different types: the 8 primitives types: byte, char, short, int, long, float, double boolean, byte, short and char are sometimes treated as int and reference a reference is a pointer to an Object in the heap long and double values takes two slots in the operand stack and local variables (all the other types takes one)

Mnemonics Introduction JVM Types and Mnemonics Arguments and Operands A mnemonic is a textual form of an operation Each mnemonic is encoded as a byte in the class file This byte is called an operation code or opcode Examples: iadd: add two integers fmul: multiply two floats lload 1 : load a long value from the local variable 1

Mnemonics Introduction JVM Types and Mnemonics Arguments and Operands Prefix/Suffix Type Size(in byte) b byte 1 s short 2 c char 1 i int 4 l long 8 f float 4 d double 8 a reference 4 or 8 Instructions dealing with the stack or local variables start with a letter corresponding to the type. Examples: iadd: adds two integers dadd: add two doubles

Arguments Introduction JVM Types and Mnemonics Arguments and Operands Arguments follows an instruction Examples: bipush 5 : load a byte onto the stack ldc Hello World! : load a constant from the constant pool iconst 0 : load 0 onto the stack Example Operand Stack... after bipush 5 bipush 8 Operand Stack 8 5...

Operands Introduction JVM Types and Mnemonics Arguments and Operands Operands are taken from the operand stack and resulting value is produced on the stack Examples: iadd Example Operand Stack 8 5... after iadd Operand Stack 13...

JVM Types and Mnemonics Arguments and Operands Exercise Write the ByteCode for the following expression: 5*(3+4) Write down the status of the stack after each instruction

Variables Function Call and Return value Control Flow Local variables can be retrieved via load/store instructions. Frame for foo Local Variables 33 5 Operand Stack 5 33 7 40 Example... f o o ( 3 3 )... void f o o ( i n t a ) { b i p u s h 5 i s t o r e 1 b i p u s h 7 i l o a d 0 i a d d }

Function Call and Return Value Variables Function Call and Return value Control Flow Function call A function call is performed with one of the invoke instructions (dynamic, interface, special, static, virtual). When a function call occurs, a new frame is created and the arguments taken from the operand stack become local variables Return value When a value is returned by the function, the current frame is destroyed and the return value is passed back to the callee onto its operand stack.

Variables Function Call and Return value Control Flow Example: SmallC/Java i n t add ( i n t x, i n t y ) { r e t u r n x+y ; } void main ( ) { add ( 7, 1 3 ) ; } Example: i n t add ( i n t x, i n t y ) { i l o a d 0 i l o a d 1 i r e t u r n } v o i d main ( ) { b i p u s h 7 b i p u s h 13 i n v o k e s t a t i c add ( I I ) I }

Variables Function Call and Return value Control Flow Branching instructions Branching instructions takes a label that points to the place where to jump to. offers both unconditional and conditional branches. Unconditional branch: goto Conditional branches: if icmpeq jump if two stack operands are equals if icmplt jump if the first operand is less than the second one... ifeq jump if operand is 0 ifge jump if operand is greater than 0...

Variables Function Call and Return value Control Flow Exmple 0 : i l o a d 1 1 : i l o a d 2 2 : i f i c m p l e 7 5 : i c o n s t 0 6 : i r e t u r n 7 : i c o n s t 1 8 : i r e t u r n First the two parameters are loaded onto the operand stack using iload 1 and iload 2. if icmple then compares the top two values on the operand stack. This operand branches to byte code 7 if the first operand is less then or equal to the second one.

Variables Function Call and Return value Control Flow Next lecture: Introduction to code generation