Skip to content

Commit

Permalink
feature(compiler): produce a developer map, mapping instruction opcod…
Browse files Browse the repository at this point in the history
…e pos to source line number
  • Loading branch information
ineed bots committed Dec 27, 2023
1 parent cbfcce1 commit 7f777b5
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/xsk/arc/assembler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class assembler
function const* func_;
assembly const* assembly_;
utils::writer script_;
utils::writer dev_map_;
std::unordered_map<std::string, u16> strpool_;
std::vector<export_ref> exports_;
std::vector<import_ref> imports_;
Expand All @@ -26,6 +27,7 @@ class assembler
public:
assembler(context const* ctx);
auto assemble(assembly const& data, std::string const& name = {}) -> buffer;
auto get_dev_map() -> buffer;

private:
auto assemble_function(function& func) -> void;
Expand Down
1 change: 1 addition & 0 deletions include/xsk/arc/common/assembly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ struct instruction
u32 size;
opcode opcode;
std::vector<std::string> data;
std::optional<position> pos;

static auto make() -> instruction::ptr
{
Expand Down
2 changes: 1 addition & 1 deletion include/xsk/arc/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#pragma once

#include "xsk/arc/common/asset.hpp"
#include "xsk/arc/common/location.hpp"
#include "xsk/arc/common/assembly.hpp"
#include "xsk/arc/common/buffer.hpp"
#include "xsk/arc/common/location.hpp"
#include "xsk/arc/common/exception.hpp"
#include "xsk/arc/common/lookahead.hpp"
#include "xsk/arc/common/directive.hpp"
Expand Down
1 change: 1 addition & 0 deletions include/xsk/arc/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class compiler
bool can_break_;
bool can_continue_;
bool developer_thread_;
std::optional<position> debug_pos_;

public:
compiler(context* ctx);
Expand Down
2 changes: 2 additions & 0 deletions include/xsk/gsc/assembler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ class assembler
assembly const* assembly_;
utils::writer script_;
utils::writer stack_;
utils::writer dev_map_;

public:
assembler(context const* ctx);
auto assemble(assembly const& data) -> std::pair<buffer, buffer>;
auto get_dev_map() -> buffer;

private:
auto assemble_function(function const& func) -> void;
Expand Down
1 change: 1 addition & 0 deletions include/xsk/gsc/common/assembly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ struct instruction
u32 size;
opcode opcode;
std::vector<std::string> data;
std::optional<position> pos;

static auto make() -> instruction::ptr
{
Expand Down
2 changes: 1 addition & 1 deletion include/xsk/gsc/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "xsk/gsc/common/asset.hpp"
#include "xsk/gsc/common/scope.hpp"
#include "xsk/gsc/common/buffer.hpp"
#include "xsk/gsc/common/assembly.hpp"
#include "xsk/gsc/common/location.hpp"
#include "xsk/gsc/common/assembly.hpp"
#include "xsk/gsc/common/exception.hpp"
#include "xsk/gsc/common/lookahead.hpp"
#include "xsk/gsc/common/directive.hpp"
Expand Down
1 change: 1 addition & 0 deletions include/xsk/gsc/compiler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class compiler
bool can_continue_;
bool developer_thread_;
bool animload_;
std::optional<position> debug_pos_;

public:
compiler(context* ctx);
Expand Down
13 changes: 13 additions & 0 deletions src/arc/assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ auto assembler::assemble(assembly const& data, std::string const& name) -> buffe
{
assembly_ = &data;
script_.clear();
dev_map_.clear();
strpool_.clear();
exports_.clear();
imports_.clear();
Expand Down Expand Up @@ -226,6 +227,11 @@ auto assembler::assemble(assembly const& data, std::string const& name) -> buffe
return buffer{ script_.data(), script_.pos() };
}

auto assembler::get_dev_map() -> buffer
{
return buffer{ dev_map_.data(), dev_map_.pos() };
}

auto assembler::assemble_function(function& func) -> void
{
auto labels = std::unordered_map<u32, std::string>();
Expand Down Expand Up @@ -270,6 +276,13 @@ auto assembler::assemble_function(function& func) -> void

auto assembler::assemble_instruction(instruction const& inst) -> void
{
if (inst.pos.has_value())
{
dev_map_.write<u32>(script_.pos());
dev_map_.write<position::counter_type>(inst.pos->line);
dev_map_.write<position::counter_type>(inst.pos->column);
}

script_.write<u8>(static_cast<u8>(ctx_->opcode_id(inst.opcode)));

switch (inst.opcode)
Expand Down
19 changes: 19 additions & 0 deletions src/arc/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ auto compiler::emit_decl_function(decl_function const& func) -> void

auto compiler::emit_stmt(stmt const& stm) -> void
{
debug_pos_ = stm.loc().begin;

switch (stm.kind())
{
case node::stmt_list:
Expand Down Expand Up @@ -686,6 +688,8 @@ auto compiler::emit_stmt_prof_end(stmt_prof_end const&) -> void

auto compiler::emit_expr(expr const& exp) -> void
{
debug_pos_ = exp.loc().begin;

switch (exp.kind())
{
case node::expr_paren:
Expand Down Expand Up @@ -1867,6 +1871,11 @@ auto compiler::emit_opcode(opcode op) -> void
inst->size = ctx_->opcode_size(op);
inst->index = index_;

if (debug_pos_.has_value())
{
inst->pos = debug_pos_;
}

index_ += inst->size;
}

Expand All @@ -1880,6 +1889,11 @@ auto compiler::emit_opcode(opcode op, std::string const& data) -> void
inst->index = index_;
inst->data.push_back(data);

if (debug_pos_.has_value())
{
inst->pos = debug_pos_;
}

index_ += inst->size;
}

Expand All @@ -1893,6 +1907,11 @@ auto compiler::emit_opcode(opcode op, std::vector<std::string> const& data) -> v
inst->index = index_;
inst->data = data;

if (debug_pos_.has_value())
{
inst->pos = debug_pos_;
}

index_ += inst->size;
}

Expand Down
13 changes: 13 additions & 0 deletions src/gsc/assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ auto assembler::assemble(assembly const& data) -> std::pair<buffer, buffer>
assembly_ = &data;
script_.clear();
stack_.clear();
dev_map_.clear();

script_.write<u8>(ctx_->opcode_id(opcode::OP_End));

Expand All @@ -30,6 +31,11 @@ auto assembler::assemble(assembly const& data) -> std::pair<buffer, buffer>
return { buffer{ script_.data(), script_.pos() }, buffer{ stack_.data(), stack_.pos() } };
}

auto assembler::get_dev_map() -> buffer
{
return buffer{ dev_map_.data(), dev_map_.pos() };
}

auto assembler::assemble_function(function const& func) -> void
{
func_ = &func;
Expand Down Expand Up @@ -61,6 +67,13 @@ auto assembler::assemble_function(function const& func) -> void

auto assembler::assemble_instruction(instruction const& inst) -> void
{
if (inst.pos.has_value())
{
dev_map_.write<u32>(script_.pos());
dev_map_.write<position::counter_type>(inst.pos->line);
dev_map_.write<position::counter_type>(inst.pos->column);
}

script_.write<u8>(ctx_->opcode_id(inst.opcode));

switch (inst.opcode)
Expand Down
19 changes: 19 additions & 0 deletions src/gsc/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ auto compiler::emit_decl_function(decl_function const& func) -> void

auto compiler::emit_stmt(stmt const& stm, scope& scp, bool last) -> void
{
debug_pos_ = stm.loc().begin;

switch (stm.kind())
{
case node::stmt_list:
Expand Down Expand Up @@ -925,6 +927,8 @@ auto compiler::emit_stmt_assertmsg(stmt_assertmsg const&, scope&) -> void

auto compiler::emit_expr(expr const& exp, scope& scp) -> void
{
debug_pos_ = exp.loc().begin;

switch (exp.kind())
{
case node::expr_paren:
Expand Down Expand Up @@ -2198,6 +2202,11 @@ auto compiler::emit_opcode(opcode op) -> void
inst->size = ctx_->opcode_size(op);
inst->index = index_;

if (debug_pos_.has_value())
{
inst->pos = debug_pos_;
}

index_ += inst->size;
}

Expand All @@ -2211,6 +2220,11 @@ auto compiler::emit_opcode(opcode op, std::string const& data) -> void
inst->index = index_;
inst->data.push_back(data);

if (debug_pos_.has_value())
{
inst->pos = debug_pos_;
}

index_ += inst->size;
}

Expand All @@ -2224,6 +2238,11 @@ auto compiler::emit_opcode(opcode op, std::vector<std::string> const& data) -> v
inst->index = index_;
inst->data = data;

if (debug_pos_.has_value())
{
inst->pos = debug_pos_;
}

index_ += inst->size;
}

Expand Down
14 changes: 14 additions & 0 deletions src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ auto compile_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto result = script.serialize();
utils::file::save(fs::path{ "compiled" } / rel, result);
std::cout << fmt::format("compiled {}\n", rel.generic_string());

auto dev_map = contexts[game][mach]->assembler().get_dev_map();
if (dev_map.size > 0)
{
utils::file::save(fs::path{ "compiled" } / fs::path{ "developer_maps" } / rel, dev_map.data, dev_map.size);
std::cout << fmt::format("wrote developer map {}\n", rel.generic_string());
}
}
}
}
Expand Down Expand Up @@ -753,6 +760,13 @@ void compile_file(game game, mach mach, fs::path const& file, fs::path rel)

utils::file::save(fs::path{ "compiled" } / rel, outbin.data, outbin.size);
std::cout << fmt::format("compiled {}\n", rel.generic_string());

auto dev_map = contexts[game][mach]->assembler().get_dev_map();
if (dev_map.size > 0)
{
utils::file::save(fs::path{ "compiled" } / fs::path{ "developer_maps" } / rel, dev_map.data, dev_map.size);
std::cout << fmt::format("wrote developer map {}\n", rel.generic_string());
}
}
catch (std::exception const& e)
{
Expand Down

0 comments on commit 7f777b5

Please sign in to comment.