1 /*****************************************************************************/
5 /* .STRUCT/.UNION commands */
9 /* (C) 2003-2011, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static long Member (long AllocSize)
72 /* Read one struct member and return its size */
76 /* A multiplicator may follow */
77 if (CurTok.Tok != TOK_SEP) {
78 Multiplicator = ConstExpression ();
79 if (Multiplicator <= 0) {
80 ErrorSkip ("Range error");
83 AllocSize *= Multiplicator;
86 /* Check the size for a reasonable value */
87 if (AllocSize >= 0x10000) {
88 ErrorSkip ("Range error");
97 static long DoStructInternal (long Offs, unsigned Type)
98 /* Handle the .STRUCT command */
102 /* Outside of other structs, we need a name. Inside another struct or
103 * union, the struct may be anonymous, in which case no new lexical level
106 int Anon = (CurTok.Tok != TOK_IDENT);
108 /* Enter a new scope, then skip the name */
109 SymEnterLevel (&CurTok.SVal, ST_STRUCT, ADDR_SIZE_ABS);
111 /* Start at zero offset in the new scope */
115 /* Test for end of line */
118 /* Read until end of struct */
119 while (CurTok.Tok != TOK_ENDSTRUCT &&
120 CurTok.Tok != TOK_ENDUNION &&
121 CurTok.Tok != TOK_EOF) {
127 /* Allow empty and comment lines */
128 if (CurTok.Tok == TOK_SEP) {
133 /* The format is "[identifier] storage-allocator [, multiplicator]" */
135 if (CurTok.Tok == TOK_IDENT) {
136 /* We have an identifier, generate a symbol */
137 Sym = SymFind (CurrentScope, &CurTok.SVal, SYM_ALLOC_NEW);
139 /* Assign the symbol the offset of the current member */
140 SymDef (Sym, GenLiteralExpr (Offs), ADDR_SIZE_DEFAULT, SF_NONE);
142 /* Skip the member name */
146 /* Read storage allocators */
147 MemberSize = 0; /* In case of errors, use zero */
148 switch (CurTok.Tok) {
152 MemberSize = Member (1);
159 MemberSize = Member (2);
164 MemberSize = Member (3);
169 MemberSize = Member (4);
174 if (CurTok.Tok == TOK_SEP) {
175 ErrorSkip ("Size is missing");
177 MemberSize = Member (1);
183 Struct = ParseScopedSymTable ();
185 ErrorSkip ("Unknown struct/union");
186 } else if (GetSymTabType (Struct) != ST_STRUCT) {
187 ErrorSkip ("Not a struct/union");
189 SymEntry* SizeSym = GetSizeOfScope (Struct);
190 if (!SymIsDef (SizeSym) || !SymIsConst (SizeSym, &MemberSize)) {
191 ErrorSkip ("Size of struct/union is unknown");
194 MemberSize = Member (MemberSize);
199 MemberSize = DoStructInternal (Offs, STRUCT);
204 MemberSize = DoStructInternal (Offs, UNION);
208 if (!CheckConditionals ()) {
209 /* Not a conditional directive */
210 ErrorSkip ("Invalid storage allocator in struct/union");
214 /* Assign the size to the member if it has a name */
216 DefSizeOfSymbol (Sym, MemberSize);
220 if (Type == STRUCT) {
226 if (MemberSize > Size) {
231 /* Expect end of line */
235 /* If this is not a anon struct, enter a special symbol named ".size"
236 * into the symbol table of the struct that holds the size of the
237 * struct. Since the symbol starts with a dot, it cannot be accessed
239 * Leave the struct scope level.
243 SymEntry* SizeSym = GetSizeOfScope (CurrentScope);
244 SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE);
246 /* Close the struct scope */
250 /* End of struct/union definition */
251 if (Type == STRUCT) {
252 Consume (TOK_ENDSTRUCT, "`.ENDSTRUCT' expected");
254 Consume (TOK_ENDUNION, "`.ENDUNION' expected");
257 /* Return the size of the struct */
263 long GetStructSize (SymTable* Struct)
264 /* Get the size of a struct or union */
266 SymEntry* Sym = SymFind (Struct, &SizeEntryName, SYM_FIND_EXISTING);
268 Error ("Size of struct/union is unknown");
271 return GetSymVal (Sym);
278 /* Handle the .STRUCT command */
280 DoStructInternal (0, STRUCT);
286 /* Handle the .UNION command */
288 DoStructInternal (0, UNION);