-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbintran_main.cpp
48 lines (40 loc) · 1.37 KB
/
bintran_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "bintran.hpp"
#include "exceptions.hpp"
#include <experimental/filesystem>
#include <string>
namespace fs = std::experimental::filesystem;
inline void DisplayUsage() {
std::printf("Usage: bintran PROGRAM\n");
}
static const char* FILE_EXTENSION = ".x86";
int main(int argc, char* argv[]) {
using namespace zvm;
if (argc != 2) {
DisplayUsage();
return ERR_WRONG_CMD_LINE_ARGS;
}
std::string filename = argv[1];
std::string x86_filename = argv[1] + std::string(FILE_EXTENSION);
try {
BinTran bt;
if (fs::exists(filename) && fs::exists(x86_filename) &&
fs::last_write_time(filename) <= fs::last_write_time(x86_filename)) {
bt.LoadX86CodeFromFile(x86_filename);
} else {
bt.LoadBinary(filename);
bt.Translate();
}
bt.Execute();
bt.SaveX86CodeToFile(x86_filename);
} catch (const IoException& ioerr) {
std::fprintf(stderr, "IO error: %s\n", ioerr.what());
return ioerr.GetErrorCode();
} catch (const AllocException& allocerr) {
std::fprintf(stderr, "Allocation error: %s\n", allocerr.what());
return ERR_FAILED_MEM_ALLOC;
} catch (const UndefinedOpcodeException& opcerr) {
std::fprintf(stderr, "Runtime error: %s\n", opcerr.what());
return ERR_OUT_OF_BOUNDS;
}
return ERR_OK;
}