/
sem.c
7103 lines (5720 loc) · 196 KB
/
sem.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) 2011-2015 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 "phase.h"
#include "util.h"
#include "common.h"
#include "hash.h"
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef struct scope scope_t;
typedef struct loop_stack loop_stack_t;
typedef struct type_set type_set_t;
typedef struct defer_check defer_check_t;
typedef struct import_list import_list_t;
typedef bool (*defer_fn_t)(tree_t t);
struct defer_check {
defer_check_t *next;
defer_fn_t fn;
tree_t arg;
};
struct import_list {
ident_t name;
bool all;
import_list_t *next;
};
typedef enum {
SCOPE_PACKAGE = (1 << 0),
SCOPE_FORMAL = (1 << 1),
SCOPE_PROTECTED = (1 << 2),
SCOPE_CONTEXT = (1 << 3)
} scope_flags_t;
struct scope {
scope_t *down;
defer_check_t *deferred;
hash_t *decls;
tree_t subprog;
wait_level_t wait_level;
impure_io_t impure_io;
// For design unit scopes
ident_t prefix;
import_list_t *imported;
scope_flags_t flags;
};
struct loop_stack {
loop_stack_t *up;
ident_t name;
};
struct type_set {
type_t *members;
unsigned n_members;
unsigned alloc;
type_set_t *down;
};
typedef struct {
const loc_t *loc;
lib_t lib;
bool error;
} lib_walk_params_t;
typedef struct {
tree_t decl;
bool have;
bool partial;
} formal_map_t;
typedef tree_t (*get_fn_t)(tree_t);
typedef void (*set_fn_t)(tree_t, tree_t);
typedef tree_t (*get_nth_fn_t)(tree_t, unsigned);
static bool sem_check_constrained(tree_t t, type_t type);
static bool sem_check_array_ref(tree_t t);
static bool sem_declare(tree_t decl, bool add_predefined);
static bool sem_locally_static(tree_t t);
static bool sem_globally_static(tree_t t);
static tree_t sem_check_lvalue(tree_t t);
static bool sem_check_type(tree_t t, type_t *ptype);
static bool sem_static_name(tree_t t);
static bool sem_check_range(range_t *r, type_t context);
static bool sem_check_attr_ref(tree_t t, bool allow_range);
static type_t sem_implicit_dereference(tree_t t, get_fn_t get, set_fn_t set);
static void scope_insert_alias(tree_t t, ident_t name);
static bool scope_import_unit(ident_t unit_name, lib_t lib,
bool all, const loc_t *loc);
static int sem_ambiguous_rate(tree_t t);
static scope_t *top_scope = NULL;
static int errors = 0;
static unsigned relax = -1;
static type_set_t *top_type_set = NULL;
static loop_stack_t *loop_stack = NULL;
#define sem_error(t, ...) do { \
error_at(t ? tree_loc(t) : NULL , __VA_ARGS__); \
errors++; \
return false; \
} while (0)
static void scope_push(ident_t prefix)
{
scope_t *s = xmalloc(sizeof(scope_t));
s->decls = hash_new(1024, false);
s->prefix = prefix;
s->imported = NULL;
s->down = top_scope;
s->subprog = (top_scope ? top_scope->subprog : NULL) ;
s->flags = (top_scope ? top_scope->flags : 0);
s->deferred = NULL;
s->wait_level = WAITS_NO;
s->impure_io = 0;
top_scope = s;
}
static void scope_pop(void)
{
assert(top_scope != NULL);
assert(top_scope->deferred == NULL);
while (top_scope->imported != NULL) {
import_list_t *tmp = top_scope->imported->next;
free(top_scope->imported);
top_scope->imported = tmp;
}
hash_free(top_scope->decls);
scope_t *s = top_scope;
if (s->down != NULL && s->down->subprog == s->subprog) {
s->down->wait_level |= s->wait_level;
s->down->impure_io |= s->down->impure_io;
}
top_scope = s->down;
free(s);
}
static void scope_defer_check(defer_fn_t fn, tree_t arg)
{
assert(top_scope != NULL);
defer_check_t *d = xmalloc(sizeof(defer_check_t));
d->next = NULL;
d->fn = fn;
d->arg = arg;
if (top_scope->deferred == NULL)
top_scope->deferred = d;
else {
defer_check_t *where = top_scope->deferred;
while (where->next != NULL)
where = where->next;
where->next = d;
}
}
static bool scope_run_deferred_checks(void)
{
assert(top_scope != NULL);
bool result = true;
defer_check_t *it = top_scope->deferred;
while (it != NULL) {
defer_check_t *tmp = it->next;
result = (*it->fn)(it->arg) && result;
free(it);
it = tmp;
}
top_scope->deferred = NULL;
return result;
}
static void scope_apply_prefix(tree_t t)
{
if (top_scope->prefix)
tree_set_ident(t, ident_prefix(top_scope->prefix,
tree_ident(t), '.'));
}
static scope_t *scope_containing(scope_t *s, tree_t decl)
{
int k = 0, tmp;
tree_t next;
ident_t name = tree_ident(decl);
while (tmp = k++, (next = hash_get_nth(s->decls, name, &tmp))) {
if (next == decl)
return s;
}
return s->down == NULL ? NULL : scope_containing(s->down, decl);
}
static tree_t scope_find_in(ident_t i, scope_t *s, bool recur, int k)
{
if (s == NULL)
return NULL;
else {
void *value = hash_get_nth(s->decls, i, &k);
if (value != NULL)
return (tree_t)value;
else
return (recur ? scope_find_in(i, s->down, true, k) : NULL);
}
}
static tree_t scope_find(ident_t i)
{
return scope_find_in(i, top_scope, true, 0);
}
static tree_t scope_find_nth(ident_t i, int n)
{
return scope_find_in(i, top_scope, true, n);
}
static bool scope_walk(hash_iter_t *now, tree_t *decl)
{
const void *key;
void *value;
while (hash_iter(top_scope->decls, now, &key, &value)) {
*decl = value;
if (tree_ident(*decl) != key)
continue; // Skip aliases
else
return true;
}
return false;
}
static bool scope_can_overload(tree_t t)
{
const tree_kind_t kind = tree_kind(t);
return kind == T_ENUM_LIT
|| kind == T_FUNC_DECL
|| kind == T_FUNC_BODY
|| kind == T_PROC_DECL
|| kind == T_PROC_BODY
|| (kind == T_ALIAS && type_is_subprogram(tree_type(t)));
}
static void scope_insert_fields(type_t type, ident_t prefix)
{
// When adding an object R with record type to the scope make all its
// fields visible as R.X, R.Y, etc.
if (type_kind(type) == T_FUNC)
type = type_result(type);
switch (type_base_kind(type)) {
case T_RECORD:
{
// Insert a name into the scope for each field
const int nfields = type_fields(type);
for (int i = 0; i < nfields; i++) {
tree_t field = type_field(type, i);
ident_t qual = ident_prefix(prefix, tree_ident(field), '.');
scope_insert_alias(field, qual);
type_t field_type = tree_type(field);
if (type_is_record(field_type))
scope_insert_fields(field_type, qual);
}
}
break;
case T_ACCESS:
{
type_t deref_type = type_access(type);
if (type_is_record(deref_type)) {
// Pointers to records can be dereferenced implicitly
scope_insert_fields(deref_type, prefix);
}
}
break;
case T_PROTECTED:
{
// Make protected methods visible
const int ndecls = type_decls(type);
for (int i = 0; i < ndecls; i++) {
tree_t decl = type_decl(type, i);
if (tree_kind(decl) == T_USE)
continue;
ident_t suffix = ident_rfrom(tree_ident(decl), '.');
ident_t qual = ident_prefix(prefix, suffix, '.');
scope_insert_alias(decl, qual);
}
}
default:
break;
}
}
static bool scope_insert_hiding(tree_t t, ident_t name, bool overload)
{
assert(top_scope != NULL);
tree_t existing;
int n = 0;
do {
if ((existing = scope_find_in(name, top_scope, false, n++))) {
if (!overload)
sem_error(t, "%s already declared in this region", istr(name));
if (tree_kind(existing) == T_UNIT_DECL)
continue;
const bool builtin = (tree_attr_str(existing, builtin_i) != NULL);
if (builtin && type_eq(tree_type(t), tree_type(existing))) {
type_t arg0_type = type_param(tree_type(existing), 0);
ident_t t_region = ident_runtil(tree_ident(t), '.');
ident_t e_region = ident_runtil(type_ident(arg0_type), '.');
const bool same_region = (t_region == e_region);
// Allow builtin functions to be hidden by explicit functins
// declared in the same region
if (same_region) {
hash_replace(top_scope->decls, existing, t);
return true;
}
}
}
} while (existing != NULL);
hash_put(top_scope->decls, name, t);
const tree_kind_t kind = tree_kind(t);
const bool may_have_fields =
kind == T_VAR_DECL
|| kind == T_CONST_DECL
|| kind == T_SIGNAL_DECL
|| kind == T_PORT_DECL
|| kind == T_FUNC_BODY
|| kind == T_FUNC_DECL;
if (may_have_fields)
scope_insert_fields(tree_type(t), name);
return true;
}
static bool scope_insert(tree_t t)
{
return scope_insert_hiding(t, tree_ident(t), scope_can_overload(t));
}
static void scope_insert_alias(tree_t t, ident_t name)
{
(void)scope_insert_hiding(t, name, true);
}
static void scope_replace(tree_t t, tree_t with)
{
assert(top_scope != NULL);
hash_replace(top_scope->decls, t, with);
}
static void loop_push(ident_t name)
{
loop_stack_t *ls = xmalloc(sizeof(loop_stack_t));
ls->up = loop_stack;
ls->name = name;
loop_stack = ls;
}
static void loop_pop(void)
{
loop_stack_t *tmp = loop_stack->up;
free(loop_stack);
loop_stack = tmp;
}
static const char *sem_type_minify(const char *name)
{
// If name without its fully qualified prefix is unique in the
// current scope then drop the prefix to make messages easier
// to read.
// E.g. IEEE.STD_LOGIC_1164.STD_LOGIC -> STD_LOGIC if no other
// STD_LOGIC type is visible
const char *suffix = strrchr(name, '.');
if (suffix == NULL)
return name;
ident_t suffix_i = ident_new(suffix + 1);
int matches = 0;
for (scope_t *s = top_scope; s != NULL; s = s->down) {
for (import_list_t *i = s->imported; i != NULL; i = i->next) {
ident_t search = ident_prefix(i->name, suffix_i, '.');
if (scope_find_in(search, s, false, 0) != NULL)
matches++;
}
}
return (matches == 1) ? suffix + 1 : name;
}
static const char *sem_type_str(type_t type)
{
return type_pp_minify(type, sem_type_minify);
}
static void scope_append_import_list(ident_t name, bool all)
{
import_list_t *new = xmalloc(sizeof(import_list_t));
new->name = name;
new->all = all;
new->next = top_scope->imported;
top_scope->imported = new;
}
static void scope_walk_lib(ident_t name, int kind, void *context)
{
lib_walk_params_t *params = context;
if (kind == T_PACKAGE) {
if (scope_import_unit(name, params->lib, false, params->loc))
params->error = true;
}
}
static bool scope_import_use_clause(tree_t c, bool search)
{
ident_t cname = tree_ident(c);
const bool all = tree_has_ident2(c) && (tree_ident2(c) == all_i);
ident_t lname = ident_until(cname, '.');
lib_t lib = lib_find(istr(lname), search, search);
if (lib != NULL) {
if (lname == cname) {
assert(all);
lib_walk_params_t params = {
.loc = tree_loc(c),
.lib = lib,
.error = false
};
lib_walk_index(lib, scope_walk_lib, ¶ms);
return params.error;
}
else if (scope_import_unit(cname, lib, all, tree_loc(c))) {
if (tree_has_ident2(c) && !all) {
ident_t full = ident_prefix(tree_ident(c), tree_ident2(c), '.');
int n = 0;
tree_t object;
while ((object = scope_find_nth(full, n++)))
scope_insert_alias(object, tree_ident2(c));
if (n == 1)
sem_error(c, "declaration %s not found in unit %s",
istr(tree_ident2(c)), istr(cname));
}
return true;
}
else
return false;
}
else
sem_error(c, "missing library clause for %s", istr(lname));
}
static bool scope_import_decls(tree_t unit, bool unqual_only, bool all)
{
lib_t work = lib_work();
ident_t work_name = lib_name(work);
ident_t unit_lib = ident_until(tree_ident(unit), '.');
const bool work_alias =
unit_lib != NULL
&& unit_lib == work_name
&& work_name != work_i;
const int ndecls = tree_decls(unit);
for (int n = 0; n < ndecls; n++) {
tree_t decl = tree_decl(unit, n);
tree_kind_t kind = tree_kind(decl);
if (kind == T_ATTR_SPEC)
continue;
else if (kind == T_USE) {
if (!scope_import_use_clause(decl, true))
return false;
continue;
}
ident_t dname = tree_ident(decl);
// Make unqualified and package qualified names visible
if (!unqual_only) {
if (!sem_declare(decl, true))
return false;
ident_t pqual = ident_from(dname, '.');
if (pqual != NULL)
scope_insert_alias(decl, pqual);
}
if (all) {
ident_t unqual = ident_rfrom(dname, '.');
if (unqual != NULL)
scope_insert_alias(decl, unqual);
}
if (work_alias) {
ident_t alias = ident_prefix(work_i, ident_from(dname, '.'), '.');
scope_insert_alias(decl, alias);
}
}
scope_append_import_list(tree_ident(unit), all);
return true;
}
static bool scope_import_unit(ident_t unit_name, lib_t lib,
bool all, const loc_t *loc)
{
// Check we haven't already imported this
bool unqual_only = false;
for (scope_t *s = top_scope; s != NULL; s = s->down) {
import_list_t *it;
for (it = s->imported; it != NULL; it = it->next) {
if (it->name == unit_name) {
if (it->all || !all)
return true;
else {
unqual_only = true;
break;
}
}
}
}
tree_t unit = lib_get_check_stale(lib, unit_name);
if (unit == NULL) {
error_at(loc, "unit %s not found in library %s",
istr(unit_name), istr(lib_name(lib)));
++errors;
return false;
}
if (!scope_import_decls(unit, unqual_only, all))
return false;
if (unit_name != tree_ident(unit))
scope_append_import_list(unit_name, all);
return true;
}
static void type_set_push(void)
{
type_set_t *t = xmalloc(sizeof(type_set_t));
t->n_members = 0;
t->alloc = 32;
t->members = xmalloc(t->alloc * sizeof(type_t));
t->down = top_type_set;
top_type_set = t;
}
static void type_set_pop(void)
{
assert(top_type_set != NULL);
type_set_t *old = top_type_set;
top_type_set = old->down;
free(old->members);
free(old);
}
static void type_set_add(type_t t)
{
assert(top_type_set != NULL);
assert(t != NULL);
assert(type_kind(t) != T_UNRESOLVED);
for (unsigned i = 0; i < top_type_set->n_members; i++) {
if (type_eq(top_type_set->members[i], t))
return;
}
ARRAY_APPEND(top_type_set->members, t, top_type_set->n_members,
top_type_set->alloc);
}
static bool type_set_restrict(bool (*pred)(type_t))
{
if (top_type_set == NULL)
return false;
int j = 0;
for (int i = 0; i < top_type_set->n_members; i++) {
type_t type = top_type_set->members[i];
if ((*pred)(type))
top_type_set->members[j++] = type;
}
top_type_set->n_members = j;
return j > 0;
}
static bool type_set_uniq(type_t *pt)
{
assert(top_type_set != NULL);
if (top_type_set->n_members == 1) {
*pt = top_type_set->members[0];
return true;
}
else {
*pt = NULL;
return false;
}
}
static bool type_set_any(type_t *pt)
{
if ((top_type_set == NULL) || (top_type_set->n_members == 0))
return false;
else {
*pt = top_type_set->members[0];
return true;
}
}
static text_buf_t *type_set_fmt(void)
{
text_buf_t *tb = tb_new();
if (top_type_set != NULL && top_type_set->n_members > 0) {
tb_printf(tb, " (");
for (unsigned n = 0; n < top_type_set->n_members; n++) {
tb_printf(tb, "%s%s", n > 0 ? ", " : "",
sem_type_str(top_type_set->members[n]));
}
tb_printf(tb, ")");
}
return tb;
}
static bool type_set_member(type_t t)
{
if (top_type_set == NULL || top_type_set->n_members == 0)
return true;
for (unsigned n = 0; n < top_type_set->n_members; n++) {
if (type_eq(top_type_set->members[n], t))
return true;
}
return false;
}
static type_t sem_std_type(const char *name)
{
ident_t name_i = ident_new(name);
ident_t qual = ident_prefix(std_standard_i, name_i, '.');
tree_t decl = scope_find(qual);
if (decl == NULL)
fatal("cannot find %s type", istr(qual));
return tree_type(decl);
}
static tree_t sem_add_port(tree_t d, type_t type, port_mode_t mode, tree_t def)
{
type_t ftype = tree_type(d);
char *argname LOCAL = xasprintf("_arg%d", type_params(ftype));
tree_t port = tree_new(T_PORT_DECL);
tree_set_ident(port, ident_new(argname));
tree_set_type(port, type);
tree_set_subkind(port, mode);
if (def != NULL)
tree_set_value(port, def);
tree_add_port(d, port);
type_add_param(ftype, type);
return port;
}
static tree_t sem_builtin_proc(ident_t name, const char *builtin, ...)
{
type_t f = type_new(T_PROC);
type_set_ident(f, name);
tree_t d = tree_new(T_PROC_DECL);
tree_set_ident(d, name);
tree_set_type(d, f);
tree_add_attr_str(d, builtin_i, ident_new(builtin));
return d;
}
static tree_t sem_builtin_fn(ident_t name, type_t result,
const char *builtin, ...)
{
type_t f = type_new(T_FUNC);
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_add_attr_str(d, builtin_i, ident_new(builtin));
va_list ap;
va_start(ap, builtin);
type_t arg;
while ((arg = va_arg(ap, type_t)))
sem_add_port(d, arg, PORT_IN, NULL);
va_end(ap);
return d;
}
static void sem_declare_binary(tree_t decl, ident_t name, type_t lhs,
type_t rhs, type_t result, const char *builtin)
{
tree_t d = sem_builtin_fn(name, result, builtin, lhs, rhs, NULL);
scope_insert(d);
if (decl != NULL)
tree_add_op(decl, d);
}
static void sem_declare_unary(tree_t decl, ident_t name, type_t operand,
type_t result, const char *builtin)
{
tree_t d = sem_builtin_fn(name, result, builtin, operand, NULL);
scope_insert(d);
if (decl != NULL)
tree_add_op(decl, d);
}
static tree_t sem_int_lit(type_t type, int64_t i)
{
tree_t f = tree_new(T_LITERAL);
tree_set_subkind(f, L_INT);
tree_set_ival(f, i);
tree_set_type(f, type);
return f;
}
static void sem_declare_predefined_ops(tree_t decl)
{
// Prefined operators are defined in LRM 93 section 7.2
type_t t = tree_type(decl);
if (type_kind(t) == T_CARRAY) {
// Construct an unconstrained array type for parameters
type_t u = type_new(T_UARRAY);
type_set_ident(u, type_ident(t));
type_set_elem(u, type_elem(t));
const int ndims = type_dims(t);
for (int i = 0; i < ndims; i++)
type_add_index_constr(u, tree_type(type_dim(t, i).left));
t = u;
}
ident_t mult = ident_new("\"*\"");
ident_t div = ident_new("\"/\"");
ident_t plus = ident_new("\"+\"");
ident_t minus = ident_new("\"-\"");
// Predefined operators
type_t std_bool = sem_std_type("BOOLEAN");
type_t std_int = sem_std_type("INTEGER");
type_t std_real = sem_std_type("REAL");
type_t std_string = sem_std_type("STRING");
type_kind_t kind = type_kind(t);
assert(kind != T_UNRESOLVED);
switch (kind) {
case T_SUBTYPE:
// Use operators of base type
break;
case T_CARRAY:
case T_UARRAY:
// Operators on arrays
sem_declare_binary(decl, ident_new("\"=\""), t, t, std_bool, "aeq");
sem_declare_binary(decl, ident_new("\"/=\""), t, t, std_bool, "aneq");
if (array_dimension(t) == 1) {
sem_declare_binary(decl, ident_new("\"<\""), t, t, std_bool, "alt");
sem_declare_binary(decl, ident_new("\"<=\""), t, t, std_bool, "aleq");
sem_declare_binary(decl, ident_new("\">\""), t, t, std_bool, "agt");
sem_declare_binary(decl, ident_new("\">=\""), t, t, std_bool, "ageq");
}
break;
case T_RECORD:
// Operators on records
sem_declare_binary(decl, ident_new("\"=\""), t, t, std_bool, "req");
sem_declare_binary(decl, ident_new("\"/=\""), t, t, std_bool, "rneq");
break;
case T_PHYSICAL:
// Multiplication
sem_declare_binary(decl, mult, t, std_int, t, "mul");
sem_declare_binary(decl, mult, t, std_real, t, "mulpr");
sem_declare_binary(decl, mult, std_int, t, t, "mul");
sem_declare_binary(decl, mult, std_real, t, t, "mulrp");
// Division
sem_declare_binary(decl, div, t, std_int, t, "div");
sem_declare_binary(decl, div, t, std_real, t, "divpr");
sem_declare_binary(decl, div, t, t, std_int, "div");
// Addition
sem_declare_binary(decl, plus, t, t, t, "add");
// Subtraction
sem_declare_binary(decl, minus, t, t, t, "sub");
// Sign operators
sem_declare_unary(decl, plus, t, t, "identity");
sem_declare_unary(decl, minus, t, t, "neg");
// Comparison
sem_declare_binary(decl, ident_new("\"<\""), t, t, std_bool, "lt");
sem_declare_binary(decl, ident_new("\"<=\""), t, t, std_bool, "leq");
sem_declare_binary(decl, ident_new("\">\""), t, t, std_bool, "gt");
sem_declare_binary(decl, ident_new("\">=\""), t, t, std_bool, "geq");
// Equality
sem_declare_binary(decl, ident_new("\"=\""), t, t, std_bool, "eq");
sem_declare_binary(decl, ident_new("\"/=\""), t, t, std_bool, "neq");
// Absolute value
sem_declare_unary(decl, ident_new("\"abs\""), t, t, "abs");
break;
case T_INTEGER:
// Modulus
sem_declare_binary(decl, ident_new("\"mod\""), t, t, t, "mod");
// Remainder
sem_declare_binary(decl, ident_new("\"rem\""), t, t, t, "rem");
// Fall-through
case T_REAL:
// Addition
sem_declare_binary(decl, plus, t, t, t, "add");
// Subtraction
sem_declare_binary(decl, minus, t, t, t, "sub");
// Multiplication
sem_declare_binary(decl, mult, t, t, t, "mul");
// Division
sem_declare_binary(decl, div, t, t, t, "div");
// Sign operators
sem_declare_unary(decl, plus, t, t, "identity");
sem_declare_unary(decl, minus, t, t, "neg");
// Exponentiation
sem_declare_binary(decl, ident_new("\"**\""), t, std_int, t, "exp");
// Absolute value
sem_declare_unary(decl, ident_new("\"abs\""), t, t, "abs");
// Fall-through
case T_ENUM:
sem_declare_binary(decl, ident_new("\"<\""), t, t, std_bool, "lt");
sem_declare_binary(decl, ident_new("\"<=\""), t, t, std_bool, "leq");
sem_declare_binary(decl, ident_new("\">\""), t, t, std_bool, "gt");
sem_declare_binary(decl, ident_new("\">=\""), t, t, std_bool, "geq");
// Fall-through
default:
sem_declare_binary(decl, ident_new("\"=\""), t, t, std_bool, "eq");
sem_declare_binary(decl, ident_new("\"/=\""), t, t, std_bool, "neq");
break;
}
// Logical operators
ident_t boolean_i = ident_new("STD.STANDARD.BOOLEAN");
ident_t bit_i = ident_new("STD.STANDARD.BIT");
bool logical = (type_ident(t) == boolean_i || type_ident(t) == bit_i);
if (logical) {
sem_declare_binary(decl, ident_new("\"and\""), t, t, t, "and");
sem_declare_binary(decl, ident_new("\"or\""), t, t, t, "or");
sem_declare_binary(decl, ident_new("\"xor\""), t, t, t, "xor");
sem_declare_binary(decl, ident_new("\"nand\""), t, t, t, "nand");
sem_declare_binary(decl, ident_new("\"nor\""), t, t, t, "nor");
sem_declare_binary(decl, ident_new("\"xnor\""), t, t, t, "xnor");
sem_declare_unary(decl, ident_new("\"not\""), t, t, "not");
}
bool vec_logical = false;
if (kind == T_CARRAY || kind == T_UARRAY) {
type_t base = type_elem(t);
vec_logical = (type_ident(base) == boolean_i
|| type_ident(base) == bit_i);
}
if (vec_logical) {
sem_declare_binary(decl, ident_new("\"and\""), t, t, t, "v_and");
sem_declare_binary(decl, ident_new("\"or\""), t, t, t, "v_or");
sem_declare_binary(decl, ident_new("\"xor\""), t, t, t, "v_xor");
sem_declare_binary(decl, ident_new("\"nand\""), t, t, t, "v_nand");
sem_declare_binary(decl, ident_new("\"nor\""), t, t, t, "v_nor");
sem_declare_binary(decl, ident_new("\"xnor\""), t, t, t, "v_xnor");
sem_declare_unary(decl, ident_new("\"not\""), t, t, "v_not");
sem_declare_binary(decl, ident_new("\"sll\""), t, std_int, t, "sll");
sem_declare_binary(decl, ident_new("\"srl\""), t, std_int, t, "srl");
sem_declare_binary(decl, ident_new("\"sla\""), t, std_int, t, "sla");
sem_declare_binary(decl, ident_new("\"sra\""), t, std_int, t, "sra");
sem_declare_binary(decl, ident_new("\"rol\""), t, std_int, t, "rol");
sem_declare_binary(decl, ident_new("\"ror\""), t, std_int, t, "ror");
}
// Predefined procedures
switch (kind) {
case T_FILE:
{
tree_t read_mode = scope_find(ident_new("READ_MODE"));
assert(read_mode != NULL);
// The underlying type of the file may not have been checked yet
type_t of = type_file(t);
tree_t type_decl = scope_find(type_ident(of));
if (type_decl == NULL)
break; // Will generate error later
of = tree_type(type_decl);
ident_t file_open_i = ident_new("FILE_OPEN");
ident_t file_close_i = ident_new("FILE_CLOSE");
ident_t read_i = ident_new("READ");
ident_t write_i = ident_new("WRITE");
ident_t endfile_i = ident_new("ENDFILE");
type_t open_kind = sem_std_type("FILE_OPEN_KIND");
type_t open_status = sem_std_type("FILE_OPEN_STATUS");
tree_t file_open1 = sem_builtin_proc(file_open_i, "file_open1");