CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)

Slides:



Advertisements
Similar presentations
What is a Computer Program? For a computer to be able to do anything (multiply, play a song, run a word processor), it must be given the instructions.
Advertisements

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.
Compilation 2007 Code Generation Michael I. Schwartzbach BRICS, University of Aarhus.
JAVA Programming Environment © Juhani Välimäki 2003.
Introduction To Java Objectives For Today â Introduction To Java â The Java Platform & The (JVM) Java Virtual Machine â Core Java (API) Application Programming.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
Lab 9 Java Bytecode & The Jasmin Assembler
Java: History and Introduction (Lecture # 1). History… Java – Based on C and C++ – Developed in 1991 for intelligent consumer electronic devices – Green.
JDK, Jan Pettersen Nytun - HiA 1 The Java Programming Environment: The programmer write source code with a text editor. The programmer write source code.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
1 Introduction to Java and Applet Lecture 3 from Chapters 1 and 2 of the complete reference.
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.
Lecture 2: Do you speak Java?. From Problem to Program Last Lecture we looked at modeling with objects! Steps to solving a business problem –Investigate.
1 Software Testing and Quality Assurance Lecture 31 – SWE 205 Course Objective: Basics of Programming Languages & Software Construction Techniques.
Introduction to the JDK Java for Computational Finance
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Introduction to Java Programming. Contents 1. Java, etc. 2. Java's Advantages 3. Java's Disadvantages 4. Types of Java Code 5. Java Bytecodes 6. Steps.
Computer Programming 12 Mr. Jean March 3 rd, 2014.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Introduction to Java Tonga Institute of Higher Education.
Parts of a Computer Why Use Binary Numbers? Source Code - Assembly - Machine Code.
Application Security Tom Chothia Computer Security, Lecture 14.
Arpit Jain Mtech1. Outline Introduction Dalvik VM Java VM Examples Comparisons Experimental Evaluation.
CSCI 273: Processing An Introduction. Programming Languages –An abstract "human understandable" language for telling the computer what to do –The abstract.
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
Java Virtual Machine Java Virtual Machine A Java Virtual Machine (JVM) is a set of computer software programs and data structures that use.
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.
FRST JAVA PROGRAM. Getting Started with Java Programming A Simple Java Application Compiling Programs Executing Applications.
1 Introduction to JVM Based on material produced by Bill Venners.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
© 2012 Pearson Education, Inc. All rights reserved. 1-1 Why Java? Needed program portability – Program written in a language that would run on various.
POS 406 Java Technology And Beginning Java Code
Java Programming, Second Edition Chapter One Creating Your First Java Program.
CSCI Processing CSCI Introduction to Algorithm Design An Introduction.
Lecture 8 February 29, Topics Questions about Exercise 4, due Thursday? Object Based Programming (Chapter 8) –Basic Principles –Methods –Fields.
CS Software Studio Java Lab 1 Meng-Ting Wang PLLAB, Computer Science Department, National Tsing-Hua University.
Getting started with Programming using IDE. JAVA JAVA IS A PROGRAMMING LANGUAGE AND A PLATFORM. IT CAN BE USED TO DELIVER AND RUN HIGHLY INTERACTIVE DYNAMIC.
Lecture 1. Introduction to Programming and Java MIT- AITI 2003.
A compiler is a computer program that translate written code (source code) into another computer language Associated with high level languages A well.
Overview of Java CSCI 392 Day One. Running C code vs Java code C Source Code C Compiler Object File (machine code) Library Files Linker Executable File.
By: Cheryl Mok & Sarah Tan. Java is partially interpreted. 1. Programmer writes a program in textual form 2. Runs the compiler, which converts the textual.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
© 2012 Pearson Education, Inc. All rights reserved types of Java programs Application – Stand-alone program (run without a web browser) – Relaxed.
Getting Started With Java September 22, Java Bytecode  Bytecode : is a highly optimized set of instructions designed to be executed by the Java.
CSCI 212 Object-Oriented Programming in Java. Prerequisite: CSCI 111 variable assignment statement while loop for loop post-increment (i++) strong typing.
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.
3/5/2002e-business and Information Systems1 Java Java Java Virtual Machine (JVM) Java Application Program Interface (API) HW Kernel API Application Programs.
Know Your Java. Java is special Java source code Byte code/ native code Object code on windows Object code on Dos Object code on Lynux.
ITP 109 Week 2 Trina Gregory Introduction to Java.
RealTimeSystems Lab Jong-Koo, Lim
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
CS216: Program and Data Representation
Internet and Java Foundations, Programming and Practice
Java programming lecture one
Introduction to Algorithm Design
Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software
Introduction CSC 111.
Byte Code Verification
How Java Program Executes
Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson
Computer Programming-1 CSC 111
Programming language translators
Presentation transcript:

CSC 8505 Compiler Construction IR Example: Java Bytecode (looking inside class files)

What are these.class files? You’ve created.java source code files. You’ve compiled.java source code into.class files. You’ve run your.class files. But what’s are those.class files?

Typical Software Production Source code is ported to different platform- specific sources. Each port is compiled with a platform- specific compiler. Each compiler produces platform-specific machine code (or binaries). Binaries execute on a single platform.

Write Once, Run Anywhere Java source code is is compiled into machine-independent bytecode class files. javac command compiles.java source code into.class bytecode. Bytecode is interpreted by machine- specific Java Virtual Machines (JVMs). Bytecode consists of simple, step-by-step instructions for the JVM.

Java Virtual Machines JVM is a computer simulated in a computer. JVMs are built into most web browsers. java command gives.class file to JVM. JVM interprets the bytecode into instructions that a specific platform can understand. Different JVMs for different platforms: Windows, Mac OS X, Linux, cell phones.

Using javap javap is a program that can give you information on.class files. If you get a class file without documentation, javap can tell you what methods you can call. javap can show you how javac compiles your program into simple instructions. Good for high-performance optimizations or if you don’t have access to an Application Programming Interface (API).

javap Output Given “Mystery.class”: > javap Mystery Compiled from "Mystery.java" class Mystery extends java.lang.Object{ Mystery(); public static int unknown(int,int); } One static method called “unknown” that takes two integers and returns an integer.

Using the “-c” flag will disassemble the bytecode: public static int unknown(int,int); Code: 0: iload_0 1: iload_1 2: iadd 3: ireturn } javap -c Output Loads the first integer argument. Loads the second integer argument. Adds the two loaded integer values. Returns the integer sum. This method just adds two numbers together and returns the sum.

Example: String Appends Disassemble the following method using javap -c : public String append(String a, String b) { return a+b; } The output is surprisingly complicated…

public static java.lang.String stringAdd(java.lang.String,java.lang.String,int); Code: 0: new #2; //class StringBuffer 3: dup 4: invokespecial #3; //Method java/lang/StringBuffer." ":()V 7: aload_0 8: invokevirtual #4; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/ lang/StringBuffer; 11: aload_1 12: invokevirtual #4; //Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/ lang/StringBuffer; 15: invokevirtual #5; //Method java/lang/StringBuffer.toString:()Ljava/lang/String; 18: areturn }

Example: String Appends All that bytecode is equivalent to: StringBuffer temp; temp = new StringBuffer(); temp.append(a); temp.append(b); return temp.toString(); One line of code initializes an object and makes three method calls.

String Append Optimization What if we had: String a =“”; String b = “hello”; for (int i=0; i<n; i++) a += b; This code performs n initializations and 3n method calls. Using javap showed us there’s a class called StringBuffer that has an append() function.

String Append Optimization A more efficient way: StringBuffer aBuff = new StringBuffer(); String b = “hello”; for (int i=0; i<n; i++) a.append(b); String a = aBuff.toString(); Only performs two initializations and n+1 method calls -- three times faster.

Questions What advantages are there in running programs on simulated computers, like JVMs? What disadvantages might there be? Could you compile Java directly into native machine code like C or C++, instead of using a JVM? Is it possible to generate Java source code from class files, i.e. decompile?