1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson.

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

Virtual Machines Matthew Dwyer 324E Nichols Hall
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
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.
CS 153: Concepts of Compiler Design November 10 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Lab 9 Java Bytecode & The Jasmin Assembler
1 Java Virtual Machine Professor Yihjia Tsai Tamkang University.
JETT 2003 Java.compareTo(C++). JAVA Java Platform consists of 4 parts: –Java Language –Java API –Java class format –Java Virtual Machine.
Run time vs. Compile time
JVM-1 Introduction to Java Virtual Machine. JVM-2 Outline Java Language, Java Virtual Machine and Java Platform Organization of Java Virtual Machine Garbage.
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.
1 Languages and Compilers (SProg og Oversættere) Lecture 9 (2) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Intro to Java The Java Virtual Machine. What is the JVM  a software emulation of a hypothetical computing machine that runs Java bytecodes (Java compiler.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
UNDER THE HOOD: THE JAVA VIRTUAL MACHINE Lecture 24 – CS2110 – Fall 2009.
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.
Implement High-level Program Language on JVM CSCE 531 ZHONGHAO LIU ZHONGHAO LIU XIAO LIN.
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.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Lecture 10 : Introduction to Java Virtual Machine
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Implementation of a Stored Program Computer ITCS 3181 Logic and Computer Systems 2014 B. Wilkinson Slides2.ppt Modification date: Oct 16,
10/12/2015© Hal Perkins & UW CSEV-1 CSE P 501 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Winter 2008.
1 Introduction to JVM Based on material produced by Bill Venners.
Roopa.T PESIT, Bangalore. Source and Credits Dalvik VM, Dan Bornstein Google IO 2008 The Dalvik virtual machine Architecture by David Ehringer.
Syntax Directed Translation Compiler Design Lecture (03/16//98) Computer Science Rensselaer Polytechnic.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
12/10/2002© 2002 Hal Perkins & UW CSET-1 CSE 582 – Compilers Java Implementation – JVMs, JITs &c Hal Perkins Autumn 2002.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
Runtime Organization (Chapter 6) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
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.
CSCE 314 Programming Languages JVM Dr. Hyunyoung Lee 1.
1 Lecture 12 Java Virtual Machine:.class file Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute.
Chap. 10, Intermediate Representations J. H. Wang Dec. 14, 2015.
Object Oriented Software Development 4. C# data types, objects and references.
Programming Languages and Paradigms Activation Records in Java.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 18: Code Safety and Virtual Machines
CS 153: Concepts of Compiler Design November 4 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design November 18 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Chap. 10, Intermediate Representations J. H. Wang Dec. 27, 2011.
Review on Program Challenge CSc3210 Yuan Long.
Hello world !!! ASCII representation of hello.c.
RealTimeSystems Lab Jong-Koo, Lim
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
(Not too) Real-Time JVM (Progress Report)
CS 432: Compiler Construction Lecture 15
Assembly language.
The Java Virtual Machine (JVM)
CS216: Program and Data Representation
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
Java Virtual Machine (JVM)
Java Byte Codes (0xCAFEBABE) cs205: engineering software
CSc 453 Interpreters & Interpretation
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Byte Code Verification
Course Overview PART I: overview material PART II: inside a compiler
Java Implementation – JVMs, JITs &c Hal Perkins Summer 2004
CS 153: Concepts of Compiler Design November 6 Class Meeting
CMPE 152: Compiler Design April 11 Class Meeting
CMPE 152: Compiler Design April 16 Class Meeting
CSc 453 Interpreters & Interpretation
Presentation transcript:

1 Languages and Compilers (SProg og Oversættere) Bent Thomsen Department of Computer Science Aalborg University With acknowledgement to Norm Hutchinson whose slides this lecture is based on.

2 The JVM In this lecture we look at the JVM as an example of a real-world runtime system for a modern object-oriented programming language. The material in this lecture is interesting because: 1)it will help understand some things about the JVM 2)JVM is probably the most common and widely used VM in the world. 3) You’ll get a better idea what a real VM looks like.

3 RECAP: Interpretive Compilers Why? A tradeoff between fast(er) compilation and a reasonable runtime performance. How? Use an “intermediate language” more high-level than machine code => easier to compile to more low-level than source language => easy to implement as an interpreter Example: A “Java Development Kit” for machine M Java->JVM M JVM M

4 Abstract Machines Abstract machine implements an intermediate language “in between” the high-level language (e.g. Java) and the low-level hardware (e.g. Pentium) Java Pentium Java Pentium JVM (.class files) High level Low level Java compiler Java JVM interpreter or JVM JIT compiler Implemented in Java: Machine independent

5 P JVMJava P Interpretive Compilers Remember: our “Java Development Kit” to run a Java program P Java->JVM M JVM M M JVM P M javacjava

6 Hybrid compiler / interpreter

7 Abstract Machines An abstract machine is intended specifically as a runtime system for a particular (kind of) programming language. JVM is a virtual machine for Java programs: It directly supports object oriented concepts such as classes, objects, methods, method invocation etc. easy to compile Java to JVM => 1) easy to implement compiler 2) fast compilation another advantage: portability

8 Class Files and Class File Format The JVM is an abstract machine in the true sense of the word. The JVM spec. does not specify implementation details (can be dependent on target OS/platform, performance requirements etc.) The JVM spec defines a machine independent “class file format” that all JVM implementations must support..class files JVM load External representation platform independent internal representation implementation dependent objects classes methods integers arrays primitive types

9 Class File Table of constants. Tables describing the class –name, superclass, interfaces –attributes, constructor Tables describing fields and methods –name, type/signature –attributes (private, public, etc) The code for methods.

10 ClassFile { u4 magic; u2 minor_version; u2 major_version; u2 constant_pool_count; cp_info constant_pool[ constant_pool_count-1 ]; u2 access_flags; u2 this_class; u2 super_class; u2 interfaces_count; u2 interfaces[interfaces_count]; u2 fields_count; field_info fields[fields_count]; u2 methods_count; method_info methods[methods_count]; u2 attributes_count; attribute_info attributes[ attributes_count ]; }

11 Data Types JVM (and Java) distinguishes between two kinds of types: Primitive types: boolean: boolean numeric integral: byte, short, int, long, char numeric floating point: float, double internal, for exception handling: returnAddress Reference types: class types array types interface types Note: Primitive types are represented directly, reference types are represented indirectly (as pointers to array or class instances)

12 Data Types: Some additional remarks Return Address Type –Used by the JVM instructions jsr (jump to subroutine) jsr_w (wide jump to subroutine) ret (return from subroutine) The boolean Type –Very limited support for boolean type in JVM Java´s boolean type is compiled to int type Coding: true = 1, false = 0 Explicit support for boolean arrays implemented as byte-arrays Floating Point Types –No Exceptions (signaling conditions acc. to IEEE 754) –Positive and negative zero, positive and negative infinity special value NaN (not a number, comparision always yields false)

13 Internal Architecture of JVM Execution engine Class loader subsystem method area heap Java stacks pc registers native method stacks Runtime data area class files Native Method Interface Native Method Libraries

14 JVM: Runtime Data Areas Besides OO concepts, JVM also supports multi-threading. Threads are directly supported by the JVM. => Two kinds of runtime data areas: 1) shared between all threads 2) private to a single thread SharedThread 1Thread 2 pc Java Stack Native Method Stack pc Java Stack Native Method Stack Garbage Collected Heap Method area

15 Java Stacks JVM is a stack based machine, much like TAM. JVM instructions implicitly take arguments from the stack top put their result on the top of the stack The stack is used to pass arguments to methods return result from a method store intermediate results in evaluating expressions store local variables This works similarly to (but not exactly the same as) the way we discussed in the lectures on stack-based allocation and routines.

16 Stack Frames The Java stack consists of frames. The JVM specification does not say exactly how the stack and frames should be implemented. The JVM specification specifies that a stack frame has areas for … to runtime constant pool local vars operand stack args + A new call frame is created by executing some JVM instruction for invoking a method (e.g. invokevirtual, invokenonvirtual,...) The operand stack is initially empty. But grows and shrinks during execution.

17 Stack Frames local vars operand stack args + Stack for storing intermediate results during the execution of the method. Initially it is empty. The maximum depth is known at compile time. The role/purpose of each of the areas in a stack frame: pointer to constant pool Needed for resolution: more about this later. This is used implicitly when executing JVM instructions that contain entries into the CPool space where the arguments and local variables of a method are stored. This includes a space for the receiver ( this ) at position 0.

18 Stack Frames An implementation using registers such as SB, ST and LB and a dynamic link is one possible implementation. LB ST dynamic link SB to previous frame on the stack to runtime constant pool local vars operand stack args + JVM instructions store and load for accessing args and locals use addresses which are numbers from 0 to #args + #locals - 1

19 JVM Interpreter The core of a JVM interpreter is basically this: do { byte opcode = fetch an opcode; switch (opcode) { case opCode1 : fetch operands for opCode1; execute action for opCode1; break; case opCode2 : fetch operands for opCode2; execute action for opCode2; break; case... } while (more to do)

20 Instruction-set: typed instructions! JVM instructions are explicitly typed: different opCodes for instructions for integers, floats, arrays and reference types. This is reflected by a naming convention in the first letter of the opCode mnemonics: Example: different types of “load” instructions iload lload fload dload aload integer load long load float load double load reference-type load

21 Instruction set: kinds of operands JVM instructions have three kinds of operands: - from the top of the operand stack - from the bytes following the opCode - part of the opCode One instructions may have different “forms” supporting different kinds of operands. Example: different forms of “iload”. iload_0 iload_1 iload_2 iload_3 Assembly codeBinary instruction code layout n iload n wide iload n196n21

22 Instruction-set: accessing arguments and locals locals: indexes #args.. #args+#locals-1 args: indexes 0.. #args-1 arguments and locals area inside a stack frame Instruction examples: iload_1 iload_3 aload 5 aload_0 istore_1 astore_1 fstore_3 0: 1: 2: 3: A load instruction: loads something from the args/locals area to the top of the operand stack. A store instruction takes something from the top of the operand stack and stores it in the argument/local area

23 Instruction-set: non-local memory access In the JVM, the contents of different “kinds” of memory can be accessed by different kinds of instructions. accessing locals and arguments: load and store instructions accessing fields in objects: getfield, putfield accessing static fields: getstatic, putstatic Note: static fields are a lot like global variables. They are allocated in the “method area” where also code for methods and representations for classes are stored. Q: what memory area are getfield and putfield accessing? Note: we don’t have something similar to L1, L2, etc. addresses in JVM

24 Instruction-set: operations on numbers add: iadd, ladd, fadd, dadd subtract: isub, lsub, fsub, dsub multiply: imul, lmul, fmul, dmul etc. Arithmethic Conversion i2l, i2f, i2d l2f, l2d, f2s f2i, d2i, …

25 Instruction-set … Operand stack manipulation pop, pop2, dup, dup2, dup_x1, swap, … Control transfer Unconditional : goto, goto_w, jsr, ret, … Conditional: ifeq, iflt, ifgt, …

26 Instruction-set … Method invocation: invokevirtual : usual instruction for calling a method on an object. invokeinterface : same as invokevirtual, but used when the called method is declared in an interface. (requires different kind of method lookup) invokespecial : for calling things such as constructors. These are not dynamically dispatched (this instruction is also known as invokenonvirtual ) invokestatic : for calling methods that have the “static” modifier (these methods “belong” to a class, rather an object) Returning from methods: return, ireturn, lreturn, areturn, freturn, …

27 Instruction-set: Heap Memory Allocation Create new class instance (object): new Create new array: newarray : for creating arrays of primitive types. anewarray, multianewarray : for arrays of reference types

28 Instructions and the “Constant Pool” Many JVM instructions have operands which are indexes pointing to an entry in the so called constant pool. The constant pool contains all kinds of entries representing “symbolic” references for “linking”. This is the way that instructions refer to things such as classes, interfaces, methods, fields and constants such as string literals and numbers. These are the kinds of constant pool entries that exist: Class_info Fieldref_info Methodref_info InterfaceMethodref_info String Integer Float Long Double NameAndType_info Utf8

29 Instructions and the “Constant Pool” Example: We examine the getfield instruction in detail. 180indexbyte1indexbyte2 Format: CONSTANT_Fieldref_info { u1 tag; u2 class_index; u2 name_and_type index; } Class_info { u1 tag; u2 name_index; } Utf8Info fully qualified class name CONSTANT_Name_andType_info { u1 tag; u2 name_index; u2 descriptor_index; } Utf8Info name of field Utf8Info field descriptor

30 Instructions and the “Constant Pool” 180indexbyte1indexbyte2 Format: Fieldref Class Utf8Info fully qualified class name Name_and_Type Utf8Info name of field Utf8Info field descriptor That previous picture is rather complicated, let’s simplify it a little:

31 Instructions and the “Constant Pool” Luckily, we have a Java assembler that allows us to write a kind of textual assembly code and that is then transformed into a binary.class file. This assembler takes care of creating the constant pool entries for us. When an instruction operand expects a constant pool entry the assembler allows you to enter the entry “in place” in an easy syntax. Example: getfield mypackage/Queue i I The constant entries format is part of the Java class file format.

32 Instructions and the “Constant Pool” Fully qualified class names and descriptors in constant pool UTF8 entries. 1) Fully qualified class names: a package + class name string. Note this uses “/” instead of “.” 2) Descriptors: are strings that define a type for a method or field. Javadescriptor booleanZ integerI ObjectLjava/lang/Object; String[][Ljava/lang/String; int foo(int,Object)(ILjava/lang/Object;)I

33 Linking In general: linking is the process of resolving symbolic references in binary files. Most programming language implementations have what we call “separate compilation”. Modules or files can be compiled separately and transformed into some binary format. But since these separately compiled files may have connections to other files, they have to be linked. => The binary file is not yet executable, it has some kind of “symbolic links” in it that point to things (methods, classes, procedures, variables, etc.) in other files/modules. Linking is the process of resolving these symbolic links and replacing them by real addresses so that the code can be executed.

34 Loading and Linking in JVM In JVM, loading and linking of class files happens at runtime, while the program is running! Classes are loaded as needed. The constant pool contains symbolic references that need to be resolved before a JVM instruction that uses them can be executed (this is the equivalent of linking). In JVM a constant pool entry is resolved the first time it is used by a JVM instruction. Example: When a getfield is executed for the first time, the constant pool entry index in the instruction may be replaced by the offset of the field.

35 Closing Example class Factorial { int fac(int n) { int result = 1; for (int i=2; i<n; i++) { result = result * i; } return result; } As a closing example on the JVM, we will take a look at the compiled code of the following simple Java class declaration.

36 Compiling and Disassembling % javac Factorial.java % javap -c -verbose Factorial Compiled from Factorial.java public class Factorial extends java.lang.Object { public Factorial(); /* Stack=1, Locals=1, Args_size=1 */ public int fac(int); /* Stack=2, Locals=4, Args_size=2 */ } Method Factorial() 0 aload_0 1 invokespecial #1 4 return % javac Factorial.java % javap -c -verbose Factorial Compiled from Factorial.java public class Factorial extends java.lang.Object { public Factorial(); /* Stack=1, Locals=1, Args_size=1 */ public int fac(int); /* Stack=2, Locals=4, Args_size=2 */ } Method Factorial() 0 aload_0 1 invokespecial #1 4 return

37 Compiling and Disassembling... // address: Method int fac(int) // stack: this n result i 0 iconst_1 // stack: this n result i 1 1 istore_2 // stack: this n result i 2 iconst_2 // stack: this n result i 2 3 istore_3 // stack: this n result i 4 goto 14 7 iload_2 // stack: this n result i result 8 iload_3 // stack: this n result i result i 9 imul // stack: this n result i result i 10 istore_2 11 iinc iload_3 // stack: this n result i i 15 iload_1 // stack: this n result i i n 16 if_icmple 7 // stack: this n result i 19 iload_2 // stack: this n result i result 20 ireturn

38 JASMIN JASMIN is an assembler for the JVM –Takes an ASCII description of a Java classes –Input written in a simple assembler like syntax Using the JVM instruction set –Outputs binary class file –Suitable for loading by the JVM Running JASMIN –jasmin myfile.j Produces a.class file with the name specified by the.class directive in myfile.j

39 Writing Factorial in “jasmin”.class package Factorial.super java/lang/Object.method package ()V.limit stack 50.limit locals 1 aload_0 invokenonvirtual java/lang/Object/ ()V return.end method.class package Factorial.super java/lang/Object.method package ()V.limit stack 50.limit locals 1 aload_0 invokenonvirtual java/lang/Object/ ()V return.end method

40 Writing Factorial in “jasmin”.method package fac(I)I.limit stack 50.limit locals 4 iconst_1 istore 2 iconst_2 istore 3 Label_1: iload 3 iload 1 if_icmplt Label_4 iconst_0 goto Label_5 Label_4: iconst_1 Label_5: ifeq Label_2 iload 2 iload 3 imul dup istore 2 pop Label_3: iload 3 dup iconst_1 iadd istore 3 pop goto Label_1 Label_2: iload 2 ireturn iconst_0 ireturn.end method

41 Another example: out.j.class public out.super java/lang/Object.method public ()V aload_0 invokespecial java/lang/Object/ ()V return.end method.method public static main([Ljava/lang/String;)V.limit stack 2 getstatic java/lang/System/out Ljava/io/PrintStream; ldc “ Hello World ” invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V return.end method

42 The result: out.class

43 Jasmin file format Directives –.catch. Class.end.field.implements.interface.limit.line –.method.source.super.throws.var Instructions –JVM instructions: ldc, iinc bipush Labels –Any name followed by : - e.g. Foo: –Cannot start with = :. * –Labels can only be used within method definitions

44 Not just one JVM, but a whole family JVM (J2EE & J2SE) –SUN Classis, SUN HotSpots, IBM, BEA, … CVM, KVM (J2ME) –Small devices. –Reduces some VM features to fit resource-constrained devices. JCVM (Java Card) –Smart cards. –It has least VM features. And there are also lots of other JVMs

45 Java Platform & VM & Devices