t6 clean code

This commit is contained in:
xensik 2022-09-09 22:56:57 +02:00
parent 71818f0aa3
commit a286f52fed
10 changed files with 297 additions and 358 deletions

View File

@ -23,7 +23,7 @@ auto assembler::output() -> std::vector<std::uint8_t>
void assembler::assemble(const std::string&, std::vector<std::uint8_t>&) void assembler::assemble(const std::string&, std::vector<std::uint8_t>&)
{ {
throw error("assemble from source unimplemented!"); throw error("assemble from source not implemented!");
} }
void assembler::assemble(const std::string& file, assembly::ptr& data) void assembler::assemble(const std::string& file, assembly::ptr& data)
@ -75,7 +75,7 @@ void assembler::assemble(const std::string& file, assembly::ptr& data)
header_.cseg_size = script_->pos() - header_.cseg_offset; header_.cseg_size = script_->pos() - header_.cseg_offset;
header_.source_crc = 0; // calcule_crc(); header_.source_crc = 0;
// assemble exports // assemble exports
header_.exports_offset = script_->pos(); header_.exports_offset = script_->pos();
@ -215,18 +215,20 @@ void assembler::assemble_function(const function::ptr& func)
assemble_instruction(inst); assemble_instruction(inst);
} }
export_ref obj; export_ref entry;
obj.checksum = 0; // calculate_checksum(); entry.checksum = 0;
obj.offset = func->index; entry.offset = func->index;
obj.name = func->name; entry.name = func->name;
obj.params = func->params; entry.params = func->params;
obj.flags = func->flags; entry.flags = func->flags;
exports_.push_back(obj); exports_.push_back(entry);
} }
void assembler::assemble_instruction(const instruction::ptr& inst) 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_End: case opcode::OP_End:
case opcode::OP_Return: case opcode::OP_Return:
@ -310,31 +312,25 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_ThreadObject: case opcode::OP_ThreadObject:
case opcode::OP_EvalLocalVariable: case opcode::OP_EvalLocalVariable:
case opcode::OP_EvalLocalVariableRef: case opcode::OP_EvalLocalVariableRef:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
break; break;
case opcode::OP_GetByte: case opcode::OP_GetByte:
case opcode::OP_GetNegByte: 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]))); script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break; break;
case opcode::OP_GetUnsignedShort: case opcode::OP_GetUnsignedShort:
case opcode::OP_GetNegUnsignedShort: case opcode::OP_GetNegUnsignedShort:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(2); script_->align(2);
script_->write<std::uint16_t>(static_cast<std::uint16_t>(std::stoi(inst->data[0]))); script_->write<std::uint16_t>(static_cast<std::uint16_t>(std::stoi(inst->data[0])));
break; break;
case opcode::OP_GetInteger: case opcode::OP_GetInteger:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(4); script_->align(4);
script_->write<std::int32_t>(std::stoi(inst->data[0])); script_->write<std::int32_t>(std::stoi(inst->data[0]));
break; break;
case opcode::OP_GetFloat: case opcode::OP_GetFloat:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(4); script_->align(4);
script_->write<float>(std::stof(inst->data[0])); script_->write<float>(std::stof(inst->data[0]));
break; break;
case opcode::OP_GetVector: case opcode::OP_GetVector:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(4); script_->align(4);
script_->write<float>(std::stof(inst->data[0])); script_->write<float>(std::stof(inst->data[0]));
script_->write<float>(std::stof(inst->data[1])); script_->write<float>(std::stof(inst->data[1]));
@ -342,25 +338,20 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
break; break;
case opcode::OP_GetString: case opcode::OP_GetString:
case opcode::OP_GetIString: case opcode::OP_GetIString:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(2); script_->align(2);
script_->write<std::uint16_t>(0); script_->write<std::uint16_t>(0);
break; break;
case opcode::OP_GetAnimation: case opcode::OP_GetAnimation:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(4); script_->align(4);
script_->write<std::uint32_t>(0); script_->write<std::uint32_t>(0);
break; break;
case opcode::OP_WaitTillMatch: 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]))); script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break; break;
case opcode::OP_VectorConstant: case opcode::OP_VectorConstant:
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]))); script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break; break;
case opcode::OP_GetHash: case opcode::OP_GetHash:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(4); script_->align(4);
script_->write<std::uint32_t>(static_cast<std::uint32_t>(std::stoul(inst->data[0], 0, 16))); script_->write<std::uint32_t>(static_cast<std::uint32_t>(std::stoul(inst->data[0], 0, 16)));
break; break;
@ -372,13 +363,11 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_EvalLocalArrayRefCached: case opcode::OP_EvalLocalArrayRefCached:
case opcode::OP_SafeSetWaittillVariableFieldCached: case opcode::OP_SafeSetWaittillVariableFieldCached:
case opcode::OP_EvalLocalVariableRefCached: case opcode::OP_EvalLocalVariableRefCached:
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]))); script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break; break;
case opcode::OP_EvalFieldVariable: case opcode::OP_EvalFieldVariable:
case opcode::OP_EvalFieldVariableRef: case opcode::OP_EvalFieldVariableRef:
case opcode::OP_ClearFieldVariable: case opcode::OP_ClearFieldVariable:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(2); script_->align(2);
script_->write<std::uint16_t>(0); script_->write<std::uint16_t>(0);
break; break;
@ -386,11 +375,9 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_ScriptMethodCallPointer: case opcode::OP_ScriptMethodCallPointer:
case opcode::OP_ScriptThreadCallPointer: case opcode::OP_ScriptThreadCallPointer:
case opcode::OP_ScriptMethodThreadCallPointer: case opcode::OP_ScriptMethodThreadCallPointer:
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]))); script_->write<std::uint8_t>(static_cast<std::uint8_t>(std::stoi(inst->data[0])));
break; break;
case opcode::OP_GetFunction: case opcode::OP_GetFunction:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->align(4); script_->align(4);
script_->write<std::uint32_t>(0); script_->write<std::uint32_t>(0);
break; break;
@ -400,7 +387,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_ScriptMethodCall: case opcode::OP_ScriptMethodCall:
case opcode::OP_ScriptThreadCall: case opcode::OP_ScriptThreadCall:
case opcode::OP_ScriptMethodThreadCall: case opcode::OP_ScriptMethodThreadCall:
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(0); script_->write<std::uint8_t>(0);
script_->align(4); script_->align(4);
script_->write<std::uint32_t>(0); script_->write<std::uint32_t>(0);
@ -411,6 +397,7 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_JumpOnTrueExpr: case opcode::OP_JumpOnTrueExpr:
case opcode::OP_Jump: case opcode::OP_Jump:
case opcode::OP_JumpBack: case opcode::OP_JumpBack:
case opcode::OP_DevblockBegin:
assemble_jump(inst); assemble_jump(inst);
break; break;
case opcode::OP_Switch: case opcode::OP_Switch:
@ -419,18 +406,13 @@ void assembler::assemble_instruction(const instruction::ptr& inst)
case opcode::OP_EndSwitch: case opcode::OP_EndSwitch:
assemble_end_switch(inst); assemble_end_switch(inst);
break; break;
case opcode::OP_DevblockBegin:
case opcode::OP_DevblockEnd:
assemble_devblock(inst);
break;
default: default:
throw asm_error(utils::string::va("Unhandled opcode 0x%X at index '%04X'!", inst->opcode, inst->index)); throw asm_error(utils::string::va("unhandled opcode 0x%X at index '%04X'!", inst->opcode, inst->index));
} }
} }
void assembler::assemble_localvars(const instruction::ptr& inst) void assembler::assemble_localvars(const instruction::ptr& inst)
{ {
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->data.size())); script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->data.size()));
for (auto i = 0u; i < inst->data.size(); i++) for (auto i = 0u; i < inst->data.size(); i++)
@ -442,8 +424,6 @@ void assembler::assemble_localvars(const instruction::ptr& inst)
void assembler::assemble_jump(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst)
{ {
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
const auto addr = static_cast<std::int16_t>(resolve_label(inst->data[0]) - inst->index - inst->size); const auto addr = static_cast<std::int16_t>(resolve_label(inst->data[0]) - inst->index - inst->size);
script_->align(2); script_->align(2);
@ -452,8 +432,6 @@ void assembler::assemble_jump(const instruction::ptr& inst)
void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_switch(const instruction::ptr& inst)
{ {
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode));
const std::int32_t addr = ((resolve_label(inst->data[0]) + 4) & 0xFFFFFFFC) - inst->index - inst->size; const std::int32_t addr = ((resolve_label(inst->data[0]) + 4) & 0xFFFFFFFC) - inst->index - inst->size;
script_->align(4); script_->align(4);
@ -462,8 +440,6 @@ void assembler::assemble_switch(const instruction::ptr& inst)
void assembler::assemble_end_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]); const auto count = std::stoul(inst->data[0]);
const auto numerical = inst->data.back() == "i"; const auto numerical = inst->data.back() == "i";
@ -495,17 +471,11 @@ void assembler::assemble_end_switch(const instruction::ptr& inst)
script_->write<int32_t>(addr); script_->write<int32_t>(addr);
} }
} else
}
void assembler::assemble_devblock(const instruction::ptr& inst)
{ {
script_->write<std::uint8_t>(static_cast<std::uint8_t>(inst->opcode)); throw asm_error("invalid switch case '" + inst->data[1 + (3 * i)] + "'!");
}
const auto addr = static_cast<std::int16_t>(resolve_label(inst->data[0]) - inst->index - inst->size); }
script_->align(2);
script_->write<std::int16_t>(addr);
} }
void assembler::align_instruction(const instruction::ptr& inst) void assembler::align_instruction(const instruction::ptr& inst)
@ -513,7 +483,7 @@ void assembler::align_instruction(const instruction::ptr& inst)
inst->size = opcode_size(inst->opcode); inst->size = opcode_size(inst->opcode);
script_->seek(1); script_->seek(1);
switch (opcode(inst->opcode)) switch (static_cast<opcode>(inst->opcode))
{ {
case opcode::OP_End: case opcode::OP_End:
case opcode::OP_Return: case opcode::OP_Return:
@ -641,16 +611,18 @@ void assembler::align_instruction(const instruction::ptr& inst)
script_->seek(4); script_->seek(4);
break; break;
case opcode::OP_SafeCreateLocalVariables: case opcode::OP_SafeCreateLocalVariables:
script_->seek(1);
{ {
script_->seek(1);
for (auto i = 0u; i < inst->data.size(); i++) for (auto i = 0u; i < inst->data.size(); i++)
{ {
inst->size += script_->align(2) + 2; inst->size += script_->align(2) + 2;
add_string_reference(inst->data[i], string_type::canonical, script_->pos()); add_string_reference(inst->data[i], string_type::canonical, script_->pos());
script_->seek(2); script_->seek(2);
} }
}
break; break;
}
case opcode::OP_RemoveLocalVariables: case opcode::OP_RemoveLocalVariables:
case opcode::OP_EvalLocalVariableCached: case opcode::OP_EvalLocalVariableCached:
case opcode::OP_EvalLocalArrayRefCached: case opcode::OP_EvalLocalArrayRefCached:
@ -693,6 +665,7 @@ void assembler::align_instruction(const instruction::ptr& inst)
case opcode::OP_JumpOnTrueExpr: case opcode::OP_JumpOnTrueExpr:
case opcode::OP_Jump: case opcode::OP_Jump:
case opcode::OP_JumpBack: case opcode::OP_JumpBack:
case opcode::OP_DevblockBegin:
inst->size += script_->align(2); inst->size += script_->align(2);
script_->seek(2); script_->seek(2);
break; break;
@ -721,15 +694,11 @@ void assembler::align_instruction(const instruction::ptr& inst)
inst->size += 8; inst->size += 8;
script_->seek(8); script_->seek(8);
} }
break;
} }
break;
case opcode::OP_DevblockBegin:
case opcode::OP_DevblockEnd:
inst->size += script_->align(2);
script_->seek(2);
break;
default: default:
throw asm_error(utils::string::va("Unhandled opcode 0x%X at index '%04X'!", inst->opcode, inst->index)); throw asm_error(utils::string::va("unhandled opcode 0x%X at index '%04X'!", inst->opcode, inst->index));
} }
} }
@ -755,7 +724,7 @@ void assembler::process_function(const function::ptr& func)
void assembler::process_instruction(const instruction::ptr& inst) void assembler::process_instruction(const instruction::ptr& inst)
{ {
switch (opcode(inst->opcode)) switch (static_cast<opcode>(inst->opcode))
{ {
case opcode::OP_GetString: case opcode::OP_GetString:
case opcode::OP_GetIString: case opcode::OP_GetIString:
@ -771,8 +740,9 @@ void assembler::process_instruction(const instruction::ptr& inst)
{ {
process_string(entry); process_string(entry);
} }
}
break; break;
}
case opcode::OP_EvalFieldVariable: case opcode::OP_EvalFieldVariable:
case opcode::OP_EvalFieldVariableRef: case opcode::OP_EvalFieldVariableRef:
case opcode::OP_ClearFieldVariable: case opcode::OP_ClearFieldVariable:
@ -806,8 +776,9 @@ void assembler::process_instruction(const instruction::ptr& inst)
} }
} }
} }
}
break; break;
}
default: default:
break; break;
} }
@ -815,15 +786,15 @@ void assembler::process_instruction(const instruction::ptr& inst)
auto assembler::resolve_label(const std::string& name) -> std::int32_t auto assembler::resolve_label(const std::string& name) -> std::int32_t
{ {
for (const auto& func : labels_) for (const auto& entry : labels_)
{ {
if (func.second == name) if (entry.second == name)
{ {
return func.first; return entry.first;
} }
} }
throw asm_error("Couldn't resolve label address of '" + name + "'!"); throw asm_error("couldn't resolve label address of '" + name + "'!");
} }
auto assembler::string_offset(const std::string& name) -> std::uint16_t auto assembler::string_offset(const std::string& name) -> std::uint16_t
@ -842,7 +813,7 @@ void assembler::add_string_reference(const std::string& str, string_type type, s
{ {
for (auto& entry : stringtables_) for (auto& entry : stringtables_)
{ {
if (entry.name == str && entry.type == std::uint8_t(type)) if (entry.name == str && entry.type == static_cast<std::uint8_t>(type))
{ {
entry.refs.push_back(ref); entry.refs.push_back(ref);
return; return;
@ -863,13 +834,13 @@ void assembler::add_import_reference(const std::vector<std::string>& data, std::
} }
} }
import_ref n; import_ref new_entry;
n.space = data[0]; new_entry.space = data[0];
n.name = data[1]; new_entry.name = data[1];
n.params = static_cast<std::uint8_t>(std::stoi(data[2])); new_entry.params = static_cast<std::uint8_t>(std::stoi(data[2]));
n.flags = static_cast<std::uint8_t>(std::stoi(data[3])); new_entry.flags = static_cast<std::uint8_t>(std::stoi(data[3]));
n.refs.push_back(ref); new_entry.refs.push_back(ref);
imports_.push_back(std::move(n)); imports_.push_back(std::move(new_entry));
} }
void assembler::add_anim_reference(const std::vector<std::string>& data, std::uint32_t ref) void assembler::add_anim_reference(const std::vector<std::string>& data, std::uint32_t ref)
@ -883,10 +854,10 @@ void assembler::add_anim_reference(const std::vector<std::string>& data, std::ui
} }
} }
animtree_ref n; animtree_ref new_entry;
n.name = data[0]; new_entry.name = data[0];
n.anims.push_back({ data[1], ref }); new_entry.anims.push_back({ data[1], ref });
animtrees_.push_back(std::move(n)); animtrees_.push_back(std::move(new_entry));
} }
} // namespace xsk::arc::t6 } // namespace xsk::arc::t6

View File

@ -33,7 +33,6 @@ private:
void assemble_jump(const instruction::ptr& inst); void assemble_jump(const instruction::ptr& inst);
void assemble_switch(const instruction::ptr& inst); void assemble_switch(const instruction::ptr& inst);
void assemble_end_switch(const instruction::ptr& inst); void assemble_end_switch(const instruction::ptr& inst);
void assemble_devblock(const instruction::ptr& inst);
void process_string(const std::string& data); void process_string(const std::string& data);
void process_function(const function::ptr& func); void process_function(const function::ptr& func);
void process_instruction(const instruction::ptr& inst); void process_instruction(const instruction::ptr& inst);

View File

@ -69,9 +69,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 compiler::parse_file(const std::string& file) -> ast::program::ptr
{ {
auto data = resolver::file_data(file); 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) void compiler::compile_program(const ast::program::ptr& program)
@ -1250,16 +1249,16 @@ void compiler::emit_expr_call_function(const ast::expr_function::ptr& expr, bool
emit_expr_arguments(expr->args); emit_expr_arguments(expr->args);
auto argcount = utils::string::va("%d", expr->args->list.size()); auto argcount = utils::string::va("%d", expr->args->list.size());
auto flags = developer_thread_ ? std::uint8_t(import_flags::developer) : 0; auto flags = developer_thread_ ? static_cast<std::uint8_t>(import_flags::developer) : 0;
switch (expr->mode) switch (expr->mode)
{ {
case ast::call::mode::normal: case ast::call::mode::normal:
flags |= std::uint8_t(import_flags::func_call); flags |= static_cast<std::uint8_t>(import_flags::func_call);
emit_opcode(opcode::OP_ScriptFunctionCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) }); emit_opcode(opcode::OP_ScriptFunctionCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) });
break; break;
case ast::call::mode::thread: case ast::call::mode::thread:
flags |= std::uint8_t(import_flags::func_call_thread); flags |= static_cast<std::uint8_t>(import_flags::func_call_thread);
emit_opcode(opcode::OP_ScriptThreadCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) }); emit_opcode(opcode::OP_ScriptThreadCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) });
break; break;
default: default:
@ -1339,16 +1338,16 @@ void compiler::emit_expr_method_function(const ast::expr_function::ptr& expr, co
emit_expr(obj); emit_expr(obj);
auto argcount = utils::string::va("%d", expr->args->list.size()); auto argcount = utils::string::va("%d", expr->args->list.size());
auto flags = developer_thread_ ? std::uint8_t(import_flags::developer) : 0; auto flags = developer_thread_ ? static_cast<std::uint8_t>(import_flags::developer) : 0;
switch (expr->mode) switch (expr->mode)
{ {
case ast::call::mode::normal: case ast::call::mode::normal:
flags |= std::uint8_t(import_flags::meth_call); flags |= static_cast<std::uint8_t>(import_flags::meth_call);
emit_opcode(opcode::OP_ScriptMethodCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) }); emit_opcode(opcode::OP_ScriptMethodCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) });
break; break;
case ast::call::mode::thread: case ast::call::mode::thread:
flags |= std::uint8_t(import_flags::meth_call_thread); flags |= static_cast<std::uint8_t>(import_flags::meth_call_thread);
emit_opcode(opcode::OP_ScriptMethodThreadCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) }); emit_opcode(opcode::OP_ScriptMethodThreadCall, { expr->path->value, expr->name->value, argcount, utils::string::va("%d", flags) });
break; break;
default: default:
@ -1519,9 +1518,9 @@ void compiler::emit_expr_reference(const ast::expr_reference::ptr& expr)
// TODO: resolve import calls path // TODO: resolve import calls path
auto flags = developer_thread_ ? std::uint8_t(import_flags::developer) : 0; auto flags = developer_thread_ ? static_cast<std::uint8_t>(import_flags::developer) : 0;
flags |= std::uint8_t(import_flags::func_reference); flags |= static_cast<std::uint8_t>(import_flags::func_reference);
emit_opcode(opcode::OP_GetFunction, { expr->path->value, expr->name->value, "0", utils::string::va("%d", flags) }); emit_opcode(opcode::OP_GetFunction, { expr->path->value, expr->name->value, "0", utils::string::va("%d", flags) });
} }
@ -1633,7 +1632,7 @@ void compiler::emit_expr_field_ref(const ast::expr_field::ptr& expr, bool set)
void compiler::emit_expr_local_ref(const ast::expr_identifier::ptr& expr, bool set) void compiler::emit_expr_local_ref(const ast::expr_identifier::ptr& expr, bool set)
{ {
const auto itr = constants_.find(expr->value); const auto& itr = constants_.find(expr->value);
if (itr != constants_.end()) if (itr != constants_.end())
{ {
@ -1724,16 +1723,11 @@ void compiler::emit_expr_field(const ast::expr_field::ptr& expr)
void compiler::emit_expr_local(const ast::expr_identifier::ptr& expr) void compiler::emit_expr_local(const ast::expr_identifier::ptr& expr)
{ {
// is constant ( should only allow: string, loc string, number, vector) // is constant ( should only allow: string, loc string, number, vector)
const auto itr = constants_.find(expr->value); const auto& itr = constants_.find(expr->value);
if (itr != constants_.end()) if (itr != constants_.end())
{ emit_expr(itr->second);
const auto& value = itr->second; else
emit_expr(value);
return;
}
// is local var
emit_opcode(opcode::OP_EvalLocalVariableCached, variable_access(expr)); emit_opcode(opcode::OP_EvalLocalVariableCached, variable_access(expr));
} }
@ -2277,6 +2271,7 @@ void compiler::process_expr_parameters(const ast::expr_parameters::ptr& expr)
void compiler::variable_register(const std::string& name) void compiler::variable_register(const std::string& name)
{ {
auto found = false; auto found = false;
for (std::size_t i = 0; i < local_stack_.size(); i++) for (std::size_t i = 0; i < local_stack_.size(); i++)
{ {
if (local_stack_[i] == name) if (local_stack_[i] == name)
@ -2286,10 +2281,7 @@ void compiler::variable_register(const std::string& name)
} }
} }
if (!found) if (!found) local_stack_.push_back(name);
{
local_stack_.push_back(name);
}
} }
auto compiler::variable_access(const ast::expr_identifier::ptr& name) -> std::string auto compiler::variable_access(const ast::expr_identifier::ptr& name) -> std::string
@ -2332,13 +2324,12 @@ auto compiler::is_constant_condition(const ast::expr& expr) -> bool
auto compiler::create_label() -> std::string auto compiler::create_label() -> std::string
{ {
label_idx_++; label_idx_++;
auto name = utils::string::va("loc_%d", label_idx_); return utils::string::va("loc_%d", label_idx_);
return name;
} }
auto compiler::insert_label() -> std::string auto compiler::insert_label() -> std::string
{ {
const auto itr = function_->labels.find(index_); const auto& itr = function_->labels.find(index_);
if (itr != function_->labels.end()) if (itr != function_->labels.end())
{ {
@ -2355,13 +2346,13 @@ auto compiler::insert_label() -> std::string
void compiler::insert_label(const std::string& name) void compiler::insert_label(const std::string& name)
{ {
const auto itr = function_->labels.find(index_); const auto& itr = function_->labels.find(index_);
if (itr != function_->labels.end()) if (itr != function_->labels.end())
{ {
for (auto& inst : function_->instructions) for (auto& inst : function_->instructions)
{ {
switch (opcode(inst->opcode)) switch (static_cast<opcode>(inst->opcode))
{ {
case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnFalse:
case opcode::OP_JumpOnTrue: case opcode::OP_JumpOnTrue:
@ -2415,7 +2406,7 @@ void compiler::print_instruction(const instruction::ptr& inst)
{ {
output_->write_string(utils::string::va("\t\t%s(", resolver::opcode_name(inst->opcode).data())); 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_GetHash: case opcode::OP_GetHash:
case opcode::OP_GetString: case opcode::OP_GetString:

File diff suppressed because it is too large Load Diff

View File

@ -40,7 +40,7 @@ private:
void decompile_devblocks(const ast::stmt_list::ptr& stmt); void decompile_devblocks(const ast::stmt_list::ptr& stmt);
void decompile_if(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end); void decompile_if(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end);
void decompile_ifelse(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end); void decompile_ifelse(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end);
void decompile_infinite(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end); void decompile_inf(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end);
void decompile_loop(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end); void decompile_loop(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end);
void decompile_while(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end); void decompile_while(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end);
void decompile_dowhile(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end); void decompile_dowhile(const ast::stmt_list::ptr& stmt, std::size_t begin, std::size_t end);

View File

@ -230,9 +230,6 @@ void disassembler::disassemble(const std::string& file, std::vector<std::uint8_t
func->labels = labels_; func->labels = labels_;
labels_.clear(); labels_.clear();
} }
// fixup list ...
// profile list ...
} }
void disassembler::disassemble_function(const function::ptr& func) void disassembler::disassemble_function(const function::ptr& func)
@ -274,7 +271,7 @@ void disassembler::disassemble_function(const function::ptr& func)
void disassembler::disassemble_instruction(const instruction::ptr& inst) void disassembler::disassemble_instruction(const instruction::ptr& inst)
{ {
switch (opcode(inst->opcode)) switch (static_cast<opcode>(inst->opcode))
{ {
case opcode::OP_End: case opcode::OP_End:
case opcode::OP_Return: case opcode::OP_Return:
@ -438,6 +435,7 @@ void disassembler::disassemble_instruction(const instruction::ptr& inst)
case opcode::OP_JumpOnTrueExpr: case opcode::OP_JumpOnTrueExpr:
case opcode::OP_Jump: case opcode::OP_Jump:
case opcode::OP_JumpBack: case opcode::OP_JumpBack:
case opcode::OP_DevblockBegin:
disassemble_jump(inst); disassemble_jump(inst);
break; break;
case opcode::OP_Switch: case opcode::OP_Switch:
@ -446,11 +444,8 @@ void disassembler::disassemble_instruction(const instruction::ptr& inst)
case opcode::OP_EndSwitch: case opcode::OP_EndSwitch:
disassemble_end_switch(inst); disassemble_end_switch(inst);
break; break;
case opcode::OP_DevblockBegin:
disassemble_devblock(inst);
break;
default: default:
throw disasm_error(utils::string::va("Unhandled opcode 0x%X at index '%04X'!", inst->opcode, inst->index)); throw disasm_error(utils::string::va("unhandled opcode 0x%X at index '%04X'!", inst->opcode, inst->index));
} }
} }
@ -458,10 +453,16 @@ void disassembler::disassemble_string(const instruction::ptr& inst)
{ {
inst->size += script_->align(2); inst->size += script_->align(2);
const auto& entry = string_refs_.at(script_->pos()); const auto& entry = string_refs_.find(script_->pos());
inst->data.push_back(entry->name); if (entry != string_refs_.end())
{
inst->data.push_back(entry->second->name);
script_->seek(2); script_->seek(2);
return;
}
throw disasm_error(utils::string::va("string reference not found at index '%04X'!", inst->index));
} }
void disassembler::disassemble_animation(const instruction::ptr& inst) void disassembler::disassemble_animation(const instruction::ptr& inst)
@ -469,20 +470,24 @@ void disassembler::disassemble_animation(const instruction::ptr& inst)
inst->size += script_->align(4); inst->size += script_->align(4);
const auto ref = script_->pos(); const auto ref = script_->pos();
const auto& entry = anim_refs_.at(ref); const auto& entry = anim_refs_.find(ref);
inst->data.push_back(entry->name); if (entry != anim_refs_.end())
{
inst->data.push_back(entry->second->name);
for (const auto& anim : entry->anims) for (const auto& anim : entry->second->anims)
{ {
if (anim.ref == ref) if (anim.ref == ref)
{ {
inst->data.push_back(anim.name); inst->data.push_back(anim.name);
break; script_->seek(4);
return;
}
} }
} }
script_->seek(4); throw disasm_error(utils::string::va("animation reference not found at index '%04X'!", inst->index));
} }
void disassembler::disassemble_localvars(const instruction::ptr& inst) void disassembler::disassemble_localvars(const instruction::ptr& inst)
@ -501,17 +506,23 @@ void disassembler::disassemble_import(const instruction::ptr& inst)
inst->size += script_->align(4); inst->size += script_->align(4);
script_->seek(4); script_->seek(4);
const auto& entry = import_refs_.at(inst->index); const auto& entry = import_refs_.find(inst->index);
inst->data.push_back(entry->space); if (entry != import_refs_.end())
inst->data.push_back(entry->name); {
inst->data.push_back(entry->second->space);
inst->data.push_back(entry->second->name);
return;
}
throw disasm_error(utils::string::va("import reference not found at index '%04X'!", inst->index));
} }
void disassembler::disassemble_jump(const instruction::ptr& inst) void disassembler::disassemble_jump(const instruction::ptr& inst)
{ {
inst->size += script_->align(2); inst->size += script_->align(2);
const auto addr = inst->index + inst->size + script_->read<std::int16_t>(); const auto addr = script_->read<std::int16_t>() + script_->pos();
const auto label = utils::string::va("loc_%X", addr); const auto label = utils::string::va("loc_%X", addr);
inst->data.push_back(label); inst->data.push_back(label);
@ -597,17 +608,6 @@ void disassembler::disassemble_end_switch(const instruction::ptr& inst)
inst->data.push_back((numerical) ? "i" : "s"); inst->data.push_back((numerical) ? "i" : "s");
} }
void disassembler::disassemble_devblock(const instruction::ptr& inst)
{
inst->size += script_->align(2);
const auto addr = inst->index + inst->size + script_->read<std::int16_t>();
const auto label = utils::string::va("loc_%X", addr);
inst->data.push_back(label);
labels_.insert({ addr, label });
}
void disassembler::print_function(const function::ptr& func) void disassembler::print_function(const function::ptr& func)
{ {
output_->write_string("\n"); output_->write_string("\n");

View File

@ -40,7 +40,6 @@ private:
void disassemble_jump(const instruction::ptr& inst); void disassemble_jump(const instruction::ptr& inst);
void disassemble_switch(const instruction::ptr& inst); void disassemble_switch(const instruction::ptr& inst);
void disassemble_end_switch(const instruction::ptr& inst); void disassemble_end_switch(const instruction::ptr& inst);
void disassemble_devblock(const instruction::ptr& inst);
void print_function(const function::ptr& func); void print_function(const function::ptr& func);
void print_instruction(const instruction::ptr& inst); void print_instruction(const instruction::ptr& inst);
}; };

View File

@ -709,7 +709,7 @@ lex_number:
if (last == '\'' || buffer_.length <= 0) if (last == '\'' || buffer_.length <= 0)
throw comp_error(loc_, "invalid octal literal"); throw comp_error(loc_, "invalid octal literal");
return parser::make_INTEGER(xsk::utils::string::oct_to_dec(buffer_.data), loc_); return parser::make_INTEGER(utils::string::oct_to_dec(buffer_.data), loc_);
} }
else if (curr == 'b') else if (curr == 'b')
{ {
@ -743,7 +743,7 @@ lex_number:
if (last == '\'' || buffer_.length < 3) if (last == '\'' || buffer_.length < 3)
throw comp_error(loc_, "invalid binary literal"); throw comp_error(loc_, "invalid binary literal");
return parser::make_INTEGER(xsk::utils::string::bin_to_dec(buffer_.data), loc_); return parser::make_INTEGER(utils::string::bin_to_dec(buffer_.data), loc_);
} }
else if (curr == 'x') else if (curr == 'x')
{ {
@ -777,7 +777,7 @@ lex_number:
if (last == '\'' || buffer_.length < 3) if (last == '\'' || buffer_.length < 3)
throw comp_error(loc_, "invalid hexadecimal literal"); throw comp_error(loc_, "invalid hexadecimal literal");
return parser::make_INTEGER(xsk::utils::string::hex_to_dec(buffer_.data), loc_); return parser::make_INTEGER(utils::string::hex_to_dec(buffer_.data), loc_);
} }
throw error("UNEXPECTED LEXER INTERNAL ERROR!"); throw error("UNEXPECTED LEXER INTERNAL ERROR!");

View File

@ -39,7 +39,7 @@ auto resolver::opcode_id(const std::string& name) -> std::uint8_t
return itr->second; return itr->second;
} }
throw error(utils::string::va("Couldn't resolve opcode id for name '%s'!", name.data())); throw error(utils::string::va("couldn't resolve opcode id for name '%s'!", name.data()));
} }
auto resolver::opcode_name(std::uint8_t id) -> std::string auto resolver::opcode_name(std::uint8_t id) -> std::string
@ -51,7 +51,7 @@ auto resolver::opcode_name(std::uint8_t id) -> std::string
return std::string(itr->second); return std::string(itr->second);
} }
throw error(utils::string::va("Couldn't resolve opcode name for id '0x%hhX'!", id)); throw error(utils::string::va("couldn't resolve opcode name for id '0x%hhX'!", id));
} }
auto resolver::dvar_name(std::uint32_t id) -> std::string auto resolver::dvar_name(std::uint32_t id) -> std::string

View File

@ -11,7 +11,7 @@ namespace xsk::arc::t6
auto opcode_size(std::uint8_t id) -> std::uint32_t auto opcode_size(std::uint8_t id) -> std::uint32_t
{ {
switch (opcode(id)) switch (static_cast<opcode>(id))
{ {
case opcode::OP_End: case opcode::OP_End:
case opcode::OP_Return: case opcode::OP_Return:
@ -145,7 +145,7 @@ auto opcode_size(std::uint8_t id) -> std::uint32_t
case opcode::OP_GetVector: case opcode::OP_GetVector:
return 13; return 13;
default: default:
throw std::runtime_error("Couldn't resolve instruction size for " + std::to_string(id)); throw error("couldn't resolve instruction size for " + std::to_string(id));
} }
} }