CS216: Program and Data Representation

Slides:



Advertisements
Similar presentations
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
Advertisements

1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
BA1 JVM arkitektur BA2 Specifikation I The Java Virtual Machine Specification specificeres bl.a.: –De grundlæggende datatyper.
1 1 Lecture 14 Java Virtual Machine Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Lab 9 Java Bytecode & The Jasmin Assembler
CS 536 Spring Code generation I Lecture 20.
Procedure calls (1) The fact: Most programming languages support the concept of procedures (methods). Each method has its own local variables that are.
Chapter 4 H1 Assembly Language: Part 2. Direct instruction Contains the absolute address of the memory location it accesses. ld instruction:
Consider With x = 10 we may proceed as (10-1) = 9 (10-7) = 3 (9*3) = 27 (10-11) = -1 27/(-1) = -27 Writing intermediates on paper.
Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)
Application Security Tom Chothia Computer Security, Lecture 14.
JVM And CLR Dan Agar April 16, Outline Java and.NET Design Philosophies Overview of Virtual Machines Technical Look at JVM and CLR Comparison of.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
1 The Java Virtual Machine Yearly Programming Project.
The Java Virtual Machine 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 19: Security in Java Real or.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
Java Bytecode What is a.class file anyway? Dan Fleck George Mason University Fall 2007.
Lecture 10 : Introduction to Java Virtual Machine
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
1 Introduction to JVM Based on material produced by Bill Venners.
ITEC 352 Lecture 12 ISA(3). Review Buses Memory ALU Registers Process of compiling.
Class 14: Object-Oriented Programming Fall 2010 University of Virginia David Evans cs2220: Engineering Software.
A Java Compiler Overview. October 21, 2003Shane A. Brewer2 Who Am I? Shane A. Brewer Masters Graduate.
1 CPSC 185 Introduction to Computing The course home page
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
Prof. Fateman CS 164 Lecture 281 Virtual Machine Structure Lecture 28.
Programming Languages
Runtime System CS 153: Compilers. Runtime System Runtime system: all the stuff that the language implicitly assumes and that is not described in the program.
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
#include "dump.h" int main ( int argc, char* argv[] ) { __asm { mov eax, 1// init eax to 1 mov ebx, esp; keep a copy of esp mov ecx, 3/* init ecx to 3.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
20-Feb-16 javap. Bytecode Java is compiled into bytecode, which is intermediate between Java and a “real” assembly language To implement Java, it is only.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE II CS2110 Fall 200 Lecture 25 1.
C is a high level language (HLL)
Review on Program Challenge CSc3210 Yuan Long.
RealTimeSystems Lab Jong-Koo, Lim
CS 536 © CS 536 Spring Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 15.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
Computer Architecture Instruction Set Architecture
The Java Virtual Machine (JVM)
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
CS 153: Concepts of Compiler Design November 2 Class Meeting
CS 11 C track: lecture 8 Last week: hash tables, C preprocessor
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
Lecture 4: MIPS Instruction Set
Security in Java Real or Decaf? cs205: engineering software
CSc 453 Interpreters & Interpretation
Introduction to Algorithm Design
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
CS 153: Concepts of Compiler Design November 6 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 17: 0xCAFEBABE (Virtual Machines) http://www.cs.virginia.edu/cs216

CS216 Roadmap Bits Data Representation Program Representation Real World Problems High-level language Objects Python code “Hello” Arrays [‘H’,’i’,\0] Low-level language Addresses, Numbers, Characters C code 0x42381a, 3.14, ‘x’ JVML Virtual Machine language x86 Assembly Bits 01001010 Real World Physics

Java Virtual Machine JVML is a detour: everything else we have seen is part of running the PS1 Python program

Java: Programming Language “A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high­performance, multithreaded, and dynamic language.” [Sun95] Properties of language implementations, not languages Wednesday’s class

Java: Programming Language “A simple, object-oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high­performance, multithreaded, and dynamic language.” [Sun95] compared to C++, not to C sort of ^ ^ Java: int is 32 bits C: int is >= 16 bits

JVML code.java code.class Java javac Source Compiler JVML Code Object JavaVM

Java Virtual Machine Small and simple to implement All VMs will run all programs the same way “Secure” Java Ring (1998)

Implementing the JavaVM load class into memory set the instruction pointer to point to the beginning of main while not finished: fetch the next instruction execute that instruction Some other issues we will talk about Wednesday: Verification – need to check byte codes satisfy security policy

Java Byte Codes Stack-based virtual machine Small instruction set: 202 instructions (all are 1 byte opcode + operands) Intel x86: ~280 instructions (1 to 17 bytes long!) Memory is typed Every Java class file begins with magic number 3405691582 = 0xCAFEBABE in hex

Stack-Based Computation push – put something on the top of the stack pop – get and remove the top of the stack Stack push 2 2 5 push 3 3 add Does 2 pops, pushes sum

Some JVML Instructions Opcode Mnemonic Description nop Does nothing 1 aconst_null Push null on the stack 3 iconst_0 Push int 0 on the stack 4 iconst_1 Push int 1 on the stack … Why do we need both aconst_null and iconst_0?

Constant may be an int, float or String Load Constant Opcode Mnemonic Description 18 ldc <value> Push a one-word (4 bytes) constant onto the stack Constant may be an int, float or String ldc “Hello” ldc 216 The String is really a reference to an entry in the string constant table!

Arithmetic Opcode Mnemonic Description 96 iadd Pops two integers from the stack and pushes their sum iconst_2 iconst_3 iadd

Arithmetic Opcode Mnemonic Description 96 iadd 97 ladd … 106 fmul 119 Pops two integers from the stack and pushes their sum 97 ladd Pops two long integers from the stack and pushes their sum … 106 fmul Pops two floats from the stack and pushes their product 119 dneg Pops a double from the stack, and pushes its negation

Java Byte Code Instructions 0: nop 1-20: putting constants on the stack 96-119: arithmetic on ints, longs, floats, doubles 1 byte opcode: 146 left What other kinds of instructions do we need?

Other Instruction Classes Control Flow (~20 instructions) if, goto, return Loading and Storing Variables (65 instructions) Method Calls (4 instructions) Creating objects (1 instruction) Using object fields (4 instructions) Arrays (3 instructions)

Control Flow ifeq <label> if_icmple <label> Pop an int off the stack. If it is zero, jump to the label. Otherwise, continue normally. if_icmple <label> Pop two ints off the stack. If the second one is <= the first one, jump to the label. Otherwise, continue normally.

Referencing Memory iload <varnum> istore <varnum> Pushes the int in local variable <varnum> (1 bytes) on the stack istore <varnum> Pops the int on the top of the stack and stores it in local variable <varnum>

Referencing Example public class Locals1 { Method void main(java.lang.String[]) 0 iconst_2 1 istore_1 2 iconst_3 3 istore_2 4 iload_1 5 iload_2 6 iadd 7 istore_3 8 getstatic #2 <Field java.io.PrintStream err> 11 new #3 <Class java.lang.StringBuffer> 14 dup 15 invokespecial #4 <Method java.lang.StringBuffer()> 18 ldc #5 <String "c: "> 20 invokevirtual #6 <Method java.lang.StringBuffer append(java.lang.String)> 23 iload_3 24 invokevirtual #7 <Method java.lang.StringBuffer append(int)> 27 invokevirtual #8 <Method java.lang.String toString()> 30 invokevirtual #9 <Method void println(java.lang.String)> 33 return public class Locals1 { static public void main (String args[]) { int a = 2; int b = 3; int c = a + b; System.err.println ("c: " + c); } }

invokevirtual <Method void println(java.lang.String)> Method Calls invokevirtual <method> Invokes the method <method> on the parameters and object on the top of the stack. Finds the appropriate method at run-time based on the actual type of the this object. invokevirtual <Method void println(java.lang.String)>

Method Calls invokestatic <method> Invokes a static (class) method <method> on the parameters on the top of the stack. Finds the appropriate method at run-time based on the actual type of the this object.

Example public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); }

> javap -c Sample1 Compiled from Sample1.java public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); } } > javap -c Sample1 Compiled from Sample1.java public class Sample1 extends java.lang.Object { public Sample1(); public static void main(java.lang.String[]); } Method Sample1() 0 aload_0 1 invokespecial #1 <Method java.lang.Object()> 4 return Method void main(java.lang.String[]) 0 getstatic #2 <Field java.io.PrintStream err> 3 ldc #3 <String "Hello!"> 5 invokevirtual #4 <Method void println(java.lang.String)> 8 iconst_1 9 invokestatic #5 <Method void exit(int)> 12 return

Charge PS5: Due Wednesday Question 2 is a “tricky” question Focus on correctness: implement something simple for questions 7-9 first You can describe clever designs for question 6, simplicity should be the main factor in deciding what to implement