Move duplicated utilities to the common util folder and remove NEXT_DIR (#29)

* move things to the common library and remove next_dir

* fix for windows

* one last windows fix

* last fix for real this time

* debug listener test

* fix listener threading bug
This commit is contained in:
water111
2020-09-10 20:03:31 -04:00
committed by GitHub
parent 8cbcb36687
commit de5aa7e5e4
45 changed files with 138 additions and 351 deletions
+2 -2
View File
@@ -12,7 +12,6 @@ add_executable(decompiler
util/FileIO.cpp
config.cpp
util/LispPrint.cpp
util/Timer.cpp
Function/BasicBlocks.cpp
Disasm/InstructionMatching.cpp
TypeSystem/GoalType.cpp
@@ -22,4 +21,5 @@ add_executable(decompiler
TypeSystem/TypeSpec.cpp Function/CfgVtx.cpp Function/CfgVtx.h)
target_link_libraries(decompiler
minilzo)
minilzo
common_util)
+1 -17
View File
@@ -2,23 +2,7 @@
#define JAK_DISASSEMBLER_INSTRUCTIONMATCHING_H
#include "Instruction.h"
template <typename T>
struct MatchParam {
MatchParam() { is_wildcard = true; }
// intentionally not explicit so you don't have to put MatchParam<whatever>(blah) everywhere
MatchParam(T x) {
value = x;
is_wildcard = false;
}
T value;
bool is_wildcard = true;
bool operator==(const T& other) { return is_wildcard || (value == other); }
bool operator!=(const T& other) { return !(*this == other); }
};
#include "common/util/MatchParam.h"
bool is_no_link_gpr_store(const Instruction& instr,
MatchParam<int> size,
+7 -6
View File
@@ -13,9 +13,10 @@
#include "LinkedObjectFileCreation.h"
#include "decompiler/config.h"
#include "third-party/minilzo/minilzo.h"
#include "decompiler/util/BinaryReader.h"
#include "common/util/BinaryReader.h"
#include "decompiler/util/FileIO.h"
#include "decompiler/util/Timer.h"
#include "common/util/Timer.h"
#include "common/util/FileUtil.h"
#include "decompiler/Function/BasicBlocks.h"
/*!
@@ -142,7 +143,7 @@ constexpr int MAX_CHUNK_SIZE = 0x8000;
* Load the objects stored in the given DGO into the ObjectFileDB
*/
void ObjectFileDB::get_objs_from_dgo(const std::string& filename) {
auto dgo_data = read_binary_file(filename);
auto dgo_data = file_util::read_binary_file(filename);
stats.total_dgo_bytes += dgo_data.size();
const char jak2_header[] = "oZlB";
@@ -415,7 +416,7 @@ void ObjectFileDB::write_object_file_words(const std::string& output_dir, bool d
auto file_text = obj.linked_data.print_words();
auto file_name = combine_path(output_dir, obj.record.to_unique_name() + ".txt");
total_bytes += file_text.size();
write_text_file(file_name, file_text);
file_util::write_text_file(file_name, file_text);
total_files++;
}
});
@@ -442,7 +443,7 @@ void ObjectFileDB::write_disassembly(const std::string& output_dir,
auto file_text = obj.linked_data.print_disassembly();
auto file_name = combine_path(output_dir, obj.record.to_unique_name() + ".func");
total_bytes += file_text.size();
write_text_file(file_name, file_text);
file_util::write_text_file(file_name, file_text);
total_files++;
}
});
@@ -517,7 +518,7 @@ void ObjectFileDB::find_and_write_scripts(const std::string& output_dir) {
});
auto file_name = combine_path(output_dir, "all_scripts.lisp");
write_text_file(file_name, all_scripts);
file_util::write_text_file(file_name, all_scripts);
printf("Found scripts:\n");
printf(" total %.3f ms\n", timer.getMs());
+2 -1
View File
@@ -1,6 +1,7 @@
#include "config.h"
#include "third-party/json.hpp"
#include "util/FileIO.h"
#include "common/util/FileUtil.h"
Config gConfig;
@@ -9,7 +10,7 @@ Config& get_config() {
}
void set_config(const std::string& path_to_config_file) {
auto config_str = read_text_file(path_to_config_file);
auto config_str = file_util::read_text_file(path_to_config_file);
// to ignore comments in json, which may be useful
auto cfg = nlohmann::json::parse(config_str, nullptr, true, true);
+3 -2
View File
@@ -5,6 +5,7 @@
#include "config.h"
#include "util/FileIO.h"
#include "TypeSystem/TypeInfo.h"
#include "common/util/FileUtil.h"
int main(int argc, char** argv) {
printf("Jak Disassembler\n");
@@ -26,8 +27,8 @@ int main(int argc, char** argv) {
}
ObjectFileDB db(dgos);
write_text_file(combine_path(out_folder, "dgo.txt"), db.generate_dgo_listing());
write_text_file(combine_path(out_folder, "obj.txt"), db.generate_obj_listing());
file_util::write_text_file(combine_path(out_folder, "dgo.txt"), db.generate_dgo_listing());
file_util::write_text_file(combine_path(out_folder, "obj.txt"), db.generate_obj_listing());
db.process_link_data();
db.find_code();
-40
View File
@@ -1,40 +0,0 @@
#ifndef JAK_V2_BINARYREADER_H
#define JAK_V2_BINARYREADER_H
#include <cstdint>
#include <cassert>
#include <vector>
class BinaryReader {
public:
BinaryReader(uint8_t* _buffer, uint32_t _size) : buffer(_buffer), size(_size) {}
explicit BinaryReader(std::vector<uint8_t>& _buffer)
: buffer((uint8_t*)_buffer.data()), size(_buffer.size()) {}
template <typename T>
T read() {
assert(seek + sizeof(T) <= size);
T& obj = *(T*)(buffer + seek);
seek += sizeof(T);
return obj;
}
void ffwd(int amount) {
seek += amount;
assert(seek <= size);
}
uint32_t bytes_left() const { return size - seek; }
uint8_t* here() { return buffer + seek; }
uint32_t get_seek() { return seek; }
private:
uint8_t* buffer;
uint32_t size;
uint32_t seek = 0;
};
#endif // JAK_V2_BINARYREADER_H
-35
View File
@@ -3,35 +3,10 @@
#include <sstream>
#include <cassert>
std::string read_text_file(const std::string& path) {
std::ifstream file(path);
std::stringstream ss;
ss << file.rdbuf();
return ss.str();
}
std::string combine_path(const std::string& parent, const std::string& child) {
return parent + "/" + child;
}
std::vector<uint8_t> read_binary_file(const std::string& filename) {
auto fp = fopen(filename.c_str(), "rb");
if (!fp)
throw std::runtime_error("File " + filename + " cannot be opened");
fseek(fp, 0, SEEK_END);
auto len = ftell(fp);
rewind(fp);
std::vector<uint8_t> data;
data.resize(len);
if (fread(data.data(), len, 1, fp) != 1) {
throw std::runtime_error("File " + filename + " cannot be read");
}
return data;
}
std::string base_name(const std::string& filename) {
size_t pos = 0;
assert(!filename.empty());
@@ -70,13 +45,3 @@ uint32_t crc32(const uint8_t* data, size_t size) {
uint32_t crc32(const std::vector<uint8_t>& data) {
return crc32(data.data(), data.size());
}
void write_text_file(const std::string& file_name, const std::string& text) {
FILE* fp = fopen(file_name.c_str(), "w");
if (!fp) {
printf("Failed to fopen %s\n", file_name.c_str());
throw std::runtime_error("Failed to open file");
}
fprintf(fp, "%s\n", text.c_str());
fclose(fp);
}
-4
View File
@@ -4,12 +4,8 @@
#include <string>
#include <vector>
std::string read_text_file(const std::string& path);
std::string combine_path(const std::string& parent, const std::string& child);
std::vector<uint8_t> read_binary_file(const std::string& filename);
std::string base_name(const std::string& filename);
void write_text_file(const std::string& file_name, const std::string& text);
void init_crc();
uint32_t crc32(const uint8_t* data, size_t size);
uint32_t crc32(const std::vector<uint8_t>& data);
-54
View File
@@ -1,54 +0,0 @@
#include "Timer.h"
#ifdef _WIN32
#include <Windows.h>
#define MS_PER_SEC 1000ULL // MS = milliseconds
#define US_PER_MS 1000ULL // US = microseconds
#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns)
#define NS_PER_US 1000ULL
#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US)
#define NS_PER_HNS (100ULL) // NS = nanoseconds
#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US)
int Timer::clock_gettime_monotonic(struct timespec* tv) {
static LARGE_INTEGER ticksPerSec;
LARGE_INTEGER ticks;
double seconds;
if (!ticksPerSec.QuadPart) {
QueryPerformanceFrequency(&ticksPerSec);
if (!ticksPerSec.QuadPart) {
errno = ENOTSUP;
return -1;
}
}
QueryPerformanceCounter(&ticks);
seconds = (double)ticks.QuadPart / (double)ticksPerSec.QuadPart;
tv->tv_sec = (time_t)seconds;
tv->tv_nsec = (long)((ULONGLONG)(seconds * NS_PER_SEC) % NS_PER_SEC);
return 0;
}
#endif
void Timer::start() {
#ifdef __linux__
clock_gettime(CLOCK_MONOTONIC, &_startTime);
#elif _WIN32
clock_gettime_monotonic(&_startTime);
#endif
}
int64_t Timer::getNs() {
struct timespec now = {};
#ifdef __linux__
clock_gettime(CLOCK_MONOTONIC, &now);
#elif _WIN32
clock_gettime_monotonic(&now);
#endif
return (int64_t)(now.tv_nsec - _startTime.tv_nsec) +
1000000000 * (now.tv_sec - _startTime.tv_sec);
}
-47
View File
@@ -1,47 +0,0 @@
#ifndef JAK_V2_TIMER_H
#define JAK_V2_TIMER_H
#include <cassert>
#include <cstdint>
#include <ctime>
/*!
* Timer for measuring time elapsed with clock_monotonic
*/
class Timer {
public:
/*!
* Construct and start timer
*/
explicit Timer() { start(); }
#ifdef _WIN32
int clock_gettime_monotonic(struct timespec* tv);
#endif
/*!
* Start the timer
*/
void start();
/*!
* Get milliseconds elapsed
*/
double getMs() { return (double)getNs() / 1.e6; }
double getUs() { return (double)getNs() / 1.e3; }
/*!
* Get nanoseconds elapsed
*/
int64_t getNs();
/*!
* Get seconds elapsed
*/
double getSeconds() { return (double)getNs() / 1.e9; }
struct timespec _startTime = {};
};
#endif // JAK_V2_TIMER_H