-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdl_textir.hpp
455 lines (386 loc) · 14.5 KB
/
hdl_textir.hpp
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// Copyright 2023 Can Joshua Lehmann
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef HDL_TEXTIR_HPP
#define HDL_TEXTIR_HPP
#include <unordered_map>
#include <string>
#include <sstream>
#include "hdl.hpp"
#define throw_error(Error, msg) { \
std::ostringstream error_message; \
error_message << msg; \
throw Error(error_message.str()); \
}
namespace hdl {
namespace textir {
class Reader {
private:
Module& _module;
bool is_digit(char chr) const {
return chr >= '0' && chr <= '9';
}
bool is_whitespace(char chr) const {
return chr == ' ' || chr == '\t' || chr == '\r';
}
void skip_whitespace(std::istream& stream) const {
while (!stream.eof() && is_whitespace(stream.peek())) {
stream.get();
}
}
size_t read_size(std::istream& stream) const {
skip_whitespace(stream);
size_t value = 0;
if (stream.eof() || !is_digit(stream.peek())) {
throw_error(Error, "Expected number");
}
stream >> value;
return value;
}
uint64_t read_uint64(std::istream& stream) const {
skip_whitespace(stream);
uint64_t value = 0;
if (stream.eof() || !is_digit(stream.peek())) {
throw_error(Error, "Expected uint64");
}
stream >> value;
return value;
}
size_t read_id(std::istream& stream) const {
return read_size(stream);
}
char hex_digit(char chr) const {
if (chr >= '0' && chr <= '9') {
return chr - '0';
} else if (chr >= 'a' && chr <= 'f') {
return chr - 'a' + 10;
} else if (chr >= 'A' && chr <= 'F') {
return chr - 'A' + 10;
} else {
throw_error(Error, "Invalid hex digit");
}
}
std::string read_string(std::istream& stream) const {
skip_whitespace(stream);
if (stream.get() != '\"') {
throw_error(Error, "Expected \"");
}
std::string string;
while (stream.peek() != '\"') {
if (stream.eof()) {
throw_error(Error, "Unterminated string literal");
}
if (stream.peek() == '\\') {
stream.get();
if (stream.get() != 'x') {
throw_error(Error, "Expected x");
}
char chr = hex_digit(stream.get()) << 4;
chr |= hex_digit(stream.get());
string.push_back(chr);
} else {
string.push_back(char(stream.get()));
}
}
stream.get();
return string;
}
std::string read_word(std::istream& stream) const {
skip_whitespace(stream);
std::string word;
while (!stream.eof() && stream.peek() != '\n' && !is_whitespace(stream.peek())) {
word.push_back(char(stream.get()));
}
return word;
}
BitString read_bit_string(std::istream& stream) const {
size_t width = read_size(stream);
if (stream.get() != '\'') {
throw_error(Error, "Expected \'");
}
if (stream.get() != 'b') {
throw_error(Error, "Expected b");
}
BitString bit_string(width);
std::string bits = read_word(stream);
for (size_t it = 0; it < bits.size(); it++) {
char bit = bits[bits.size() - it - 1];
if (bit != '0' && bit != '1') {
throw_error(Error, "Invalid binary digit");
}
bit_string.set(it, bit == '1');
}
return bit_string;
}
public:
Reader(Module& module): _module(module) {}
static Module read_module(std::istream& stream) {
Module module("top");
Reader reader(module);
reader.read(stream);
return module;
}
static Module load_module(const char* path) {
Module module("top");
Reader reader(module);
reader.load(path);
return module;
}
void read(std::istream& stream) const {
std::unordered_map<size_t, Value*> values;
std::unordered_map<size_t, Memory*> memories;
skip_whitespace(stream);
while (!stream.eof()) {
while (!stream.eof() && stream.peek() == '#') {
while (!stream.eof() && stream.get() != '\n') {}
skip_whitespace(stream);
}
size_t id = 0;
bool has_id = false;
if (is_digit(stream.peek())) {
id = read_id(stream);
has_id = true;
skip_whitespace(stream);
if (stream.get() != '=') {
throw_error(Error, "Expected =");
}
skip_whitespace(stream);
}
std::string cmd = read_word(stream);
if (cmd == "input") {
std::string name = read_string(stream);
size_t width = read_size(stream);
if (!has_id) { throw_error(Error, "Does not have id"); }
values[id] = _module.input(name, width);
} else if (cmd == "reg") {
BitString initial = read_bit_string(stream);
Reg* reg = _module.reg(initial, nullptr);
reg->name = read_string(stream);
if (!has_id) { throw_error(Error, "Does not have id"); }
values[id] = reg;
} else if (cmd == "memory") {
size_t width = read_size(stream);
size_t size = read_size(stream);
hdl::Memory* memory = _module.memory(width, size);
memory->name = read_string(stream);
if (!has_id) { throw_error(Error, "Does not have id"); }
memories[id] = memory;
} else if (cmd == "next") {
Reg* reg = dynamic_cast<Reg*>(values.at(read_size(stream)));
reg->clock = values.at(read_id(stream));
reg->next = values.at(read_id(stream));
} else if (cmd == "read") {
Memory* memory = memories.at(read_id(stream));
Value* address = values.at(read_id(stream));
if (!has_id) { throw_error(Error, "Does not have id"); }
values[id] = memory->read(address);
} else if (cmd == "write") {
Memory* memory = memories.at(read_id(stream));
Value* clock = values.at(read_id(stream));
Value* address = values.at(read_id(stream));
Value* enable = values.at(read_id(stream));
Value* value = values.at(read_id(stream));
memory->write(clock, address, enable, value);
} else if (cmd == "output") {
std::string name = read_string(stream);
Value* value = values.at(read_id(stream));
_module.output(name, value);
} else if (cmd == "constant") {
BitString bit_string = read_bit_string(stream);
if (!has_id) { throw_error(Error, "Does not have id"); }
values[id] = _module.constant(bit_string);
} else if (cmd == "unknown") {
size_t width = read_size(stream);
if (!has_id) { throw_error(Error, "Does not have id"); }
values[id] = _module.unknown(width);
} else if (cmd == "init") {
Memory* memory = memories.at(read_id(stream));
uint64_t address = read_uint64(stream);
BitString value = read_bit_string(stream);
memory->init(address, value);
} else {
Op::Kind kind;
bool has_kind = false;
for (size_t it = 0; it < Op::KIND_COUNT; it++) {
if (cmd == Op::KIND_NAMES[it]) {
kind = Op::Kind(it);
has_kind = true;
break;
}
}
if (!has_kind) {
throw_error(Error, "Unknown command " << cmd);
}
std::vector<Value*> args;
skip_whitespace(stream);
while (!stream.eof() && stream.peek() != '\n') {
args.push_back(values.at(read_id(stream)));
skip_whitespace(stream);
}
if (!has_id) { throw_error(Error, "Does not have id"); }
values[id] = _module.op(kind, args);
}
skip_whitespace(stream);
if (!stream.eof() && stream.get() != '\n') {
throw_error(Error, "Expected newline or EOF");
}
skip_whitespace(stream);
}
}
void load(const char* path) const {
std::ifstream file;
file.open(path);
if (!file) {
throw_error(Error, "Failed to open \"" << path << "\"");
}
read(file);
}
};
class Printer {
private:
Module& _module;
bool is_printable(char chr) const {
return (chr >= '!' && chr <= '~' && chr != '\\' && chr != '\"') || chr == ' ';
}
void print(std::ostream& stream, const char* str) const {
static const char* HEX_DIGITS = "0123456789abcdef";
stream << '\"';
for (const char* cur = str; *cur != '\0'; cur++) {
if (is_printable(*cur)) {
stream << *cur;
} else {
stream << "\\x" << HEX_DIGITS[(*cur >> 4) & 0xf] << HEX_DIGITS[*cur & 0xf];
}
}
stream << '\"';
}
void print(std::ostream& stream, const std::string& str) const {
print(stream, str.c_str());
}
void print(std::ostream& stream, const BitString& bit_string) const {
bit_string.write_short(stream);
}
struct Context {
std::ostream& stream;
size_t id_count = 0;
std::unordered_map<const Value*, size_t> values;
std::unordered_map<const Memory*, size_t> memories;
Context(std::ostream& _stream): stream(_stream) {}
size_t alloc(const Value* value) {
size_t id = id_count++;
values[value] = id;
return id;
}
size_t alloc(const Memory* memory) {
size_t id = id_count++;
memories[memory] = id;
return id;
}
size_t operator[](const Value* value) const { return values.at(value); }
size_t operator[](const Memory* memory) const { return memories.at(memory); }
bool has(const Value* value) const { return values.find(value) != values.end(); }
};
void print(Value* value, Context& context) const {
if (context.has(value)) {
return;
}
if (Constant* constant = dynamic_cast<Constant*>(value)) {
context.stream << context.alloc(value) << " = constant ";
print(context.stream, constant->value);
} else if (Unknown* unknown = dynamic_cast<Unknown*>(value)) {
context.stream << context.alloc(value) << " = unknown ";
context.stream << unknown->width;
} else if (Op* op = dynamic_cast<Op*>(value)) {
for (Value* arg : op->args) {
print(arg, context);
}
context.stream << context.alloc(value) << " = " << op->kind;
for (Value* arg : op->args) {
context.stream << ' ' << context[arg];
}
} else if (Memory::Read* read = dynamic_cast<Memory::Read*>(value)) {
print(read->address, context);
context.stream << context.alloc(value) << " = read ";
context.stream << context[read->memory] << ' ';
context.stream << context[read->address];
} else {
throw_error(Error, "Unreachable");
}
context.stream << '\n';
}
public:
Printer(Module& module): _module(module) {}
void print(std::ostream& stream) const {
Context context(stream);
for (Input* input : _module.inputs()) {
stream << context.alloc(input) << " = input ";
print(stream, input->name);
stream << " " << input->width << '\n';
}
for (Reg* reg : _module.regs()) {
stream << context.alloc(reg) << " = reg ";
print(stream, reg->initial);
stream << ' ';
print(stream, reg->name);
stream << '\n';
}
for (Memory* memory : _module.memories()) {
size_t id = context.alloc(memory);
stream << id << " = memory ";
stream << memory->width << ' ' << memory->size << ' ';
print(stream, memory->name);
stream << '\n';
for (const auto& [address, value] : memory->initial) {
stream << "init " << id << ' ' << address;
stream << ' ';
print(stream, value);
stream << '\n';
}
}
for (Reg* reg : _module.regs()) {
print(reg->clock, context);
print(reg->next, context);
stream << "next " << context[reg] << ' ';
stream << context[reg->clock] << ' ';
stream << context[reg->next] << '\n';
}
for (Memory* memory : _module.memories()) {
for (const Memory::Write& write : memory->writes) {
print(write.clock, context);
print(write.address, context);
print(write.enable, context);
print(write.value, context);
stream << "write " << context[memory] << ' ';
stream << context[write.clock] << ' ';
stream << context[write.address] << ' ';
stream << context[write.enable] << ' ';
stream << context[write.value] << '\n';
}
}
for (const Output& output : _module.outputs()) {
print(output.value, context);
stream << "output ";
print(stream, output.name);
stream << ' ' << context[output.value] << '\n';
}
}
void save(const char* path) const {
std::ofstream file;
file.open(path);
print(file);
}
};
}
}
#undef throw_error
#endif