forked from ztachip/ztachip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ident.cpp
executable file
·1167 lines (1076 loc) · 35 KB
/
ident.cpp
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 [2014] [Ztachip Technologies Inc]
//
// Author: Vuong Nguyen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except IN compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to IN writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//------------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <vector>
#include "../base/zta.h"
#include "ast.h"
#include "ident.h"
#include "instruction.h"
#include "gen.h"
#define MAX_STACK_SIZE 32
// Array of identifiers
static cIdentifierShared *M_SHARE=0;
void cIdentifier::Init()
{
// This identifier points to the whole memory space
M_SHARE = new cIdentifierShared(0,0,0,0,0);
M_SHARE->allocate(0);
M_SHARE->m_num_dim=1;
M_SHARE->m_dim[0] = ((1 << REGISTER_FILE_DEPTH));
M_SHARE->m_len=((1 << REGISTER_FILE_DEPTH));
}
cIdentifierVector::cIdentifierVector() :
std::vector<cIdentifier *>()
{
}
cIdentifierVector::~cIdentifierVector()
{
}
// Append a identifers to the list
void cIdentifierVector::append(cIdentifier *_id)
{
if(!exist(_id))
push_back(_id);
}
// Duplicate this list of identifiers
void cIdentifierVector::clone(cIdentifierVector *x)
{
size_t i;
clear();
for(i=0;i < x->size();i++)
append(x->at(i));
}
// y=y UNION x
void cIdentifierVector::vector_union(cIdentifierVector *x)
{
size_t i;
for(i=0;i < x->size();i++)
{
if(!exist(x->at(i)))
append(x->at(i));
}
}
// y=y MINUS x
void cIdentifierVector::vector_minus(cIdentifierVector *x)
{
cIdentifierVector temp;
cIdentifier *ele;
size_t i;
for(i=0;i < size();i++)
{
ele=at(i);
if(!x->exist(ele))
temp.append(at(i));
}
clone(&temp);
}
// Check if 2 vectors have overlap elements
bool cIdentifierVector::isOverlap(cIdentifierVector *x)
{
cIdentifierVector temp;
cIdentifier *ele;
size_t i;
for(i=0;i < size();i++)
{
ele=at(i);
if(x->exist(ele))
return true;
}
return false;
}
// Check of 2 identifier vectors are the same
bool cIdentifierVector::equal(cIdentifierVector *x)
{
size_t i;
if(x->size() != size())
return false;
for(i=0;i < x->size();i++)
{
if(!exist(x->at(i)))
return false;
}
return true;
}
// Check if an identifier is included in this list
bool cIdentifierVector::exist(cIdentifier *x)
{
size_t i;
for(i=0;i < size();i++)
{
if(at(i)==x)
return true;
}
return false;
}
// Check is any item in one list is included in another list
bool cIdentifierVector::exist(cIdentifierVector *v)
{
int i;
for(i=0;i < (int)v->size();i++)
{
if(exist(v->at(i)))
return true;
}
return false;
}
// Base class for all identifiers
int cIdentifier::M_id=0;
INSTANTIATE_OBJECT(cIdentifier);
cIdentifier::cIdentifier(cAstNode *owner,cAstNode *parent,cAstNode *type)
{
cAstNode *node;
m_id=M_id++;
m_type=type;
m_offset=-1;
m_byteOffset=-1;
m_num_dim=0;
m_interference=0;
m_color=0;
m_defCount=0;
m_useCount=0;
m_owner=owner;
if(!parent)
{
// This is a temporary variable
m_num_dim=0;
m_len=1;
m_name.clear();
m_context.clear();
}
else
{
// This is a declared variable
node=(cAstNode *)parent->getChild(1,eTOKEN_direct_declarator11);
if(node)
{
// Array
node=(cAstNode *)node->getChildList();
if(node->getID()!=eTOKEN_IDENTIFIER)
error(node->m_lineNo,"Invalid variable declaration");
else
{
if(ParseName(CAST(cAstStringNode,node)->getStringValue(),&m_name,&m_context))
error(parent->m_lineNo,"Invalid variable name");
m_len=1;
CAST(cAstIdentifierNode,node)->setIdentifier(this);
node=(cAstNode *)node->getNext();
while(node)
{
if(m_num_dim >= MAX_VAR_DIM)
error(parent->m_lineNo,"Exceed max array dimension allowed");
if(!node->isKindOf(cAstIntNode::getCLID()))
error(parent->m_lineNo,"Invalid array definition");
m_len *= CAST(cAstIntNode,node)->getIntValue();
m_dim[m_num_dim]=CAST(cAstIntNode,node)->getIntValue();
if(m_dim[m_num_dim] <= 0)
error(parent->m_lineNo,"Invalid array definition");
m_num_dim++;
node=(cAstNode *)node->getNext();
}
}
}
else
{
node=(cAstNode *)parent->getChild(1,eTOKEN_IDENTIFIER);
assert(node!=0);
// Scalar
m_num_dim=0;
m_len=1;
if(ParseName(CAST(cAstStringNode,node)->getStringValue(),&m_name,&m_context))
error(parent->m_lineNo,"Invalid variable name");
}
}
if(owner)
{
if(cIdentifier::isReservedName((char *)m_name.c_str()))
error(parent->m_lineNo,"Variable name is a reserved name");
if(!CAST(cAstCodeBlockNode,owner)->setIdentifierList(this))
error(parent->m_lineNo,"Variable already defined");
}
}
cIdentifier::~cIdentifier()
{
}
// Return if there is an alias identifier associated
cIdentifier *cIdentifier::getAlias()
{
if(isKindOf(cIdentifierParameter::getCLID()))
return CAST(cIdentifierParameter,this)->m_alias;
else if(isKindOf(cIdentifierShared::getCLID()))
return CAST(cIdentifierShared,this)->m_alias;
else
return 0;
}
// Return if this identifier is associated with parameter list
bool cIdentifier::isParameter()
{
return isKindOf(cIdentifierParameter::getCLID()) ||
isKindOf(cIdentifierShared::getCLID()) ||
(isKindOf(cIdentifierFixed::getCLID()) && CAST(cIdentifierFixed,this)->m_persistentIndex >= 0) ||
isKindOf(cIdentifierReturnValue::getCLID());
}
// Return if this identifier is a stack variable
bool cIdentifier::isStack()
{
return isKindOf(cIdentifierStack::getCLID());
}
// Check if this identifier is persistent and fixed in allocation
bool cIdentifier::isFixed()
{
if(isParameter())
return true;
if(isStack())
return true;
if(isKindOf(cIdentifierFixed::getCLID()) && CAST(cIdentifierFixed,this)->m_persistent)
return true;
if(isKindOf(cIdentifierExReg::getCLID()) && CAST(cIdentifierExReg,this)->m_persistent)
return true;
return false;
}
// Is the name reserved name?
bool cIdentifier::isReservedName(char *name)
{
int oc;
if(!name)
return false;
if(strcasecmp(name,"tid")==0 ||
strcasecmp(name,"pid")==0 ||
strcasecmp(name,"_VMASK")==0)
return true;
if(cConfig::decode_mu_oc(name,&oc))
return true;
return false;
}
bool cIdentifier::isSameContext(cAstNode *func)
{
cAstNode *node3;
char *funcName;
node3=func->getChild(3,eTOKEN_declarator,eTOKEN_direct_declarator12,eTOKEN_IDENTIFIER);
if(!node3)
node3=func->getChild(2,eTOKEN_declarator,eTOKEN_IDENTIFIER);
assert(node3!=0);
funcName=CAST(cAstIdentifierNode,node3)->getStringValue();
if(m_context.length()==0 ||
(memcmp(m_context.c_str(),funcName,m_context.length())==0 && memcmp(&funcName[m_context.length()],"::",2)==0) ||
(!strstr(funcName,"::") && memcmp(m_context.c_str(),funcName,m_context.length())==0))
return true;
else
return false;
}
// Return number of parameters defined in a function parameter list
int cIdentifier::getFuncParameterCount(cAstNode *owner)
{
cIdentifier *id;
int count=0;
id=CAST(cAstCodeBlockNode,owner)->getIdentifierList();
while(id)
{
if(id->isKindOf(cIdentifierParameter::getCLID()) ||
id->isKindOf(cIdentifierShared::getCLID()))
{
count++;
}
id=(cIdentifier *)id->getNext();
}
return count;
}
// Return identifier associated with a function parameter
cIdentifier *cIdentifier::getFuncParameter(cAstNode *owner,int index)
{
cIdentifier *id;
int count=0;
id=CAST(cAstCodeBlockNode,owner)->getIdentifierList();
while(id)
{
if(id->isKindOf(cIdentifierParameter::getCLID()) ||
id->isKindOf(cIdentifierShared::getCLID()))
{
if(count==index)
return id;
count++;
}
id=(cIdentifier *)id->getNext();
}
return 0;
}
// Return return value parameter variable associated with a function
cIdentifierReturnValue *cIdentifier::getReturnValue(cAstNode *owner)
{
cIdentifier *id;
id=CAST(cAstCodeBlockNode,owner)->getIdentifierList();
while(id)
{
if(id->isKindOf(cIdentifierReturnValue::getCLID()))
{
return CAST(cIdentifierReturnValue,id);
}
id=(cIdentifier *)id->getNext();
}
// Allocate new stack variable
return 0;
}
// Return stack variable. Allocate if doesnot exist yet
cIdentifierStack *cIdentifier::getStackVariable(cAstNode *owner,int pos,int width)
{
cIdentifier *id;
id=CAST(cAstCodeBlockNode,owner)->getIdentifierList();
while(id)
{
if(id->isStack())
{
if(CAST(cIdentifierStack,id)->m_pos==pos &&
CAST(cIdentifierStack,id)->m_w==width)
return CAST(cIdentifierStack,id); // Already defined...
}
id=(cIdentifier *)id->getNext();
}
// Allocate new stack variable
return new cIdentifierStack(owner,0,0,pos,width);
}
// Extract initialization values declared with identifier declaration
void cIdentifier::setInitializer(cAstNode *init,int level,int *levelIndex)
{
int index;
int i;
int vw;
int num_dim;
cAstNode *node;
cAstNode *node2;
assert(init==0 || init->getID()==eTOKEN_initializer);
if(!init)
return;
node2=init->getChildList();
num_dim=m_num_dim;
if(isKindOf(cIdentifierStorage::getCLID()))
vw=CAST(cIdentifierStorage,this)->m_w;
else
vw=-1;
if(vw > 0)
num_dim++;
if(level > num_dim)
error(init->m_lineNo,"Array too deep");
if(node2->getID()==eTOKEN_initializer_list)
{
// Go to next nested level
node=init->getChildList()->getChildList();
index=0;
while(node)
{
levelIndex[level]=index++;
setInitializer(node,level+1,levelIndex);
node=(cAstNode *)node->getNext();
}
if(level < m_num_dim && index != m_dim[level])
error(init->m_lineNo,"Invalid array initialization");
if(level >= m_num_dim && index != (1<<vw))
error(init->m_lineNo,"Invalid array initialization");
}
else if(node2->getID()==eTOKEN_I_CONSTANT)
{
// Initializer is an integer
if(level != num_dim)
error(node2->m_lineNo,"Invalid array initialization");
// printf("\n-->INT(%d)> %d >",level,CAST(cAstIntNode,node2)->getIntValue());
// for(i=0;i < level;i++)
// printf(" %d",levelIndex[i]);
// printf("\n");
m_init.push_back((float)CAST(cAstIntNode,node2)->getIntValue());
}
else if(node2->getID()==eTOKEN_F_CONSTANT)
{
// Initializer is a float
if(level != num_dim)
error(node2->m_lineNo,"Invalid array initialization");
printf("\n-->FLOAT(%d)> %d \n",level,(int)CAST(cAstFloatNode,node2)->getFloatValue());
for(i=0;i < level;i++)
printf(" %d",levelIndex[i]);
printf("\n");
m_init.push_back((float)CAST(cAstFloatNode,node2)->getFloatValue());
}
else
{
error(node2->m_lineNo,"Invalid or unsupported variable auto-initialization");
}
}
// Assign a space allocation for an identifier
void cIdentifier::allocate(int offset)
{
m_offset=offset;
m_byteOffset=offset;
if(isKindOf(cIdentifierStorage::getCLID()))
{
if((m_offset & ((1<<CAST(cIdentifierStorage,this)->m_w)-1)))
error(0,"Invalid address");
m_byteOffset=m_offset;
m_offset = m_offset/(1<<CAST(cIdentifierStorage,this)->m_w);
}
}
int cIdentifier::getLen()
{
if(isKindOf(cIdentifierStorage::getCLID()))
{
// This is a vector storage
return m_len * (1<<CAST(cIdentifierStorage,this)->m_w);
}
else
return m_len;
}
int cIdentifier::getVectorWidth()
{
if(isKindOf(cIdentifierStorage::getCLID()))
return CAST(cIdentifierStorage,this)->m_w;
else
return 0;
}
// Get a size for a array dimension
int cIdentifier::getDimSize(int index)
{
int size=1;
int i;
assert(index < m_num_dim);
for(i=index+1;i < m_num_dim;i++)
size = size*m_dim[i];
// if(isKindOf(cIdentifierStorage::getCLID()))
// size = size*(1<<CAST(cIdentifierStorage,this)->m_w);
return size;
}
char *cIdentifier::getPrintName(char *buf)
{
if(m_name.size()>0)
{
sprintf(buf,"%s@%d",m_name.c_str(),m_byteOffset);
return buf;
}
else
{
if(isKindOf(cIdentifierExReg::getCLID()))
sprintf(buf,"TD%d@%d",m_id,m_byteOffset);
else
sprintf(buf,"TS%d@%d",m_id,m_byteOffset);
return buf;
}
}
// Based class for floating point identifier
INSTANTIATE_OBJECT(cIdentifierFloat);
cIdentifierFloat::cIdentifierFloat(cAstNode *owner,cAstNode *parent,cAstNode *type)
: cIdentifier(owner,parent,type)
{
}
cIdentifierFloat::~cIdentifierFloat()
{
}
// Based class for storage identifier
INSTANTIATE_OBJECT(cIdentifierStorage);
cIdentifierStorage::cIdentifierStorage(cAstNode *owner,cAstNode *parent,cAstNode *type,int _width)
: cIdentifierFloat(owner,parent,type)
{
m_w=_width;
}
cIdentifierStorage::~cIdentifierStorage()
{
}
// Floating point identifier in shared space
INSTANTIATE_OBJECT(cIdentifierShared);
cIdentifierShared::cIdentifierShared(cAstNode *owner,cAstNode *parent,cAstNode *type,cIdentifier *alias,int _width) :
cIdentifierStorage(owner,parent,type,_width)
{
m_alias=alias;
}
// Floating point identifier in private space
INSTANTIATE_OBJECT(cIdentifierPrivate);
cIdentifierPrivate::cIdentifierPrivate(cAstNode *owner,cAstNode *parent,cAstNode *type,int _width) :
cIdentifierStorage(owner,parent,type,_width)
{
}
// Floating point identifier in parameter variable
INSTANTIATE_OBJECT(cIdentifierParameter);
cIdentifierParameter::cIdentifierParameter(cAstNode *owner,cAstNode *parent,cAstNode *type,cIdentifier *alias,int _width) :
cIdentifierPrivate(owner,parent,type,_width)
{
m_alias=alias;
}
// Floating point identifier for a stack variable
INSTANTIATE_OBJECT(cIdentifierStack);
cIdentifierStack::cIdentifierStack(cAstNode *owner,cAstNode *parent,cAstNode *type,int pos,int _width) :
cIdentifierPrivate(owner,parent,type,_width)
{
m_pos=pos;
}
// Floating point identifier for a function return value
INSTANTIATE_OBJECT(cIdentifierReturnValue);
cIdentifierReturnValue::cIdentifierReturnValue(cAstNode *owner,cAstNode *parent,cAstNode *type,bool _isFloat,int _width) :
cIdentifierPrivate(owner,parent,type,_width)
{
m_float=_isFloat;
}
// Floating point identifier in constant space
INSTANTIATE_OBJECT(cIdentifierConst);
cIdentifierConst::cIdentifierConst(cAstNode *owner,cAstNode *parent,cAstNode *type) :
cIdentifierFloat(owner,parent,type)
{
}
// Based class for integer identifier
INSTANTIATE_OBJECT(cIdentifierFixed);
cIdentifierFixed::cIdentifierFixed(cAstNode *owner,cAstNode *parent,cAstNode *type,bool persistent,int persistentIndex) :
cIdentifier(owner,parent,type)
{
if(m_num_dim > 0)
error(parent->m_lineNo,"Array is not allowed for this variable type");
m_persistent=persistent;
m_persistentIndex=persistentIndex;
}
cIdentifierFixed::~cIdentifierFixed()
{
}
// Identifier for pointer variable
INSTANTIATE_OBJECT(cIdentifierPointer);
cIdentifierPointer::cIdentifierPointer(cAstNode *owner,cAstNode *parent,cAstNode *type,bool isConst,int _width,bool persistent) :
cIdentifierFixed(owner,parent,type,persistent,-1)
{
m_isConst=isConst;
m_width=_width;
}
// Return list of variables that are referenced by this pointer
void cIdentifierPointer::getIdentifierScope(cIdentifierVector *lst)
{
cIdentifierVector dirtyLst;
_getIdentifierScope(lst,&dirtyLst);
}
// Return list of variables that are referenced by this pointer
// Do it recursively for pointer alias...
void cIdentifierPointer::_getIdentifierScope(cIdentifierVector *lst,cIdentifierVector *dirtyLst)
{
int i;
for(i=0;i < (int)m_scope.size();i++)
{
if(m_scope[i]->isKindOf(cIdentifierPointer::getCLID()))
{
if(!dirtyLst->exist(m_scope[i]))
{
dirtyLst->append(m_scope[i]);
CAST(cIdentifierPointer,m_scope[i])->_getIdentifierScope(lst,dirtyLst);
}
}
else
{
if(!lst->exist(m_scope[i]))
lst->append(m_scope[i]);
}
}
}
// Based class for integer valued variables
INSTANTIATE_OBJECT(cIdentifierInteger);
cIdentifierInteger::cIdentifierInteger(cAstNode *owner,cAstNode *parent,cAstNode *type,bool isUnsigned,bool persistent,int persistentIndex) :
cIdentifierFixed(owner,parent,type,persistent,persistentIndex)
{
m_isUnsigned=isUnsigned;
m_useForMuIndex=false;
}
// Check if this variable can be referenced the same variables referenced by a pointer
bool cIdentifier::isPointerOverlap(cIdentifier *pointer)
{
assert(pointer->isKindOf(cIdentifierPointer::getCLID()));
if(CAST(cIdentifierPointer,pointer)->m_scope.size() <= 0)
error(pointer->m_type->m_lineNo,"Pointer is never initialized");
if(isKindOf(cIdentifierPointer::getCLID()))
{
// If both are pointers then check if their scopes (list of referenced variables) are overlapped.
assert(CAST(cIdentifierPointer,this)->m_scope.size() > 0);
cIdentifierVector lst1,lst2;
CAST(cIdentifierPointer,pointer)->getIdentifierScope(&lst1);
CAST(cIdentifierPointer,this)->getIdentifierScope(&lst2);
return lst1.isOverlap(&lst2);
}
else if(isKindOf(cIdentifierPrivate::getCLID()) || isKindOf(cIdentifierShared::getCLID()))
{
// Is the variable included in the pointer scope
cIdentifierVector lst1;
CAST(cIdentifierPointer,pointer)->getIdentifierScope(&lst1);
return lst1.exist(this);
}
else
return false;
}
// Identifier for RESULT variable (integer values returned by MU operation)
cIdentifierResult cIdentifierResult::M_singleInstance;
INSTANTIATE_OBJECT(cIdentifierResult);
cIdentifierResult::cIdentifierResult() :
cIdentifierFixed(0,0,0,false,-1)
{
}
// Identifier for xregister variable (extended register)
INSTANTIATE_OBJECT(cIdentifierExReg);
cIdentifierExReg::cIdentifierExReg(cAstNode *owner,cAstNode *parent,cAstNode *type,int _width,bool _persistent) :
cIdentifierStorage(owner,parent,type,_width)
{
m_persistent=_persistent;
}
// Identifier for LANE control register
cIdentifierLane cIdentifierLane::M_singleInstance;
INSTANTIATE_OBJECT(cIdentifierLane);
cIdentifierLane::cIdentifierLane() :
cIdentifierFixed(0,0,0,false,-1)
{
}
// Find parameter given the function name and parameter name
cIdentifier *cIdentifier::lookupParm(cAstNode *_root,char *funcName,char *parmName)
{
cIdentifier *attr;
cAstNode *node,*node2,*node3,*func=0;
char *name;
if(strlen(funcName)>2 && strcmp(&funcName[strlen(funcName)-2],"::")==0)
{
// Only context is given
char context[MAX_STRING_LEN];
strcpy(context,funcName);
context[strlen(context)-2]=0;
// Find it in global variable list
attr=CAST(cAstCodeBlockNode,_root)->getIdentifierList();
while(attr)
{
if(strcmp(attr->m_name.c_str(),parmName)==0 && strcmp(attr->m_context.c_str(),context)==0)
return attr;
attr=(cIdentifier *)attr->getNext();
}
return 0;
}
if(strcasecmp(funcName,"root")==0)
{
if (strcasecmp(parmName, "constant")==0)
{
// This is special keyword to point to whole shared memory
return M_SHARE;
}
else
return 0;
}
node=(cAstNode *)_root->getChildList();
while(node)
{
if(node->getID()==eTOKEN_function_definition2)
{
node3=node->getChild(3,eTOKEN_declarator,eTOKEN_direct_declarator12,eTOKEN_IDENTIFIER);
if(!node3)
node3=node->getChild(2,eTOKEN_declarator,eTOKEN_IDENTIFIER);
assert(node3!=0);
if(strcmp(CAST(cAstIdentifierNode,node3)->getStringValue(),funcName)==0)
{
// This is a function...
func=node;
node2=node->getChild(1,eTOKEN_block_item_list);
attr=CAST(cAstCodeBlockNode,node2)->getIdentifierList();
while(attr)
{
if (attr->m_name.size()>0)
name = (char *)attr->m_name.c_str();
else if (attr->isKindOf(cIdentifierParameter::getCLID()))
name = (char *)(CAST(cIdentifierParameter, attr)->m_alias->m_name.c_str());
else if (attr->isKindOf(cIdentifierShared::getCLID()))
name = (char *)(CAST(cIdentifierShared, attr)->m_alias->m_name.c_str());
else
name = 0;
if(attr->isParameter() && name && strcmp(name,parmName)==0)
return attr;
attr=(cIdentifier *)attr->getNext();
}
break;
}
}
node=(cAstNode *)node->getNext();
}
if(!func)
return 0;
// Find it in global variable list
attr=CAST(cAstCodeBlockNode,_root)->getIdentifierList();
while(attr)
{
if(attr->m_name.size()>0 && strcmp(attr->m_name.c_str(),parmName)==0 && attr->isSameContext(func))
return attr;
attr=(cIdentifier *)attr->getNext();
}
return 0;
}
// Locate a variable from its definition list
// Variable declaration may be nested and multi-level. Search
// from the bottom level first
cIdentifier *cIdentifier::lookup(cAstNode *_root,cAstNode *func,char *name,cAstNode **stack,int stackSize)
{
cIdentifier *item;
cAstNode *node;
int i;
// Find it in local variable first
assert(func!=0 && (func->getID()==eTOKEN_function_definition2));
for(i=stackSize-1;i >= 0;i--)
{
node=stack[i];
item=CAST(cAstCodeBlockNode,node)->getIdentifierList();
while(item)
{
if(item->m_owner==stack[i] && item->m_name.size()>0 && strcmp(item->m_name.c_str(),name)==0)
return item;
item=(cIdentifier *)item->getNext();
}
}
// Find it in global variable list
item=CAST(cAstCodeBlockNode,_root)->getIdentifierList();
while(item)
{
if(item->m_name.size()>0 && strcmp(item->m_name.c_str(),name)==0 && item->isSameContext(func))
return item;
item=(cIdentifier *)item->getNext();
}
return 0;
}
// Assign cIndentifier to each occurence of the IDENTIFIER in the AST tree.
void cIdentifier::assign(cAstNode *_root,cAstNode *func,cAstNode *node,cAstNode **stack,int stackSize)
{
cAstNode *node2;
cIdentifier *id;
if(node->getID()==eTOKEN_block_item_list)
{
node->setCodeBlock((stackSize>0)?stack[stackSize-1]:0);
assert(stackSize < MAX_STACK_SIZE);
stack[stackSize++]=node;
}
node2=(cAstNode *)node->getChildList();
while(node2)
{
node2->setCodeBlock(stack[stackSize-1]);
if(node2->getID()==eTOKEN_IDENTIFIER)
{
id=cIdentifier::lookup(_root,func,CAST(cAstStringNode,node2)->getStringValue(),stack,stackSize);
if(!id)
{
if(!cIdentifier::isReservedName(CAST(cAstStringNode,node2)->getStringValue()))
{
if(node->getID()!=eTOKEN_postfix_expression4)
error(node->m_lineNo,"Undefined variable");
}
}
CAST(cAstIdentifierNode,node2)->setIdentifier(id);
}
else
cIdentifier::assign(_root,func,node2,stack,stackSize);
node2=(cAstNode *)node2->getNext();
}
}
// Create a cIdentifier for each variable declarations
// The created cIdentifier class is then attached to the eAST_NodeTypeCodeBlock node where the variable is
// defined
// Scan the AST tree for variable declarations inorder to create the variable definitions
void cIdentifier::scan(cAstNode *_func,cAstNode *owner,cAstNode *parent,bool _global)
{
cAstNode *node,*node2,*node3,*node4,*node5;
cAstNode *init;
cIdentifier *id=0;
int levelIndex[MAX_VAR_DIM+1];
if(parent->getID()==eTOKEN_block_item_list)
{
node=(cAstNode *)parent->getChildList();
while(node)
{
cIdentifier::scan(_func,owner,node,_global);
node=(cAstNode *)node->getNext();
}
return;
}
node=(cAstNode *)parent->getChildList();
while(node)
{
if(node->getID()==eTOKEN_block_item_list)
{
cIdentifier::scan(_func,node,node,_global);
}
else if(node->getID()==eTOKEN_declaration)
{
// Got a declaration
node2=node->getChild(1,eTOKEN_init_declarator_list);
if(!node2)
error(node->m_lineNo,"Invalid variable declaration");
// List of variables
node3=(cAstNode *)node2->getChildList();
while(node3)
{
id=0;
if(node3->getID()==eTOKEN_init_declarator)
{
node5=node3->getChildList();
init=(cAstNode *)node5->getNext();
assert(init->getID()==eTOKEN_initializer);
}
else
{
node5=node3;
init=0;
}
assert(node5->getID()==eTOKEN_declarator);
if(node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_SHARE))
{
if(!_global)
error(node->m_lineNo,"Share variable is only available for global variable");
if((node4=node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_FLOAT)))
{
id=new cIdentifierShared(owner,node5,node4,0,CAST(cAstTokenNode,node4)->m_tokenParm);
}
else
error(node->m_lineNo,"Only float are supported for global variables");
}
else if(node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_EXTERN))
{
// Ignore this. Nothing to do
id=0;
}
else if(node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_STATIC))
{
error(node->m_lineNo,"Static variable not supported");
}
else if(node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_UNSIGNED))
{
if((node4=node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_INT)))
id=new cIdentifierInteger(owner,node5,node4,true,_global,-1);
else
error(node->m_lineNo,"Static variable not supported");
}
else if(node->getChild(2,eTOKEN_declaration_specifiers,eTOKEN_CONST))
{
error(node->m_lineNo,"Constant variables not supported");
}
else if((node4=node->getChild(1,eTOKEN_POINTER_SCOPE)))
{
assert(0);
// id=new cIdentifierPointer(owner,node5,node4,false);
}
else if((node4=node->getChild(1,eTOKEN_INT)))
id=new cIdentifierInteger(owner,node5,node4,false,_global,-1);
else if((node4=node->getChild(1,eTOKEN_FLOAT)))
{
if(node5->getChild(1,eTOKEN_pointer))
id=new cIdentifierPointer(owner,node5,node4,false,CAST(cAstTokenNode,node4)->m_tokenParm,_global);
else
{
if(_global)
id=new cIdentifierParameter(owner,node5,node4,0,CAST(cAstTokenNode,node4)->m_tokenParm);
else
id=new cIdentifierPrivate(owner,node5,node4,CAST(cAstTokenNode,node4)->m_tokenParm);
}
}
else if((node4=node->getChild(1,eTOKEN_DOUBLE)))
{
if(node5->getChild(1,eTOKEN_pointer))
{
error(node->m_lineNo,"Pointer to double not supported");
}
else
{
if(_global)
id=new cIdentifierExReg(owner,node5,node4,VECTOR_DEPTH,_global);
else
id=new cIdentifierExReg(owner,node5,node4,VECTOR_DEPTH,_global);
}
}
else
{
error(node->m_lineNo,"Unsupported variable type 1");
}
if(id)
id->setInitializer(init,0,levelIndex);
node3=(cAstNode *)node3->getNext();
}
}
node=(cAstNode *)node->getNext();
}
}
// Scan for variable definitions in the function parameter list
// Note that if function variable is an integer, then we will need a shadow
// copy in the private/public space. This is required for MCORE to be able
// to read/write integer variables
void cIdentifier::scanParm(
cAstNode *func,
cAstNode *owner, // eTOKEN_function_definition2
cAstNode *parent // eTOKEN_parameter_list
)
{