updates to the binning tool

This commit is contained in:
FeralChild64 committed 2019-09-09 17:46:52 +02:00
1 parent 8de99f2b14
commit 53e4babbe3
2 files changed
+177 -111

No files matched your search

+6 -6
View File
@@ -84,9 +84,9 @@ build/chargen.rom: $(TOOL_PNGPREPARE) assets/8x8font.png
# Dependencies - BASIC and KERNAL
build/target_generic/OUTB.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_BASIC_generic) $(foreach dir,$(SRCDIR_BASIC_generic),$(wildcard $(dir)/*.s)) build/,generated/gen_packed_messages.s
build/target_mega65/OUTB.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_BASIC_mega65) $(foreach dir,$(SRCDIR_BASIC_mega65),$(wildcard $(dir)/*.s)) build/,generated/gen_packed_messages.s
build/target_ultimate64/OUTB.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_BASIC_ultimate64) $(foreach dir,$(SRCDIR_BASIC_ultimate64),$(wildcard $(dir)/*.s)) build/,generated/gen_packed_messages.s
build/target_generic/OUTB.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_BASIC_generic) $(foreach dir,$(SRCDIR_BASIC_generic),$(wildcard $(dir)/*.s)) build/,generated/packed_messages.s
build/target_mega65/OUTB.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_BASIC_mega65) $(foreach dir,$(SRCDIR_BASIC_mega65),$(wildcard $(dir)/*.s)) build/,generated/packed_messages.s
build/target_ultimate64/OUTB.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_BASIC_ultimate64) $(foreach dir,$(SRCDIR_BASIC_ultimate64),$(wildcard $(dir)/*.s)) build/,generated/packed_messages.s
build/target_generic/OUTK.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_KERNAL_generic) $(foreach dir,$(SRCDIR_KERNAL_generic),$(wildcard $(dir)/*.s))
build/target_mega65/OUTK.BIN: $(TOOL_ASSEMBLER) $(TOOL_BUILD_SEGMENT) $(SRCDIR_KERNAL_mega65) $(foreach dir,$(SRCDIR_KERNAL_mega65),$(wildcard $(dir)/*.s))
@@ -106,15 +106,15 @@ build/basic_ultimate64.rom: build/target_ultimate64/newrom
# Rules - BASIC and KERNAL
build/,generated/gen_packed_messages.s: $(TOOL_COMPRESS_TEXT)
build/,generated/packed_messages.s: $(TOOL_COMPRESS_TEXT)
@mkdir -p build/,generated
$(TOOL_COMPRESS_TEXT) > build/,generated/gen_packed_messages.s
$(TOOL_COMPRESS_TEXT) > build/,generated/packed_messages.s
.PRECIOUS: build/target_%/OUTB.BIN
build/target_%/OUTB.BIN:
@mkdir -p build/target_$*
@rm -f $@* build/target_$*/BASIC*
@$(TOOL_BUILD_SEGMENT) -s BASIC -i BASIC-$* -o OUTB.BIN -t build/target_$* -l a000 -h e4d2 $(SRCDIR_BASIC_$*) build/,generated
@$(TOOL_BUILD_SEGMENT) -s BASIC -i BASIC-$* -o OUTB.BIN -t build/target_$* -l a000 -h e4d2 $(SRCDIR_BASIC_$*) build/,generated/packed_messages.s
.PRECIOUS: build/target_%/OUTK.BIN
build/target_%/OUTK.BIN:
+171 -105
View File
@@ -4,8 +4,11 @@
//
#include <dirent.h>
#include <libgen.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <algorithm>
#include <fstream>
@@ -24,6 +27,7 @@
#endif
const std::string ASM_CMD = "java -jar assembler/KickAss.jar ";
const std::string LAB_OUT_START = "__routine_START_";
const std::string LAB_OUT_END = "__routine_END_";
const std::string LAB_IN_START = ".label __routine_START_";
@@ -42,53 +46,53 @@ std::string CMD_segInfo = "(unnamed)";
int CMD_loAddress = 0xC000;
int CMD_hiAddress = 0xCFFF;
std::list<std::string> CMD_dirList;
std::list<std::string> CMD_inList;
//
// Common helper functions
//
void bailOut()
void ERROR()
{
exit(-1);
}
void bailOut(const std::string &message)
void ERROR(const std::string &message)
{
std::cout << std::endl << "FATAL: " << message << std::endl << std::endl;
std::cout << "\n" << "ERROR: " << message << "\n\n";
exit(-1);
}
void printUsage()
{
std::cout << std::endl <<
"usage: build_segment [-o <out file>] [-d <out dir>] [-t <temp dir>]" << std::endl <<
" [-l <start/low address>] [-h <end/high address>]" << std::endl <<
" [-s <segment name>] [-i <segment display info>] <input dir list>" <<
std::endl << std::endl;
std::cout << "\n" <<
"usage: build_segment [-o <out file>] [-d <out dir>] [-t <temp dir>]" << "\n" <<
" [-l <start/low address>] [-h <end/high address>]" << "\n" <<
" [-s <segment name>] [-i <segment display info>] <input dir/file list>" <<
"\n\n";
}
void printBannerLineTop()
{
std::cout << std::endl << std::endl << std::endl << BANNER_LINE << std::endl;
std::cout << "\n\n\n" << BANNER_LINE << "\n";
}
void printBannerLineBottom()
{
std::cout << BANNER_LINE << std::endl << std::endl;
std::cout << BANNER_LINE << "\n\n";
}
void printBannerCollectAnalyse()
{
printBannerLineTop();
std::cout << "// Segment '" << CMD_segInfo << "' - collecting and analysing routines" << std::endl;
std::cout << "// Segment '" << CMD_segInfo << "' - collecting and analysing routines" << "\n";
printBannerLineBottom();
}
void printBannerBinCompile()
{
printBannerLineTop();
std::cout << "// Segment '" << CMD_segInfo << "' - binning and compiling the assembly" << std::endl;
std::cout << "// Segment '" << CMD_segInfo << "' - binning and compiling the assembly" << "\n";
printBannerLineBottom();
}
@@ -96,6 +100,24 @@ void printBannerBinCompile()
// Class definitions
//
class DualStream
{
public:
DualStream(std::ostream& str1, std::ostream& str2) : str1(str1), str2(str2) {}
template<class T> DualStream &operator<<(const T& x)
{
str1 << x;
str2 << x;
return *this;
}
private:
std::ostream& str1;
std::ostream& str2;
};
class SourceFile
{
public:
@@ -125,9 +147,9 @@ public:
bool isSolved() const;
void addToProblem(SourceFile *routine);
void fillGap(int gapAddress, const std::list<SourceFile *> &routines);
void performObviousSteps();
void removeUselessGaps();
void fillGap(DualStream &logOutput, int gapAddress, const std::list<SourceFile *> &routines);
void performObviousSteps(DualStream &logOutput);
void removeUselessGaps(DualStream &logOutput);
void sortFloatingRoutinesBySize();
std::map<int, SourceFile *> fixedRoutines; // routines with location already fixed
@@ -138,7 +160,7 @@ public:
class Solver
{
public:
Solver(BinningProblem &problem) : problem(problem) {}
Solver(BinningProblem &problem) : problem(problem), logOutput(logFile, std::cout) {}
void run();
@@ -148,6 +170,9 @@ public:
private:
BinningProblem &problem;
std::ofstream logFile;
DualStream logOutput;
};
//
@@ -180,26 +205,52 @@ void parseCommandLine(int argc, char **argv)
case 'i': CMD_segInfo = optarg; break;
case 'l': CMD_loAddress = strtol(optarg, nullptr ,16); break;
case 'h': CMD_hiAddress = strtol(optarg, nullptr ,16); break;
default: printUsage(); bailOut();
default: printUsage(); ERROR();
}
}
// Retrieve directory list
// Retrieve file/directory list
for (int idx = optind; idx < argc; idx++)
{
CMD_dirList.push_back(argv[idx]);
CMD_inList.push_back(argv[idx]);
}
if (CMD_dirList.empty()) { printUsage(); bailOut("empty directory list"); }
if (CMD_inList.empty()) { printUsage(); ERROR("empty directory/file list"); }
}
void readSourceFiles()
{
for (const auto &dirName : CMD_dirList)
struct stat statBuf;
for (const auto &objName : CMD_inList)
{
DIR *dirHandle = opendir(dirName.c_str());
if (!dirHandle) bailOut(std::string("unable to open directory '") + dirName + "'");
if (stat(objName.c_str(), &statBuf) < 0)
{
ERROR(std::string("can't get information about '") + objName + "'");
}
if (S_ISREG(statBuf.st_mode))
{
// This is a regular file
char *tmp1 = strdup(objName.c_str());
char *tmp2 = strdup(objName.c_str());
std::string dirName = dirname(tmp1);
std::string fileName = basename(tmp2);
free(tmp1);
free(tmp2);
GLOBAL_sourceFiles.push_back(SourceFile(fileName, dirName));
GLOBAL_maxFileNameLen = std::max(GLOBAL_maxFileNameLen, fileName.length());
continue;
}
// This should be a directory
DIR *dirHandle = opendir(objName.c_str());
if (!dirHandle) ERROR(std::string("unable to open directory '") + objName + "'");
struct dirent *dirEntry;
while ((dirEntry = readdir(dirHandle)) != nullptr)
@@ -215,14 +266,14 @@ void readSourceFiles()
// Add file to the global list
GLOBAL_sourceFiles.push_back(SourceFile(fileName, dirName));
GLOBAL_sourceFiles.push_back(SourceFile(fileName, objName));
GLOBAL_maxFileNameLen = std::max(GLOBAL_maxFileNameLen, fileName.length());
}
closedir(dirHandle);
}
if (GLOBAL_sourceFiles.empty()) bailOut("no source files found");
if (GLOBAL_sourceFiles.empty()) ERROR("no source files found");
// Sort the file list by name, to provide deterministic results
@@ -237,7 +288,7 @@ void checkInputFileLabels()
{
if (usedLabels.count(sourceFile.label) != 0)
{
bailOut(std::string("input file '") + sourceFile.fileName + "' has a name too similar to another one");
ERROR(std::string("input file '") + sourceFile.fileName + "' has a name too similar to another one");
}
usedLabels.insert(sourceFile.label);
@@ -259,26 +310,28 @@ void calcRoutineSizes()
// Write test file to determine routine sizes
std::ofstream outFile(outFileName, std::fstream::out | std::fstream::trunc);
if (!outFile.good()) bailOut(std::string("can't open temporary file '") + outFileName + "'");
if (!outFile.good()) ERROR(std::string("can't open temporary file '") + outFileName + "'");
// Start at $100, so that no local data gets accesses using ZP addressing modes
// during this pass, which would otherwise upset things later
outFile << std::endl << ".segment " << CMD_segName << " [start=$100, min=$100, max=$FFFF]" << std::endl;
outFile << "\n" << ".segment " << CMD_segName << " [start=$100, min=$100, max=$FFFF]" << "\n";
outFile << "#define BUILD_" << CMD_segName << "\n";
for (const auto &sourceFile : GLOBAL_sourceFiles)
{
outFile << std::endl << std::endl << std::endl << std::endl;
outFile << "// Source file: " << sourceFile.fileName << std::endl << std::endl;
outFile << LAB_OUT_START << sourceFile.label << ":" << std::endl << std::endl;
outFile << "\n\n\n\n";
outFile << "/* Source file: " << sourceFile.fileName << " */" << "\n\n";
outFile << ".memblock \"" << sourceFile.fileName << "\"" << "\n";
outFile << LAB_OUT_START << sourceFile.label << ":" << "\n\n";
outFile << std::string(sourceFile.content.begin(), sourceFile.content.end());
outFile << std::endl << std::endl;
outFile << LAB_OUT_END << sourceFile.label << ":" << std::endl;
outFile << "\n\n";
outFile << LAB_OUT_END << sourceFile.label << ":" << "\n";
}
if (!outFile.good()) bailOut(std::string("error writing temporary file '") + outFileName + "'");
if (!outFile.good()) ERROR(std::string("error writing temporary file '") + outFileName + "'");
outFile.close();
// All written - now launch the assembler
@@ -287,14 +340,14 @@ void calcRoutineSizes()
std::cout << std::flush;
if (0 != system(cmd.c_str()))
{
bailOut("assembler running failed");
ERROR("assembler running failed");
}
// Read addresses
std::ifstream symFile;
symFile.open(symFileName);
if (!symFile.good()) bailOut(std::string("unable to open results file '") + symFileName + "'");
if (!symFile.good()) ERROR(std::string("unable to open results file '") + symFileName + "'");
std::string line;
while (std::getline(symFile, line))
@@ -343,7 +396,7 @@ void calcRoutineSizes()
if (sourceFile.testAddrStart <= 0 || sourceFile.testAddrEnd <= 0 ||
sourceFile.testAddrStart > sourceFile.testAddrEnd)
{
bailOut(std::string("unable to determine code length in '") + sourceFile.fileName + "'");
ERROR(std::string("unable to determine code length in '") + sourceFile.fileName + "'");
}
sourceFile.codeLength = sourceFile.testAddrEnd - sourceFile.testAddrStart;
@@ -352,10 +405,10 @@ void calcRoutineSizes()
// Check whether total code size is sane
if (0 == GLOBAL_totalRoutinesSize) bailOut("total code size is 0");
if (0 == GLOBAL_totalRoutinesSize) ERROR("total code size is 0");
if (signed(GLOBAL_totalRoutinesSize) > CMD_hiAddress - CMD_loAddress)
{
bailOut(std::string("total code size is ") + std::to_string(GLOBAL_totalRoutinesSize) + ", too much for this segment!");
ERROR(std::string("total code size is ") + std::to_string(GLOBAL_totalRoutinesSize) + ", too much for this segment!");
}
// Sort the file list by code size, starting from the smallest
@@ -379,7 +432,7 @@ void prepareBinningProblem()
const std::string logFileName = CMD_tmpDir + DIR_SEPARATOR + CMD_segName + "_binproblem.log";
unlink(logFileName.c_str());
std::ofstream logFile(logFileName, std::fstream::out | std::fstream::trunc);
std::string logLine;
DualStream logOutput(logFile, std::cout);
// Print out code length information
@@ -388,10 +441,8 @@ void prepareBinningProblem()
std::string spacing;
spacing.resize(GLOBAL_maxFileNameLen + 4 - sourceFile.fileName.length(), ' ');
std::string floating = sourceFile.floating ? "(floating) " : " ";
logLine = "file: " + floating + sourceFile.fileName + spacing + "size: " + std::to_string(sourceFile.codeLength);
logFile << logLine << std::endl;
std::cout << logLine << std::endl;
logOutput << "file: " << floating << sourceFile.fileName << spacing << "size: " << std::to_string(sourceFile.codeLength) << "\n";
}
// Create the binning problem
@@ -405,40 +456,34 @@ void prepareBinningProblem()
// Print out some more statistics
logLine = "free space (after floating routines are placed): " +
std::to_string(CMD_hiAddress - CMD_loAddress - GLOBAL_totalRoutinesSize) ;
logFile << std::endl << logLine << std::endl;
std::cout << std::endl << logLine << std::endl;
logLine = "number of floating routines: " +
std::to_string(GLOBAL_binningProblem.floatingRoutines.size());
logFile << logLine << std::endl;
std::cout << logLine << std::endl;
logLine = "number of gaps for the floating routines: " +
std::to_string(GLOBAL_binningProblem.gaps.size());
logFile << logLine << std::endl << std::endl;
std::cout << logLine << std::endl << std::endl;
logOutput << "\n" <<
"free space (after floating routines are placed): " <<
std::to_string(CMD_hiAddress - CMD_loAddress - GLOBAL_totalRoutinesSize) << "\n" <<
"number of floating routines: " <<
std::to_string(GLOBAL_binningProblem.floatingRoutines.size()) << "\n" <<
"number of gaps for the floating routines: " <<
std::to_string(GLOBAL_binningProblem.gaps.size()) << "\n" <<
"\n";
// Print out the available gaps
logLine = "gap address: $";
for (auto& gap : GLOBAL_binningProblem.gaps)
{
std::string gapSize = std::string(" size: ") + std::to_string(gap.second);
logFile << logLine << std::uppercase << std::hex << gap.first << gapSize << std::endl;
std::cout << logLine << std::uppercase << std::hex << gap.first << gapSize << std::endl;
logOutput << "gap address: $" << std::uppercase << std::hex << gap.first <<
" size: " << std::to_string(gap.second) << "\n";
}
std::cout << std::endl;
logOutput << "\n";
// Close the log file
if (!logFile.good()) bailOut(std::string("error writing log file '") + logFileName + "'");
if (!logFile.good()) ERROR(std::string("error writing log file '") + logFileName + "'");
logFile.close();
}
void solveBinningProblem()
{
std::cout << "trying to solve the routine binning problem" << std::endl << std::endl;
std::cout << "trying to solve the routine binning problem" << "\n\n";
// Do some routine actions on the binning problem object
@@ -447,10 +492,10 @@ void solveBinningProblem()
if (!GLOBAL_binningProblem.isSolved())
{
bailOut("unable to solve the routine binning problem");
ERROR("unable to solve the routine binning problem");
}
std::cout << std::endl;
std::cout << "\n";
}
void compileSegment()
@@ -460,39 +505,43 @@ void compileSegment()
const std::string outFileName = CMD_tmpDir + DIR_SEPARATOR + CMD_segName + "_combined.s";
unlink(outFileName.c_str());
std::ofstream outFile(outFileName, std::fstream::out | std::fstream::trunc);
if (!outFile.good()) bailOut(std::string("can't open temporary file '") + outFileName + "'");
if (!outFile.good()) ERROR(std::string("can't open temporary file '") + outFileName + "'");
// Write the header
outFile << std::endl << ".segment " << CMD_segName <<
outFile << "\n" <<
".segment " << CMD_segName <<
" [start=$" << std::hex << CMD_loAddress <<
", min=$" << std::hex << CMD_loAddress <<
", max=$" << std::hex << CMD_hiAddress <<
", outBin=\"" << CMD_outFile << "\", fill]" <<
std::endl;
"\n";
outFile << "#define BUILD_" << CMD_segName << "\n";
// Write files which only contain definitions (no routines)
for (const auto &sourceFile : GLOBAL_sourceFiles_noCode)
{
outFile << std::endl << std::endl << std::endl << std::endl;
outFile << "// Source file: " << sourceFile.fileName << std::endl << std::endl;
outFile << "\n\n\n\n";
outFile << "/* Source file: " << sourceFile.fileName << " */" << "\n\n";
outFile << ".memblock \"" << sourceFile.fileName << "\"" << "\n";
outFile << std::string(sourceFile.content.begin(), sourceFile.content.end());
outFile << std::endl;
outFile << "\n";
}
// Write remaining files, these should be placed under their proper locations
for (const auto &routine : GLOBAL_binningProblem.fixedRoutines)
{
outFile << std::endl << std::endl << std::endl << std::endl;
outFile << "// Source file: " << routine.second->fileName << std::endl << std::endl;
outFile << "\t* = $" << std::hex << routine.first << std::endl << std::endl;
outFile << "\n\n\n\n";
outFile << "/* Source file: " << routine.second->fileName << " */" << "\n\n";
outFile << ".memblock \"" << routine.second->fileName << "\"" << "\n";
outFile << "\t* = $" << std::hex << routine.first << "\n\n";
outFile << std::string(routine.second->content.begin(), routine.second->content.end());
outFile << std::endl;
outFile << "\n";
}
if (!outFile.good()) bailOut(std::string("error writing temporary file '") + outFileName + "'");
if (!outFile.good()) ERROR(std::string("error writing temporary file '") + outFileName + "'");
outFile.close();
// All written - now launch the assembler
@@ -501,7 +550,7 @@ void compileSegment()
std::cout << std::flush;
if (0 != system(cmd.c_str()))
{
bailOut("assembler running failed");
ERROR("assembler running failed");
}
}
@@ -542,24 +591,24 @@ SourceFile::SourceFile(const std::string &fileName, const std::string &dirName)
testAddrEnd(-1)
{
const std::string fileNameWithPath = dirName + DIR_SEPARATOR + fileName;
std::cout << "reading file: " << fileNameWithPath << std::endl;
std::cout << "reading file: " << fileNameWithPath << "\n";
// Open the file
std::ifstream inFile;
inFile.open(fileNameWithPath);
if (!inFile.good()) bailOut("unable to open file");
if (!inFile.good()) ERROR("unable to open file");
// Read the content
inFile.seekg(0, inFile.end);
auto fileLength = inFile.tellg();
if (fileLength == 0) bailOut("file is empty");
if (fileLength == 0) ERROR("file is empty");
content.resize(fileLength);
inFile.seekg(0);
inFile.read(&content[0], fileLength);
if (!inFile.good()) bailOut("error reading file content");
if (!inFile.good()) ERROR("error reading file content");
inFile.close();
@@ -600,7 +649,7 @@ BinningProblem::BinningProblem(int loAddress, int hiAddress)
{
if (hiAddress < loAddress || hiAddress < 0 || loAddress < 0 || hiAddress > 0xFFFF || loAddress > 0xFFFF)
{
bailOut("invalid lo/hi address");
ERROR("invalid lo/hi address");
}
// Create one large gap (initial) to represent the assembly
@@ -635,7 +684,7 @@ void BinningProblem::addToProblem(SourceFile *routine)
if (gap.first + gap.second < routine->startAddr + routine->codeLength)
{
bailOut(std::string("fixed address file '") + routine->fileName + "' won't fit in the available gap");
ERROR(std::string("fixed address file '") + routine->fileName + "' won't fit in the available gap");
}
gapFound = true;
@@ -669,12 +718,12 @@ void BinningProblem::addToProblem(SourceFile *routine)
if (!gapFound)
{
bailOut(std::string("start address of fixed address file '") + routine->fileName + "' already occupied");
ERROR(std::string("start address of fixed address file '") + routine->fileName + "' already occupied");
}
}
}
void BinningProblem::fillGap(int gapAddress, const std::list<SourceFile *> &routines)
void BinningProblem::fillGap(DualStream &logOutput, int gapAddress, const std::list<SourceFile *> &routines)
{
int offset = 0;
std::string spacing;
@@ -684,13 +733,13 @@ void BinningProblem::fillGap(int gapAddress, const std::list<SourceFile *> &rout
int targetAddr = gapAddress + offset;
spacing.resize(GLOBAL_maxFileNameLen + 4 - routine->fileName.length(), ' ');
std::cout << " $" << std::hex << targetAddr << std::dec << ": " <<
routine->fileName << spacing << "size: " << routine->codeLength << std::endl;
logOutput << " $" << std::hex << targetAddr << std::dec << ": " <<
routine->fileName << spacing << "size: " << routine->codeLength << "\n";
fixedRoutines[targetAddr] = routine;
offset += routine->codeLength;
if (offset > gaps[gapAddress]) bailOut(std::string("internal error line ") + std::to_string(__LINE__));
if (offset > gaps[gapAddress]) ERROR(std::string("internal error line ") + std::to_string(__LINE__));
floatingRoutines.erase(std::remove(floatingRoutines.begin(), floatingRoutines.end(), routine), floatingRoutines.end());
}
@@ -699,21 +748,21 @@ void BinningProblem::fillGap(int gapAddress, const std::list<SourceFile *> &rout
if (offset == gaps[gapAddress])
{
std::cout << "filled to the last byte" << std::endl;
logOutput << "filled to the last byte" << "\n";
}
else if (!isSolved())
{
std::cout << "filled in - dropped bytes: " << gaps[gapAddress] << std::endl;
logOutput << "filled in - dropped bytes: " << gaps[gapAddress] << "\n";
}
else
{
std::cout << "out of routines" << std::endl;
logOutput << "out of routines" << "\n";
}
gaps.erase(gapAddress);
}
void BinningProblem::performObviousSteps()
void BinningProblem::performObviousSteps(DualStream &logOutput)
{
// Get the size of the biggest routine; if there is just one gap which
// can handle it - put the roputine exactly there
@@ -746,13 +795,13 @@ void BinningProblem::performObviousSteps()
int targetAddr = gapAddress + gaps[gapAddress];
fixedRoutines[targetAddr] = floatingRoutines.back();
std::cout << "forced reducing gap $" << std::hex << gapAddress << std::dec << " to size " <<
gaps[gapAddress] << std::endl;
logOutput << "forced reducing gap $" << std::hex << gapAddress << std::dec << " to size " <<
gaps[gapAddress] << "\n";
spacing.resize(GLOBAL_maxFileNameLen + 4 - floatingRoutines.back()->fileName.length(), ' ');
std::cout << " $" << std::hex << targetAddr << std::dec << ": " <<
logOutput << " $" << std::hex << targetAddr << std::dec << ": " <<
floatingRoutines.back()->fileName << spacing << "size: " <<
floatingRoutines.back()->codeLength << std::endl;
floatingRoutines.back()->codeLength << "\n";
floatingRoutines.pop_back();
if (gaps[gapAddress] == 0) gaps.erase(gapAddress);
@@ -762,7 +811,7 @@ void BinningProblem::performObviousSteps()
}
}
void BinningProblem::removeUselessGaps()
void BinningProblem::removeUselessGaps(DualStream &logOutput)
{
// Get the size of the smallest floating routine,
// remove all the gaps which are smaller in size
@@ -777,7 +826,7 @@ void BinningProblem::removeUselessGaps()
{
if (gap.second < minUsefulSize)
{
std::cout << "dropping gap: $" << std::hex << gap.first << std::dec << " (size: " << gap.second << ")" << std::endl;
logOutput << "dropping gap: $" << std::hex << gap.first << std::dec << " (size: " << gap.second << ")" << "\n";
gaps.erase(gap.first);
repeat = true;
break;
@@ -800,28 +849,45 @@ void BinningProblem::sortFloatingRoutinesBySize()
void Solver::run()
{
problem.sortFloatingRoutinesBySize(); // just to be extra sure
// Prepare the log file
const std::string logFileName = CMD_tmpDir + DIR_SEPARATOR + CMD_segName + "_solver.log";
unlink(logFileName.c_str());
logFile.open(logFileName, std::fstream::out | std::fstream::trunc);
// Sort the floating routines, just to be extra sure
problem.sortFloatingRoutinesBySize();
// Run the solver until all is done
while (!problem.gaps.empty() && !problem.floatingRoutines.empty())
{
problem.performObviousSteps();
problem.removeUselessGaps();
problem.performObviousSteps(logOutput);
problem.removeUselessGaps(logOutput);
if (problem.gaps.empty() || problem.floatingRoutines.empty()) break;
int gapAddr = selectGapToFill();
std::cout << "selected gap: " << "$" << std::hex << gapAddr << std::dec << " (size: " << problem.gaps[gapAddr] << ")" << std::endl;
logOutput << "selected gap: $" << std::hex << gapAddr << std::dec << " (size: " << problem.gaps[gapAddr] << ")" << "\n";
std::list<SourceFile *> partialSolution;
findPartialSolution(problem.gaps[gapAddr], partialSolution);
problem.fillGap(gapAddr, partialSolution);
problem.fillGap(logOutput, gapAddr, partialSolution);
}
// Print out the result
if (problem.isSolved())
{
std::cout << std::endl << "all the routines sucessfully placed" << std::endl << std::endl;
logOutput << "\n" << "all the routines sucessfully placed" << "\n\n";
}
// Close the log file
if (!logFile.good()) ERROR(std::string("error writing log file '") + logFileName + "'");
logFile.close();
}
int Solver::selectGapToFill()
@@ -964,7 +1030,7 @@ void Solver::findPartialSolution(int gapSize, std::list<SourceFile *> &partialSo
std::list<bool> solution;
KS(routines, cacheV, cacheS, routines.size(), gapSize, solution);
if (routines.size() != solution.size()) bailOut(std::string("internal error line ") + std::to_string(__LINE__));
if (routines.size() != solution.size()) ERROR(std::string("internal error line ") + std::to_string(__LINE__));
// Return the solution upstream