diff --git a/src/experimental/iw5c/xsk/assembler.cpp b/src/experimental/iw5c/xsk/assembler.cpp index adc349a0..0aaded87 100644 --- a/src/experimental/iw5c/xsk/assembler.cpp +++ b/src/experimental/iw5c/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_End: case opcode::OP_Return: @@ -229,28 +231,22 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CastBool: case opcode::OP_BoolNot: case opcode::OP_BoolComplement: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write_endian(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write_endian(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write_endian(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write_endian(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); inst->size += script_->align(4); script_->write_endian(std::stof(inst->data[0])); script_->write_endian(std::stof(inst->data[1])); @@ -258,23 +254,19 @@ void assembler::assemble_instruction(const instruction::ptr& inst) break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_CreateLocalVariable: @@ -291,7 +283,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_SetLocalVariableFieldCached: case opcode::OP_ClearLocalVariableFieldCached: case opcode::OP_EvalLocalVariableObjectCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalLevelFieldVariable: @@ -314,7 +305,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptMethodChildThreadCallPointer: case opcode::OP_CallBuiltinPointer: case opcode::OP_CallBuiltinMethodPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_ScriptLocalFunctionCall2: @@ -390,8 +380,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -404,8 +392,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -419,7 +405,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -439,8 +424,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write_endian(addr - inst->index - 4); @@ -448,8 +431,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write_endian(static_cast(count)); @@ -500,8 +481,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFF; @@ -517,8 +496,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/experimental/iw5c/xsk/compiler.cpp b/src/experimental/iw5c/xsk/compiler.cpp index 128cf0cc..1a802b26 100644 --- a/src/experimental/iw5c/xsk/compiler.cpp +++ b/src/experimental/iw5c/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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) @@ -1876,8 +1875,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; } @@ -2934,7 +2932,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: @@ -2958,679 +2956,9 @@ void compiler::insert_label(const std::string& name) } } - -gsc::include_t compiler::include_maps_mp_utility_ = +auto compiler::map_known_includes(const std::string&) -> bool { - "maps/mp/_utility", - { - "exploder_sound", - "_beginlocationselection", - "stoplocationselection", - "endselectiononemp", - "endselectiononaction", - "endselectiononendgame", - "isattachment", - "getattachmenttype", - "delaythread", - "delaythread_proc", - "getplant", - "orienttonormal", - "deleteplacedentity", - "playsoundonplayers", - "sortlowermessages", - "addlowermessage", - "removelowermessage", - "getlowermessage", - "setlowermessage", - "updatelowermessage", - "clearondeath", - "clearafterfade", - "clearlowermessage", - "clearlowermessages", - "printonteam", - "printboldonteam", - "printboldonteamarg", - "printonteamarg", - "printonplayers", - "printandsoundoneveryone", - "printandsoundonteam", - "printandsoundonplayer", - "_playlocalsound", - "dvarintvalue", - "dvarfloatvalue", - "play_sound_on_tag", - "getotherteam", - "wait_endon", - "initpersstat", - "getpersstat", - "incpersstat", - "setpersstat", - "initplayerstat", - "incplayerstat", - "setplayerstat", - "getplayerstat", - "getplayerstattime", - "setplayerstatifgreater", - "setplayerstatiflower", - "updatepersratio", - "updatepersratiobuffered", - "waittillslowprocessallowed", - "waitfortimeornotify", - "isexcluded", - "leaderdialog", - "leaderdialogbothteams", - "leaderdialogonplayers", - "leaderdialogonplayer", - "playleaderdialogonplayer", - "updatemainmenu", - "updateobjectivetext", - "setobjectivetext", - "setobjectivescoretext", - "setobjectivehinttext", - "getobjectivetext", - "getobjectivescoretext", - "getobjectivehinttext", - "gettimepassed", - "getsecondspassed", - "getminutespassed", - "clearkillcamstate", - "isinkillcam", - "isvalidclass", - "getvalueinrange", - "waitfortimeornotifies", - "closemenus", - "logxpgains", - "registerroundswitchdvar", - "registerroundlimitdvar", - "registerwinlimitdvar", - "registerscorelimitdvar", - "registertimelimitdvar", - "registerhalftimedvar", - "registernumlivesdvar", - "setovertimelimitdvar", - "get_damageable_player", - "get_damageable_sentry", - "get_damageable_grenade", - "get_damageable_mine", - "get_damageable_player_pos", - "getstancecenter", - "get_damageable_grenade_pos", - "getdvarvec", - "strip_suffix", - "_takeweaponsexcept", - "savedata", - "restoredata", - "_setactionslot", - "isfloat", - "registerwatchdvarint", - "registerwatchdvarfloat", - "registerwatchdvar", - "setoverridewatchdvar", - "getwatcheddvar", - "updatewatcheddvars", - "isroundbased", - "islastround", - "wasonlyround", - "waslastround", - "hitroundlimit", - "hitscorelimit", - "hitwinlimit", - "getscorelimit", - "getroundswon", - "isobjectivebased", - "gettimelimit", - "gethalftime", - "inovertime", - "gamehasstarted", - "getaverageorigin", - "getlivingplayers", - "setusingremote", - "getremotename", - "freezecontrolswrapper", - "clearusingremote", - "isusingremote", - "queuecreate", - "queueadd", - "queueremovefirst", - "_giveweapon", - "_hasperk", - "giveperk", - "_setperk", - "_setextraperks", - "_unsetperk", - "_unsetextraperks", - "_clearperks", - "quicksort", - "quicksortmid", - "swap", - "_suicide", - "isreallyalive", - "playdeathsound", - "rankingenabled", - "privatematch", - "matchmakinggame", - "setaltsceneobj", - "endsceneondeath", - "getgametypenumlives", - "givecombathigh", - "arrayinsertion", - "getproperty", - "getintproperty", - "getfloatproperty", - "statusmenu", - "ischangingweapon", - "killshouldaddtokillstreak", - "streakshouldchain", - "isjuggernaut", - "iskillstreakweapon", - "isenvironmentweapon", - "getweaponclass", - "isdeathstreakweapon", - "getbaseweaponname", - "fixakimbostring", - "playsoundinspace", - "limitdecimalplaces", - "rounddecimalplaces", - "playerforclientid", - "isrested", - "stringtofloat", - "setselfusable", - "maketeamusable", - "_updateteamusable", - "makeenemyusable", - "_updateenemyusable", - "getnextlifeid", - "initgameflags", - "gameflaginit", - "gameflag", - "gameflagset", - "gameflagclear", - "gameflagwait", - "isprimarydamage", - "isbulletdamage", - "initlevelflags", - "levelflaginit", - "levelflag", - "levelflagset", - "levelflagclear", - "levelflagwait", - "levelflagwaitopen", - "getweaponattachments", - "isemped", - "isairdenied", - "isnuked", - "getplayerforguid", - "teamplayercardsplash", - "iscacprimaryweapon", - "iscacsecondaryweapon", - "getlastlivingplayer", - "getpotentiallivingplayers", - "waittillrecoveredhealth", - "attachmentmap", - "validateattachment", - "_objective_delete", - "touchingbadtrigger", - "setthirdpersondof", - "killtrigger", - "findisfacing", - "combinearrays", - "setrecoilscale", - "cleanarray", - "notusableforjoiningplayers", - "isstrstart", - "validateusestreak", - "currentactivevehiclecount", - "maxvehiclesallowed", - "incrementfauxvehiclecount", - "decrementfauxvehiclecount", - "lightweightscalar", - "allowteamchoice", - "allowclasschoice", - "isbuffunlockedforweapon", - "isbuffequippedonweapon", - "setcommonrulesfrommatchrulesdata", - "reinitializematchrulesonmigration", - "getmatchrulesspecialclass", - "recipeclassapplyjuggernaut", - } -}; - -gsc::include_t compiler::include_common_scripts_createfx_ = -{ - "common_scripts/_createfx", - { - "createeffect", - "getloopeffectdelaydefault", - "getoneshoteffectdelaydefault", - "getexploderdelaydefault", - "getintervalsounddelaymindefault", - "getintervalsounddelaymaxdefault", - "add_effect", - "createloopsound", - "createintervalsound", - "createnewexploder", - "createexploderex", - "set_origin_and_angles", - "set_forward_and_up_vectors", - "createfx_common", - "createfxlogic", - "copy_angles_of_selected_ents", - "reset_axis_of_selected_ents", - "last_selected_entity_has_changed", - "createfx_showorigin", - "drop_selection_to_ground", - "set_off_exploders", - "draw_distance", - "createfx_autosave", - "rotate_over_time", - "delete_pressed", - "remove_selected_option", - "remove_option", - "delete_selection", - "move_selection_to_cursor", - "insert_effect", - "show_help", - "select_last_entity", - "select_all_exploders_of_currently_selected", - "copy_ents", - "post_entity_creation_function", - "paste_ents", - "add_and_select_entity", - "get_center_of_array", - "ent_draw_axis", - "rotation_is_occuring", - "print_fx_options", - "entity_highlight_disable", - "entity_highlight_enable", - "toggle_createfx_drawing", - "manipulate_createfx_ents", - "clear_settable_fx", - "reset_fx_hud_colors", - "button_is_held", - "button_is_clicked", - "toggle_entity_selection", - "select_entity", - "ent_is_highlighted", - "deselect_entity", - "index_is_selected", - "ent_is_selected", - "clear_entity_selection", - "draw_axis", - "clear_fx_hudelements", - "set_fx_hudelement", - "createfx_centerprint", - "createfx_centerprint_thread", - "buttondown", - "buttonpressed_internal", - "get_selected_move_vector", - "process_button_held_and_clicked", - "locked", - "kb_locked", - "add_button", - "add_kb_button", - "set_anglemod_move_vector", - "cfxprintlnstart", - "cfxprintln", - "cfxprintlnend", - "update_selected_entities", - "hack_start", - "get_player", - "createfx_orgranize_array", - "stop_fx_looper", - "stop_loopsound", - "func_get_level_fx", - "restart_fx_looper", - "process_fx_rotater", - "generate_fx_log", - } -}; - -gsc::include_t compiler::include_common_scripts_utility_ = -{ - "common_scripts/utility", - { - "scriptprintln", - "debugprintln", - "draw_debug_line", - "waittillend", - "noself_func", - "self_func", - "randomvector", - "randomvectorrange", - "angle_dif", - "sign", - "track", - "get_enemy_team", - "clear_exception", - "set_exception", - "set_all_exceptions", - "cointoss", - "choose_from_weighted_array", - "get_cumulative_weights", - "waittill_string", - "waittill_multiple", - "waittill_multiple_ents", - "waittill_any_return", - "waittill_any_timeout", - "_timeout", - "waittill_any", - "waittill_any_ents", - "isflashed", - "flag_exist", - "flag", - "init_flags", - "flag_init", - "empty_init_func", - "issuffix", - "flag_set", - "assign_unique_id", - "flag_wait", - "flag_clear", - "flag_waitopen", - "waittill_either", - "array_thread", - "array_call", - "array_thread4", - "array_thread5", - "trigger_on", - "trigger_on_proc", - "trigger_off", - "trigger_off_proc", - "set_trigger_flag_permissions", - "update_trigger_based_on_flags", - "create_flags_and_return_tokens", - "init_trigger_flags", - "getstruct", - "getstructarray", - "struct_class_init", - "fileprint_start", - "fileprint_map_start", - "fileprint_map_header", - "fileprint_map_keypairprint", - "fileprint_map_entity_start", - "fileprint_map_entity_end", - "fileprint_radiant_vec", - "array_remove", - "array_remove_array", - "array_removeundefined", - "array_levelthread", - "array_levelcall", - "add_to_array", - "flag_assert", - "flag_wait_either", - "flag_wait_either_return", - "flag_wait_any", - "flag_wait_any_return", - "flag_wait_all", - "flag_wait_or_timeout", - "flag_waitopen_or_timeout", - "wait_for_flag_or_time_elapses", - "delaycall", - "delaycall_proc", - "noself_delaycall", - "noself_delaycall_proc", - "issp", - "issp_towerdefense", - "string_starts_with", - "plot_points", - "draw_line_for_time", - "array_combine", - "flat_angle", - "flat_origin", - "draw_arrow_time", - "get_linked_ents", - "get_linked_vehicle_nodes", - "get_linked_ent", - "get_linked_vehicle_node", - "get_links", - "run_thread_on_targetname", - "run_thread_on_noteworthy", - "draw_arrow", - "getfx", - "fxexists", - "print_csv_asset", - "fileprint_csv_start", - "_loadfx", - "getlastweapon", - "playerunlimitedammothread", - "isusabilityenabled", - "_disableusability", - "_enableusability", - "resetusability", - "_disableweapon", - "_enableweapon", - "isweaponenabled", - "_disableweaponswitch", - "_enableweaponswitch", - "isweaponswitchenabled", - "_disableoffhandweapons", - "_enableoffhandweapons", - "isoffhandweaponenabled", - "random", - "spawn_tag_origin", - "waittill_notify_or_timeout", - "fileprint_launcher_start_file", - "fileprint_launcher", - "fileprint_launcher_end_file", - "launcher_write_clipboard", - "isdestructible", - "pauseeffect", - "activate_individual_exploder", - "waitframe", - "brush_delete", - "brush_throw", - "get_target_ent", - "brush_show", - "exploder_earthquake", - "do_earthquake", - "exploder_rumble", - "exploder_delay", - "exploder_damage", - "effect_loopsound", - "play_loopsound_in_space", - "sound_effect", - "effect_soundalias", - "play_sound_in_space", - "cannon_effect", - "exploder_playsound", - "fire_effect", - "loop_fx_sound", - "loop_fx_sound_interval", - "loop_sound_delete", - "exploder_before_load", - "exploder_after_load", - "activate_exploder", - "createloopeffect", - "createoneshoteffect", - "createexploder", - "alphabetize", - "is_later_in_alphabet", - "alphabet_compare", - "play_loop_sound_on_entity", - "stop_loop_sound_on_entity", - "delete_on_death", - "error", - "exploder", - "create_dvar", - "void", - "tag_project", - "ter_op", - "create_lock", - "lock", - "is_locked", - "unlock_wait", - "unlock", - "unlock_thread", - "get_template_level", - } -}; - -gsc::include_t compiler::include_maps_mp_gametypes_hud_util_ = -{ - "maps/mp/gametypes/_hud_util", - { - "setparent", - "getparent", - "addchild", - "removechild", - "setpoint", - "setpointbar", - "updatebar", - "updatebarscale", - "createfontstring", - "createserverfontstring", - "createservertimer", - "createtimer", - "createicon", - "createservericon", - "createserverbar", - "createbar", - "getcurrentfraction", - "createprimaryprogressbar", - "createprimaryprogressbartext", - "createteamprogressbar", - "createteamprogressbartext", - "setflashfrac", - "hideelem", - "showelem", - "flashthread", - "destroyelem", - "seticonshader", - "geticonshader", - "seticonsize", - "setwidth", - "setheight", - "setsize", - "updatechildren", - "transitionreset", - "transitionzoomin", - "transitionpulsefxin", - "transitionslidein", - "transitionslideout", - "transitionzoomout", - "transitionfadein", - "transitionfadeout", - "getweeklyref", - "getdailyref", - "ch_getprogress", - "ch_getstate", - "ch_setprogress", - "ch_setstate", - "ch_gettarget", - } -}; - -auto compiler::map_known_includes(const std::string& include) -> bool -{ - if (include == "maps/mp/_utility") - { - includes_.push_back(include_maps_mp_utility_); - return true; - } - else if (include == "common_scripts/utility") - { - includes_.push_back(include_common_scripts_utility_); - return true; - } - else if (include == "common_scripts/_createfx") - { - includes_.push_back(include_common_scripts_createfx_); - return true; - } - else if (include == "maps/mp/gametypes/_hud_util") - { - includes_.push_back(include_maps_mp_gametypes_hud_util_); - return true; - } - return false; } -void compiler::print_debug_info() -{ - printf("----------------------------------\n"); - printf("files included: %zu\n", includes_.size()); - printf("animtrees used: %zu\n", animtrees_.size()); - printf("functions compiled: %zu\n",assembly_.size()); - - for (auto& func : assembly_) - { - print_function(func); - - for (auto& inst : func->instructions) - { - const auto itr = func->labels.find(inst->index); - - if (itr != func->labels.end()) - { - print_label(itr->second); - } - - print_instruction(inst); - } - } - - printf("----------------------------------\n"); -} - -void compiler::print_opcodes(std::uint32_t, std::uint32_t) -{ - printf(" "); -} - -void compiler::print_function(const function::ptr& func) -{ - printf("\n"); - printf("%s\n", func->name.data()); -} - -void compiler::print_instruction(const instruction::ptr& inst) -{ - switch (opcode(inst->opcode)) - { - case opcode::OP_endswitch: - print_opcodes(inst->index, 3); - printf("%s", resolver::opcode_name(inst->opcode).data()); - printf(" %s\n", inst->data[0].data()); - { - std::uint32_t totalcase = std::stoul(inst->data[0]); - auto index = 0; - for (auto casenum = 0u; casenum < totalcase; casenum++) - { - print_opcodes(inst->index, 7); - if (inst->data[1 + index] == "case") - { - printf("%s %s %s", inst->data[1 + index].data(), inst->data[1 + index + 1].data(), inst->data[1 + index + 2].data()); - index += 3; - } - else if (inst->data[1 + index] == "default") - { - printf("%s %s", inst->data[1 + index].data(), inst->data[1 + index + 1].data()); - index += 2; - } - if (casenum != totalcase - 1) - { - printf("\n"); - } - } - } - break; - default: - print_opcodes(inst->index, inst->size); - printf("%s", resolver::opcode_name(inst->opcode).data()); - for (auto& d : inst->data) - { - printf(" %s", d.data()); - } - break; - } - - printf("\n"); -} - -void compiler::print_label(const std::string& label) -{ - printf(" %s\n", label.data()); -} - } // namespace xsk::gsc::iw5c diff --git a/src/experimental/iw5c/xsk/compiler.hpp b/src/experimental/iw5c/xsk/compiler.hpp index 0ebae67f..279e1fc2 100644 --- a/src/experimental/iw5c/xsk/compiler.hpp +++ b/src/experimental/iw5c/xsk/compiler.hpp @@ -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); @@ -153,18 +153,7 @@ private: auto insert_label() -> std::string; void insert_label(const std::string& label); - static gsc::include_t include_maps_mp_utility_; - static gsc::include_t include_common_scripts_utility_; - static gsc::include_t include_common_scripts_createfx_; - static gsc::include_t include_maps_mp_gametypes_hud_util_; auto map_known_includes(const std::string& include) -> bool; - - // debug - void print_debug_info(); - void print_opcodes(std::uint32_t index, std::uint32_t size); - void print_function(const function::ptr& func); - void print_instruction(const instruction::ptr& inst); - void print_label(const std::string& label); }; } // namespace xsk::gsc::iw5c diff --git a/src/experimental/iw5c/xsk/decompiler.cpp b/src/experimental/iw5c/xsk/decompiler.cpp index 9794b442..ffb3ce12 100644 --- a/src/experimental/iw5c/xsk/decompiler.cpp +++ b/src/experimental/iw5c/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1822,7 +1822,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) { diff --git a/src/experimental/iw5c/xsk/disassembler.cpp b/src/experimental/iw5c/xsk/disassembler.cpp index 2d78427a..7f77aad7 100644 --- a/src/experimental/iw5c/xsk/disassembler.cpp +++ b/src/experimental/iw5c/xsk/disassembler.cpp @@ -77,18 +77,13 @@ void disassembler::dissasemble_function(const function::ptr& func) dissasemble_instruction(inst); - if(inst->size > size) - { - throw disasm_error("aaaaa"); - } - size -= inst->size; } } void disassembler::dissasemble_instruction(const instruction::ptr& inst) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_End: case opcode::OP_Return: @@ -465,20 +460,20 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { - case opcode::OP_GetLocalFunction: - case opcode::OP_ScriptLocalFunctionCall: - case opcode::OP_ScriptLocalFunctionCall2: - case opcode::OP_ScriptLocalMethodCall: - case opcode::OP_ScriptLocalThreadCall: - case opcode::OP_ScriptLocalChildThreadCall: - case opcode::OP_ScriptLocalMethodThreadCall: - case opcode::OP_ScriptLocalMethodChildThreadCall: - inst->data[0] = resolve_function(inst->data[0]); - break; - default: - break; + case opcode::OP_GetLocalFunction: + case opcode::OP_ScriptLocalFunctionCall: + case opcode::OP_ScriptLocalFunctionCall2: + case opcode::OP_ScriptLocalMethodCall: + case opcode::OP_ScriptLocalThreadCall: + case opcode::OP_ScriptLocalChildThreadCall: + case opcode::OP_ScriptLocalMethodThreadCall: + case opcode::OP_ScriptLocalMethodChildThreadCall: + inst->data[0] = resolve_function(inst->data[0]); + break; + default: + break; } } } @@ -490,14 +485,14 @@ 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) { return func->name; } } - //return "error"; + throw disasm_error(utils::string::va("couldn't resolve function name at index '0x%04X'!", idx)); } @@ -528,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -540,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\n", 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())); @@ -566,9 +560,9 @@ void disassembler::print_instruction(const instruction::ptr& inst) } break; default: - for (auto& d : inst->data) + for (const auto& data : inst->data) { - output_->write_string(utils::string::va(" %s", d.data())); + output_->write_string(utils::string::va(" %s", data.data())); } output_->write_string("\n"); diff --git a/src/experimental/iw5c/xsk/iw5c.cpp b/src/experimental/iw5c/xsk/iw5c.cpp index fdfed367..5f6ff468 100644 --- a/src/experimental/iw5c/xsk/iw5c.cpp +++ b/src/experimental/iw5c/xsk/iw5c.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::iw5c auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_End: case opcode::OP_Return: @@ -172,7 +172,7 @@ auto opcode_size(std::uint8_t id) -> std::uint32_t case opcode::OP_GetVector: return 13; default: - throw std::runtime_error("Couldn't resolve instruction size for " + std::to_string(id)); + throw std::runtime_error("couldn't resolve instruction size for " + std::to_string(id)); } } diff --git a/src/experimental/iw5c/xsk/lexer.cpp b/src/experimental/iw5c/xsk/lexer.cpp index 99285fad..c30f9e20 100644 --- a/src/experimental/iw5c/xsk/lexer.cpp +++ b/src/experimental/iw5c/xsk/lexer.cpp @@ -566,7 +566,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()) { @@ -586,7 +586,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_); diff --git a/src/experimental/iw5c/xsk/resolver.cpp b/src/experimental/iw5c/xsk/resolver.cpp index 80913585..3f74da6d 100644 --- a/src/experimental/iw5c/xsk/resolver.cpp +++ b/src/experimental/iw5c/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/experimental/iw5c/xsk/resolver.hpp b/src/experimental/iw5c/xsk/resolver.hpp index fcbd17a0..3ae10d75 100644 --- a/src/experimental/iw5c/xsk/resolver.hpp +++ b/src/experimental/iw5c/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/h1/xsk/assembler.cpp b/src/h1/xsk/assembler.cpp index b86ddd40..5aa9b38f 100644 --- a/src/h1/xsk/assembler.cpp +++ b/src/h1/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(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(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(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(static_cast(inst->opcode)); script_->write(0); script_->write(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(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(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(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(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(static_cast(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(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/h1/xsk/compiler.cpp b/src/h1/xsk/compiler.cpp index d00e9d58..49539c0c 100644 --- a/src/h1/xsk/compiler.cpp +++ b/src/h1/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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; } @@ -2941,7 +2939,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/h1/xsk/compiler.hpp b/src/h1/xsk/compiler.hpp index 857520bd..6b5fcec3 100644 --- a/src/h1/xsk/compiler.hpp +++ b/src/h1/xsk/compiler.hpp @@ -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); diff --git a/src/h1/xsk/decompiler.cpp b/src/h1/xsk/decompiler.cpp index adca184a..71d8a6ed 100644 --- a/src/h1/xsk/decompiler.cpp +++ b/src/h1/xsk/decompiler.cpp @@ -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(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) { diff --git a/src/h1/xsk/disassembler.cpp b/src/h1/xsk/disassembler.cpp index e03c34c2..509e9cfe 100644 --- a/src/h1/xsk/disassembler.cpp +++ b/src/h1/xsk/disassembler.cpp @@ -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(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(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(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; } diff --git a/src/h1/xsk/h1.cpp b/src/h1/xsk/h1.cpp index 142051bd..1ffef108 100644 --- a/src/h1/xsk/h1.cpp +++ b/src/h1/xsk/h1.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::h1 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_Return: case opcode::OP_BoolNot: diff --git a/src/h1/xsk/lexer.cpp b/src/h1/xsk/lexer.cpp index d5d2d8e9..4cc5335b 100644 --- a/src/h1/xsk/lexer.cpp +++ b/src/h1/xsk/lexer.cpp @@ -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_); diff --git a/src/h1/xsk/resolver.cpp b/src/h1/xsk/resolver.cpp index 9317b500..439404ac 100644 --- a/src/h1/xsk/resolver.cpp +++ b/src/h1/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/h1/xsk/resolver.hpp b/src/h1/xsk/resolver.hpp index ba133688..2e19b6ed 100644 --- a/src/h1/xsk/resolver.hpp +++ b/src/h1/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/h2/xsk/assembler.cpp b/src/h2/xsk/assembler.cpp index 13875286..5d2e0998 100644 --- a/src/h2/xsk/assembler.cpp +++ b/src/h2/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(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(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(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(static_cast(inst->opcode)); script_->write(0); script_->write(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(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(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(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(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(static_cast(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(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/h2/xsk/compiler.cpp b/src/h2/xsk/compiler.cpp index 5792d8d4..8b6fe198 100644 --- a/src/h2/xsk/compiler.cpp +++ b/src/h2/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/h2/xsk/compiler.hpp b/src/h2/xsk/compiler.hpp index 2c1b4441..18ec681c 100644 --- a/src/h2/xsk/compiler.hpp +++ b/src/h2/xsk/compiler.hpp @@ -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); diff --git a/src/h2/xsk/decompiler.cpp b/src/h2/xsk/decompiler.cpp index 7abd36b5..e0916cee 100644 --- a/src/h2/xsk/decompiler.cpp +++ b/src/h2/xsk/decompiler.cpp @@ -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(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) { diff --git a/src/h2/xsk/disassembler.cpp b/src/h2/xsk/disassembler.cpp index 848f8111..e27727ef 100644 --- a/src/h2/xsk/disassembler.cpp +++ b/src/h2/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -456,11 +456,11 @@ auto disassembler::disassemble_offset() -> std::int32_t void disassembler::resolve_local_functions() { - for (auto& func : functions_) + for (const auto& func : functions_) { - for (auto& inst : func->instructions) + for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(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(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; } diff --git a/src/h2/xsk/h2.cpp b/src/h2/xsk/h2.cpp index 2d59d6db..ae050959 100644 --- a/src/h2/xsk/h2.cpp +++ b/src/h2/xsk/h2.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::h2 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_Return: case opcode::OP_BoolNot: diff --git a/src/h2/xsk/lexer.cpp b/src/h2/xsk/lexer.cpp index e88bd95f..9009c814 100644 --- a/src/h2/xsk/lexer.cpp +++ b/src/h2/xsk/lexer.cpp @@ -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_); diff --git a/src/h2/xsk/resolver.cpp b/src/h2/xsk/resolver.cpp index f5cb2177..5b10b145 100644 --- a/src/h2/xsk/resolver.cpp +++ b/src/h2/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/h2/xsk/resolver.hpp b/src/h2/xsk/resolver.hpp index 1a17f710..14fc8980 100644 --- a/src/h2/xsk/resolver.hpp +++ b/src/h2/xsk/resolver.hpp @@ -36,7 +36,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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/iw5/xsk/assembler.cpp b/src/iw5/xsk/assembler.cpp index 54c3d1ab..926bf9e9 100644 --- a/src/iw5/xsk/assembler.cpp +++ b/src/iw5/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_End: case opcode::OP_Return: @@ -229,51 +231,41 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CastBool: case opcode::OP_BoolNot: case opcode::OP_BoolComplement: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_CreateLocalVariable: @@ -290,7 +282,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_SetLocalVariableFieldCached: case opcode::OP_ClearLocalVariableFieldCached: case opcode::OP_EvalLocalVariableObjectCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalLevelFieldVariable: @@ -313,7 +304,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptMethodChildThreadCallPointer: case opcode::OP_CallBuiltinPointer: case opcode::OP_CallBuiltinMethodPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_ScriptLocalFunctionCall2: @@ -389,8 +379,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -403,8 +391,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -418,7 +404,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -438,8 +423,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(addr - inst->index - 4); @@ -447,8 +430,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(count)); @@ -499,8 +480,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFF; @@ -516,8 +495,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/iw5/xsk/compiler.cpp b/src/iw5/xsk/compiler.cpp index 57083b8c..efc31823 100644 --- a/src/iw5/xsk/compiler.cpp +++ b/src/iw5/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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) @@ -1876,8 +1875,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; } @@ -2931,7 +2929,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: @@ -3543,91 +3541,4 @@ auto compiler::map_known_includes(const std::string& include) -> bool return false; } -void compiler::print_debug_info() -{ - printf("----------------------------------\n"); - printf("files included: %zu\n", includes_.size()); - printf("animtrees used: %zu\n", animtrees_.size()); - printf("functions compiled: %zu\n",assembly_.size()); - - for (auto& func : assembly_) - { - print_function(func); - - for (auto& inst : func->instructions) - { - const auto itr = func->labels.find(inst->index); - - if (itr != func->labels.end()) - { - print_label(itr->second); - } - - print_instruction(inst); - } - } - - printf("----------------------------------\n"); -} - -void compiler::print_opcodes(std::uint32_t, std::uint32_t) -{ - printf(" "); -} - -void compiler::print_function(const function::ptr& func) -{ - printf("\n"); - printf("%s\n", func->name.data()); -} - -void compiler::print_instruction(const instruction::ptr& inst) -{ - switch (opcode(inst->opcode)) - { - case opcode::OP_endswitch: - print_opcodes(inst->index, 3); - printf("%s", resolver::opcode_name(inst->opcode).data()); - printf(" %s\n", inst->data[0].data()); - { - std::uint32_t totalcase = std::stoul(inst->data[0]); - auto index = 0; - for (auto casenum = 0u; casenum < totalcase; casenum++) - { - print_opcodes(inst->index, 7); - if (inst->data[1 + index] == "case") - { - printf("%s %s %s", inst->data[1 + index].data(), inst->data[1 + index + 1].data(), inst->data[1 + index + 2].data()); - index += 3; - } - else if (inst->data[1 + index] == "default") - { - printf("%s %s", inst->data[1 + index].data(), inst->data[1 + index + 1].data()); - index += 2; - } - if (casenum != totalcase - 1) - { - printf("\n"); - } - } - } - break; - default: - print_opcodes(inst->index, inst->size); - printf("%s", resolver::opcode_name(inst->opcode).data()); - for (auto& d : inst->data) - { - printf(" %s", d.data()); - } - break; - } - - printf("\n"); -} - -void compiler::print_label(const std::string& label) -{ - printf(" %s\n", label.data()); -} - } // namespace xsk::gsc::iw5 diff --git a/src/iw5/xsk/compiler.hpp b/src/iw5/xsk/compiler.hpp index e2f087db..21853616 100644 --- a/src/iw5/xsk/compiler.hpp +++ b/src/iw5/xsk/compiler.hpp @@ -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); @@ -158,13 +158,6 @@ private: static gsc::include_t include_common_scripts_createfx_; static gsc::include_t include_maps_mp_gametypes_hud_util_; auto map_known_includes(const std::string& include) -> bool; - - // debug - void print_debug_info(); - void print_opcodes(std::uint32_t index, std::uint32_t size); - void print_function(const function::ptr& func); - void print_instruction(const instruction::ptr& inst); - void print_label(const std::string& label); }; } // namespace xsk::gsc::iw5 diff --git a/src/iw5/xsk/decompiler.cpp b/src/iw5/xsk/decompiler.cpp index bbe8de0c..8e0a7b89 100644 --- a/src/iw5/xsk/decompiler.cpp +++ b/src/iw5/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1822,7 +1822,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) { diff --git a/src/iw5/xsk/disassembler.cpp b/src/iw5/xsk/disassembler.cpp index 64f987c2..50c0961c 100644 --- a/src/iw5/xsk/disassembler.cpp +++ b/src/iw5/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: case opcode::OP_Return: @@ -459,7 +459,7 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -484,7 +484,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) { @@ -522,7 +522,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -534,8 +534,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())); @@ -560,9 +559,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())); } output_->write_string("\n"); diff --git a/src/iw5/xsk/iw5.cpp b/src/iw5/xsk/iw5.cpp index f0453f50..161f0176 100644 --- a/src/iw5/xsk/iw5.cpp +++ b/src/iw5/xsk/iw5.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::iw5 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_End: case opcode::OP_Return: diff --git a/src/iw5/xsk/lexer.cpp b/src/iw5/xsk/lexer.cpp index b48fb6f5..b731f2fb 100644 --- a/src/iw5/xsk/lexer.cpp +++ b/src/iw5/xsk/lexer.cpp @@ -566,7 +566,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()) { @@ -586,7 +586,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_); diff --git a/src/iw5/xsk/resolver.cpp b/src/iw5/xsk/resolver.cpp index 3755806f..06b72d17 100644 --- a/src/iw5/xsk/resolver.cpp +++ b/src/iw5/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/iw5/xsk/resolver.hpp b/src/iw5/xsk/resolver.hpp index 61fc4152..eda9b798 100644 --- a/src/iw5/xsk/resolver.hpp +++ b/src/iw5/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/iw6/xsk/assembler.cpp b/src/iw6/xsk/assembler.cpp index 28c4d4a3..88fc26a1 100644 --- a/src/iw6/xsk/assembler.cpp +++ b/src/iw6/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -229,52 +231,42 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_AddArray: case opcode::OP_waittillmatch2: case opcode::OP_shift_right: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_SetNewLocalVariableFieldCached0: @@ -291,7 +283,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CreateLocalVariable: case opcode::OP_EvalLocalVariableObjectCached: case opcode::OP_EvalLocalArrayCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalSelfFieldVariable: @@ -314,7 +305,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptChildThreadCallPointer: case opcode::OP_ScriptMethodThreadCallPointer: case opcode::OP_ScriptMethodChildThreadCallPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetLocalFunction: @@ -390,8 +380,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -404,8 +392,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -419,7 +405,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -439,8 +424,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(addr - inst->index - 4); @@ -448,8 +431,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(count)); @@ -500,8 +481,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFF; @@ -517,8 +496,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/iw6/xsk/compiler.cpp b/src/iw6/xsk/compiler.cpp index 25b7a71a..20ace881 100644 --- a/src/iw6/xsk/compiler.cpp +++ b/src/iw6/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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) @@ -1876,8 +1875,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; } @@ -2931,7 +2929,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/iw6/xsk/compiler.hpp b/src/iw6/xsk/compiler.hpp index 091594e6..86f3a26e 100644 --- a/src/iw6/xsk/compiler.hpp +++ b/src/iw6/xsk/compiler.hpp @@ -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); diff --git a/src/iw6/xsk/decompiler.cpp b/src/iw6/xsk/decompiler.cpp index f41e93d5..e15be07c 100644 --- a/src/iw6/xsk/decompiler.cpp +++ b/src/iw6/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1822,7 +1822,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) { diff --git a/src/iw6/xsk/disassembler.cpp b/src/iw6/xsk/disassembler.cpp index 29bc14d6..a719bbe0 100644 --- a/src/iw6/xsk/disassembler.cpp +++ b/src/iw6/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -459,7 +459,7 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -484,7 +484,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) { @@ -522,7 +522,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -534,8 +534,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())); @@ -562,9 +561,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; } diff --git a/src/iw6/xsk/iw6.cpp b/src/iw6/xsk/iw6.cpp index c235e9ab..097a37bd 100644 --- a/src/iw6/xsk/iw6.cpp +++ b/src/iw6/xsk/iw6.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::iw6 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_Return: case opcode::OP_BoolNot: diff --git a/src/iw6/xsk/lexer.cpp b/src/iw6/xsk/lexer.cpp index 2e0b2a09..500b0078 100644 --- a/src/iw6/xsk/lexer.cpp +++ b/src/iw6/xsk/lexer.cpp @@ -566,7 +566,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()) { @@ -586,7 +586,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_); diff --git a/src/iw6/xsk/resolver.cpp b/src/iw6/xsk/resolver.cpp index 040ba477..f6477123 100644 --- a/src/iw6/xsk/resolver.cpp +++ b/src/iw6/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/iw6/xsk/resolver.hpp b/src/iw6/xsk/resolver.hpp index e353a0d8..45820ab2 100644 --- a/src/iw6/xsk/resolver.hpp +++ b/src/iw6/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/iw7/xsk/assembler.cpp b/src/iw7/xsk/assembler.cpp index 3a38db01..1a2051cd 100644 --- a/src/iw7/xsk/assembler.cpp +++ b/src/iw7/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -229,52 +231,42 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_AddArray: case opcode::OP_waittillmatch2: case opcode::OP_shift_right: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_SetNewLocalVariableFieldCached0: @@ -291,7 +283,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CreateLocalVariable: case opcode::OP_EvalLocalVariableObjectCached: case opcode::OP_EvalLocalArrayCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalSelfFieldVariable: @@ -314,7 +305,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptChildThreadCallPointer: case opcode::OP_ScriptMethodThreadCallPointer: case opcode::OP_ScriptMethodChildThreadCallPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetLocalFunction: @@ -390,8 +380,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -404,8 +392,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -419,7 +405,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -439,8 +424,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(addr - inst->index - 4); @@ -448,8 +431,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(count)); @@ -500,8 +481,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFFFFFF; @@ -517,8 +496,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/iw7/xsk/compiler.cpp b/src/iw7/xsk/compiler.cpp index ee86edd5..781e1465 100644 --- a/src/iw7/xsk/compiler.cpp +++ b/src/iw7/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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) @@ -1876,8 +1875,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; } @@ -2931,7 +2929,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/iw7/xsk/compiler.hpp b/src/iw7/xsk/compiler.hpp index 6817836c..5ff5dec6 100644 --- a/src/iw7/xsk/compiler.hpp +++ b/src/iw7/xsk/compiler.hpp @@ -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); diff --git a/src/iw7/xsk/decompiler.cpp b/src/iw7/xsk/decompiler.cpp index 37f1b6b6..9c43788e 100644 --- a/src/iw7/xsk/decompiler.cpp +++ b/src/iw7/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1822,7 +1822,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) { diff --git a/src/iw7/xsk/disassembler.cpp b/src/iw7/xsk/disassembler.cpp index c2fe0bc6..b36d51dc 100644 --- a/src/iw7/xsk/disassembler.cpp +++ b/src/iw7/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -459,7 +459,7 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -484,7 +484,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) { @@ -522,7 +522,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -534,8 +534,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())); @@ -562,9 +561,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; } diff --git a/src/iw7/xsk/iw7.cpp b/src/iw7/xsk/iw7.cpp index 69e49951..a2f82557 100644 --- a/src/iw7/xsk/iw7.cpp +++ b/src/iw7/xsk/iw7.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::iw7 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_Return: case opcode::OP_BoolNot: diff --git a/src/iw7/xsk/lexer.cpp b/src/iw7/xsk/lexer.cpp index 1ec6bc58..139eb555 100644 --- a/src/iw7/xsk/lexer.cpp +++ b/src/iw7/xsk/lexer.cpp @@ -566,7 +566,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()) { @@ -586,7 +586,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_); diff --git a/src/iw7/xsk/resolver.cpp b/src/iw7/xsk/resolver.cpp index d93c9a92..ba8776e1 100644 --- a/src/iw7/xsk/resolver.cpp +++ b/src/iw7/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/iw7/xsk/resolver.hpp b/src/iw7/xsk/resolver.hpp index 081d5ade..ab804781 100644 --- a/src/iw7/xsk/resolver.hpp +++ b/src/iw7/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/iw8/xsk/assembler.cpp b/src/iw8/xsk/assembler.cpp index 2d08bfa9..5d2e957e 100644 --- a/src/iw8/xsk/assembler.cpp +++ b/src/iw8/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_CastFieldObject: case opcode::OP_plus: @@ -242,52 +244,42 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_BoolNotAfterAnd: case opcode::OP_IsDefined: case opcode::OP_IsTrue: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(encrypt_string(inst->data[0])); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(encrypt_string(inst->data[0])); stack_->write_c_string(encrypt_string(inst->data[1])); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(encrypt_string(inst->data[0])); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_SetNewLocalVariableFieldCached0: @@ -304,7 +296,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CreateLocalVariable: case opcode::OP_EvalLocalVariableObjectCached: case opcode::OP_EvalLocalArrayCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalSelfFieldVariable: @@ -327,7 +318,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptChildThreadCallPointer: case opcode::OP_ScriptMethodThreadCallPointer: case opcode::OP_ScriptMethodChildThreadCallPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetLocalFunction: @@ -445,8 +435,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -459,8 +447,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -474,7 +460,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -494,8 +479,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(addr - inst->index - 4); @@ -503,8 +486,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(count)); @@ -555,8 +536,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFFFFFF; @@ -572,8 +551,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_formal_params(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoi(inst->data[0]); script_->write(static_cast(count)); @@ -586,8 +563,6 @@ void assembler::assemble_formal_params(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/iw8/xsk/compiler.cpp b/src/iw8/xsk/compiler.cpp index 0186ed33..25c4e5d1 100644 --- a/src/iw8/xsk/compiler.cpp +++ b/src/iw8/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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) @@ -1935,8 +1934,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; } @@ -2991,7 +2989,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/iw8/xsk/compiler.hpp b/src/iw8/xsk/compiler.hpp index 0005043d..3e17576c 100644 --- a/src/iw8/xsk/compiler.hpp +++ b/src/iw8/xsk/compiler.hpp @@ -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); diff --git a/src/iw8/xsk/decompiler.cpp b/src/iw8/xsk/decompiler.cpp index 2c0d545f..5473a298 100644 --- a/src/iw8/xsk/decompiler.cpp +++ b/src/iw8/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1859,7 +1859,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) { diff --git a/src/iw8/xsk/disassembler.cpp b/src/iw8/xsk/disassembler.cpp index 2b334ee8..432d5728 100644 --- a/src/iw8/xsk/disassembler.cpp +++ b/src/iw8/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_CastFieldObject: case opcode::OP_plus: @@ -523,7 +523,7 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -548,7 +548,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) { @@ -603,7 +603,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -615,8 +615,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())); @@ -643,9 +642,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; } diff --git a/src/iw8/xsk/iw8.cpp b/src/iw8/xsk/iw8.cpp index f19d8630..1851ba80 100644 --- a/src/iw8/xsk/iw8.cpp +++ b/src/iw8/xsk/iw8.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::iw8 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_CastFieldObject: case opcode::OP_plus: diff --git a/src/iw8/xsk/lexer.cpp b/src/iw8/xsk/lexer.cpp index 6814c600..3f9229ce 100644 --- a/src/iw8/xsk/lexer.cpp +++ b/src/iw8/xsk/lexer.cpp @@ -569,7 +569,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()) { @@ -600,7 +600,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_); diff --git a/src/iw8/xsk/resolver.cpp b/src/iw8/xsk/resolver.cpp index 51e13d55..efc4d6a6 100644 --- a/src/iw8/xsk/resolver.cpp +++ b/src/iw8/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/iw8/xsk/resolver.hpp b/src/iw8/xsk/resolver.hpp index 8a043999..0be8ceda 100644 --- a/src/iw8/xsk/resolver.hpp +++ b/src/iw8/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/s1/xsk/assembler.cpp b/src/s1/xsk/assembler.cpp index a3d788f7..e343fd0c 100644 --- a/src/s1/xsk/assembler.cpp +++ b/src/s1/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(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(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); script_->write(static_cast(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(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(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(static_cast(inst->opcode)); script_->write(0); script_->write(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(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(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(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(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(static_cast(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(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/s1/xsk/compiler.cpp b/src/s1/xsk/compiler.cpp index f570af95..0ae391df 100644 --- a/src/s1/xsk/compiler.cpp +++ b/src/s1/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/s1/xsk/compiler.hpp b/src/s1/xsk/compiler.hpp index 4f285267..f444da84 100644 --- a/src/s1/xsk/compiler.hpp +++ b/src/s1/xsk/compiler.hpp @@ -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); diff --git a/src/s1/xsk/decompiler.cpp b/src/s1/xsk/decompiler.cpp index 5f491ff9..fd7e6719 100644 --- a/src/s1/xsk/decompiler.cpp +++ b/src/s1/xsk/decompiler.cpp @@ -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(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) { diff --git a/src/s1/xsk/disassembler.cpp b/src/s1/xsk/disassembler.cpp index 698363fb..8eaf1cb4 100644 --- a/src/s1/xsk/disassembler.cpp +++ b/src/s1/xsk/disassembler.cpp @@ -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(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(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(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; } diff --git a/src/s1/xsk/lexer.cpp b/src/s1/xsk/lexer.cpp index 402e0b40..76adf52b 100644 --- a/src/s1/xsk/lexer.cpp +++ b/src/s1/xsk/lexer.cpp @@ -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_); diff --git a/src/s1/xsk/resolver.cpp b/src/s1/xsk/resolver.cpp index 9c921a53..dbff5bc0 100644 --- a/src/s1/xsk/resolver.cpp +++ b/src/s1/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/s1/xsk/resolver.hpp b/src/s1/xsk/resolver.hpp index 623b6329..8bf65c5b 100644 --- a/src/s1/xsk/resolver.hpp +++ b/src/s1/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/s1/xsk/s1.cpp b/src/s1/xsk/s1.cpp index c4b7dc40..d6b82a32 100644 --- a/src/s1/xsk/s1.cpp +++ b/src/s1/xsk/s1.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::s1 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_Return: case opcode::OP_BoolNot: diff --git a/src/s2/xsk/assembler.cpp b/src/s2/xsk/assembler.cpp index 4271f951..a3569e28 100644 --- a/src/s2/xsk/assembler.cpp +++ b/src/s2/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -231,52 +233,42 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_waittillmatch2: case opcode::OP_shift_right: case opcode::OP_BoolNotAfterAnd: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_SetNewLocalVariableFieldCached0: @@ -293,7 +285,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CreateLocalVariable: case opcode::OP_EvalLocalVariableObjectCached: case opcode::OP_EvalLocalArrayCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalSelfFieldVariable: @@ -316,7 +307,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptChildThreadCallPointer: case opcode::OP_ScriptMethodThreadCallPointer: case opcode::OP_ScriptMethodChildThreadCallPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetLocalFunction: @@ -392,8 +382,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -406,8 +394,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -421,7 +407,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -441,8 +426,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(addr - inst->index - 4); @@ -450,8 +433,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(count)); @@ -502,8 +483,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFF; @@ -519,8 +498,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/s2/xsk/compiler.cpp b/src/s2/xsk/compiler.cpp index fb4515f2..5f790544 100644 --- a/src/s2/xsk/compiler.cpp +++ b/src/s2/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/s2/xsk/compiler.hpp b/src/s2/xsk/compiler.hpp index b618fbcb..4d0aab5d 100644 --- a/src/s2/xsk/compiler.hpp +++ b/src/s2/xsk/compiler.hpp @@ -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); diff --git a/src/s2/xsk/decompiler.cpp b/src/s2/xsk/decompiler.cpp index d7e45cc4..6b54a5e4 100644 --- a/src/s2/xsk/decompiler.cpp +++ b/src/s2/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1835,7 +1835,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) { diff --git a/src/s2/xsk/disassembler.cpp b/src/s2/xsk/disassembler.cpp index 130431f9..032e8136 100644 --- a/src/s2/xsk/disassembler.cpp +++ b/src/s2/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_Return: case opcode::OP_BoolNot: @@ -461,7 +461,7 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -486,7 +486,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) { @@ -524,7 +524,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -536,8 +536,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())); @@ -564,9 +563,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; } diff --git a/src/s2/xsk/lexer.cpp b/src/s2/xsk/lexer.cpp index 00ea9894..8fa03fbb 100644 --- a/src/s2/xsk/lexer.cpp +++ b/src/s2/xsk/lexer.cpp @@ -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_); diff --git a/src/s2/xsk/resolver.cpp b/src/s2/xsk/resolver.cpp index 0b62b438..653560b4 100644 --- a/src/s2/xsk/resolver.cpp +++ b/src/s2/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/s2/xsk/resolver.hpp b/src/s2/xsk/resolver.hpp index dbe501ae..0edafe96 100644 --- a/src/s2/xsk/resolver.hpp +++ b/src/s2/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/s2/xsk/s2.cpp b/src/s2/xsk/s2.cpp index dc548879..1395ba6a 100644 --- a/src/s2/xsk/s2.cpp +++ b/src/s2/xsk/s2.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::s2 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_Return: case opcode::OP_BoolNot: diff --git a/src/s4/xsk/assembler.cpp b/src/s4/xsk/assembler.cpp index 42627c84..584fd61f 100644 --- a/src/s4/xsk/assembler.cpp +++ b/src/s4/xsk/assembler.cpp @@ -87,12 +87,12 @@ void assembler::assemble(const std::string& file, std::vector& dat { auto inst = std::make_unique(); inst->index = index; - inst->opcode = static_cast(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(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(static_cast(inst->opcode)); + + switch (static_cast(inst->opcode)) { case opcode::OP_CastFieldObject: case opcode::OP_plus: @@ -242,52 +244,42 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_BoolNotAfterAnd: case opcode::OP_IsDefined: case opcode::OP_IsTrue: - script_->write(static_cast(inst->opcode)); break; case opcode::OP_GetByte: case opcode::OP_GetNegByte: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetUnsignedShort: case opcode::OP_GetNegUnsignedShort: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetInteger: - script_->write(static_cast(inst->opcode)); script_->write(std::stoi(inst->data[0])); break; case opcode::OP_GetFloat: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); break; case opcode::OP_GetVector: - script_->write(static_cast(inst->opcode)); script_->write(std::stof(inst->data[0])); script_->write(std::stof(inst->data[1])); script_->write(std::stof(inst->data[2])); break; case opcode::OP_GetString: case opcode::OP_GetIString: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_GetAnimation: - script_->write(static_cast(inst->opcode)); script_->write(0); script_->write(0); stack_->write_c_string(inst->data[0]); stack_->write_c_string(inst->data[1]); break; case opcode::OP_GetAnimTree: - script_->write(static_cast(inst->opcode)); script_->write(0); stack_->write_c_string(inst->data[0]); break; case opcode::OP_waittillmatch: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_SetNewLocalVariableFieldCached0: @@ -304,7 +296,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_CreateLocalVariable: case opcode::OP_EvalLocalVariableObjectCached: case opcode::OP_EvalLocalArrayCached: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_EvalSelfFieldVariable: @@ -327,7 +318,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) case opcode::OP_ScriptChildThreadCallPointer: case opcode::OP_ScriptMethodThreadCallPointer: case opcode::OP_ScriptMethodChildThreadCallPointer: - script_->write(static_cast(inst->opcode)); script_->write(static_cast(std::stoi(inst->data[0]))); break; case opcode::OP_GetLocalFunction: @@ -445,8 +435,6 @@ void assembler::assemble_instruction(const instruction::ptr& inst) void assembler::assemble_builtin_call(const instruction::ptr& inst, bool method, bool args) { - script_->write(static_cast(inst->opcode)); - if (args) { script_->write(static_cast(std::stoi(inst->data[1]))); @@ -459,8 +447,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(static_cast(inst->opcode)); - const auto addr = resolve_function(inst->data[0]); const auto offset = static_cast(addr - inst->index - 1); @@ -474,7 +460,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(static_cast(inst->opcode)); script_->write(0); script_->write(0); @@ -494,8 +479,6 @@ void assembler::assemble_far_call(const instruction::ptr& inst, bool thread) void assembler::assemble_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); script_->write(addr - inst->index - 4); @@ -503,8 +486,6 @@ void assembler::assemble_switch(const instruction::ptr& inst) void assembler::assemble_end_switch(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoul(inst->data[0]); script_->write(static_cast(count)); @@ -555,8 +536,6 @@ void assembler::assemble_end_switch(const instruction::ptr& inst) void assembler::assemble_field_variable(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - auto id = resolver::token_id(inst->data[0]); if (id == 0) id = 0xFFFFFFFF; @@ -572,8 +551,6 @@ void assembler::assemble_field_variable(const instruction::ptr& inst) void assembler::assemble_formal_params(const instruction::ptr& inst) { - script_->write(static_cast(inst->opcode)); - const auto count = std::stoi(inst->data[0]); script_->write(static_cast(count)); @@ -586,8 +563,6 @@ void assembler::assemble_formal_params(const instruction::ptr& inst) void assembler::assemble_jump(const instruction::ptr& inst, bool expr, bool back) { - script_->write(static_cast(inst->opcode)); - const auto addr = resolve_label(inst->data[0]); if (expr) diff --git a/src/s4/xsk/compiler.cpp b/src/s4/xsk/compiler.cpp index eb46c98e..11c1d2df 100644 --- a/src/s4/xsk/compiler.cpp +++ b/src/s4/xsk/compiler.cpp @@ -20,7 +20,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(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) @@ -1935,8 +1934,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; } @@ -2991,7 +2989,7 @@ void compiler::insert_label(const std::string& name) { for (auto& inst : function_->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_JumpOnFalse: case opcode::OP_JumpOnTrue: diff --git a/src/s4/xsk/compiler.hpp b/src/s4/xsk/compiler.hpp index e714ad21..194ffcb4 100644 --- a/src/s4/xsk/compiler.hpp +++ b/src/s4/xsk/compiler.hpp @@ -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); diff --git a/src/s4/xsk/decompiler.cpp b/src/s4/xsk/decompiler.cpp index 5413ad19..e779919e 100644 --- a/src/s4/xsk/decompiler.cpp +++ b/src/s4/xsk/decompiler.cpp @@ -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(inst->opcode)) { case opcode::OP_End: { @@ -1859,7 +1859,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) { diff --git a/src/s4/xsk/disassembler.cpp b/src/s4/xsk/disassembler.cpp index 88f1bc97..456a4ffb 100644 --- a/src/s4/xsk/disassembler.cpp +++ b/src/s4/xsk/disassembler.cpp @@ -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(inst->opcode)) { case opcode::OP_CastFieldObject: case opcode::OP_plus: @@ -525,7 +525,7 @@ void disassembler::resolve_local_functions() { for (const auto& inst : func->instructions) { - switch (opcode(inst->opcode)) + switch (static_cast(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -550,7 +550,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) { @@ -588,7 +588,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(inst->opcode)) { case opcode::OP_GetLocalFunction: case opcode::OP_ScriptLocalFunctionCall: @@ -600,8 +600,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())); @@ -628,9 +627,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; } diff --git a/src/s4/xsk/lexer.cpp b/src/s4/xsk/lexer.cpp index 4af77a3a..62deebe8 100644 --- a/src/s4/xsk/lexer.cpp +++ b/src/s4/xsk/lexer.cpp @@ -569,7 +569,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()) { @@ -600,7 +600,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_); diff --git a/src/s4/xsk/resolver.cpp b/src/s4/xsk/resolver.cpp index 11a6bca5..2052cf8c 100644 --- a/src/s4/xsk/resolver.cpp +++ b/src/s4/xsk/resolver.cpp @@ -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 +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/s4/xsk/resolver.hpp b/src/s4/xsk/resolver.hpp index 671e785d..a5bf4315 100644 --- a/src/s4/xsk/resolver.hpp +++ b/src/s4/xsk/resolver.hpp @@ -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; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; }; diff --git a/src/s4/xsk/s4.cpp b/src/s4/xsk/s4.cpp index fac0b3d8..d9169b0b 100644 --- a/src/s4/xsk/s4.cpp +++ b/src/s4/xsk/s4.cpp @@ -11,7 +11,7 @@ namespace xsk::gsc::s4 auto opcode_size(std::uint8_t id) -> std::uint32_t { - switch (opcode(id)) + switch (static_cast(id)) { case opcode::OP_CastFieldObject: case opcode::OP_plus: diff --git a/src/t6/xsk/assembler.cpp b/src/t6/xsk/assembler.cpp index e235e231..193d73f4 100644 --- a/src/t6/xsk/assembler.cpp +++ b/src/t6/xsk/assembler.cpp @@ -200,7 +200,7 @@ void assembler::assemble_function(const function::ptr& func) func->size += inst->size; - const auto& itr = func->labels.find(old_idx); + const auto itr = func->labels.find(old_idx); if (itr != func->labels.end()) { @@ -799,7 +799,7 @@ auto assembler::resolve_label(const std::string& name) -> std::int32_t auto assembler::string_offset(const std::string& name) -> std::uint16_t { - const auto& itr = stringlist_.find(name); + const auto itr = stringlist_.find(name); if (itr != stringlist_.end()) { diff --git a/src/t6/xsk/compiler.cpp b/src/t6/xsk/compiler.cpp index 0734b00a..8e43a1e8 100644 --- a/src/t6/xsk/compiler.cpp +++ b/src/t6/xsk/compiler.cpp @@ -40,7 +40,7 @@ void compiler::compile(const std::string& file, std::vector& data) { filename_ = file; - auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); + auto prog = parse_buffer(filename_, reinterpret_cast(data.data()), data.size()); compile_program(prog); } @@ -50,7 +50,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); @@ -147,7 +147,7 @@ void compiler::emit_decl_usingtree(const ast::decl_usingtree::ptr& animtree) { if (developer_thread_) { - throw comp_error(animtree->loc(), "cannot put #using_animtree inside /# ... #/ comment"); + throw comp_error(animtree->loc(), "cannot put #using_animtree inside developer block comment"); } animtrees_.push_back({ animtree->name->value, false }); @@ -155,10 +155,10 @@ void compiler::emit_decl_usingtree(const ast::decl_usingtree::ptr& animtree) void compiler::emit_decl_constant(const ast::decl_constant::ptr& constant) { - if (constants_.contains(constant->name->value)) - { - throw comp_error(constant->loc(), "duplicated constant definition"); - } + const auto itr = constants_.find(constant->name->value); + + if (itr != constants_.end()) + throw comp_error(constant->loc(), "duplicated constant '" + constant->name->value + "'"); constants_.insert({ constant->name->value, std::move(constant->value) }); } @@ -1632,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) { - const auto& itr = constants_.find(expr->value); + const auto itr = constants_.find(expr->value); if (itr != constants_.end()) { @@ -1723,7 +1723,7 @@ void compiler::emit_expr_field(const ast::expr_field::ptr& expr) void compiler::emit_expr_local(const ast::expr_identifier::ptr& expr) { // 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()) emit_expr(itr->second); @@ -2329,7 +2329,7 @@ auto compiler::create_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()) { @@ -2346,7 +2346,7 @@ auto compiler::insert_label() -> std::string 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()) { @@ -2387,7 +2387,7 @@ void compiler::print_function(const function::ptr& func) output_->write_string("\n"); output_->write_string(utils::string::va("sub_%s %i %i\n", func->name.data(), func->params, func->flags)); - for (auto& inst : func->instructions) + for (const auto& inst : func->instructions) { const auto itr = func->labels.find(inst->index); diff --git a/src/t6/xsk/compiler.hpp b/src/t6/xsk/compiler.hpp index 190f9ce1..d737e0d2 100644 --- a/src/t6/xsk/compiler.hpp +++ b/src/t6/xsk/compiler.hpp @@ -28,6 +28,7 @@ class compiler : public arc::compiler bool can_continue_; bool developer_thread_; build mode_; + utils::byte_buffer::ptr output_; public: auto output() -> assembly::ptr; @@ -36,7 +37,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); @@ -154,10 +155,7 @@ private: auto create_label() -> std::string; auto insert_label() -> std::string; void insert_label(const std::string& label); - auto map_known_includes(const std::string& include) -> bool; - - utils::byte_buffer::ptr output_; void print_function(const function::ptr& func); void print_instruction(const instruction::ptr& inst); }; diff --git a/src/t6/xsk/decompiler.cpp b/src/t6/xsk/decompiler.cpp index d8bdeca9..f994a445 100644 --- a/src/t6/xsk/decompiler.cpp +++ b/src/t6/xsk/decompiler.cpp @@ -77,7 +77,7 @@ void decompiler::decompile_function(const function::ptr& func) if (stack_.size() > 0) { - throw decomp_error("stack isn't emty at function end " + func->name); + throw decomp_error("stack isn't empty at function end " + func->name); } const auto& stmt = func_->stmt; @@ -1184,7 +1184,7 @@ void decompiler::decompile_instruction(const instruction::ptr& inst, bool last) void decompiler::decompile_expressions(const instruction::ptr& inst) { - const auto& itr = labels_.find(inst->index); + const auto itr = labels_.find(inst->index); if (itr == labels_.end()) return; @@ -1347,7 +1347,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) { diff --git a/src/t6/xsk/disassembler.cpp b/src/t6/xsk/disassembler.cpp index 38b2be4b..626814c3 100644 --- a/src/t6/xsk/disassembler.cpp +++ b/src/t6/xsk/disassembler.cpp @@ -453,7 +453,7 @@ void disassembler::disassemble_string(const instruction::ptr& inst) { inst->size += script_->align(2); - const auto& entry = string_refs_.find(script_->pos()); + const auto entry = string_refs_.find(script_->pos()); if (entry != string_refs_.end()) { @@ -470,7 +470,7 @@ void disassembler::disassemble_animation(const instruction::ptr& inst) inst->size += script_->align(4); const auto ref = script_->pos(); - const auto& entry = anim_refs_.find(ref); + const auto entry = anim_refs_.find(ref); if (entry != anim_refs_.end()) { @@ -506,7 +506,7 @@ void disassembler::disassemble_import(const instruction::ptr& inst) inst->size += script_->align(4); script_->seek(4); - const auto& entry = import_refs_.find(inst->index); + const auto entry = import_refs_.find(inst->index); if (entry != import_refs_.end()) { @@ -544,7 +544,7 @@ void disassembler::disassemble_end_switch(const instruction::ptr& inst) { inst->size += script_->align(4); - const auto& itr = labels_.find(script_->pos()); + const auto itr = labels_.find(script_->pos()); if (itr != labels_.end()) { @@ -557,7 +557,7 @@ void disassembler::disassemble_end_switch(const instruction::ptr& inst) labels_.erase(script_->pos()); const auto label = utils::string::va("loc_%X", inst->index); - const auto& itr2 = labels_.find(inst->index); + const auto itr2 = labels_.find(inst->index); if (itr2 == labels_.end()) { @@ -615,7 +615,7 @@ void disassembler::print_function(const function::ptr& func) for (const auto& inst : func->instructions) { - const auto& itr = func->labels.find(inst->index); + const auto itr = func->labels.find(inst->index); if (itr != func->labels.end()) { @@ -632,7 +632,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(inst->opcode)) { case opcode::OP_GetHash: case opcode::OP_GetString: @@ -653,9 +653,9 @@ void disassembler::print_instruction(const instruction::ptr& inst) output_->write_string(utils::string::va("\"%s\", \"%s\"", inst->data[0].data(), inst->data[1].data())); break; case opcode::OP_SafeCreateLocalVariables: - for (const auto& d : inst->data) + for (const auto& data : inst->data) { - output_->write_string(utils::string::va("\"%s\"%s", d.data(), &d == &inst->data.back() ? "" : ", ")); + output_->write_string(utils::string::va("\"%s\"%s", data.data(), &data == &inst->data.back() ? "" : ", ")); } break; case opcode::OP_EndSwitch: @@ -681,9 +681,9 @@ void disassembler::print_instruction(const instruction::ptr& inst) } break; default: - for (const auto& d : inst->data) + for (const auto& data : inst->data) { - output_->write_string(utils::string::va("%s%s", d.data(), &d == &inst->data.back() ? "" : ", ")); + output_->write_string(utils::string::va("%s%s", data.data(), &data == &inst->data.back() ? "" : ", ")); } break; } diff --git a/src/t6/xsk/lexer.cpp b/src/t6/xsk/lexer.cpp index 1ba54a56..2a659515 100644 --- a/src/t6/xsk/lexer.cpp +++ b/src/t6/xsk/lexer.cpp @@ -580,7 +580,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()) { @@ -608,7 +608,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_); diff --git a/src/t6/xsk/resolver.cpp b/src/t6/xsk/resolver.cpp index 8b240d4a..8f01cffe 100644 --- a/src/t6/xsk/resolver.cpp +++ b/src/t6/xsk/resolver.cpp @@ -32,7 +32,7 @@ void resolver::cleanup() auto resolver::opcode_id(const std::string& name) -> std::uint8_t { - const auto& itr = opcode_map_rev.find(name); + const auto itr = opcode_map_rev.find(name); if (itr != opcode_map_rev.end()) { @@ -44,7 +44,7 @@ auto resolver::opcode_id(const std::string& name) -> std::uint8_t auto resolver::opcode_name(std::uint8_t id) -> std::string { - const auto& itr = opcode_map.find(id); + const auto itr = opcode_map.find(id); if (itr != opcode_map.end()) { @@ -56,7 +56,7 @@ auto resolver::opcode_name(std::uint8_t id) -> std::string auto resolver::dvar_name(std::uint32_t id) -> std::string { - const auto& itr = dvar_map.find(id); + const auto itr = dvar_map.find(id); if (itr != dvar_map.end()) { @@ -84,22 +84,22 @@ auto resolver::make_token(std::string_view str) -> std::string return data; } -auto resolver::file_data(const std::string& name) -> std::tuple +auto resolver::file_data(const std::string& name) -> std::tuple { - const auto& itr = files.find(name); + const auto itr = files.find(name); if (itr != files.end()) { - return { &itr->first ,reinterpret_cast(itr->second.data()), itr->second.size() }; + return { &itr->first ,reinterpret_cast(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(res.first->second.data()), res.first->second.size() }; + return { &res.first->first, reinterpret_cast(res.first->second.data()), res.first->second.size() }; } throw error("couldn't open gsc file '" + name + "'"); diff --git a/src/t6/xsk/resolver.hpp b/src/t6/xsk/resolver.hpp index 62c1b413..3bb44bcf 100644 --- a/src/t6/xsk/resolver.hpp +++ b/src/t6/xsk/resolver.hpp @@ -18,7 +18,7 @@ public: static auto dvar_name(std::uint32_t id) -> std::string; static auto make_token(std::string_view str) -> std::string; - static auto file_data(const std::string& name) -> std::tuple; + static auto file_data(const std::string& name) -> std::tuple; static auto fs_to_game_path(const std::filesystem::path& file) -> std::filesystem::path; };