Support compiling to and running from binary files
This commit is contained in:
@@ -12,7 +12,7 @@ int main(int argc, char ** argv)
|
|||||||
std::string outputFile("program.bin");
|
std::string outputFile("program.bin");
|
||||||
|
|
||||||
auto cli = (
|
auto cli = (
|
||||||
clipp::value("input file").set(inputFile),
|
clipp::value("input file (*.wasm or *.bin)").set(inputFile),
|
||||||
(
|
(
|
||||||
clipp::required("-e", "--execute").set(execute),
|
clipp::required("-e", "--execute").set(execute),
|
||||||
clipp::option("-m", "--memory-size") & clipp::value("memory size in bytes (defaults to 4096)", memorySize),
|
clipp::option("-m", "--memory-size") & clipp::value("memory size in bytes (defaults to 4096)", memorySize),
|
||||||
|
|||||||
@@ -184,10 +184,40 @@ void Wassembler::EnableByteTranslationLogging()
|
|||||||
|
|
||||||
bool Wassembler::CompileAndRun(std::string const & filePath)
|
bool Wassembler::CompileAndRun(std::string const & filePath)
|
||||||
{
|
{
|
||||||
std::vector<std::string> lines;
|
|
||||||
std::vector<std::uint8_t> bytes;
|
std::vector<std::uint8_t> bytes;
|
||||||
if (!CompileFile(filePath, bytes))
|
if (filePath.size() > 4 && filePath.compare(filePath.size() - 4, 4, ".bin") == 0)
|
||||||
{
|
{
|
||||||
|
std::ifstream inputFile(filePath);
|
||||||
|
if (!inputFile.is_open())
|
||||||
|
{
|
||||||
|
std::printf("Error: Cannot open file %s for reading", filePath.c_str());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t previousSize = 0;
|
||||||
|
bytes.resize(100);
|
||||||
|
while(inputFile.read(reinterpret_cast<char*>(&bytes[previousSize]), 100))
|
||||||
|
{
|
||||||
|
previousSize = bytes.size();
|
||||||
|
bytes.resize(bytes.size() + 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes.resize(bytes.size() - (100 - inputFile.gcount()));
|
||||||
|
}
|
||||||
|
else if (filePath.size() > 5 &&
|
||||||
|
filePath.compare(filePath.size() - 5, 5, ".wasm") == 0)
|
||||||
|
{
|
||||||
|
if (!CompileFile(filePath, bytes))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::printf(
|
||||||
|
"Error: unrecognized file extension on input file %s",
|
||||||
|
filePath.c_str());
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,9 +251,11 @@ bool Wassembler::CompileToFile(
|
|||||||
std::printf("Error: Cannot open file %s for writing", outputFilePath.c_str());
|
std::printf("Error: Cannot open file %s for writing", outputFilePath.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for(std::size_t i = 0; i < bytes.size(); ++i)
|
|
||||||
|
if (!output.write(reinterpret_cast<char*>(bytes.data()), bytes.size()))
|
||||||
{
|
{
|
||||||
output << bytes[i];
|
std::printf("Error: An error occurred whilst writing to %s", outputFilePath.c_str());
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in New Issue
Block a user