Skip to content

Commit

Permalink
Fix string lookup and implement some more instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Dec 23, 2023
1 parent 23694bf commit a9e2a1d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/VM/Instructions/ComparisonInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,19 @@ void if_icmple(uint8_t* args, uint16_t argsCount, int8_t arg, JavaHeap* heap, VM
thread->pc = thread->pc-3+branchByte;
}
}

void if_acmpne(uint8_t* args, uint16_t argsCount, int8_t arg, JavaHeap* heap, VMThread* thread, VM* VM)
{
u1 byte1 = args[0];
u1 byte2 = args[1];

i2 branchByte = (byte1 << 8) | byte2;

Variable var2 = thread->currentFrame->popOperand();
Variable var1 = thread->currentFrame->popOperand();

if (((i4)var1.data) != ((i4)var2.data))
{
thread->pc = thread->pc-3+branchByte;
}
}
1 change: 1 addition & 0 deletions src/VM/Instructions/ComparisonInstructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ void if_icmpne(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread,
void if_icmplt(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void if_icmpgt(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void if_icmple(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void if_acmpne(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
9 changes: 9 additions & 0 deletions src/VM/Instructions/StoreInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ void istore(uint8_t* args, uint16_t argsCount, int8_t arg, JavaHeap* heap, VMThr
var->type = refVar.type;
}

void astore(uint8_t* args, uint16_t argsCount, int8_t arg, JavaHeap* heap, VMThread* thread, VM* VM)
{
Variable refVar = thread->currentFrame->popOperand();
u1 index = args[0];
Variable* var = &thread->currentFrame->localVariables[index];
var->data = refVar.data;
var->type = refVar.type;
}

void istore_i(uint8_t* args, uint16_t argsCount, int8_t arg, JavaHeap* heap, VMThread* thread, VM* VM)
{
// TODO: Check types
Expand Down
1 change: 1 addition & 0 deletions src/VM/Instructions/StoreInstructions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class VMThread;
class JavaHeap;

void istore(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void astore(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void istore_i(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void astore_i(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
void iastore(u1* args, u2 argsCount, i1 arg, JavaHeap* heap, VMThread* thread, VM* VM);
Expand Down
3 changes: 2 additions & 1 deletion src/VM/JavaHeap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ u4 JavaHeap::getString(const char* utf8String) const
if (strcmp(obj->classInfo->getName(), "java/lang/String") == 0) {
Variable charArrRef = obj->fields[0].data[0];
Array* arr = getArray(charArrRef.data);
if (strncmp((const char*)arr->data, utf8String, arr->length) == 0) {
if (strncmp((const char*)arr->data, utf8String,
(arr->length > strlen(utf8String) ? arr->length : strlen(utf8String))) == 0) {
return currentObj;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/VM/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct Instruction

class VM {
private:
Instruction instructions[78] =
Instruction instructions[80] =
{
// Constants
{i_nop, 0, "nop", 0, nop},
Expand Down Expand Up @@ -72,6 +72,7 @@ class VM {
{i_caload, 0, "caload", 0, caload},
// Stores
{i_istore, 1, "istore", 0, istore},
{i_astore, 1, "astore", 0, astore},
{i_istore_0, 0, "astore_0", 0, istore_i},
{i_istore_1, 0, "astore_1", 1, istore_i},
{i_istore_2, 0, "astore_2", 2, istore_i},
Expand Down Expand Up @@ -102,6 +103,7 @@ class VM {
{i_if_icmplt, 2, "if_icmplt", 0, if_icmplt},
{i_if_icmpgt, 2, "if_icmpgt", 0, if_icmpgt},
{i_if_icmple, 2, "if_icmple", 0, if_icmple},
{i_if_acmpne, 2, "if_acmpne", 0, if_acmpne},
// References
{i_getstatic, 0, "getstatic", 0, getstatic},
{i_putstatic, 0, "putstatic", 0, putstatic},
Expand Down

0 comments on commit a9e2a1d

Please sign in to comment.