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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
|
/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2018 SymbioticEDA
*
* jsonparse.cc -- liberally copied from the yosys file of the same name by
*
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "jsonparse.h"
#include <assert.h>
#include <fstream>
#include <iostream>
#include <log.h>
#include <string>
#include "chip.h"
#include "design.h"
extern bool check_all_nets_driven(Design *design);
namespace JsonParser {
const bool json_debug = false;
typedef std::string string;
template <typename T> int GetSize(const T &obj) { return obj.size(); }
struct JsonNode
{
char type; // S=String, N=Number, A=Array, D=Dict
string data_string;
int data_number;
vector<JsonNode *> data_array;
dict<string, JsonNode *> data_dict;
vector<string> data_dict_keys;
JsonNode(std::istream &f)
{
type = 0;
data_number = 0;
while (1) {
int ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n");
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n')
continue;
if (ch == '\"') {
type = 'S';
while (1) {
ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON string.\n");
if (ch == '\"')
break;
if (ch == '\\') {
int ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON string.\n");
}
data_string += ch;
}
break;
}
if ('0' <= ch && ch <= '9') {
type = 'N';
data_number = ch - '0';
data_string += ch;
while (1) {
ch = f.get();
if (ch == EOF)
break;
if (ch == '.')
goto parse_real;
if (ch < '0' || '9' < ch) {
f.unget();
break;
}
data_number = data_number * 10 + (ch - '0');
data_string += ch;
}
data_string = "";
break;
parse_real:
type = 'S';
data_number = 0;
data_string += ch;
while (1) {
ch = f.get();
if (ch == EOF)
break;
if (ch < '0' || '9' < ch) {
f.unget();
break;
}
data_string += ch;
}
break;
}
if (ch == '[') {
type = 'A';
while (1) {
ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n");
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' ||
ch == ',')
continue;
if (ch == ']')
break;
f.unget();
data_array.push_back(new JsonNode(f));
}
break;
}
if (ch == '{') {
type = 'D';
while (1) {
ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n");
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' ||
ch == ',')
continue;
if (ch == '}')
break;
f.unget();
JsonNode key(f);
while (1) {
ch = f.get();
if (ch == EOF)
log_error("Unexpected EOF in JSON file.\n");
if (ch == ' ' || ch == '\t' || ch == '\r' ||
ch == '\n' || ch == ':')
continue;
f.unget();
break;
}
JsonNode *value = new JsonNode(f);
if (key.type != 'S')
log_error("Unexpected non-string key in JSON dict.\n");
data_dict[key.data_string] = value;
data_dict_keys.push_back(key.data_string);
}
break;
}
log_error("Unexpected character in JSON file: '%c'\n", ch);
}
}
~JsonNode()
{
for (auto it : data_array)
delete it;
for (auto &it : data_dict)
delete it.second;
}
};
NetInfo *ground_net(NetInfo *net)
{
CellInfo *cell = new CellInfo;
PortInfo port_info;
PortRef port_ref;
cell->name = string(net->name + ".GND");
cell->type = string("GND");
port_info.name = cell->name + "[]";
port_info.net = net;
port_info.type = PORT_OUT;
port_ref.cell = cell;
port_ref.port = port_info.name;
net->driver = port_ref;
cell->ports[port_info.name] = port_info;
return net;
}
NetInfo *vcc_net(NetInfo *net)
{
CellInfo *cell = new CellInfo;
PortInfo port_info;
PortRef port_ref;
cell->name = string(net->name + ".VCC");
cell->type = string("VCC");
port_info.name = cell->name + "[]";
port_info.net = net;
port_info.type = PORT_OUT;
port_ref.cell = cell;
port_ref.port = port_info.name;
net->driver = port_ref;
cell->ports[port_info.name] = port_info;
return net;
}
NetInfo *floating_net(NetInfo *net)
{
PortInfo port_info;
PortRef port_ref;
port_info.name = net->name + ".floating";
port_info.net = net;
port_info.type = PORT_OUT;
port_ref.cell = NULL;
port_ref.port = port_info.name;
net->driver = port_ref;
return net;
}
//
// is_blackbox
//
// Checks the JsonNode for an attributes dictionary, with a "blackbox" entry.
// An item is deemed to be a blackbox if this entry exists and if its
// value is not zero. If the item is a black box, this routine will return
// true, false otherwise
bool is_blackbox(JsonNode *node)
{
JsonNode *attr_node, *bbox_node;
if (node->data_dict.count("attributes") == 0)
return false;
attr_node = node->data_dict.at("attributes");
if (attr_node == NULL)
return false;
if (attr_node->type != 'D')
return false;
if (GetSize(attr_node->data_dict) == 0)
return false;
if (attr_node->data_dict.count("blackbox") == 0)
return false;
bbox_node = attr_node->data_dict.at("blackbox");
if (bbox_node == NULL)
return false;
if (bbox_node->type != 'N')
log_error("JSON module blackbox is not a number\n");
if (bbox_node->data_number == 0)
return false;
return true;
}
void json_import_cell_params(Design *design, string &modname, CellInfo *cell,
JsonNode *param_node,
dict<IdString, std::string> *dest, int param_id)
{
//
JsonNode *param;
IdString pId;
//
param = param_node->data_dict.at(param_node->data_dict_keys[param_id]);
pId = param_node->data_dict_keys[param_id];
if (param->type == 'N') {
(*dest)[pId] = std::to_string(param->data_number);
} else if (param->type == 'S')
(*dest)[pId] = param->data_string;
else
log_error(
"JSON parameter type of \"%s\' of cell \'%s\' not supported\n",
pId.c_str(), cell->name.c_str());
if (json_debug)
log_info(" Added parameter \'%s\'=%s to cell \'%s\' "
"of module \'%s\'\n",
pId.c_str(), cell->params[pId].c_str(), cell->name.c_str(),
modname.c_str());
}
void json_import_cell_ports(Design *design, string &modname, CellInfo *cell,
string &port_name, JsonNode *dir_node,
JsonNode *wire_group_node)
{
// Examine and connect a single port of the given cell to its nets,
// generating them as necessary
assert(dir_node);
if (json_debug)
log_info(" Examining port %s, node %s\n", port_name.c_str(),
cell->name.c_str());
if (!wire_group_node)
log_error("JSON no connection match "
"for port_direction \'%s\' of node \'%s\' "
"in module \'%s\'\n",
port_name.c_str(), cell->name.c_str(), modname.c_str());
assert(wire_group_node);
assert(dir_node->type == 'S');
assert(wire_group_node->type == 'A');
PortInfo port_info;
port_info.name = port_name;
if (dir_node->data_string.compare("input") == 0)
port_info.type = PORT_IN;
else if (dir_node->data_string.compare("output") == 0)
port_info.type = PORT_OUT;
else if (dir_node->data_string.compare("inout") == 0)
port_info.type = PORT_INOUT;
else
log_error("JSON unknown port direction \'%s\' in node \'%s\' "
"of module \'%s\'\n",
dir_node->data_string.c_str(), cell->name.c_str(),
modname.c_str());
//
// Find an update, or create a net to connect
// to this port.
//
NetInfo *this_net;
bool is_bus;
//
// If this port references a bus, then there will be multiple nets
// connected to it, all specified as part of an array.
//
is_bus = (wire_group_node->data_array.size() > 1);
// Now loop through all of the connections to this port.
if (wire_group_node->data_array.size() == 0) {
//
// There is/are no connections to this port.
//
// Create the port, but leave the net NULL
PortInfo this_port;
//
this_port.name = port_info.name;
this_port.type = port_info.type;
this_port.net = NULL;
cell->ports[this_port.name] = this_port;
if (json_debug)
log_info(" Port \'%s\' has no connection in \'%s\'\n",
this_port.name.c_str(), cell->name.c_str());
} else
for (int index = 0; index < wire_group_node->data_array.size();
index++) {
//
JsonNode *wire_node;
PortInfo this_port;
PortRef port_ref;
bool const_input = false;
IdString net_id;
//
wire_node = wire_group_node->data_array[index];
port_ref.cell = cell;
//
// Pick a name for this port
if (is_bus)
this_port.name =
port_info.name + '[' + std::to_string(index) + ']';
else
this_port.name = port_info.name;
this_port.type = port_info.type;
port_ref.port = this_port.name;
if (wire_node->type == 'N') {
int net_num;
// A simple net, specified by a number
net_num = wire_node->data_number;
net_id = std::to_string(net_num);
if (design->nets.count(net_id) == 0) {
// The net doesn't exist in the design (yet)
// Create in now
if (json_debug)
log_info(" Generating a new net, \'%d\'\n",
net_num);
this_net = new NetInfo;
this_net->name = net_id;
this_net->driver.cell = NULL;
this_net->driver.port = "";
design->nets[net_id] = this_net;
} else {
//
// The net already exists within the design.
// We'll connect to it
//
this_net = design->nets[net_id];
if (json_debug)
log_info(" Reusing net \'%s\', id \'%s\', "
"with driver \'%s\'\n",
this_net->name.c_str(), net_id.c_str(),
(this_net->driver.cell != NULL)
? this_net->driver.port.c_str()
: "NULL");
}
} else if (wire_node->type == 'S') {
// Strings are only used to drive wires for the fixed
// values "0", "1", and "x". Handle those constant
// values here.
//
// Constants always get their own new net
this_net = new NetInfo;
this_net->name = net_id;
const_input = (this_port.type == PORT_IN);
if (wire_node->data_string.compare(string("0")) == 0) {
if (json_debug)
log_info(" Generating a constant "
"zero net\n");
this_net = ground_net(this_net);
} else if (wire_node->data_string.compare(string("1")) == 0) {
if (json_debug)
log_info(" Generating a constant "
"one net\n");
this_net = vcc_net(this_net);
} else if (wire_node->data_string.compare(string("x")) == 0) {
this_net = floating_net(this_net);
log_warning(" Floating wire node value, "
"\'%s\' of port \'%s\' "
"in cell \'%s\' of module \'%s\'\n",
wire_node->data_string.c_str(),
port_name.c_str(), cell->name.c_str(),
modname.c_str());
} else
log_error(" Unknown fixed type wire node "
"value, \'%s\'\n",
wire_node->data_string.c_str());
}
if (json_debug)
log_info(" Inserting port \'%s\' into cell \'%s\'\n",
this_port.name.c_str(), cell->name.c_str());
this_port.net = this_net;
cell->ports[this_port.name] = this_port;
if (this_port.type == PORT_OUT) {
assert(this_net->driver.cell == NULL);
this_net->driver = port_ref;
} else
this_net->users.push_back(port_ref);
if (design->nets.count(this_net->name) == 0)
design->nets[this_net->name] = this_net;
}
}
void json_import_cell(Design *design, string modname, JsonNode *cell_node,
string cell_name)
{
JsonNode *cell_type, *param_node, *attr_node;
cell_type = cell_node->data_dict.at("type");
if (cell_type == NULL)
return;
CellInfo *cell = new CellInfo;
cell->name = cell_name;
assert(cell_type->type == 'S');
cell->type = cell_type->data_string;
// No BEL assignment here/yet
if (json_debug)
log_info(" Processing %s $ %s\n", modname.c_str(), cell->name.c_str());
param_node = cell_node->data_dict.at("parameters");
if (param_node->type != 'D')
log_error("JSON parameter list of \'%s\' is not a data dictionary\n",
cell->name.c_str());
//
// Loop through all parameters, adding them into the
// design to annotate the cell
//
for (int paramid = 0; paramid < GetSize(param_node->data_dict_keys);
paramid++) {
json_import_cell_params(design, modname, cell, param_node,
&cell->params, paramid);
}
attr_node = cell_node->data_dict.at("attributes");
if (attr_node->type != 'D')
log_error("JSON attribute list of \'%s\' is not a data dictionary\n",
cell->name.c_str());
//
// Loop through all attributes, adding them into the
// design to annotate the cell
//
for (int attrid = 0; attrid < GetSize(attr_node->data_dict_keys);
attrid++) {
json_import_cell_params(design, modname, cell, attr_node, &cell->attrs,
attrid);
}
//
// Now connect the ports of this module. The ports are defined by
// both the port directions node as well as the connections node.
// Both should contain dictionaries having the same keys.
//
JsonNode *pdir_node = NULL;
if (cell_node->data_dict.count("port_directions") > 0) {
pdir_node = cell_node->data_dict.at("port_directions");
if (pdir_node->type != 'D')
log_error("JSON port_directions node of \'%s\' "
"in module \'%s\' is not a "
"dictionary\n",
cell->name.c_str(), modname.c_str());
} else if (cell_node->data_dict.count("ports") > 0) {
pdir_node = cell_node->data_dict.at("ports");
if (pdir_node->type != 'D')
log_error("JSON ports node of \'%s\' "
"in module \'%s\' is not a "
"dictionary\n",
cell->name.c_str(), modname.c_str());
}
JsonNode *connections = cell_node->data_dict.at("connections");
if (connections->type != 'D')
log_error("JSON connections node of \'%s\' "
"in module \'%s\' is not a "
"dictionary\n",
cell->name.c_str(), modname.c_str());
if (GetSize(pdir_node->data_dict_keys) !=
GetSize(connections->data_dict_keys))
log_error("JSON number of connections doesnt "
"match number of ports in node \'%s\' "
"of module \'%s\'\n",
cell->name.c_str(), modname.c_str());
//
// Loop through all of the ports of this logic element
//
for (int portid = 0; portid < GetSize(pdir_node->data_dict_keys);
portid++) {
//
string port_name;
JsonNode *dir_node, *wire_group_node;
//
port_name = pdir_node->data_dict_keys[portid];
dir_node = pdir_node->data_dict.at(port_name);
wire_group_node = connections->data_dict.at(port_name);
json_import_cell_ports(design, modname, cell, port_name, dir_node,
wire_group_node);
}
design->cells[cell->name] = cell;
// check_all_nets_driven(design);
}
void json_import(Design *design, string modname, JsonNode *node)
{
if (is_blackbox(node))
return;
log_info("Importing module %s\n", modname.c_str());
if (node->data_dict.count("cells")) {
JsonNode *cell_parent = node->data_dict.at("cells");
//
//
// Loop through all of the logic elements in a flattened design
//
//
for (int cellid = 0; cellid < GetSize(cell_parent->data_dict_keys);
cellid++) {
JsonNode *cell_type, *here, *param_node;
here = cell_parent->data_dict.at(
cell_parent->data_dict_keys[cellid]);
json_import_cell(design, modname, here,
cell_parent->data_dict_keys[cellid]);
}
}
check_all_nets_driven(design);
}
struct JsonFrontend
{
// JsonFrontend() : Frontend("json", "read JSON file") { }
JsonFrontend(void) {}
virtual void help() {}
virtual void execute(std::istream *&f, std::string &filename,
Design *design)
{
// log_header(design, "Executing JSON frontend.\n");
JsonNode root(*f);
if (root.type != 'D')
log_error("JSON root node is not a dictionary.\n");
if (root.data_dict.count("modules") != 0) {
JsonNode *modules = root.data_dict.at("modules");
if (modules->type != 'D')
log_error("JSON modules node is not a dictionary.\n");
for (auto &it : modules->data_dict)
json_import(design, it.first, it.second);
}
}
}; // JsonFrontend;
}; // End Namespace JsonParser
void parse_json_file(std::istream *&f, std::string &filename, Design *design)
{
auto *parser = new JsonParser::JsonFrontend();
parser->execute(f, filename, design);
}
|