-
Notifications
You must be signed in to change notification settings - Fork 0
/
edif.c
664 lines (639 loc) · 16.7 KB
/
edif.c
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
// Edif parser
// Copyright (C) 2008 Joseph H. Allen
// See file COPYING for license.
#include <iostream>
#include <fstream>
#include <string>
#include <ctype.h>
#include <stdlib.h>
using namespace std;
extern string legalize_string(string s);
#include "hash.h"
#include "dlist.h"
#include "lisp.h"
#include "net.h"
#include "edif.h"
// Recursive descent parser
// Convert dumb lisp parse-tree into real structured edif parse-tree
Name parsename(Node *n)
{
Name name;
if(n->type==Ident_id)
{ /* Simple name */
name.name=lower(n->ident()->s);
name.array=0;
name.fullname="";
name.hasfull=0;
}
else if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="array" &&
n->list()->item->next->next->type==Num_id)
{ /* Array */
name=parsename(n->list()->item->next);
name.array=n->list()->item->next->next->num()->num;
}
else if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="rename" &&
n->list()->item->next->type==Ident_id && n->list()->item->next->next->type==Str_id)
{ /* Renamed name */
name.name=lower(n->list()->item->next->ident()->s);
name.array=0;
name.fullname=n->list()->item->next->next->str()->s;
name.hasfull=1;
}
else
{
cerr << n->file_name << " " << n->line << ": Error: unknown name format\n";
exit(-1);
}
return name;
}
string parserefname(Node *n)
{
if(n->type==Ident_id)
{ /* Simple name */
return lower(n->ident()->s);
}
else
{
cerr << n->file_name << " " << n->line << ": Error: bad ref name\n";
exit(-1);
}
}
Port *parseport(View *v,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="port")
{
Port *port=new Port();
port->mom=v;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing port name\n";
exit(-1);
}
port->name=parsename(n->list()->item->next);
if(n->list()->item->next->next && n->list()->item->next->next->type==List_id &&
n->list()->item->next->next->list()->item->type==Ident_id &&
n->list()->item->next->next->list()->item->ident()->s=="direction" &&
n->list()->item->next->next->list()->item->next->type==Ident_id)
{
if(n->list()->item->next->next->list()->item->next->ident()->s=="INPUT")
port->direction=0;
else if(n->list()->item->next->next->list()->item->next->ident()->s=="OUTPUT")
port->direction=1;
else if(n->list()->item->next->next->list()->item->next->ident()->s=="INOUT")
port->direction=2;
else
{
cerr << n->file_name << " " << n->line << ": Error: unknown direction on port\n";
exit(-1);
}
// port->misc_after=n->list()->item->next->next->next;
return port;
}
else
{
cerr << n->file_name << " " << n->line << ": Error: bad or missing direction on port\n";
exit(-1);
}
}
else
return 0;
}
void parseportname(Portref *i,Node *n)
{
if(n->type==Ident_id)
{
i->member = -1;
i->portRef=lower(n->ident()->s);
}
else if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="member")
{
if(!n->list()->item->next || n->list()->item->next->type!=Ident_id)
{
cerr << n->file_name << " " << n->line << ": Error: bad or missing portref name\n";
exit(-1);
}
i->portRef=lower(n->list()->item->next->ident()->s);
if(!n->list()->item->next->next || n->list()->item->next->next->type!=Num_id)
{
cerr << n->file_name << " " << n->line << ": Error: missing number in member\n";
exit(-1);
}
i->member=n->list()->item->next->next->num()->num;
if(n->list()->item->next->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: extra junk in member\n";
exit(-1);
}
}
else
{
cerr << n->file_name << " " << n->line << ": Error: bad portRef name\n";
exit(-1);
}
}
int parseinstanceref(View *v,Portref *i,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="instanceRef")
{
if(!n->list()->item->next || n->list()->item->next->type!=Ident_id)
{
cerr << n->file_name << " " << n->line << ": Error: Missing or bad instanceRef name\n";
exit(-1);
}
i->instanceRef=lower(n->list()->item->next->ident()->s);
if(n->list()->item->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: Extra junk after instanceRef name\n";
exit(-1);
}
// Link
i->instance=v->instances.get(i->instanceRef);
if(!i)
{
cerr << n->file_name << " " << n->line << ": Error: Couldn't find instanceRef- used before defined?\n";
exit(-1);
}
return 1;
}
else
return 0;
}
Portref *parseportref(View *v,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="portRef")
{
Portref *i=new Portref();
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: Missing port name\n";
exit(-1);
}
parseportname(i,n->list()->item->next);
if(n->list()->item->next->next)
{
if(!parseinstanceref(v,i,n->list()->item->next->next) || n->list()->item->next->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: Extra junk in portRef\n";
exit(-1);
}
}
// Link
if(i->instance)
{ // Find port in instance
if(!i->instance->ref.view)
{
cerr << n->file_name << " " << n->line << ": Error: no viewref in instance?\n";
exit(-1);
}
i->port=i->instance->ref.view->ports.get(i->portRef);
}
else
{ // Find port in v
i->port=v->ports.get(i->portRef);
}
if(!i->port)
{
cerr << n->file_name << " " << n->line << ": Error: couldn't link port " << i->instanceRef << "." << i->portRef <<"\n";
exit(-1);
}
return i;
}
else
return 0;
}
int parsejoin(View *v,Net *i,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="joined")
{
for(n=n->list()->item->next;n;n=n->next)
{
Portref *r=parseportref(v,n);
if(!r)
{
cerr << n->file_name << " " << n->line << ": Error: Unknown item in joined\n";
exit(-1);
}
r->next=i->pins;
i->pins=r;
}
return 1;
}
else
return 0;
}
Net *parsenet(View *v,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="net")
{
Net *i=new Net();
i->mom=v;
i->pins=0;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: Missing net name\n";
exit(-1);
}
i->name=parsename(n->list()->item->next);
if(!n->list()->item->next->next || !parsejoin(v,i,n->list()->item->next->next))
{
cerr << n->file_name << " " << n->line << ": Error: Empty net\n";
exit(-1);
}
// i->misc_after=n->list()->item->next->next->next;
/* if(n->list()->item->next->next->next)
{
cerr << "Extra junk in net\n";
lisp_dump(n, cerr);
cerr << '\n';
} */
return i;
}
else
return 0;
}
int parselibraryref(View *v,Viewref *r,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="libraryRef")
{
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing libraryRef name\n";
exit(-1);
}
r->libraryRef=parserefname(n->list()->item->next);
if(n->list()->item->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: extra junk at end of libraryRef list\n";
exit(-1);
}
r->lib=v->mom->mom->mom->libraries.get(r->libraryRef);
if(!r->lib)
{
cerr << n->file_name << " " << n->line << ": Error: forward ref or unknown library\n";
exit(-1);
}
return 1;
}
else
return 0;
}
int parsecellref(View *v,Viewref *r,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="cellRef")
{
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: Missing cellRef name\n";
exit(-1);
}
r->cellRef=parserefname(n->list()->item->next);
if(!n->list()->item->next->next || !parselibraryref(v,r,n->list()->item->next->next))
{
/*
cerr << "Missing libraryRef\n";
n->show(cerr,0,0);
cerr << '\n';
exit(-1); - this is ok- it means cell is in current library */
r->libraryRef="";
r->lib=v->mom->mom;
if(n->list()->item->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: Extra junk at end of cellRef list\n";
exit(-1);
}
r->cell=r->lib->cells.get(r->cellRef);
if(!r)
{
cerr << n->file_name << " " << n->line << ": Error: forward ref to or missing cell\n";
exit(-1);
}
return 1;
}
if(n->list()->item->next->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: Extra junk at end of cellRef list\n";
exit(-1);
}
r->cell=r->lib->cells.get(r->cellRef);
if(!r)
{
cerr << n->file_name << " " << n->line << ": Error: forward ref to or missing cell\n";
exit(-1);
}
return 1;
}
else
return 0;
}
int parseviewref(View *v,Viewref *r,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="viewRef")
{
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing viewRef name\n";
exit(-1);
}
r->viewRef=parserefname(n->list()->item->next);
if(!n->list()->item->next->next || !parsecellref(v,r,n->list()->item->next->next))
{
cerr << n->file_name << " " << n->line << ": Error: missing cellRef in viewRef\n";
exit(-1);
}
if(n->list()->item->next->next->next)
{
cerr << n->file_name << " " << n->line << ": Error: extra junk at end of viewRef list\n";
exit(-1);
}
r->view=r->cell->views.get(r->viewRef);
if(!r->view)
{
cerr << n->file_name << " " << n->line << ": Error: forward ref to or missing view in cell\n";
exit(-1);
}
return 1;
}
else
return 0;
}
Instance *parseinstance(View *v,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="instance")
{
Instance *i=new Instance();
i->mom=v;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing instance name\n";
exit(-1);
}
i->name=parsename(n->list()->item->next);
if(!n->list()->item->next->next || !parseviewref(v,&i->ref,n->list()->item->next->next))
{
cerr << n->file_name << " " << n->line << ": Error: missing view reference in instance\n";
exit(-1);
}
// i->misc_after=n->list()->item->next->next->next;
return i;
}
else
return 0;
}
int parseinterface(View *v,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="interface")
{
Node *first, *last;
Node *next;
Port *port;
first=last=0;
for(n=n->list()->item->next;n;n=next)
{
next=n->next;
if(port=parseport(v,n))
{
port->mom=v;
v->ports.add(port->name.name,port);
}
else
{ // Add to misc
n->next=last, last=n;
if(!first) first=last;
}
}
if(first)
{
cerr << n->file_name << " " << n->line << ": Error: unexpected junk in interface\n";
exit(-1);
}
return 1;
}
else
return 0;
}
int parsecontents(View *v,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="contents")
{
Node *first, *last;
Node *next;
Net *net;
Instance *instance;
first=last=0;
for(n=n->list()->item->next;n;n=next)
{
next=n->next;
if(net=parsenet(v,n))
{
net->mom=v;
v->nets.add(net->name.name,net);
}
else if(instance=parseinstance(v,n))
{
instance->mom=v;
v->instances.add(instance->name.name,instance);
}
else
{ // Add to misc
n->next=last, last=n;
if(!first) first=last;
}
}
if(first)
{
cerr << n->file_name << " " << n->line << ": Error: unexpected junk in contents\n";
exit(-1);
}
return 1;
}
else
return 0;
}
View *parseview(Cell *cell,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="view")
{
int flg=0;
Node *first, *last;
Node *next;
View *view;
view=new View();
view->mom=cell;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing view name\n";
exit(-1);
}
view->name=parsename(n->list()->item->next);
first=last=0;
for(n=n->list()->item->next->next;n;n=next)
{
next=n->next;
if(parseinterface(view,n) || parsecontents(view,n))
{
if(!flg)
{
// view->misc_before=first;
first=last=0;
flg=1;
}
}
else
{ // Add to misc
n->next=last, last=n;
if(!first) first=last;
}
}
// view->misc_after=first;
return view;
}
return 0;
}
Cell *parsecell(Lib *lib,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
n->list()->item->ident()->s=="cell")
{
int flg=0;
Node *first, *last;
Node *next;
Cell *cell;
View *v;
cell=new Cell();
cell->mom=lib;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing cell name\n";
exit(-1);
}
cell->name=parsename(n->list()->item->next);
// cout << "Parsing " << cell->name.name << "\n";
first=last=0;
for(n=n->list()->item->next->next;n;n=next)
{
next=n->next;
if(v=parseview(cell,n))
{
if(!flg)
{
// cell->misc_before=first;
first=last=0;
flg=1;
}
// Add to hash table
cell->views.add(v->name.name,v);
}
else
{ // Add to misc
n->next=last, last=n;
if(!first) first=last;
}
}
// cell->misc_after=first;
return cell;
}
return 0;
}
Lib *parselibrary(Design *e,Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id &&
(n->list()->item->ident()->s=="library" ||
n->list()->item->ident()->s=="external"))
{
int flg=0;
Node *first, *last;
Node *next;
Lib *lib;
Cell *l;
lib = new Lib();
if(n->list()->item->ident()->s=="library") lib->lib_type = 0;
else lib->lib_type = 1;
lib->mom=e;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing library name\n";
exit(-1);
}
lib->name=parsename(n->list()->item->next);
first=last=0;
for(n=n->list()->item->next->next;n;n=next)
{
next=n->next;
if(l=parsecell(lib,n))
{
if(!flg)
{
// lib->misc_before=first;
first=last=0;
flg=1;
}
// Add to hash table
lib->cells.add(l->name.name,l);
}
else
{ // Add to misc
n->next=last, last=n;
if(!first) first=last;
}
}
// lib->misc_after=first;
return lib;
}
return 0;
}
Design *parse_edif(Node *n)
{
if(n->type==List_id && n->list()->item->type==Ident_id && n->list()->item->ident()->s=="edif")
{
int flg=0;
Node *first, *last;
Node *next;
Design *edif=new Design();
Lib *l;
if(!n->list()->item->next)
{
cerr << n->file_name << " " << n->line << ": Error: missing edif name\n";
exit(-1);
}
edif->name=parsename(n->list()->item->next);
first=last=0;
for(n=n->list()->item->next->next;n;n=next)
{
next=n->next;
if(l=parselibrary(edif,n))
{
if(!flg)
{
// edif->misc_before=first;
first=last=0;
flg=1;
}
// Add to hash table
edif->libraries.add(l->name.name,l);
}
else
{ // Add to misc
n->next=last, last=n;
if(!first) first=last;
}
}
// edif->misc_after=first;
return edif;
}
return 0;
}