-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxcmp.cpp
More file actions
96 lines (91 loc) · 3.75 KB
/
xcmp.cpp
File metadata and controls
96 lines (91 loc) · 3.75 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <boost/format.hpp>
#include "hex.hpp"
#include "hexasm.hpp"
#include "xcmp.hpp"
//===---------------------------------------------------------------------===//
// Driver
//===---------------------------------------------------------------------===//
static void help(const char *argv[]) {
std::cout << "X compiler\n\n";
std::cout << "Usage: " << argv[0] << " file\n\n";
std::cout << "Positional arguments:\n";
std::cout << " file A source file to compile\n\n";
std::cout << "Optional arguments:\n";
std::cout << " -h,--help Display this message\n";
std::cout << " --tokens Tokenise the input only\n";
std::cout << " --tree Display the syntax tree only\n";
std::cout << " --tree-opt Display the optimised syntax tree only\n";
std::cout << " --insts Display the intermediate instructions only\n";
std::cout << " --insts-lowered Display the lowered instructions only\n";
std::cout << " --insts-optimised Display the lowered optimised instructions only\n";
std::cout << " --memory-info Report memory information\n";
std::cout << " -S Emit the assembly program\n";
std::cout << " --insts-asm Display the assembled instructions only\n";
std::cout << " -o,--output file Specify a file for output (default a.out)\n";
}
int main(int argc, const char *argv[]) {
xcmp::Driver driver(std::cout);
try {
// Handle arguments.
auto driverAction = xcmp::DriverAction::EMIT_BINARY;
const char *inputFilename = nullptr;
const char *outputFilename = "a.out";
bool reportMemoryInfo = false;
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "-h") == 0 ||
std::strcmp(argv[i], "--help") == 0) {
help(argv);
std::exit(1);
} else if (std::strcmp(argv[i], "--tokens") == 0) {
driverAction = xcmp::DriverAction::EMIT_TOKENS;
} else if (std::strcmp(argv[i], "--tree") == 0) {
driverAction = xcmp::DriverAction::EMIT_TREE;
} else if (std::strcmp(argv[i], "--tree-opt") == 0) {
driverAction = xcmp::DriverAction::EMIT_OPTIMISED_TREE;
} else if (std::strcmp(argv[i], "--insts") == 0) {
driverAction = xcmp::DriverAction::EMIT_INTERMEDIATE_INSTS;
} else if (std::strcmp(argv[i], "--insts-lowered") == 0) {
driverAction = xcmp::DriverAction::EMIT_LOWERED_INSTS;
} else if (std::strcmp(argv[i], "--insts-optimised") == 0) {
driverAction = xcmp::DriverAction::EMIT_OPTIMISED_INSTS;
} else if (std::strcmp(argv[i], "-S") == 0) {
driverAction = xcmp::DriverAction::EMIT_ASM;
} else if (std::strcmp(argv[i], "--insts-asm") == 0) {
driverAction = xcmp::DriverAction::EMIT_TREE;
} else if (std::strcmp(argv[i], "--memory-info") == 0) {
reportMemoryInfo = true;
} else if (std::strcmp(argv[i], "--output") == 0 ||
std::strcmp(argv[i], "-o") == 0) {
outputFilename = argv[++i];
} else if (argv[i][0] == '-') {
throw std::runtime_error(std::string("unrecognised argument: ")+argv[i]);
} else {
if (!inputFilename) {
inputFilename = argv[i];
} else {
throw std::runtime_error("cannot specify more than one file");
}
}
}
// A file must be specified.
if (!inputFilename) {
help(argv);
std::exit(1);
}
// Run.
return driver.runCatchExceptions(driverAction, inputFilename, outputFilename, "a.out", reportMemoryInfo);
} catch (const std::exception &e) {
std::cerr << boost::format("Error: %s\n") % e.what();
return 1;
}
}