Updated src/Main.cpp to use the cmake variable ASSERT and to improve compatibility with the .class spec

This commit is contained in:
calcium 2024-08-19 20:07:59 -04:00
parent 38802e1f3a
commit 1749594952

View File

@ -2,6 +2,7 @@
#include "Methods.h"
#include <ConstantPoolTags/Tags.h>
#include <SpecTypes.h>
#include <any>
#include <cassert>
#include <cstdint>
#include <cstdlib>
@ -10,8 +11,13 @@
#include <Utils/Utils.h>
#include <iostream>
#include <Globals.h>
#include <typeindex>
#include <typeinfo>
#include <Attributes.h>
#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<uint32_t>(Globals::buffer(), 4);
assert(jc.magic.ToHostFormat() == 0xcafebabe);
ASSERT(jc.magic.ToHostFormat() == 0xCAFEBABE);
jc.minor = Utils::ReadFromStartIntoVal<uint16_t>(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<int>(Globals::buffer(), 1);
ConstantPoolTags::Tags tag = (ConstantPoolTags::Tags)Utils::ReadFromStartIntoVal<int>(Globals::buffer(), 1);
ConstantPoolTags::ConstantPoolParser(cp, jc);
ConstantPoolTags::ConstantPoolParser(tag, jc);
}
jc.access_flags = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
jc.this_class = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
jc.super_class = Utils::ReadFromStartIntoVal<uint16_t>(Globals::buffer(), 2);
jc.interfaces_count = Utils::ReadFromStartIntoVal<uint16_t>(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<ConstantPoolTags::ConstantClassInfo>(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<uint16_t>(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<ConstantPoolTags::ConstantUtf8Info>(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<ConstantPoolTags::ConstantStringInfo>(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<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[std::any_cast<ConstantPoolTags::ConstantStringInfo>(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<ConstantPoolTags::ConstantUtf8Info>(jc.constant_pool[29]).bytes << std::endl;
std::cout << Globals::buffer().size() << std::endl;
// std::cout << &std::any_cast<ConstantPoolTags::ConstantPool>(jc.constant_pool[30]).info << std::endl;
// delete[] ((ConstantPoolTags::ConstantUtf8Info*)(&jc.constant_pool[30].info))->bytes;
// std::cout << jc.methods_count.ToHostFormat() << std::endl;