]> git.sur5r.net Git - cc65/blob - src/ca65/struct.c
6e854727cb86340cf300e3c41167eb5af669968d
[cc65] / src / ca65 / struct.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 struct.c                                  */
4 /*                                                                           */
5 /*                          .STRUCT/.UNION commands                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "addrsize.h"
38
39 /* ca65 */
40 #include "condasm.h"
41 #include "error.h"
42 #include "expr.h"
43 #include "macro.h"
44 #include "nexttok.h"
45 #include "scanner.h"
46 #include "sizeof.h"
47 #include "symbol.h"
48 #include "symtab.h"
49 #include "struct.h"
50
51
52
53 /*****************************************************************************/
54 /*                                   Data                                    */
55 /*****************************************************************************/
56
57
58
59 enum {
60     STRUCT,
61     UNION
62 };
63
64
65
66 /*****************************************************************************/
67 /*                                   Code                                    */
68 /*****************************************************************************/
69
70
71
72 static long Member (long AllocSize)
73 /* Read one struct member and return its size */
74 {
75     long Multiplicator;
76
77     /* A multiplicator may follow */
78     if (CurTok.Tok != TOK_SEP) {
79         Multiplicator = ConstExpression ();
80         if (Multiplicator <= 0) {
81             ErrorSkip ("Range error");
82             Multiplicator = 1;
83         }
84         AllocSize *= Multiplicator;
85     }
86
87     /* Check the size for a reasonable value */
88     if (AllocSize >= 0x10000) {
89         ErrorSkip ("Range error");
90     }
91
92     /* Return the size */
93     return AllocSize;
94 }
95
96
97
98 static long DoStructInternal (long Offs, unsigned Type)
99 /* Handle the .STRUCT command */
100 {
101     long Size = 0;
102
103     /* Outside of other structs, we need a name. Inside another struct or
104      * union, the struct may be anonymous, in which case no new lexical level
105      * is started.
106      */
107     int Anon = (CurTok.Tok != TOK_IDENT);
108     if (!Anon) {
109         /* Enter a new scope, then skip the name */
110         SymEnterLevel (&CurTok.SVal, ST_STRUCT, ADDR_SIZE_ABS, 0);
111         NextTok ();
112         /* Start at zero offset in the new scope */
113         Offs = 0;
114     }
115
116     /* Test for end of line */
117     ConsumeSep ();
118
119     /* Read until end of struct */
120     while (CurTok.Tok != TOK_ENDSTRUCT &&
121            CurTok.Tok != TOK_ENDUNION  &&
122            CurTok.Tok != TOK_EOF) {
123
124         long      MemberSize;
125         SymTable* Struct;
126         SymEntry* Sym;
127
128         /* Allow empty and comment lines */
129         if (CurTok.Tok == TOK_SEP) {
130             NextTok ();
131             continue;
132         }
133
134         /* The format is "[identifier] storage-allocator [, multiplicator]" */
135         Sym = 0;
136         if (CurTok.Tok == TOK_IDENT) {
137
138             /* Beware: An identifier may also be a macro, in which case we have
139              * to start over.
140              */
141             Macro* M = FindMacro (&CurTok.SVal);
142             if (M) {
143                 MacExpandStart (M);
144                 continue;
145             }
146
147             /* We have an identifier, generate a symbol */
148             Sym = SymFind (CurrentScope, &CurTok.SVal, SYM_ALLOC_NEW);
149
150             /* Assign the symbol the offset of the current member */
151             SymDef (Sym, GenLiteralExpr (Offs), ADDR_SIZE_DEFAULT, SF_NONE);
152
153             /* Skip the member name */
154             NextTok ();
155         }
156
157         /* Read storage allocators */
158         MemberSize = 0;                 /* In case of errors, use zero */
159         switch (CurTok.Tok) {
160
161             case TOK_BYTE:
162                 NextTok ();
163                 MemberSize = Member (1);
164                 break;
165
166             case TOK_DBYT:
167             case TOK_WORD:
168             case TOK_ADDR:
169                 NextTok ();
170                 MemberSize = Member (2);
171                 break;
172
173             case TOK_FARADDR:
174                 NextTok ();
175                 MemberSize = Member (3);
176                 break;
177
178             case TOK_DWORD:
179                 NextTok ();
180                 MemberSize = Member (4);
181                 break;
182
183             case TOK_RES:
184                 NextTok ();
185                 if (CurTok.Tok == TOK_SEP) {
186                     ErrorSkip ("Size is missing");
187                 } else {
188                     MemberSize = Member (1);
189                 }
190                 break;
191
192             case TOK_TAG:
193                 NextTok ();
194                 Struct = ParseScopedSymTable ();
195                 if (Struct == 0) {
196                     ErrorSkip ("Unknown struct/union");
197                 } else if (GetSymTabType (Struct) != ST_STRUCT) {
198                     ErrorSkip ("Not a struct/union");
199                 } else {
200                     SymEntry* SizeSym = GetSizeOfScope (Struct);
201                     if (!SymIsDef (SizeSym) || !SymIsConst (SizeSym, &MemberSize)) {
202                         ErrorSkip ("Size of struct/union is unknown");
203                     }
204                 }
205                 MemberSize = Member (MemberSize);
206                 break;
207
208             case TOK_STRUCT:
209                 NextTok ();
210                 MemberSize = DoStructInternal (Offs, STRUCT);
211                 break;
212
213             case TOK_UNION:
214                 NextTok ();
215                 MemberSize = DoStructInternal (Offs, UNION);
216                 break;
217
218             default:
219                 if (!CheckConditionals ()) {
220                     /* Not a conditional directive */
221                     ErrorSkip ("Invalid storage allocator in struct/union");
222                 }
223         }
224
225         /* Assign the size to the member if it has a name */
226         if (Sym) {
227             DefSizeOfSymbol (Sym, MemberSize);
228         }
229
230         /* Next member */
231         if (Type == STRUCT) {
232             /* Struct */
233             Offs += MemberSize;
234             Size += MemberSize;
235         } else {
236             /* Union */
237             if (MemberSize > Size) {
238                 Size = MemberSize;
239             }
240         }
241
242         /* Expect end of line */
243         ConsumeSep ();
244     }
245
246     /* If this is not a anon struct, enter a special symbol named ".size"
247      * into the symbol table of the struct that holds the size of the
248      * struct. Since the symbol starts with a dot, it cannot be accessed
249      * by user code.
250      * Leave the struct scope level.
251      */
252     if (!Anon) {
253         /* Add a symbol */
254         SymEntry* SizeSym = GetSizeOfScope (CurrentScope);
255         SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE);
256
257         /* Close the struct scope */
258         SymLeaveLevel ();
259     }
260
261     /* End of struct/union definition */
262     if (Type == STRUCT) {
263         Consume (TOK_ENDSTRUCT, "`.ENDSTRUCT' expected");
264     } else {
265         Consume (TOK_ENDUNION, "`.ENDUNION' expected");
266     }
267
268     /* Return the size of the struct */
269     return Size;
270 }
271
272
273
274 long GetStructSize (SymTable* Struct)
275 /* Get the size of a struct or union */
276 {
277     SymEntry* SizeSym = FindSizeOfScope (Struct);
278     if (SizeSym == 0) {
279         Error ("Size of struct/union is unknown");
280         return 0;
281     } else {
282         return GetSymVal (SizeSym);
283     }
284 }
285
286
287
288 void DoStruct (void)
289 /* Handle the .STRUCT command */
290 {
291     DoStructInternal (0, STRUCT);
292 }
293
294
295
296 void DoUnion (void)
297 /* Handle the .UNION command */
298 {
299     DoStructInternal (0, UNION);
300 }
301
302
303