JAM 16: The Instruction Set & Sample Programs

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

Compiling Techniques

Recap: Printing Trees into Bytecodes

Static Program Analysis

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

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

Topics. Block Diagram of Mic-1 Microarchitecture. Review Sheet. Microarchitecture IJVM ISA

Compiler construction 2009

Final Exam. 12 December 2018, 120 minutes, 26 questions, 100 points

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

Run-time Program Management. Hwansoo Han

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

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

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

CSCE 314 Programming Languages

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

Chapter 5. A Closer Look at Instruction Set Architectures

High-Level Language VMs

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

Code Generation Introduction

CSc 453 Interpreters & Interpretation

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

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

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

Principles of Computer Architecture. Chapter 5: Languages and the Machine

CMPSC 497: Java Security

Oak Intermediate Bytecodes

Course Administration

Let s make some Marc R. Hoffmann Eclipse Summit Europe

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Practical Malware Analysis

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

Implementing on Object-Oriented Language 2.14

Java: framework overview and in-the-small features

Systems I. Machine-Level Programming V: Procedures

The Java Virtual Machine

Java byte code verification

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

Virtual Machine Tutorial

02 B The Java Virtual Machine

Part VII : Code Generation

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Final Exam. 11 May 2018, 120 minutes, 26 questions, 100 points

Compiler construction 2009

Translating JVM Code to MIPS Code 1 / 43

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CSC 4181 Handout : JVM

The Java Virtual Machine

COMP3131/9102: Programming Languages and Compilers

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

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

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

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

Tutorial 3: Code Generation

ASSEMBLY III: PROCEDURES. Jo, Heeseung

Programming Language Systems

Assembly III: Procedures. Jo, Heeseung

Lecture 4: MIPS Instruction Set

MIPS Instruction Set Architecture (2)

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

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

Problems of Bytecode Verification

Digital Forensics Lecture 3 - Reverse Engineering

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

Grundlagen Microcontroller Processor Core. Günther Gridling Bettina Weiss

Computer Architecture /

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

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

CSE-304 Compiler Design

CSC 2400: Computer Systems. Using the Stack for Function Calls

Java Instrumentation for Dynamic Analysis

1 Little Man Computer

SOEN228, Winter Revision 1.2 Date: October 25,

Java Class Loading and Bytecode Verification

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

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

Subprograms, Subroutines, and Functions

Exercise 7 Bytecode Verification self-study exercise sheet

CSc 520 Principles of Programming Languages

Administration CS 412/413. Why build a compiler? Compilers. Architectural independence. Source-to-source translator

What is SaM? SaM is a simple stack machine designed to introduce you to compilers in a few lectures SaM I: written by Dr. Keshav Pingali around 2000

The Microarchitecture Level

CSCE 5610: Computer Architecture

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Chapter 5. A Closer Look at Instruction Set Architectures

Just In Time Compilation

Midterm 2 Review Chapters 4-16 LC-3

CSc 453. Compilers and Systems Software. 18 : Interpreters. Department of Computer Science University of Arizona. Kinds of Interpreters

Assembly III: Procedures. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

MIPS Functions and the Runtime Stack

CSC 8400: Computer Systems. Using the Stack for Function Calls

Lecture 6: Assembly Programs

The MARIE Architecture

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 19, 2014

Problem with Scanning an Infix Expression

CSE 431S Final Review. Washington University Spring 2013

COSC121: Computer Systems: Review

Run-time Environments

Transcription:

JAM 16: The Instruction Set & Sample Programs Copyright Peter M. Kogge CSE Dept. Univ. of Notre Dame Jan. 8, 1999, modified 4/4/01 Revised to 16 bits: Dec. 5, 2007 JAM 16: 1

Java Terms Java: A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, and dynamic language. (Sun) Java Virtual Machine (JVM): - an Instruction Set Architecture into which Java programs are compiled - an interpreter which simulates this ISA (like XSPIM) JVM bytecodes: a sequence of bytes that define a JVM instruction Interpreter: software program that emulates at RTL level the execution of a program written in some ISA (usually different from that of the machine running the interpreted) - Java Interpreter: interprets programs constructed as JVM bytecodes Java Application: program written in Java that is executed like a program in any other language Java Applet: - (Small) program written in Java - Embedded in a web page - Loaded by, & interpreted in, web browser R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 2

The Java Execution Model (non Applet).class file format: Header information Constant Pool - Values of constants in the Java program Interfaces - Names of superclasses, interface methods,... Methods (i.e. procedures) - One per method in original program - Storage requirements for execution - Compiled code in JVM machine language *.java files (Java source) Java Compiler *.class files Class Loader Parser Verifier JVM = Java Virtual Machine Instruction Set Architecture Easily interpreted by software Java chips under development JVM bytecode interpreter R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 3

The Java Virtual Machine Four 32 bit Programmer visible registers - pc: program counter = pointer to next bytecoded JVM instruction - optop: pointer to the top of a programmer-visible stack in memory - frame: pointer to memory area describing current method - vars: pointer to beginning of local variables for current method Heap: area in memory from which objects dynamically allocated Stack: area in memory referenced by optop, frame, vars registers - Separate stack allocated from heap at each method invocation - Holds arguments for, and results from, bytecode instructions - Holds arguments for, and results from, method invocations (local variables) - Holds state of each method invocation (its frame or environment) - Each stack word holds 32 bits (4 bytes) Bytecoded Instructions: one or more bytes that represent an instruction Support for operands of different sizes, 8, 16, 32 fixed, 32, 64 floating, strings, arrays,... R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 4

JAM-16 JAM-16 = Java Abstract Machine - 16 bit version - Too hard to say JVM-16 Similarities to JVM - Same four registers - Subset of same bytecodes - Still stack oriented Differences from JVM - No heap - Only single stack which holds multiple method areas - All registers, objects only 16 bits wide - No floating point, extended precision operations - 16 bit integer subset selected - All addresses only 16 bits; Memory space limited to 65K locations - Method call/return somewhat simplified - Only simple objects supported R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 5

65K Memory Stack Free Space JAM-16 Memory Model vars=pointer to arguments and local variables frame=pointer to state info for current method optop=pointer to top of stack Pushing involves decrementing optop Popping involves incrementing optop Code pc=pointer to next bytecoded instruction 0 R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 6

JAM-16 Method Stack Environment Caller s environment Argument_0 Argument_1... Argument_n old_pc old_vars old_frame Working value Topmost working vars = pointer to start of current local variables (current method arguments and local storage) A single environment for a method invocation frame = pointer to info about caller Intermediate values for this method s computation optop = pointer to last used stack cell Stack grows down towards smaller addresses R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 7

JAM-16 Instruction Descriptions Each instruction includes: - Bytecode (in binary and hexadecimal) -Mnemonic - Description: English, stack, RTL - Instructions of form <mnemonic> # mean a bytecoded instruction followed by an 8 bit integer *used for address or data) Mnemonic is followed by footnotes: - 1 => same opcode and semantics as JVM - 2 => operand value is only 8 bits - 3 => # (2nd byte) is 1, not 2, bytes long (JVM has 2 bytes) - 4 => same opcode, but slightly different semantics from JVM - 5 => new instruction not present in JVM BEFORE: Stack notation used in bytecode descriptions: 65K. -..., a1, a2,... an =>..., b1, b2,... bk.. same - Left of => is stack before instruction is executed a1 values... - Right of => is stack after instruction is executed an - top n values are replaced by k results 65K... AFTER: b1... bk R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 8

JAM-8: Basic Stack Push Operations Bytecode Mnemonic English/Stack/RTL 00010000 bipush # Push a one byte constant to stack 0x10 1... =>..., # 01011001 0x59 00010101 0x15 11111110 0xFE 00011001 0x19 dup 1 iload # 1,2 rload 5 aload # 4 optop--; M(optop)<-M(pc+1); pc<-pc+2 Duplicate top element of stack..., value =>..., value, value M(optop-1)<-M(optop); optop-- pc++ Push local variable # to stack... =>..., Local_variable_# M(optop-1)<-M(vars-#); optop--; pc<-pc+2 Load using top of stack as address..., adr =>..., M(adr) M(optop)<-M(M(optop)); pc++ Push address of local var # to stack... =>..., vars-# optop--; M(optop)<-vars-#; pc<-pc+2 R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 9

Basic Stack Pop Operations Bytecode Mnemonic Stack/RTL 00110110 istore # Pop stack to local variable # 0x36 1,2..., value =>... 111111110x FF 01010111 0x57 rstore 5 pop 1 M(vars-#)<-M(optop++); pc<-pc+2 Pop stack top to memory..., adr, value =>... M(M(optop+1))<-M(optop); optop<-optop+2; pc++ Pop top value off of stack and discard..., value =>... optop++ pc++ R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 10

Basic Computational Operations Bytecode Mnemonic Stack/RTL 01100000 0x60 iadd 1,2 Add top two elements on stack..., value1, value2 => result M(optop+1)<-M(optop+1)+M(optop); optop++; pc++ 01100100 0x64 isub 1,2 Subtract top two elements on stack..., value1, value2 => result M(optop+1)<-M(optop+1)-M(optop); optop++; pc++ 01111111 0x7E iand 1,2 And top two elements on stack..., value1, value2 => result M(optop+1)<-M(optop+1) and M(optop); optop++; pc++ 10000000 0x80 ior 1,2 Or top two elements on stack..., value1, value2 => result M(optop+1)<-M(optop+1) or M(optop); optop++; pc++ 10000100 0x84 iinc #1,#2 1,2 Increment local variable #1 by #2... =>... M(vars-#1)<-M(vars-#1)+#2; pc<-pc+3 01011111 0x5F swap 1 Swap top two values..., value1, value2 =>..., value 2, value1 temp<-m(optop); M(optop)<-M(optop+1); M(optop+1)<-temp; pc++ R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 11

Basic Transfer of Control Bytecode Mnemonic Stack/RTL 10100111 goto # go to # 0xA7 1,3... =>... 10011001 0x99 10011010 0x9A 10011011 0x9B 10011100 0x9C 11001010 0xCA ifeq # 1,3 ifne # 1,3 iflt # 1,3 ifge # 1,3 breakpoint 4 PC<-# if top of stack=0 then go to #..., value =>... if M(optop++)=0 then PC<-# else PC<-PC+2 if top of stack<>0 then go to #..., value =>... if M(optop++)<>0 then PC<-# else PC<-PC+2; if top of stack<0 then go to #..., value =>... if M(optop++)<0 then PC<-# else PC<-PC+2; if top of stack>=0 then go to #..., value =>... if M(optop++)>=0 then PC<-# else PC<-PC+2; wait until external signal... =>... when go goes from 0 to 1 then continue R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 12

Basic Method (Procedure) Invocation Bytecode Mnemonic Stack/RTL 10101000 0xA8 10101001 0xA9 10111000 0xB8 10101100 0xAC jsr # 1,3 ret 4 invoke # 4 ireturn 1,2 Jump to subroutine at #... =>..., return_address M(optop-1)<-PC+2;optop-- PC<-# return from subroutine..., return_address =>... pc<-m(optop++) call new method... =>..., space, old_pc, old_vars, old_frame optop<-optop-m(#); M(optop)<-pc+2; M(optop-1)<-vars; vars<-optop+m(#+1); M(optop-2)<-frame; frame<-optop-2; pc<-#+2; optop<-optop-2; return value to calling method..., value => stack as of last invoke, value M(vars)<-M(optop); optop<-vars; pc<-m(frame+2); vars<-m(frame+1); frame<-m(frame) R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 13

JAM-16 at Reset pc<-0; vars<-0; frame<-0; optop<-0 Program execution starts at location 0 First push of any value goes to location 65,535 (0xFF) - top of memory To allocate space for global variables, need an early invoke 0: invoke 3 (Note: this uses the two numbers at 3 as constants to set up stack space) 2: breakpoint 3: n+1 (a magic constant) where n=amount of storage for global variables; see invoke 4: n 5: start of main program - can now reference local vars R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 14

Compilation Constant value accessing: bipush # to place on stack Variable value accessing: - Local variable: iload # or istore # to move value to/from stack and # argument in current stack frame - Static/Global variable: rload or rstore use value on the stack as an address Expression evaluation: convert expression into Reverse Polish - Eg. A + (B and ( C - D)) => A B C D - and + - Generate code from left to right: vars -> iloads, ops -> bytecodes - Eg. A B C D - and + => iload A; iload B; iload C; iload D; sub; and; add; If <expr1> <comparator> <expr2> then <then_code> else <else_code>: - Assume <comparator> is = or!= - Compile <expr1> to leave value on stack - Compile <expr2> to leave value on stack - sub - ifeq or ifne # where # is address of start of else code - Compile <then_code>, followed by goto # where # is after <else> - Compile <else_code> R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 15

Compilation (Continued) while <expr1> <comparator> <expr2> do <while_body> - goto # where # is address of start of condition - Compile <while_body> - Compile <expr1> - Compile <expr2> - ifeq or ifne with #, where # is address of start of <while_body> Simple procedure call - jsr # to save pc on stack and go to #. frame, vars unchanged - In body of procedure, for return use ret Method/Function call with arguments/local definitions: foo(a0,... an) - Compile each argument ai to leave value on stack, in order 0,... n - invoke # where # is address of start of procedure Function/method body - At entry, first byte has value = # of extra local storage needed +1 - Next is a byte with value = # arguments + # local storage - Following this is start of code for body of method - At completion, top of stack has return value, do ireturn R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 16

char n, m; char temp[10]; main() { initarray(); m=total(1, 5, &temp[0]); } Sample Program void initarray(void); // initialize global array temp { n=0; while (n!= 10) {temp[n]=n; n++} } byte total(char n, char m, char *ptr); char sum=0; char i; // Sum up elements of input array from n to m { for (i=n; i!= m; i++) sum=sum+*(ptr+i); return sum} R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 17

Sample Compilation int n, m; int temp[10]; 0: invoke 3 // initial call to establish storage for globals 2: breakpoint // this is here to stop the machine if it runs amok 3: 24 //space for n,m, and 10 array elts // note that n is local 0, m is local 2, temp starts at local 4 4: 24 // magic constant needed by invoke main (void) initarray; 5: jsr 18 // call initarray without building new context m=total(1, 5, &temp[0]); 7: bipush 1 // first argument 9: bipush 5 // second argument 11: aload 2 // third argument - address of temp[0] 13: invoke 43 // call total with new frame 15: istore 1 // save returned value in m 17: breakpoint // our instruction to stop after main program is done } R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 18

Sample Compilation (page 2) void initarray(void);// start initarray next { n=0; 18: bipush 0 // write 0 into n 20: istore 0 while (n!= 10) 22: goto 35 // jump to the loop test {temp[n]=n; 24: aload 2 // compute address of temp[n] 26: iload 0 28: isub 29: iload 0 // get value of n 31: rstore // store n into temp(n) n++} } 32: iinc 0,1 // increment n by 1 35: iload 0 // start of the loop test 37: bipush 10 39: isub 40: ifne 24 // if loop continues, branch backwards to loop body 42: ret // return from initarray R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 19

Sample Compilation (page 3) byte total(char n, char m, char *ptr); char sum=0; char i; 43: 3 // 2 local variables 44: 5 // 3 arguments + 2 local vars 45: bipush 0 // initial value for sum 47: istore 3 // store it in sum for (i=n; i!= m; i++) sum=sum+&(ptr+i); 49: iload 0 // get the local n 51: istore 4 // store in local i 53: goto 69 // enter the test code 55: iload 3 // get sum 57: iload 2 // get pointer to array 59: iload 4 // get i 61: isub // compute address of array 62: rload // get value of array location 63: iadd // compute new sum 64: istore 3 // and update sum 66: iinc 4,1 // increment i by 1 69: iload 4 // get i. start of loop test 71: iload 1 // get m 73: isub 75: ifne 55 // return to loop start return sum} 77: iload 3 79: ireturn R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 20

Layers in the ISA Simple 12 equivalent ISA bipush rload rstore iadd isub iand ior goto ifeq iflt Minimal Java extensions invoke ireturn aload Basic computing enhancements: jsr ret Java enhancements iload istore Additional enhancements ifne ifgt swap dup pop iinc JAM-8 R e v i s e d t o 1 6 b i t s : D e c. 5, 2 0 0 7 21