From 17495949522d00d2b60a97749fde5e9927497f17 Mon Sep 17 00:00:00 2001 From: calcium Date: Mon, 19 Aug 2024 20:07:59 -0400 Subject: [PATCH] Updated src/Main.cpp to use the cmake variable `ASSERT` and to improve compatibility with the .class spec --- src/Main.cpp | 93 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 76 insertions(+), 17 deletions(-) diff --git a/src/Main.cpp b/src/Main.cpp index 34b6dba..0347966 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -2,6 +2,7 @@ #include "Methods.h" #include #include +#include #include #include #include @@ -10,8 +11,13 @@ #include #include #include +#include +#include +#include -#define JVM_STACK_SIZE 4096 +#define KB 1024 +#define MB 1024 * KB +#define JVM_STACK_SIZE 16 * MB int stack[JVM_STACK_SIZE]; @@ -47,7 +53,7 @@ int main(int argc, char **argv) jc.magic = Utils::ReadFromStartIntoVal(Globals::buffer(), 4); - assert(jc.magic.ToHostFormat() == 0xcafebabe); + ASSERT(jc.magic.ToHostFormat() == 0xCAFEBABE); jc.minor = Utils::ReadFromStartIntoVal(Globals::buffer(), 2); @@ -63,21 +69,23 @@ int main(int argc, char **argv) for (int i = 0; i < jc.constant_pool_count.ToHostFormat() - 1; i++) { - ConstantPoolTags::ConstantPool cp; - cp.tag = (ConstantPoolTags::Tags)Utils::ReadFromStartIntoVal(Globals::buffer(), 1); + ConstantPoolTags::Tags tag = (ConstantPoolTags::Tags)Utils::ReadFromStartIntoVal(Globals::buffer(), 1); - ConstantPoolTags::ConstantPoolParser(cp, jc); + ConstantPoolTags::ConstantPoolParser(tag, jc); } jc.access_flags = Utils::ReadFromStartIntoVal(Globals::buffer(), 2); jc.this_class = Utils::ReadFromStartIntoVal(Globals::buffer(), 2); jc.super_class = Utils::ReadFromStartIntoVal(Globals::buffer(), 2); jc.interfaces_count = Utils::ReadFromStartIntoVal(Globals::buffer(), 2); - assert(jc.this_class.ToHostFormat() >= 0); - assert(jc.super_class.ToHostFormat() >= 0); + ASSERT(jc.this_class.ToHostFormat() >= 0); + ASSERT(jc.super_class.ToHostFormat() >= 0); - assert(jc.constant_pool.size() > jc.this_class.ToHostFormat()); - assert(jc.constant_pool[jc.this_class.ToHostFormat() - 1].tag == ConstantPoolTags::Tags::Class); + ASSERT(jc.constant_pool.size() > jc.this_class.ToHostFormat()); + // std::cout << (int)(jc.constant_pool[jc.this_class.ToHostFormat() - 1]).tag << std::endl; + + std::cout << std::dec << (int)std::any_cast(jc.constant_pool[jc.this_class.ToHostFormat() - 1]).tag << std::endl; + // ASSERT(jc.constant_pool[jc.this_class.ToHostFormat() - 1].type().name()); if (jc.super_class.ToHostFormat() != 0) { @@ -90,8 +98,8 @@ int main(int argc, char **argv) // item of its ClassFile structure. // FIXME: Finish implementing the above comment. - assert(jc.constant_pool.size() > jc.super_class.ToHostFormat()); - assert(jc.constant_pool[jc.super_class.ToHostFormat() - 1].tag == ConstantPoolTags::Tags::Class); + ASSERT(jc.constant_pool.size() > jc.super_class.ToHostFormat()); + ASSERT(jc.constant_pool[jc.super_class.ToHostFormat() - 1].type() == std::type_index(typeid(ConstantPoolTags::ConstantClassInfo))); } else { @@ -105,12 +113,11 @@ int main(int argc, char **argv) if (jc.access_flags.ToHostFormat() >= (int)JavaClass::ACCESS_FLAGS::ACC_MODULE) { - assert(jc.access_flags.ToHostFormat() == (int)JavaClass::ACCESS_FLAGS::ACC_MODULE); + ASSERT(jc.access_flags.ToHostFormat() == (int)JavaClass::ACCESS_FLAGS::ACC_MODULE); // Conditions for when jc.access_flags == JavaClass::ACCESS_FLAGS::ACC_MODULE // major_version, minor_version: ≥ 53.0 (i.e., Java SE 9 and above) - assert(jc.major.ToHostFormat() >= 53); - assert(jc.minor.ToHostFormat() >= 0); - + ASSERT(jc.major.ToHostFormat() >= 53); + ASSERT(jc.minor.ToHostFormat() >= 0); } // std::cout << std::dec << (int)(jc.constant_pool[jc.this_class.ToHostFormat()].info.tag) << std::endl; @@ -143,12 +150,64 @@ int main(int argc, char **argv) // std::cout << std::hex << size - Globals::buffer().size() << std::endl; } + jc.attributes_count = Utils::ReadFromStartIntoVal(Globals::buffer()); + Attributes::AttributesParser(jc); + + std::cout << std::dec << "Attributes Count: " << jc.attributes_count.ToHostFormat() << std::endl; + for (int i = 0; i < jc.constant_pool_count.ToHostFormat() - 1; i++) { - std::cout << "(" << i << ") " << (int)jc.constant_pool[i].tag << std::endl; + auto x = jc.constant_pool[i].type().name(); + + std::cout << "(" << i << ") "; + + if (x == TYPE(ConstantPoolTags::ConstantUtf8Info)) + { + std::cout << "utf8:\t" << std::any_cast(jc.constant_pool[i]).bytes << std::endl; + } + else if (x == TYPE(ConstantPoolTags::ConstantClassInfo)) + { + std::cout << "class" << std::endl; + } + else if (x == TYPE(ConstantPoolTags::ConstantStringInfo)) + { + ASSERT((jc.constant_pool[std::any_cast(jc.constant_pool[i]) + .string_index.ToHostFormat() - + 1]) + .type() + .name() + == TYPE(ConstantPoolTags::ConstantUtf8Info)); + std::cout + << "string index:\t" << i << "\t(utf-8 str):\t" + << std::any_cast(jc.constant_pool[std::any_cast(jc.constant_pool[i]) + .string_index.ToHostFormat() - + 1]).bytes + << std::endl; + + } + else if (x == TYPE(ConstantPoolTags::ConstantFieldrefInfo)) + { + std::cout << "fieldref" << std::endl; + } + else if (x == TYPE(ConstantPoolTags::ConstantMethodrefInfo)) + { + std::cout << "methodref" << std::endl; + } + else if (x == TYPE(ConstantPoolTags::ConstantNameAndTypeInfo)) + { + std::cout << "nameandtype" << std::endl; + } + else + { + std::cout << "Unimplemented tag " << jc.constant_pool[i].type().name() << std::endl; + exit(EXIT_FAILURE); + } } - std::cout << &jc.constant_pool[30].info << std::endl; + // std::cout << std::any_cast(jc.constant_pool[29]).bytes << std::endl; + std::cout << Globals::buffer().size() << std::endl; + + // std::cout << &std::any_cast(jc.constant_pool[30]).info << std::endl; // delete[] ((ConstantPoolTags::ConstantUtf8Info*)(&jc.constant_pool[30].info))->bytes; // std::cout << jc.methods_count.ToHostFormat() << std::endl;