Merge pull request #7158 from lioncash/dsptool

DSPTool: Minor cleanups
This commit is contained in:
Markus Wick 2018-06-23 10:50:45 +02:00 committed by GitHub
commit b3fa5a4f2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,11 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "Common/Common.h" #include <string>
#include <utility>
#include <vector>
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
#include "Core/DSP/DSPCodeUtil.h" #include "Core/DSP/DSPCodeUtil.h"
@ -39,14 +43,15 @@ void DSP::Host::UpdateDebugger()
{ {
} }
static void CodeToHeader(const std::vector<u16>& code, std::string filename, const char* name, static std::string CodeToHeader(const std::vector<u16>& code, const std::string& filename)
std::string& header)
{ {
std::vector<u16> code_padded = code; std::vector<u16> code_padded = code;
// Pad with nops to 32byte boundary // Pad with nops to 32byte boundary
while (code_padded.size() & 0x7f) while (code_padded.size() & 0x7f)
code_padded.push_back(0); code_padded.push_back(0);
header.clear();
std::string header;
header.reserve(code_padded.size() * 4); header.reserve(code_padded.size() * 4);
header.append("#define NUM_UCODES 1\n\n"); header.append("#define NUM_UCODES 1\n\n");
std::string filename_without_extension; std::string filename_without_extension;
@ -65,51 +70,54 @@ static void CodeToHeader(const std::vector<u16>& code, std::string filename, con
header.append("\n\t},\n"); header.append("\n\t},\n");
header.append("};\n"); header.append("};\n");
return header;
} }
static void CodesToHeader(const std::vector<u16>* codes, const std::vector<std::string>* filenames, static std::string CodesToHeader(const std::vector<std::vector<u16>>& codes,
u32 num_codes, const char* name, std::string& header) const std::vector<std::string>& filenames)
{ {
std::vector<std::vector<u16>> codes_padded; std::vector<std::vector<u16>> codes_padded;
u32 reserveSize = 0; std::size_t reserve_size = 0;
for (u32 i = 0; i < num_codes; i++) for (std::size_t i = 0; i < codes.size(); i++)
{ {
codes_padded.push_back(codes[i]); codes_padded.push_back(codes[i]);
// Pad with nops to 32byte boundary // Pad with nops to 32byte boundary
while (codes_padded.at(i).size() & 0x7f) while (codes_padded[i].size() & 0x7f)
codes_padded.at(i).push_back(0); codes_padded[i].push_back(0);
reserveSize += (u32)codes_padded.at(i).size(); reserve_size += codes_padded[i].size();
} }
header.clear();
header.reserve(reserveSize * 4); std::string header;
header.append(StringFromFormat("#define NUM_UCODES %u\n\n", num_codes)); header.reserve(reserve_size * 4);
header.append(StringFromFormat("#define NUM_UCODES %zu\n\n", codes.size()));
header.append("const char* UCODE_NAMES[NUM_UCODES] = {\n"); header.append("const char* UCODE_NAMES[NUM_UCODES] = {\n");
for (u32 i = 0; i < num_codes; i++) for (const std::string& in_filename : filenames)
{ {
std::string filename; std::string filename;
if (!SplitPath(filenames->at(i), nullptr, &filename, nullptr)) if (!SplitPath(in_filename, nullptr, &filename, nullptr))
filename = filenames->at(i); filename = in_filename;
header.append(StringFromFormat("\t\"%s\",\n", filename.c_str())); header.append(StringFromFormat("\t\"%s\",\n", filename.c_str()));
} }
header.append("};\n\n"); header.append("};\n\n");
header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n"); header.append("const unsigned short dsp_code[NUM_UCODES][0x1000] = {\n");
for (u32 i = 0; i < num_codes; i++) for (std::size_t i = 0; i < codes.size(); i++)
{ {
if (codes[i].size() == 0) if (codes[i].empty())
continue; continue;
header.append("\t{\n\t\t"); header.append("\t{\n\t\t");
for (u32 j = 0; j < codes_padded.at(i).size(); j++) for (std::size_t j = 0; j < codes_padded[i].size(); j++)
{ {
if (j && ((j & 15) == 0)) if (j && ((j & 15) == 0))
header.append("\n\t\t"); header.append("\n\t\t");
header.append(StringFromFormat("0x%04x, ", codes_padded.at(i).at(j))); header.append(StringFromFormat("0x%04x, ", codes_padded[i][j]));
} }
header.append("\n\t},\n"); header.append("\n\t},\n");
} }
header.append("};\n"); header.append("};\n");
return header;
} }
static void PerformBinaryComparison(const std::string& lhs, const std::string& rhs) static void PerformBinaryComparison(const std::string& lhs, const std::string& rhs)
@ -235,6 +243,23 @@ static bool PerformDisassembly(const std::string& input_name, const std::string&
return true; return true;
} }
static std::vector<std::string> GetAssemblerFiles(const std::string& source)
{
std::vector<std::string> files;
std::size_t last_pos = 0;
std::size_t pos = 0;
while ((pos = source.find('\n', last_pos)) != std::string::npos)
{
std::string temp = source.substr(last_pos, pos - last_pos);
if (!temp.empty())
files.push_back(std::move(temp));
last_pos = pos + 1;
}
return files;
}
static bool PerformAssembly(const std::string& input_name, const std::string& output_name, static bool PerformAssembly(const std::string& input_name, const std::string& output_name,
const std::string& output_header_name, bool multiple, bool force, const std::string& output_header_name, bool multiple, bool force,
bool output_size) bool output_size)
@ -246,42 +271,30 @@ static bool PerformAssembly(const std::string& input_name, const std::string& ou
} }
std::string source; std::string source;
if (File::ReadFileToString(input_name.c_str(), source)) if (File::ReadFileToString(input_name, source))
{ {
if (multiple) if (multiple)
{ {
source.append("\n");
// When specifying a list of files we must compile a header // When specifying a list of files we must compile a header
// (we can't assemble multiple files to one binary) // (we can't assemble multiple files to one binary)
// since we checked it before, we assume output_header_name isn't empty // since we checked it before, we assume output_header_name isn't empty
int lines; std::string currentSource;
std::vector<u16>* codes; const std::vector<std::string> files = GetAssemblerFiles(source);
std::vector<std::string> files;
std::string header, currentSource;
size_t lastPos = 0, pos = 0;
source.append("\n");
while ((pos = source.find('\n', lastPos)) != std::string::npos)
{
std::string temp = source.substr(lastPos, pos - lastPos);
if (!temp.empty())
files.push_back(temp);
lastPos = pos + 1;
}
lines = (int)files.size();
std::size_t lines = files.size();
if (lines == 0) if (lines == 0)
{ {
printf("ERROR: Must specify at least one file\n"); printf("ERROR: Must specify at least one file\n");
return false; return false;
} }
codes = new std::vector<u16>[lines]; std::vector<std::vector<u16>> codes(lines);
for (int i = 0; i < lines; i++) for (std::size_t i = 0; i < lines; i++)
{ {
if (!File::ReadFileToString(files[i].c_str(), currentSource)) if (!File::ReadFileToString(files[i], currentSource))
{ {
printf("ERROR reading %s, skipping...\n", files[i].c_str()); printf("ERROR reading %s, skipping...\n", files[i].c_str());
lines--; lines--;
@ -295,15 +308,13 @@ static bool PerformAssembly(const std::string& input_name, const std::string& ou
} }
if (output_size) if (output_size)
{ {
printf("%s: %d\n", files[i].c_str(), (int)codes[i].size()); printf("%s: %zu\n", files[i].c_str(), codes[i].size());
} }
} }
} }
CodesToHeader(codes, &files, lines, output_header_name.c_str(), header); const std::string header = CodesToHeader(codes, files);
File::WriteStringToFile(header, output_header_name + ".h"); File::WriteStringToFile(header, output_header_name + ".h");
delete[] codes;
} }
else else
{ {
@ -317,7 +328,7 @@ static bool PerformAssembly(const std::string& input_name, const std::string& ou
if (output_size) if (output_size)
{ {
printf("%s: %d\n", input_name.c_str(), (int)code.size()); printf("%s: %zu\n", input_name.c_str(), code.size());
} }
if (!output_name.empty()) if (!output_name.empty())
@ -327,8 +338,7 @@ static bool PerformAssembly(const std::string& input_name, const std::string& ou
} }
if (!output_header_name.empty()) if (!output_header_name.empty())
{ {
std::string header; const std::string header = CodeToHeader(code, input_name);
CodeToHeader(code, input_name, output_header_name.c_str(), header);
File::WriteStringToFile(header, output_header_name + ".h"); File::WriteStringToFile(header, output_header_name + ".h");
} }
} }