David Evans http://www.cs.virginia.edu/evans Lecture 19: 0xCAFEBABE (Java Byte Codes) CS201j: Engineering Software University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans
Menu Running Programs Java Virtual Machine Byte Codes Crash Course in Architecture (CS333) Crash Course in Compilers (CS571) Java Virtual Machine Byte Codes 12 November 2002 CS 201J Fall 2002
Computer Architecture Processor does computation Memory stores bits Input Devices (mouse, keyboard) get input from user Output Devices (display, speakers) present output to user 12 November 2002 CS 201J Fall 2002
Central Processing Unit (CPU) 12 November 2002 CS 201J Fall 2002
Intel 4004 First general purpose microprocessor, 1971 4-bit data 46 instructions 8-bit instructions! 12 November 2002 CS 201J Fall 2002
From http://www.cyberiapc.com/hardwarebeg.htm PC Motherboard Memory CPU From http://www.cyberiapc.com/hardwarebeg.htm 12 November 2002 CS 201J Fall 2002
Inside the CPU Registers Loads and decodes instructions from memory ALU: Arithmetic Logic Unit Does arithmetic Can only operate on values in registers Must load values from memory into registers before computing with them 12 November 2002 CS 201J Fall 2002
Compiler Translates a program in a high-level language into machine instructions Calling convention How are parameters passed to functions How is the stack managed to return Register allocation Figure out how to use registers efficiently 12 November 2002 CS 201J Fall 2002
00401026 rep stos dword ptr [edi] 7: if (a > b) { 6: int max (int a, int b) { 00401010 push ebp 00401011 mov ebp,esp 00401013 sub esp,40h 00401016 push ebx 00401017 push esi 00401018 push edi 00401019 lea edi,[ebp-40h] 0040101C mov ecx,10h 00401021 mov eax,0CCCCCCCCh 00401026 rep stos dword ptr [edi] 7: if (a > b) { 00401028 mov eax,dword ptr [ebp+8] 0040102B cmp eax,dword ptr [ebp+0Ch] 0040102E jle max+25h (00401035) 8: return b; 00401030 mov eax,dword ptr [ebp+0Ch] 00401033 jmp max+28h (00401038) 9: } else { 10: return a; 00401035 mov eax,dword ptr [ebp+8] 00401038 pop edi 00401039 pop esi 0040103A pop ebx 0040103B mov esp,ebp 0040103D pop ebp 0040103E ret push instruction is 1 byte mov instruction is 2 bytes Dealing with function call: updating stack, moving arguments In Visual C++, see assembly code by running Debug, then Window | Disassembly int max (int a, int b) { if (a > b) { return b; } else { return a; } 12 November 2002 CS 201J Fall 2002
Java Virtual Machine 12 November 2002 CS 201J Fall 2002
Java Ring (1998) 12 November 2002 CS 201J Fall 2002
Java Card 12 November 2002 CS 201J Fall 2002
Java Virtual Machine Small and simple to implement All VMs will run all programs the same way Secure 12 November 2002 CS 201J Fall 2002
Java Byte Codes Stack-based virtual machine Small instruction set: 202 instructions Intel x86: ~280 instructions (1 to 17 bytes long!) Memory is typed Every Java class file begins with magic number 3405691582 = 0xCAFEBABE in base 16 12 November 2002 CS 201J Fall 2002
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 12 November 2002 CS 201J Fall 2002
Some Java 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 … 12 November 2002 CS 201J Fall 2002
Some Java Instructions Opcode Mnemonic Description 18 ldc <value> Push a one-word constant onto the stack Constant may be an int, float or String ldc “Hello” ldc 201 The String is really a reference to an entry in the string constant table! 12 November 2002 CS 201J Fall 2002
Arithmetic Opcode Mnemonic Description 96 iadd Pops two integers from the stack and pushes their sum iconst_2 iconst_3 iadd 12 November 2002 CS 201J Fall 2002
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 12 November 2002 CS 201J Fall 2002
Java Byte Code Instructions 0: nop 1-20: putting constants on the stack 96-119: arithmetic on ints, longs, floats, doubles What other kinds of instructions do we need? 12 November 2002 CS 201J Fall 2002
Other Instruction Classes Control Flow (~20 instructions) if, goto, return Method Calls (4 instructions) Loading and Storing Variables (65 instructions) Creating objects (1 instruction) Using object fields (4 instructions) Arrays (3 instructions) 12 November 2002 CS 201J Fall 2002
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. 12 November 2002 CS 201J Fall 2002
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)> 12 November 2002 CS 201J Fall 2002
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. 12 November 2002 CS 201J Fall 2002
Example public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); } 12 November 2002 CS 201J Fall 2002
> 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 12 November 2002 CS 201J Fall 2002
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> 12 November 2002 CS 201J Fall 2002
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); } } 12 November 2002 CS 201J Fall 2002
static public void main (String args[]) { public class Locals { static public void main (String args[]) { int a1, a2, a3, a4, a5, a6, a7, a8, a9; int a10, a11, a12, a13, a14, a15, a16, a17, a18, a19; int a20, a21, a22, a23, a24, a25, a26, a27, a28, a29; int a30, a31, a32, a33, a34, a35, a36, a37, a38, a39; int a40, a41, a42, a43, a44, a45, a46, a47, a48, a49; int a50, a51, a52, a53, a54, a55, a56, a57, a58, a59; int a60, a61, a62, a63, a64, a65, a66, a67, a68, a69; int a70, a71, a72, a73, a74, a75, a76, a77, a78, a79; int a80, a81, a82, a83, a84, a85, a86, a87, a88, a89; int a90, a91, a92, a93, a94, a95, a96, a97, a98, a99; int a100, a101, a102, a103, a104, a105, a106, a107, a108, a109; int a110, a111, a112, a113, a114, a115, a116, a117, a118, a119; int a120, a121, a122, a123, a124, a125, a126, a127, a128, a129; int a130, a131, a132, a133, a134, a135, a136, a137, a138, a139; int a140, a141, a142, a143, a144, a145, a146, a147, a148, a149; int a150, a151, a152, a153, a154, a155, a156, a157, a158, a159; int a160, a161, a162, a163, a164, a165, a166, a167, a168, a169; int a170, a171, a172, a173, a174, a175, a176, a177, a178, a179; int a180, a181, a182, a183, a184, a185, a186, a187, a188, a189; int a190, a191, a192, a193, a194, a195, a196, a197, a198, a199; int a200, a201, a202, a203, a204, a205, a206, a207, a208, a209; int a210, a211, a212, a213, a214, a215, a216, a217, a218, a219; int a220, a221, a222, a223, a224, a225, a226, a227, a228, a229; int a230, a231, a232, a233, a234, a235, a236, a237, a238, a239; int a240, a241, a242, a243, a244, a245, a246, a247, a248, a249; int a250, a251, a252, a253, a254, a255, a256; a255 = 11; a256 = 12; } bipush <n> Push a one-byte signed integer Why not just use lcd? Method void main(java.lang.String[]) 0 bipush 11 2 istore 255 4 bipush 12 6 istore_w 256 10 return 12 November 2002 CS 201J Fall 2002
Max Why is the compiled C code so much longer and more complicated? 6: int max (int a, int b) { 00401010 push ebp 00401011 mov ebp,esp 00401013 sub esp,40h 00401016 push ebx 00401017 push esi 00401018 push edi 00401019 lea edi,[ebp-40h] 0040101C mov ecx,10h 00401021 mov eax,0CCCCCCCCh 00401026 rep stos dword ptr [edi] 7: if (a > b) { 00401028 mov eax,dword ptr [ebp+8] 0040102B cmp eax,dword ptr [ebp+0Ch] 0040102E jle max+25h (00401035) 8: return b; 00401030 mov eax,dword ptr [ebp+0Ch] 00401033 jmp max+28h (00401038) 9: } else { 10: return a; 00401035 mov eax,dword ptr [ebp+8] 00401038 pop edi 00401039 pop esi 0040103A pop ebx 0040103B mov esp,ebp 0040103D pop ebp 0040103E ret Max Why is the compiled C code so much longer and more complicated? public class IfExample { public int max (int a, int b) { if (a > b) { return a; } else { return b; } Method int max(int, int) 0 iload_1 1 iload_2 2 if_icmple 7 5 iload_1 6 ireturn 7 iload_2 8 ireturn 12 November 2002 CS 201J Fall 2002
Creating Objects new <class> Pushes an object reference to new object of type <class> on the stack. (Note: doesn’t call a constructor!) 12 November 2002 CS 201J Fall 2002
Duplicates the reference, not the object! Creating Objects public class NewExample { public StringBuffer makeBigBuffer () { StringBuffer res; res = new StringBuffer ("Hello!"); return res; } Method java.lang.StringBuffer makeBigBuffer() 0 new #2 <Class java.lang.StringBuffer> 3 dup 4 ldc #3 <String "Hello!"> 6 invokespecial #4 <Method java.lang.StringBuffer(java.lang.String)> 9 astore_1 10 aload_1 11 areturn Duplicates the reference, not the object! 12 November 2002 CS 201J Fall 2002
Using Object Fields getfield <field-spec> Pops an object reference from the stack, and pushes the value of the field on the stack putfield <field-spec> Pops a value and an object reference from the stack, and stores the value in the in the object’s field 12 November 2002 CS 201J Fall 2002
private Genome genome; public Species (String n, Genome g) { name = n; public class Species { private String name; private Genome genome; public Species (String n, Genome g) { name = n; genome = g; } 0 aload_0 1 invokespecial #1 <Method java.lang.Object()> 4 aload_0 5 aload_1 6 putfield #2 <Field java.lang.String name> 9 aload_0 10 aload_2 11 putfield #3 <Field Genome genome> 14 return 12 November 2002 CS 201J Fall 2002
Charge Use javap –c <classname> to look at what the javac compiler produces for your code How do we know bad things won’t happen? If you want a review next class, send me questions by 3pm tomorrow! Method void main(java.lang.String[]) 0 iconst_2 1 iadd 2 return 12 November 2002 CS 201J Fall 2002