Skip to content

Commit

Permalink
Automatically get float label type in most cases (#463)
Browse files Browse the repository at this point in the history
* automatically get float type in most cases

* try harder to print json errors

* fix test
  • Loading branch information
water111 authored May 12, 2021
1 parent 0599d14 commit 223adee
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 178 deletions.
14 changes: 12 additions & 2 deletions common/util/json_util.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common/log/log.h"
#include "common/util/assert.h"
#include "json_util.h"

Expand Down Expand Up @@ -75,8 +76,17 @@ std::string strip_cpp_style_comments(const std::string& input) {
* Parse JSON file with comments stripped. Unlike the default comment stripping feature
* of nlohmann::json, this allows you to have multiple comments in a row!
*/
nlohmann::json parse_commented_json(const std::string& input) {
return nlohmann::json::parse(strip_cpp_style_comments(input));
nlohmann::json parse_commented_json(const std::string& input, const std::string& source_name) {
try {
return nlohmann::json::parse(strip_cpp_style_comments(input));
} catch (const std::exception& e) {
if (source_name.empty()) {
lg::error("Error parsing JSON file: {}", e.what());
} else {
lg::error("Error parsing JSON in file {}: {}", source_name, e.what());
}
throw;
}
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion common/util/json_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#include "third-party/json.hpp"

std::string strip_cpp_style_comments(const std::string& input);
nlohmann::json parse_commented_json(const std::string& input);
nlohmann::json parse_commented_json(const std::string& input, const std::string& source_name);
Range<int> parse_json_optional_integer_range(const nlohmann::json& json);
12 changes: 12 additions & 0 deletions decompiler/IR2/AtomicOpForm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,18 @@ Form* LoadVarOp::get_load_src(FormPool& pool, const Env& env) const {
}
}
}

if (m_kind == Kind::FLOAT && m_size == 4) {
assert((label.offset % 4) == 0);
const auto& words = env.file->words_by_seg.at(label.target_segment);
if ((int)words.size() > label.offset / 4) {
auto word = words.at(label.offset / 4);
assert(word.kind == LinkedWord::PLAIN_DATA);
float value;
memcpy(&value, &word.data, 4);
return pool.alloc_single_element_form<ConstantFloatElement>(nullptr, value);
}
}
}

auto source = pool.alloc_single_element_form<SimpleExpressionElement>(nullptr, m_src, m_my_idx);
Expand Down
2 changes: 1 addition & 1 deletion decompiler/ObjectFile/ObjectFileDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ ObjectFileDB::ObjectFileDB(const std::vector<std::string>& _dgos,
}

void ObjectFileDB::load_map_file(const std::string& map_data) {
auto j = parse_commented_json(map_data);
auto j = parse_commented_json(map_data, "ObjectFileDB Map File");

for (auto& x : j) {
auto mapped_name = x[0].get<std::string>();
Expand Down
4 changes: 2 additions & 2 deletions decompiler/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace {
nlohmann::json read_json_file_from_config(const nlohmann::json& cfg, const std::string& file_key) {
auto file_name = cfg.at(file_key).get<std::string>();
auto file_txt = file_util::read_text_file(file_util::get_file_path({file_name}));
return parse_commented_json(file_txt);
return parse_commented_json(file_txt, file_name);
}
} // namespace

Expand All @@ -25,7 +25,7 @@ nlohmann::json read_json_file_from_config(const nlohmann::json& cfg, const std::
Config read_config_file(const std::string& path_to_config_file) {
Config config;
auto config_str = file_util::read_text_file(path_to_config_file);
auto cfg = parse_commented_json(config_str);
auto cfg = parse_commented_json(config_str, path_to_config_file);

config.game_version = cfg.at("game_version").get<int>();

Expand Down
176 changes: 6 additions & 170 deletions decompiler/config/jak1_ntsc_black_label/label_types.jsonc
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
{
"gcommon": [
["L345", "float", true],
["L346", "uint64", true]
],

"gkernel": [
["L345", "_auto_", true],
["L344", "_auto_", true],
["L346", "float", true],
["L347", "float", true],
["L348", "float", true],
["L289", "_auto_", true],
["L282", "_auto_", true],
["L122", "_lambda_", true],
Expand All @@ -23,9 +19,7 @@
"gstring": [["L170", "float", true]],

"math": [
["L41", "float", true],
["L34", "float", true],
["L35", "float", true]
["L41", "float", true]
],

"vector-h": [
Expand All @@ -43,8 +37,6 @@
["L2", "_auto_", true]
],

"matrix": [["L60", "float", true]],

"transform": [["L11", "float", true]],
"quaternion-h": [["L1", "quaternion", true]],
"quaternion": [
Expand All @@ -54,73 +46,17 @@
["L70", "vector", true],
["L71", "vector", true],
["L72", "vector", true],
["L77", "float", true],
["L78", "float", true],
["L79", "float", true],
["L80", "float", true],
["L81", "float", true],
["L82", "float", true],
["L93", "float", true],
["L94", "float", true]
],

"euler": [["L18", "float", true]],

"geometry": [
["L115", "float", true],
["L116", "float", true],
["L117", "float", true],
["L118", "float", true],
["L119", "float", true],
["L120", "float", true],
["L121", "float", true],
["L122", "float", true],
["L123", "float", true],
["L124", "float", true],
["L125", "float", true],
["L126", "float", true],
["L127", "float", true],
["L128", "float", true]
],

"trigonometry": [
["L143", "float", true],
["L144", "float", true],
["L145", "float", true],
["L137", "float", true],
["L106", "float", true],
["L134", "float", true],
["L112", "float", true],
["L114", "float", true],
["L135", "float", true],
["L121", "float", true],
["L150", "float", true],
["L147", "float", true],
["L149", "float", true],
["L107", "float", true],
["L129", "float", true],
["L152", "float", true],
["L109", "float", true],
["L138", "float", true],
["L127", "float", true],
["L128", "float", true],
["L110", "float", true],
["L136", "float", true]
["L81", "float", true]
],

"timer": [["L20", "float", true]],

"dma": [["L56", "uint64", true]],

"dma-bucket": [["L10", "uint64", true]],

"video-h": [["L1", "video-parms", true]],

"pad": [
["L44", "float", true],
["L42", "float", true],
["L43", "float", true],
["L41", "float", true]
["L44", "float", true]
],

"dma-disasm": [["L148", "(array vif-disasm-element)", true]],
Expand All @@ -130,8 +66,6 @@

"display-h": [["L6", "rgba", true]],

"loader-h": [["L10", "float", true]],

"level-h": [["L3", "level-group", true]],

"level-info": [
Expand Down Expand Up @@ -166,63 +100,15 @@

"level-h": [["L3", "_auto_", true]],

"math-camera": [
["L51", "float", true],
["L52", "float", true],
["L53", "float", true],
["L55", "float", true],
["L56", "float", true],
["L38", "float", true],
["L37", "float", true],
["L54", "float", true],
["L50", "float", true],
["L49", "float", true],
["L27", "float", true],
["L35", "float", true],
["L34", "float", true],
["L28", "float", true],
["L26", "float", true],
["L34", "float", true],
["L33", "float", true],
["L30", "float", true],
["L44", "float", true],
["L45", "float", true],
["L32", "float", true],
["L31", "float", true],
["L47", "float", true],
["L48", "float", true],
["L46", "float", true],
["L36", "float", true],
["L43", "float", true],
["L42", "float", true],
["L40", "float", true],
["L41", "float", true],
["L39", "float", true],
["L59", "float", true]
],

"font-h": [
["L18", "matrix", true],
["L19", "float", true],
["L17", "font-work", true]
],

"text-h": [["L2", "_auto_", true]],

"display": [
["L47", "float", true],
["L42", "float", true],
["L43", "float", true],
["L44", "float", true],
["L45", "float", true],
["L48", "float", true],
["L46", "float", true],
["L50", "float", true],
["L49", "float", true],
["L87", "float", true],
["L77", "float", true],
["L78", "float", true],
["L79", "float", true],
["L76", "(pointer uint32)", true, 2],
["L51", "uint64", true],
["L52", "uint64", true],
Expand Down Expand Up @@ -296,29 +182,9 @@
],

"ocean-frames": [["L1", "(pointer uint32)", true, 16384]],

"sky-h": [
["L73", "float", true],
["L71", "float", true],
["L72", "float", true]
],

"mood-h": [["L3", "float", true]],

"time-of-day-h": [["L2", "time-of-day-context", true]],

"merc-h": [
["L4", "float", true],
["L5", "float", true],
["L6", "float", true],
["L7", "float", true]
],

"shadow-cpu-h": [
["L9", "float", true],
["L10", "float", true]
],

"game-info-h": [
["L4", "game-bank", true],
["L3", "game-info", true]
Expand All @@ -327,13 +193,10 @@
"wind-h": [["L3", "_auto_", true]],

"dynamics-h": [
["L7", "float", true],
["L6", "_auto_", true]
],

"surface-h": [
["L72", "float", true],
["L71", "float", true],
["L68", "_auto_", true],
["L67", "_auto_", true],
["L66", "_auto_", true],
Expand Down Expand Up @@ -376,29 +239,15 @@
"pat-h": [["L1", "(inline-array pat-mode-info)", true, 4]],

"fact-h": [
["L35", "_auto_", true],
["L36", "float", true]
["L35", "_auto_", true]
],

"sync-info": [
["L78", "float", true],
["L79", "float", true],
["L80", "float", true],
["L81", "float", true],
["L82", "float", true],
["L83", "float", true],
["L84", "float", true],
["L85", "float", true],
["L86", "float", true]
],

"smush-control-h": [
["L20", "float", true],
["L18", "float", true],
["L19", "float", true],
["L17", "float", true]
],

"collide-shape-h": [
["L27", "float", true],
["L25", "_auto_", true],
Expand All @@ -410,8 +259,7 @@
"vector": [
["L114", "float", true],
["L113", "float", true],
["L112", "float", true],
["L111", "float", true]
["L112", "float", true]
],

"texture": [
Expand All @@ -425,19 +273,7 @@
["L349", "_auto_", true],
["L369", "uint64", true],
["L373", "uint64", true],
["L371", "uint64", true],
["L364", "float", true],
["L360", "float", true],
["L362", "float", true],
["L359", "float", true],
["L358", "float", true],
["L361", "float", true]
],

"lights": [
["L8", "float", true],
["L9", "float", true],
["L10", "float", true]
["L371", "uint64", true]
],

"hud-h": [
Expand Down
2 changes: 1 addition & 1 deletion test/decompiler/FormRegressionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ parse_var_json(const std::string& str) {
std::vector<std::string> args;
std::unordered_map<std::string, LocalVarOverride> var_overrides;

auto j = parse_commented_json(str);
auto j = parse_commented_json(str, "Regression Test variable map");

auto arg = j.find("args");
if (arg != j.end()) {
Expand Down
3 changes: 2 additions & 1 deletion test/offline/offline_test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ int main(int argc, char** argv) {
}
// use the all_objs.json file to place them in the correct build order
auto j = parse_commented_json(
file_util::read_text_file(file_util::get_file_path({"goal_src", "build", "all_objs.json"})));
file_util::read_text_file(file_util::get_file_path({"goal_src", "build", "all_objs.json"})),
"all_objs.json");
for (auto& x : j) {
auto mapped_name = x[0].get<std::string>();
for (auto& p : reference_files_rough_order) {
Expand Down

0 comments on commit 223adee

Please sign in to comment.