Lecture 19 0 x CAFEBABE Java Byte Codes
Lecture 19: 0 x. CAFEBABE (Java Byte Codes) CS 201 j: Engineering Software University of Virginia 12 November 2002 Computer Science CS 201 J Fall 2002 David Evans http: //www. cs. virginia. edu/evans
Menu • Running Programs – Crash Course in Architecture (CS 333) – Crash Course in Compilers (CS 571) • Java Virtual Machine • Byte Codes 12 November 2002 CS 201 J Fall 2002 2
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 201 J Fall 2002 3
Central Processing Unit (CPU) 12 November 2002 CS 201 J Fall 2002 4
Intel 4004 • First general purpose microprocessor, 1971 • 4 -bit data • 46 instructions – 8 -bit instructions! 12 November 2002 CS 201 J Fall 2002 5
PC Motherboard Memory CPU From http: //www. cyberiapc. com/hardwarebeg. htm 12 November 2002 CS 201 J Fall 2002 6
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 201 J Fall 2002 7
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 201 J Fall 2002 8
6: int max (int a, int b) { push ebp push instruction is 1 byte 00401010 00401011 mov ebp, esp mov instruction is 2 bytes 00401013 Dealing with sub esp, 40 h function call: 00401016 push ebx updating 00401017 push esi stack, 00401018 push edi moving 00401019 lea edi, [ebp-40 h] In Visual C++, see arguments 0040101 C mov ecx, 10 h assembly 00401021 mov eax, 0 CCCCh code by running Debug, 00401026 rep stos dword ptr [edi] then 7: if (a > b) { Window | Disassembly 00401028 mov eax, dword ptr [ebp+8] 0040102 B cmp eax, dword ptr [ebp+0 Ch] int max (int a, int b) { 0040102 E jle max+25 h (00401035) if (a > b) { 8: return b; 00401030 mov eax, dword ptr [ebp+0 Ch] } else { 00401033 jmp max+28 h (00401038) return a; 9: } else { } 10: return a; } 12 November 2002 00401035 00401038 00401039 0040103 A 0040103 B 0040103 D 0040103 E mov pop pop mov pop ret eax, dword ptr [ebp+8] edi esi ebx esp, ebp CS 201 J Fall 2002 9
Java Virtual Machine 12 November 2002 CS 201 J Fall 2002 10
Java Ring (1998) 12 November 2002 CS 201 J Fall 2002 11
Java Card 12 November 2002 CS 201 J Fall 2002 12
Java Virtual Machine • Small and simple to implement • All VMs will run all programs the same way • Secure 12 November 2002 CS 201 J Fall 2002 13
Java Byte Codes • Stack-based virtual machine • Small instruction set: 202 instructions – Intel x 86: ~280 instructions (1 to 17 bytes long!) • Memory is typed • Every Java class file begins with magic number 3405691582 = 0 x. CAFEBABE in base 16 12 November 2002 CS 201 J Fall 2002 14
Stack-Based Computation • push – put something on the top of the stack • pop – get and remove the top of the stack Stack push 2 push 3 add 2 5 3 Does 2 pops, pushes sum 12 November 2002 CS 201 J Fall 2002 15
Some Java Instructions Opcode Mnemonic Description 0 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 201 J Fall 2002 16
Some Java Instructions Opcode 18 Mnemonic ldc <value> Description 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 201 J Fall 2002 17
Arithmetic Opcode 96 Mnemonic iadd Description Pops two integers from the stack and pushes their sum iconst_2 iconst_3 iadd 12 November 2002 CS 201 J Fall 2002 18
Arithmetic Opcode Mnemonic Description 96 iadd 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 201 J Fall 2002 19
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 201 J Fall 2002 20
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 201 J Fall 2002 21
Control Flow • ifeq <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 201 J Fall 2002 22
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 201 J Fall 2002 23
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 201 J Fall 2002 24
Example public class Sample 1 { static public void main (String args[]) { System. err. println ("Hello!"); System. exit (1); } } 12 November 2002 CS 201 J Fall 2002 25
> javap -c Sample 1 public class Sample 1 { static public void main (String args[]) { System. err. println ("Hello!"); System. exit (1); } } Compiled from Sample 1. java public class Sample 1 extends java. lang. Object { public Sample 1(); public static void main(java. lang. String[]); } Method Sample 1() 0 aload_0 1 invokespecial #1 <Method java. lang. Object()> 4 return Method void main(java. lang. String[]) 0 getstatic #2 <Field java. io. Print. Stream 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 201 J Fall 2002 26
Referencing Memory • iload <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 201 J Fall 2002 27
Referencing Example Method void main(java. lang. String[]) 0 iconst_2 public class Locals 1 { 1 istore_1 static public void main (String args[]) { 2 iconst_3 int a = 2; 3 istore_2 int b = 3; 4 iload_1 int c = a + b; 5 iload_2 6 iadd System. err. println ("c: " + c); } } 7 istore_3 8 getstatic #2 <Field java. io. Print. Stream err> 11 new #3 <Class java. lang. String. Buffer> 14 dup 15 invokespecial #4 <Method java. lang. String. Buffer()> 18 ldc #5 <String "c: "> 20 invokevirtual #6 <Method java. lang. String. Buffer append(java. 23 iload_3 24 invokevirtual #7 <Method java. lang. String. Buffer append(int)> 27 invokevirtual #8 <Method java. lang. String to. String()> 30 invokevirtual #9 <Method void println(java. lang. String)> 33 return 12 November 2002 CS 201 J Fall 2002 28
public class Locals { static public void main (String args[]) { int a 1, a 2, a 3, a 4, a 5, a 6, a 7, a 8, a 9; int a 10, a 11, a 12, a 13, a 14, a 15, a 16, a 17, a 18, a 19; int a 20, a 21, a 22, a 23, a 24, a 25, a 26, a 27, a 28, a 29; int a 30, a 31, a 32, a 33, a 34, a 35, a 36, a 37, a 38, a 39; int a 40, a 41, a 42, a 43, a 44, a 45, a 46, a 47, a 48, a 49; int a 50, a 51, a 52, a 53, a 54, a 55, a 56, a 57, a 58, a 59; int a 60, a 61, a 62, a 63, a 64, a 65, a 66, a 67, a 68, a 69; int a 70, a 71, a 72, a 73, a 74, a 75, a 76, a 77, a 78, a 79; int a 80, a 81, a 82, a 83, a 84, a 85, a 86, a 87, a 88, a 89; int a 90, a 91, a 92, a 93, a 94, a 95, a 96, a 97, a 98, a 99; int a 100, a 101, a 102, a 103, a 104, a 105, a 106, a 107, a 108, a 109; int a 110, a 111, a 112, a 113, a 114, a 115, a 116, a 117, a 118, a 119; int a 120, a 121, a 122, a 123, a 124, a 125, a 126, a 127, a 128, a 129; int a 130, a 131, a 132, a 133, a 134, a 135, a 136, a 137, a 138, a 139; int a 140, a 141, a 142, a 143, a 144, a 145, a 146, a 147, a 148, a 149; int a 150, a 151, a 152, a 153, a 154, a 155, a 156, a 157, a 158, a 159; int a 160, a 161, a 162, a 163, a 164, a 165, a 166, a 167, a 168, a 169; int a 170, a 171, a 172, a 173, a 174, a 175, a 176, a 177, a 178, a 179; int a 180, a 181, a 182, a 183, a 184, a 185, a 186, a 187, a 188, a 189; int a 190, a 191, a 192, a 193, a 194, a 195, a 196, a 197, a 198, a 199; int a 200, a 201, a 202, a 203, a 204, a 205, a 206, a 207, a 208, a 209; int a 210, a 211, a 212, a 213, a 214, a 215, a 216, a 217, a 218, a 219; int a 220, a 221, a 222, a 223, a 224, a 225, a 226, a 227, a 228, a 229; int a 230, a 231, a 232, a 233, a 234, a 235, a 236, a 237, a 238, a 239; int a 240, a 241, a 242, a 243, a 244, a 245, a 246, a 247, a 248, a 249; } bipush <n> Push a one-byte signed integer Why not just use lcd? Method void main(java. lang. String[]) 0 bipush 11 int a 250, a 251, a 252, a 253, a 254, a 255, a 256; 2 istore 255 4 bipush 12 a 255 = 11; 6 istore_w 256 a 256 = 12; 10 return } 12 November 2002 CS 201 J Fall 2002 29
6: int max (int a, int b) { 00401010 push ebp 00401011 mov ebp, esp 00401013 sub esp, 40 h 00401016 push ebx 00401017 push esi 00401018 push. If. Example edi public class { 00401019 lea edi, [ebp-40 h] publicmov int max (intecx, 10 h a, int b) { 0040101 C 00401021 if (a mov> b) { eax, 0 CCCCh 00401026 rep stos return a; dword ptr [edi] 7: if (a > b) { } else { 00401028 mov eax, dword ptr [ebp+8] return b; 0040102 B cmp eax, dword ptr [ebp+0 Ch] 0040102 E } jle max+25 h (00401035) 8: return b; } 00401030 mov eax, dword ptr [ebp+0 Ch] } 00401033 jmp max+28 h (00401038) 9: } else { 10: return a; 00401035 mov eax, dword ptr [ebp+8] 00401038 pop edi 00401039 pop esi 0040103 A pop ebx 0040103 B mov esp, ebp 0040103 D pop ebp 0040103 E ret Max. Why is the compiled C code so much longer and more complicated? 12 November 2002 CS 201 J Fall 2002 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 30
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 201 J Fall 2002 31
Creating Objects public class New. Example { public String. Buffer make. Big. Buffer () { String. Buffer res; res = new String. Buffer ("Hello!"); return res; } } Method java. lang. String. Buffer make. Big. Buffer() 0 new #2 <Class java. lang. String. Buffer> Duplicates the reference, not the object! 3 dup 4 ldc #3 <String "Hello!"> 6 invokespecial #4 <Method java. lang. String. Buffer(java. lang. String)> 9 astore_1 10 aload_1 11 areturn 12 November 2002 CS 201 J Fall 2002 32
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 object’s field 12 November 2002 CS 201 J Fall 2002 33
public class Species { private String name; private Genome genome; public Species (String n, Genome g) { name = n; genome = g; } 0 1 4 5 6 9 10 11 14 aload_0 invokespecial #1 <Method java. lang. Object()> aload_0 aload_1 putfield #2 <Field java. lang. String name> aload_0 aload_2 putfield #3 <Field Genome genome> return 12 November 2002 CS 201 J Fall 2002 34
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? Method void main(java. lang. String[]) 0 iconst_2 1 iadd 2 return • If you want a review next class, send me questions by 3 pm tomorrow! 12 November 2002 CS 201 J Fall 2002 35
- Slides: 35