This repository has been archived by the owner on Jul 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass3.cc
2008 lines (1737 loc) · 59 KB
/
pass3.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 1994-2005, Elliot Mednick and Mark Hummel
* This file is part of Veriwell.
*
* Veriwell 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 2 of the License, or
* (at your option) any later version.
*
* Veriwell 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 Foobar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/* PASS3.C - Snap forward pointers, resolve hierarchical names, expr lengths */
#define PASS3_C
#include <stdlib.h>
#include <string.h>
#include "glue.h"
#include "vtypes.h"
#include "tree.h"
#include "schedule.h"
#include "obstack.h"
#include "acc_user.h"
#include "pli.h"
#include "systask.h"
#include "decl.h"
#include "multdiv.h"
#include "pass3.h"
#include "scope.h"
#include "specify.h"
#include "gates.h"
#include "eval.h"
#include "pass2.h"
#include "timescal.h"
#include "veriwell.h"
#if VDEBUG != 0
#include <stdio.h>
#include "flags.h"
#endif
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
extern tree current_scope;
tree *debug_;
tree debug_tree;
static int in_lval = 0; /* set if processing lvalue (used in concat) */
static int in_connect_ports = 0; /* set if building port connections */
#define INSERT_PTR(NODE) obstack_ptr_grow (&inst_obstack, (tree) NODE)
/* This obstack contains pointers to expression/ref/decl nodes in the
order that they should be evaluated (postfix). */
struct obstack inst_obstack;
struct obstack alt_inst_obstack;
/* This is some global information that needs to be kept around for
when markers need to be generated for references inside event
expressions. Perhaps this is a grungy way of doing it. */
struct Marker_info marker_info;
/* save the number of instructions for the last expression
processed in case bit_ref needs it */
static int last_inst_length;
/* this is used as a temp stack fro saving stack size */
extern struct obstack linkage_obstack;
/* In fixup_nbits, while the tree is being walked, use the following
variables to keep track of the space required to execute the
expression. */
ngroups_t have_for_push, have_for_pad, stack_size = 0, stack_extension = 0;
int stack_lineno;
char *stack_filename;
tree debug_node;
/* Lets NET lvals point to its source */
tree global_cont_assign;
/* Keep track of the largest number of regsisters required thus far. */
int max_label = 0;
/* The instruction obstack may be interrupted so that part-select references
and other constants can be evaluated. When something needs to be
evaluated, push the current state of the obstack, and start a new one.
When the evaluation is complete, pop the obstack and restore the state
so that new stuff can be added to the old stuff. */
struct inst_level {
struct inst_level *prev; /* where to pop to */
void *base;
void *finish;
};
static struct inst_level *inst_stack;
void push_inst()
{
void *finish = obstack_next_free(&inst_obstack); /* mark temporary end */
void *base = obstack_finish(&inst_obstack);
struct inst_level *tem =
(struct inst_level *) obstack_alloc(&inst_obstack,
sizeof(struct inst_level));
tem->prev = inst_stack;
tem->base = base; /* save the base */
tem->finish = finish; /* save the current end */
inst_stack = tem;
}
void pop_inst()
{
struct inst_level *tem = inst_stack;
void *base;
inst_stack = tem->prev;
base = tem->base;
obstack_free(&inst_obstack, tem->finish); /* unwind to end of prev stack */
obstack_set_base(&inst_obstack, base); /* restore prev stack base */
return;
}
/* Certain instructions need to keep track of the size of subexpressions.
In doing so, it will push the current stack size settings and reset
them. It will pop the saved setting at the end of its operation. */
struct stack_size_level {
struct stack_size_level *prev; /* where to pop to */
ngroups_t have_for_push;
ngroups_t have_for_pad;
};
static struct stack_size_level *stack_size_stack;
void push_stack_size()
{
struct stack_size_level *tem
= (struct stack_size_level *) obstack_alloc(&linkage_obstack,
sizeof(struct
stack_size_level));
tem->prev = stack_size_stack;
tem->have_for_push = have_for_push;
tem->have_for_pad = have_for_pad;
stack_size_stack = tem;
have_for_pad = have_for_push = 0;
}
void pop_stack_size()
{
struct stack_size_level *tem = stack_size_stack;
stack_size_stack = tem->prev;
have_for_push = tem->have_for_push;
have_for_pad = tem->have_for_pad;
obstack_free(&linkage_obstack, (char *) tem);
return;
}
/* At the end of pass 3, stack_allocate is called to allocate space
for the stack */
void stack_allocate()
{
if (!R_alloc(max_label, stack_size)) {
lineno = stack_lineno;
input_filename = stack_filename;
fatal("Out of memory", NULL);
}
}
/* Make sure there is enough space on the stack for an expression that
will be evaluated in the middle of another expression. Eg shift count
and conditional operator. */
/* Note that this code is very similar to pass3_expr() and perhaps should
be merged someday. */
static nbits_t ensure_stack_space(tree node)
{
nbits_t nbits;
ngroups_t tmp_pad;
char *type = tree_code_type[TREE_CODE(node)];
push_stack_size(); /* suspend current stack count */
nbits = fixup_nbits(node); /* generate instructions & shift cnt */
adjust_nbits(nbits, &node, (tree *) obstack_base(&inst_obstack));
if (*type != 'e' || (*++type != '1' && *type != 'r'))
reserve_stack_space(TREE_SUB_LABEL(node), nbits, nbits);
tmp_pad = have_for_push + have_for_pad; /* treat all as padding */
pop_stack_size(); /* get back current count */
have_for_pad = max(tmp_pad, have_for_pad); /* account for the extra padding */
return nbits;
}
/* reserve space on the expression stack. Most of the space in needed to
push datum, but some is for the result which would be included
in the calculation for the parent expression, if any, so it is
treated as temporary padding. */
static void
reserve_stack_space(int number, nbits_t nbits_push, nbits_t nbits_pad)
{
ngroups_t need_for_push = number * (bits_to_groups(nbits_push) + 1);
ngroups_t need_for_pad = bits_to_groups(nbits_pad) + 1;
have_for_push += need_for_push; /* add to the stack */
if (need_for_push >= have_for_pad) /* take away from the padding */
have_for_pad = 0;
else
have_for_pad -= need_for_push;
have_for_pad = max(have_for_pad, need_for_pad); /* incread pad if nec. */
}
/* Return the module containing the given scope */
tree module_of(tree scope)
{
if (TREE_CODE(scope) == MODULE_BLOCK)
return scope;
else
return (module_of(BLOCK_UP(scope)));
}
tree lookdown_scope(char *ident, tree scope)
{
tree t;
// ??? mdh - also look in parent scopes
do {
for (t = BLOCK_DOWN(scope); t; t = TREE_CHAIN(t)) {
if (!strcmp(IDENT(BLOCK_NAME(t)), ident))
return t;
}
scope = BLOCK_UP(scope);
}
while (scope);
return NULL_TREE;
}
/* Build a marker if the reference is inside an even expression */
void maybe_build_marker(tree decl)
{
if (decl == error_mark_node) {
return;
}
if (marker_info.in_event)
BuildMarker(decl, &marker_info);
}
void adjust_nbits(nbits_t nbits, tree * tree_addr, tree * code_base)
{
tree node = *tree_addr;
ngroups_t ngroups;
char *tree_type_ptr = tree_code_type[TREE_CODE(node)];
/* Always make sure there is space on the stack */
if ((ngroups = bits_to_groups(nbits)) >= stack_size)
stack_size = ngroups + 1;
// if (stack_size > 50)
// fatal ("Big stack size at line %lu\n", lineno);
if (TREE_NBITS(node) >= nbits)
return;
switch (*tree_type_ptr) {
case ('e'):
switch (*++tree_type_ptr) {
case ('1'): /* binary condition */
TREE_NBITS(node) = nbits;
return;
case ('x'): /* binary context-dependent */
TREE_NBITS(node) = nbits;
adjust_nbits(nbits, &TREE_OPERAND(node, 0), code_base);
adjust_nbits(nbits, &TREE_OPERAND(node, 1), code_base);
return;
case ('u'): /* unary context-dependent */
TREE_NBITS(node) = nbits;
adjust_nbits(nbits, &TREE_OPERAND(node, 0), code_base);
return;
case ('r'): /* unary reduction */
case ('c'): /* unary type conversion */
TREE_NBITS(node) = nbits;
return;
case ('s'): /* shift */
TREE_NBITS(node) = nbits;
/* pass on to operand, not to shift count */
adjust_nbits(nbits, &TREE_OPERAND(node, 0), code_base);
return;
case ('3'): /* conditional */
TREE_NBITS(node) = nbits;
adjust_nbits(nbits, &COND_TRUE(node), COND_TRUE_CODE(node));
adjust_nbits(nbits, &COND_FALSE(node), COND_FALSE_CODE(node));
return;
}
case ('r'):
case ('c'):
TREE_NBITS(node) = nbits;
return;
case ('d'):
{
tree t = make_node(SHADOW_REF);
TREE_CHAIN(t) = node;
TREE_REAL_ATTR(t) = TREE_REAL_ATTR(node);
TREE_INTEGER_ATTR(t) = TREE_INTEGER_ATTR(node);
*tree_addr = t;
/* go back through code pointers and change decl pointer to
ref pointer */
{
tree *p;
for (p = code_base; *p != node; p++);
*p = t;
}
TREE_NBITS(t) = nbits;
return;
}
}
}
static struct part_info *cook_part_ref(nbits_t offset_high,
nbits_t offset_low, int concat)
{
struct part_info *info =
(struct part_info *) xmalloc(sizeof(struct part_info));
int rel_offset_low = offset_low % BITS_IN_GROUP;
int rel_offset_high = offset_high % BITS_IN_GROUP;
int length = offset_high - offset_low + 1;
/* Case 1: within one group */
if (offset_high / BITS_IN_GROUP == offset_low / BITS_IN_GROUP) {
PART_ALIGNED(info) = 1;
PART_LMASK1(info) =
~(mask_right[rel_offset_low] ^
mask_right[rel_offset_high + 1]);
PART_RMASK(info) =
mask_right[rel_offset_high - rel_offset_low + 1];
PART_NGROUPS(info) = 0;
PART_SHIFT(info) = rel_offset_low;
if (rel_offset_high == BITS_IN_GROUP - 1)
PART_NEXT(info) = 1; /* next avail group in concat */
else
PART_NEXT(info) = 0; /* still room in this group */
} else {
PART_NGROUPS(info) = bits_to_groups(length);
PART_ALIGNED(info) = rel_offset_high >= rel_offset_low;
PART_RMASK(info) =
mask_right[rel_offset_high - rel_offset_low + 1];
PART_LMASK1(info) = mask_right[rel_offset_low];
if (!PART_ALIGNED(info))
PART_LMASK2(info) = ~mask_right[rel_offset_high + 1];
else
PART_LMASK2(info) =
~(mask_right[rel_offset_low] ^
mask_right[rel_offset_high + 1]);
PART_SHIFT(info) = rel_offset_low;
PART_NEXT(info) = bits_to_groups(length + rel_offset_low + 1);
}
return info;
}
/* Evaluate LSB or MSB of DECL or part-select reference. Must scan
expression tree (see fixup_nbits) and generate code. Then evaluate
expression checking for legal range limits. */
nbits_t get_range(tree node, char *ident)
{
tree *expr_code;
nbits_t index;
push_inst(); /* Push the code obstack since we're prob in the middle of something */
expr_code = pass3_expr(node);
/*
* allow things to continue even if errors exist
*/
// if( max_label == 0 ) {
// return 0;
// }
if (TREE_NBITS(node) > MAX_BITS_IN_NUMBER)
error("Range expression must be within 32 bits", NULL_CHAR,
NULL_CHAR);
if (!R_alloc(max_label, stack_size))
fatal("Not enough memory to evaluate range expression of %s",
IDENTIFIER_POINTER(DECL_NAME(node)));
index = eval_range(expr_code, ident);
pop_inst();
return index;
}
/* Evaluate 32-bit constant */
Group *get_const(tree node, nbits_t * nbits_ptr)
{
tree *expr_code;
Group *val;
push_inst(); /* Push the code obstack since we're prob in the middle of something */
expr_code = pass3_expr(node);
if (!R_alloc(max_label, stack_size))
fatal("Not enough memory to evaluate constant expression",
NULL_CHAR);
val = eval_(expr_code, nbits_ptr);
// if (TREE_NBITS (node) > MAX_BITS_IN_NUMBER)
// error ("Constant expression must be within 32 bits", NULL_CHAR, NULL_CHAR);
pop_inst();
return val;
}
/* Evaluate 32-bit scaled delay */
Bit get_delay(tree node)
{
tree *expr_code;
Group *val;
nbits_t nbits_ptr;
push_inst(); /* Push the code obstack since we're prob in the middle of something */
expr_code = timescale_scale(node);
if (!R_alloc(max_label, stack_size))
fatal("Not enough memory to evaluate constant expression",
NULL_CHAR);
val = eval_(expr_code, &nbits_ptr);
// if (TREE_NBITS (node) > MAX_BITS_IN_NUMBER)
// error ("Constant expression must be within 32 bits", NULL_CHAR, NULL_CHAR);
// ASSERT( nbits_ptr <= 32 );
pop_inst();
return AVAL(val);
}
/* resolve the hierarchical name and build a shadow for the decl node */
static nbits_t pass3_hierarchy(tree node)
{
tree t1 = resolve_hierarchical_name(node);
tree t = make_node(SHADOW_REF);
REF_NAME(t) = node;
TREE_CHAIN(t) = t1;
TREE_NBITS(t) = TREE_NBITS(t1);
HIERARCHICAL_ATTR(t) = 1;
INSERT_PTR(t);
maybe_build_marker(t1); /* Marker to original decl */
return TREE_NBITS(t);
}
static nbits_t fixup_reference(tree node)
{
enum tree_code code = TREE_CODE(node);
nbits_t msb, lsb, msb_decl, lsb_decl;
nbits_t nbits, nbits1, nbits_decl, prev_nbits;
Group *val;
tree t;
switch (code) {
case BIT_REF:
BIT_EXPR_CODE(node) = pass3_expr_intrude(BIT_EXPR(node), 1);
TREE_LABEL(node) = TREE_LABEL(BIT_EXPR(node));
TREE_SUB_LABEL(node) = TREE_SUB_LABEL(BIT_EXPR(node));
if (TREE_NBITS(*BIT_EXPR_CODE(node)) > MAX_BITS_IN_NUMBER)
error("Bit-select index expression must be within 32 bits",
NULL_CHAR, NULL_CHAR);
/* If this is a hierarchical reference, then there was no way to
determine if this was a bit reference or an array reference.
Once we know, make sure the reference is set correctly. Since
bit and array references look identical, then all that need to
be dome is to set the code correctly so that eval() will
do the right thing. */
if (HIERARCHICAL_ATTR(node)) {
BIT_REF_DECL(node) =
resolve_hierarchical_name(BIT_REF_NAME(node));
if (TREE_CODE(BIT_REF_DECL(node)) == ARRAY_DECL)
TREE_SET_CODE(node, ARRAY_REF);
}
maybe_build_marker(BIT_REF_DECL(node));
if (TREE_CODE(node) == ARRAY_REF)
TREE_NBITS(node) = TREE_NBITS(BIT_REF_DECL(node));
else
TREE_NBITS(node) = 1;
break;
case ARRAY_REF:
ARRAY_EXPR_CODE(node) = pass3_expr_intrude(ARRAY_EXPR(node), 1);
if (TREE_NBITS(*ARRAY_EXPR_CODE(node)) > MAX_BITS_IN_NUMBER)
error("Array index expression must be within 32 bits",
NULL_CHAR, NULL_CHAR);
maybe_build_marker(ARRAY_REF_DECL(node));
TREE_NBITS(node) = TREE_NBITS(ARRAY_REF_DECL(node));
break;
case CONCAT_REP_REF:
val = get_const(CONCAT_EXPR(node), &nbits);
if (!BVAL(val))
CONCAT_COUNT(node) = AVAL(val);
else
CONCAT_COUNT(node) = 0; /* X or Z in expression */
if (nbits > MAX_BITS_IN_NUMBER)
warning
("Repeat count expression is larger than 32 bits; will be truncated",
NULL, NULL);
case CONCAT_REF:
nbits = prev_nbits = 0;
// push_inst ();
/* TREE_PURPOSE (t) is the member expression. TREE_VALUE (t) points
to another tree_list node. TREE_PURPOSE of this 2nd list node is
the expression code; TREE_VALUE of the 2nd list node points to a
part_info structure.
*/
for (t = CONCAT_LIST(node); t; t = TREE_CHAIN(t)) {
tree tmp_tree;
TREE_VALUE(t) = make_node(TREE_LIST);
TREE_PURPOSE(TREE_VALUE(t)) =
(tree) pass3_expr_intrude(TREE_PURPOSE(t), 1);
TREE_LABEL(node) =
max(TREE_LABEL(node), TREE_LABEL(TREE_PURPOSE(t)));
TREE_SUB_LABEL(node) =
max(TREE_SUB_LABEL(node), TREE_SUB_LABEL(TREE_PURPOSE(t)));
/* To get the right NBITS, use TREE_NBITS from the original expression
if it is not a hierarchical reference; get TREE_NBITS from the
dereferenced expression code if it is a hierarchical reference.
*/
tmp_tree =
H_(*(tree *) TREE_PURPOSE(TREE_VALUE(t)), TREE_PURPOSE(t));
nbits1 = TREE_NBITS(tmp_tree);
/* lval case shifts over previous concat entry; rval case shifts
from absolute location */
TREE_VALUE(TREE_VALUE(t)) =
(tree) cook_part_ref(nbits + nbits1 - 1, nbits, 1);
/* nbits is the total number of bits in the concat ref */
nbits += nbits1;
if (bits_to_groups(nbits) > bits_to_groups(nbits1))
prev_nbits = nbits % BITS_IN_GROUP;
else
prev_nbits = nbits1;
}
TREE_LABEL(node) += 1;
TREE_SUB_LABEL(node) += 1;
CONCAT_NBITS(node) = nbits;
if (code == CONCAT_REF)
TREE_NBITS(node) = nbits;
else
TREE_NBITS(node) = nbits * CONCAT_COUNT(node);
break;
case PART_REF:
if (HIERARCHICAL_ATTR(node)) {
PART_DECL(node) = resolve_hierarchical_name(PART_NAME(node));
if (PART_DECL(node) == error_mark_node) {
TREE_NBITS(node) = 1;
break;
}
}
/* If the decl that node is pointing to is a redeclared port, move the pointer
to the newer decl */
/*
if ((PORT_OUTPUT_ATTR (PART_DECL (node)) ||
PORT_INPUT_ATTR (PART_DECL (node)))
&& DECL_THREAD (PART_DECL (node)))
PART_DECL (node) = DECL_THREAD (PART_DECL (node));
*/
PART_MSB(node) = msb =
get_range(PART_MSB_(node),
IDENTIFIER_POINTER(DECL_NAME(PART_DECL(node))));
PART_LSB(node) = lsb =
get_range(PART_LSB_(node),
IDENTIFIER_POINTER(DECL_NAME(PART_DECL(node))));
nbits = msb - lsb;
if (ABS(nbits) + 1 > MAX_BITS)
error("Part-select for '%s' encompases too many bits",
IDENT(DECL_NAME(PART_DECL(node))), NULL_CHAR);
msb_decl = MSB(PART_DECL(node));
lsb_decl = LSB(PART_DECL(node));
nbits_decl = msb_decl - lsb_decl;
if (nbits < 0) {
if (nbits_decl > 0)
error("MSB and LSB of '%s' are reversed",
IDENT(DECL_NAME(PART_DECL(node))), NULL_CHAR);
else if (msb < msb_decl || lsb > lsb_decl)
error
("Part-select is outside limits set by declaration of '%s'",
IDENT(DECL_NAME(PART_DECL(node))), NULL_CHAR);
} else {
if (nbits_decl < 0)
error("MSB and LSB of '%s' are reversed",
IDENT(DECL_NAME(PART_DECL(node))), NULL_CHAR);
else if (msb > msb_decl || lsb < lsb_decl)
error
("Part-select is outside limits set by declaration of '%s'",
IDENT(DECL_NAME(PART_DECL(node))), NULL_CHAR);
}
PART_STORAGE(node) = DECL_STORAGE(PART_DECL(node))
+ (ABS(lsb - lsb_decl)) / BITS_IN_GROUP;
PART_INFO(node) =
cook_part_ref(ABS(msb - lsb_decl), ABS(lsb - lsb_decl), 0);
TREE_NBITS(node) = ABS(nbits) + 1;
maybe_build_marker(PART_DECL(node));
break;
case FUNCTION_REF:
if (HIERARCHICAL_ATTR(node))
t = search_scope(FUNC_REF_NAME(node), 0);
else
// t = lookdown_scope (FUNC_REF_NAME (node), current_pass3_module);
t = lookdown_scope(FUNC_REF_NAME(node),
module_of(current_scope));
if (!t) {
error("Function '%s' not defined", FUNC_REF_NAME(node),
NULL_CHAR);
break;
}
FUNC_REF_INASSIGN(node) = NULL_TREE;
FUNC_REF_FUNC(node) = t;
FUNC_REF_CONTEXT(node) = current_scope;
if (FUNC_REF_ARGS(node)) {
pass3_args(FUNC_REF_ARGS(node), FUNC_REF_NAME(node),
&FUNC_REF_INASSIGN(node), (tree *) NULL,
BLOCK_PORTS(t), 1);
}
TREE_NBITS(node) = TREE_NBITS(FUNCT_DECL(t));
TREE_REAL_ATTR(node) = TREE_REAL_ATTR(FUNCT_DECL(t));
TREE_INTEGER_ATTR(node) = TREE_INTEGER_ATTR(FUNCT_DECL(t));
break;
case SYSFUNCTION_REF:
{
p_tfcell p_tf;
enum sysfunction_type sysfunction =
lookup_sysfunction(FUNC_REF_NAME(node), &p_tf);
if (sysfunction == -1) {
if (lookup_systask(FUNC_REF_NAME(node), &p_tf) != -1)
error
("'%s' is a system task; it cannot be called as a function",
FUNC_REF_NAME(node), NULL_CHAR);
else
error("Unknown system function '%s'",
FUNC_REF_NAME(node), NULL_CHAR);
TREE_NBITS(node) = 32;
break;
}
FUNC_REF_CONTEXT(node) = current_scope;
SET_FUNC_REF_SYSTYPE(node, sysfunction);
FUNC_REF_USERTF(node) = p_tf;
init_sysfunction(node);
}
break;
} /* switch */
return TREE_NBITS(node);
}
/* Convert op1 to real if necessary */
int pass3_early_conversion(tree * op1_ptr, tree op2)
{
tree op1 = *op1_ptr;
/* if op1 is not real but op2 is, insert a real conversion operator */
if (!TREE_REAL_ATTR(op1) && TREE_REAL_ATTR(op2)) {
*op1_ptr = build_unary_op(REAL_CONV_EXPR, op1);
TREE_REAL_ATTR(*op1_ptr) = 1;
TREE_INTEGER_ATTR(*op1_ptr) = 0;
TREE_NBITS(*op1_ptr) = 32;
return 1;
}
return 0;
}
/* Convert op1 to integer if necessary */
int pass3_early_both_conversion(tree * op1_ptr, tree op2)
{
tree op1 = *op1_ptr;
/* if op1 is real, but op2 is not, convert op1 to integer */
if (TREE_REAL_ATTR(op1) && !TREE_REAL_ATTR(op2)) {
*op1_ptr = build_unary_op(INT_CONV_EXPR, op1);
TREE_REAL_ATTR(*op1_ptr) = 0;
TREE_INTEGER_ATTR(*op1_ptr) = 1;
TREE_NBITS(*op1_ptr) = 1;
return 1;
} else
return pass3_early_conversion(op1_ptr, op2);
}
/* After it is already too late, add an integer->real conversion block */
int pass3_late_conversion(tree * op1_ptr, tree op2, int code_offset)
{
tree *p, *p_end;
if (pass3_early_conversion(op1_ptr, op2)) {
INSERT_PTR(NULL_TREE); /* Make room for one more pointer */
/* Start at current end, shift all instruction pointers down one
until we are at the place where the conversion instruction needs
to be inserted */
p = (tree *) obstack_next_free(&inst_obstack);
p_end =
(tree *) ((char *) obstack_base(&inst_obstack) + code_offset);
while (--p > p_end)
*p = *(p - 1);
*p = *op1_ptr; /* Insert newly-created conversion block */
return 1;
}
return 0;
}
/* Fixup_nbits performs several tasks:
1) sets TREE_NBITS for each node with is the expression length of the
result of the operation. It make sure that the Verilog rules for
context-dependent expression length are followed. If two operands
of an operator ar of different lengths, adjust_nbits will be called
to enlarge the smaller subexpression. Adjusting does not go below
conditional and reduction operators since their lengths are not context-
dependent.
2) As the expression tree is walked, a list is created that points to
all of the nodes in the expression in postfix order. This list will
later be used to execute/evaluate the expression at runtime.
3) As part-references are found, the expressions for their msbs and lsbs
are evaluated and tested for consistancy (same direction as the declared
var, etc.). This is done by recursively calling fixup_nbits.
*/
nbits_t fixup_nbits(tree node)
{
nbits_t nbits_left, nbits_right, nbits;
enum tree_code code = TREE_CODE(node);
char *tree_type_ptr = tree_code_type[code];
tree *finish;
int finish_size, op0_offset;
switch (*tree_type_ptr) {
case 'x':
if (HIERARCHICAL_ATTR(node))
return pass3_hierarchy(node);
else if (node == error_mark_node)
return 1;
#if VDEBUG != 0
else if (vdebug) {
if (code == ERROR_MARK) {
printf_error_V("Pass3 over error_mark");
return 0;
}
}
#endif
else
fatal("Internal: Attempt to reparse identifier", NULL_CHAR);
break;
case ('r'):
INSERT_PTR(node);
TREE_NBITS(node) = fixup_reference(node);
return TREE_NBITS(node);
case ('d'): /* decl nodes will have had TREE_NBITS set */
maybe_build_marker(node);
// if (!INITIALIZED_ATTR (node))
// fatal ("Uninitialized decl: %s\n", IDENTIFIER_POINTER (DECL_NAME (node)));
if (in_lval)
if (code == NET_VECTOR_DECL || code == NET_SCALAR_DECL)
NET_ASSIGNMENT(node) = global_cont_assign;
case ('c'): /* as will constant nodes */
INSERT_PTR(node);
return TREE_NBITS(node);
case ('e'):
switch (*++tree_type_ptr) {
/* The condition operator (?:) is treated as a binary op for the
purpose of allocating stack space since in the worst case
(condition = X), both true and false expressions need to be
evaluated. But, make sure there is enough stack space for
the condition expression */
case ('3'):
COND_EXPR_CODE(node) = pass3_expr_intrude(COND_EXPR(node), 0);
push_inst();
nbits_left = fixup_nbits(COND_TRUE(node));
INSERT_PTR(NULL_TREE); /* Make room for possible conversion */
op0_offset = obstack_object_size(&inst_obstack);
INSERT_PTR(NULL_TREE);
finish_size = obstack_object_size(&inst_obstack);
finish = (tree *) obstack_finish(&inst_obstack);
COND_TRUE_CODE(node) =
(tree *) obstack_copy(&alt_inst_obstack, finish,
finish_size);
if (pass3_early_conversion(&COND_FALSE(node), COND_TRUE(node)))
TREE_REAL_ATTR(node) = 1;
nbits_right = fixup_nbits(COND_FALSE(node));
INSERT_PTR(NULL_TREE);
finish_size = obstack_object_size(&inst_obstack);
finish = (tree *) obstack_finish(&inst_obstack);
COND_FALSE_CODE(node) =
(tree *) obstack_copy(&alt_inst_obstack, finish,
finish_size);
pop_inst(); /* go back to current expression */
/* go back and add real conversion if necessary */
if (pass3_early_conversion(&COND_TRUE(node), COND_FALSE(node))) {
tree *p = COND_TRUE_CODE(node) + op0_offset;
*p = COND_TRUE(node);
nbits_left = 32;
TREE_REAL_ATTR(node) = 1;
}
if (nbits_left < nbits_right) {
adjust_nbits(nbits_right, &COND_TRUE(node),
COND_TRUE_CODE(node));
nbits = nbits_right;
} else if (nbits_left > nbits_right) {
adjust_nbits(nbits_left, &COND_FALSE(node),
COND_FALSE_CODE(node));
nbits = nbits_left;
} else
nbits = nbits_left; /* nbits_left equals nbits_right */
INSERT_PTR(node);
TREE_NBITS(node) = (*tree_type_ptr == '1') ? 1 : nbits_left;
break;
/* For a binary context-dependent operations, return the largest of the
left and right operands and make the smaller side the same size. */
case ('x'):
case ('1'):
nbits_left = fixup_nbits(TREE_OPERAND(node, 0));
op0_offset = obstack_object_size(&inst_obstack);
/* && and || are conditional, the operator terminate once the
truth can be deterimined. Thus, the right op is recuresively
evaluated at runtime only if necessary; a seperate set of
instructions is generated for this. */
if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR) {
push_inst();
if (pass3_early_conversion(&TREE_OPERAND(node, 1),
TREE_OPERAND(node, 0))) {
TREE_REAL_ATTR(node) = 1;
TREE_INTEGER_ATTR(node) = 0;
}
nbits_right = fixup_nbits(TREE_OPERAND(node, 1));
INSERT_PTR(NULL_TREE);
finish_size = obstack_object_size(&inst_obstack);
finish = (tree *) obstack_finish(&inst_obstack);
TRUTH_EXPR_CODE(node) =
(tree *) obstack_copy(&alt_inst_obstack, finish,
finish_size);
pop_inst();
if (pass3_late_conversion(&TREE_OPERAND(node, 1),
TREE_OPERAND(node, 0),
op0_offset)) {
TREE_REAL_ATTR(node) = 1;
TREE_INTEGER_ATTR(node) = 0;
}
} else {
if (pass3_early_conversion(&TREE_OPERAND(node, 1),
TREE_OPERAND(node, 0))) {
TREE_REAL_ATTR(node) = 1;
TREE_INTEGER_ATTR(node) = 0;
}
nbits_right = fixup_nbits(TREE_OPERAND(node, 1));
if (pass3_late_conversion(&TREE_OPERAND(node, 0),
TREE_OPERAND(node, 1),
op0_offset)) {
TREE_REAL_ATTR(node) = 1;
TREE_INTEGER_ATTR(node) = 0;
}
if (TREE_REAL_ATTR(TREE_OPERAND(node, 0))
&& TREE_REAL_ATTR(TREE_OPERAND(node, 1))) {
TREE_REAL_ATTR(node) = 1;
TREE_INTEGER_ATTR(node) = 0;
}
}
if (nbits_left < nbits_right) {
adjust_nbits(nbits_right, &TREE_OPERAND(node, 0),
(tree *) obstack_base(&inst_obstack));
nbits = nbits_right;
} else if (nbits_left > nbits_right) {
if (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)
adjust_nbits(nbits_left, &TREE_OPERAND(node, 1),
TRUTH_EXPR_CODE(node));
else
adjust_nbits(nbits_left, &TREE_OPERAND(node, 1),
(tree *) obstack_base(&inst_obstack));
nbits = nbits_left;
} else
nbits = nbits_left; /* nbits_left equals nbits_right */
/* reserve enough stack space for the subexpression */
if (*tree_type_ptr == '1')
reserve_stack_space(TREE_SUB_LABEL(node), nbits,
(nbits_t) 1);
INSERT_PTR(node);
TREE_NBITS(node) = nbits = (*tree_type_ptr == '1') ? 1 : nbits;
break;
case 'r':
if (code == POSEDGE_EXPR || code == NEGEDGE_EXPR)
TREE_OPERAND(node, 1) = (tree) X;
reserve_stack_space(TREE_SUB_LABEL(node),
fixup_nbits(TREE_OPERAND(node, 0)),
(nbits_t) 1);
if (TREE_REAL_ATTR(TREE_OPERAND(node, 0)))
error("Reduction not allowed on real numbers", NULL, NULL);
INSERT_PTR(node);
TREE_NBITS(node) = nbits = 1;
break;
case 's': /* shift operations */
TREE_NBITS(node) = nbits = fixup_nbits(SHIFT_OPERAND(node));
SHIFT_NBITS(node) = ensure_stack_space(SHIFT_COUNT(node));
if (SHIFT_NBITS(node) > MAX_BITS_IN_NUMBER)
error("Shift count must be within 32 bits", NULL_CHAR,
NULL_CHAR);
if (TREE_REAL_ATTR(SHIFT_OPERAND(node))
|| TREE_REAL_ATTR(SHIFT_COUNT(node)))
error("Shift not allowed on real numbers", NULL, NULL);
INSERT_PTR(node);
break;
case 'u':
TREE_NBITS(node) = nbits = fixup_nbits(TREE_OPERAND(node, 0));
TREE_REAL_ATTR(node) = TREE_REAL_ATTR(TREE_OPERAND(node, 0));
TREE_INTEGER_ATTR(node) =
TREE_INTEGER_ATTR(TREE_OPERAND(node, 0));
INSERT_PTR(node);
break;
case 'c':
nbits = fixup_nbits(TREE_OPERAND(node, 0));
if (code == REAL_CONV_EXPR) {
/* It is possible that the operand is already real (function or
hierarchical name). If so, make it a nop. */
if (TREE_REAL_ATTR(TREE_OPERAND(node, 0))) {
TREE_SET_CODE(node, NOP_EXPR);
TREE_NBITS(node) = nbits;
break;
}
TREE_NBITS(node) = nbits = 32;
} else
/* When converting to integer, let adjust_nbits fill in the correct
size */
TREE_NBITS(node) = nbits = 1;
INSERT_PTR(node);
break;
default:
TREE_NBITS(node) = nbits = fixup_nbits(TREE_OPERAND(node, 0));
TREE_REAL_ATTR(node) = TREE_REAL_ATTR(TREE_OPERAND(node, 0));
TREE_INTEGER_ATTR(node) =
TREE_INTEGER_ATTR(TREE_OPERAND(node, 0));
INSERT_PTR(node);
break;
} /* switch 'e' */
}
return nbits;
}
/* Process lval; discard any generated code; set lval flag so the proper