Skip to content

Commit

Permalink
cleanup disassembler
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik committed Nov 9, 2024
1 parent bf397fe commit 5197cbe
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 414 deletions.
16 changes: 9 additions & 7 deletions include/xsk/gsc/disassembler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ class disassembler
private:
auto dissasemble_function(function& func) -> void;
auto dissasemble_instruction(instruction& inst) -> void;
auto disassemble_builtin_call(instruction& inst, bool method, bool args) -> void;
auto disassemble_local_call(instruction& inst, bool thread) -> void;
auto disassemble_far_call(instruction& inst, bool thread) -> void;
auto disassemble_switch(instruction& inst) -> void;
auto disassemble_end_switch(instruction& inst) -> void;
auto disassemble_field_variable(instruction& inst) -> void;
auto disassemble_formal_params(instruction& inst) -> void;
auto disassemble_field(instruction& inst) -> void;
auto disassemble_params(instruction& inst) -> void;
auto disassemble_call_far(instruction& inst, bool thread) -> void;
auto disassemble_call_far2(instruction& inst, bool thread) -> void;
auto disassemble_call_local(instruction& inst, bool thread) -> void;
auto disassemble_call_builtin(instruction& inst, bool method, bool args) -> void;
auto disassemble_call_builtin2(instruction& inst, bool method, bool args) -> void;
auto disassemble_jump(instruction& inst, bool expr, bool back) -> void;
auto disassemble_switch(instruction& inst) -> void;
auto disassemble_switch_table(instruction& inst) -> void;
auto disassemble_offset() -> i32;
auto resolve_functions() -> void;
auto resolve_function(std::string const& index) -> std::string;
Expand Down
2 changes: 1 addition & 1 deletion include/xsk/gsc/preprocessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class preprocessor
u32 skip_;

public:
preprocessor(context* ctx, std::string const& name, char const* data, usize size);
preprocessor(context* ctx, std::string const& name, u8 const* data, usize size);
auto process() -> token;
auto push_header(std::string const& file) -> void;
auto pop_header() -> void;
Expand Down
2 changes: 1 addition & 1 deletion include/xsk/gsc/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class source
{
context* ctx_;
std::vector<u8> buf_;
u32 indent_;
u32 indent_ = 0;

public:
source(context* ctx);
Expand Down
13 changes: 7 additions & 6 deletions include/xsk/utils/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@ struct reader

private:
u8 const* data_;
u32 size_;
u32 pos_ = 0;
usize size_;
usize pos_ = 0;
bool swap_;

public:
explicit reader(bool swap = false);
reader(std::vector<u8> const& data, bool swap = false);
reader(u8 const* data, u32 size, bool swap = false);
reader(u8 const* data, usize size, bool swap = false);
template <typename T>
auto read() -> T;
auto read_i24() -> i32;
auto read_cstr() -> std::string;
auto read_bytes(u32 pos, u32 count) -> std::string;
auto is_avail() const -> bool;
auto seek(u32 size) -> void;
auto seek_neg(u32 size) -> void;
auto align(u32 size) -> u32;
auto data() const -> u8 const*;
auto size() const -> u32;
auto pos() const -> u32;
auto pos(u32 pos) -> void;
auto size() const -> usize;
auto pos() const -> usize;
auto pos(usize pos) -> void;
};

} // namespace xsk::utils
2 changes: 1 addition & 1 deletion include/xsk/utils/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct string
static auto quote(std::string const& str, bool single = true) -> std::string;
static auto unquote(std::string const& str) -> std::string;
static auto split(std::string& str, char delimiter) -> std::vector<std::string>;
static auto clean_buffer_lines(std::vector<u8>& buffer) -> std::vector<std::string>;
static auto clean_buffer_lines(u8 const* data, usize size) -> std::vector<std::string>;
static auto parse_code(std::string& line) -> std::vector<std::string>;
static auto float_string(float value, bool toint = false) -> std::string;
};
Expand Down
68 changes: 25 additions & 43 deletions src/gsc/decompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2248,55 +2248,37 @@ auto decompiler::decompile_foreach(stmt_list& stm, usize begin, usize end) -> vo
auto decompiler::decompile_switch(stmt_list& stm, usize begin, usize end) -> void
{
auto const& data = stm.list[end]->as<stmt_jmp_endswitch>().data;
auto const count = std::stoul(data[0]);
auto count = std::stoul(data[0]);
auto index = 1u;

if (count)
for (auto i = 0u; i < count; i++)
{
auto type = static_cast<switch_type>(std::stoul(data.back()));
auto index = 1u;

for (auto i = 0u; i < count; i++)
if (data[index] == "case")
{
if (data[index] == "case")
{
if (ctx_->engine() == engine::iw9)
{
type = static_cast<switch_type>(std::stoul(data[index + 1]));
auto j = find_location_index(stm, data[index + 3]);
auto loc = stm.list[j]->loc();
auto exp = (type == switch_type::integer) ? expr::ptr{ expr_integer::make(loc, data[index + 2]) } : expr::ptr{ expr_string::make(loc, data[index + 2]) };
while (stm.list[j]->is<stmt_case>()) j++;
stm.list.insert(stm.list.begin() + j, stmt_case::make(loc, std::move(exp), stmt_list::make(loc)));
index += 4;
}
else
{
auto j = find_location_index(stm, data[index + 2]);
auto loc = stm.list[j]->loc();
auto exp = (type == switch_type::integer) ? expr::ptr{ expr_integer::make(loc, data[index + 1]) } : expr::ptr{ expr_string::make(loc, data[index + 1]) };
while (stm.list[j]->is<stmt_case>()) j++;
stm.list.insert(stm.list.begin() + j, stmt_case::make(loc, std::move(exp), stmt_list::make(loc)));
index += 3;
}

}
else if (data[index] == "default")
{
auto j = find_location_index(stm, data[index + 1]);
auto loc = stm.list[j]->loc();
while (stm.list[j]->is<stmt_case>()) j++;
stm.list.insert(stm.list.begin() + j, stmt_default::make(loc, stmt_list::make(loc)));
index += 2;
}
else
{
decomp_error("malformed endswitch statement");
}
auto type = static_cast<switch_type>(std::stoul(data[index + 1]));
auto pos = find_location_index(stm, data[index + 3]);
auto loc = stm.list[pos]->loc();
auto exp = (type == switch_type::integer) ? expr::ptr{ expr_integer::make(loc, data[index + 2]) } : expr::ptr{ expr_string::make(loc, data[index + 2]) };
while (stm.list[pos]->is<stmt_case>()) pos++;
stm.list.insert(stm.list.begin() + pos, stmt_case::make(loc, std::move(exp), stmt_list::make(loc)));
index += 4;
}
else if (data[index] == "default")
{
auto pos = find_location_index(stm, data[index + 1]);
auto loc = stm.list[pos]->loc();
while (stm.list[pos]->is<stmt_case>()) pos++;
stm.list.insert(stm.list.begin() + pos, stmt_default::make(loc, stmt_list::make(loc)));
index += 2;
}
else
{
decomp_error("malformed endswitch statement");
}

end += count;
}

end += count;

auto save = locs_;
locs_.last = false;
locs_.brk = last_location_index(stm, end) ? locs_.end : stm.list[end + 1]->label();
Expand Down
Loading

0 comments on commit 5197cbe

Please sign in to comment.