Skip to content

Commit

Permalink
[feature] mroe interpreting
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Jun 22, 2024
1 parent f92a997 commit baf9986
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build_and_run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
make format
make install
./build/bin/Debug/CVM sample/Add.class
./build/bin/Debug/CVM sample/AddMain.class
Binary file modified sample/AddMain.class
Binary file not shown.
6 changes: 2 additions & 4 deletions sample/AddMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ class AddMain {
public static int main(String args[]) {
int a = 14;
int b = 15;
return add(a, b);
int c = a + b;
return c;
}

public static int add(int a, int b) {
return a + b;
}
}
60 changes: 60 additions & 0 deletions sample/javap_AddMain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,63 @@ Constant pool:
line 10: 0
}
SourceFile: "AddMain.java"
Classfile /home/levent/Projects/CVM/sample/AddMain.class
Last modified Jun 22, 2024; size 282 bytes
SHA-256 checksum 686603e16d220775966c2568e490983b83c5b5e5725340e919941eec59436e23
Compiled from "AddMain.java"
class AddMain
minor version: 0
major version: 66
flags: (0x0020) ACC_SUPER
this_class: #7 // AddMain
super_class: #2 // java/lang/Object
interfaces: 0, fields: 0, methods: 2, attributes: 1
Constant pool:
#1 = Methodref #2.#3 // java/lang/Object."<init>":()V
#2 = Class #4 // java/lang/Object
#3 = NameAndType #5:#6 // "<init>":()V
#4 = Utf8 java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Class #8 // AddMain
#8 = Utf8 AddMain
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 main
#12 = Utf8 ([Ljava/lang/String;)I
#13 = Utf8 SourceFile
#14 = 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."<init>":()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=4, args_size=1
0: bipush 14
2: istore_1
3: bipush 15
5: istore_2
6: iload_1
7: iload_2
8: iadd
9: istore_3
10: iload_3
11: ireturn
LineNumberTable:
line 4: 0
line 5: 3
line 6: 6
line 7: 10
}
SourceFile: "AddMain.java"
5 changes: 3 additions & 2 deletions src/classfile/classfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,16 @@ bool validateMagicNumber(uint32_t magic) {
}

void parseConstantPool(const uint8_t* bytes, size_t& offset, Classfile& cf) {
setLevel();
cf.constant_pool.resize(cf.constant_pool_count - 1);
spdlog::info("Constant pool size: {}", cf.constant_pool_count - 1);

for (uint16_t i = 1; i < cf.constant_pool_count; i++) {
cp_info& cp_entry = cf.constant_pool[i - 1];
cp_entry.tag = bytes[offset++];

spdlog::info("Parsing constant pool entry {}, tag: {}, offset: {}", i,
cp_entry.tag, offset);
spdlog::debug("Parsing constant pool entry {}, tag: {}, offset: {}", i,
cp_entry.tag, offset);

switch (cp_entry.tag) {
case CONSTANT_Class:
Expand Down
20 changes: 17 additions & 3 deletions src/execute_engine/cvm_execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ const uint8_t* CVM::getByteCode(const Classfile& cf,
getUtf8FromConstantPool(cf, attr.attribute_name_index);
if (codeAttribute == "Code") {
codeLength = attr.attribute_length;
spdlog::info("code length: {}", codeLength);
spdlog::info("code length: {}, The attribute name index: {:#x}",
codeLength, attr.attribute_name_index);
return attr.info;
}
}
Expand All @@ -87,9 +88,22 @@ void CVM::interprete(const uint8_t* byteCode, const Classfile& cf) {
uint8_t opcode = byteCode[pc++];
switch (opcode) {
case 0x0:
spdlog::info("==> Opcode: {} - NOP - DO NOTHING", opcode);
spdlog::info("==> Opcode: {:#X} - NOP - DO NOTHING", opcode);
break;
case 0x2:
operandStack.push(-1);
spdlog::info(
"==> Opcode {:#X} - iconst_m1 - Load m1 to the operand stack: "
"<TOP: {}>",
opcode, operandStack.top());
break;
case 0x3:
operandStack.push(0);
spdlog::info(
"==> Opcode {:#X} - iconst_0 - Load 0 to the operand stack: <TOP: "
"{}>",
opcode, operandStack.top());
break;

default:
spdlog::error("Unknown Opcode: {:#x} at PC: {}", opcode, pc);
break;
Expand Down

0 comments on commit baf9986

Please sign in to comment.