-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
163 lines (145 loc) · 4.94 KB
/
Copy pathmain.cpp
File metadata and controls
163 lines (145 loc) · 4.94 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Written by Paul Baxter
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <cstdint>
#include <cctype>
#include <optional>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <utility>
#include <format>
#include <fstream>
#include <sstream>
#include <exception>
#include <stack>
#include <chrono>
#include <fstream>
#define GEN_RULEMAP
#include "ruletype.h"
#undef GEN_RULEMAP
#define GEN_TOKMAP
#include "tokenkind.h"
#undef GEN_TOKMAP
#include "opcodedict.h"
#include "PasmTokenizer.hpp"
#include "anonymouslabel.h"
#include "multipassassembler.h"
#include "symboltable.h"
#include "sourceManager.h"
#include "getmangledsymbol.h"
#include "opcodeinfo.h"
#include "exprNode.h"
#include "macrodef.h"
#include "utilities.h"
#include "AssemblerParser.h"
#include "options.h"
int main(int argc, char* argv[])
{
Options options;
auto arg = 1;
while(arg < argc) {
std::string arg_str = std::string(argv[arg]);
if (arg_str[0] != '-') {
options.input_filenames.push_back(argv[arg]);
}
else if (arg_str == "-o") {
arg++;
if (arg >= argc) {
std::cout << "invalid output file\n";
return -1;
}
options.outfile = argv[arg];
}
else if (arg_str == "-c64") {
options.c64 = true;
}
else if (arg_str == "-i") {
arg++;
if (arg >= argc) {
std::cout << "invalid include directory\n";
return -1;
}
options.include.push_back(argv[arg]);
}
else if (arg_str == "-st") {
arg++;
if (arg >= argc) {
std::cout << "invalid symbol trace\n";
return -1;
}
options.traced_symbols.push_back(argv[arg]);
}
else if (arg_str == "-d") {
arg++;
if (arg >= argc) {
std::cout << "invalid symbol\n";
return -1;
}
std::string sym = argv[arg];
arg++;
std::string val = argv[arg];
int symval;
std::stringstream ss;
if (val[0] == '$') {
ss << std::hex << val.substr(1);
}
else {
ss << std::dec << val;
}
ss >> symval;
options.defined_symbols.push_back({sym, symval} );
}
else {
std::cout << "Unknown option '" << arg_str << "'\n";
return -1;
}
arg++;
}
if (options.input_filenames.empty()) {
std::cout << "No input file specified.\n";
return 1;
}
try {
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
SourceManager src_mgr;
PasmTokenizer tokenizer;
std::unordered_map<std::string, MacroDef> macros_;
std::vector<AnonymousLabel> anonymous_labels;
std::vector<PasmTokenizer::Token> tokens;
for (const auto& root_file : options.input_filenames) {
auto file_tokens = LoadAndTokenizeFile(root_file, src_mgr, tokenizer);
tokens.insert(tokens.end(), file_tokens.begin(), file_tokens.end());
}
AssemblerParser parser(tokens, options);
auto statements = parser.ParseProgram(src_mgr, macros_, tokenizer);
MultiPassAssembler assembler(options);
assembler.Assemble(statements, anonymous_labels, src_mgr);
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << assembler.listing_file << "\n";
if (options.outfile.length() > 0) {
std::ofstream out(options.outfile, std::ios::out | std::ios::binary);
auto sz = assembler.binary_output.size();
if (options.c64) {
auto lo = static_cast<unsigned char>(assembler.load_address & 0xFF);
auto hi = static_cast<unsigned char>((assembler.load_address >> 8) & 0xFF);
out.write(reinterpret_cast<const char*>(&lo), sizeof(unsigned char));
out.write(reinterpret_cast<const char*>(&hi), sizeof(unsigned char));
sz += 2;
}
out.write(reinterpret_cast<const char*>(assembler.binary_output.data()), assembler.binary_output.size());
out.close();
std::cout << "Wrote " << sz << " bytes to " << options.outfile << "\n";
}
std::chrono::duration<double> elapsed_seconds = end - begin;
std::cout << "Elapsed time: " << elapsed_seconds.count() << " seconds\n";
}
catch (std::exception& ex) {
std::cerr << "Error " << ex.what() << "\n";
}
return 0;
}