cs263: runtime systems lecture: high-level language virtual … · 2020-04-15 · lecture:...

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

Upload: others

Post on 01-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

CS263: Runtime SystemsLecture: High-level language virtual machines

Part 2 of 5

Chandra KrintzUCSB Computer Science Department

Page 2: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Classfiles

File.javaclass cls1 {…}class cls2 {…}

source compiler(javac File.java)

. . .Javabyte-code. . .

cls1.class

. . .Javabyte-code. . .

cls2.class

• Architecture-independent• Format called bytecode• Dynamically loaded / executed

by a Java Virtual Machine

• architecture-independent• architecture-dependent

Runtime environment• program loading/verif.• memory management• thread/synchronization• optimization

Java Virtual Machine

executablecode

Translation from bytecode

to native machine code

Page 3: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

A Java Program (Compiled Java Source)

• Class files (generated from source using a source compiler)• Each class file is in a binary bytecode format• Each class file contains – in a fixed, well-defined format

– Symbolic information (Metadata in MSIL/.Net binary files)• All of the necessary data to execute the class file

– Instructions (code) in the JVM ISA format

Symbolicinfo

method code

method code

. . .

Java Class File

Page 4: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

A Java Program (Compiled Java Source)

• Class files (generated from source using a source compiler)• Each class file is in a binary bytecode format• Each class file contains – in a fixed, well-defined format

– Symbolic information (Metadata in MSIL/.Net binary files)• All of the necessary data to execute the class file

– Instructions (code) in the JVM ISA format

Constant Pool

method code

method code

. . .

Java Class File Instead of repeating all of the symbol names throughout, we store all of the symbols in a table (the constant pool) and then have everything that uses the symbol (code, other data structures) use a reference into the table instead

Types (packages, classes, primitives), names, and also method and field data structures

Page 5: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

The Java Class File Format (Symbolic Info)

Magic numberVersion

Java Class File

Constant pool

Access flags

This class

Super class

Interfaces

Fields

Methods

Attributes

String: “Hello”Method: “C.M(int)V”Field: “C.f int”. . .

Flags Name type Attributes[pub] “x” int[priv] “y” C

Flags Name Sig/type Attributes[pub] “q” ( )int code

exceptions

maxstack=5, maxlocals=8Code[] = {push,add,store,ldc}ExceptionTable[]={ … }Attributes

Constant pool - an array

Fields

Methods

Code

References the constant pool

Page 6: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Constant Pool Exampleclass Simple {

static int field1; static Simple field2; static int field3; public static void foo() {} public static void main(String args[]) {

int i = args.length; int j = 1000027; Simple.field1 = i+j; Simple.field2 = null; Simple.foo();

} } //See constantpoolexample.pdf linked to schedule pageThis Class = index 6 of constant pool

Type = CONSTANT_CLASS, data = index 26index 26: CONSTANT_UTF8 type, value = “Simple”

Super Class = index 7 of constant pool (cpool)Type = CONSTANT_CLASS, data = index 27index 27: CONSTANT_UTF8 type,

value = “java/lang/Object”

Simple.classJava Class File

Constant pool

Access flags

This class = 6

Super class = 7

Interfaces

Fields

Methods

Attributes

Magic numberVersion

Page 7: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions

• Load/store• Arithmetic• Type conversions (int to long, etc)• Object creation (objects/arrays/multi-dim arrays)• Object manipulation (get/put fields, check properties)• Operand stack manipulation• Control transfer (conditionals/unconditionals)• Method invocation and return• Exception and finally implementation• Synchronization

• http://docs.oracle.com/javase/specs/jvms/se7/html/index.html• …/html/jvms-4.html#jvms-4.10.1.4

maxstack=5, maxlocals=8Code[] = {push,add,store,ldc}ExceptionTable[]={ … }Attributes

Code

Page 8: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Implements a Stack-based ISA

• Operands are implicit - on the top of stack (tos)• Compiler simplification (no register allocation)• Compact instruction encoding (b/c few or 0 operands encoded in instruction)

push A //push A on tospush B //push B on tosadd //add the to elements at the tospop C //pop the tos and put the value in C

C = A + B

tosOperand Stack

A B (A+B)A

Used in hardware in old HP calculatorsUse in most modern language runtimes to enable program portabilityJavaCard implements Java bytecode ISA in hardware; as does Azul Vega processor

Page 9: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Stack-based ISA• Can manipulate data (operands) in following formats: typed instrs

instr. starts with– Integers (32-bits) i– Longs (64-bits) l– Floats (32-bits) f– Doubles (64-bits) d– Shorts (16-bits) s– Bytes (8-bits) b– Object references (32-bits) a– Char (16-bits) c– Arrays

• Opcode + data - variable length instructions– Opcode: 1-byte wide (256 possible)– Data: zero or more values to be operated on (operands)

• Instruction format:

. . .1-byteopcode operand operand

Operands are 1-byte each soFor a 2-byte index you need:(operand1 << 8) | operand2

Page 10: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions

• Code from methods in a class are in the code array (Code[]) attribute in the Methods data structure of class file

• All operations performed on the method’s operand stack– Virtual LIFO data structure

– Instructions can 1. Encode their operands directly or2. Get/Set operands from the constant pool: #index3. Get/Set operands from the method’s local variable array4. Many instructions ALSO use the operand stack

maxstack=5, maxlocals=8Code[] = {push,add,store,ldc}ExceptionTable[]={ … }Attributes

ClassFile.Methods[i] Bytecode instruction (1+ bytes)Indexed by byte in method

opcode 0+ operandsidx:

Java Class File

Constant pool

Access flags

This class

Super class

Interfaces

Fields

Methods

Attributes

Magic numberVersion

int: 22int: 4Cls &obj

Operand stack @runtime, holds typed valuesEach method gets its own

Page 11: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

Instructions can 1. Encode their operands directly2. Get/Set operands from the constant pool: #index3. Get/Set operands from the method’s local variable array (_load/_store)4. Many instructions ALSO use the operand stack

Page 12: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions

• Operand Stack (FIFO data structure)– Holds the operands during operations– All (virtual) computation performed here– 32-bits wide; longs/doubles take 2 stack entries– Everything happens relative to top of stack (TOS)

Constant pool in bytecode file given a data structure in VM @runtime0: ldc #2 //Integer value=100027 in constant pool= load constant integer value 100027 from constant pool rep to TOS//everything in Java and .Net are typed so type is carried

Operand stackEach method gets its own

Grows/shrinkswith instructions executed

maxstack=5, maxlocals=8Code[] = {push,add,store,ldc}ExceptionTable[]={ … }Attributes

Reference withinan instruction tothe constant pool

Constant Pool rep in VM

(idx 2 = int:100027)

instructionint: 100027

ldc #2

Get/Set operands from the constant pool: #index

After the ldc instruction:

Page 13: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions

• Code from methods in a class are in the code array (Code[]) attribute in the Methods data structure of class file

– Instructions can get their operands from the method’s local variable array LVA (a virtual data structure)

– Load from LVA index to top-of-operand-stack (TOS)– Store from TOS to LVA index

Index: 0 1 2 3 4 5

Local variable array (max index is 64K-1):

Operand stack

Grows/shrinkswith instructions executed

Holds parameters followed by local vars

maxstack=5, maxlocals=8Code[] = {push,add,store,ldc}ExceptionTable[]={ … }Attributes

Java Class File

Constant pool

Access flags

This class

Super class

Interfaces

Fields

Methods

Attributes

Magic numberVersion

ClassFile.Methods[i] Each method gets its own local

variable array and operand stack

Get/Set operands from the method’s local variable array (_load/_store)

TOSSize of LVA = maxlocals Maximum depth/growth

of Op Stack = maxstack

Page 14: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Local variable array & operand stack

• Operand Stack (FIFO data structure)– Holds the operands during operations

• Local Variable Array holds the local variables (1 per index)– Includes all arguments passed into parameters + local variables

iload 4 //load int value from local variable array onto TOSiload_0 //same as above only short cuts (no operand!)iadd //pop two TOS ints, add them, then push int result to TOS

Index: 0 1 2 3 4 5

Local variable array (max index is 64K-1):

Operand stackEach method gets its own

Grows/shrinkswith instructions executed

Two per method data structures:

Holds params followed by local vars

int ref int int int int2 “s” 0 2 1 3RT values:

int:1int:2

After the iload_0 instruction:

Page 15: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Local variable array & operand stack

• Operand Stack (FIFO data structure)– Holds the operands during operations

• Local Variable Array holds the local variables (1 per index)– Includes all arguments passed into parameters + local variables

iload 4 //load int value from local variable array onto TOSiload_0 //same as above only short cuts (no operand!)iadd //pop two TOS ints, add them, then push int result to TOS

Index: 0 1 2 3 4 5

Local variable array (max index is 64K-1):

Operand stackEach method gets its own

Grows/shrinkswith instructions executed

Two per method data structures:

Holds params followed by local vars

RT values:

int:3

int ref int int int int2 “s” 0 2 1 3

After the iadd instruction:

Page 16: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Local variable array & operand stack

• Operand Stack (FIFO data structure)– Holds the operands during operations

• Local Variable Array (LVA) holds the local vars (1 per index)– Includes all arguments passed into parameters + local variables

iload 4 //load value from LVA onto TOSiload_0 //same as above only short cuts (no operand!)iadd //pop two TOS ints, add them, then push int result to TOSistore_2 //pop TOS and store into LVA index 2

Index: 0 1 2 3 4 5

Local variable array (max index is 64K-1):

Operand stackEach method gets its own

Grows/shrinkswith instructions executed

Two per method data structures:

Holds params followed by local vars

RT values:int ref int int int int2 “s” 3 2 1 3

After the istore_2 instruction:

Page 17: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Local variable array & operand stack

• Operand Stack (FIFO data structure)– Holds the operands during operations

• Local Variable Array (LVA) holds the local vars (1 per index)– Includes all arguments passed into parameters + local variables

aload_1 //load value from LVA onto TOS//”a” prefix stands for address (ie reference/class type)

Index: 0 1 2 3 4 5

Local variable array (max index is 64K-1):

Operand stackEach method gets its own

Grows/shrinkswith instructions executed

Two per method data structures:

Holds params followed by local vars

RT values:

string:“s”

int ref int int int int2 “s” 3 2 1 3

Page 18: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Local variable array & operand stack

• Operand Stack (FIFO data structure)– Holds the operands during operations – tracks types

• Local Variable Array (LVA) holds the local vars (1 per index)– Includes all arguments passed into parameters + local variables

aload_1 //load value from LVA onto TOS//”a” prefix stands for address (ie reference/class type)

astore_2 //error b/c TOS has type string and idx 2 has type int//type safety verification via symbolic (type) execution when class is loaded first time

Index: 0 1 2 3 4 5

Local variable array (max index is 64K-1):

Operand stackEach method gets its own

Grows/shrinkswith instructions executed

Two per method data structures:

Holds params followed by local vars

RT values:

string:“s”

int ref int int int int2 “s” 3 2 1 3

Page 19: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Static field access

• Operand Stack (FIFO data structure)– Holds the operands during operations – tracks types

putstatic #4 //index 4 in constant pool: field Cls.myfield:I//chase index 4 through multiple constant pool entries to find this full name//pop TOS, put value at location/address for static field myfield @runtime//here myfield has type int and is defined in class Cls

//so TOS before this instruction must have type int!

Operand stack

intbefore

Operand stack

after

Field attribute = static

Page 20: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Static field access

• Operand Stack (FIFO data structure)– Holds the operands during operations – tracks types

getstatic #25 //index 25 in constant pool: field Cls.myfield:I//chase index 25 through multiple constant pool entries to find this full name//get value from location/address for static field @runtime and put on TOS//myfield has type int (+holds 17 at this point), and is defined in class Cls

Operand stack

int:17before

Operand stack

after

Field attribute = static

Page 21: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Instance field access

• Operand Stack (FIFO data structure)– Holds the operands during operations – tracks types

putfield #2 //index 2 in constant pool: field Cls.myfield:I//instance fields require object instance (obj) to be on the stack//pop two TOS entries and store the value into the object (@field_offset)

Operand stack

int:1Cls:obj

Operand stack

after

Page 22: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Instance field access

• Operand Stack (FIFO data structure)– Holds the operands during operations – tracks types

getfield #2 //index 2 in constant pool: field Cls.myfield:I//instance fields require object instance (obj) to be on the stack//pop TOS and load the value in the object (@field_offset) onto TOS

Operand stack

Cls:obj

Operand stack

afterint:1

Page 23: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Static method invocation• Operand Stack (FIFO data structure)

– Holds the operands during operations – tracks types

invokestatic #24 //index 24 in constant pool: method Cls.mymethod:()I//chase index 24 through multiple constant pool entries to find this full name//nothing on TOS b/c static and type is ()I = nothing passed in, int returned

Operand stackOf caller

int:17before

Operand stackOf caller

after

Method attribute = static

TOS

Page 24: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Static method invocation• Operand Stack (FIFO data structure)

– Holds the operands during operations – tracks types

invokestatic #24 //index 24 in constant pool: method Cls.mymethod:()I//chase index 24 through multiple constant pool entries to find this full name//nothing on TOS b/c static and type is ()I = nothing passed in, int returned

At runtime, mymethod gets invoked and gets its own LVA and operand stackLVA of callee is populated with types/values of arguments, if any

These go away when mymethod returns, and the caller regains control and uses its operand stack and LVA; any return value is placed on its TOS upon return

Operand stackOf caller

int:17before

Operand stackOf caller

after

Method attribute = static

TOS

Page 25: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Static method invocation• Operand Stack (FIFO data structure)

– Holds the operands during operations – tracks types

invokestatic #24 //index 24 in constant pool: method Cls.mymethod:(IILjava/lang/String;I)D

parameter type: int, int, String, intreturn type: doubleinstructions prior to to this one pushes the arguments on the stack:bipush 10

bipush 20

ldc #8 //string “Chandra”bipush 30invokestatic #24dstore_1 //store return double value in lVA

Operand stackOf caller

double:before

Operand stackOf caller

after

Method attribute = static

TOS

int:10int:20

ref:String ”Chandra”int:30 Takes 2 stack

entries (64bits)

Page 26: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Dynamic Dispatch of instance methods (aka virtual methods)

• Operand Stack (FIFO data structure)

invokevirtual #21 //index 21 in constant pool: method Cls.myvmeth:(I)V//chase index 21 through multiple constant pool entries to find this full name//instance method so object must be on TOS + any arguments (int here)

- Remember: obj.myvmeth(14) turns into myvmeth(obj, 14) so that they can be implemented as a function call at the machine level (no notion of objects here)

Operand stackOf caller

caller, beforeinvocation

int: 14Cls:obj

after

Operand stackOf caller

Page 27: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlSetting up the LVA upon method entry

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• LVA for main: parameter types from left to right, followed by Class type if main is an instance method (it is not), followed by its local variables top to bottom (types are given by instruction prefixes on load and store instructions)

Index: 0 1 2 3String[] int int int

ref

Page 28: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlSetting up the LVA upon method entry

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• LVA for main: parameter types from left to right, followed by Class type if main is an instance method (it is not), followed by its local variables top to bottom (types are given by instruction prefixes on load and store instructions)

Index: 0 1 2 3String[] int int int

We can look at the source and map LVA indices to name (top to bottom):

args=LVA[0], i=LVA[1], j=LVA[2], k= LVA[3]

refThe operand stack at method entry is empty (required by JVM)

Page 29: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Method Invocations: Putting it all togetherCaller:

bipush 10bipush 20

ldc #8 //string “Chandra”bipush 30

invokestatic #24 //method Cls.mymethod:(IILjava/lang/String;I)D

dstore_1 //store return double value in lVA

Operand stackOf caller

before

int:10int:20

ref:String ”Chandra”int:30

Page 30: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Method Invocations: Putting it all togetherCaller: Callee:

bipush 10 ldc2_w #10 //double 524dbipush 20 dreturn

ldc #8 //string “Chandra”bipush 30

invokestatic #24 //method Cls.mymethod:(IILjava/lang/String;I)D

dstore_1 //store return double value in lVA

Operand stackOf caller

before

int:10int:20

ref:String ”Chandra”int:30

Callee LVA and operand stack upon entry

Operand stackOf callee

(empty upon entry)

String

int int ref int10 20 ”Chandra” 30

Index: 0 1 2 3Holds params followed by local vars (none here)

LVA

Afterldc2_2

Afterdreturn

double:524d

Page 31: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Method Invocations: Putting it all togetherCaller: Callee:

bipush 10 ldc2_w #10 //double 524dbipush 20 dreturn

ldc #8 //string “Chandra”bipush 30

invokestatic #24 //method Cls.mymethod:(IILjava/lang/String;I)D

dstore_1 //store return double value in lVA

Operand stackOf caller

before

int:10int:20

ref:String ”Chandra”int:30

Operand stackOf callee

(empty upon entry)

Afterldc2_2

Afterdreturn

double:524d

Operand stackOf caller

afterdouble:524d

Callee LVA and operand stack upon entryString

int int ref int10 20 ”Chandra” 30

Index: 0 1 2 3Holds params followed by local vars (none here)

LVA

Page 32: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Bytecode Instructions: Instance methods• Operand Stack (FIFO data structure)

invokevirtual #21 //index 21 in constant pool: method Cls.myvmeth:(I)V//chase index 21 through multiple constant pool entries to find this full name//instance method so object must be on TOS + any arguments (int here)

At runtime, myvmeth gets invoked and gets its own LVA and operand stackLVA of callee is populated with types/values of arguments (object and int here)

- Remember: obj.myvmeth(14) turns into myvmeth(obj, 14) so that they can be implemented as a function call at the machine level (no notion of objects here)

Operand stackOf caller

caller, beforeinvocation

Operand stackOf callee

callee, upon invocation

Index: 0 1 2 3 4 5

LVA of callee (upon invocation of myvmeth)

Holds params followed by local vars

Clsref int int intobj 14 RT values:

int: 14Cls:obj

Page 33: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Constructors in Java Bytecode• Operand Stack (FIFO data structure)

invokespecial #2 //index 2 in constant pool: method Cls.<init>:()V//chase index 2 through multiple constant pool entries to find this full name//instance method so object must be on TOS + any arguments (int here)

This instruction invokes constructors (instance methods that perform object initialization). They occur after a new instruction.

Their implementation is complex so we will leave that until later, so just note that this exists and that constructors are handled as special cases in Java- And that they all have method name <init>- And are distinguished by their signature if there are multiple constructors- Constructors always have void return type in Java- They are statically dispatched despite being instance methods

because we know which method implementation the developer intends (the once specified in the call to new: obj = new Cls();

Page 34: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Load reference type from LVA[0] on to TOS• Array’s are object references with array [] type Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterString[]

before

Page 35: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Pop TOS (must be array object), get value in its length field• Length field is built-in in Java bytecode• Push value on TOS

Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterString[]before

int

Page 36: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Pop TOS (must be int type), store in LVA[1] (also int type)

Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterint

before

Page 37: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Load constant from constant pool index 2 on TOS• Type on TOS becomes int, because constant pool

entry type is int (CONSTANT_Integer)Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterint

before

Page 38: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Pop TOS (must be int type), store in LVA[2] (also int type)

Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterbeforeint

Page 39: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Get value from static field in memory and push on TOS• Type of value on TOS is the type specified in the

constant pool entry (CONSTANT_Name_And_Type) (here=int)

Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterbeforeint

Page 40: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Pop TOS (must be int type), store in LVA[3] (also int type)

Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterint

before

Page 41: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Push integer (encoded in a byte, hence the b prefix) on TOS• Pushes constant that is encoded in instruction itself Index: 0 1 2 3

String[] int int int

Operand stack Operand stack

afterbeforeint:18

Page 42: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

class Simple {static int field1;public static void foo(int val) {}

public static voidmain(String args[]) {

int i = args.length;int j = 1000027;int k = Simple.field1;Simple.foo(18);

}}

public static void main(java.lang.String[]);Code:0: aload_01: arraylength2: istore_13: ldc #2 //int 10000275: istore_26: getstatic #3 //field Simple.field1:I9: istore_3

10: bipush 1812: invokestatic #4 //method Simple.foo:(I)V15: return

• Invoke static method (classname.methodname:methodtype) is encoded by constant pool index• This invokestatic pops 1 value from TOS• Passes it into method call, call has Void return type so

stack is empty upon return

Index: 0 1 2 3This is the caller’s LVA & OStack

String[] int int int

Operand stack Operand stack

afterbeforeint:18

During call, Simple:foo(I)V getsits own operand stack and LVA- Value passed is in placed in LVA

Page 43: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances (new), instance methods, and instance fields

class Simple {int ifield;static void foo(Simple obj){

obj.ifield = 10;obj.bar();

}…

}

static void foo(Simple);Code:0: aload_01: bipush 103: putfield #2 //Field Simple.ifield:I6: aload_07: invokevirtual #3 //Method Simple.bar:()I

10: pop11: return

• Instance field access requires an object instance• Here it is passed into foo: foo’s signature is Simple.foo(LSimple;)V

• Instance field (classname.fieldname:fieldtype) encoded by constant pool index #2• putfield pops 2 TOS values (instance & value)

Index: 0

Simple

Operand stack Operand stack

afterbefore

Simple:&obj10

LVA

Page 44: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances (new), instance methods, and instance fields

class Simple {int ifield;static void foo(Simple obj){

obj.ifield = 10;obj.bar();

}…

}

static void foo(Simple);Code:0: aload_01: bipush 103: putfield #2 //Field Simple.ifield:I6: aload_07: invokevirtual #3 //Method Simple.bar:()I

10: pop11: return

• Instance method invocations require an object instance: 6: aload_0 pushes it onTOS• Here it is passed into foo

• Instance method (classname.methodname:methodtype) encoded by constant pool index #3

• Invokevirtual pops 1 TOS value (instance) – there are no arguments• The signature of callee is Simple.bar()I so it returns an int

Index: 0

Simple

Operand stack Operand stack

afterbeforeSimple:&obj

LVA

int

Page 45: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances, instance methods, and instance fields

class Simple {int ifield;static void foo(Simple obj){

obj.ifield = 10;obj.bar();

}…

}

static void foo(Simple);Code:0: aload_01: bipush 103: putfield #2 //Field Simple.ifield:I6: aload_07: invokevirtual #3 //Method Simple.bar:()I

10: pop11: return

• Java requires that the operand stack be empty when a method returns

Index: 0

Simple

Operand stack Operand stack

after pop (and before return)before pop

LVA

int

Page 46: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Revisiting: Constructors in Java Bytecode• Operand Stack (FIFO data structure)

invokespecial #2 //index 2 in constant pool: method Cls.<init>:()V//chase index 2 through multiple constant pool entries to find this full name//instance method so object must be on TOS + any arguments (int here)

This instruction invokes constructors (instance methods that perform object initialization). They occur after a new instruction.

Their implementation is complex so we will leave that until later, so just note that this exists and that constructors are handled as special cases in Java- And that they all have method name <init>- And are distinguished by their signature if there are multiple constructors- Constructors always have void return type in Java- They are statically dispatched despite being instance methods

because we know which method implementation the developer intends (the once specified in the call to new: obj = new Cls();

Page 47: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances (new)

• Java requires that the operand stack be empty when a method returns

Index: 0

String[]

Operand stack Operand stack

after newBefore new

LVA

Simple ref

class Simple {int ifield;static void foo(Simple

obj){foo(new Simple());

} }

public static void main(java.lang.String[]);Code:0: new #4 //class Simple3: dup //<init> is the constructor4: invokespecial #5 //Method Simple.<init>:()V7: invokestatic #6 //Method foo:(LSimple;)V

10: return

Page 48: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances (new)

• Java requires that the operand stack be empty when a method returns

Index: 0

String[]

Operand stack Operand stack

after dupBefore dup

LVA

Simple refSimple ref

Simple ref

class Simple {int ifield;static void foo(Simple

obj){foo(new Simple());

} }

public static void main(java.lang.String[]);Code:0: new #4 //class Simple3: dup //<init> is the constructor4: invokespecial #5 //Method Simple.<init>:()V7: invokestatic #6 //Method foo:(LSimple;)V

10: return

Page 49: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances (new)

class Simple {int ifield;static void foo(Simple

obj){foo(new Simple());

} }

public static void main(java.lang.String[]);Code:0: new #4 //class Simple3: dup //<init> is the constructor4: invokespecial #5 //Method Simple.<init>:()V7: invokestatic #6 //Method foo:(LSimple;)V

10: return

• Java requires that the operand stack be empty when a method returns

Index: 0

String[]

Operand stack Operand stack

after constructorBefore constructor

LVA

Simple refSimple refSimple ref

Page 50: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Instruction index: http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.htmlObject instances (new)

class Simple {int ifield;static void foo(Simple

obj){foo(new Simple());

} }

public static void main(java.lang.String[]);Code:0: new #4 //class Simple3: dup //<init> is the constructor4: invokespecial #5 //Method Simple.<init>:()V7: invokestatic #6 //Method foo:(LSimple;)V

10: return

• Java requires that the operand stack be empty when a method returns

Index: 0

String[]

Operand stack Operand stack

after invokestaticBefore invokestatic

LVA

Argumentpassed in Simple ref

Page 51: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Java Object Allocation and Construction

• Object initialization - 2 step process– new C - creates a new object instance of class C

• Instance fields in the object are set to default values– invokespecial C:<init>… invokes one of C’s constructors

• Can initialize fields to non-default values or perform any arbitrary computation

– JVM specification requires that this protocol be respected• The object returned by new is uninitialized and cannot be used until

the initialization method is invoked on it & returns normally• Premature use is type-safe since default values are used• However, inconsistencies can arise if an object is accessible before

the init method has completed– Added difficulty, <init> methods don’t return an object

• However, instructor signature (type) shows a void: <init>()V

Page 52: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Object Initialization and Verification

• invokespecial doesn’t return an object– Operates by side effect, updates object pointed to by reference

• Two references (hence the dup instruction) are needed– The topmost is “consumed” by invokespecial– The second reference

• Holds the reference to the initialized object (after invokespecial)• This object goes from uninitialized (before the invokespecial) to

initialized (after it)– Initializer needs not return an object

new Cdup*compute arguments (if any) & put them on the stack*invokespecial C.<init>(arg_1,...,arg_n)V

x = new C(); arg_n…

arg_1Ref(C)Ref(C)

stack

Page 53: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Constructors + Inheritance

public class B extends A {public static void main(String args[]) {

obj = new B()} //same as leaving constructor out:public B() {}

}

public static void main(java.lang.String[]);Code:

0: new #9 // class B3: dup4: invokespecial #6 // Method B."<init>":()V7: astore_08: return

}

Allocation (new) + instantiation (<init>(...)V)Constructors are statically dispatchedConstructors must return void

So aliasing (dup) must be usedAll constructors run parent constructor first!

default parent: super() //added for youyou can change this to a different super signature via super(...)

B(); //in B’s constructorCode:

0: aload_01: invokespecial #6 // Method A."<init>":()V4: aload_05: …

}

super();Automatically inserted if omitted

Can be changed explicitly to another constructor: super(k);

Page 54: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Practice, Practice, Practice!

//dump the bytecode (shows constant pool entries resolved)javap -c Simple //Simple must be in your classpath

$ export CLASSPATH=.

//http://www.cs.ucsb.edu/~cs263/showme.tar.gz//cs263 dir must be in your class path//dump constant pool (so you can resolve the entries yourself)java cs263.ShowMeTheStructure Simple.classhttp://www.cs.ucsb.edu/~cs263/lectures/constantpoolexample.pdf

JVM instructions:http://www.ic.uff.br/~cbraga/comp/vmspec/VMSpecIX.fm.html

Page 55: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

class B extends A {static int fielda= 4;int field2;int fieldc = 2;void m2() {…}static void m4() {…}final void m5() {…}B() {…}static void foo() {

A tmpA1 = new A();A tmpA2 = new B();tmpA2.m2();

}}

Interesting bytecodes and method dispatch specializations

class A {static int field1= 4;int field2 = 3;int field3 = 7;static int field4;static void m1() {}void m2() {}void m3() {}A() {}static void main(String args[]) {

B.foo(); }}

javac A.javajava A //what is the output, assuming

//all methods announce themselves?

Page 56: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

static void foo();Code:

0: new #9 // class A3: dup 4: invokespecial #6 // Method A."<init>":()V7: astore_0 8: new #10 // class B11: dup 12: invokespecial #11 // Method "<init>":()V15: astore_1 16: aload_1 17: invokevirtual #12 // Method A.m2:()V20: return

class B extends A {static int fielda= 4;int field2;int fieldc = 2;void m2() {…}static void m4() {…}final void m5() {…}B() {…}static void foo() {

A tmpA1 = new A();A tmpA2 = new B();tmpA2.m2();

}}

Interesting bytecodes and method dispatch specializations

java A – output:class A default constructorclass A default constructorclass B default constructorclass B instance method m2()

Page 57: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

More Examples: Instance fields and methods

public class Hello {int field1;int getFieldVal() { return field1; }public static void main(String args[]) {

System.err.println("Hello World!");Hello obj = new Hello();obj.field1 = 7;int i = obj.getFieldVal();

}}

public static void main(java.lang.String[]);Code:

0: getstatic #2 // Field java/lang/// System.err:Ljava/io/// PrintStream;

3: ldc #3 // String Hello World!

5: invokevirtual #4 // Method java/io/// PrintStream.println://(Ljava/lang/String;)V

8: new #5 // class Hello11: dup 12: invokespecial #6 // Method "<init>":()V15: astore_1 16: aload_1 17: bipush 719: putfield #7 // Field field1:I22: aload_1 23: invokevirtual #8 // Method getFieldVal:()I26: istore_2 27: return

}

Require object instance: (via aload…)get/putfield; invokevirtual; invokespecial

When a method call is made (using line-by-line interpretation):Runtime moves arguments to correctlocation in caller’s registers (locals) array

Page 58: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

More examples: Constructors

public class Hello {int field1;int getField() { return field1; }public static void main(String args[]) {

System.err.println("Hello World!");Hello obj = new Hello();obj.field1 = 7;int i = obj.getField();

}}

public static void main(java.lang.String[]);Code:

0: getstatic #2 // Field java/lang/// System.err:Ljava/io/// PrintStream;

3: ldc #3 // String Hello World!

5: invokevirtual #4 // Method java/io/// PrintStream.println://(Ljava/lang/String;)V

8: new #5 // class Hello11: dup 12: invokespecial #6 // Method "<init>":()V15: astore_1 16: aload_1 17: bipush 719: putfield #7 // Field field1:I22: aload_1 23: invokevirtual #8 // Method getField:()I26: istore_2 27: return

}

Allocation (new) + instantiation (<init>(...)V)Constructors are statically dispatchedConstructors must return void

So aliasing (dup) must be used

Page 59: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

public static void main(java.lang.String[]);Code:

0: new #63: dup4: invokespecial #77: astore_1 8: aload_19: getfield #212: istore_2 13: aload_114: bipush 716: putfield #219: return

class test {int field1 = 2;void testme() {}public static void main (String a[]){

test tobj = new test();int i = tobj.field1;tobj.field1 = 7;

}}

Instance fields and Method consume the this/self object from tos

Note that instance field accesses (getfield, putfield) and instant method invocations (invokevirtual, invokespecial) consume the this reference from tos.

Ltest; Ltest;

Ltest;Ltest; Ltest;

tos

int (=2)Ltest;

7Ltest;

Operand stack for each instructionon left:

0: 3: 4: 7: 8:

9: 12: 13: 14: 16:

Page 60: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

class B extends A {static int fielda= 4;int field2;int fieldc = 2;

void m2() {…}static void m4() {…}final void m5() {…}B() {…}static void foo() {

A tmpA1 = new A();A tmpA2 = new B();tmpA2.m2();

}}

Interesting bytecodes and method dispatch specializations

class A {static int field1= 4;int field2 = 3;int field3 = 7;static int field4;static void m1() {}void m2() {}void m3() {}A() {}static void main(String args[]) {

B.foo(); }}

All instance methods have a hidden first parameter (this/self).Final instance methods can be statically dispatched! Why?Constructors are instance methods that can be statically dispatched! Why?

Page 61: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Other Things to know/remember

• Different source compilers implement the same semantics for the same thingaload_1 aload_1dup aload_1

• Constructor methods are instance methods that are statically dispatched!• You need not add super() to your constructors (it happens for you)

Par(){ //super() only works in constructors; must be first else Errorsuper(); // only add this if you want a different super, e.g. super(int);field3 = 7;

}• The "this" object for instance methods becomes the first argument

Simple obj = new Simple();int i = obj.instMethod(72, "cat"); //arg types: Simple, int, String//when invoked in hardware becomes: call instMethod(obj,72,"cat")

//you know its an instance method bc of invokevirtual16: invokevirtual #24; //method Simple.instMethod(I,LString;)I

Page 62: CS263: Runtime Systems Lecture: High-level language virtual … · 2020-04-15 · Lecture: High-level language virtual machines Part 2 of 5 Chandra Krintz ... Runtime environment

Bytecode ISA

• JVM– Typed instructions– Opcode: 1-byte wide (253 are used)– Data: zero or more values to be operated on (operands)

• MSIL– Typed instructions– Opcode is 2-bytes (64K possible)

• Python– 113 opcodes (42 with arguments and 71 without)

• All use an operand stack for one or more of their operands• Translator must translate this ISA to native code

– Interpreter (line-by-line translation): calls functions for each instr– Compiler: translates methods or code blocks to native all at once