Compiling Techniques Lecture 10: Introduction to Java ByteCode - - PowerPoint PPT Presentation

compiling techniques
SMART_READER_LITE
LIVE PREVIEW

Compiling Techniques Lecture 10: Introduction to Java ByteCode - - PowerPoint PPT Presentation

Introduction Java ByteCode Details Compiling Techniques Lecture 10: Introduction to Java ByteCode Christophe Dubach 10 November 2015 Christophe Dubach Compiling Techniques Introduction Java ByteCode Details Coursework: Block and


slide-1
SLIDE 1

Introduction Java ByteCode Details

Compiling Techniques

Lecture 10: Introduction to Java ByteCode Christophe Dubach 10 November 2015

Christophe Dubach Compiling Techniques

slide-2
SLIDE 2

Introduction Java ByteCode Details

Coursework: Block and Procedure

Christophe Dubach Compiling Techniques

slide-3
SLIDE 3

Introduction Java ByteCode Details

Table of contents

1 Introduction

Overview Java Virtual Machine Frames and Function Call

2 Java ByteCode

JVM Types and Mnemonics Arguments and Operands

3 Details

Variables Function Call and Return value Control Flow

Christophe Dubach Compiling Techniques

slide-4
SLIDE 4

Introduction Java ByteCode Details Overview Java Virtual Machine Frames and Function Call

From Java source code to the JVM

Java code (.java) Java ByteCode (.class) Java compiler ($javac) Java Virtual Machine ($java) Class Loader Interpreter JIT compiler Execution Engine

Christophe Dubach Compiling Techniques

slide-5
SLIDE 5

Introduction Java ByteCode Details Overview Java Virtual Machine Frames and Function Call

From SmallC source code to the JVM

Java code (.java) Java ByteCode (.class) Java compiler ($javac) Java Virtual Machine ($java) Class Loader Interpreter JIT compiler Execution Engine SmallC code (.c) SmallC compiler

Christophe Dubach Compiling Techniques

slide-6
SLIDE 6

Introduction Java ByteCode Details 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

  • n the fly (JIT = Just-In-Time)

Christophe Dubach Compiling Techniques

slide-7
SLIDE 7

Introduction Java ByteCode Details Overview Java Virtual Machine Frames and Function Call

Java Virtual Machine (JVM)

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 Java ByteCode) as well as other information.

Christophe Dubach Compiling Techniques

slide-8
SLIDE 8

Introduction Java ByteCode Details Overview Java Virtual Machine Frames and Function Call

Frames

In the JVM, a frame: stores data and partial results; performs dynamic linking; returns values for methods; and dispatches exceptions.

Christophe Dubach Compiling Techniques

slide-9
SLIDE 9

Introduction Java ByteCode Details 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

  • perands

Java Stack != Operand Stack The operand stack is used to perform computation. The Java stack is used to keep track of function calls.

Christophe Dubach Compiling Techniques

slide-10
SLIDE 10

Introduction Java ByteCode Details 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 Operand Stack Example

void bar ( i n t x , i n t y ) { . . . } void foo ( i n t a ) { i n t b ; bar ( 7 , 1 3 ) ; // push 7 // push 13 // c a l l bar . . . }

Christophe Dubach Compiling Techniques

slide-11
SLIDE 11

Introduction Java ByteCode Details JVM Types and Mnemonics Arguments and Operands

What is Java ByteCode? Java ByteCode 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.

  • racle.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

Christophe Dubach Compiling Techniques

slide-12
SLIDE 12

Introduction Java ByteCode Details JVM Types and Mnemonics Arguments and Operands

JVM Types

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)

Christophe Dubach Compiling Techniques

slide-13
SLIDE 13

Introduction Java ByteCode Details JVM Types and Mnemonics Arguments and Operands

Mnemonics

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

Christophe Dubach Compiling Techniques

slide-14
SLIDE 14

Introduction Java ByteCode Details JVM Types and Mnemonics Arguments and Operands

Mnemonics

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

Christophe Dubach Compiling Techniques

slide-15
SLIDE 15

Introduction Java ByteCode Details JVM Types and Mnemonics Arguments and Operands

Arguments

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 ...

Christophe Dubach Compiling Techniques

slide-16
SLIDE 16

Introduction Java ByteCode Details JVM Types and Mnemonics Arguments and Operands

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 ...

Christophe Dubach Compiling Techniques

slide-17
SLIDE 17

Introduction Java ByteCode Details 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

Christophe Dubach Compiling Techniques

slide-18
SLIDE 18

Introduction Java ByteCode Details 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

. . . foo (33) . . . void foo ( i n t a ) { bipush 5 i s t o r e 1 bipush 7 i l o a d iadd }

Christophe Dubach Compiling Techniques

slide-19
SLIDE 19

Introduction Java ByteCode Details Variables Function Call and Return value Control Flow

Function Call and Return Value

Function call A function call is performed with one of the invoke instructions (dynamic, interface, special, static, virtual). When a function call

  • ccurs, a new frame is created and the arguments taken from the
  • perand 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

  • perand stack.

Christophe Dubach Compiling Techniques

slide-20
SLIDE 20

Introduction Java ByteCode Details Variables Function Call and Return value Control Flow

Example: SmallC/Java

i n t add ( i n t x , i n t y ) { return x+y ; } void main () { add ( 7 , 1 3 ) ; }

Example: Java ByteCode

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 } void main () { bipush 7 bipush 13 i n v o k e s t a t i c ”add ( I I ) I ” }

Christophe Dubach Compiling Techniques

slide-21
SLIDE 21

Introduction Java ByteCode Details Variables Function Call and Return value Control Flow

Branching instructions Branching instructions takes a label that points to the place where to jump to. Java ByteCode 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

. . .

Christophe Dubach Compiling Techniques

slide-22
SLIDE 22

Introduction Java ByteCode Details 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.

Christophe Dubach Compiling Techniques

slide-23
SLIDE 23

Introduction Java ByteCode Details Variables Function Call and Return value Control Flow

Next lecture: Introduction to code generation

Christophe Dubach Compiling Techniques