Skip to content

Commit

Permalink
[refactor] constant pool debug messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Jun 20, 2024
1 parent 3ed8948 commit 222886a
Showing 1 changed file with 55 additions and 12 deletions.
67 changes: 55 additions & 12 deletions src/classfile/classfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,47 @@

#include "../../include/cvm/log.hpp"

// The global logger for classfile.cpp
auto logger = Log::getInstance().getLogger();

uint16_t Classfile::readShort(const uint8_t* bytes, size_t& offset)
{
spdlog::info("Reading short at offset: {}", offset);
spdlog::set_level(spdlog::level::debug);
spdlog::debug("Reading short at offset: {}", offset);
uint16_t value = (bytes[offset] << 8 | bytes[offset + 1]);
offset += 2;
spdlog::info("Read short: 0x{:04x}, new offset: {}", value, offset);
spdlog::debug("Read short: 0x{:04x}, new offset: {}", value, offset);
return value;
}

uint32_t Classfile::readInt(const uint8_t* bytes, size_t& offset)
{
spdlog::info("Reading int at offset: {}", offset);
spdlog::set_level(spdlog::level::debug);
spdlog::debug("Reading int at offset: {}", offset);
uint32_t value = (bytes[offset] << 24) | (bytes[offset + 1] << 16) | (bytes[offset + 2] << 8) | bytes[offset + 3];
offset += 4;
spdlog::info("Read int: 0x{:08x}, new offset: {}", value, offset);
spdlog::debug("Read int: 0x{:08x}, new offset: {}", value, offset);
return value;
}

uint64_t Classfile::readLong(const uint8_t* bytes, size_t& offset)
{
spdlog::info("Reading long at offset: {}", offset);
spdlog::set_level(spdlog::level::debug);
spdlog::debug("Reading long at offset: {}", offset);
uint64_t value = (static_cast<uint64_t>(bytes[offset]) << 56) | (static_cast<uint64_t>(bytes[offset + 1]) << 48) |
(static_cast<uint64_t>(bytes[offset + 2]) << 40) | (static_cast<uint64_t>(bytes[offset + 3]) << 32) |
(static_cast<uint64_t>(bytes[offset + 4]) << 24) | (static_cast<uint64_t>(bytes[offset + 5]) << 16) |
(static_cast<uint64_t>(bytes[offset + 6]) << 8) | static_cast<uint64_t>(bytes[offset + 7]);
offset += 8;
spdlog::info("Read long: 0x{:016x}, new offset: {}", value, offset);
spdlog::debug("Read long: 0x{:016x}, new offset: {}", value, offset);
return value;
}

double Classfile::readDouble(const uint8_t* bytes, size_t& offset)
{
spdlog::info("Reading double at offset: {}", offset);
spdlog::set_level(spdlog::level::debug);
spdlog::debug("Reading double at offset: {}", offset);
uint64_t longValue = readLong(bytes, offset);
double value;
std::memcpy(&value, &longValue, sizeof(value));
spdlog::info("Read double: {}, new offset: {}", value, offset);
spdlog::debug("Read double: {}, new offset: {}", value, offset);
return value;
}
/**
Expand Down Expand Up @@ -124,6 +125,37 @@ void parseConstantPool(const uint8_t* bytes, size_t& offset, Classfile& cf)
}
}

void printConstantPoolEntry(const cp_info& cp_entry)
{
spdlog::info("Tag: {}", cp_entry.tag);
switch (cp_entry.tag)
{
case CONSTANT_Class: spdlog::info("Class: name_index = {}", cp_entry.info.Class.name_index); break;
case CONSTANT_Fieldref:
case CONSTANT_Methodref:
case CONSTANT_InterfaceMethodref:
spdlog::info(
"Field/Method/InterfaceMethod: class_index = {}, name_and_type_index = {}",
cp_entry.info.Fieldref.class_index,
cp_entry.info.Fieldref.name_and_type_index);
break;
case CONSTANT_String: spdlog::info("String: string_index = {}", cp_entry.info.String.string_index); break;
case CONSTANT_Integer: spdlog::info("Integer: bytes = {}", cp_entry.info.Integer.bytes); break;
case CONSTANT_Double: spdlog::info("Double: bytes = {}", cp_entry.info.Double.bytes); break;
case CONSTANT_NameAndType:
spdlog::info(
"NameAndType: name_index = {}, descriptor_index = {}",
cp_entry.info.NameAndType.name_index,
cp_entry.info.NameAndType.descriptor_index);
break;
case CONSTANT_Utf8:
spdlog::info("Utf8: length = {}", cp_entry.info.Utf8.length);
spdlog::info("Utf8: bytes = {}", std::string((char*)cp_entry.info.Utf8.bytes, cp_entry.info.Utf8.length));
break;
default: spdlog::warn("Unknown tag type: {}", cp_entry.tag); break;
}
}

/**
* Helper methods ends
*/
Expand All @@ -147,6 +179,17 @@ Classfile Classfile::parseClassfile(const uint8_t* classBytes, size_t fileSize)
cf.major_version = readShort(classBytes, offset);
cf.constant_pool_count = readShort(classBytes, offset);
parseConstantPool(classBytes, offset, cf);
fmt::print("Constant Pool has been parsed: {}", cf.constant_pool.at(0).tag);

spdlog::info("Parsing Constant Pool has been completed: ");
/*
int cp_index = 0;
for (const auto& entry : cf.constant_pool)
{
spdlog::info("for entry no: {}", cp_index);
cp_index++;
printConstantPoolEntry(entry);
}
*/

return cf;
}

0 comments on commit 222886a

Please sign in to comment.