Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yuhanun feature/bytecode magicnum #81

Merged
merged 7 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion external/pegmatite
Submodule pegmatite updated 1 files
+6 −0 CMakeLists.txt
11 changes: 11 additions & 0 deletions src/compiler/codegen/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ namespace verona::compiler
return std::make_pair(class_item, method_item);
}

/**
* Writes the magic numbers to the bytecode
* @param code Generator to which the bytes should be emitted
*/
void write_magic_number(Generator& code)
{
code.u32(bytecode::MAGIC_NUMBER);
}

std::vector<uint8_t> codegen(
Context& context, const Program& program, const AnalysisResults& analysis)
{
Expand All @@ -100,7 +109,9 @@ namespace verona::compiler
return {};

std::vector<uint8_t> code;

Generator gen(code);
write_magic_number(gen);

Reachability reachability = compute_reachability(
context, program, gen, entry->first, entry->second, analysis);
Expand Down
3 changes: 3 additions & 0 deletions src/interpreter/bytecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Bytecode has the following layout:
*
* Program header:
* - 32-bit verona magic number, equal to the MAGIC_NUMBER constant
* - 32-bit number of descriptors, followed by that many descriptors (see below)
* - 32-bit descriptor index of Main class
* - 32-bit selector index of main method
Expand Down Expand Up @@ -131,6 +132,8 @@ namespace verona::bytecode
uint32_t size;
};

constexpr static uint32_t MAGIC_NUMBER = 0xF38932C3;

/**
* Type-safe wrapper for register indices, helps avoid implicit conversion
* from/to integers.
Expand Down
15 changes: 14 additions & 1 deletion src/interpreter/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ namespace verona::interpreter
{
return load<int16_t>(ip);
}
uint16_t u32(size_t& ip) const

uint32_t u32(size_t& ip) const
{
return load<uint32_t>(ip);
}

uint64_t u64(size_t& ip) const
{
return load<uint64_t>(ip);
Expand Down Expand Up @@ -137,6 +139,8 @@ namespace verona::interpreter
{
size_t ip = 0;

check_verona_nums(ip);

uint32_t descriptors_count = u32(ip);
for (uint32_t i = 0; i < descriptors_count; i++)
{
Expand Down Expand Up @@ -190,6 +194,15 @@ namespace verona::interpreter

SpecialDescriptors special_descriptors_;

void check_verona_nums(size_t& ip)
{
uint32_t nums = u32(ip);
if (nums != bytecode::MAGIC_NUMBER)
{
throw std::logic_error{"Invalid magic number, not recognized"};
}
}

std::unique_ptr<VMDescriptor> load_descriptor(size_t& ip)
{
std::string_view name = str(ip);
Expand Down