-
Notifications
You must be signed in to change notification settings - Fork 1
/
statements.cpp
445 lines (397 loc) · 12.3 KB
/
statements.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
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
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <assert.h>
#include "tokens.h"
#include "statements.h"
bool move_comp_to_statement(evl_tokens &statement_tokens, evl_tokens &tokens) {
assert(statement_tokens.empty());
assert(!tokens.empty());
for (; !tokens.empty();) {
statement_tokens.push_back(tokens.front());
tokens.erase(tokens.begin());
if (statement_tokens.back().str == ")")
break; //exit if the ending ")" is found
}
if (statement_tokens.back().str != ")") {
std::cerr << "Look for ')' but reach the end of file" << std::endl;
return false;
}
return true;
}
bool move_wire_to_statement(evl_tokens &statement_tokens, evl_tokens &tokens) {
assert(statement_tokens.empty());
assert(!tokens.empty());
for (; !tokens.empty();) {
statement_tokens.push_back(tokens.front());
tokens.erase(tokens.begin());
if (statement_tokens.back().str == "=")
break; //exit if the ending "=" is found
}
if (statement_tokens.back().str != "=") {
std::cerr << "Look for '=' but reach the end of file" << std::endl;
return false;
}
return true;
}
bool group_tokens_into_statements(evl_statements &statements, evl_tokens &tokens) {
assert(statements.empty());
for(; !tokens.empty();) { //generates one statement per iteration
evl_token token = tokens.front();
if (token.type != evl_token::NAME) {
std::cerr << "Need a NAME token but found '" << token.str << "' on line" << token.line_no << std::endl;
return false;
}
if ((token.str == "INPUT") || (token.str == "OUTPUT") || (token.str == "AND") ||
(token.str == "NAND") || (token.str == "OR") || (token.str == "NOR") ||
(token.str == "DFF") || (token.str == "NOT")) {
evl_statement component;
component.type = evl_statement::COMPONENT;
if(!move_comp_to_statement(component.tokens, tokens)) {
return false;
}
//INPUT and OUTPUT are both components and wires;
if ((token.str == "INPUT") || (token.str == "OUTPUT")) {
evl_statement wire;
wire.type = evl_statement::WIRE;
wire.tokens = component.tokens;
statements.push_back(wire);
}
statements.push_back(component);
}
else {
evl_statement wire;
wire.type = evl_statement::WIRE;
if(!move_wire_to_statement(wire.tokens, tokens)) {
return false;
}
statements.push_back(wire);
}
}
return true;
}
//Re-order statements so order is MODULE,WIRES,COMPONENTS,ENDMODULE,...repeat
bool reorder_statements_list (evl_statements::const_iterator m_itr, evl_statements &statements){
m_itr = statements.begin();
for(evl_statements::const_iterator it = statements.begin(); it != statements.end(); ++it) {
if (it->type == evl_statement::WIRE) {
statements.insert(++m_itr,*it);
m_itr--;
it = statements.erase(it);
//--it;
}
}
return true;
}
void display_statements(std::ostream &out, const evl_statements &statements) {
int count = 1;
for (evl_statements::const_iterator iter = statements.begin(); iter != statements.end(); ++iter) {
if (iter->type == evl_statement::MODULE) {
out << "STATEMENT " << count << " MODULE, " << iter->tokens.size() << " tokens" << std::endl;
}
else if (iter->type == evl_statement::WIRE) {
out << "STATEMENT " << count << " WIRE, " << iter->tokens.size() << " tokens" << std::endl;
}
else if (iter->type == evl_statement::COMPONENT) {
out << "STATEMENT " << count << " COMPONENT, " << iter->tokens.size() << " tokens" << std::endl;
}
else if (iter->type == evl_statement::ENDMODULE) {
out << "STATEMENT " << count << " ENDMODULE" << std::endl;
}
else {
out << "ERROR" << std::endl;
}
count++;
}
}
bool store_statements_to_file(std::string file_name, const evl_statements &statements) {
std::ofstream output_file(file_name.c_str());
if(!output_file) {
std::cerr << "I can't write Statment file." << std::endl;
return false;
}
display_statements(output_file, statements);
std::cout << file_name << std::endl;
return true;
}
//Module name and module number stored in a std::map
int i = 0;
bool process_statements(evl_modules &modules, evl_statement &s) {
//assert(s.type == evl_statement::MODULE);
enum state_type {INIT, NAME, SEMI_CHECK, CHK_STMT, WIRE, COMPONENT, ENDMOD, DONE};
state_type state;
if (s.type == evl_statement::WIRE) {
state = WIRE;
}
else if (s.type == evl_statement::COMPONENT) {
state = COMPONENT;
}
if(i == 0) {
evl_module mod;
mod.name = "top";
modules.push_back(mod);
i++;
}
for (; !s.tokens.empty() && (state != DONE); s.tokens.pop_front()) {
evl_token t = s.tokens.front();
if (state == WIRE) {
evl_wires wires;
if (!process_wire_statements(wires,s)) {
return false;
}
else {
modules.back().c_wires.insert(wires.begin(), wires.end());
state = DONE;
break;
}
}
else if (state == COMPONENT) {
evl_components comp;
if (!process_component_statements(comp,s,modules.back().c_wires,modules.back().c_checkpoints)) {
return false;
}
else {
modules.back().c_components.splice(modules.back().c_components.end(),comp);
state = DONE;
break;
}
}
else {
//state = DONE;
assert(false); //never should get here
}
}
if (!s.tokens.empty() || (state != DONE)) {
std::cerr << "something wrong with the statement in mod" << std::endl;
return false;
}
return true;
}
bool process_wire_statements(evl_wires &wires, evl_statement &s) {
assert(s.type == evl_statement::WIRE);
enum state_type {INIT, WIRE,IO, WIRES, WIRE_NAME, BUS, BUS_MSB, BUS_COLON, BUS_LSB, BUS_DONE, DONE};
state_type state = INIT;
int bus_width = 1;
for (; !s.tokens.empty() && (state != DONE); s.tokens.pop_front()) {
evl_token t = s.tokens.front();
if (state == INIT) {
if ((t.str == "INPUT") || (t.str == "OUTPUT")) {
state = IO;
}
else {
if (t.type == evl_token::NAME) {
evl_wires::iterator it = wires.find(t.str);
if(it != wires.end()) {
std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is already defined" << std::endl;
return false;
}
wires.insert(std::make_pair(t.str, bus_width));
state = WIRE_NAME;
}
}
}
else if (state == IO) {
if (t.type == evl_token::SINGLE) {
if(t.str != "("){
std::cerr << "Need a '(' but found '" << t.str << "' on line" << t.line_no << std::endl;
return false;
}
state = WIRE;
}
else {
std::cerr << "Need Input or Output designation but found '" << t.str << "' on line " << t.line_no << std::endl;
return false;
}
}
else if (state == WIRE) {
if (t.type == evl_token::NAME) {
evl_wires::iterator it = wires.find(t.str);
if(it != wires.end()) {
std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is already defined" << std::endl;
return false;
}
wires.insert(std::make_pair(t.str, bus_width));
state = WIRE_NAME;
}
else {
std::cerr << "Need 'wire' but found '" << t.str << "' on line " << t.line_no << std::endl;
return false;
}
}
else if (state == WIRE_NAME) {
if ((t.str == ")") || (t.str == "=")) {
state = DONE;
}
else {
std::cerr << "Need ',' or ';' but found '" << t.str << "' on line " << t.line_no << std::endl;
return false;
}
}
else {
assert(false); //never should get here
}
}
if (!s.tokens.empty() || (state != DONE)) {
std::cerr << "something wrong with the statement in wires" << std::endl;
return false;
}
return true;
}
bool process_component_statements(evl_components &components, evl_statement &s, evl_wires &wires, evl_checkpoints &check) {
//assert(s.type == evl_statement::COMPONENT);
enum state_type {INIT, TYPE, NAME, PINS, PIN_NAME, PINS_DONE, BUS, BUS_MSB, BUS_COLON, BUS_LSB, BUS_DONE, DONE};
std::string comp_type = "";
std::string comp_name = "NONE";
std::string pin_name = "";
int bus_msb = -1;
int bus_lsb = -1;
int count = 0;
int count_2;
state_type state = INIT;
for(; !s.tokens.empty() && (state != DONE); s.tokens.pop_front()){
evl_token t = s.tokens.front();
if (state == INIT) {
if(t.type == evl_token::NAME) {
comp_type = t.str;
state = TYPE;
}
else {
state = TYPE;
}
}
else if (state == TYPE){
if (t.type == evl_token::SINGLE && t.str == "(") {
state = PINS;
}
else {
std::cerr << "Need a NAME or a '(' but found '" << t.str << "' at line " << t.line_no << std::endl;
return false;
}
evl_component component;
component.type = comp_type;
component.name = comp_name;
components.push_back(component);
count_2 = 0;
}
else if (state == PINS) {
//int count = 0;
if (t.type == evl_token::NAME) {
evl_wires::iterator it = wires.find(t.str);
if (it == wires.end()) {
std::cerr << "Wire '" << t.str << "' on line " << t.line_no << " is not defined" << std::endl;
return false;
}
count = it->second + count_2;
it->second = ++count;
pin_name = t.str;
}
state = PIN_NAME;
}
else if (state == PIN_NAME) {
bus_msb = -1;
bus_lsb = -1;
//int count = 0;
if (t.str == ")"){
evl_wires::iterator it = wires.find(pin_name);
count = it->second + count_2;
it->second = ++count;
//if (it->second >= 2) {
// bus_msb = it->second-1;
// bus_lsb = 0;
//}
evl_pin pins;
pins.lsb = bus_lsb;
pins.msb = bus_msb;
pins.pin_name = pin_name;
components.back().comp_pins.push_back(pins);
state = DONE;
}
else if (t.str == ","){
evl_wires::iterator it = wires.find(pin_name);
//if (it->second >= 2) {
// bus_msb = it->second-1;
// bus_lsb = 0;
//}
count = it->second + count_2;
it->second = ++count;
evl_pin pins;
pins.lsb = bus_lsb;
pins.msb = bus_msb;
pins.pin_name = pin_name;
components.back().comp_pins.push_back(pins);
state = PINS;
}
else {
std::cerr << "Need ',' or ')' or '[' but found '" << t.str << "' on line " << t.line_no <<std::endl;
return false;
}
}
else {
assert(false); //never should get here
}
}
if (!s.tokens.empty() || (state != DONE)) {
std::cerr << "something wrong with the statement in Comp" << std::endl;
return false;
}
return true;
}
void display_module(std::ostream &out, const evl_modules &modules) {
for (evl_modules::const_iterator it1 = modules.begin(); it1 != modules.end(); ++it1) {
out << "module " << it1->name << " " << it1->c_wires.size() << " " << it1->c_components.size() << std::endl;
for(evl_wires::const_iterator it2 = it1->c_wires.begin(); it2 != it1->c_wires.end(); ++it2) {
out << "wire " << it2->first << " " << it2->second << std::endl;
}
for (evl_components::const_iterator it3 = it1->c_components.begin(); it3 != it1->c_components.end(); ++it3) {
out << "component " << it3->type << " " << it3->name << " " << it3->comp_pins.size() << std::endl;
for(evl_pins::const_iterator it4 = it3->comp_pins.begin(); it4 != it3->comp_pins.end(); ++it4) {
out << "pin " << it4->pin_name << " " << it4->msb << " " << it4->lsb << std::endl;
}
}
}
}
bool store_module_to_file(std::string file_name, evl_modules &modules) {
std::ofstream output_file(file_name.c_str());
if(!output_file) {
std::cerr << "I can't write Module File." << std::endl;
return false;
}
display_module(output_file, modules);
std::cout << file_name << std::endl;
return true;
}
bool parse_evl_file(std::string file_name, evl_modules &modules) {
evl_tokens tokens;
if(!extract_tokens_from_file(file_name, tokens)) {
return false;
}
//display_tokens(std::cout, tokens);
if (!store_tokens_to_file(file_name + ".tokens", tokens)) {
return false;
}
evl_statements statements;
if(!group_tokens_into_statements(statements, tokens)){
return false;
}
//display_statements(std::cout,statements); //displays before the sorting
if (!store_statements_to_file(file_name + ".statements", statements)) {
return false;
}
evl_statements::const_iterator mod_itr;
if (!reorder_statements_list(mod_itr,statements)) { //Reorders the statements list to put wires first
return false;
}
//display_statements(std::cout,statements); //displays after the sort is performed
for (evl_statements::iterator iter = statements.begin(); iter != statements.end(); ++iter) {
if (!process_statements(modules,*iter)) {
return false;
}
}
//display_module(std::cout,modules);
if (!store_module_to_file(file_name + ".syntax", modules)) {
return false;
}
return true;
}