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

Similar documents
Roadmap. Java: Assembly language: OS: Machine code: Computer system:

The Hardware/Software Interface CSE351 Spring 2015

Data III & Integers I

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

CSE351: Memory, Data, & Addressing I

Virtual Memory I. CSE 351 Spring Instructor: Ruth Anderson

L14: Structs and Alignment. Structs and Alignment. CSE 351 Spring Instructor: Ruth Anderson

Assembly Programming IV

Data III & Integers I

Data III & Integers I

The Stack & Procedures

Memory, Data, & Addressing I

Java and C. CSE 351 Autumn 2018

Structs and Alignment CSE 351 Spring

Structs & Alignment. CSE 351 Autumn Instructor: Justin Hsia

We made it! Java: Assembly language: OS: Machine code: Computer system:

Java and C CSE 351 Spring

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Run-time Program Management. Hwansoo Han

x86-64 Programming III & The Stack

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

02 B The Java Virtual Machine

Compiling Techniques

Building an Executable

x86 Programming I CSE 351 Winter

Java: framework overview and in-the-small features

Arrays. CSE 351 Autumn Instructor: Justin Hsia

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

x86 Programming I CSE 351 Autumn 2016 Instructor: Justin Hsia

University*of*Washington*

University of Washington

Hardware: Logical View

Structs and Alignment

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

Executables & Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Arrays. CSE 351 Autumn Instructor: Justin Hsia

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

Assembly Programming IV

Structs and Alignment

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

L08: Assembly Programming II. Assembly Programming II. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

Cache Example, System Control Flow

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

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

Code Generation Introduction

Caches II. CSE 351 Spring Instructor: Ruth Anderson

Practical Malware Analysis

The Hardware/Software Interface CSE351 Spring 2015

Virtual Memory I. CSE 351 Winter Instructor: Mark Wyse

x86-64 Programming III

Floating Point II, x86 64 Intro

L09: Assembly Programming III. Assembly Programming III. CSE 351 Spring Guest Lecturer: Justin Hsia. Instructor: Ruth Anderson

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

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

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

How Software Executes

The Stack & Procedures

Memory Allocation III

EXAMINATIONS 2014 TRIMESTER 1 SWEN 430. Compiler Engineering. This examination will be marked out of 180 marks.

The Hardware/Software Interface CSE351 Spring 2015

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

High-Level Language VMs

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

Computer Systems CSE 410 Autumn Memory Organiza:on and Caches

x86-64 Programming I CSE 351 Summer 2018 Instructor: Justin Hsia Teaching Assistants: Josie Lee Natalie Andreeva Teagan Horkan

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

Plan for Today. Safe Programming Languages. What is a secure programming language?

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

COMP 520 Fall 2009 Virtual machines (1) Virtual machines

x86-64 Assembly CSE 351 Winter 2018 Instructor: Mark Wyse

The Hardware/Software Interface CSE351 Spring 2013 (spring has sprung!)

Joeq Analysis Framework. CS 243, Winter

Just In Time Compilation

JAM 16: The Instruction Set & Sample Programs

CSE351 Spring 2018, Midterm Exam April 27, 2018

Caches III. CSE 351 Spring Instructor: Ruth Anderson

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Assembly Programming I

CSc 453 Interpreters & Interpretation

Roadmap. The Interface CSE351 Winter Frac3onal Binary Numbers. Today s Topics. Floa3ng- Point Numbers. What is ?

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

CS577 Modern Language Processors. Spring 2018 Lecture Interpreters

Floating Point II, x86 64 Intro

University*of*Washington*

C and Programming Basics

Compiler Design Spring 2018

Advanced Programming Introduction

Static Program Analysis

Problem: Too Many Platforms!

The Hardware/Software Interface CSE 351 Autumn 2015

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

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

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

Announcements* Hardware:*Logical*View* Hardware:*SemiVLogical*View* Hardware:*Physical*View*

Chapter 5. A Closer Look at Instruction Set Architectures

Transcription:

Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis

Administrivia Lab 5 Due TONIGHT! Fri 6/2 Course evaluations now open Please fill out evals for lecture AND separate eval for section! Final Exam: Wed, June 7, 2017 2:30-4:20pm in our regular lecture room 2

Implementing Programming Languages Many choices in how to implement programming models We ve talked about compilation, can also interpret Interpreting languages has a long history Lisp, an early programming language, was interpreted Interpreters are still in common use: Python, Javascript, Ruby, Matlab, PHP, Perl, Your source code Interpreter implementation Your source code Binary executable Hardware Interpreter binary Hardware 3

An Interpreter is a Program Execute the source code directly (or something close) Simpler/no compiler less translation More transparent to debug less translation Easier to run on different architectures runs in a simulated environment that exists only inside the interpreter process Just port the interpreter Slower and harder to optimize Interpreter implementation Your source code Interpreter binary 4

Interpreter vs. Compiler An aspect of a language implementation A language can have multiple implementations Some might be compilers and other interpreters Compiled languages vs. Interpreted languages a misuse of terminology But very common to hear this And has some validation in the real world (e.g. JavaScript vs. C) Also, as about to see, modern language implementations are often a mix of the two Compiling to a bytecode language, then interpreting Doing just-in-time compilation of parts to assembly for performance 5

The JVM Note: The JVM is different than the CSE VM running on VMWare. Yet another use of the word virtual! Java programs are usually run by a Java virtual machine (JVM) JVMs interpret an intermediate language called Java bytecode Many JVMs compile bytecode to native machine code Just-in-time (JIT) compilation http://en.wikipedia.org/wiki/just-in-time_compilation Java is sometimes compiled ahead of time (AOT) like C 6

Compiling and Running Java The Java compiler converts Java into Java bytecodes Stored in a.class file Save your Java code in a.java file To run the Java compiler: javac Foo.java To execute the program stored in the bytecodes, Java bytecodes can be interpreted by a program (an interpreter) For Java, the JVM is the interpreter java Foo runs the Java virtual machine Loads the contents of Foo.class and interprets the bytecodes 7

Virtual Machine Model Bytecode compiler (e.g. javac Foo.java) High-Level Language Program (e.g. Java, C) Ahead-of-time compiler compile time run time Virtual machine (interpreter) (e.g. java Foo) Virtual Machine Language (e.g. Java bytecodes) JIT compiler Native Machine Language (e.g. x86, ARM, MIPS) 8

Java Bytecode Like assembly code for JVM, but works on all JVMs Hardware-independent! Typed (unlike x86 assembly) Strong JVM protections Holds pointer this Other arguments to method 0 1 2 3 4 n variable table Other local variables operand stack constant pool 9

JVM Operand Stack Holds pointer this Other arguments to method Other local variables i = integer, a = reference, b for byte, c for char, d for double,... JVM: 0 1 2 3 4 n variable table operand stack constant pool Bytecode: iload 1 // push 1 st argument from table onto stack iload 2 // push 2 nd argument from table onto stack iadd // pop top 2 elements from stack, add together, and // push result back onto stack istore 3 // pop result and put it into third slot in table No registers or stack locations! All operations use operand stack Compiled to x86: mov 8(%ebp), %eax mov 12(%ebp), %edx add %edx, %eax mov %eax, -8(%ebp) 10

A Simple Java Method Method java.lang.string getemployeename() 0 aload 0 // "this" object is stored at 0 in the var table 1 getfield #5 <Field java.lang.string name> // getfield instruction has a 3-byte encoding // Pop an element from top of stack, retrieve its // specified instance field and push it onto stack // "name" field is the fifth field of the object 4 areturn // Returns object at top of stack Byte number: 0 1 4 aload_0 getfield 00 05 areturn As stored in the.class file: 2A B4 00 05 B0 http://en.wikipedia.org/wiki/java_bytecode_instruction_listings 11

Class File Format Every class in Java source code is compiled to its own class file 10 sections in the Java class file structure: Magic number: 0xCAFEBABE (legible hex from James Gosling Java s inventor) Version of class file format: The minor and major versions of the class file Constant pool: Set of constant values for the class Access flags: For example whether the class is abstract, static, final, etc. This class: The name of the current class Super class: The name of the super class Interfaces: Any interfaces in the class Fields: Any fields in the class Methods: Any methods in the class Attributes: Any attributes of the class (for example, name of source file, etc.) A.jar file collects together all of the class files needed for the program, plus any additional resources (e.g. images) 12

Disassembled Java Bytecode > javac Employee.java > javap -c Employee Compiled from Employee.java class Employee extends java.lang.object { public Employee(java.lang.String,int); public java.lang.string getemployeename(); public int getemployeenumber(); } Method Employee(java.lang.String,int) 0 aload_0 1 invokespecial #3 <Method java.lang.object()> 4 aload_0 5 aload_1 6 putfield #5 <Field java.lang.string name> 9 aload_0 10 iload_2 11 putfield #4 <Field int idnumber> 14 aload_0 15 aload_1 16 iload_2 17 invokespecial #6 <Method void storedata(java.lang.string, int)> 20 return http://en.wikipedia.org/wiki/java _bytecode_instruction_listings Method java.lang.string getemployeename() 0 aload_0 1 getfield #5 <Field java.lang.string name> 4 areturn Method int getemployeenumber() 0 aload_0 1 getfield #4 <Field int idnumber> 4 ireturn Method void storedata(java.lang.string, int) 13

Other languages for JVMs JVMs run on so many computers that compilers have been built to translate many other languages to Java bytecode: AspectJ, an aspect-oriented extension of Java ColdFusion, a scripting language compiled to Java Clojure, a functional Lisp dialect Groovy, a scripting language JavaFX Script, a scripting language for web apps JRuby, an implementation of Ruby Jython, an implementation of Python Rhino, an implementation of JavaScript Scala, an object-oriented and functional programming language And many others, even including C! 14

Microsoft s C# and.net Framework C# has similar motivations as Java Virtual machine is called the Common Language Runtime Common Intermediate Language is the bytecode for C# and other languages in the.net framework 15

We made it! C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp %rbp 0111010000011000 100011010000010000000010 1000100111000010 110000011111101000011111 Java: Car c = new Car(); c.setmiles(100); c.setgals(17); float mpg = c.getmpg(); OS: Memory & data Integers & floats x86 assembly Procedures & stacks Executables Arrays & structs Memory & caches Processes Virtual memory Memory allocation Java vs. C 16