From f92a99794c9defe5ecc9b5ed6775d434897d3ca0 Mon Sep 17 00:00:00 2001 From: lvntky Date: Sat, 22 Jun 2024 19:47:33 +0300 Subject: [PATCH] [feature] main method execution added --- sample/AddMain.class | Bin 0 -> 344 bytes sample/AddMain.java | 12 ++ sample/Hello.class | Bin 393 -> 0 bytes sample/Hello.java | 5 - sample/HelloMain.class | Bin 453 -> 0 bytes sample/HelloMain.java | 6 - sample/javap_Add.txt | 51 ----- sample/javap_AddMain.txt | 298 +++++++++++++++++++++++++++++ sample/javap_output.txt | 15 -- src/execute_engine/cvm_execute.cpp | 16 +- src/main.cpp | 2 +- 11 files changed, 322 insertions(+), 83 deletions(-) create mode 100644 sample/AddMain.class create mode 100644 sample/AddMain.java delete mode 100644 sample/Hello.class delete mode 100644 sample/Hello.java delete mode 100644 sample/HelloMain.class delete mode 100644 sample/HelloMain.java delete mode 100644 sample/javap_Add.txt create mode 100644 sample/javap_AddMain.txt delete mode 100644 sample/javap_output.txt diff --git a/sample/AddMain.class b/sample/AddMain.class new file mode 100644 index 0000000000000000000000000000000000000000..7251a89d920ba1f7177eff22487e9b01f159c61d GIT binary patch literal 344 zcmYLE%Syvg5IvKmiM_@oD!8x}T&5!S18fBqgh16r#f=+pdPBLTO(CRyl^a(|7k+>r zCCa)C7NEOIu$N+_d+s zu!D906=D}5A-Fc?UfYsEZ49B6W?3&Ic(+Sa5aN+7i-*-BFRCY<&w1i48un58GTJ(g z>&lkXOA)H^a#c-=J1gXHv;9J-IAv>xj|N;0z7~W%?oPOpOwK+*AKVZk&H_u{MvR23 zP|xLAu3-`ll6ctZej#{oSfYp;%;PM}eOB_18)cW3V(uMxUbl*W55=5)#vB~{0h9MK A3jhEB literal 0 HcmV?d00001 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 fdd0d0bfa8dc2a94254f7fba84e97c70ef87001d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 393 zcmZut%}T>S7@V(3x=l=NZLR-8dk|U?^8ghO3gV%t5UJ;+UFw!?A~6wsEIlZA@Bw@% z@!JTB;2ypmW@qMG=Hv729l!~8JXBx_*ghOo3C(%*5Cu9)X2E4Vmy?`OJyMCvj|o

MfTcP2z9oZJ5nE`P|WDwz!jV}Uh-Q|1!v({w5cjgd;^MX`uwb{)l< z8Ryn4CU{|5WD|L&4C$4wL$elLbhr(3%;Y{mSZB1$HyLLC74*b_u))(j8%)^zk?yh8 au@c5S?;ik8(6Uj2Wxk zZFpERQFBnoGNC@lMlc@~Bubd;|A31LnJSZUw>?mhrXs8mbpN6~FtJ9kijOV>$v*uw z9SMTllZm*R#-Ye=gD~QX9vA(le?JdQu>0vW8;Ucj#MNKoftnL7tn&KQQ8zCTgmuog zIFhRDy@6ghA#Cui+A6oOSxA)hkgclfy~3":()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;