Skip to content

Commit

Permalink
Add System class init and improve virtual method invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Mar 6, 2024
1 parent e17a0ca commit 9bb1179
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/Library/java/lang/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
JCALL void lib_java_lang_System_registerNatives(NATIVE_ARGS)
{
registerNative("java/lang/System/arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V", lib_java_lang_System_arraycopy);
registerNative("java/lang/System/initProperties", "(Ljava/util/Properties;)Ljava/util/Properties;", lib_java_lang_System_initProperties);
}

JCALL void lib_java_lang_System_arraycopy(NATIVE_ARGS)
Expand Down Expand Up @@ -49,3 +50,8 @@ JCALL void lib_java_lang_System_arraycopy(NATIVE_ARGS)
srcArray->data+(srcPosVar.data*bytes),
lengthVar.data * bytes);
}

JCALL void lib_java_lang_System_initProperties(NATIVE_ARGS)
{
thread->returnVar(thread->m_currentFrame->localVariables[0]);
}
3 changes: 2 additions & 1 deletion src/Library/java/lang/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#include "Library/NativeDefs.h"

JCALL void lib_java_lang_System_registerNatives(NATIVE_ARGS);
JCALL void lib_java_lang_System_arraycopy(NATIVE_ARGS);
JCALL void lib_java_lang_System_arraycopy(NATIVE_ARGS);
JCALL void lib_java_lang_System_initProperties(NATIVE_ARGS);
42 changes: 38 additions & 4 deletions src/VM/Instructions/ReferenceInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ static void invokeVirtual(ClassInfo* classInfo, MethodInfo* methodInfo, VMThread
}
}

if (methodInfo->isNative()) {
if (targetMethod->isNative()) {
thread->pushNativeStackFrame(targetClass, targetMethod, arguments.size());
} else {
thread->pushStackFrameWithoutParams(targetClass, targetMethod);
Expand All @@ -237,7 +237,7 @@ static void invokeVirtual(ClassInfo* classInfo, MethodInfo* methodInfo, VMThread
}
}

if (methodInfo->isNative())
if (targetMethod->isNative())
{
VM->executeNativeMethod(targetClass, targetMethod, heap, thread);
thread->popFrame();
Expand All @@ -255,9 +255,43 @@ void invokevirtual(INSTRUCTION_ARGS)
const std::string_view methodDescriptor = topFrame->constantPool->getString(nameAndTypeInfo->descriptorIndex);
const std::string_view className = topFrame->constantPool->getString(cpClassInfo->nameIndex);
ClassInfo* targetClassInfo = VM->getClass(className.data(), thread);
MethodInfo* methodInfo = targetClassInfo->findMethodWithNameAndDescriptor(methodName.data(), methodDescriptor.data());
MethodInfo* foundMethod = targetClassInfo->findMethodWithNameAndDescriptor(methodName.data(), methodDescriptor.data());

MethodInfo* targetMethod = nullptr;

// BEGIN BROL
if (foundMethod != nullptr)
{
targetMethod = foundMethod;
} else
{

ClassInfo* currentClass = targetClassInfo;
while (currentClass->superClass != 0) {
CPClassInfo* ci = currentClass->constantPool->getClassInfo(currentClass->superClass);
[[maybe_unused]] const std::string_view superClass = currentClass->constantPool->getString(ci->nameIndex);
currentClass = VM->getClass(superClass.data(), thread);
if (currentClass != nullptr) {
foundMethod = currentClass->findMethodWithNameAndDescriptor(methodName.data(),
methodDescriptor.data());
if (foundMethod != nullptr) {
targetClassInfo = currentClass;
targetMethod = foundMethod;
break;
}
} else {
break;
}
}

if (foundMethod == nullptr) {
thread->internalError("Failed to get the correct method on the object.\n"
" Searching on superclass and generic search is not implemented yet.");
}
}
// END BROL

invokeVirtual(targetClassInfo, methodInfo, thread, VM, heap);
invokeVirtual(targetClassInfo, targetMethod, thread, VM, heap);
printf("> Created new stack frame for virtual call on: %s.%s()\n", className.data(), methodName.data());
}

Expand Down
22 changes: 20 additions & 2 deletions src/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ void VM::start(const char* commandLineName)
getClass("java/lang/Number", &m_mainThread);
ClassInfo* classInfo = getClass("java/lang/Class", &m_mainThread);
getClass("java/lang/String", &m_mainThread);
getClass("java/lang/System", &m_mainThread);
ClassInfo* systemClass = getClass("java/lang/System", &m_mainThread);
getClass("java/lang/Thread", &m_mainThread);
getClass("java/lang/ThreadGroup", &m_mainThread);
getClass("java/lang/reflect/Field", &m_mainThread);
getClass("java/lang/reflect/AccessibleObject", &m_mainThread);
getClass("java/io/PrintStream", &m_mainThread);
getClass("java/io/FilterOutputStream", &m_mainThread);
getClass("java/io/OutputStream", &m_mainThread);
getClass("java/util/Properties", &m_mainThread);

m_heap.setClassInfo(classInfo);

const u4 threadGroupReference = createThreadGroupObject(&m_mainThread);
m_mainThread.threadObject = createThreadObject(&m_mainThread, threadGroupReference);

initSystemClass(systemClass, &m_mainThread);
}

u4 VM::createThreadGroupObject(VMThread* thread)
Expand Down Expand Up @@ -334,7 +337,6 @@ void VM::runMain()
Platform::exitProgram(6);
}

Memory memory(1000, KIB(5));
ClassInfo* startupClass = getClass(m_configuration.mainClassName.data(), mainThread);
MethodInfo* entryPoint = startupClass->findMethodWithNameAndDescriptor("main", "([Ljava/lang/String;)V");

Expand All @@ -358,6 +360,21 @@ void VM::runMain()
printf("> Done executing\n");
}

void VM::initSystemClass(ClassInfo* systemClass, VMThread* thread)
{
MethodInfo* initMethod = systemClass->findMethodWithNameAndDescriptor("initializeSystemClass", "()V");

thread->m_pc = 0;
thread->m_currentClass = systemClass;
thread->m_currentMethod = initMethod;

thread->pushStackFrameSpecial(systemClass, initMethod, nullptr, &m_heap);

printf("> Executing System init method...\n");
executeLoop(thread);
printf("> Done executing System init method\n");
}

void VM::shutdown()
{
PHYSFS_deinit();
Expand All @@ -371,3 +388,4 @@ void VM::checkType(const Variable var, const VariableType type, VMThread* thread
thread->internalError("Error: TypeMismatch");
}
}

1 change: 1 addition & 0 deletions src/VM/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class VM {
return nullptr;
}
private:
void initSystemClass(ClassInfo* class_info, VMThread* vm_thread);
inline static constexpr std::array<Instruction,114> m_instructions{{
// Constants
{i_nop, 0, "nop", 0, nop},
Expand Down

0 comments on commit 9bb1179

Please sign in to comment.