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 /*****************************************************************************/
38 #include "scopedefs.h"
54 /*****************************************************************************/
56 /*****************************************************************************/
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static long Member (long AllocSize)
74 /* Read one struct member and return its size */
78 /* A multiplicator may follow */
79 if (CurTok.Tok != TOK_SEP) {
80 Multiplicator = ConstExpression ();
81 if (Multiplicator <= 0) {
82 ErrorSkip ("Range error");
85 AllocSize *= Multiplicator;
88 /* Check the size for a reasonable value */
89 if (AllocSize >= 0x10000) {
90 ErrorSkip ("Range error");
99 static long DoStructInternal (long Offs, unsigned Type)
100 /* Handle the .STRUCT command */
104 /* Outside of other structs, we need a name. Inside another struct or
105 * union, the struct may be anonymous, in which case no new lexical level
108 int Anon = (CurTok.Tok != TOK_IDENT);
110 /* Enter a new scope, then skip the name */
111 SymEnterLevel (&CurTok.SVal, SCOPE_STRUCT, ADDR_SIZE_ABS, 0);
113 /* Start at zero offset in the new scope */
117 /* Test for end of line */
120 /* Read until end of struct */
121 while (CurTok.Tok != TOK_ENDSTRUCT &&
122 CurTok.Tok != TOK_ENDUNION &&
123 CurTok.Tok != TOK_EOF) {
129 /* Allow empty and comment lines */
130 if (CurTok.Tok == TOK_SEP) {
135 /* The format is "[identifier] storage-allocator [, multiplicator]" */
137 if (CurTok.Tok == TOK_IDENT) {
139 /* Beware: An identifier may also be a macro, in which case we have
142 Macro* M = FindMacro (&CurTok.SVal);
148 /* We have an identifier, generate a symbol */
149 Sym = SymFind (CurrentScope, &CurTok.SVal, SYM_ALLOC_NEW);
151 /* Assign the symbol the offset of the current member */
152 SymDef (Sym, GenLiteralExpr (Offs), ADDR_SIZE_DEFAULT, SF_NONE);
154 /* Skip the member name */
158 /* Read storage allocators */
159 MemberSize = 0; /* In case of errors, use zero */
160 switch (CurTok.Tok) {
164 MemberSize = Member (1);
171 MemberSize = Member (2);
176 MemberSize = Member (3);
181 MemberSize = Member (4);
186 if (CurTok.Tok == TOK_SEP) {
187 ErrorSkip ("Size is missing");
189 MemberSize = Member (1);
195 Struct = ParseScopedSymTable ();
197 ErrorSkip ("Unknown struct/union");
198 } else if (GetSymTabType (Struct) != SCOPE_STRUCT) {
199 ErrorSkip ("Not a struct/union");
201 SymEntry* SizeSym = GetSizeOfScope (Struct);
202 if (!SymIsDef (SizeSym) || !SymIsConst (SizeSym, &MemberSize)) {
203 ErrorSkip ("Size of struct/union is unknown");
206 MemberSize = Member (MemberSize);
211 MemberSize = DoStructInternal (Offs, STRUCT);
216 MemberSize = DoStructInternal (Offs, UNION);
220 if (!CheckConditionals ()) {
221 /* Not a conditional directive */
222 ErrorSkip ("Invalid storage allocator in struct/union");
226 /* Assign the size to the member if it has a name */
228 DefSizeOfSymbol (Sym, MemberSize);
232 if (Type == STRUCT) {
238 if (MemberSize > Size) {
243 /* Expect end of line */
247 /* If this is not a anon struct, enter a special symbol named ".size"
248 * into the symbol table of the struct that holds the size of the
249 * struct. Since the symbol starts with a dot, it cannot be accessed
251 * Leave the struct scope level.
255 SymEntry* SizeSym = GetSizeOfScope (CurrentScope);
256 SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE);
258 /* Close the struct scope */
262 /* End of struct/union definition */
263 if (Type == STRUCT) {
264 Consume (TOK_ENDSTRUCT, "`.ENDSTRUCT' expected");
266 Consume (TOK_ENDUNION, "`.ENDUNION' expected");
269 /* Return the size of the struct */
275 long GetStructSize (SymTable* Struct)
276 /* Get the size of a struct or union */
278 SymEntry* SizeSym = FindSizeOfScope (Struct);
280 Error ("Size of struct/union is unknown");
283 return GetSymVal (SizeSym);
290 /* Handle the .STRUCT command */
292 DoStructInternal (0, STRUCT);
298 /* Handle the .UNION command */
300 DoStructInternal (0, UNION);