This commit is contained in:
xensik
2022-09-16 18:05:58 +02:00
parent 7cc15969db
commit f91a2552b3
98 changed files with 358 additions and 1404 deletions

View File

@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector<std::uint8_t>& dat
{
auto inst = std::make_unique<instruction>();
inst->index = index;
inst->opcode = static_cast<std::uint8_t>(resolver::opcode_id(opdata[0]));
inst->opcode = resolver::opcode_id(opdata[0]);
inst->size = opcode_size(inst->opcode);
opdata.erase(opdata.begin());
inst->data = std::move(opdata);
switch (opcode(inst->opcode))
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_GetLocalFunction:
case opcode::OP_ScriptLocalFunctionCall:
@ -158,7 +158,9 @@ void assembler::assemble_function(const function::ptr& func)
void assembler::assemble_instruction(const instruction::ptr& inst)
{
switch (opcode(inst->opcode))
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_Return:
case opcode::OP_BoolNot:
@ -230,52 +232,42 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_AddArray:
case opcode::OP_waittillmatch2:
case opcode::OP_shift_right:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
break;
case opcode::OP_GetByte:
case opcode::OP_GetNegByte:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break;
case opcode::OP_GetUnsignedShort:
case opcode::OP_GetNegUnsignedShort:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint16_t>(static_cast<std::uint16_t>(std::stoi(inst->data[0])));
break;
case opcode::OP_GetInteger:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::int32_t>(std::stoi(inst->data[0]));
break;
case opcode::OP_GetFloat:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<float>(std::stof(inst->data[0]));
break;
case opcode::OP_GetVector:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<float>(std::stof(inst->data[0]));
script_->write<float>(std::stof(inst->data[1]));
script_->write<float>(std::stof(inst->data[2]));
break;
case opcode::OP_GetString:
case opcode::OP_GetIString:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint32_t>(0);
stack_->write_c_string(inst->data[0]);
break;
case opcode::OP_GetAnimation:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint32_t>(0);
script_->write<std::uint32_t>(0);
stack_->write_c_string(inst->data[0]);
stack_->write_c_string(inst->data[1]);
break;
case opcode::OP_GetAnimTree:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(0);
stack_->write_c_string(inst->data[0]);
break;
case opcode::OP_waittillmatch:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break;
case opcode::OP_SetNewLocalVariableFieldCached0:
@ -292,7 +284,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_CreateLocalVariable:
case opcode::OP_EvalLocalVariableObjectCached:
case opcode::OP_EvalLocalArrayCached:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break;
case opcode::OP_EvalSelfFieldVariable:
@ -315,7 +306,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_ScriptChildThreadCallPointer:
case opcode::OP_ScriptMethodThreadCallPointer:
case opcode::OP_ScriptMethodChildThreadCallPointer:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break;
case opcode::OP_GetLocalFunction:
@ -391,8 +381,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
if (args)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[1])));
@ -405,8 +393,6 @@ void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method,
void assembler::assemble_local_call(const instruction::ptr& inst, bool thread)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
const auto addr = resolve_function(inst->data[0]);
const auto offset = static_cast<std::int32_t>(addr - inst->index - 1);
@ -420,7 +406,6 @@ void assembler::assemble_local_call(const instruction::ptr& inst, bool thread)
void assembler::assemble_far_call(const instruction::ptr& inst, bool thread)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(0);
script_->write<std::uint16_t>(0);
@ -440,8 +425,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread)
void assembler::assemble_switch(const instruction::ptr& inst)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
const auto addr = resolve_label(inst->data[0]);
script_->write<std::int32_t>(addr - inst->index - 4);
@ -449,8 +432,6 @@ void assembler::assemble_switch(const instruction::ptr& inst)
void assembler::assemble_end_switch(const instruction::ptr& inst)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
const auto count = std::stoul(inst->data[0]);
script_->write<std::uint16_t>(static_cast<std::uint16_t>(count));
@ -501,8 +482,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst)
void assembler::assemble_field_variable(const instruction::ptr& inst)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
auto id = resolver::token_id(inst->data[0]);
if (id == 0) id = 0xFFFF;
@ -518,8 +497,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst)
void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back)
{
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
const auto addr = resolve_label(inst->data[0]);
if (expr)

View File

@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector<std::uint8_t>& data)
{
filename_ = file;
auto prog = parse_buffer(filename_, reinterpret_cast<char*>(data.data()), data.size());
auto prog = parse_buffer(filename_, reinterpret_cast<const char*>(data.data()), data.size());
compile_program(prog);
}
@ -30,7 +30,7 @@ void compiler::mode(build mode)
mode_ = mode;
}
auto compiler::parse_buffer(const std::string& file, char* data, size_t size) -> ast::program::ptr
auto compiler::parse_buffer(const std::string& file, const char* data, size_t size) -> ast::program::ptr
{
ast::program::ptr result(nullptr);
@ -49,9 +49,8 @@ auto compiler::parse_buffer(const std::string& file, char* data, size_t size) ->
auto compiler::parse_file(const std::string& file) -> ast::program::ptr
{
auto data = resolver::file_data(file);
auto result = parse_buffer(file, std::get<1>(data), std::get<2>(data));
return result;
return parse_buffer(file, std::get<1>(data), std::get<2>(data));
}
void compiler::compile_program(const ast::program::ptr& program)
@ -1884,8 +1883,7 @@ void compiler::emit_expr_local(const ast::expr_identifier::ptr& expr, const bloc
if (itr != constants_.end())
{
const auto& value = itr->second;
emit_expr(value, blk);
emit_expr(itr->second, blk);
return;
}
@ -2940,7 +2938,7 @@ void compiler::insert_label(const std::string& name)
{
for (auto& inst : function_->instructions)
{
switch (opcode(inst->opcode))
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_JumpOnFalse:
case opcode::OP_JumpOnTrue:

View File

@ -36,7 +36,7 @@ public:
void mode(build mode);
private:
auto parse_buffer(const std::string& file, char* data, size_t size) -> ast::program::ptr;
auto parse_buffer(const std::string& file, const char* data, size_t size) -> ast::program::ptr;
auto parse_file(const std::string& file) -> ast::program::ptr;
void compile_program(const ast::program::ptr& program);
void emit_include(const ast::include::ptr& include);

View File

@ -61,7 +61,7 @@ void decompiler::decompile_function(const function::ptr& func)
if (stack_.size() > 0)
{
throw decomp_error("stack isn't emty at function end");
throw decomp_error("stack isn't empty at function end");
}
const auto& stmt = func_->stmt;
@ -85,7 +85,7 @@ void decompiler::decompile_instruction(const instruction::ptr& inst)
auto loc = location(&filename_, inst->index);
switch (opcode(inst->opcode))
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_End:
{
@ -1828,7 +1828,7 @@ void decompiler::decompile_ifelses(const ast::stmt_list::ptr& stmt)
{
for (auto i = 0u; i < stmt->list.size(); i++)
{
auto& entry = stmt->list.at(i);
const auto& entry = stmt->list.at(i);
if (entry == ast::kind::asm_jump_cond)
{

View File

@ -83,7 +83,7 @@ void disassembler::dissasemble_function(const function::ptr& func)
void disassembler::dissasemble_instruction(const instruction::ptr& inst)
{
switch (opcode(inst->opcode))
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_Return:
case opcode::OP_BoolNot:
@ -460,7 +460,7 @@ void disassembler::resolve_local_functions()
{
for (const auto& inst : func->instructions)
{
switch (opcode(inst->opcode))
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_GetLocalFunction:
case opcode::OP_ScriptLocalFunctionCall:
@ -485,7 +485,7 @@ auto disassembler::resolve_function(const std::string& index) -> std::string
{
std::uint32_t idx = std::stoul(index, nullptr, 16);
for (auto& func : functions_)
for (const auto& func : functions_)
{
if (func->index == idx)
{
@ -523,7 +523,7 @@ void disassembler::print_instruction(const instruction::ptr& inst)
{
output_->write_string(utils::string::va("\t\t%s", resolver::opcode_name(inst->opcode).data()));
switch (opcode(inst->opcode))
switch (static_cast<opcode>(inst->opcode))
{
case opcode::OP_GetLocalFunction:
case opcode::OP_ScriptLocalFunctionCall:
@ -535,8 +535,7 @@ void disassembler::print_instruction(const instruction::ptr& inst)
case opcode::OP_ScriptLocalChildThreadCall:
case opcode::OP_ScriptLocalMethodThreadCall:
case opcode::OP_ScriptLocalMethodChildThreadCall:
output_->write_string(utils::string::va(" sub_%s", inst->data[0].data()));
output_->write_string(utils::string::va(" %s", inst->data[1].data()));
output_->write_string(utils::string::va(" sub_%s %s\n", inst->data[0].data(), inst->data[1].data()));
break;
case opcode::OP_endswitch:
output_->write_string(utils::string::va(" %s\n", inst->data[0].data()));
@ -563,9 +562,9 @@ void disassembler::print_instruction(const instruction::ptr& inst)
}
break;
default:
for (auto& d : inst->data)
for (auto& data : inst->data)
{
output_->write_string(utils::string::va(" %s", d.data()));
output_->write_string(utils::string::va(" %s", data.data()));
}
break;
}

View File

@ -567,7 +567,7 @@ lex_name:
if (buffer_.length < 16)
{
const auto& itr = keyword_map.find(std::string_view(buffer_.data, buffer_.length));
const auto itr = keyword_map.find(std::string_view(buffer_.data, buffer_.length));
if (itr != keyword_map.end())
{
@ -587,7 +587,7 @@ lex_name:
{
if (buffer_.length < 17)
{
const auto& itr = keyword_map.find(std::string_view(buffer_.data, buffer_.length));
const auto itr = keyword_map.find(std::string_view(buffer_.data, buffer_.length));
if (itr != keyword_map.end())
return parser::symbol_type(itr->second, loc_);

View File

@ -249,22 +249,22 @@ auto resolver::make_token(std::string_view str) -> std::string
return data;
}
auto resolver::file_data(const std::string& name) -> std::tuple<const std::string*, char*, size_t>
auto resolver::file_data(const std::string& name) -> std::tuple<const std::string*, const char*, size_t>
{
const auto& itr = files.find(name);
const auto itr = files.find(name);
if (itr != files.end())
{
return { &itr->first ,reinterpret_cast<char*>(itr->second.data()), itr->second.size() };
return { &itr->first ,reinterpret_cast<const char*>(itr->second.data()), itr->second.size() };
}
auto data = read_callback(name);
const auto& res = files.insert({ name, std::move(data)});
const auto res = files.insert({ name, std::move(data)});
if (res.second)
{
return { &res.first->first, reinterpret_cast<char*>(res.first->second.data()), res.first->second.size() };
return { &res.first->first, reinterpret_cast<const char*>(res.first->second.data()), res.first->second.size() };
}
throw error("couldn't open gsc file '" + name + "'");

View File

@ -33,7 +33,7 @@ public:
static void add_method(const std::string& name, std::uint16_t id);
static auto make_token(std::string_view str) -> std::string;
static auto file_data(const std::string& name) -> std::tuple<const std::string*, char*, size_t>;
static auto file_data(const std::string& name) -> std::tuple<const std::string*, const char*, size_t>;
static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path;
};

View File

@ -11,7 +11,7 @@ namespace xsk::gsc::s1
auto opcode_size(std::uint8_t id) -> std::uint32_t
{
switch (opcode(id))
switch (static_cast<opcode>(id))
{
case opcode::OP_Return:
case opcode::OP_BoolNot: