-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
13700 lines (10552 loc) · 332 KB
/
parse.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
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) 2014-2024 Nick Gasson
//
// This program 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.
//
// 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, see <http://www.gnu.org/licenses/>.
//
#include "util.h"
#include "array.h"
#include "common.h"
#include "diag.h"
#include "hash.h"
#include "inst.h"
#include "lib.h"
#include "names.h"
#include "object.h"
#include "option.h"
#include "phase.h"
#include "psl/psl-node.h"
#include "psl/psl-phase.h"
#include "scan.h"
#include "thread.h"
#include "tree.h"
#include "type.h"
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
#include <inttypes.h>
typedef struct {
token_t token;
yylval_t lval;
loc_t loc;
} tokenq_t;
typedef bool (*look_fn_t)(token_t);
typedef struct {
token_t look[4];
token_t stop[4];
token_t abort;
token_t nest_in;
token_t nest_out;
look_fn_t lookfn;
int depth;
} look_params_t;
typedef A(tree_t) tree_list_t;
typedef A(type_t) type_list_t;
typedef struct _ident_list ident_list_t;
struct _ident_list {
ident_list_t *next;
ident_t ident;
loc_t loc;
};
#define LOCAL_IDENT_LIST \
__attribute__((cleanup(_ident_list_cleanup))) ident_list_t *
static loc_t start_loc;
static loc_t last_loc;
static const char *hint_str = NULL;
static int n_correct = 0;
static tokenq_t *tokenq;
static int tokenq_sz;
static int tokenq_head;
static int tokenq_tail;
static yylval_t last_lval;
static token_t opt_hist[8];
static int nopt_hist = 0;
static nametab_t *nametab = NULL;
static bool bootstrapping = false;
static tree_list_t pragmas = AINIT;
extern loc_t yylloc;
#define scan(...) _scan(1, __VA_ARGS__, -1)
#define expect(...) _expect(1, __VA_ARGS__, -1)
#define one_of(...) _one_of(1, __VA_ARGS__, -1)
#define not_at_token(...) ((peek() != tEOF) && !_scan(1, __VA_ARGS__, -1))
#define peek() peek_nth(1)
#define parse_error(loc, ...) do { \
if (n_correct >= RECOVER_THRESH) { \
error_at(loc, __VA_ARGS__); \
} \
} while (0)
#define RECOVER_THRESH 5
#define TRACE_PARSE 0
#define WARN_LOOKAHEAD 0
#define TRACE_RECOVERY 0
#define STD(x, y) (standard() >= (STD_##x) ? y : -1)
typedef void (*add_func_t)(tree_t, tree_t);
typedef struct {
const char *old_hint;
loc_t old_start_loc;
} state_t;
#if TRACE_PARSE
static int depth = 0;
static void _push_state(const state_t *s);
#else
#define _push_state(s)
#endif
#define EXTEND(s) \
__attribute__((cleanup(_pop_state), unused)) \
const state_t _state = { hint_str, start_loc }; \
hint_str = s; \
_push_state(&_state);
#define BEGIN_WITH_HEAD(s, t) \
EXTEND(s); \
start_loc = (t) ? *tree_loc(t) : LOC_INVALID; \
#define BEGIN(s) BEGIN_WITH_HEAD(s, NULL)
#define CURRENT_LOC _diff_loc(&start_loc, &last_loc)
static tree_t p_expression(void);
static tree_t p_sequential_statement(void);
static tree_t p_concurrent_statement(void);
static tree_t p_package_declaration(tree_t unit);
static tree_t p_package_body(tree_t unit);
static tree_t p_subprogram_declaration(tree_t spec);
static tree_t p_subprogram_body(tree_t spec);
static tree_t p_subprogram_specification(void);
static tree_t p_name(name_mask_t stop_mask);
static tree_t p_block_configuration(tree_t of);
static tree_t p_protected_type_body(ident_t id);
static type_t p_signature(void);
static type_t p_type_mark(void);
static tree_t p_function_call(ident_t id, tree_t prefix);
static tree_t p_resolution_indication(void);
static void p_conditional_waveforms(tree_t stmt, tree_t target, tree_t s0);
static void p_generic_map_aspect(tree_t inst, tree_t unit);
static ident_t p_designator(void);
static void p_interface_list(tree_t parent, tree_kind_t kind, bool ordered);
static void p_trailing_label(ident_t label);
static tree_t p_condition(void);
static type_t p_subtype_indication(void);
static tree_t p_record_constraint(type_t base);
static tree_t p_qualified_expression(tree_t prefix);
static tree_t p_concurrent_procedure_call_statement(ident_t label, tree_t name);
static tree_t p_subprogram_instantiation_declaration(void);
static tree_t p_record_element_constraint(type_t base);
static void p_selected_waveforms(tree_t stmt, tree_t target, tree_t reject);
static type_t p_index_subtype_definition(tree_t head);
static type_t p_anonymous_type_indication(void);
static void p_alias_declaration(tree_t parent);
static void p_variable_declaration(tree_t parent);
static void p_psl_declaration(tree_t parent);
static psl_node_t p_psl_sequence(void);
static psl_node_t p_psl_property(void);
static psl_node_t p_psl_sere(void);
static tree_t p_psl_directive(ident_t label);
static psl_node_t p_psl_repeated_sere(psl_node_t head);
static psl_node_t p_psl_braced_sere(void);
static psl_node_t p_hdl_expression(tree_t head, psl_type_t type);
static psl_node_t p_psl_builtin_function_call(void);
static bool consume(token_t tok);
static bool optional(token_t tok);
static void _pop_state(const state_t *s)
{
#if TRACE_PARSE
printf("%*s<-- %s\n", depth--, "", hint_str);
#endif
hint_str = s->old_hint;
if (s->old_start_loc.first_line != LINE_INVALID)
start_loc = s->old_start_loc;
}
#if TRACE_PARSE
static void _push_state(const state_t *s)
{
printf("%*s--> %s\n", depth++, "", hint_str);
}
#endif
static void skip_pragma(pragma_kind_t kind)
{
if (nametab == NULL)
warn_at(&yylloc, "ignoring pragma outside of design unit");
else {
tree_t p = tree_new(T_PRAGMA);
tree_set_loc(p, &yylloc);
tree_set_subkind(p, kind);
APUSH(pragmas, p);
}
}
static token_t wrapped_yylex(void)
{
for (;;) {
const token_t token = processed_yylex();
switch (token) {
case tSYNTHON:
skip_pragma(PRAGMA_SYNTHESIS_ON);
break;
case tSYNTHOFF:
skip_pragma(PRAGMA_SYNTHESIS_OFF);
break;
case tCOVERAGEON:
skip_pragma(PRAGMA_COVERAGE_ON);
break;
case tCOVERAGEOFF:
skip_pragma(PRAGMA_COVERAGE_OFF);
break;
case tTRANSLATEON:
skip_pragma(PRAGMA_TRANSLATE_ON);
break;
case tTRANSLATEOFF:
skip_pragma(PRAGMA_TRANSLATE_OFF);
break;
default:
return token;
}
}
}
static token_t peek_nth(int n)
{
while (((tokenq_head - tokenq_tail) & (tokenq_sz - 1)) < n) {
const token_t token = wrapped_yylex();
int next = (tokenq_head + 1) & (tokenq_sz - 1);
if (unlikely(next == tokenq_tail)) {
const int newsz = tokenq_sz * 2;
tokenq_t *new = xmalloc_array(newsz, sizeof(tokenq_t));
tokenq_t *p = new;
for (int i = tokenq_tail; i != tokenq_head;
i = (i + 1) & (tokenq_sz - 1))
*p++ = tokenq[i];
free(tokenq);
tokenq = new;
tokenq_sz = newsz;
tokenq_head = p - new;
tokenq_tail = 0;
next = (tokenq_head + 1) & (tokenq_sz - 1);
}
extern yylval_t yylval;
tokenq[tokenq_head].token = token;
tokenq[tokenq_head].lval = yylval;
tokenq[tokenq_head].loc = yylloc;
tokenq_head = next;
}
const int pos = (tokenq_tail + n - 1) & (tokenq_sz - 1);
return tokenq[pos].token;
}
static bool look_for(const look_params_t *params)
{
bool found = false;
token_t tok = -1;
int n, nest = 0;
for (n = 1; ;) {
tok = peek_nth(n++);
if ((tok == tEOF) || (tok == params->abort))
goto stop_looking;
else if (tok == params->nest_in)
nest++;
if (nest == params->depth) {
for (int i = 0; i < ARRAY_LEN(params->look); i++) {
if (tok == params->look[i]) {
found = true;
goto stop_looking;
}
}
if (params->lookfn != NULL && (*params->lookfn)(tok)) {
found = true;
goto stop_looking;
}
for (int i = 0; i < ARRAY_LEN(params->stop); i++) {
if (tok == params->stop[i])
goto stop_looking;
}
}
if (tok == params->nest_out)
nest--;
}
stop_looking:
#if WARN_LOOKAHEAD > 0
if (n >= WARN_LOOKAHEAD)
warn_at(&(tokenq[tokenq_tail].loc), "look ahead depth %d", n);
#endif
return found;
}
static void drop_token(void)
{
assert(tokenq_head != tokenq_tail);
if (start_loc.first_line == LINE_INVALID)
start_loc = tokenq[tokenq_tail].loc;
last_lval = tokenq[tokenq_tail].lval;
last_loc = tokenq[tokenq_tail].loc;
tokenq_tail = (tokenq_tail + 1) & (tokenq_sz - 1);
nopt_hist = 0;
}
static void drop_tokens_until(token_t tok)
{
token_t next = tEOF;
do {
free_token(tok, &last_lval);
next = peek();
drop_token();
} while ((tok != next) && (next != tEOF));
#if TRACE_RECOVERY
if (peek() != tEOF)
fmt_loc(stdout, &(tokenq[tokenq_tail].loc));
#endif
}
static void _vexpect(va_list ap)
{
if (n_correct >= RECOVER_THRESH) {
diag_t *d = diag_new(DIAG_ERROR, &(tokenq[tokenq_tail].loc));
diag_printf(d, "unexpected $yellow$%s$$ while parsing %s, expecting ",
token_str(peek()), hint_str);
bool first = true;
for (int i = 0; i < nopt_hist; i++) {
diag_printf(d, "%s$yellow$%s$$", i == 0 ? "one of " : ", ",
token_str(opt_hist[i]));
first = false;
}
int tok = va_arg(ap, int);
while (tok != -1) {
const int tmp = tok;
tok = va_arg(ap, int);
if (first && (tok != -1))
diag_printf(d, "one of ");
else if (!first)
diag_printf(d, (tok == -1) ? " or " : ", ");
diag_printf(d, "$yellow$%s$$", token_str(tmp));
first = false;
}
diag_hint(d, &(tokenq[tokenq_tail].loc), "this token was unexpected");
diag_emit(d);
}
n_correct = 0;
drop_token();
suppress_errors(nametab);
}
static void _expect(int dummy, ...)
{
va_list ap;
va_start(ap, dummy);
_vexpect(ap);
va_end(ap);
}
static bool consume(token_t tok)
{
const token_t got = peek();
if (tok != got) {
expect(tok);
return false;
}
else {
n_correct++;
drop_token();
return true;
}
}
static bool optional(token_t tok)
{
if (peek() == tok) {
consume(tok);
return true;
}
else {
if (nopt_hist < ARRAY_LEN(opt_hist))
opt_hist[nopt_hist++] = tok;
return false;
}
}
static bool _scan(int dummy, ...)
{
va_list ap;
va_start(ap, dummy);
token_t p = peek();
bool found = false;
while (!found) {
const int tok = va_arg(ap, token_t);
if (tok == -1)
break;
else if (p == tok)
found = true;
}
va_end(ap);
return found;
}
static int _one_of(int dummy, ...)
{
va_list ap;
va_start(ap, dummy);
token_t p = peek();
bool found = false;
while (!found) {
const int tok = va_arg(ap, token_t);
if (tok == -1)
break;
else if (p == tok)
found = true;
}
va_end(ap);
if (found) {
consume(p);
return p;
}
else {
va_start(ap, dummy);
_vexpect(ap);
va_end(ap);
return -1;
}
}
static const loc_t *_diff_loc(const loc_t *start, const loc_t *end)
{
static loc_t result;
result = get_loc(start->first_line,
start->first_column,
end->first_line + end->line_delta,
end->first_column + end->column_delta,
start->file_ref);
return &result;
}
static ident_t error_marker(void)
{
return well_known(W_ERROR);
}
static tree_t error_expr(void)
{
tree_t t = tree_new(T_REF);
tree_set_ident(t, error_marker());
tree_set_type(t, type_new(T_NONE));
return t;
}
static tree_t find_binding(tree_t inst)
{
ident_t name;
tree_t unit = NULL;
if (tree_kind(inst) == T_BINDING) {
name = tree_ident(inst);
if (tree_has_ident2(inst))
name = ident_prefix(name, tree_ident2(inst), '-');
}
else {
name = tree_ident2(inst);
if (tree_has_ref(inst))
unit = tree_ref(inst);
}
if (unit == NULL)
unit = resolve_name(nametab, tree_loc(inst), name);
if (unit == NULL)
return NULL;
const char *what = is_design_unit(unit) ? "design unit" : "object";
const tree_kind_t kind = tree_kind(unit);
switch (tree_class(inst)) {
case C_COMPONENT:
if (kind != T_COMPONENT) {
parse_error(tree_loc(inst), "%s %s is not a component declaration",
what, istr(name));
return NULL;
}
break;
case C_ENTITY:
if (kind != T_ENTITY && kind != T_ARCH) {
parse_error(tree_loc(inst), "%s %s is not an entity",
what, istr(name));
return NULL;
}
break;
case C_CONFIGURATION:
if (kind != T_CONFIGURATION) {
parse_error(tree_loc(inst), "%s %s is not a configuration",
what, istr(name));
return NULL;
}
break;
default:
break;
}
return unit;
}
static void set_label_and_loc(tree_t t, ident_t label, const loc_t *loc)
{
tree_set_loc(t, loc);
if (label == NULL)
label = get_implicit_label(t, nametab);
tree_set_ident(t, label);
}
static void require_std(vhdl_standard_t which, const char *feature)
{
static bool warned = false;
if (standard() < which && !warned) {
warned = true;
if (n_correct >= RECOVER_THRESH) {
diag_t *d = diag_new(DIAG_ERROR, CURRENT_LOC);
diag_printf(d, "%s %s not supported in VHDL-%s",
feature, feature[strlen(feature)-1] == 's' ? "are" : "is",
standard_text(standard()));
diag_hint(d, NULL, "pass $bold$--std=%s$$ to enable this feature",
standard_text(which));
diag_emit(d);
}
}
}
static tree_t bit_string_to_literal(const char *str, const loc_t *loc)
{
tree_t t = tree_new(T_STRING);
tree_set_loc(t, loc);
const char *p = str;
int length = -1;
if (isdigit_iso88591(*p)) {
require_std(STD_08, "bit string literals with length specifier");
unsigned long slength = strtoul(p, (char **)&p, 10);
if (slength > INT32_MAX) {
error_at(loc, "sorry, bit strings longer than %d elements are "
"not supported", INT32_MAX);
return t;
}
length = slength;
}
enum { UNSIGNED, SIGNED } mode = UNSIGNED;
switch (*p) {
case 'U': case 'u': mode = UNSIGNED; ++p; break;
case 'S': case 's': mode = SIGNED; ++p; break;
}
char base_ch = *p++;
int base;
switch (base_ch) {
case 'X': case 'x': base = 16; break;
case 'O': case 'o': base = 8; break;
case 'B': case 'b': base = 2; break;
case 'D': case 'd': base = 10; break;
default:
parse_error(loc, "invalid base '%c' for bit string", base_ch);
return t;
}
tree_t one = tree_new(T_REF);
tree_set_ident(one, ident_new("'1'"));
tree_set_loc(one, loc);
tree_t zero = tree_new(T_REF);
tree_set_ident(zero, ident_new("'0'"));
tree_set_loc(zero, loc);
if (base == 10) {
require_std(STD_08, "decimal bit string literals");
int ddigits = 0;
uint8_t *decimal LOCAL = xmalloc(strlen(str));
for (++p; *p != '\"'; p++) {
if (!isdigit_iso88591(*p)) {
parse_error(loc, "invalid digit '%c' in decimal bit string", *p);
return t;
}
else
decimal[ddigits++] = *p - '0';
}
const int maxbits = (length == -1 ? ddigits * 4 : length);
tree_t *bits LOCAL = xmalloc_array(maxbits, sizeof(tree_t));
int pos = maxbits - 1;
for (;;) {
bool all_zero = true;
int cout = 0;
for (int i = 0, cin = 0; i < ddigits; i++, cin = cout) {
all_zero &= (decimal[i] == 0);
cout = decimal[i] & 1;
decimal[i] >>= 1;
if (cin) decimal[i] += 5;
}
if (all_zero)
break;
else if (pos < 0 && cout) {
parse_error(CURRENT_LOC, "excess non-zero digits in "
"decimal bit string literal");
return t;
}
else if (pos >= 0)
bits[pos--] = cout ? one : zero;
}
if (length == -1)
length = maxbits - pos - 1;
for (int i = maxbits - length; i < maxbits; i++)
tree_add_char(t, i <= pos ? zero : bits[i]);
return t;
}
tree_t *bits LOCAL = NULL;
if (length >= 0)
bits = xmalloc_array(length, sizeof(tree_t));
tree_t pad = mode == UNSIGNED ? zero : NULL;
int nbits = 0;
for (++p; *p != '\"'; p++) {
const bool extended = (isdigit_iso88591(*p) && *p < '0' + base)
|| (base > 10 && *p >= 'A' && *p < 'A' + base - 10)
|| (base > 10 && *p >= 'a' && *p < 'a' + base - 10);
int n = (isdigit_iso88591(*p) ? (*p - '0')
: 10 + (isupper_iso88591(*p) ? (*p - 'A') : (*p - 'a')));
tree_t digit = NULL;
if (!extended) {
if (standard() < STD_08 || !isprint_iso88591(*p)) {
parse_error(loc, "invalid digit '%c' in bit string", *p);
return t;
}
else {
const char rune[] = { '\'', *p, '\'', '\0' };
digit = tree_new(T_REF);
tree_set_ident(digit, ident_new(rune));
tree_set_loc(digit, loc);
}
}
for (int d = (base >> 1); d > 0; n = n % d, d >>= 1) {
tree_t bit = extended ? ((n / d) ? one : zero) : digit;
if (pad == NULL) pad = bit;
if (length >= 0) {
tree_t left = nbits == 0 ? bit : bits[0];
if (nbits < length)
bits[nbits++] = bit;
else if (left != pad && tree_ident(left) != tree_ident(pad)) {
parse_error(CURRENT_LOC, "excess %s digits in bit "
"string literal",
mode == SIGNED ? "significant" : "non-zero");
return t;
}
else if (length > 0) {
for (int i = 0; i < length - 1; i++)
bits[i] = bits[i + 1];
bits[length - 1] = bit;
}
}
else
tree_add_char(t, bit);
}
}
if (pad == NULL && nbits < length)
parse_error(CURRENT_LOC, "signed bit string literal cannot be an "
"empty string");
else if (length >= 0) {
// Left-pad with sign bit or zero
int pos = 0;
for (; pos < length - nbits; pos++)
tree_add_char(t, pad);
for (int i = 0; pos < length; pos++, i++)
tree_add_char(t, bits[i]);
}
return t;
}
static tree_t get_time(int64_t fs, const loc_t *loc)
{
tree_t lit = tree_new(T_LITERAL);
tree_set_subkind(lit, L_INT);
tree_set_ival(lit, fs);
tree_set_loc(lit, loc);
tree_set_type(lit, std_type(NULL, STD_TIME));
return lit;
}
static void set_delay_mechanism(tree_t t, tree_t reject)
{
if (reject == NULL) {
// Inertial delay with same value as waveform
// LRM 93 section 8.4 the rejection limit in this case is
// specified by the time expression of the first waveform
tree_t assign = (tree_kind(t) == T_COND_ASSIGN ? tree_cond(t, 0) : t);
if (tree_waveforms(assign) == 0)
return;
tree_t w = tree_waveform(assign, 0);
if (tree_has_delay(w))
tree_set_reject(t, tree_delay(w));
}
else {
tree_set_reject(t, reject);
solve_types(nametab, reject, std_type(NULL, STD_TIME));
}
}
static tree_t add_port(tree_t d, const char *name, type_t type,
port_mode_t mode, tree_t def)
{
type_t ftype = tree_type(d);
tree_t port = tree_new(T_PARAM_DECL);
tree_set_ident(port, ident_new(name));
tree_set_loc(port, tree_loc(d));
tree_set_type(port, type);
tree_set_subkind(port, mode);
if (def != NULL)
tree_set_value(port, def);
if (type_is_file(type))
tree_set_class(port, C_FILE);
tree_add_port(d, port);
type_add_param(ftype, type);
return port;
}
static tree_t builtin_proc(ident_t name, subprogram_kind_t kind, ...)
{
type_t f = type_new(T_SIGNATURE);
type_set_ident(f, name);
tree_t d = tree_new(T_PROC_DECL);
tree_set_ident(d, name);
tree_set_type(d, f);
tree_set_subkind(d, kind);
tree_set_flag(d, TREE_F_NEVER_WAITS);
tree_set_flag(d, TREE_F_PREDEFINED);
tree_set_loc(d, CURRENT_LOC);
return d;
}
static tree_t builtin_fn(ident_t name, type_t result,
subprogram_kind_t kind, ...)
{
type_t f = type_new(T_SIGNATURE);
type_set_ident(f, name);
type_set_result(f, result);
tree_t d = tree_new(T_FUNC_DECL);
tree_set_ident(d, name);
tree_set_type(d, f);
tree_set_subkind(d, kind);
va_list ap;
va_start(ap, kind);
char *argname;
while ((argname = va_arg(ap, char*))) {
type_t type = va_arg(ap, type_t);
assert(type != NULL);
add_port(d, argname, type, PORT_IN, NULL);
}
va_end(ap);
tree_set_flag(d, TREE_F_PREDEFINED);
tree_set_loc(d, CURRENT_LOC);
return d;
}
static void declare_binary(tree_t container, ident_t name, type_t lhs,
type_t rhs, type_t result, subprogram_kind_t kind)
{
tree_t d = builtin_fn(name, result, kind, "L", lhs, "R", rhs, NULL);
mangle_func(nametab, d);
insert_name(nametab, d, NULL);
tree_add_decl(container, d);
}
static void declare_unary(tree_t container, ident_t name, type_t operand,
type_t result, subprogram_kind_t kind)
{
tree_t d = builtin_fn(name, result, kind, "VALUE", operand, NULL);
mangle_func(nametab, d);
insert_name(nametab, d, NULL);
tree_add_decl(container, d);
}
static bool is_bit_or_std_ulogic(type_t type)
{
if (!type_is_enum(type))
return false;
ident_t name = type_ident(type);
return name == well_known(W_STD_BIT) || name == well_known(W_IEEE_ULOGIC);
}
static type_t get_element_subtype(type_t type)
{
type_t elem = type_elem(type);
if (is_anonymous_subtype(elem)) {
// Create a distinct subtype to ensure any errors are only
// reported once
type_t sub = type_new(T_SUBTYPE);
type_set_base(sub, elem);
return sub;
}
else
return elem;
}
static void declare_predefined_ops(tree_t container, type_t t)
{
// Prefined operators are defined in LRM 93 section 7.2
ident_t mult = well_known(W_OP_TIMES);
ident_t div = well_known(W_OP_DIVIDE);
ident_t plus = well_known(W_OP_ADD);
ident_t minus = well_known(W_OP_MINUS);
ident_t cmp_lt = well_known(W_OP_LESS_THAN);
ident_t cmp_le = well_known(W_OP_LESS_EQUAL);
ident_t cmp_gt = well_known(W_OP_GREATER_THAN);
ident_t cmp_ge = well_known(W_OP_GREATER_EQUAL);
ident_t eq = well_known(W_OP_EQUAL);
ident_t neq = well_known(W_OP_NOT_EQUAL);
ident_t min_i = NULL, max_i = NULL;
if (standard() >= STD_08) {
min_i = ident_new("MINIMUM");
max_i = ident_new("MAXIMUM");
}
// Predefined operators
tree_t std = find_std(nametab);
type_t std_bool = std_type(std, STD_BOOLEAN);
type_t std_int = NULL;
type_t std_real = NULL;
type_t std_uint = NULL;
type_kind_t kind = type_kind(t);
switch (kind) {
case T_SUBTYPE:
// Use operators of base type
break;
case T_FILE:
// No predefined operators
break;
case T_ARRAY:
// Operators on arrays
declare_binary(container, eq, t, t, std_bool, S_ARRAY_EQ);
declare_binary(container, neq, t, t, std_bool, S_ARRAY_NEQ);
if (dimension_of(t) == 1) {
type_t elem = get_element_subtype(t);
const bool scalar_elem = type_is_scalar(elem);
const bool ordered =
standard() >= STD_19 ? scalar_elem : type_is_discrete(elem);
if (ordered) {
declare_binary(container, cmp_lt, t, t, std_bool, S_ARRAY_LT);
declare_binary(container, cmp_le, t, t, std_bool, S_ARRAY_LE);
declare_binary(container, cmp_gt, t, t, std_bool, S_ARRAY_GT);
declare_binary(container, cmp_ge, t, t, std_bool, S_ARRAY_GE);
}
ident_t concat = ident_new("\"&\"");
declare_binary(container, concat, t, t, t, S_CONCAT);
declare_binary(container, concat, t, elem, t, S_CONCAT);
declare_binary(container, concat, elem, t, t, S_CONCAT);
declare_binary(container, concat, elem, elem, t, S_CONCAT);
if (standard() >= STD_08) {
if (ordered) {
declare_binary(container, min_i, t, t, t, S_MINIMUM);
declare_binary(container, max_i, t, t, t, S_MAXIMUM);
}
if (scalar_elem) {
declare_unary(container, min_i, t, elem, S_MINIMUM);
declare_unary(container, max_i, t, elem, S_MAXIMUM);
}
}
}
break;
case T_RECORD:
// Operators on records
declare_binary(container, eq, t, t, std_bool, S_RECORD_EQ);
declare_binary(container, neq, t, t, std_bool, S_RECORD_NEQ);
break;
case T_PHYSICAL:
std_int = std_type(std, STD_INTEGER);
std_real = std_type(std, STD_REAL);
std_uint = std_type(std, STD_UNIVERSAL_INTEGER);
// Multiplication
declare_binary(container, mult, t, std_int, t, S_MUL_PI);
declare_binary(container, mult, t, std_real, t, S_MUL_PR);
declare_binary(container, mult, std_int, t, t, S_MUL_IP);
declare_binary(container, mult, std_real, t, t, S_MUL_RP);
// Division
declare_binary(container, div, t, std_int, t, S_DIV_PI);
declare_binary(container, div, t, std_real, t, S_DIV_PR);
declare_binary(container, div, t, t, std_uint, S_DIV_PP);
// Addition
declare_binary(container, plus, t, t, t, S_ADD);
// Subtraction
declare_binary(container, minus, t, t, t, S_SUB);
// Sign operators
declare_unary(container, plus, t, t, S_IDENTITY);
declare_unary(container, minus, t, t, S_NEGATE);
// Comparison
declare_binary(container, cmp_lt, t, t, std_bool, S_SCALAR_LT);