diff --git a/sample/AddMain.class b/sample/AddMain.class new file mode 100644 index 0000000..7251a89 Binary files /dev/null and b/sample/AddMain.class differ diff --git a/sample/AddMain.java b/sample/AddMain.java new file mode 100644 index 0000000..a52ec50 --- /dev/null +++ b/sample/AddMain.java @@ -0,0 +1,12 @@ +class AddMain { + + public static int main(String args[]) { + int a = 14; + int b = 15; + return add(a, b); + } + + public static int add(int a, int b) { + return a + b; + } +} diff --git a/sample/Hello.class b/sample/Hello.class deleted file mode 100644 index fdd0d0b..0000000 Binary files a/sample/Hello.class and /dev/null differ diff --git a/sample/Hello.java b/sample/Hello.java deleted file mode 100644 index 65f3110..0000000 --- a/sample/Hello.java +++ /dev/null @@ -1,5 +0,0 @@ -class Hello { - public void hello() { - System.out.println("Hello, World!"); - } -} diff --git a/sample/HelloMain.class b/sample/HelloMain.class deleted file mode 100644 index 4c2cef2..0000000 Binary files a/sample/HelloMain.class and /dev/null differ diff --git a/sample/HelloMain.java b/sample/HelloMain.java deleted file mode 100644 index e638afd..0000000 --- a/sample/HelloMain.java +++ /dev/null @@ -1,6 +0,0 @@ -class HelloMain { - public static void main(String[] args) { - System.out.println("Hello, World!"); - System.out.println(1 + 2); - } -} diff --git a/sample/javap_Add.txt b/sample/javap_Add.txt deleted file mode 100644 index 6c46f72..0000000 --- a/sample/javap_Add.txt +++ /dev/null @@ -1,51 +0,0 @@ -Classfile /home/levent/Projects/CVM/sample/Add.class - Last modified Jun 20, 2024; size 236 bytes - SHA-256 checksum 9ba59699c2b64d51d0d2eb8ea8c0167c8011a4aeebb0454a49b7d522eb8aa162 - Compiled from "Add.java" -public class Add - minor version: 0 - major version: 66 - flags: (0x0021) ACC_PUBLIC, ACC_SUPER - this_class: #7 // Add - super_class: #2 // java/lang/Object - interfaces: 0, fields: 0, methods: 2, attributes: 1 -Constant pool: - #1 = Methodref #2.#3 // java/lang/Object."":()V - #2 = Class #4 // java/lang/Object - #3 = NameAndType #5:#6 // "":()V - #4 = Utf8 java/lang/Object - #5 = Utf8 - #6 = Utf8 ()V - #7 = Class #8 // Add - #8 = Utf8 Add - #9 = Utf8 Code - #10 = Utf8 LineNumberTable - #11 = Utf8 add - #12 = Utf8 (II)I - #13 = Utf8 SourceFile - #14 = Utf8 Add.java -{ - public Add(); - descriptor: ()V - flags: (0x0001) ACC_PUBLIC - Code: - stack=1, locals=1, args_size=1 - 0: aload_0 - 1: invokespecial #1 // Method java/lang/Object."":()V - 4: return - LineNumberTable: - line 1: 0 - - public static int add(int, int); - descriptor: (II)I - flags: (0x0009) ACC_PUBLIC, ACC_STATIC - Code: - stack=2, locals=2, args_size=2 - 0: iload_0 - 1: iload_1 - 2: iadd - 3: ireturn - LineNumberTable: - line 3: 0 -} -SourceFile: "Add.java" diff --git a/sample/javap_AddMain.txt b/sample/javap_AddMain.txt new file mode 100644 index 0000000..0366d12 --- /dev/null +++ b/sample/javap_AddMain.txt @@ -0,0 +1,298 @@ +Classfile /home/levent/Projects/CVM/sample/AddMain.class + Last modified Jun 22, 2024; size 347 bytes + SHA-256 checksum 14811c1816314b13c819db3a96c61676607f69186573fc379f79bd6562982da8 + Compiled from "AddMain.java" +class AddMain + minor version: 0 + major version: 66 + flags: (0x0020) ACC_SUPER + this_class: #8 // AddMain + super_class: #2 // java/lang/Object + interfaces: 0, fields: 0, methods: 3, attributes: 1 +Constant pool: + #1 = Methodref #2.#3 // java/lang/Object."":()V + #2 = Class #4 // java/lang/Object + #3 = NameAndType #5:#6 // "":()V + #4 = Utf8 java/lang/Object + #5 = Utf8 + #6 = Utf8 ()V + #7 = Methodref #8.#9 // AddMain.add:(II)I + #8 = Class #10 // AddMain + #9 = NameAndType #11:#12 // add:(II)I + #10 = Utf8 AddMain + #11 = Utf8 add + #12 = Utf8 (II)I + #13 = Utf8 Code + #14 = Utf8 LineNumberTable + #15 = Utf8 main + #16 = Utf8 ([Ljava/lang/String;)V + #17 = Utf8 SourceFile + #18 = Utf8 AddMain.java +{ + AddMain(); + descriptor: ()V + flags: (0x0000) + Code: + stack=1, locals=1, args_size=1 + 0: aload_0 + 1: invokespecial #1 // Method java/lang/Object."":()V + 4: return + LineNumberTable: + line 1: 0 + + public static void main(java.lang.String[]); + descriptor: ([Ljava/lang/String;)V + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=3, args_size=1 + 0: iconst_1 + 1: istore_1 + 2: iconst_2 + 3: istore_2 + 4: iload_1 + 5: iload_2 + 6: invokestatic #7 // Method add:(II)I + 9: pop + 10: return + LineNumberTable: + line 4: 0 + line 5: 2 + line 6: 4 + line 7: 10 + + public static int add(int, int); + descriptor: (II)I + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=2, args_size=2 + 0: iload_0 + 1: iload_1 + 2: iadd + 3: ireturn + LineNumberTable: + line 10: 0 +} +SourceFile: "AddMain.java" +Classfile /home/levent/Projects/CVM/sample/AddMain.class + Last modified Jun 22, 2024; size 349 bytes + SHA-256 checksum 51968c311d074303b8bebc57dd66676d30055945651608ab6a2a303dd3204ac7 + Compiled from "AddMain.java" +class AddMain + minor version: 0 + major version: 66 + flags: (0x0020) ACC_SUPER + this_class: #8 // AddMain + super_class: #2 // java/lang/Object + interfaces: 0, fields: 0, methods: 3, attributes: 1 +Constant pool: + #1 = Methodref #2.#3 // java/lang/Object."":()V + #2 = Class #4 // java/lang/Object + #3 = NameAndType #5:#6 // "":()V + #4 = Utf8 java/lang/Object + #5 = Utf8 + #6 = Utf8 ()V + #7 = Methodref #8.#9 // AddMain.add:(II)I + #8 = Class #10 // AddMain + #9 = NameAndType #11:#12 // add:(II)I + #10 = Utf8 AddMain + #11 = Utf8 add + #12 = Utf8 (II)I + #13 = Utf8 Code + #14 = Utf8 LineNumberTable + #15 = Utf8 main + #16 = Utf8 ([Ljava/lang/String;)V + #17 = Utf8 SourceFile + #18 = Utf8 AddMain.java +{ + AddMain(); + descriptor: ()V + flags: (0x0000) + Code: + stack=1, locals=1, args_size=1 + 0: aload_0 + 1: invokespecial #1 // Method java/lang/Object."":()V + 4: return + LineNumberTable: + line 1: 0 + + public static void main(java.lang.String[]); + descriptor: ([Ljava/lang/String;)V + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=3, args_size=1 + 0: bipush 14 + 2: istore_1 + 3: bipush 25 + 5: istore_2 + 6: iload_1 + 7: iload_2 + 8: invokestatic #7 // Method add:(II)I + 11: pop + 12: return + LineNumberTable: + line 4: 0 + line 5: 3 + line 6: 6 + line 7: 12 + + public static int add(int, int); + descriptor: (II)I + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=2, args_size=2 + 0: iload_0 + 1: iload_1 + 2: iadd + 3: ireturn + LineNumberTable: + line 10: 0 +} +SourceFile: "AddMain.java" +Classfile /home/levent/Projects/CVM/sample/AddMain.class + Last modified Jun 22, 2024; size 349 bytes + SHA-256 checksum 539564bfbc4e1351399408117b2917887b1fa54f643692643b467d55aaa76bd9 + Compiled from "AddMain.java" +class AddMain + minor version: 0 + major version: 66 + flags: (0x0020) ACC_SUPER + this_class: #8 // AddMain + super_class: #2 // java/lang/Object + interfaces: 0, fields: 0, methods: 3, attributes: 1 +Constant pool: + #1 = Methodref #2.#3 // java/lang/Object."":()V + #2 = Class #4 // java/lang/Object + #3 = NameAndType #5:#6 // "":()V + #4 = Utf8 java/lang/Object + #5 = Utf8 + #6 = Utf8 ()V + #7 = Methodref #8.#9 // AddMain.add:(II)I + #8 = Class #10 // AddMain + #9 = NameAndType #11:#12 // add:(II)I + #10 = Utf8 AddMain + #11 = Utf8 add + #12 = Utf8 (II)I + #13 = Utf8 Code + #14 = Utf8 LineNumberTable + #15 = Utf8 main + #16 = Utf8 ([Ljava/lang/String;)V + #17 = Utf8 SourceFile + #18 = Utf8 AddMain.java +{ + AddMain(); + descriptor: ()V + flags: (0x0000) + Code: + stack=1, locals=1, args_size=1 + 0: aload_0 + 1: invokespecial #1 // Method java/lang/Object."":()V + 4: return + LineNumberTable: + line 1: 0 + + public static void main(java.lang.String[]); + descriptor: ([Ljava/lang/String;)V + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=3, args_size=1 + 0: bipush 14 + 2: istore_1 + 3: bipush 15 + 5: istore_2 + 6: iload_1 + 7: iload_2 + 8: invokestatic #7 // Method add:(II)I + 11: pop + 12: return + LineNumberTable: + line 4: 0 + line 5: 3 + line 6: 6 + line 7: 12 + + public static int add(int, int); + descriptor: (II)I + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=2, args_size=2 + 0: iload_0 + 1: iload_1 + 2: iadd + 3: ireturn + LineNumberTable: + line 10: 0 +} +SourceFile: "AddMain.java" +Classfile /home/levent/Projects/CVM/sample/AddMain.class + Last modified Jun 22, 2024; size 344 bytes + SHA-256 checksum cfc5738c9de9d7cd704372a808960c2c054d836aa52edb6b489e47d0d2ced72f + Compiled from "AddMain.java" +class AddMain + minor version: 0 + major version: 66 + flags: (0x0020) ACC_SUPER + this_class: #8 // AddMain + super_class: #2 // java/lang/Object + interfaces: 0, fields: 0, methods: 3, attributes: 1 +Constant pool: + #1 = Methodref #2.#3 // java/lang/Object."":()V + #2 = Class #4 // java/lang/Object + #3 = NameAndType #5:#6 // "":()V + #4 = Utf8 java/lang/Object + #5 = Utf8 + #6 = Utf8 ()V + #7 = Methodref #8.#9 // AddMain.add:(II)I + #8 = Class #10 // AddMain + #9 = NameAndType #11:#12 // add:(II)I + #10 = Utf8 AddMain + #11 = Utf8 add + #12 = Utf8 (II)I + #13 = Utf8 Code + #14 = Utf8 LineNumberTable + #15 = Utf8 main + #16 = Utf8 ([Ljava/lang/String;)I + #17 = Utf8 SourceFile + #18 = Utf8 AddMain.java +{ + AddMain(); + descriptor: ()V + flags: (0x0000) + Code: + stack=1, locals=1, args_size=1 + 0: aload_0 + 1: invokespecial #1 // Method java/lang/Object."":()V + 4: return + LineNumberTable: + line 1: 0 + + public static int main(java.lang.String[]); + descriptor: ([Ljava/lang/String;)I + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=3, args_size=1 + 0: bipush 14 + 2: istore_1 + 3: bipush 15 + 5: istore_2 + 6: iload_1 + 7: iload_2 + 8: invokestatic #7 // Method add:(II)I + 11: ireturn + LineNumberTable: + line 4: 0 + line 5: 3 + line 6: 6 + + public static int add(int, int); + descriptor: (II)I + flags: (0x0009) ACC_PUBLIC, ACC_STATIC + Code: + stack=2, locals=2, args_size=2 + 0: iload_0 + 1: iload_1 + 2: iadd + 3: ireturn + LineNumberTable: + line 10: 0 +} +SourceFile: "AddMain.java" diff --git a/sample/javap_output.txt b/sample/javap_output.txt deleted file mode 100644 index 422a4d1..0000000 --- a/sample/javap_output.txt +++ /dev/null @@ -1,15 +0,0 @@ -Compiled from "Add.java" -public class Add { - public Add(); - Code: - 0: aload_0 - 1: invokespecial #1 // Method java/lang/Object."":()V - 4: return - - public static int add(int, int); - Code: - 0: iload_0 - 1: iload_1 - 2: iadd - 3: ireturn -} diff --git a/src/execute_engine/cvm_execute.cpp b/src/execute_engine/cvm_execute.cpp index 4bf3fde..177a2f0 100644 --- a/src/execute_engine/cvm_execute.cpp +++ b/src/execute_engine/cvm_execute.cpp @@ -2,6 +2,9 @@ #include +// Temporarily store codeLength globally +size_t codeLength = 0; + /** * Helper methods * for validating execution and @@ -39,8 +42,6 @@ void CVM::execute(const Classfile& cf, const std::string& methodName) { const uint8_t* bytecode = getByteCode(cf, methodInfo); validateByteCode(bytecode); - spdlog::info("Size of bytecode: {}", sizeof(bytecode)); - interprete(bytecode, cf); } @@ -70,6 +71,8 @@ const uint8_t* CVM::getByteCode(const Classfile& cf, std::string codeAttribute = getUtf8FromConstantPool(cf, attr.attribute_name_index); if (codeAttribute == "Code") { + codeLength = attr.attribute_length; + spdlog::info("code length: {}", codeLength); return attr.info; } } @@ -79,13 +82,16 @@ const uint8_t* CVM::getByteCode(const Classfile& cf, void CVM::interprete(const uint8_t* byteCode, const Classfile& cf) { size_t pc = 0; std::stack operandStack; - size_t codeLength = sizeof(byteCode); - while (pc < 100) { + while (pc < codeLength) { uint8_t opcode = byteCode[pc++]; switch (opcode) { + case 0x0: + spdlog::info("==> Opcode: {} - NOP - DO NOTHING", opcode); + break; + default: - spdlog::error("Unknown Opcode: {:#04x} at PC: {}", opcode, pc); + spdlog::error("Unknown Opcode: {:#x} at PC: {}", opcode, pc); break; } } diff --git a/src/main.cpp b/src/main.cpp index 30d4da2..e7a8d67 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,7 +35,7 @@ int main(int argc, char** argv) cf = cf.parseClassfile(buffer.data(), fileSize); CVM cvm; - cvm.execute(cf, "add"); + cvm.execute(cf, "main"); classFile.close(); return 0;