forked from steveicarus/iverilog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht-dll.cc
2887 lines (2339 loc) · 76.4 KB
/
t-dll.cc
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
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2000-2022 Stephen Williams ([email protected])
* Copyright CERN 2013 / Stephen Williams ([email protected])
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "config.h"
# include <iostream>
# include <cstring>
# include <cstdio> // sprintf()
# include "compiler.h"
# include "t-dll.h"
# include "netclass.h"
# include "netqueue.h"
# include "netmisc.h"
# include "discipline.h"
# include <cstdlib>
# include "ivl_assert.h"
# include "ivl_alloc.h"
using namespace std;
struct dll_target dll_target_obj;
#if defined(__WIN32__)
inline ivl_dll_t ivl_dlopen(const char *name)
{
ivl_dll_t res = (ivl_dll_t) LoadLibrary(name);
return res;
}
inline void * ivl_dlsym(ivl_dll_t dll, const char *nm)
{
return (void*)GetProcAddress((HMODULE)dll, nm);
}
inline void ivl_dlclose(ivl_dll_t dll)
{
FreeLibrary((HMODULE)dll);
}
const char *dlerror(void)
{
static char msg[256];
unsigned long err = GetLastError();
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &msg,
sizeof(msg) - 1,
NULL
);
return msg;
}
#elif defined(HAVE_DLFCN_H)
inline ivl_dll_t ivl_dlopen(const char*name)
{ return dlopen(name,RTLD_LAZY); }
inline void* ivl_dlsym(ivl_dll_t dll, const char*nm)
{
void*sym = dlsym(dll, nm);
/* Not found? try without the leading _ */
if (sym == 0 && nm[0] == '_')
sym = dlsym(dll, nm+1);
return sym;
}
inline void ivl_dlclose(ivl_dll_t dll)
{ dlclose(dll); }
#elif defined(HAVE_DL_H)
inline ivl_dll_t ivl_dlopen(const char*name)
{ return shl_load(name, BIND_IMMEDIATE, 0); }
inline void* ivl_dlsym(ivl_dll_t dll, const char*nm)
{
void*sym;
int rc = shl_findsym(&dll, nm, TYPE_PROCEDURE, &sym);
return (rc == 0) ? sym : 0;
}
inline void ivl_dlclose(ivl_dll_t dll)
{ shl_unload(dll); }
inline const char*dlerror(void)
{ return strerror( errno ); }
#endif
ivl_scope_s::ivl_scope_s()
: func_type(IVL_VT_NO_TYPE)
{
func_signed = false;
func_width = 0;
}
/*
* The custom new operator for the ivl_nexus_s type allows us to
* allocate nexus objects in blocks. There are generally lots of them
* permanently allocated, and allocating them in blocks reduces the
* allocation overhead.
*/
template <class TYPE> void* pool_permalloc(size_t s)
{
static TYPE * pool_ptr = 0;
static int pool_remaining = 0;
static const size_t POOL_SIZE = 4096;
assert(s == sizeof(TYPE));
if (pool_remaining <= 0) {
pool_ptr = new TYPE[POOL_SIZE];
pool_remaining = POOL_SIZE;
}
TYPE*tmp = pool_ptr;
pool_ptr += 1;
pool_remaining -= 1;
return tmp;
}
void* ivl_nexus_s::operator new(size_t s)
{
return pool_permalloc<struct ivl_nexus_s>(s);
}
void ivl_nexus_s::operator delete(void*, size_t)
{
assert(0);
}
void* ivl_net_const_s::operator new(size_t s)
{
return pool_permalloc<struct ivl_net_const_s>(s);
}
void ivl_net_const_s::operator delete(void*, size_t)
{
assert(0);
}
static StringHeapLex net_const_strings;
static perm_string make_scope_name(const hname_t&name)
{
if (! name.has_numbers())
return name.peek_name();
char buf[1024];
snprintf(buf, sizeof buf, "%s", name.peek_name().str());
char*cp = buf + strlen(buf);
size_t ncp = sizeof buf - (cp-buf);
for (size_t idx = 0 ; idx < name.has_numbers() ; idx += 1) {
int len = snprintf(cp, ncp, "[%d]", name.peek_number(idx));
cp += len;
ncp -= len;
}
return lex_strings.make(buf);
}
static void drive_from_link(const Link&lnk, ivl_drive_t&drv0, ivl_drive_t&drv1)
{
drv0 = lnk.drive0();
drv1 = lnk.drive1();
}
ivl_attribute_s* dll_target::fill_in_attributes(const Attrib*net)
{
ivl_attribute_s*attr;
unsigned nattr = net->attr_cnt();
if (nattr == 0)
return 0;
attr = new struct ivl_attribute_s[nattr];
for (unsigned idx = 0 ; idx < nattr ; idx += 1) {
verinum tmp = net->attr_value(idx);
attr[idx].key = net->attr_key(idx);
if (tmp.is_string()) {
attr[idx].type = IVL_ATT_STR;
attr[idx].val.str = strings_.add(tmp.as_string().c_str());
} else if (tmp == verinum()) {
attr[idx].type = IVL_ATT_VOID;
} else {
attr[idx].type = IVL_ATT_NUM;
attr[idx].val.num = tmp.as_long();
}
}
return attr;
}
/*
* This function locates an ivl_scope_t object that matches the
* NetScope object. The search works by looking for the parent scope,
* then scanning the parent scope for the NetScope object.
*/
static ivl_scope_t find_scope_from_root(ivl_scope_t root, const NetScope*cur)
{
if (const NetScope*par = cur->parent()) {
ivl_scope_t parent = find_scope_from_root(root, par);
if (parent == 0) {
return 0;
}
map<hname_t,ivl_scope_t>::iterator idx = parent->children.find(cur->fullname());
if (idx == parent->children.end())
return 0;
else
return idx->second;
} else {
perm_string cur_name = make_scope_name(cur->fullname());
if (strcmp(root->name_, cur_name) == 0)
return root;
}
return 0;
}
ivl_scope_t dll_target::find_scope(ivl_design_s &des, const NetScope*cur)
{
assert(cur);
// If the scope is a PACKAGE, then it is a special kind of
// root scope and it in the packages array instead.
if (cur->type() == NetScope::PACKAGE) {
perm_string cur_name = cur->module_name();
for (size_t idx = 0 ; idx < des.packages.size() ; idx += 1) {
if (des.packages[idx]->name_ == cur_name)
return des.packages[idx];
}
return 0;
}
for (unsigned idx = 0; idx < des.roots.size(); idx += 1) {
assert(des.roots[idx]);
ivl_scope_t scop = find_scope_from_root(des.roots[idx], cur);
if (scop)
return scop;
}
for (size_t idx = 0; idx < des.packages.size(); idx += 1) {
assert(des.packages[idx]);
ivl_scope_t scop = find_scope_from_root(des.packages[idx], cur);
if (scop)
return scop;
}
return 0;
}
ivl_scope_t dll_target::lookup_scope_(const NetScope*cur)
{
return find_scope(des_, cur);
}
/*
* This is a convenience function to locate an ivl_signal_t object
* given the NetESignal that has the signal name.
*/
ivl_signal_t dll_target::find_signal(ivl_design_s &des, const NetNet*net)
{
ivl_scope_t scop = find_scope(des, net->scope());
assert(scop);
perm_string nname = net->name();
for (unsigned idx = 0 ; idx < scop->sigs_.size() ; idx += 1) {
if (strcmp(scop->sigs_[idx]->name_, nname) == 0)
return scop->sigs_[idx];
}
assert(0);
return 0;
}
static ivl_nexus_t nexus_sig_make(ivl_signal_t net, unsigned pin)
{
ivl_nexus_t tmp = new struct ivl_nexus_s;
tmp->ptrs_.resize(1);
tmp->ptrs_[0].pin_ = pin;
tmp->ptrs_[0].type_ = __NEXUS_PTR_SIG;
tmp->ptrs_[0].l.sig = net;
ivl_drive_t drive = IVL_DR_HiZ;
switch (ivl_signal_type(net)) {
case IVL_SIT_REG:
drive = IVL_DR_STRONG;
break;
default:
break;
}
tmp->ptrs_[0].drive0 = drive;
tmp->ptrs_[0].drive1 = drive;
return tmp;
}
static void nexus_sig_add(ivl_nexus_t nex, ivl_signal_t net, unsigned pin)
{
unsigned top = nex->ptrs_.size();
nex->ptrs_.resize(top+1);
ivl_drive_t drive = IVL_DR_HiZ;
switch (ivl_signal_type(net)) {
case IVL_SIT_REG:
drive = IVL_DR_STRONG;
break;
default:
break;
}
nex->ptrs_[top].type_= __NEXUS_PTR_SIG;
nex->ptrs_[top].drive0 = drive;
nex->ptrs_[top].drive1 = drive;
nex->ptrs_[top].pin_ = pin;
nex->ptrs_[top].l.sig= net;
}
static void nexus_bra_add(ivl_nexus_t nex, ivl_branch_t net, unsigned pin)
{
unsigned top = nex->ptrs_.size();
nex->ptrs_.resize(top+1);
nex->ptrs_[top].type_= __NEXUS_PTR_BRA;
nex->ptrs_[top].drive0 = 0;
nex->ptrs_[top].drive1 = 0;
nex->ptrs_[top].pin_ = pin;
nex->ptrs_[top].l.bra= net;
}
/*
* Add the pin of the logic object to the nexus, and return the nexus
* pointer used for the pin.
*
* NOTE: This pointer is only valid until another pin is added to the
* nexus.
*/
static ivl_nexus_ptr_t nexus_log_add(ivl_nexus_t nex,
ivl_net_logic_t net,
unsigned pin)
{
unsigned top = nex->ptrs_.size();
nex->ptrs_.resize(top+1);
nex->ptrs_[top].type_= __NEXUS_PTR_LOG;
nex->ptrs_[top].drive0 = (pin == 0)? IVL_DR_STRONG : IVL_DR_HiZ;
nex->ptrs_[top].drive1 = (pin == 0)? IVL_DR_STRONG : IVL_DR_HiZ;
nex->ptrs_[top].pin_ = pin;
nex->ptrs_[top].l.log= net;
return & (nex->ptrs_[top]);
}
static void nexus_con_add(ivl_nexus_t nex, ivl_net_const_t net, unsigned pin,
ivl_drive_t drive0, ivl_drive_t drive1)
{
unsigned top = nex->ptrs_.size();
nex->ptrs_.resize(top+1);
nex->ptrs_[top].type_= __NEXUS_PTR_CON;
nex->ptrs_[top].drive0 = drive0;
nex->ptrs_[top].drive1 = drive1;
nex->ptrs_[top].pin_ = pin;
nex->ptrs_[top].l.con= net;
}
static void nexus_lpm_add(ivl_nexus_t nex, ivl_lpm_t net, unsigned pin,
ivl_drive_t drive0, ivl_drive_t drive1)
{
unsigned top = nex->ptrs_.size();
nex->ptrs_.resize(top+1);
nex->ptrs_[top].type_= __NEXUS_PTR_LPM;
nex->ptrs_[top].drive0 = drive0;
nex->ptrs_[top].drive1 = drive1;
nex->ptrs_[top].pin_ = pin;
nex->ptrs_[top].l.lpm= net;
}
static void nexus_switch_add(ivl_nexus_t nex, ivl_switch_t net, unsigned pin)
{
unsigned top = nex->ptrs_.size();
nex->ptrs_.resize(top+1);
nex->ptrs_[top].type_= __NEXUS_PTR_SWI;
nex->ptrs_[top].drive0 = IVL_DR_HiZ;
nex->ptrs_[top].drive1 = IVL_DR_HiZ;
nex->ptrs_[top].pin_ = pin;
nex->ptrs_[top].l.swi= net;
}
void scope_add_logic(ivl_scope_t scope, ivl_net_logic_t net)
{
if (scope->nlog_ == 0) {
scope->nlog_ = 1;
scope->log_ = (ivl_net_logic_t*)malloc(sizeof(ivl_net_logic_t));
scope->log_[0] = net;
} else {
scope->nlog_ += 1;
scope->log_ = (ivl_net_logic_t*)
realloc(scope->log_, scope->nlog_*sizeof(ivl_net_logic_t));
scope->log_[scope->nlog_-1] = net;
}
}
void scope_add_event(ivl_scope_t scope, ivl_event_t net)
{
if (scope->nevent_ == 0) {
scope->nevent_ = 1;
scope->event_ = (ivl_event_t*)malloc(sizeof(ivl_event_t));
scope->event_[0] = net;
} else {
scope->nevent_ += 1;
scope->event_ = (ivl_event_t*)
realloc(scope->event_, scope->nevent_*sizeof(ivl_event_t));
scope->event_[scope->nevent_-1] = net;
}
}
static void scope_add_lpm(ivl_scope_t scope, ivl_lpm_t net)
{
if (scope->nlpm_ == 0) {
assert(scope->lpm_ == 0);
scope->nlpm_ = 1;
scope->lpm_ = (ivl_lpm_t*)malloc(sizeof(ivl_lpm_t));
scope->lpm_[0] = net;
} else {
assert(scope->lpm_);
scope->nlpm_ += 1;
scope->lpm_ = (ivl_lpm_t*)
realloc(scope->lpm_,
scope->nlpm_*sizeof(ivl_lpm_t));
scope->lpm_[scope->nlpm_-1] = net;
}
}
static void scope_add_switch(ivl_scope_t scope, ivl_switch_t net)
{
scope->switches.push_back(net);
}
ivl_parameter_t dll_target::scope_find_param(ivl_scope_t scope,
const char*name)
{
unsigned idx = 0;
while (idx < scope->param.size()) {
if (strcmp(name, scope->param[idx].basename) == 0)
return &scope->param[idx];
idx += 1;
}
return 0;
}
/*
* This method scans the parameters of the scope, and makes
* ivl_parameter_t objects. This involves saving the name and scanning
* the expression value.
*/
void dll_target::make_scope_parameters(ivl_scope_t scop, const NetScope*net)
{
if (net->parameters.empty()) {
scop->param.clear();
return;
}
scop->param.resize(net->parameters.size());
unsigned idx = 0;
typedef map<perm_string,NetScope::param_expr_t>::const_iterator pit_t;
for (pit_t cur_pit = net->parameters.begin()
; cur_pit != net->parameters.end() ; ++ cur_pit ) {
assert(idx < scop->param.size());
ivl_parameter_t cur_par = &scop->param[idx];
cur_par->basename = cur_pit->first;
cur_par->local = cur_pit->second.local_flag ||
!cur_pit->second.overridable;
cur_par->is_type = cur_pit->second.type_flag;
if (cur_pit->second.ivl_type == 0) {
cerr << "?:?: internal error: "
<< "No type for parameter " << cur_pit->first
<< " in scope " << net->fullname() << "?" << endl;
}
assert(cur_pit->second.ivl_type);
cur_par->signed_flag = cur_pit->second.ivl_type->get_signed();
cur_par->scope = scop;
FILE_NAME(cur_par, &(cur_pit->second));
// Type parameters don't have a range or expression
if (!cur_pit->second.type_flag) {
calculate_param_range(cur_pit->second,
cur_pit->second.ivl_type,
cur_par->msb, cur_par->lsb,
cur_pit->second.val->expr_width());
NetExpr*etmp = cur_pit->second.val;
if (etmp == 0) {
cerr << "?:?: internal error: What is the parameter "
<< "expression for " << cur_pit->first
<< " in " << net->fullname() << "?" << endl;
}
assert(etmp);
make_scope_param_expr(cur_par, etmp);
}
idx += 1;
}
}
void dll_target::make_scope_param_expr(ivl_parameter_t cur_par, NetExpr*etmp)
{
if (const NetEConst*e = dynamic_cast<const NetEConst*>(etmp)) {
expr_const(e);
assert(expr_);
switch (expr_->type_) {
case IVL_EX_STRING:
expr_->u_.string_.parameter = cur_par;
break;
case IVL_EX_NUMBER:
expr_->u_.number_.parameter = cur_par;
break;
default:
assert(0);
}
} else if (const NetECReal*er = dynamic_cast<const NetECReal*>(etmp)) {
expr_creal(er);
assert(expr_);
assert(expr_->type_ == IVL_EX_REALNUM);
expr_->u_.real_.parameter = cur_par;
}
if (expr_ == 0) {
cerr << etmp->get_fileline() << ": internal error: "
<< "Parameter expression not reduced to constant? "
<< *etmp << endl;
}
ivl_assert(*etmp, expr_);
cur_par->value = expr_;
expr_ = 0;
}
static void fill_in_scope_function(ivl_scope_t scope, const NetScope*net)
{
scope->type_ = IVL_SCT_FUNCTION;
const NetFuncDef*def = net->func_def();
assert(def);
if (def->is_void()) {
// Special case: If there is no return signal, this is
// apparently a VOID function.
scope->func_type = IVL_VT_VOID;
scope->func_signed = 0;
scope->func_width = 0;
} else {
const NetNet*return_sig = def->return_sig();
scope->func_type = return_sig->data_type();
scope->func_signed = return_sig->get_signed();
scope->func_width = return_sig->vector_width();
}
scope->tname_ = def->scope()->basename();
}
void dll_target::add_root(const NetScope *s)
{
ivl_scope_t root_ = new struct ivl_scope_s;
perm_string name = s->basename();
root_->name_ = name;
FILE_NAME(root_, s);
root_->parent = 0;
root_->nlog_ = 0;
root_->log_ = 0;
root_->nevent_ = 0;
root_->event_ = 0;
root_->nlpm_ = 0;
root_->lpm_ = 0;
root_->def = 0;
make_scope_parameters(root_, s);
root_->tname_ = root_->name_;
root_->time_precision = s->time_precision();
root_->time_units = s->time_unit();
root_->nattr = s->attr_cnt();
root_->attr = fill_in_attributes(s);
root_->is_auto = 0;
root_->is_cell = s->is_cell();
switch (s->type()) {
case NetScope::PACKAGE:
root_->type_ = IVL_SCT_PACKAGE;
break;
case NetScope::MODULE:
root_->type_ = IVL_SCT_MODULE;
break;
case NetScope::CLASS:
root_->type_ = IVL_SCT_CLASS;
break;
default:
assert(0);
}
switch (s->type()) {
case NetScope::MODULE:
root_->ports = s->module_port_nets();
if (root_->ports > 0) {
root_->u_.net = new NetNet*[root_->ports];
for (unsigned idx = 0; idx < root_->ports; idx += 1) {
root_->u_.net[idx] = s->module_port_net(idx);
}
}
root_->module_ports_info = s->module_port_info();
des_.roots.push_back(root_);
break;
case NetScope::PACKAGE:
root_->ports = 0;
des_.packages.push_back(root_);
break;
default:
assert(0);
break;
}
}
bool dll_target::start_design(const Design*des)
{
const char*dll_path_ = des->get_flag("DLL");
dll_ = ivl_dlopen(dll_path_);
if ((dll_ == 0) && (dll_path_[0] != '/')) {
size_t len = strlen(basedir) + 1 + strlen(dll_path_) + 1;
char*tmp = new char[len];
snprintf(tmp, len, "%s/%s", basedir, dll_path_);
dll_ = ivl_dlopen(tmp);
delete[]tmp;
}
if (dll_ == 0) {
cerr << "error: " << dll_path_ << " failed to load." << endl;
cerr << dll_path_ << ": " << dlerror() << endl;
return false;
}
stmt_cur_ = 0;
// Initialize the design object.
des_.self = des;
des_.time_precision = des->get_precision();
des_.disciplines.resize(disciplines.size());
unsigned idx = 0;
for (map<perm_string,ivl_discipline_t>::const_iterator cur = disciplines.begin()
; cur != disciplines.end() ; ++ cur ) {
des_.disciplines[idx] = cur->second;
idx += 1;
}
assert(idx == des_.disciplines.size());
list<NetScope *> scope_list;
scope_list = des->find_package_scopes();
for (list<NetScope*>::const_iterator cur = scope_list.begin()
; cur != scope_list.end(); ++ cur ) {
add_root(*cur);
}
scope_list = des->find_root_scopes();
for (list<NetScope*>::const_iterator cur = scope_list.begin()
; cur != scope_list.end(); ++ cur ) {
add_root(*cur);
}
target_ = (target_design_f)ivl_dlsym(dll_, LU "target_design" TU);
if (target_ == 0) {
cerr << dll_path_ << ": error: target_design entry "
"point is missing." << endl;
return false;
}
return true;
}
/*
* Here ivl is telling us that the design is scanned completely, and
* here is where we call the API to process the constructed design.
*/
int dll_target::end_design(const Design*)
{
int rc;
if (errors == 0) {
if (verbose_flag) {
cout << " ... invoking target_design" << endl;
}
rc = (target_)(&des_);
} else {
if (verbose_flag) {
cout << " ... skipping target_design due to errors." << endl;
}
rc = errors;
}
ivl_dlclose(dll_);
return rc;
}
void dll_target::switch_attributes(struct ivl_switch_s *obj,
const NetNode*net)
{
obj->nattr = net->attr_cnt();
obj->attr = fill_in_attributes(net);
}
void dll_target::logic_attributes(struct ivl_net_logic_s *obj,
const NetNode*net)
{
obj->nattr = net->attr_cnt();
obj->attr = fill_in_attributes(net);
}
void dll_target::make_delays_(ivl_expr_t*delay, const NetObj*net)
{
delay[0] = 0;
delay[1] = 0;
delay[2] = 0;
/* Translate delay expressions to ivl_target form. Try to
preserve pointer equality, not as a rule but to save on
expression trees. */
if (net->rise_time()) {
expr_ = 0;
net->rise_time()->expr_scan(this);
delay[0] = expr_;
expr_ = 0;
}
if (net->fall_time()) {
if (net->fall_time() == net->rise_time()) {
delay[1] = delay[0];
} else {
expr_ = 0;
net->fall_time()->expr_scan(this);
delay[1] = expr_;
expr_ = 0;
}
}
if (net->decay_time()) {
if (net->decay_time() == net->rise_time()) {
delay[2] = delay[0];
} else {
expr_ = 0;
net->decay_time()->expr_scan(this);
delay[2] = expr_;
expr_ = 0;
}
}
}
void dll_target::make_logic_delays_(struct ivl_net_logic_s*obj,
const NetObj*net)
{
make_delays_(obj->delay, net);
}
void dll_target::make_switch_delays_(struct ivl_switch_s*obj,
const NetObj*net)
{
make_delays_(obj->delay, net);
}
void dll_target::make_lpm_delays_(struct ivl_lpm_s*obj,
const NetObj*net)
{
make_delays_(obj->delay, net);
}
void dll_target::make_const_delays_(struct ivl_net_const_s*obj,
const NetObj*net)
{
make_delays_(obj->delay, net);
}
bool dll_target::branch(const NetBranch*net)
{
struct ivl_branch_s*obj = net->target_obj();
ivl_assert(*net, net->pin_count() == 2);
assert(net->pin(0).nexus()->t_cookie());
obj->pins[0] = net->pin(0).nexus()->t_cookie();
nexus_bra_add(obj->pins[0], obj, 0);
assert(net->pin(1).nexus()->t_cookie());
obj->pins[1] = net->pin(1).nexus()->t_cookie();
nexus_bra_add(obj->pins[1], obj, 1);
obj->island = net->get_island();
return true;
}
/*
* Add a bufz object to the scope that contains it.
*
* Note that in the ivl_target API a BUFZ device is a special kind of
* ivl_net_logic_t device, so create an ivl_net_logic_t cookie to
* handle it.
*/
bool dll_target::bufz(const NetBUFZ*net)
{
struct ivl_net_logic_s *obj = new struct ivl_net_logic_s;
assert(net->pin_count() == 2);
obj->type_ = net->transparent()? IVL_LO_BUFT : IVL_LO_BUFZ;
obj->width_= net->width();
obj->is_cassign = 0;
obj->is_port_buffer = net->port_info_index() >= 0;
obj->npins_= 2;
obj->pins_ = new ivl_nexus_t[2];
FILE_NAME(obj, net);
/* Get the ivl_nexus_t objects connected to the two pins.
(We know a priori that the ivl_nexus_t objects have been
allocated, because the signals have been scanned before
me. This saves me the trouble of allocating them.) */
assert(net->pin(0).nexus()->t_cookie());
obj->pins_[0] = net->pin(0).nexus()->t_cookie();
ivl_nexus_ptr_t out_ptr = nexus_log_add(obj->pins_[0], obj, 0);
out_ptr->drive0 = net->pin(0).drive0();
out_ptr->drive1 = net->pin(0).drive1();
assert(net->pin(1).nexus()->t_cookie());
obj->pins_[1] = net->pin(1).nexus()->t_cookie();
nexus_log_add(obj->pins_[1], obj, 1);
/* Attach the logic device to the scope that contains it. */
assert(net->scope());
ivl_scope_t scop = find_scope(des_, net->scope());
assert(scop);
obj->scope_ = scop;
obj->name_ = net->name();
logic_attributes(obj, net);
make_logic_delays_(obj, net);
scope_add_logic(scop, obj);
// Add bufz to the corresponding port_info entry,
// if it is an input / output buffer
// This is needed for the SDF interconnect feature
// to access the buffers directly from the port_info
if (obj->is_port_buffer) {
scop->module_ports_info[net->port_info_index()].buffer = obj;
}
return true;
}
bool dll_target::class_type(const NetScope*in_scope, netclass_t*net)
{
ivl_scope_t use_scope = find_scope(des_, in_scope);
use_scope->classes.push_back(net);
return true;
}
bool dll_target::enumeration(const NetScope*in_scope, netenum_t*net)
{
ivl_scope_t use_scope = find_scope(des_, in_scope);
use_scope->enumerations_.push_back(net);
return true;
}
void dll_target::event(const NetEvent*net)
{
struct ivl_event_s *obj = new struct ivl_event_s;
FILE_NAME(obj, net);
ivl_scope_t scop = find_scope(des_, net->scope());
obj->name = net->name();
obj->scope = scop;
scope_add_event(scop, obj);
obj->nany = 0;
obj->nneg = 0;
obj->npos = 0;
obj->nedg = 0;
if (net->nprobe() >= 1) {
for (unsigned idx = 0 ; idx < net->nprobe() ; idx += 1) {
const NetEvProbe*pr = net->probe(idx);
switch (pr->edge()) {
case NetEvProbe::ANYEDGE:
obj->nany += pr->pin_count();
break;
case NetEvProbe::NEGEDGE:
obj->nneg += pr->pin_count();
break;
case NetEvProbe::POSEDGE:
obj->npos += pr->pin_count();
break;
case NetEvProbe::EDGE:
obj->nedg += pr->pin_count();
break;
}
}
unsigned npins = obj->nany + obj->nneg + obj->npos + obj->nedg;
obj->pins = (ivl_nexus_t*)calloc(npins, sizeof(ivl_nexus_t));
} else {
obj->pins = 0;
}
}
void dll_target::logic(const NetLogic*net)
{
struct ivl_net_logic_s *obj = new struct ivl_net_logic_s;
obj->width_ = net->width();
obj->is_port_buffer = 0;
FILE_NAME(obj, net);
switch (net->type()) {
case NetLogic::AND:
obj->type_ = IVL_LO_AND;
break;
case NetLogic::BUF:
obj->type_ = IVL_LO_BUF;
break;
case NetLogic::BUFIF0:
obj->type_ = IVL_LO_BUFIF0;
break;
case NetLogic::BUFIF1:
obj->type_ = IVL_LO_BUFIF1;
break;
case NetLogic::CMOS:
obj->type_ = IVL_LO_CMOS;
break;
case NetLogic::EQUIV:
obj->type_ = IVL_LO_EQUIV;
break;
case NetLogic::IMPL:
obj->type_ = IVL_LO_IMPL;
break;
case NetLogic::NAND:
obj->type_ = IVL_LO_NAND;
break;
case NetLogic::NMOS: