From 97a077644117047d2438b03e9c7325fa90adec6d Mon Sep 17 00:00:00 2001 From: lvntky Date: Sat, 22 Jun 2024 14:15:58 +0300 Subject: [PATCH] [format] re-format code on google cpp style --- .clang-format | 103 ++++++++-- include/cvm/banner.hpp | 3 +- include/cvm/classfile/attribute_info.hpp | 14 +- include/cvm/classfile/classfile.hpp | 13 +- include/cvm/classfile/cp_info.hpp | 43 ++-- include/cvm/classfile/field_info.hpp | 14 +- include/cvm/classfile/method_info.hpp | 14 +- include/cvm/execute_engine/cvm_execute.hpp | 9 +- include/cvm/log.hpp | 4 +- include/cvm/stack/cvm_stack.hpp | 18 +- include/cvm/stack/frame.hpp | 10 +- src/classfile/classfile.cpp | 227 ++++++++++----------- src/execute_engine/cvm_execute.cpp | 61 +++--- src/log.cpp | 6 +- src/main.cpp | 9 +- src/stack/frame.cpp | 21 +- 16 files changed, 274 insertions(+), 295 deletions(-) diff --git a/.clang-format b/.clang-format index 8d70915..df2202e 100644 --- a/.clang-format +++ b/.clang-format @@ -1,24 +1,83 @@ ---- +# Google C/C++ Code Style settings +# https://clang.llvm.org/docs/ClangFormatStyleOptions.html +# Author: Kehan Xue, kehan.xue (at) gmail.com + +Language: Cpp BasedOnStyle: Google -AlignAfterOpenBracket: 'AlwaysBreak' -AllowAllConstructorInitializersOnNextLine: 'false' -AllowAllParametersOfDeclarationOnNextLine: 'false' -AlignConsecutiveMacros: 'true' -AllowShortCaseLabelsOnASingleLine: 'true' -AllowShortFunctionsOnASingleLine: 'None' -AllowShortIfStatementsOnASingleLine: 'Never' -AllowShortLoopsOnASingleLine: 'false' -BreakBeforeBraces: Allman -BinPackArguments: 'false' -BinPackParameters: 'false' -Cpp11BracedListStyle: 'false' -ColumnLimit: 125 -NamespaceIndentation: All -SpaceAfterTemplateKeyword: 'false' -SpaceBeforeCtorInitializerColon: 'true' -SpaceBeforeInheritanceColon: 'true' +AccessModifierOffset: -1 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: None +AlignOperands: Align +AllowAllArgumentsOnNextLine: true +AllowAllConstructorInitializersOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Empty +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never # To avoid conflict, set this "Never" and each "if statement" should include brace when coding +AllowShortLambdasOnASingleLine: Inline +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: None +AlwaysBreakTemplateDeclarations: Yes +BinPackArguments: true +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterStruct: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +BreakBeforeBinaryOperators: None +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeColon +BreakInheritanceList: BeforeColon +ColumnLimit: 80 +CompactNamespaces: false +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false # Make sure the * or & align on the left +EmptyLineBeforeAccessModifier: LogicalBlock +FixNamespaceComments: true +IncludeBlocks: Preserve +IndentCaseLabels: true +IndentPPDirectives: None +IndentWidth: 2 +KeepEmptyLinesAtTheStartOfBlocks: true +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PointerAlignment: Left +ReflowComments: false +# SeparateDefinitionBlocks: Always # Only support since clang-format 14 +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true SpaceBeforeParens: ControlStatements -SpaceBeforeRangeBasedForLoopColon: 'true' -SpaceInEmptyBlock: true -Standard: 'Latest' -... +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: c++11 +TabWidth: 4 +UseTab: Never diff --git a/include/cvm/banner.hpp b/include/cvm/banner.hpp index bd07e72..cab0404 100644 --- a/include/cvm/banner.hpp +++ b/include/cvm/banner.hpp @@ -7,8 +7,7 @@ #define CVM_VERSION "2.0.0" -void printBanner() -{ +void printBanner() { const std::string green = "\033[32m"; const std::string reset = "\033[0m"; diff --git a/include/cvm/classfile/attribute_info.hpp b/include/cvm/classfile/attribute_info.hpp index 22155e5..b772e38 100644 --- a/include/cvm/classfile/attribute_info.hpp +++ b/include/cvm/classfile/attribute_info.hpp @@ -5,26 +5,20 @@ #include "../fmt_commons.hpp" -typedef struct attribute_info -{ +typedef struct attribute_info { uint16_t attribute_name_index; uint32_t attribute_length; uint8_t* info; - ~attribute_info() - { - delete[] info; - } + ~attribute_info() { delete[] info; } - std::string to_string() const - { + std::string to_string() const { return fmt::format( "Attribute:\n" " attribute_name_index: {}\n" " attribute_length: {}\n" " info: [{}]", - attribute_name_index, - attribute_length, + attribute_name_index, attribute_length, fmt::join(info, info + attribute_length, ", ")); } } attribute_info; diff --git a/include/cvm/classfile/classfile.hpp b/include/cvm/classfile/classfile.hpp index 6934986..783936d 100644 --- a/include/cvm/classfile/classfile.hpp +++ b/include/cvm/classfile/classfile.hpp @@ -12,8 +12,7 @@ #include "field_info.hpp" #include "method_info.hpp" -class Classfile -{ +class Classfile { public: uint32_t magic; uint16_t minor_version; @@ -32,11 +31,11 @@ class Classfile uint16_t attributes_count; std::vector attributes; - uint16_t readShort(const uint8_t *bytes, size_t &offset); - uint32_t readInt(const uint8_t *bytes, size_t &offset); - uint64_t readLong(const uint8_t *bytes, size_t &offset); - double readDouble(const uint8_t *bytes, size_t &offset); - Classfile parseClassfile(const uint8_t *classBytes, size_t fileSize); + uint16_t readShort(const uint8_t* bytes, size_t& offset); + uint32_t readInt(const uint8_t* bytes, size_t& offset); + uint64_t readLong(const uint8_t* bytes, size_t& offset); + double readDouble(const uint8_t* bytes, size_t& offset); + Classfile parseClassfile(const uint8_t* classBytes, size_t fileSize); std::string to_string() const; }; diff --git a/include/cvm/classfile/cp_info.hpp b/include/cvm/classfile/cp_info.hpp index 603166e..bf80634 100644 --- a/include/cvm/classfile/cp_info.hpp +++ b/include/cvm/classfile/cp_info.hpp @@ -6,49 +6,40 @@ #include "../fmt_commons.hpp" // Constants for constant pool tags -#define CONSTANT_Class 7 -#define CONSTANT_Fieldref 9 -#define CONSTANT_Methodref 10 +#define CONSTANT_Class 7 +#define CONSTANT_Fieldref 9 +#define CONSTANT_Methodref 10 #define CONSTANT_InterfaceMethodref 11 -#define CONSTANT_String 8 -#define CONSTANT_Integer 3 -#define CONSTANT_NameAndType 12 -#define CONSTANT_Double 6 -#define CONSTANT_Utf8 1 +#define CONSTANT_String 8 +#define CONSTANT_Integer 3 +#define CONSTANT_NameAndType 12 +#define CONSTANT_Double 6 +#define CONSTANT_Utf8 1 -typedef struct cpinfo -{ +typedef struct cpinfo { uint8_t tag; - union - { - struct - { + union { + struct { uint16_t name_index; } Class; - struct - { + struct { uint16_t string_index; } String; - struct - { + struct { uint16_t class_index; uint16_t name_and_type_index; } Fieldref, Methodref, InterfaceMethodref; - struct - { + struct { int32_t bytes; } Integer; - struct - { + struct { uint16_t name_index; uint16_t descriptor_index; } NameAndType; - struct - { + struct { uint64_t bytes; } Double; - struct - { + struct { uint16_t length; uint8_t* bytes; } Utf8; diff --git a/include/cvm/classfile/field_info.hpp b/include/cvm/classfile/field_info.hpp index 0579651..5aae448 100644 --- a/include/cvm/classfile/field_info.hpp +++ b/include/cvm/classfile/field_info.hpp @@ -7,19 +7,16 @@ #include "../fmt_commons.hpp" #include "attribute_info.hpp" -typedef struct field_info -{ +typedef struct field_info { uint16_t access_flags; uint16_t name_index; uint16_t descriptor_index; uint16_t attributes_count; std::vector attributes; - std::string to_string() const - { + std::string to_string() const { std::string attributes_str; - for (const auto& attr : attributes) - { + for (const auto& attr : attributes) { attributes_str += attr.to_string() + "\n"; } return fmt::format( @@ -29,10 +26,7 @@ typedef struct field_info " descriptor_index: {}\n" " attributes_count: {}\n" " attributes: [\n{}\n ]", - access_flags, - name_index, - descriptor_index, - attributes_count, + access_flags, name_index, descriptor_index, attributes_count, attributes_str); } } field_info; diff --git a/include/cvm/classfile/method_info.hpp b/include/cvm/classfile/method_info.hpp index aabf10e..66f6f11 100644 --- a/include/cvm/classfile/method_info.hpp +++ b/include/cvm/classfile/method_info.hpp @@ -7,19 +7,16 @@ #include "../fmt_commons.hpp" #include "attribute_info.hpp" -typedef struct method_info -{ +typedef struct method_info { uint16_t access_flags; uint16_t name_index; uint16_t descriptor_index; uint16_t attributes_count; std::vector attributes; - std::string to_string() const - { + std::string to_string() const { std::string attributes_str; - for (const auto& attr : attributes) - { + for (const auto& attr : attributes) { attributes_str += attr.to_string() + "\n"; } return fmt::format( @@ -29,10 +26,7 @@ typedef struct method_info " descriptor_index: {}\n" " attributes_count: {}\n" " attributes: [\n{}\n ]", - access_flags, - name_index, - descriptor_index, - attributes_count, + access_flags, name_index, descriptor_index, attributes_count, attributes_str); } } method_info; diff --git a/include/cvm/execute_engine/cvm_execute.hpp b/include/cvm/execute_engine/cvm_execute.hpp index 24af29a..5c2fb9e 100644 --- a/include/cvm/execute_engine/cvm_execute.hpp +++ b/include/cvm/execute_engine/cvm_execute.hpp @@ -8,16 +8,17 @@ #include "../classfile/classfile.hpp" #include "../log.hpp" -class CVM -{ +class CVM { public: CVM() = default; void execute(const Classfile& cf, const std::string& methodName); private: std::string getUtf8FromConstantPool(const Classfile& cf, uint16_t index); - const method_info* findMehodByName(const Classfile& cf, const std::string& methodName); - const uint8_t* getByteCode(const Classfile& cf, const method_info* methodInfo); + const method_info* findMehodByName(const Classfile& cf, + const std::string& methodName); + const uint8_t* getByteCode(const Classfile& cf, + const method_info* methodInfo); void interprete(const uint8_t* byteCode, const Classfile& cf); }; diff --git a/include/cvm/log.hpp b/include/cvm/log.hpp index 9e1c3eb..b468498 100644 --- a/include/cvm/log.hpp +++ b/include/cvm/log.hpp @@ -5,8 +5,8 @@ #include -#define LOG_OK "OK" -#define LOG_NOK "NOK" +#define LOG_OK "OK" +#define LOG_NOK "NOK" #define DEBUG_ENABLED 0 void setLevel(); diff --git a/include/cvm/stack/cvm_stack.hpp b/include/cvm/stack/cvm_stack.hpp index 041f843..d05cae1 100644 --- a/include/cvm/stack/cvm_stack.hpp +++ b/include/cvm/stack/cvm_stack.hpp @@ -5,23 +5,13 @@ #include "frame.hpp" -class CVMStack -{ +class CVMStack { public: - void pushFrame(Frame frame) - { - frames.push(frame); - } + void pushFrame(Frame frame) { frames.push(frame); } - Frame& topFrame() - { - return frames.top(); - } + Frame& topFrame() { return frames.top(); } - void popFrame() - { - frames.pop(); - } + void popFrame() { frames.pop(); } private: std::stack frames; diff --git a/include/cvm/stack/frame.hpp b/include/cvm/stack/frame.hpp index f83076b..e2cbd73 100644 --- a/include/cvm/stack/frame.hpp +++ b/include/cvm/stack/frame.hpp @@ -8,12 +8,12 @@ #include "../fmt_commons.hpp" // #include "../log.hpp" -class Frame -{ +class Frame { public: - Frame(size_t maxLocals, size_t maxStack) : localVariables(maxLocals), operandStack(maxStack), operandStackSize(maxStack) - { - } + Frame(size_t maxLocals, size_t maxStack) + : localVariables(maxLocals), + operandStack(maxStack), + operandStackSize(maxStack) {} int32_t getLocalVariable(size_t index) const; void setLocalVariable(size_t index, int32_t value); void pushOperand(int32_t value); diff --git a/src/classfile/classfile.cpp b/src/classfile/classfile.cpp index 754ebab..44dba74 100644 --- a/src/classfile/classfile.cpp +++ b/src/classfile/classfile.cpp @@ -6,45 +6,44 @@ #include "../../include/cvm/log.hpp" -std::string cp_info_to_string(const cp_info& cp) -{ - switch (cp.tag) - { - case CONSTANT_Class: return fmt::format("Class: name_index = {}", cp.info.Class.name_index); - case CONSTANT_String: return fmt::format("String: string_index = {}", cp.info.String.string_index); +std::string cp_info_to_string(const cp_info& cp) { + switch (cp.tag) { + case CONSTANT_Class: + return fmt::format("Class: name_index = {}", cp.info.Class.name_index); + case CONSTANT_String: + return fmt::format("String: string_index = {}", + cp.info.String.string_index); case CONSTANT_Fieldref: - return fmt::format( - "Fieldref: class_index = {}, name_and_type_index = {}", - cp.info.Fieldref.class_index, - cp.info.Fieldref.name_and_type_index); + return fmt::format("Fieldref: class_index = {}, name_and_type_index = {}", + cp.info.Fieldref.class_index, + cp.info.Fieldref.name_and_type_index); case CONSTANT_Methodref: return fmt::format( "Methodref: class_index = {}, name_and_type_index = {}", - cp.info.Methodref.class_index, - cp.info.Methodref.name_and_type_index); + cp.info.Methodref.class_index, cp.info.Methodref.name_and_type_index); case CONSTANT_InterfaceMethodref: return fmt::format( "InterfaceMethodref: class_index = {}, name_and_type_index = {}", cp.info.InterfaceMethodref.class_index, cp.info.InterfaceMethodref.name_and_type_index); - case CONSTANT_Integer: return fmt::format("Integer: bytes = {}", cp.info.Integer.bytes); + case CONSTANT_Integer: + return fmt::format("Integer: bytes = {}", cp.info.Integer.bytes); case CONSTANT_NameAndType: - return fmt::format( - "NameAndType: name_index = {}, descriptor_index = {}", - cp.info.NameAndType.name_index, - cp.info.NameAndType.descriptor_index); - case CONSTANT_Double: return fmt::format("Double: bytes = {}", cp.info.Double.bytes); + return fmt::format("NameAndType: name_index = {}, descriptor_index = {}", + cp.info.NameAndType.name_index, + cp.info.NameAndType.descriptor_index); + case CONSTANT_Double: + return fmt::format("Double: bytes = {}", cp.info.Double.bytes); case CONSTANT_Utf8: - return fmt::format( - "Utf8: length = {}, bytes = {}", - cp.info.Utf8.length, - std::string(cp.info.Utf8.bytes, cp.info.Utf8.bytes + cp.info.Utf8.length)); - default: return "Unknown cp_info tag"; + return fmt::format("Utf8: length = {}, bytes = {}", cp.info.Utf8.length, + std::string(cp.info.Utf8.bytes, + cp.info.Utf8.bytes + cp.info.Utf8.length)); + default: + return "Unknown cp_info tag"; } } -uint16_t Classfile::readShort(const uint8_t* bytes, size_t& offset) -{ +uint16_t Classfile::readShort(const uint8_t* bytes, size_t& offset) { setLevel(); spdlog::debug("Reading short at offset: {}", offset); uint16_t value = (bytes[offset] << 8 | bytes[offset + 1]); @@ -53,31 +52,33 @@ uint16_t Classfile::readShort(const uint8_t* bytes, size_t& offset) return value; } -uint32_t Classfile::readInt(const uint8_t* bytes, size_t& offset) -{ +uint32_t Classfile::readInt(const uint8_t* bytes, size_t& offset) { setLevel(); spdlog::debug("Reading int at offset: {}", offset); - uint32_t value = (bytes[offset] << 24) | (bytes[offset + 1] << 16) | (bytes[offset + 2] << 8) | bytes[offset + 3]; + uint32_t value = (bytes[offset] << 24) | (bytes[offset + 1] << 16) | + (bytes[offset + 2] << 8) | bytes[offset + 3]; offset += 4; spdlog::debug("Read int: 0x{:08x}, new offset: {}", value, offset); return value; } -uint64_t Classfile::readLong(const uint8_t* bytes, size_t& offset) -{ +uint64_t Classfile::readLong(const uint8_t* bytes, size_t& offset) { setLevel(); spdlog::debug("Reading long at offset: {}", offset); - uint64_t value = (static_cast(bytes[offset]) << 56) | (static_cast(bytes[offset + 1]) << 48) | - (static_cast(bytes[offset + 2]) << 40) | (static_cast(bytes[offset + 3]) << 32) | - (static_cast(bytes[offset + 4]) << 24) | (static_cast(bytes[offset + 5]) << 16) | - (static_cast(bytes[offset + 6]) << 8) | static_cast(bytes[offset + 7]); + uint64_t value = (static_cast(bytes[offset]) << 56) | + (static_cast(bytes[offset + 1]) << 48) | + (static_cast(bytes[offset + 2]) << 40) | + (static_cast(bytes[offset + 3]) << 32) | + (static_cast(bytes[offset + 4]) << 24) | + (static_cast(bytes[offset + 5]) << 16) | + (static_cast(bytes[offset + 6]) << 8) | + static_cast(bytes[offset + 7]); offset += 8; spdlog::debug("Read long: 0x{:016x}, new offset: {}", value, offset); return value; } -double Classfile::readDouble(const uint8_t* bytes, size_t& offset) -{ +double Classfile::readDouble(const uint8_t* bytes, size_t& offset) { setLevel(); spdlog::debug("Reading double at offset: {}", offset); uint64_t longValue = readLong(bytes, offset); @@ -87,35 +88,30 @@ double Classfile::readDouble(const uint8_t* bytes, size_t& offset) return value; } -std::string Classfile::to_string() const -{ +std::string Classfile::to_string() const { std::string cp_str; - for (size_t i = 0; i < constant_pool.size(); ++i) - { - cp_str += fmt::format(" [{:2}] {}\n", i + 1, cp_info_to_string(constant_pool.at(i))); + for (size_t i = 0; i < constant_pool.size(); ++i) { + cp_str += fmt::format(" [{:2}] {}\n", i + 1, + cp_info_to_string(constant_pool.at(i))); } std::string intf_str; - for (const auto& intf : interfaces) - { + for (const auto& intf : interfaces) { intf_str += fmt::format(" 0x{:04x}\n", intf); } std::string fields_str; - for (const auto& field : fields) - { + for (const auto& field : fields) { fields_str += fmt::format(" {}\n", field.to_string()); } std::string methods_str; - for (const auto& method : methods) - { + for (const auto& method : methods) { methods_str += fmt::format(" {}\n", method.to_string()); } std::string attributes_str; - for (const auto& attr : attributes) - { + for (const auto& attr : attributes) { attributes_str += fmt::format(" {}\n", attr.to_string()); } @@ -137,58 +133,47 @@ std::string Classfile::to_string() const " Methods:\n [\n{}\n ]\n" " Attributes Count: {}\n" " Attributes:\n [\n{}\n ]\n", - magic, - minor_version, - major_version, - constant_pool_count, - cp_str, - access_flags, - this_class, - super_class, - interfaces_count, - intf_str, - fields_count, - fields_str, - methods_count, - methods_str, - attributes_count, + magic, minor_version, major_version, constant_pool_count, cp_str, + access_flags, this_class, super_class, interfaces_count, intf_str, + fields_count, fields_str, methods_count, methods_str, attributes_count, attributes_str); } -std::ostream& operator<<(std::ostream& os, const Classfile& cf) -{ +std::ostream& operator<<(std::ostream& os, const Classfile& cf) { return os << cf.to_string(); } /** * Helper methods for validating etc. */ -bool validateMagicNumber(uint32_t magic) -{ +bool validateMagicNumber(uint32_t magic) { return magic == 0xcafebabe; } -void parseConstantPool(const uint8_t* bytes, size_t& offset, Classfile& cf) -{ +void parseConstantPool(const uint8_t* bytes, size_t& offset, Classfile& cf) { cf.constant_pool.resize(cf.constant_pool_count - 1); spdlog::info("Constant pool size: {}", cf.constant_pool_count - 1); - for (uint16_t i = 1; i < cf.constant_pool_count; i++) - { + for (uint16_t i = 1; i < cf.constant_pool_count; i++) { cp_info& cp_entry = cf.constant_pool[i - 1]; cp_entry.tag = bytes[offset++]; - spdlog::info("Parsing constant pool entry {}, tag: {}, offset: {}", i, cp_entry.tag, offset); + spdlog::info("Parsing constant pool entry {}, tag: {}, offset: {}", i, + cp_entry.tag, offset); - switch (cp_entry.tag) - { - case CONSTANT_Class: cp_entry.info.Class.name_index = cf.readShort(bytes, offset); break; + switch (cp_entry.tag) { + case CONSTANT_Class: + cp_entry.info.Class.name_index = cf.readShort(bytes, offset); + break; case CONSTANT_Fieldref: case CONSTANT_Methodref: case CONSTANT_InterfaceMethodref: cp_entry.info.Fieldref.class_index = cf.readShort(bytes, offset); - cp_entry.info.Fieldref.name_and_type_index = cf.readShort(bytes, offset); + cp_entry.info.Fieldref.name_and_type_index = + cf.readShort(bytes, offset); + break; + case CONSTANT_String: + cp_entry.info.String.string_index = cf.readShort(bytes, offset); break; - case CONSTANT_String: cp_entry.info.String.string_index = cf.readShort(bytes, offset); break; case CONSTANT_Integer: cp_entry.info.Integer.bytes = cf.readInt(bytes, offset); break; @@ -207,13 +192,15 @@ void parseConstantPool(const uint8_t* bytes, size_t& offset, Classfile& cf) break; case CONSTANT_NameAndType: cp_entry.info.NameAndType.name_index = cf.readShort(bytes, offset); - cp_entry.info.NameAndType.descriptor_index = cf.readShort(bytes, offset); + cp_entry.info.NameAndType.descriptor_index = + cf.readShort(bytes, offset); break; case CONSTANT_Utf8: cp_entry.info.Utf8.length = cf.readShort(bytes, offset); cp_entry.info.Utf8.bytes = new uint8_t[cp_entry.info.Utf8.length]; - std::memcpy(cp_entry.info.Utf8.bytes, &bytes[offset], cp_entry.info.Utf8.length); + std::memcpy(cp_entry.info.Utf8.bytes, &bytes[offset], + cp_entry.info.Utf8.length); offset += cp_entry.info.Utf8.length; break; /* @@ -230,64 +217,64 @@ void parseConstantPool(const uint8_t* bytes, size_t& offset, Classfile& cf) break; */ default: - spdlog::error("Unsupported Constant Pool Tag: {}! At location i: {})", cp_entry.tag, i); + spdlog::error("Unsupported Constant Pool Tag: {}! At location i: {})", + cp_entry.tag, i); offset += 2; break; } } } -void parseFields(const uint8_t* bytes, size_t& offset, Classfile& cf) -{ +void parseFields(const uint8_t* bytes, size_t& offset, Classfile& cf) { cf.fields.resize(cf.fields_count); - for (size_t i = 0; i < cf.fields_count; ++i) - { + for (size_t i = 0; i < cf.fields_count; ++i) { cf.fields[i].access_flags = cf.readShort(bytes, offset); cf.fields[i].name_index = cf.readShort(bytes, offset); cf.fields[i].descriptor_index = cf.readShort(bytes, offset); cf.fields[i].attributes_count = cf.readShort(bytes, offset); cf.fields[i].attributes.resize(cf.fields[i].attributes_count); - for (size_t j = 0; j < cf.fields[i].attributes_count; ++j) - { - cf.fields[i].attributes[j].attribute_name_index = cf.readShort(bytes, offset); + for (size_t j = 0; j < cf.fields[i].attributes_count; ++j) { + cf.fields[i].attributes[j].attribute_name_index = + cf.readShort(bytes, offset); cf.fields[i].attributes[j].attribute_length = cf.readInt(bytes, offset); - cf.fields[i].attributes[j].info = new uint8_t[cf.fields[i].attributes[j].attribute_length]; - std::memcpy(cf.fields[i].attributes[j].info, &bytes[offset], cf.fields[i].attributes[j].attribute_length); + cf.fields[i].attributes[j].info = + new uint8_t[cf.fields[i].attributes[j].attribute_length]; + std::memcpy(cf.fields[i].attributes[j].info, &bytes[offset], + cf.fields[i].attributes[j].attribute_length); offset += cf.fields[i].attributes[j].attribute_length; } } } -void parseMethods(const uint8_t* bytes, size_t& offset, Classfile& cf) -{ +void parseMethods(const uint8_t* bytes, size_t& offset, Classfile& cf) { cf.methods.resize(cf.methods_count); - for (size_t i = 0; i < cf.methods_count; ++i) - { + for (size_t i = 0; i < cf.methods_count; ++i) { cf.methods[i].access_flags = cf.readShort(bytes, offset); cf.methods[i].name_index = cf.readShort(bytes, offset); cf.methods[i].descriptor_index = cf.readShort(bytes, offset); cf.methods[i].attributes_count = cf.readShort(bytes, offset); cf.methods[i].attributes.resize(cf.methods[i].attributes_count); - for (size_t j = 0; j < cf.methods[i].attributes_count; ++j) - { - cf.methods[i].attributes[j].attribute_name_index = cf.readShort(bytes, offset); + for (size_t j = 0; j < cf.methods[i].attributes_count; ++j) { + cf.methods[i].attributes[j].attribute_name_index = + cf.readShort(bytes, offset); cf.methods[i].attributes[j].attribute_length = cf.readInt(bytes, offset); - cf.methods[i].attributes[j].info = new uint8_t[cf.methods[i].attributes[j].attribute_length]; - std::memcpy(cf.methods[i].attributes[j].info, &bytes[offset], cf.methods[i].attributes[j].attribute_length); + cf.methods[i].attributes[j].info = + new uint8_t[cf.methods[i].attributes[j].attribute_length]; + std::memcpy(cf.methods[i].attributes[j].info, &bytes[offset], + cf.methods[i].attributes[j].attribute_length); offset += cf.methods[i].attributes[j].attribute_length; } } } -void parseAttributes(const uint8_t* bytes, size_t& offset, Classfile& cf) -{ +void parseAttributes(const uint8_t* bytes, size_t& offset, Classfile& cf) { cf.attributes.resize(cf.attributes_count); - for (size_t i = 0; i < cf.attributes_count; ++i) - { + for (size_t i = 0; i < cf.attributes_count; ++i) { cf.attributes[i].attribute_name_index = cf.readShort(bytes, offset); cf.attributes[i].attribute_length = cf.readInt(bytes, offset); cf.attributes[i].info = new uint8_t[cf.attributes[i].attribute_length]; - std::memcpy(cf.attributes[i].info, &bytes[offset], cf.attributes[i].attribute_length); + std::memcpy(cf.attributes[i].info, &bytes[offset], + cf.attributes[i].attribute_length); offset += cf.attributes[i].attribute_length; } } @@ -296,19 +283,16 @@ void parseAttributes(const uint8_t* bytes, size_t& offset, Classfile& cf) * Helper methods ends */ -Classfile Classfile::parseClassfile(const uint8_t* classBytes, size_t fileSize) -{ +Classfile Classfile::parseClassfile(const uint8_t* classBytes, + size_t fileSize) { size_t offset = 0; Classfile cf; // Read magic number cf.magic = readInt(classBytes, offset); - if (!validateMagicNumber(cf.magic)) - { + if (!validateMagicNumber(cf.magic)) { throw std::runtime_error("The magic number of classfile is not valid."); - } - else - { + } else { spdlog::info("{} Magic number validated", LOG_OK); } cf.minor_version = readShort(classBytes, offset); @@ -322,8 +306,7 @@ Classfile Classfile::parseClassfile(const uint8_t* classBytes, size_t fileSize) cf.super_class = readShort(classBytes, offset); cf.interfaces_count = readShort(classBytes, offset); cf.interfaces.resize(cf.interfaces_count); - for (size_t i = 0; i < cf.interfaces_count; ++i) - { + for (size_t i = 0; i < cf.interfaces_count; ++i) { cf.interfaces[i] = readShort(classBytes, offset); } cf.fields_count = readShort(classBytes, offset); @@ -338,17 +321,15 @@ Classfile Classfile::parseClassfile(const uint8_t* classBytes, size_t fileSize) spdlog::info("The Parsed Classfile:\n"); spdlog::info("{}", classFileString); - if (offset == fileSize) - { - spdlog::info("{} Loading classfile has done at offset: {}, for filesize: {}", LOG_OK, offset, fileSize); - } - else - { + if (offset == fileSize) { + spdlog::info( + "{} Loading classfile has done at offset: {}, for filesize: {}", LOG_OK, + offset, fileSize); + } else { spdlog::warn( - "{} While loading classfile, the offset:{} does not match with filesize: {}. The parsing might be wrong!", - LOG_NOK, - offset, - fileSize); + "{} While loading classfile, the offset:{} does not match with " + "filesize: {}. The parsing might be wrong!", + LOG_NOK, offset, fileSize); } return cf; } diff --git a/src/execute_engine/cvm_execute.cpp b/src/execute_engine/cvm_execute.cpp index 50ad202..44331c9 100644 --- a/src/execute_engine/cvm_execute.cpp +++ b/src/execute_engine/cvm_execute.cpp @@ -8,27 +8,21 @@ * interpreting */ -void validateMethod(const method_info* methodInfo, std::string methodName) -{ - if (methodInfo != nullptr) - { - spdlog::info("{} The given [method: {}] has been found on classfile", LOG_OK, methodName); - } - else - { - spdlog::error("{} The given [method: {}] cannot be foudn on classfile", LOG_NOK, methodName); +void validateMethod(const method_info* methodInfo, std::string methodName) { + if (methodInfo != nullptr) { + spdlog::info("{} The given [method: {}] has been found on classfile", + LOG_OK, methodName); + } else { + spdlog::error("{} The given [method: {}] cannot be foudn on classfile", + LOG_NOK, methodName); throw std::runtime_error("Method not found"); } } -void validateByteCode(const uint8_t* byteCode) -{ - if (byteCode != nullptr) - { +void validateByteCode(const uint8_t* byteCode) { + if (byteCode != nullptr) { spdlog::info("{} Bytcode is obtained from given method:", LOG_OK); - } - else - { + } else { throw std::runtime_error("Could not get bytecode for given method"); } } @@ -37,8 +31,7 @@ void validateByteCode(const uint8_t* byteCode) * Helper methods ends */ -void CVM::execute(const Classfile& cf, const std::string& methodName) -{ +void CVM::execute(const Classfile& cf, const std::string& methodName) { spdlog::info("CVM executing method: {} on parsed classfile.", methodName); const method_info* methodInfo = findMehodByName(cf, methodName); validateMethod(methodInfo, methodName); @@ -47,36 +40,32 @@ void CVM::execute(const Classfile& cf, const std::string& methodName) validateByteCode(bytecode); } -std::string CVM::getUtf8FromConstantPool(const Classfile& cf, uint16_t index) -{ +std::string CVM::getUtf8FromConstantPool(const Classfile& cf, uint16_t index) { const cp_info& cpEntry = cf.constant_pool.at(index - 1); - if (cpEntry.tag == CONSTANT_Utf8) - { - return std::string(cpEntry.info.Utf8.bytes, cpEntry.info.Utf8.bytes + cpEntry.info.Utf8.length); + if (cpEntry.tag == CONSTANT_Utf8) { + return std::string(cpEntry.info.Utf8.bytes, + cpEntry.info.Utf8.bytes + cpEntry.info.Utf8.length); } return ""; } -const method_info* CVM::findMehodByName(const Classfile& cf, const std::string& methodName) -{ - for (auto& method : cf.methods) - { +const method_info* CVM::findMehodByName(const Classfile& cf, + const std::string& methodName) { + for (auto& method : cf.methods) { std::string name = getUtf8FromConstantPool(cf, method.name_index); - if (name == methodName) - { + if (name == methodName) { return &method; } } return nullptr; } -const uint8_t* CVM::getByteCode(const Classfile& cf, const method_info* methodInfo) -{ - for (auto& attr : methodInfo->attributes) - { - std::string codeAttribute = getUtf8FromConstantPool(cf, attr.attribute_name_index); - if (codeAttribute == "Code") - { +const uint8_t* CVM::getByteCode(const Classfile& cf, + const method_info* methodInfo) { + for (auto& attr : methodInfo->attributes) { + std::string codeAttribute = + getUtf8FromConstantPool(cf, attr.attribute_name_index); + if (codeAttribute == "Code") { return attr.info; } } diff --git a/src/log.cpp b/src/log.cpp index 5c11b67..6a5cb05 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,9 +1,7 @@ #include "../include/cvm/log.hpp" -void setLevel() -{ - if (DEBUG_ENABLED) - { +void setLevel() { + if (DEBUG_ENABLED) { spdlog::set_level(spdlog::level::debug); } } diff --git a/src/main.cpp b/src/main.cpp index 47f7053..30d4da2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,16 +10,14 @@ int main(int argc, char** argv) { printBanner(); - if (argc < 2) - { + if (argc < 2) { throw std::runtime_error("Please specify .class file path as an argument."); } std::string classFilePath = argv[1]; std::ifstream classFile(classFilePath, std::ios::binary); - if (!classFile) - { + if (!classFile) { throw std::runtime_error("Cannot open class file at specified path."); } @@ -28,8 +26,7 @@ int main(int argc, char** argv) classFile.seekg(0, std::ios::beg); std::vector buffer(fileSize); - if (!classFile.read(reinterpret_cast(buffer.data()), fileSize)) - { + if (!classFile.read(reinterpret_cast(buffer.data()), fileSize)) { throw std::runtime_error("Error while reading class data."); return 1; } diff --git a/src/stack/frame.cpp b/src/stack/frame.cpp index 8ae7518..50e5816 100644 --- a/src/stack/frame.cpp +++ b/src/stack/frame.cpp @@ -1,33 +1,26 @@ #include "../../include/cvm/stack/frame.hpp" -int32_t Frame::getLocalVariable(size_t index) const -{ +int32_t Frame::getLocalVariable(size_t index) const { return localVariables.at(index); } -void Frame::setLocalVariable(size_t index, int32_t value) -{ +void Frame::setLocalVariable(size_t index, int32_t value) { localVariables.at(index) = value; } -void Frame::pushOperand(int32_t value) -{ - if (operandStackSize >= operandStack.size()) - { +void Frame::pushOperand(int32_t value) { + if (operandStackSize >= operandStack.size()) { throw std::overflow_error("Operand stack overflow"); } operandStack[operandStackSize++] = value; } -int32_t Frame::popOperand() -{ - if (operandStackSize == 0) - { +int32_t Frame::popOperand() { + if (operandStackSize == 0) { throw std::underflow_error("Operand stack underflow"); } return operandStack[--operandStackSize]; } -size_t Frame::getOperandStackSize() -{ +size_t Frame::getOperandStackSize() { return operandStackSize; }