java byte code

Post on 26-Jan-2017

207 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

SOFTWARE MAINTENANCE

BYTECODE

PROF. DR. SAFEEULLAH SOOMRO

JAVED AHMED SAMO

Course Title

Presented to:

Presented by:

JAVA

BYTE CODE

JVM

OBJECT CODE

.CLASS FILE

JAVAC

JIKES

JAMAICA

LILA

C

JIT

PUSH

POP

iload_2

stack

BYTECODE & JVM• Bytecode is an intermediate code that runs on JVM

• A Java virtual machine (JVM) is an abstract computing machine that enables a computer to run a Java program

FURTHERMORE ON

BYTE CODE

P-code (various forms of instruction sets designed for efficient execution by a software interpreter as well as being suitable for further compilation into machine code. / Java bytecode is the instruction set of the Java virtual machine. Each bytecode is composed by one, or in some cases two, bytes that represent the instruction (opcode), along with zero or more bytes for passing parameters.Advantages of Bytecode

• Bytecode is architecture independent (and writing a VM is easier than rewriting a compiler for every architecture)

• Just In-Time (JIT) compiling helps achieve same or better speed than traditional compiled code

Introduction to Byte Code

Other languages that have an intermediate representation

• C#-----------------MSIL• Perl ---------------perlcompiler

• PHP ---------------bcompiler• Python -------------

• Jikes, compiles from Java to Java bytecode (developed by IBM, implemented in C++)• Espresso, compiles from Java to Java bytecode (Java 1.0 only)

• GCJ, the GNU Compiler for Java, compiles from Java to Java bytecode; it is also able to compile to native machine code and is available as part of the GNU Compiler Collection (GCC).

• Some projects provide Java assemblers to enable writing Java bytecode by hand. Assembly code may be also generated by machine, for example by a compiler targeting a Java virtual machine. Notable Java assemblers include:

• Jasmin, takes textual descriptions for Java classes, written in a simple assembly-like syntax using Java Virtual Machine instruction set and generates a Java class file

• Jamaica, a macro assembly language for the Java virtual machine. Java syntax is used for class or interface definition. Method bodies are specified using bytecode instructions.

• Krakatau currently contains three tools: a decompiler and disassembler for Java classfiles and an assembler to create classfiles

• Lilac, an assembler and disassembler for the Java virtual machine.

Some projects provide Java assemblers to enable writing Java bytecode

•JVM-Types and their prefixes▫Byte  b ▫Short  s ▫ Integer  i  (java booleans are mapped to jvm ints!)▫Long  l ▫Character  c▫Single float f ▫double float d ▫References  a  to Classes, Interfaces, Arrays

•These Prefixes used in opcodes (iadd, astore,...)

Bytecode Basics

Instructions The JVM has 256 instructions for: •Arithmetic operations •Branch operations •Constant loading operations •Locals operations •Stack operations •Class operations •Method operations

Of the 256 possible byte-long opcodes, as of 2015, 198 are in use (~77%), 54 are reserved for future use, and 3 instructions (~1%) are set aside as permanently unimplemented.

Instructions fall into a number of broad groups:•Load and store (e.g. aload_0, istore)•Arithmetic and logic (e.g. ladd, fcmpl)•Type conversion (e.g. i2b, d2i)•Object creation and manipulation (new, putfield)•Operand stack management (e.g. swap, dup2)•Control transfer (e.g. ifeq, goto)•Method invocation and return (e.g. invokespecial, areturn)

Instructions Conti…

▫ Byte  b ▫ Short  s ▫ Integer  i ▫ Long  l ▫ Character c▫ Single float f ▫ double float

d ▫ References 

Arithmetic Operations Operands

Operations

Instructions Conti…

Java Bytecode Explanation

Opcode Mnemonic Description

0 nop Does nothing

1 aconst_null Push null on the stack3 iconst_0 Push int 0 on the stack

4 iconst_1 Push int 1 on the stack

18 ldc <value> Push a one-word (4 bytes) constant onto the stack

ldc “Hello”ldc 201

Constant may be an int, float or String

Instructions Conti…

Other types of Instructions

• Control Flow (~20 instructions)– if, goto, return

• Method Calls (4 instructions)• Loading and Storing Variables (65

instructions) • Creating objects (1 instruction)• Using object fields (4 instructions)• Arrays (3 instructions)

Javap examples

public class Test1 {

public int add(int a, int b) { int c= a+b; return c; }}

javap -c Test1javac -g Test1.java

Javap included with Java Development Kit (JDK)

…public int add(int, int); Code: 0: iload_1 1: iload_2 2: iadd 3: istore_3 4: iload_3 5: ireturn

// add var 1 and 2// store as local var 3// store onto stack// return int

// push onto stack// push onto stack

public class Test1 {

public int add(int a, int b) { int c= a+b; return c; }}

… public int add(int a, int b) { int c = a + b; // 0 0:iload_1 // 1 1:iload_2 // 2 2:iadd // 3 3:istore_3 return c; // 4 4:iload_3 // 5 5:ireturn }

jad -a Test1javac -g Test1.java

JAD is free, but not included with Java Development Kit (JDK)

JAD examples

THANKS

top related