This repository has been archived by the owner on Dec 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
symbols.c
1467 lines (1279 loc) · 38.4 KB
/
symbols.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) 2002, NVIDIA Corporation.
NVIDIA Corporation("NVIDIA") supplies this software to you in
consideration of your agreement to the following terms, and your use,
installation, modification or redistribution of this NVIDIA software
constitutes acceptance of these terms. If you do not agree with these
terms, please do not use, install, modify or redistribute this NVIDIA
software.
In consideration of your agreement to abide by the following terms, and
subject to these terms, NVIDIA grants you a personal, non-exclusive
license, under NVIDIA's copyrights in this original NVIDIA software (the
"NVIDIA Software"), to use, reproduce, modify and redistribute the
NVIDIA Software, with or without modifications, in source and/or binary
forms; provided that if you redistribute the NVIDIA Software, you must
retain the copyright notice of NVIDIA, this notice and the following
text and disclaimers in all such redistributions of the NVIDIA Software.
Neither the name, trademarks, service marks nor logos of NVIDIA
Corporation may be used to endorse or promote products derived from the
NVIDIA Software without specific prior written permission from NVIDIA.
Except as expressly stated in this notice, no other rights or licenses
express or implied, are granted by NVIDIA herein, including but not
limited to any patent rights that may be infringed by your derivative
works or by other works in which the NVIDIA Software may be
incorporated. No hardware is licensed hereunder.
THE NVIDIA SOFTWARE IS BEING PROVIDED ON AN "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING WITHOUT LIMITATION, WARRANTIES OR CONDITIONS OF TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
ITS USE AND OPERATION EITHER ALONE OR IN COMBINATION WITH OTHER
PRODUCTS.
IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY SPECIAL, INDIRECT,
INCIDENTAL, EXEMPLARY, CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, LOST PROFITS; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) OR ARISING IN ANY WAY
OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE
NVIDIA SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT,
TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
NVIDIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\****************************************************************************/
//
// symbols.c
//
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "slglobals.h"
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Symbol Table Variables: ///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
Scope *ScopeList = NULL;
Scope *CurrentScope = NULL;
Scope *GlobalScope = NULL;
int NextFunctionIndex = 0;
Type *UndefinedType = NULL;
Type *CFloatType = NULL;
Type *CIntType = NULL;
Type *VoidType = NULL;
Type *FloatType = NULL;
Type *IntType = NULL;
Type *BooleanType = NULL;
Type *CFloat1Type = NULL;
Type *CFloat2Type = NULL;
Type *CFloat3Type = NULL;
Type *CFloat4Type = NULL;
Type *CInt1Type = NULL;
Type *CInt2Type = NULL;
Type *CInt3Type = NULL;
Type *CInt4Type = NULL;
Type *Float1Type = NULL;
Type *Float2Type = NULL;
Type *Float3Type = NULL;
Type *Float4Type = NULL;
Type *Int1Type = NULL;
Type *Int2Type = NULL;
Type *Int3Type = NULL;
Type *Int4Type = NULL;
Type *Boolean1Type = NULL;
Type *Boolean2Type = NULL;
Type *Boolean3Type = NULL;
Type *Boolean4Type = NULL;
Symbol *FalseSymb = NULL;
Symbol *TrueSymb = NULL;
static int baseTypeNames[TYPE_BASE_LAST_USER + 1] = { 0 };
static Type *baseTypes[TYPE_BASE_LAST_USER + 1] = { NULL };
/************************************ Type Name Error Support ********************************/
/*
* SetScalarTypeName() - Set a scalar type name.
*
*/
void SetScalarTypeName(int base, int name, Type *fType)
{
if (base >= 0 && base <= TYPE_BASE_LAST_USER) {
baseTypeNames[base] = name;
baseTypes[base] = fType;
}
} // SetScalarTypeName
///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// Symbol Table Fuctions: ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
/*
* InitSymbolTable()
*
*/
int InitSymbolTable(CgStruct *Cg)
{
SourceLoc dummyLoc = { 0, 0 };
int ii, name;
// Create the super-global scope and add predefined types and symbols:
PushScope(NewScopeInPool(mem_CreatePool(0, 0)));
UndefinedType = NewType(TYPE_BASE_UNDEFINED_TYPE | TYPE_CATEGORY_SCALAR, 0);
CFloatType = NewType(TYPE_BASE_CFLOAT | TYPE_CATEGORY_SCALAR | TYPE_QUALIFIER_CONST, 1);
CIntType = NewType(TYPE_BASE_CINT | TYPE_CATEGORY_SCALAR | TYPE_QUALIFIER_CONST, 1);
VoidType = NewType(TYPE_BASE_VOID | TYPE_CATEGORY_SCALAR | TYPE_MISC_VOID, 0);
FloatType = NewType(TYPE_BASE_FLOAT | TYPE_CATEGORY_SCALAR, 1);
IntType = NewType(TYPE_BASE_INT | TYPE_CATEGORY_SCALAR, 1);
BooleanType = NewType(TYPE_BASE_BOOLEAN | TYPE_CATEGORY_SCALAR, 1);
CFloat1Type = NewPackedArrayType(CFloatType, 1, TYPE_QUALIFIER_CONST);
CFloat2Type = NewPackedArrayType(CFloatType, 2, TYPE_QUALIFIER_CONST);
CFloat3Type = NewPackedArrayType(CFloatType, 3, TYPE_QUALIFIER_CONST);
CFloat4Type = NewPackedArrayType(CFloatType, 4, TYPE_QUALIFIER_CONST);
CInt1Type = NewPackedArrayType(CIntType, 1, TYPE_QUALIFIER_CONST);
CInt2Type = NewPackedArrayType(CIntType, 2, TYPE_QUALIFIER_CONST);
CInt3Type = NewPackedArrayType(CIntType, 3, TYPE_QUALIFIER_CONST);
CInt4Type = NewPackedArrayType(CIntType, 4, TYPE_QUALIFIER_CONST);
Float1Type = NewPackedArrayType(FloatType, 1, 0);
Float2Type = NewPackedArrayType(FloatType, 2, 0);
Float3Type = NewPackedArrayType(FloatType, 3, 0);
Float4Type = NewPackedArrayType(FloatType, 4, 0);
Int1Type = NewPackedArrayType(IntType, 1, 0);
Int2Type = NewPackedArrayType(IntType, 2, 0);
Int3Type = NewPackedArrayType(IntType, 3, 0);
Int4Type = NewPackedArrayType(IntType, 4, 0);
Boolean1Type = NewPackedArrayType(BooleanType, 1, 0);
Boolean2Type = NewPackedArrayType(BooleanType, 2, 0);
Boolean3Type = NewPackedArrayType(BooleanType, 3, 0);
Boolean4Type = NewPackedArrayType(BooleanType, 4, 0);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cfloat"), CFloatType, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cint"), CIntType, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, VOID_SY, VoidType, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, FLOAT_SY, FloatType, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, INT_SY, IntType, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, BOOLEAN_SY, BooleanType, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cfloat1"), CFloat1Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cfloat2"), CFloat2Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cfloat3"), CFloat3Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cfloat4"), CFloat4Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cint1"), CInt1Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cint2"), CInt2Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cint3"), CInt3Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "cint4"), CInt4Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "float1"), Float1Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "float2"), Float2Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "float3"), Float3Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "float4"), Float4Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "int1"), Int1Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "int2"), Int2Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "int3"), Int3Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "int4"), Int4Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "bool1"), Boolean1Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "bool2"), Boolean2Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "bool3"), Boolean3Type, TYPEDEF_S);
AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "bool4"), Boolean4Type, TYPEDEF_S);
FalseSymb = AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "false"), BooleanType, CONSTANT_S);
TrueSymb = AddSymbol(&dummyLoc, CurrentScope, LookUpAddString(atable, "true"), BooleanType, CONSTANT_S);
FalseSymb->details.con.value = 0;
TrueSymb->details.con.value = 1;
SetScalarTypeName(TYPE_BASE_NO_TYPE, LookUpAddString(atable, "***no-base-type***"), UndefinedType);
SetScalarTypeName(TYPE_BASE_UNDEFINED_TYPE, LookUpAddString(atable, "***undefined-base-type***"), UndefinedType);
SetScalarTypeName(TYPE_BASE_CFLOAT, LookUpAddString(atable, "cfloat"), CFloatType);
SetScalarTypeName(TYPE_BASE_CINT, LookUpAddString(atable, "cint"), CIntType);
SetScalarTypeName(TYPE_BASE_VOID, LookUpAddString(atable, "void"), VoidType);
SetScalarTypeName(TYPE_BASE_FLOAT, LookUpAddString(atable, "float"), FloatType);
SetScalarTypeName(TYPE_BASE_INT, LookUpAddString(atable, "int"), IntType);
SetScalarTypeName(TYPE_BASE_BOOLEAN, LookUpAddString(atable, "bool"), BooleanType);
name = LookUpAddString(atable, "***unknown-profile-base-type***");
for (ii = TYPE_BASE_FIRST_USER; ii <= TYPE_BASE_LAST_USER; ii++)
SetScalarTypeName(ii, name, UndefinedType);
// Add profile specific symbols and types:
Cg->theHAL->RegisterNames(Cg->theHAL);
AddAtom(atable, "<*** end hal specific atoms ***>");
// Initialize misc. other globals:
CurrentDeclTypeSpecs.basetype = UndefinedType;
CurrentDeclTypeSpecs.IsDerived = 0;
CurrentDeclTypeSpecs.type = *UndefinedType;
return 1;
} // InitSymbolTable
int StartGlobalScope(CgStruct *Cg)
{
// Create user's global scope:
GlobalScope = NewScopeInPool(mem_CreatePool(0, 0));
PushScope(GlobalScope);
return 1;
} // StartGlobalScope
/*
* FreeSymbolTable()
*
*/
int FreeSymbolTable(CgStruct *Cg)
{
Scope *lScope, *nScope;
lScope = ScopeList;
while (lScope) {
nScope = lScope->next;
// FreeEverythingOwnedByScope(pScope);
lScope = nScope;
}
return 1;
} // FreeSymbolTable
static void unlinkScope(void *_scope) {
Scope *scope = _scope;
if (scope->next)
scope->next->prev = scope->prev;
if (scope->prev)
scope->prev->next = scope->next;
else
ScopeList = scope->next;
}
/*
* NewScope()
*
*/
Scope *NewScopeInPool(MemoryPool *pool)
{
Scope *lScope;
lScope = mem_Alloc(pool, sizeof(Scope));
lScope->pool = pool;
lScope->parent = NULL;
lScope->funScope = NULL;
lScope->symbols = NULL;
lScope->tags = NULL;
lScope->params = NULL;
lScope->returnType = NULL;
lScope->level = 0;
lScope->funindex = 0;
lScope->InFormalParameters = 0;
lScope->HasVoidParameter = 0;
lScope->HasReturnStmt = 0;
lScope->IsStructScope = 0;
lScope->HasSemantics = 0;
lScope->pid = PID_NONE_ID;
lScope->programs = NULL;
lScope->initStmts = NULL;
if ((lScope->next = ScopeList))
ScopeList->prev = lScope;
lScope->prev = 0;
ScopeList = lScope;
mem_AddCleanup(pool, unlinkScope, lScope);
return lScope;
} // NewScope
/*
* PushScope()
*
*/
void PushScope(Scope *fScope)
{
Scope *lScope;
if (CurrentScope) {
fScope->level = CurrentScope->level + 1;
if (fScope->level == 1) {
if (!GlobalScope) {
/* HACK - CTD -- if GlobalScope==NULL and level==1, we're
* defining a function in the superglobal scope. Things
* will break if we leave the level as 1, so we arbitrarily
* set it to 2 */
fScope->level = 2;
}
}
if (fScope->level >= 2) {
lScope = fScope;
while (lScope->level > 2)
lScope = lScope->next;
fScope->funScope = lScope;
}
} else {
fScope->level = 0;
}
fScope->parent = CurrentScope;
CurrentScope = fScope;
} // PushScope
/*
* PopScope()
*
*/
Scope *PopScope(void)
{
Scope *lScope;
lScope = CurrentScope;
if (CurrentScope)
CurrentScope = CurrentScope->parent;
return lScope;
} // PopScope
/*
* NewSymbol() - Allocate a new symbol node;
*
*/
Symbol *NewSymbol(SourceLoc *loc, Scope *fScope, int name, Type *fType, symbolkind kind)
{
Symbol *lSymb;
char *pch;
int ii;
lSymb = (Symbol *) mem_Alloc(fScope->pool, sizeof(Symbol));
lSymb->left = NULL;
lSymb->right = NULL;
lSymb->next = NULL;
lSymb->name = name;
lSymb->storageClass = SC_UNKNOWN;
lSymb->type = fType;
lSymb->loc = *loc;
lSymb->kind = kind;
lSymb->properties = 0;
lSymb->flags = 0;
lSymb->tempptr = NULL;
lSymb->tempptr2 = NULL;
// Clear union area:
pch = (char *) &lSymb->details;
for (ii = 0; ii < sizeof(lSymb->details); ii++)
*pch++ = 0;
return lSymb;
} // NewSymbol
/*
* lAddToTree() - Using a binary tree is not a good idea for basic atom values because they
* are generated in order. We'll fix this later (by reversing the bit pattern).
*/
static void lAddToTree(Symbol **fSymbols, Symbol *fSymb, Type *fType)
{
Symbol *lSymb;
int lrev, frev;
lSymb = *fSymbols;
if (lSymb) {
frev = GetReversedAtom(atable, fSymb->name);
while (lSymb) {
lrev = GetReversedAtom(atable, lSymb->name);
if (lrev == frev) {
InternalError(Cg->tokenLoc, 9999, "symbol \"%s\" already in table",
GetAtomString(atable, fSymb->name));
break;
} else {
if (lrev > frev) {
if (lSymb->left) {
lSymb = lSymb->left;
} else {
lSymb->left = fSymb;
break;
}
} else {
if (lSymb->right) {
lSymb = lSymb->right;
} else {
lSymb->right = fSymb;
break;
}
}
}
}
} else {
*fSymbols = fSymb;
}
} // lAddToTree
/*
* AddSymbol() - Add a variable, type, or function name to a scope.
*
*/
Symbol *AddSymbol(SourceLoc *loc, Scope *fScope, int atom, Type *fType, symbolkind kind)
{
Symbol *lSymb;
if (!fScope)
fScope = CurrentScope;
lSymb = NewSymbol(loc, fScope, atom, fType, kind);
lAddToTree(&fScope->symbols, lSymb, fType);
return lSymb;
} // AddSymbol
/*
* UniqueSymbol() - Add a symbol to fScope that is different from
* every other symbol. Useful for compiler generated temporaries.
*
*/
Symbol *UniqueSymbol(Scope *fScope, Type *fType, symbolkind kind)
{
static int nextTmp = 0;
static SourceLoc tmpLoc = { 0, 0 };
char buf[256];
int atom;
sprintf(buf, "@TMP%d", nextTmp++);
atom = AddAtom(atable, buf);
return AddSymbol(&tmpLoc, fScope, atom, fType, kind);
} // UniqueSymbol
/*
* AddTag() - Add a tag name to a scope.
*
*/
Symbol *AddTag(SourceLoc *loc, Scope *fScope, int atom, int category)
{
Symbol *lSymb;
Type *pType;
if (!fScope)
fScope = CurrentScope;
pType = NewType(category, 0);
pType->str.unqualifiedtype = pType;
lSymb = NewSymbol(loc, fScope, atom, pType, TAG_S);
lAddToTree(&fScope->tags, lSymb, pType);
return lSymb;
} // AddTag
/*********************************************************************************************/
/***************************************** Type Functions ************************************/
/*********************************************************************************************/
/*
* InitType() - Initialize a type struct.
*
*/
void InitType(Type *fType)
{
char *c = (char *) fType;
int ii;
for (ii = 0; ii < sizeof(Type); ii++)
*c++ = 0;
} // InitType
/*
* NewType() - Allocate a new type struct.
*
*/
Type *NewType(int properties, int size)
{
Type *lType;
lType = (Type *) malloc(sizeof(Type));
InitType(lType);
lType->properties = properties;
lType->co.size = size;
return lType;
} // NewType
/*
* DupType() - Duplicate a type struct.
*
*/
Type *DupType(Type *fType)
{
Type *lType;
lType = (Type *) malloc(sizeof(Type));
*lType = *fType;
return lType;
} // DupType
/*
* NewPackedArrayType() - Define a new packed (vector) array type.
*
*/
Type *NewPackedArrayType(Type *elType, int numels, int properties)
{
Type *lType;
lType = NewType(TYPE_CATEGORY_ARRAY | TYPE_MISC_PACKED | properties | GetBase(elType), 0);
lType->arr.eltype = elType;
lType->arr.numels = numels;
lType->arr.size = Cg->theHAL->GetSizeof(lType);
return lType;
} // NewPakedArrayType
/*************************************** Category Functions **********************************/
/*
* IsCategory() - See if a type is of the given category.
*
*/
int IsCategory(const Type *fType, int category)
{
if (fType && (fType->properties & TYPE_CATEGORY_MASK) == category) {
return 1;
} else {
return 0;
}
} // IsCategory
/*
* IsTypeBase() - See if a type is of the given base.
*
*/
int IsTypeBase(const Type *fType, int base)
{
if (fType && (fType->properties & TYPE_BASE_MASK) == base) {
return 1;
} else {
return 0;
}
} // IsTypeBase
/*
* IsVoid() - Returns TRUE if a void type.
*
*/
int IsVoid(const Type *fType)
{
if (fType && (fType->properties & TYPE_MISC_VOID)) {
return 1;
} else {
return 0;
}
} // IsVoid
/*
* IsBoolean() - Returns TRUE if a Boolean type.
*
*/
int IsBoolean(const Type *fType)
{
if (fType && (fType->properties & TYPE_BASE_MASK) == TYPE_BASE_BOOLEAN) {
return 1;
} else {
return 0;
}
} // IsBoolean
/*
* IsScalar() - Returns TRUE if a scalar type.
*
*/
int IsScalar(const Type *fType)
{
if (fType && (fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_SCALAR) {
return 1;
} else {
return 0;
}
} // IsScalar
/*
* IsArray() - Returns TRUE if a packed or unpacked array type.
*
*/
int IsArray(const Type *fType)
{
if (fType && (fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_ARRAY) {
return 1;
} else {
return 0;
}
} // IsScalar
/*
* IsVector() - Returns TRUE if a vector type.
*
*/
int IsVector(const Type *fType, int *len)
{
if (fType &&
(fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_ARRAY &&
(fType->properties & TYPE_MISC_PACKED) &&
!IsArray(fType->arr.eltype))
{
if (len)
*len = fType->arr.numels;
return 1;
} else {
return 0;
}
} // IsVector
/*
* IsMatrix() - Returns TRUE if a matrix type.
*
*/
int IsMatrix(const Type *fType, int *len, int *len2)
{
if (fType &&
(fType->properties & TYPE_CATEGORY_MASK) == TYPE_CATEGORY_ARRAY &&
(fType->properties & TYPE_MISC_PACKED) &&
IsVector(fType->arr.eltype, len))
{
if (len2)
*len2 = fType->arr.numels;
return 1;
} else {
return 0;
}
} // IsMatrix
/*
* IsUnsizedArray() - Returns TRUE if an array with an unspecified number of elements.
*
*/
int IsUnsizedArray(const Type *fType)
{
if (GetCategory(fType) == TYPE_CATEGORY_ARRAY &&
fType->arr.numels == 0)
{
return 1;
} else {
return 0;
}
} // IsUnsizedArray
/*
* IsStruct() - Returns TRUE if a struct.
*
*/
int IsStruct(const Type *fType)
{
if (fType &&
GetCategory(fType) == TYPE_CATEGORY_STRUCT)
{
return 1;
} else {
return 0;
}
} // IsStruct
/*
* IsProgram() - See if a type is a program.
*
*/
int IsProgram(const Type *fType)
{
if (fType && (fType->properties & TYPE_MISC_PROGRAM)) {
return 1;
} else {
return 0;
}
} // IsProgram
/*
* IsPacked()
*
*/
int IsPacked(const Type *fType)
{
if (fType && (fType->properties & TYPE_MISC_PACKED)) {
return 1;
} else {
return 0;
}
} // IsPacked
/*
* IsSameUnqualifiedType() - Returns TRUE if the unqualified types aType and bType are the same.
*
*/
int IsSameUnqualifiedType(const Type *aType, const Type *bType)
{
const int UnQMask = TYPE_BASE_MASK | TYPE_CATEGORY_MASK ; // 020122 // | TYPE_DOMAIN_MASK;
if (aType == bType) {
return 1;
} else {
if ((aType->properties & UnQMask) == (bType->properties & UnQMask)) {
switch (aType->properties & TYPE_CATEGORY_MASK) {
case TYPE_CATEGORY_SCALAR:
return 1;
case TYPE_CATEGORY_ARRAY:
if (aType->arr.numels == bType->arr.numels) {
// Should we check for Packed here??? I think so!
return IsSameUnqualifiedType(aType->arr.eltype, bType->arr.eltype);
}
break;
case TYPE_CATEGORY_FUNCTION:
break;
case TYPE_CATEGORY_STRUCT:
if (aType->str.unqualifiedtype == bType->str.unqualifiedtype)
return 1;
default:
break;
}
}
}
return 0;
} // IsSameUnqualifiedType
/*
* IsTypedef() - See if a symbol is a typedef.
*
*/
int IsTypedef(const Symbol *fSymb)
{
if (fSymb && fSymb->kind == TYPEDEF_S) {
return 1;
} else {
return 0;
}
} // IsTypedef
/*
* IsFunction() - See if a symbol is a function.
*
*/
int IsFunction(const Symbol *fSymb)
{
if (fSymb && fSymb->kind == FUNCTION_S) {
return 1;
} else {
return 0;
}
} // IsFunction
/*
* IsInline() - See if a symbol is an inline function.
*
*/
int IsInline(const Symbol *fSymb)
{
if (fSymb && fSymb->kind == FUNCTION_S && (fSymb->properties & SYMB_IS_INLINE_FUNCTION)) {
return 1;
} else {
return 0;
}
} // IsInline
/*
* GetBase() - Return the base attributes of a type.
*
*/
int GetBase(const Type *fType)
{
if (fType) {
return fType->properties & TYPE_BASE_MASK;
} else {
return TYPE_BASE_NO_TYPE;
}
} // GetBase
/*
* GetCategory() - Return the categpry of a type.
*
*/
int GetCategory(const Type *fType)
{
if (fType) {
return fType->properties & TYPE_CATEGORY_MASK;
} else {
return TYPE_CATEGORY_NONE;
}
} // GetCategory
/*
* GetDomain() - Return the domain of a type.
*
*/
int GetDomain(const Type *fType)
{
if (fType) {
return fType->properties & TYPE_DOMAIN_MASK;
} else {
return TYPE_DOMAIN_UNKNOWN;
}
} // GetDomain
/*
* GetQualifiers() - Return a type's qualifiers.
*
*/
int GetQualifiers(const Type *fType)
{
if (fType) {
return fType->properties & TYPE_QUALIFIER_MASK;
} else {
return TYPE_QUALIFIER_NONE;
}
} // GetQualifiers
/*
* GetQuadRegSize() - Return the number of quad registers required to hold an object
* of this type. Minimum size is 1.
*/
int GetQuadRegSize(const Type *fType)
{
if (fType) {
return (fType->co.size + 3) >> 2;
} else {
return 1;
}
} // GetQuadRegSize
/*
* ClearTypeMisc() - Clear bits in the properties field a type.
*
*/
void ClearTypeMisc(Type *fType, int misc)
{
if (fType)
fType->properties &= ~misc;
} // ClearTypeMisc
/*********************************************************************************************/
/************************************ Symbol Semantic Functions ******************************/
/*********************************************************************************************/
/*
* LookUpLocalSymbol()
*
*/
Symbol *LookUpLocalSymbol(Scope *fScope, int atom)
{
Symbol *lSymb;
int rname, ratom;
ratom = GetReversedAtom(atable, atom);
if (!fScope)
fScope = CurrentScope;
lSymb = fScope->symbols;
while (lSymb) {
rname = GetReversedAtom(atable, lSymb->name);
if (rname == ratom) {
return lSymb;
} else {
if (rname > ratom) {
lSymb = lSymb->left;
} else {
lSymb = lSymb->right;
}
}
}
return NULL;
} // LookUpLocalSymbol
/*
* LookUpLocalSymbolBySemanticName() - Lookup a symbol in a local tree by the : semantic name.
*
* Note: The tree is not ordered for this lookup so the next field is used. This only works
* for structs and other scopes that maintain this list.
*
* Note: There can be multiple matches. Returns the first.
*
*/
Symbol *LookUpLocalSymbolBySemanticName(Scope *fScope, int atom)
{
Symbol *lSymb;
if (!fScope)
return NULL;
lSymb = fScope->symbols;
while (lSymb) {
if (lSymb->kind == VARIABLE_S) {
if (lSymb->details.var.semantics == atom)
return lSymb;
}
lSymb = lSymb->next;
}
return NULL;
} // LookUpLocalSymbolBySemanticName
/*
* LookUpLocalSymbolByBindingName() - Lookup a symbol in a local tree by the lname in the
* semantic binding structure.
*
* Note: The tree is not ordered for this lookup so the next field is used. This only works
* for structs and other scopes that maintain this list.
*
* Note: There can be multiple matches. Returns the first.
*
*/
Symbol *LookUpLocalSymbolByBindingName(Scope *fScope, int atom)
{
Symbol *lSymb;
if (!fScope)
return NULL;
lSymb = fScope->symbols;
while (lSymb) {
if (lSymb->kind == VARIABLE_S && lSymb->details.var.bind) {
if (lSymb->details.var.bind->none.lname == atom)
return lSymb;
}
lSymb = lSymb->next;
}
return NULL;
} // LookUpLocalSymbolByBindingName
/*
* LookUpLocalTag()
*
*/
Symbol *LookUpLocalTag(Scope *fScope, int atom)
{
Symbol *lSymb;
int rname, ratom;
ratom = GetReversedAtom(atable, atom);
if (!fScope)
fScope = CurrentScope;
lSymb = fScope->tags;
while (lSymb) {
rname = GetReversedAtom(atable, lSymb->name);
if (rname == ratom) {
return lSymb;
} else {
if (rname > ratom) {
lSymb = lSymb->left;
} else {
lSymb = lSymb->right;
}
}
}
return NULL;
} // LookUpLocalTag
/*
* LookUpSymbol()