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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
|
/* declare.c - declaration routines for bcc */
/* Copyright (C) 1992 Bruce Evans */
#include "bcc.h"
#include "align.h"
#include "byteord.h"
#include "gencode.h"
#include "input.h" /* just for orig_cppmode */
#include "reg.h"
#include "sc.h"
#include "scan.h"
#include "sizes.h"
#include "table.h"
#include "type.h"
#undef EXTERN
#define EXTERN
#include "parse.h"
PRIVATE bool_t argsallowed; /* nonzero to allow args in declarator */
PRIVATE sym_t gvarsc; /* sc flags of last identifier declared */
PRIVATE char gvar2name[2 + NAMESIZE]; /* space for structure keys and .. */
#define gvarname (gvar2name + 2) /* last identifier declared */
PRIVATE struct typestruct *gvartype; /* type of last identifier declared */
PRIVATE bool_t initlistflag; /* remembers whether initializer is a list */
FORWARD struct typestruct *chainprefix P((struct typestruct *pretype,
struct typestruct *sufftype));
FORWARD void declaf P((void));
FORWARD void declarator P((void));
FORWARD void declarg P((void));
FORWARD struct typestruct *declenum P((void));
FORWARD void declfunc P((void));
FORWARD void declselt P((struct typestruct *structype, offset_T *psoffset,
struct typelist ** ptypelist));
FORWARD bool_pt declspec P((void));
FORWARD struct typestruct *declsu P((void));
FORWARD void idecllist P((void));
FORWARD void initarray P((struct typestruct *type));
FORWARD void inititem P((struct typestruct *type));
FORWARD void initstruct P((struct typestruct *type));
FORWARD void lbrace P((void));
FORWARD void multidecl P((char *sname));
FORWARD void need P((int charneeded));
FORWARD void rdeclarator P((void));
FORWARD bool_pt regdecl P((void));
PRIVATE struct typestruct *chainprefix(pretype, sufftype)
struct typestruct *pretype;
struct typestruct *sufftype;
{
if (pretype->nexttype != NULL)
{
sufftype = chainprefix(pretype->nexttype, sufftype);
if (pretype->constructor != ARRAY)
return prefix(pretype->constructor, pretype->typesize, sufftype);
else
return prefix(ARRAY,
pretype->typesize / pretype->nexttype->typesize
* sufftype->typesize, sufftype);
}
return sufftype;
}
PUBLIC void colon()
{
if (sym != COLON)
need(':');
else
nextsym();
}
/* decllist() - list of local declarations */
/* this does the non-terminal symbols declaration-list, type-declaration */
/* list and declaration */
/* the different cases are and ";" at the end are decided by idecllist() */
PUBLIC void decllist()
{
while (declspec())
{
if (gvarsc == NULLDECL)
gvarsc = AUTODECL;
idecllist();
}
}
PRIVATE void declaf()
{
uoffset_T asize;
bool_t levelnew;
#ifdef TESTING_PROTOTYPES
char ogvarname[NAMESIZE];
sym_t ogvarsc;
struct symstruct *ogvarsymptr;
struct typestruct *ogvartype;
#endif
if (sym == LPAREN) /* function */
{
nextsym();
if (argsallowed && level == GLBLEVEL)
{
newlevel();
levelnew = TRUE;
}
else
levelnew = FALSE;
#ifdef TESTING_PROTOTYPES
strcpy(ogvarname, gvarname);
ogvarsc = gvarsc;
ogvarsymptr = gvarsymptr;
ogvartype = gvartype;
if (declspec())
{
if (level == ARGLEVEL)
outstr(gvarname);
outbyte('(');
do
{
declarator();
dbtype(gvartype);
outbyte(' ');
#if 0
if (gvarname[0] != 0 && gsymptr != NULL
&& gsymptr->level == level)
error("repeated argument");
#endif
outstr(gvarname);
if (levelnew)
addloc(gsname, gvartype); /* adjust offsets later */
if (sym != COMMA)
break;
outcomma();
nextsym();
}
while (declspec());
if (sym == STRUCELTOP)
{
/* ... should be lexed. */
nextsym();
if (sym == STRUCELTOP)
{
nextsym();
if (sym == STRUCELTOP)
{
nextsym();
outstr(" ...");
}
}
}
outbyte(')');
if (level == ARGLEVEL)
outnl();
}
else
{
#endif /* TESTING_PROTOTYPES */
while (sym == IDENT)
{
if (gsymptr != NULL && gsymptr->level == level)
error("repeated argument");
if (levelnew)
addloc(gsname, itype); /* adjust types and offsets later */
nextsym();
if (sym != COMMA)
break;
nextsym();
}
#ifdef TESTING_PROTOTYPES
}
strcpy(gvarname, ogvarname);
gvarsc = ogvarsc;
gvarsymptr = ogvarsymptr;
gvartype = ogvartype;
#endif /* TESTING_PROTOTYPES */
rparen();
declaf();
switch (gvartype->constructor)
{
case ARRAY:
error("function returning array is illegal");
break;
case FUNCTION:
error("function returning function is illegal");
break;
}
gvartype = prefix(FUNCTION, ftypesize, gvartype);
}
else if (sym == LBRACKET)
{
nextsym();
if (sym == RBRACKET)
asize = 0;
else
asize = constexpression() & intmaskto; /* FIXME: warn overflow */
rbracket();
declaf();
if (gvartype->typesize == 0)
switch (gvartype->constructor)
{
case FUNCTION:
error("array of functions is illegal");
break;
case STRUCTU:
error("undefined structure");
break;
default: /* array */
error("null dimension");
gvartype->typesize = 1;
}
gvartype = prefix(ARRAY, asize * gvartype->typesize, gvartype);
}
}
PRIVATE void declarator()
{
rdeclarator();
if (gvartype->constructor == STRUCTU && gvartype->typesize == 0 &&
gvarsc != TYPEDEFDECL && gvarsc != EXTERNDECL)
error("undefined structure");
}
PRIVATE void declarg()
{
if (gvarsymptr->level != ARGLEVEL)
error2error(gvarname, " not in argument list");
else if (gvarsymptr->flags & INITIALIZED && gvarsymptr->type != gvartype)
multidecl(gvarname);
else
{
gvarsymptr->flags = INITIALIZED; /* flag otherwise unused for locals */
if (gvartype->constructor & (ARRAY | POINTER))
gvartype = pointype(gvartype->nexttype);
gvarsymptr->type = gvartype;
if (gvarsc == REGDECL && regdecl())
regarg = TRUE;
}
}
/*
enum-specifier =
"enum" [identifier] "{" enumerator {, enumerator} "}"
| "enum" identifier.
enumerator =
identifier
| identifier = constant-expression.
*/
PRIVATE struct typestruct *declenum()
{
register struct symstruct *esymptr;
register struct typestruct *enumtype;
offset_T ordinal;
nextsym();
if (sym != IDENT && sym != TYPEDEFNAME)
enumtype = addstruct("");
else if ((esymptr = findstruct(gsname)) == NULL)
{
enumtype = addstruct(gsname);
nextsym();
}
else
{
nextsym();
enumtype = esymptr->type;
if (sym != LBRACE)
return enumtype;
if (esymptr->level != level)
enumtype = addstruct(esymptr->name.namea);
}
lbrace();
if (enumtype->typesize != 0)
multidecl(enumtype->tname);
else
{
enumtype->typesize = itypesize;
enumtype->alignmask = itype->alignmask;
}
if (sym != IDENT)
error("empty enumerator list");
ordinal = 0;
while (sym == IDENT)
{
if ((esymptr = gsymptr) != NULL && esymptr->level == level)
multidecl(gsname);
else
{
(esymptr = addlorg(gsname, itype))->storage = CONSTANT;
esymptr->indcount = 0;
}
nextsym();
if (sym == ASSIGNOP)
{
nextsym();
ordinal = constexpression() & intmaskto; /* FIXME: warn ovflo */
if (ordinal > maxintto)
ordinal -= (maxuintto + 1);
}
if (esymptr->storage == CONSTANT)
esymptr->offset.offv = ordinal++;
if (sym != COMMA)
break;
nextsym();
}
rbrace();
return enumtype;
}
/* declselt - get list of declarations for a structure/union member */
PRIVATE void declselt(structype, psoffset, ptypelist)
struct typestruct *structype;
offset_T *psoffset;
struct typelist **ptypelist;
{
struct typestruct *basetype;
value_t fieldwidth;
value_t fwidth;
offset_T offset;
scalar_t scalar=0;
offset = *psoffset;
declspec();
if (gvarsc != NULLDECL)
error("illegal type name");
basetype = gvartype;
while (sym != SEMICOLON && sym != EOFSYM)
{
gvartype = basetype;
declarator();
fieldwidth = -1;
if (sym == COLON)
{
scalar = gvartype->scalar;
if (!(scalar & ISCALAR))
error("bitfield has non-integral type");
nextsym();
fwidth = constexpression();
if (fwidth < 0)
error("bitfield has negative width");
else if (fwidth > INT32BITSTO) /* XXX */
error("bitfield is too wide");
else
fieldwidth = fwidth;
}
if (gvarname[0] == 0)
{
if (fieldwidth == 0)
offset = (offset + ~gvartype->alignmask) & gvartype->alignmask;
else
/* XXX - should really align to fieldwidth'th bit. */
offset = (offset + ~gvartype->alignmask) & gvartype->alignmask;
}
else
{
if (fieldwidth == 0)
error("bitfield has zero width");
else if (fieldwidth > 0 && scalar & ISCALAR)
{
if (fieldwidth <= CHBITSTO) /* XXX! */
gvartype = ctype;
else if (fieldwidth <= INT32BITSTO) /* XXX */
gvartype = itype;
else
gvartype = ltype;
if (scalar & UNSIGNED)
gvartype = tounsigned(gvartype);
}
gvar2name[0] = structype->structkey[0];
gvar2name[1] = structype->structkey[1];
/* key provides unique name space */
if (findlorg(gvar2name) != NULL)
multidecl(gvarname);
else
{
(gvarsymptr = addlorg(gvar2name, gvartype))->storage
= NOSTORAGE;
structype->alignmask &= gvartype->alignmask;
offset = (gvarsymptr->offset.offi =
(offset+ ~gvartype->alignmask) & gvartype->alignmask)
+ gvartype->typesize;
{
register struct typelist *newtypelist;
newtypelist = qmalloc(sizeof *newtypelist);
#ifdef TS
++ts_n_newtypelist;
ts_s_newtypelist += sizeof *newtypelist;
#endif
newtypelist->tlnext = NULL;
newtypelist->tltype = gvartype;
if (*ptypelist == NULL)
structype->listtype = newtypelist;
else
(*ptypelist)->tlnext = newtypelist;
*ptypelist = newtypelist;
}
}
}
if (sym != COMMA)
break;
nextsym();
}
semicolon();
*psoffset = offset;
}
PRIVATE bool_pt declspec()
{
unsigned nsc;
unsigned nsigned;
unsigned ntype;
unsigned nunsigned;
gvarsc = NULLDECL;
gvartype = itype;
nsigned = nunsigned = ntype = nsc = 0;
while (TRUE)
{
switch (sym)
{
case AUTODECL:
++nsc;
gvarsc = sym;
nextsym();
break;
case ENUMDECL:
++ntype;
declenum();
gvartype = itype; /* kludge - ignore type of enum */
break;
case EXTERNDECL:
++nsc;
gvarsc = sym;
nextsym();
break;
case REGDECL:
++nsc;
gvarsc = sym;
nextsym();
break;
case STATICDECL:
++nsc;
gvarsc = sym;
nextsym();
break;
case STRUCTDECL:
case UNIONDECL:
++ntype;
gvartype = declsu();
break;
case TYPEDECL:
if (ntype == 1)
{
/* allow short int and long int */
if (gvartype == itype
&& (gsymptr->type == stype || gsymptr->type == ltype))
ntype = 0;
/* Allow long double and long float */
if (gvartype == ltype
&& (gsymptr->type == fltype || gsymptr->type == dtype))
{
gvartype = dtype;
nextsym();
break;
}
/* allow int short and int long, blech */
if (gsymptr->type == itype
&& (gvartype == stype || gvartype == ltype))
{
nextsym();
break;
}
}
++ntype;
gvartype = gsymptr->type;
nextsym();
break;
case TYPEDEFDECL:
++nsc;
gvarsc = sym;
nextsym();
break;
case TYPEDEFNAME:
if (ntype != 0)
goto break2;
++ntype;
gvartype = gsymptr->type;
nextsym();
break;
case SIGNDECL:
++nsigned;
nextsym();
break;
case UNSIGNDECL:
++nunsigned;
nextsym();
break;
case ASMSYM:
nextsym();
doasm();
break;
default:
goto break2;
}
}
break2:
if (nsigned > 0)
{
if (ntype == 0)
{
gvartype = itype;
ntype = 1;
}
gvartype = tosigned(gvartype);
if (nsigned > 1 || nunsigned > 0)
ntype = 2;
}
if (nunsigned > 0)
{
if (ntype == 0)
{
gvartype = uitype;
ntype = 1;
}
gvartype = tounsigned(gvartype);
if (nunsigned > 1 || nsigned > 0)
ntype = 2;
}
if (nsc > 0)
{
if (ntype == 0)
ntype = 1;
if (nsc > 1)
error("multiple storage classes in declaration");
}
if (ntype > 1)
error("multiple types in declaration");
return nsc > 0 || ntype > 0;
}
/* declsu - get structure or union name and/or declaration, return type */
PRIVATE struct typestruct *declsu()
{
sym_t ogvarsc;
offset_T soffset;
offset_T struclength;
sym_t susym;
struct typelist *typelist;
struct typestruct *structype;
susym = sym;
nextsym();
if (sym != IDENT && sym != TYPEDEFNAME)
structype = addstruct("");
else if ((gvarsymptr = findstruct(gsname)) == NULL)
{
structype = addstruct(gsname);
nextsym();
}
else
{
nextsym();
if (sym == LBRACE && gvarsymptr->level != level)
structype = addstruct(gvarsymptr->name.namea);
else
structype = gvarsymptr->type;
}
if (sym != LBRACE)
return structype;
if (structype->typesize != 0)
multidecl(structype->tname);
nextsym();
ogvarsc = gvarsc;
struclength = soffset = 0;
typelist = NULL;
while (sym != RBRACE && sym != EOFSYM)
{
declselt(structype, &soffset, &typelist);
if (susym == STRUCTDECL)
struclength = soffset;
else
{
if (struclength < soffset)
struclength = soffset;
soffset = 0;
}
}
structype->typesize = (struclength + ~structype->alignmask) &
structype->alignmask;
rbrace();
gvarsc = ogvarsc;
return structype;
}
/* declfunc() - function name and parameter list */
PRIVATE void declfunc()
{
offset_T argsp;
uoffset_T argsize;
struct symstruct *symptr;
int main_flag = 0;
strcpy(funcname, gvarname);
if( strcmp(funcname, "main") != 0 ) main_flag = -1;
if (gvarsymptr == NULL)
gvarsymptr = addglb(gvarname, gvartype);
else if (gvarsymptr->type != gvartype ||
(gvarsymptr->flags & INITIALIZED))
multidecl(gvarname); /* different type or full declare */
gvarsymptr->flags = INITIALIZED; /* show full declare */
cseg();
if (gvarsc == STATICDECL)
private(gvarname); /* don't need to put STATIC in flags */
else
public(gvarname);
callee1mask = calleemask;
#ifdef FRAMEPOINTER
frame1list = framelist;
#endif
func1saveregsize = funcsaveregsize;
#if 0
if ((returntype = gvartype->nexttype)->scalar & RSCALAR)
#else
if ((returntype = gvartype->nexttype)->scalar & DOUBLE)
#endif
{
callee1mask &= ~doublreturnregs;
#ifdef FRAMEPOINTER
frame1list &= ~doublreturnregs;
#endif
func1saveregsize = funcdsaveregsize;
}
arg1size = /* would already be 0 unless last function empty */
#ifdef FRAMEPOINTER
framep =
stackarg =
#endif
reguse = 0; /* would already be 0 unless last f had reg vars */
decllist();
argsp = returnadrsize;
if (returntype->constructor & STRUCTU)
argsp += ptypesize;
softsp = -func1saveregsize;
for (symptr = &locsyms[0];
symptr < locptr && symptr->flags != STRUCTNAME &&
*symptr->name.namea >= 'A';
symptr = (struct symstruct *)
align(&symptr->name.namea[strlen(symptr->name.namea) + 1]))
{
if( main_flag >= 0 ) main_flag++;
/* convert arg sizes to offsets */
if (symptr->type == fltype)
symptr->type = dtype;
argsize = symptr->type->typesize;
#ifdef FRAMEPOINTER
if (!arg1inreg || symptr != &locsyms[0] || symptr->flags != REGVAR)
stackarg = TRUE;
#endif
if (arg1inreg && symptr == &locsyms[0] && symptr->flags != REGVAR)
{
if ((arg1size = argsize) < itypesize)
arg1size = itypesize;
argsp = softsp -= arg1size;
}
#if INT_BIG_ENDIAN
if (argsize < itypesize)
argsp += itypesize - argsize;
symptr->offset.offi = argsp;
argsp += argsize;
#else
symptr->offset.offi = argsp;
if (argsize > itypesize)
argsp += argsize;
else
argsp += itypesize;
#endif
if (arg1inreg && symptr == &locsyms[0])
argsp = returnadrsize; /* skip return adr after 1st arg */
}
if( main_flag > 0 )
{
globl("__mkargv");
if( main_flag > 2 )
globl("environ");
}
lbrace();
compound();
clearfunclabels();
}
/* idecllist() - list of init-declarations with a given base type */
/* this does the non-terminal symbols list( init-declarator ) ";" */
/* and list( type-declaration ) ";" */
/* and complete declarations of functions */
PRIVATE void idecllist()
{
struct typestruct *basetype;
struct typestruct *inittype;
scopelev_t levelmark;
uoffset_T newoffset;
argsallowed = level == GLBLEVEL;
levelmark = level;
basetype = gvartype;
while (sym != SEMICOLON)
{
gvartype = basetype;
declarator();
if (gvarname[0] == 0)
needvarname();
if (gvartype->constructor == FUNCTION)
{
if (sym != COMMA && sym != SEMICOLON)
{
if (level == ARGLEVEL && gvarsc != TYPEDEFDECL)
declfunc();
else
error("illegal non-external function");
if (level != levelmark)
{
oldlevel();
locptr = &locsyms[0];
}
argsallowed = FALSE;
return;
}
}
if (level != levelmark)
{
oldlevel();
locptr = &locsyms[0];
}
argsallowed = FALSE;
if (level == ARGLEVEL)
{
if (gvarsymptr == NULL)
gvarsymptr = addglb(gvarname, gvartype); /* error soon */
declarg();
}
else if (gvarsymptr != NULL && (gvarsymptr->level == level ||
gvartype->constructor == FUNCTION ||
(gvarsc == EXTERNDECL &&
gvarsymptr->level == GLBLEVEL)))
{
if (gvarsymptr->level != GLBLEVEL || gvarsymptr->flags == KEYWORD)
multidecl(gvarname);
else if (gvarsymptr->type != gvartype)
{
if (gvartype->constructor != ARRAY ||
gvarsymptr->type->constructor != ARRAY ||
gvartype->nexttype != gvarsymptr->type->nexttype ||
(gvartype->typesize != 0 &&
gvarsymptr->type->typesize != 0))
multidecl(gvarname);
else if (gvartype->typesize != 0)
gvarsymptr->type = gvartype;
}
if (gvarsc == NULLDECL)
gvarsymptr->flags &= ~EXTERNAL;
}
else if (level == GLBLEVEL || gvartype->constructor == FUNCTION ||
gvarsc == EXTERNDECL)
{
gvarsymptr = addglb(gvarname, gvartype);
#ifdef DIRECTPAGE /* make all global scalar and pointer variables DIRECTPAGE */
if (!(gvartype->constructor & ~POINTER))
gvarsymptr->flags = DIRECTPAGE;
#endif
if (gvarsc == EXTERNDECL)
gvarsymptr->flags |= EXTERNAL;
}
else
{
gvarsymptr = addloc(gvarname, gvartype);
if ((gvarsc != REGDECL || !regdecl()) &&
gvartype->constructor != FUNCTION && gvarsc != STATICDECL &&
gvarsc != TYPEDEFDECL)
gvarsymptr->offset.offi = softsp =
(softsp & gvartype->alignmask) - gvartype->typesize;
}
if (gvarsc == STATICDECL)
{
gvarsymptr->flags &= ~EXTERNAL;
gvarsymptr->flags |= STATIC;
if (gvarsymptr->level != GLBLEVEL)
{
gvarsymptr->storage = GLOBAL;
gvarsymptr->offset.offlabel = getlabel();
/* must leave gvarsymptr->name.label alone */
/* since name is required for findlorg() */
/* label is activated by expression() */
}
}
if (gvarsc == TYPEDEFDECL)
{
gvarsymptr->flags = KEYWORD;
gvarsymptr->offset.offsym = TYPEDEFNAME;
}
if (sym == ASSIGNOP)
{
if (gvarsymptr->flags & INITIALIZED)
multidecl(gvarname);
nextsym();
if (level == GLBLEVEL || gvarsc == STATICDECL)
{
#ifndef DIRECTPAGE
dseg();
#else
if (gvarsymptr->flags & DIRECTPAGE)
dpseg();
else
dseg();
#endif
if ((inittype = gvartype)->constructor == ARRAY)
inittype = inittype->nexttype;
newoffset = (dataoffset + ~inittype->alignmask)
& inittype->alignmask;
defnulls(newoffset - dataoffset);
dataoffset = newoffset;
if (gvarsc == STATICDECL)
{
if (level == GLBLEVEL)
private(gvarname);
else
outnlabel(gvarsymptr->offset.offlabel);
}
else
public(gvarname);
initlistflag = TRUE;
if (sym != LBRACE)
initlistflag = FALSE;
inititem(gvartype);
dataoffset += (gvarsymptr->type = gvartype)->typesize;
if (gvarsymptr->level == GLBLEVEL)
gvarsymptr->flags |= INITIALIZED;
if (level != GLBLEVEL)
cseg();
}
else
{
if (gvarsc == EXTERNDECL)
error("initialization of externs is illegal");
switch (gvartype->constructor)
{
case ARRAY:
error("initialization of auto arrays is illegal");
break;
case FUNCTION:
error("initialization of functions is illegal");
break;
default:
initexpression(gvartype);
break;
}
}
}
else if (level != GLBLEVEL && gvarsc == STATICDECL &&
gvartype->constructor != FUNCTION)
{
#ifndef DIRECTPAGE
bssseg();
#else
if (gvarsymptr->flags & DIRECTPAGE)
dpseg();
else
dseg();
#endif
lcommlab(gvarsymptr->offset.offlabel);
outnhex(gvartype->typesize);
cseg();
}
if (sym != COMMA)
break;
nextsym();
}
semicolon();
argsallowed = FALSE;
}
PRIVATE void initarray(type)
struct typestruct *type;
{
uoffset_T basesize;
struct typestruct *basetype;
uoffset_T dimension;
char *stringend;
uoffset_T stringlength;
uoffset_T remaining;
if ((basesize = (basetype = type->nexttype)->typesize) == 0)
dimension = remaining = 0;
else
dimension = remaining = type->typesize / basesize;
if (sym == STRINGCONST && (basetype->scalar & CHAR))
{
stringlength = (stringend = charptr) - constant.value.s;
if (remaining != 0 && stringlength >= remaining)
/* same dim should be allowed but defstr always nullterms */
{
error("string longer than dimension");
stringend = constant.value.s + (/* size_t */ unsigned)
(stringlength = remaining - 1);
}
defstr(constant.value.s, stringend, TRUE);
remaining -= (stringlength + 1);
nextsym();
}
else
do
inititem(basetype);
while (--remaining != 0 && sym == COMMA && initlistflag &&
(nextsym(), sym != RBRACE));
if (dimension == 0)
{
if (type == gvartype)
gvartype = prefix(ARRAY, -remaining * basesize, basetype);
}
else
defnulls(remaining * basesize);
}
PRIVATE void inititem(type)
struct typestruct *type;
{
sym_t startsym;
if ((startsym = sym) == LBRACE)
nextsym();
switch (type->constructor)
{
case ARRAY:
initarray(type);
break;
case STRUCTU:
initstruct(type);
break;
case FUNCTION:
error("initialization of functions is illegal");
break;
default:
initexpression(type);
}
if (startsym == LBRACE)
{
if (sym == COMMA)
{
nextsym();
if (sym != RBRACE)
error("too many initializers");
}
rbrace();
}
}
PRIVATE void initstruct(type)
struct typestruct *type;
{
struct typestruct *memtype;
uoffset_T newoffset;
uoffset_T offset;
struct typelist *typelist;
offset = 0;
if ((typelist = type->listtype) != NULL)
do
{
memtype = typelist->tltype;
newoffset = (offset + ~memtype->alignmask) & memtype->alignmask;
defnulls(newoffset - offset);
offset = newoffset + memtype->typesize;
inititem(memtype);
}
while ((typelist = typelist->tlnext) != NULL && sym == COMMA &&
initlistflag && (nextsym(), sym != RBRACE));
/* eof here will break next time */
defnulls(type->typesize - offset);
}
PRIVATE void lbrace()
{
if (sym != LBRACE)
need('{');
else
nextsym();
}
PUBLIC void lparen()
{
if (sym != LPAREN)
need('(');
else
nextsym();
}
PRIVATE void multidecl(sname)
char *sname;
{
error2error(sname, " already declared");
}
PRIVATE void need(charneeded)
int charneeded;
{
static char message[] = "need 'x'";
message[6] = charneeded;
error(message);
}
PUBLIC void needvarname()
{
error("need variable name");
nextsym();
}
PUBLIC void program()
{
if (orig_cppmode)
cppscan(0);
else
{
nextsym();
while (sym != EOFSYM)
{
declspec();
idecllist();
}
}
}
PUBLIC void rbrace()
{
if (sym != RBRACE)
need('}');
else
nextsym();
}
PUBLIC void rbracket()
{
if (sym != RBRACKET)
need(']');
else
nextsym();
}
PRIVATE void rdeclarator()
{
while (sym == STAR)
{
nextsym();
gvartype = pointype(gvartype);
}
if (sym == LPAREN)
{
struct typestruct *pretype;
struct typestruct *sufftype;
nextsym();
sufftype = gvartype;
gvartype = ctype;
rdeclarator();
rparen();
pretype = gvartype;
gvartype = sufftype;
declaf();
gvartype = chainprefix(pretype, gvartype);
}
else if (sym == IDENT || sym == TYPEDEFNAME)
{
strcpy(gvarname, gsname);
gvarsymptr = gsymptr;
nextsym();
}
else
{
gvarname[0] = 0;
gvarsymptr = NULL;
}
declaf();
}
PRIVATE bool_pt regdecl()
{
store_t regavail;
#if !NOTFINISHED
if (gvarsymptr->type->constructor != POINTER)
return FALSE;
#endif
#ifdef MC6809
if (gvarsymptr->type->constructor != POINTER)
return FALSE;
#endif
if (!(regavail = regregs & ~reguse))
return FALSE;
gvarsymptr->flags = REGVAR;
gvarsymptr->indcount = 0;
#define LOWBITMASK(bits) (bits - (bits & (bits - 1))) /* & chops low bit */
reguse |= gvarsymptr->storage = LOWBITMASK(regavail);
return TRUE;
}
PUBLIC void rparen()
{
if (sym != RPAREN)
need(')');
else
nextsym();
}
PUBLIC void semicolon()
{
if (sym != SEMICOLON)
need(';');
else
nextsym();
}
PUBLIC struct typestruct *typename()
{
char ogvarname[NAMESIZE];
sym_t ogvarsc;
struct symstruct *ogvarsymptr;
struct typestruct *ogvartype;
struct typestruct *type;
/* the global variable data must be preserved */
/* although this is only called for casts and sizeof, */
/* the casts can be in initialisers */
/* and the sizeof in an array size expression */
strcpy(ogvarname, gvarname);
ogvarsc = gvarsc;
ogvarsymptr = gvarsymptr;
ogvartype = gvartype;
if (declspec())
{
declarator();
if (gvarsc != NULLDECL || gvarname[0] != 0)
error("illegal type name");
type = gvartype;
}
else
type = NULL;
strcpy(gvarname, ogvarname);
gvarsc = ogvarsc;
gvarsymptr = ogvarsymptr;
gvartype = ogvartype;
return type;
}
|