* tiny
[mascara-docs.git] / compilers / bcc / linux86-0.16.17 / bcc / type.c
blob10db803dd68dd510ead9fa5a08c15f830b9ba0fe
1 /* type.c - type and storage-class routines for bcc */
3 /* Copyright (C) 1992 Bruce Evans */
5 #include "bcc.h"
6 #include "align.h"
7 #include "gencode.h" /* s.b. switches.h */
8 #include "sc.h"
9 #include "scan.h"
10 #include "table.h"
12 #undef EXTERN
13 #define EXTERN
14 #include "type.h"
16 PUBLIC uoffset_T ctypesize = 1;
17 PUBLIC uoffset_T dtypesize = 8;
18 PUBLIC uoffset_T ftypesize = 0;
19 PUBLIC uoffset_T itypesize = 2;
20 PUBLIC uoffset_T ptypesize = 2;
21 PUBLIC uoffset_T stypesize = 2;
23 PRIVATE char skey0;
24 PRIVATE char skey1;
26 PUBLIC struct typestruct *addstruct(structname)
27 char *structname;
29 unsigned namelength;
30 struct symstruct *symptr;
31 struct typestruct *structype;
33 (structype = newtype())->constructor = STRUCTU;
34 structype->alignmask = ctype->alignmask;
35 if (++skey1 == 0)
37 ++skey1; /* must be nonzero or key string would end */
38 ++skey0; /* will never reach 'A' to be an identifier */
40 structype->structkey[0] = skey0;
41 structype->structkey[1] = skey1;
42 if (*structname != 0)
44 symptr = addlorg(structname, structype);
45 symptr->storage = 0;
46 symptr->flags = STRUCTNAME;
47 if (charptr + (namelength = strlen(structname)) >= chartop)
48 growheap(namelength + 1);
49 #ifdef TS
50 ++ts_n_structname;
51 ts_s_structname += namelength + 1;
52 #endif
53 structype->tname = strcpy(charptr, structname);
54 charptr += namelength + 1;
56 return structype;
59 PUBLIC struct typestruct *iscalartotype(scalar)
60 scalar_pt scalar;
62 if (scalar & LONG)
64 if (scalar & UNSIGNED)
65 return ultype;
66 return ltype;
68 if (scalar & UNSIGNED)
69 return uitype;
70 return itype;
73 PUBLIC struct typestruct *newtype()
75 register struct typestruct *type;
77 type = qmalloc(sizeof *type);
78 #ifdef TS
79 ++ts_n_type;
80 ts_s_type += sizeof *type;
81 #endif
82 type->typesize = /* (uoffset_T) */
83 type->scalar = /* (scalar_t) */
84 type->constructor = /* (constr_t) */
85 type->structkey[0] = 0;
86 type->nexttype =
87 type->prevtype =
88 type->sidetype = NULL;
89 type->listtype = NULL;
90 type->tname = "";
91 return type;
94 PUBLIC void outntypechar(type)
95 struct typestruct *type;
97 outnbyte(*type->tname);
100 PUBLIC struct typestruct *pointype(type)
101 struct typestruct *type;
103 return prefix(POINTER, ptypesize, type);
106 PUBLIC struct typestruct *prefix(constructor, size, type)
107 constr_pt constructor;
108 uoffset_T size;
109 struct typestruct *type;
111 register struct typestruct *searchtype;
113 for (searchtype = type->prevtype; searchtype != NULL;
114 searchtype = searchtype->sidetype)
115 if (searchtype->constructor == (constr_t) constructor &&
116 searchtype->typesize == size)
117 return searchtype;
118 switch ((searchtype = newtype())->constructor = constructor)
120 case ARRAY:
121 searchtype->alignmask = type->alignmask;
122 break;
123 case FUNCTION:
124 searchtype->alignmask = ~(uoffset_T) 0;
125 break;
126 case POINTER:
127 searchtype->alignmask = ~(ptypesize - 1) | alignmask;
128 break;
129 case STRUCTU:
130 bugerror("prefixing structure/union");
131 searchtype->alignmask = alignmask;
132 break;
134 searchtype->typesize = size;
135 searchtype->nexttype = type;
136 searchtype->sidetype = type->prevtype;
137 return type->prevtype = searchtype;
140 PUBLIC struct typestruct *promote(type)
141 struct typestruct *type;
143 scalar_t scalar;
145 if ((scalar = type->scalar) & (CHAR | SHORT))
147 if (scalar & UNSIGNED)
148 return uitype;
149 return itype;
151 if (scalar & FLOAT)
152 return dtype;
153 if (type->constructor & ARRAY)
154 return pointype(type->nexttype);
155 if (type->constructor & FUNCTION)
156 return pointype(type);
157 return type;
160 PUBLIC struct typestruct *tosigned(type)
161 struct typestruct *type;
163 switch (type->scalar & ~(UNSIGNED | DLONG))
165 case CHAR:
166 return sctype;
167 case SHORT:
168 return stype;
169 case INT:
170 return itype;
171 case LONG:
172 return ltype;
173 default:
174 error("signed only applies to integral types");
175 return type;
179 PUBLIC struct typestruct *tounsigned(type)
180 struct typestruct *type;
182 switch (type->scalar & ~(UNSIGNED | DLONG))
184 case CHAR:
185 return uctype;
186 case SHORT:
187 return ustype;
188 case INT:
189 return uitype;
190 case LONG:
191 return ultype;
192 default:
193 error("unsigned only applies to integral types");
194 return type;
198 PUBLIC void typeinit()
200 #ifdef I80386
201 if (i386_32)
203 uitype->typesize =
204 itype->typesize =
205 ptypesize =
206 itypesize = 4;
207 dtype->alignmask =
208 fltype->alignmask =
209 uitype->alignmask =
210 ltype->alignmask =
211 ultype->alignmask =
212 itype->alignmask = ~(uoffset_T) (4 - 1);
213 ltype->scalar = LONG; /* not DLONG */
214 ultype->scalar = UNSIGNED | LONG;
216 #endif
217 fitype = prefix(FUNCTION, ftypesize, itype);
218 pctype = pointype(ctype);
219 skey0 = 1;
220 vtype->constructor = VOID;