-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
289 lines (236 loc) · 6.99 KB
/
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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* main.cpp
*
* Created on: Apr 12, 2014
* Author: Pimenta
*/
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <fstream>
#include <set>
#include <map>
#include <string>
using namespace std;
typedef uint32_t uword_t;
#ifndef MEM_WORDS
#define MEM_WORDS 0x2000
#endif
#ifndef WORD_WIDTH
#define WORD_WIDTH 32
#endif
struct ObjectFile {
uword_t offset;
map<string, uword_t> exported;
map<string, set<uword_t>> imported;
set<uword_t> relative;
uword_t mem_size;
uword_t* mem;
~ObjectFile() {
delete[] mem;
}
void setStarter() {
offset = 0;
imported["start"].emplace(2);
relative.emplace(0);
relative.emplace(1);
relative.emplace(2);
mem_size = 4;
mem = new uword_t[4];
mem[0] = 3;
mem[1] = 3;
mem[2] = 0;
mem[3] = 0;
}
void read(const char* fn, uword_t offs) {
offset = offs;
fstream f(fn, fstream::in | fstream::binary);
uword_t tmp;
// read number of exported symbols
f.read((char*)&tmp, sizeof(uword_t));
// read exported symbols
for (uword_t i = 0; i < tmp; ++i) {
string sym;
uword_t addr;
// string
{
char c;
f.read(&c, sizeof(char));
while (c != '\0') {
sym += c;
f.read(&c, sizeof(char));
}
}
// address
f.read((char*)&addr, sizeof(uword_t));
exported[sym] = addr;
}
// read number of symbols of pending references
f.read((char*)&tmp, sizeof(uword_t));
// read symbols of pending references
for (uword_t i = 0; i < tmp; ++i) {
string sym;
// string
{
char c;
f.read(&c, sizeof(char));
while (c != '\0') {
sym += c;
f.read(&c, sizeof(char));
}
}
uword_t tmp2;
// read number of references to current symbol
f.read((char*)&tmp2, sizeof(uword_t));
// read references to current symbol
for (uword_t j = 0; j < tmp2; ++j) {
uword_t addr;
f.read((char*)&addr, sizeof(uword_t));
imported[sym].emplace(addr);
}
}
// read number of relative addresses
f.read((char*)&tmp, sizeof(uword_t));
// read relative addresses
for (uword_t i = 0; i < tmp; ++i) {
uword_t addr;
f.read((char*)&addr, sizeof(uword_t));
relative.emplace(addr);
}
// read assembled code size
f.read((char*)&mem_size, sizeof(uword_t));
// read assembled code
mem = new uword_t[mem_size];
f.read((char*)mem, sizeof(uword_t)*mem_size);
f.close();
}
};
int main(int argc, char* argv[]) {
if (argc < 3) {
fprintf(stderr, "Usage mode: subleq-ld <object_files...> <meminit_file>\n");
return 0;
}
bool executable = string(argv[1]) == "-exec";
// read object files
ObjectFile files[argc - 2];
if (executable) {
files[0].setStarter();
}
else {
files[0].read(argv[1], 0);
}
for (int i = 1; i < argc - 2; ++i) {
files[i].read(argv[i + 1], files[i - 1].offset + files[i - 1].mem_size);
}
uword_t mem_size = 0;
uword_t* mem = new uword_t[MEM_WORDS];
map<string, uword_t> symbols;
map<string, set<uword_t>> references;
set<uword_t> relatives;
for (auto& file : files) {
// assemble global symbol table
for (auto& sym : file.exported) {
symbols[sym.first] = sym.second + file.offset;
}
// assemble global reference table
set<uword_t> pendingReferences;
for (auto& sym : file.imported) {
set<uword_t>& refs = references[sym.first];
for (auto addr : sym.second) {
refs.emplace(addr + file.offset);
pendingReferences.emplace(addr);
}
}
// assemble global relative address table
for (auto addr : file.relative) {
relatives.emplace(addr + file.offset);
// relocate only resolved addresses
if (pendingReferences.find(addr) == pendingReferences.end()) {
file.mem[addr] += file.offset;
}
}
// copy object code
memcpy(&mem[mem_size], file.mem, file.mem_size*sizeof(uword_t));
mem_size += file.mem_size;
}
// resolve references
for (auto ref = references.begin(); ref != references.end();) {
auto sym = symbols.find(ref->first);
if (sym == symbols.end()) {
ref++;
}
else {
for (auto addr : ref->second) {
mem[addr] += sym->second;
}
references.erase(ref++);
}
}
// output
fstream f(argv[argc - 1], fstream::out | fstream::binary);
uword_t tmp;
// symbol information is only needed for object files
if (!executable) {
// write number of exported symbols
tmp = symbols.size();
f.write((const char*)&tmp, sizeof(uword_t));
// write exported symbols
for (auto& sym : symbols) {
// string
f.write(sym.first.c_str(), sym.first.size() + 1);
// address
tmp = sym.second;
f.write((const char*)&tmp, sizeof(uword_t));
}
// write number of symbols of pending references
tmp = references.size();
f.write((const char*)&tmp, sizeof(uword_t));
// write symbols of pending references
for (auto& sym : references) {
// string
f.write(sym.first.c_str(), sym.first.size() + 1);
// write number of references to current symbol
tmp = sym.second.size();
f.write((const char*)&tmp, sizeof(uword_t));
// write references to current symbol
for (auto ref : sym.second) {
tmp = ref;
f.write((const char*)&tmp, sizeof(uword_t));
}
}
}
// write number of relative addresses
tmp = relatives.size();
f.write((const char*)&tmp, sizeof(uword_t));
// write relative addresses
for (auto addr : relatives) {
tmp = addr;
f.write((const char*)&tmp, sizeof(uword_t));
}
// write assembled code size
f.write((const char*)&mem_size, sizeof(uword_t));
// write assembled code
f.write((const char*)mem, sizeof(uword_t)*mem_size);
f.close();
// output mif
f.open((string(argv[argc - 1]) + ".mif").c_str(), fstream::out);
char buf[20];
f << "DEPTH = " << MEM_WORDS << ";\n";
f << "WIDTH = " << WORD_WIDTH << ";\n";
f << "ADDRESS_RADIX = HEX;\n";
f << "DATA_RADIX = HEX;\n";
f << "CONTENT\n";
f << "BEGIN\n";
f << "\n";
for (uword_t i = 0; i < mem_size; ++i) {
sprintf(buf, "%08x", i);
f << buf;
sprintf(buf, "%08x", mem[i]);
f << " : " << buf << ";\n";
}
f << "\n";
f << "END;\n";
f.close();
delete[] mem;
return 0;
}