-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.c
More file actions
1409 lines (1341 loc) · 43.8 KB
/
value.c
File metadata and controls
1409 lines (1341 loc) · 43.8 KB
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
/* value.c: value representation and expression evaluation
*
* COPYRIGHT 2010. California Institute of Technology
*
* This file is part of chpsim.
*
* Chpsim is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version, and under the terms of the
* following disclaimer of liability:
*
* The California Institute of Technology shall allow RECIPIENT to use and
* distribute this software subject to the terms of the included license
* agreement with the understanding that:
*
* THIS SOFTWARE AND ANY RELATED MATERIALS WERE CREATED BY THE CALIFORNIA
* INSTITUTE OF TECHNOLOGY (CALTECH). THE SOFTWARE IS PROVIDED "AS-IS" TO THE
* RECIPIENT WITHOUT WARRANTY OF ANY KIND, INCLUDING ANY WARRANTIES OF
* PERFORMANCE OR MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE OR PURPOSE
* (AS SET FORTH IN UNITED STATES UCC Sect. 2312-2313) OR FOR ANY PURPOSE
* WHATSOEVER, FOR THE SOFTWARE AND RELATED MATERIALS, HOWEVER USED.
*
* IN NO EVENT SHALL CALTECH BE LIABLE FOR ANY DAMAGES AND/OR COSTS,
* INCLUDING, BUT NOT LIMITED TO, INCIDENTAL OR CONSEQUENTIAL DAMAGES OF ANY
* KIND, INCLUDING ECONOMIC DAMAGE OR INJURY TO PROPERTY AND LOST PROFITS,
* REGARDLESS OF WHETHER CALTECH BE ADVISED, HAVE REASON TO KNOW, OR, IN FACT,
* SHALL KNOW OF THE POSSIBILITY.
*
* RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
* SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY CALTECH FOR
* ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS OF RECIPIENT IN THE
* USE OF THE SOFTWARE.
*
* In addition, RECIPIENT also agrees that Caltech is under no obligation to
* provide technical support for the Software.
*
* Finally, Caltech places no restrictions on RECIPIENT's use, preparation of
* Derivative Works, public display or redistribution of the Software other
* than those specified in the GNU General Public License and the requirement
* that all copies of the Software released be marked with the language
* provided in this notice.
*
* You should have received a copy of the GNU General Public License
* along with chpsim. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Marcel van der Goot and Chris Moore
*/
#include <standard.h>
#include "parse_obj.h"
#include "value.h"
#include "ifrchk.h"
#include "exec.h"
#include "interact.h"
#include "routines.h"
#include "expr.h"
#include "types.h"
/*extern*/ int app_eval = -1;
/*extern*/ int app_reval = -1;
/*extern*/ int app_range = -1;
/*extern*/ int app_assign = -1;
/*extern*/ int app_conn = -1;
/********** expression evaluation ********************************************/
extern void push_value(value_tp *v, exec_info *f)
/* push *v on stack (direct copy: top = *v) */
{ eval_stack *w;
if (f->fl)
{ w = f->fl; f->fl = w->next; }
else
{ NEW(w); }
w->v = *v;
w->next = f->stack; f->stack = w;
}
extern void pop_value(value_tp *v, exec_info *f)
/* store top of stack in *v (direct copy: *v = top) and pop stack */
{ eval_stack *w;
assert(f->stack);
w = f->stack; f->stack = w->next;
*v = w->v;
w->next = f->fl; f->fl = w;
}
extern void push_repval(value_tp *v, ctrl_state *cs, exec_info *f)
/* push *v on repval stack (direct copy: top = *v) */
{ eval_stack *w;
if (f->fl)
{ w = f->fl; f->fl = w->next; }
else
{ NEW(w); }
w->v = *v;
w->next = cs->rep_vals; cs->rep_vals = w;
}
extern void pop_repval(value_tp *v, ctrl_state *cs, exec_info *f)
/* store top of stack in *v (direct copy: *v = top) and pop stack */
{ eval_stack *w;
assert(cs->rep_vals);
w = cs->rep_vals; cs->rep_vals = w->next;
*v = w->v;
w->next = f->fl; f->fl = w;
}
static void no_eval(expr *x, exec_info *f)
/* called when x has no eval function */
{ error("Internal error: "
"Object class %s has no eval method", x->class->nm);
}
extern void eval_expr(void *obj, exec_info *f)
/* Evaluate expr *obj */
{ expr *x = obj;
value_tp *xv, xval;
exec_flags flags;
if (IS_SET(x->flags, EXPR_lvalue))
{ flags = f->flags;
RESET_FLAG(f->flags, EVAL_assign);
xv = reval_expr(x, f);
ASSIGN_FLAG(f->flags, flags, EVAL_assign);
if (IS_SET(x->flags, EXPR_ifrchk))
{ f->err_obj = x;
if (IS_SET(x->flags, EXPR_port) && !IS_SET(f->flags, EVAL_probe))
{ strict_check_write(xv, f); }
else
{ strict_check_read(xv, f); }
}
alias_value_tp(&xval, xv, f);
push_value(&xval, f);
}
else
{ APP_OBJ_VFZ(app_eval, obj, f, no_eval); }
}
static void *no_reval(expr *x, exec_info *f)
/* called when x has no eval function */
{ error("Internal error: "
"Object class %s has no reval method", x->class->nm);
return 0;
}
extern void *reval_expr(void *obj, exec_info *f)
/* Evaluate expr *obj */
{ APP_OBJ_PFZ(app_reval, obj, f, no_reval); }
/* var_str_func_tp: use as first arg for %v */
extern int vstr_mpz(var_string *s, int pos, void *z)
/* print mpz_t z to s[pos...] */
{ var_str_ensure(s, pos + mpz_sizeinbase(z, 10) + 2);
mpz_get_str(s->s + pos, 10, z);
return 0;
}
/* var_str_func_tp: use as first arg for %v */
extern int vstr_val(var_string *s, int pos, void *val)
/* print value_tp *val to s[pos...] */
{ print_info f;
print_info_init(&f);
f.s = s; f.pos = pos;
print_value_tp(val, &f);
VAR_STR_X(f.s, f.pos) = 0;
return 0;
}
extern void print_value_tp(value_tp *v, print_info *f)
/* Print v to f */
{ char bo = '{', bc = '}';
wire_value *w;
int i, n;
switch (v->rep)
{ case REP_none: print_char('?', f);
break;
case REP_bool: if (v->v.i == 1) print_string("true", f);
else if (v->v.i == 0) print_string("false", f);
else assert(!"Illegal bool value");
break;
case REP_int: f->pos += var_str_printf(f->s, f->pos, "%ld", v->v.i);
break;
case REP_z:
var_str_ensure(f->s, f->pos + mpz_sizeinbase(v->v.z->z, 10)+1);
mpz_get_str(f->s->s + f->pos, 10, v->v.z->z);
f->pos += strlen(f->s->s + f->pos);
break;
case REP_symbol:
print_char('`', f);
print_string(v->v.s, f);
break;
case REP_array: bo = '['; bc = ']';
/* fall-through */
case REP_record:
print_char(bo, f);
i = 0; n = v->v.l->size;
while (n)
{ print_value_tp(&v->v.l->vl[i], f);
i++; n--;
if (n)
{ print_char(',', f); print_char(' ', f); }
}
print_char(bc, f);
break;
case REP_union:
print_value_tp(&v->v.u->v, f);
break;
case REP_port:
if (!v->v.p->p)
{ print_string("disconnected port", f); }
else if (!is_visible(v->v.p->p->wprobe.wps) && v->v.p->p->dec)
{ print_string("port decomposed through field ", f);
print_string(v->v.p->p->dec->id, f);
}
else
{ print_string("port --> ", f);
if (!print_port_connect(v->v.p->p, f))
{ if (!IS_SET(v->v.p->wprobe.flags, WIRE_value))
{ print_string(", # = false", f); }
else if (v->v.p->v.rep)
{ print_string(", # = true, data = ", f);
print_value_tp(&v->v.p->v, f);
}
else
{ print_string(", # = true", f); }
}
}
break;
case REP_process:
print_string("process ", f);
print_string(v->v.ps->nm, f);
break;
case REP_wwire: case REP_rwire:
w = v->v.w;
while (IS_SET(w->flags, WIRE_forward)) w = w->u.w;
f->pos += var_str_printf(f->s, f->pos, "(%c)",
IS_SET(w->flags, WIRE_undef)?
'X' : '0' + IS_SET(w->flags, WIRE_value));
break;
case REP_cnt:
f->pos += var_str_printf(f->s, f->pos, "%d", v->v.c->cnt);
break;
case REP_type:
print_string("<type>", f);
break;
default: assert(!"Illegal representation");
break;
}
VAR_STR_X(f->s, f->pos) = 0;
}
/* var_str_func_tp: use as first arg for %v */
extern int print_string_value(var_string *s, int pos, value_tp *val)
/* Pre: val is an array of {0..255}.
Print val as a (0-terminated) string to s[pos...].
Return is nr chars printed.
*/
{ int4 i, n;
int r = 0, c;
value_list *l;
value_tp *ev;
assert(val->rep == REP_array);
l = val->v.l;
n = l->size;
for (i = 0; i < n; i++)
{ ev = &l->vl[i];
if (!ev->rep)
{ VAR_STR_X(s, pos) = '?'; pos++; r++; }
else
{ assert(ev->rep == REP_int || ev->rep == REP_z);
if (ev->rep == REP_int)
{ c = ev->v.i; }
else
{ c = mpz_get_si(ev->v.z->z); }
if (!c)
{ break; }
else
{ VAR_STR_X(s, pos) = c; pos++; r++; }
}
}
VAR_STR_X(s, pos) = 0;
return r;
}
static int _print_wire_value
(value_tp *v, type *tp, wire_value *w, exec_info *f)
/* Also used for port values pv, where w is &pv->wprobe */
{ llist l, rl;
long i, res;
array_type *atps;
value_tp idxv;
record_type *rtps;
record_field *rf;
wired_type *wtps;
wire_decl *wd;
var_decl *d;
int pos = VAR_STR_LEN(&f->scratch);
process_state *meta_ps, *ps;
meta_parameter *mp;
type_value *tpv;
if (tp && tp->kind == TP_generic)
{ mp = (meta_parameter*)tp->tps;
assert(mp->class == CLASS_meta_parameter);
meta_ps = f->meta_ps;
tpv = meta_ps->meta[mp->meta_idx].v.tp;
f->meta_ps = tpv->meta_ps;
res = _print_wire_value(v, &tpv->tp, w, f);
f->meta_ps = meta_ps;
return res;
}
switch(v->rep)
{ case REP_record:
if (tp->kind == TP_wire)
{ wtps = (wired_type*)tp->tps;
l = wtps->li;
for (i = 0; i < v->v.l->size; i++)
{ wd = (wire_decl*)llist_head(&l);
var_str_printf(&f->scratch, pos, ".%s", wd->id);
if (_print_wire_value(&v->v.l->vl[i], &wd->tps->tp, w, f))
{ return 1; }
l = llist_alias_tail(&l);
if (llist_is_empty(&l)) l = wtps->lo;
}
}
else
{ l = tp->elem.l;
rtps = (record_type*)tp->tps;
rl = rtps->l;
for (i = 0; i < v->v.l->size; i++)
{ rf = (record_field*)llist_head(&rl);
var_str_printf(&f->scratch, pos, ".%s", rf->id);
if (_print_wire_value(&v->v.l->vl[i], llist_head(&l), w, f))
{ return 1; }
l = llist_alias_tail(&l);
rl = llist_alias_tail(&rl);
}
}
return 0;
case REP_union:
return _print_wire_value(&v->v.u->v, &v->v.u->f->tp, w, f);
case REP_array:
if (tp->kind == TP_array)
{ atps = (array_type*)tp->tps;
eval_expr(atps->l, f);
pop_value(&idxv, f);
}
else
{ idxv.rep = REP_int; idxv.v.i = 0; }
for (i = 0; i < v->v.l->size; i++)
{ var_str_printf(&f->scratch, pos, "[%v]", vstr_val, &idxv);
if (_print_wire_value(&v->v.l->vl[i], tp->elem.tp, w, f))
{ clear_value_tp(&idxv, f);
return 1;
}
int_inc(&idxv, f);
}
clear_value_tp(&idxv, f);
return 0;
case REP_port:
if (w == &v->v.p->wprobe) return 1;
else if (w == v->v.p->wpp) return 1;
else if (v->v.p->p && !IS_SET(v->v.p->wprobe.flags, PORT_deadwu))
{ if (w == &v->v.p->p->wprobe) return 1;
if (!v->v.p->p->dec) return 0;
var_str_printf(&f->scratch, pos, ".%s", v->v.p->p->dec->id);
meta_ps = f->meta_ps;
f->meta_ps = ps = v->v.p->p->wprobe.wps;
d = llist_idx(&ps->p->pl, 0);
if (ps->var[d->var_idx].v.p == v->v.p->p)
{ d = llist_idx(&ps->p->pl, 1); }
res = _print_wire_value(&ps->var[d->var_idx], &d->tp, w, f);
f->meta_ps = meta_ps;
return res;
}
else if (v->v.p->nv)
{ assert(v->v.p->nv != v);
if (v->v.p->dec)
{ var_str_printf(&f->scratch, pos, ".%s", v->v.p->dec->id);
meta_ps = f->meta_ps;
f->meta_ps = ps = v->v.p->wprobe.wps;
res = _print_wire_value(v->v.p->nv,&v->v.p->dec->tps->tp,w,f);
f->meta_ps = meta_ps;
return res;
}
else
{ return _print_wire_value(v->v.p->nv, tp, w, f); }
}
return 0;
case REP_wwire: case REP_rwire:
return w == v->v.w;
default:
return 0;
}
}
extern void print_wire_exec(wire_value *w, exec_info *f)
/* Print a name for w in the reference frame of f->meta_ps to f->scratch */
{ llist l;
var_decl *d;
value_tp *var = f->meta_ps->var;
wire_expr *e;
while (IS_SET(w->flags, WIRE_virtual))
{ assert(llist_size(&w->u.dep) == 1);
e = llist_head(&w->u.dep);
assert(IS_SET(e->flags, WIRE_x));
w = e->u.hold;
}
l = f->meta_ps->p->pl;
while (!llist_is_empty(&l))
{ d = llist_head(&l);
l = llist_alias_tail(&l);
var_str_printf(&f->scratch, 0, "%s", d->id);
if (_print_wire_value(&var[d->var_idx], &d->tp, w, f)) return;
}
l = f->meta_ps->b->dl;
while (!llist_is_empty(&l))
{ d = llist_head(&l);
l = llist_alias_tail(&l);
if (d->class != CLASS_var_decl) continue;
var_str_printf(&f->scratch, 0, "%s", d->id);
if (_print_wire_value(&var[d->var_idx], &d->tp, w, f)) return;
}
assert(!"Wire not found");
}
extern void print_wire_value(wire_value *w, process_state *ps, print_info *f)
/* Print a name for w in the reference frame of ps to f */
{ exec_info g;
exec_info_init_eval(&g, ps);
print_wire_exec(w, &g);
print_string(g.scratch.s, f);
exec_info_term(&g);
}
/* var_str_func2_tp: use as first arg for %V */
extern int vstr_wire(var_string *s, int pos, void *w, void *ps)
/* Print a name for w in the reference frame of ps to s[pos...] */
{ exec_info g;
exec_info_init_eval(&g, ps);
print_wire_exec(w, &g);
var_str_printf(s, pos, "%s", g.scratch.s);
exec_info_term(&g);
return 0;
}
/* var_str_func2_tp: use as first arg for %V */
extern int vstr_wire_context(var_string *s, int pos, void *w, void *ps)
/* Print both w and the name of the reference frame of ps to s[pos...]
* This also handles the case when the real reference frame is hidden
*/
{ exec_info g;
var_decl *d;
port_value *pv;
const char *ext;
exec_info_init_eval(&g, ps);
print_wire_exec(w, &g);
if (is_visible(ps))
{ var_str_printf(s, pos, "%s of process %s", g.scratch.s, g.meta_ps->nm); }
else
{ ext = strchr(g.scratch.s, '.'); /* "Remove" local port name */
if (!ext) ext = ""; /* Sometimes the local port name is all there is */
d = llist_idx(&g.meta_ps->p->pl, 0);
if (g.meta_ps->var[d->var_idx].rep != REP_port)
{ d = llist_idx(&g.meta_ps->p->pl, 1); }
assert(g.meta_ps->var[d->var_idx].rep == REP_port);
pv = g.meta_ps->var[d->var_idx].v.p; /* The "real" port */
assert(pv->dec);
if (w == &pv->wprobe)
{ var_str_printf(s, pos, "%v%s of process %s", vstr_port, pv->p,
ext, pv->p->wprobe.wps->nm);
}
else
{ var_str_printf(s, pos, "%v.%s%s of process %s", vstr_port, pv->p,
pv->dec->id, ext, pv->p->wprobe.wps->nm);
}
}
exec_info_term(&g);
return 0;
}
/* var_str_func2_tp: use as first arg for %V */
extern int vstr_wire_context_short(var_string *s, int pos, void *w, void *ps)
/* Same as above, but use shorthand ':' notation */
{ exec_info g;
var_decl *d;
port_value *pv;
char *ext;
exec_info_init_eval(&g, ps);
print_wire_exec(w, &g);
if (is_visible(ps))
{ var_str_printf(s, pos, "%s:%s", g.meta_ps->nm, g.scratch.s); }
else
{ ext = strchr(g.scratch.s, '.'); /* "Remove" local port name */
if (!ext) ext = ""; /* Sometimes the local port name is all there is */
d = llist_idx(&g.meta_ps->p->pl, 0);
if (g.meta_ps->var[d->var_idx].rep != REP_port)
{ d = llist_idx(&g.meta_ps->p->pl, 1); }
assert(g.meta_ps->var[d->var_idx].rep == REP_port);
pv = g.meta_ps->var[d->var_idx].v.p; /* The "real" port */
assert(pv->dec);
if (w == &pv->wprobe)
{ var_str_printf(s, pos, "%s:%v%s", pv->p->wprobe.wps->nm,
vstr_port, pv->p, ext);
}
else
{ var_str_printf(s, pos, "%s:%v.%s%s", pv->p->wprobe.wps->nm,
vstr_port, pv->p, pv->dec->id, ext);
}
}
exec_info_term(&g);
return 0;
}
static void print_port_exec(port_value *p, exec_info *f)
/* Pre: f->meta_ps == p->wprobe.wps: print the name of port p to f->scratch */
{ llist l = p->wprobe.wps->p->pl;
var_decl *d;
value_tp *var = p->wprobe.wps->var;
while (!llist_is_empty(&l))
{ d = llist_head(&l);
l = llist_alias_tail(&l);
var_str_printf(&f->scratch, 0, "%s", d->id);
if (_print_wire_value(&var[d->var_idx], &d->tp, &p->wprobe, f))
{ return; }
}
assert(!"Port not found");
}
extern void print_port_value(port_value *p, print_info *f)
/* print the name of port p */
{ exec_info g;
exec_info_init_eval(&g, p->wprobe.wps);
print_port_exec(p, &g);
print_string(g.scratch.s, f);
exec_info_term(&g);
}
/* var_str_func_tp: use as first arg for %v */
extern int vstr_port(var_string *s, int pos, void *p)
/* print the name of port p to s[pos...] */
{ exec_info g;
exec_info_init_eval(&g, ((port_value*)p)->wprobe.wps);
print_port_exec(p, &g);
var_str_printf(s, pos, "%s", g.scratch.s);
exec_info_term(&g);
return 0;
}
extern int print_port_connect(port_value *p, print_info *f)
/* Print (process name):(port_name), where port name may include one or
* more levels of decomposition. If disconnected, print "disconnected"
* and return 1, otherwise return 0;
*/
{ exec_info g;
value_tp *pv;
var_decl *d;
char *rs, *is;
process_state *ps;
if (!p)
{ print_string("(disconnected)", f);
return 1;
}
ps = p->wprobe.wps;
if (!is_visible(ps))
{ assert(!p->dec);
d = llist_idx(&ps->p->pl, 0);
pv = &ps->var[d->var_idx];
if (pv->rep != REP_port || !pv->v.p->dec)
{ d = llist_idx(&ps->p->pl, 1);
pv = &ps->var[d->var_idx];
}
assert(pv->rep == REP_port && pv->v.p->dec);
if (print_port_connect(pv->v.p->p, f)) return 1;
print_char('.', f);
print_string(pv->v.p->dec->id, f);
exec_info_init_eval(&g, ps);
print_port_exec(p, &g);
rs = strchr(g.scratch.s, '.');
is = strchr(g.scratch.s, '[');
if (rs && (!is || is > rs))
{ print_string(rs, f); }
else if (is && (!rs || rs > is))
{ print_string(is, f); }
exec_info_term(&g);
}
else
{ print_string(ps->nm, f);
print_char(':', f);
print_port_value(p, f);
}
return 0;
}
/* var_str_func_tp: use as first arg for %v */
extern int vstr_port_connect(var_string *s, int pos, void *p)
/* print p to s[pos...] (see print_port_connect) */
{ print_info f;
print_info_init(&f);
f.s = s; f.pos = pos;
print_port_connect(p, &f);
VAR_STR_X(f.s, f.pos) = 0;
return 0;
}
extern value_list *new_value_list(int size, exec_info *f)
/* allocate new list with given size */
{ value_list *vl;
int i;
MALLOC(vl, sizeof(*vl) + (size - 1) * sizeof(vl->vl[0]));
vl->size = size;
vl->refcnt = 1;
for (i = 0; i < size; i++)
{ vl->vl[i].rep = REP_none; }
return vl;
}
extern value_union *new_value_union(exec_info *f)
/* create new union value of the given type */
{ value_union *vu;
NEW(vu);
vu->refcnt = 1;
vu->v.rep = REP_none;
return vu;
}
extern port_value *new_port_value(process_state *ps, exec_info *f)
/* allocate new port_value */
{ port_value *p;
NEW(p);
p->wprobe.refcnt = 1;
p->wprobe.flags = WIRE_is_probe;
p->wprobe.wps = ps;
p->wpp = 0;
p->p = 0;
p->v.rep = REP_none;
p->dec = 0;
p->nv = 0;
return p;
}
extern type_value *new_type_value(exec_info *f)
/* allocate new type_value */
{ type_value *tp;
NEW(tp);
tp->refcnt = 1;
tp->meta_ps = f->curr->ps;
f->curr->ps->refcnt++;
return tp;
}
extern wire_value *new_wire_value(token_tp x, exec_info *f)
/* allocate new wire_value */
{ wire_value *w;
NEW(w);
w->refcnt = 1;
w->flags = (x? 0 : WIRE_undef) | (x=='+'? WIRE_value : 0);
w->wframe = &f->curr->act;
return w;
}
extern counter_value *new_counter_value(long x, exec_info *f)
/* allocate new counter_value */
{ counter_value *c;
NEW(c);
c->refcnt = 1;
c->cnt = x;
llist_init(&c->dep);
return c;
}
extern void wire_fix(wire_value **w, exec_info *f)
/* Remove all wire forwarding from w
* Returned wire value is not reference counted
*/
{ wire_value *ww = *w;
if (!IS_SET(ww->flags, WIRE_forward)) return;
wire_fix(&ww->u.w, f);
*w = ww->u.w;
ww->refcnt--;
if (!ww->refcnt) free(ww);
else ww->u.w->refcnt++;
}
static void free_port_value(port_value *p, exec_info *f)
/* disconnect and deallocate p */
{ assert(!p->p);
free(p);
}
extern z_value *new_z_value(exec_info *f)
/* allocate a new z_value, with initial value 0 */
{ z_value *z;
NEW(z);
z->refcnt = 1;
mpz_init(z->z);
return z;
}
extern void clear_value_tp(value_tp *v, exec_info *f)
/* Reset v to have no value. If v has allocated memory (e.g., for array),
free that if there are no longer any references to it.
*/
{ int i;
value_list *vl;
value_union *vu;
process_state *ps;
port_value *p;
type_value *tp;
wire_value *w, *ww;
counter_value *c;
z_value *vz;
switch (v->rep)
{ case REP_z:
vz = v->v.z;
vz->refcnt--;
if (!vz->refcnt)
{ mpz_clear(vz->z);
free(vz);
}
break;
case REP_array: case REP_record:
vl = v->v.l;
vl->refcnt--;
if (!vl->refcnt)
{ for (i = 0; i < vl->size; i++)
{ clear_value_tp(&vl->vl[i], f); }
free(vl);
}
break;
case REP_union:
vu = v->v.u;
vu->refcnt--;
if (!vu->refcnt)
{ clear_value_tp(&vu->v, f);
free(vu);
}
break;
case REP_process:
ps = v->v.ps;
ps->refcnt--;
if (!ps->refcnt)
{ free_process_state(ps, f); }
break;
case REP_port:
p = v->v.p;
p->wprobe.refcnt--;
if (!p->wprobe.refcnt)
{ free_port_value(p, f); }
break;
case REP_type:
tp = v->v.tp;
tp->refcnt--;
if (!tp->refcnt)
{ ps = tp->meta_ps;
ps->refcnt--;
if (!ps->refcnt)
{ free_process_state(ps, f); }
free(tp);
}
break;
case REP_wwire: case REP_rwire:
w = v->v.w;
w->refcnt--;
while (!w->refcnt)
{ if (!IS_SET(w->flags, WIRE_forward))
{ free(w); break; }
ww = w->u.w;
free(w);
w = ww;
w->refcnt--;
}
break;
case REP_cnt:
c = v->v.c;
c->refcnt--;
if (!c->refcnt)
{ free(c); }
break;
default:
break;
}
v->rep = REP_none;
}
extern void copy_value_tp(value_tp *w, value_tp *v, exec_info *f)
/* Copy v to w, allocating new memory */
{ int i;
value_list *vl, *wl;
value_union *vu, *wu;
switch (v->rep)
{ case REP_z:
w->rep = REP_z;
w->v.z = new_z_value(f);
mpz_set(w->v.z->z, v->v.z->z);
return;
case REP_array: case REP_record:
w->rep = v->rep;
vl = v->v.l;
w->v.l = wl = new_value_list(vl->size, f);
for (i = 0; i < vl->size; i++)
{ copy_value_tp(&wl->vl[i], &vl->vl[i], f); }
return;
case REP_union:
w->rep = REP_union;
vu = v->v.u;
w->v.u = wu = new_value_union(f);
wu->d = vu->d;
wu->f = vu->f;
copy_value_tp(&wu->v, &vu->v, f);
return;
/* No new memory for the remaining cases */
case REP_process: *w = *v; w->v.ps->refcnt++; return;
case REP_port: *w = *v; w->v.p->wprobe.refcnt++; return;
case REP_wwire: case REP_rwire: *w = *v; w->v.w->refcnt++; return;
case REP_type: *w = *v; w->v.tp->refcnt++; return;
case REP_cnt: *w = *v; w->v.c->refcnt++; return;
default: *w = *v; return;
}
}
extern void alias_value_tp(value_tp *w, value_tp *v, exec_info *f)
/* Copy v to w, sharing the memory */
{ *w = *v;
switch (v->rep)
{ case REP_z: w->v.z->refcnt++; return;
case REP_array: case REP_record: w->v.l->refcnt++; return;
case REP_union: w->v.u->refcnt++; return;
case REP_process: w->v.ps->refcnt++; return;
case REP_port: w->v.p->wprobe.refcnt++; return;
case REP_wwire: case REP_rwire: w->v.w->refcnt++; return;
case REP_type: w->v.tp->refcnt++; return;
case REP_cnt: w->v.c->refcnt++; return;
default: return;
}
}
extern void copy_and_clear(value_tp *w, value_tp *v, exec_info *f)
/* Same as: copy_value_tp(w, v, f); clear_value_tp(v, f);
but more efficient.
*/
{ int i;
value_list *vl, *wl;
value_union *vu, *wu;
z_value *vz;
if (v->rep == REP_z)
{ vz = v->v.z;
if (vz->refcnt == 1)
{ *w = *v; }
else
{ vz->refcnt--;
copy_value_tp(w, v, f);
}
}
else if (v->rep == REP_array || v->rep == REP_record)
{ w->rep = v->rep;
vl = v->v.l;
if (vl->refcnt == 1)
{ w->v.l = wl = vl;
for (i = 0; i < vl->size; i++)
{ copy_and_clear(&wl->vl[i], &vl->vl[i], f); }
}
else
{ vl->refcnt--;
w->v.l = wl = new_value_list(vl->size, f);
for (i = 0; i < vl->size; i++)
{ copy_value_tp(&wl->vl[i], &vl->vl[i], f); }
}
}
else if (v->rep == REP_union)
{ w->rep = REP_union;
vu = v->v.u;
if (vu->refcnt == 1)
{ w->v.u = wu = vu;
copy_and_clear(&wu->v, &vu->v, f);
}
else
{ vu->refcnt--;
w->v.u = wu = new_value_union(f);
wu->d = vu->d;
wu->f = vu->f;
copy_value_tp(&wu->v, &vu->v, f);
}
}
else
{ *w = *v; }
if (w != v)
{ v->rep = REP_none; }
}
/********** range checks *****************************************************/
static void no_range(expr *x, exec_info *f)
/* called when x has no range function */
{ error("Internal error: "
"Object class %s has no range method", x->class->nm);
}
extern void range_check(type_spec *tps, value_tp *val, exec_info *f, void *obj)
/* Verify that val is a valid value for tps. obj is used for error msgs */
{ value_tp *w = f->val;
void *err_obj = f->err_obj;
if (!val->rep)
{ /* at the top-level this is usually wrong, but when you assign an
array or record it is OK if some fields are unknown. */
return;
}
if (!tps) return; /* happens with some generic types */
f->val = val;
f->err_obj = obj;
APP_OBJ_VFZ(app_range, tps, f, no_range);
f->val = w;
f->err_obj = err_obj;
}
/********** assignment *******************************************************/
static void update_rule(wire_expr_flags val, wire_expr_action u, exec_info *f)
{ action_flags af;
switch(val & WIRE_action)
{ case WIRE_pu: case WIRE_pd:
/* Just mark the changes for now, and add the action to the queue */
af = IS_SET(val, WIRE_pu)? ACTION_up_nxt : ACTION_dn_nxt;
if (IS_SET(val, WIRE_value)) SET_FLAG(u.act->flags, af);
else RESET_FLAG(u.act->flags, af);
if (!IS_SET(u.act->flags, ACTION_check))
{ llist_prepend(&f->check, u.act);
SET_FLAG(u.act->flags, ACTION_check);
}
return;
case WIRE_susp: /* suspended thread, schedule immediately */
if (!IS_SET(val, WIRE_value))
{ assert(!"Action has become unscheduled"); }
if (!IS_SET(u.act->flags, ACTION_sched))
{ SET_FLAG(u.act->flags, ACTION_atomic);
action_sched(u.act, f);
RESET_FLAG(u.act->flags, ACTION_atomic);
}
return;
case WIRE_hu:
if (IS_SET(val, WIRE_value))
{ RESET_FLAG(u.hold->flags, WIRE_held_up);
if (IS_SET(u.hold->flags, WIRE_wait))
{ write_wire(1, u.hold, f); }
RESET_FLAG(u.hold->flags, WIRE_wait);
}
else
{ SET_FLAG(u.hold->flags, WIRE_held_up); }
return;
case WIRE_hd:
if (IS_SET(val, WIRE_value))
{ RESET_FLAG(u.hold->flags, WIRE_held_dn);
if (IS_SET(u.hold->flags, WIRE_wait))
{ write_wire(0, u.hold, f); }
RESET_FLAG(u.hold->flags, WIRE_wait);
}
else
{ SET_FLAG(u.hold->flags, WIRE_held_dn); }
return;
case WIRE_xu:
if (IS_SET(val, WIRE_value))
{ write_wire(1, u.hold, f); }
return;
case WIRE_xd:
if (IS_SET(val, WIRE_value))
{ write_wire(0, u.hold, f); }
return;
case WIRE_x:
write_wire(IS_SET(val, WIRE_value)? 1 : 0, u.hold, f);
return;
case WIRE_vc:
if (IS_SET(val, WIRE_value))
{ clear_value_tp(u.val, f); }
return;
default:
return;
}
}
static void update_wire_expr(wire_flags val, wire_expr *e, exec_info *f)
/* Flag WIRE_value of val has the new value of the wire, flag WIRE_undef
* of val tells us whether the wire was previously undefined.
*/
{ int old;
llist m;
if (!e->undefcnt) /* Nothing undefined, make this fast */
{ if (IS_SET(e->flags, WIRE_trigger))
{ e->valcnt -= (~val ^ (e->flags >> WIRE_vd_shft)) & WIRE_value;
/* Here we have made use of the fact that WIRE_value == 1 */
if (e->valcnt > 0) return;
e->valcnt = e->refcnt;
if (IS_SET(e->flags, WIRE_xor))
{ e->flags ^= WIRE_value | WIRE_val_dir; }
}
else if (IS_SET(e->flags, WIRE_xor))
{ e->flags = e->flags ^ WIRE_value; }
else
{ old = e->valcnt;