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