]> git.sur5r.net Git - cc65/blob - src/ca65/struct.c
Added new .FATAL pseudo op.
[cc65] / src / ca65 / struct.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 struct.c                                  */
4 /*                                                                           */
5 /*                          .STRUCT/.UNION commands                          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003      Ullrich von Bassewitz                                       */
10 /*               Römerstraße 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 (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 = (Tok != TOK_IDENT);
107     if (!Anon) {
108         /* Enter a new scope, then skip the name */
109         SymEnterLevel (&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 (Tok != TOK_ENDSTRUCT && Tok != TOK_ENDUNION && Tok != TOK_EOF) {
120
121         long      MemberSize;
122         SymTable* Struct;
123         SymEntry* Sym;
124
125         /* Allow empty and comment lines */
126         if (Tok == TOK_SEP) {
127             NextTok ();
128             continue;
129         }
130
131         /* The format is "[identifier] storage-allocator [, multiplicator]" */
132         Sym = 0;
133         if (Tok == TOK_IDENT) {
134             /* We have an identifier, generate a symbol */
135             Sym = SymFind (CurrentScope, &SVal, SYM_ALLOC_NEW);
136
137             /* Assign the symbol the offset of the current member */
138             SymDef (Sym, GenLiteralExpr (Offs), ADDR_SIZE_DEFAULT, SF_NONE);
139
140             /* Skip the member name */
141             NextTok ();
142         }
143
144         /* Read storage allocators */
145         MemberSize = 0;                 /* In case of errors, use zero */
146         switch (Tok) {
147
148             case TOK_BYTE:
149                 NextTok ();
150                 MemberSize = Member (1);
151                 break;
152
153             case TOK_DBYT:
154             case TOK_WORD:
155             case TOK_ADDR:
156                 NextTok ();
157                 MemberSize = Member (2);
158                 break;
159
160             case TOK_FARADDR:
161                 NextTok ();
162                 MemberSize = Member (3);
163                 break;
164
165             case TOK_DWORD:
166                 NextTok ();
167                 MemberSize = Member (4);
168                 break;
169
170             case TOK_RES:
171                 NextTok ();
172                 if (Tok == TOK_SEP) {
173                     ErrorSkip ("Size is missing");
174                 } else {
175                     MemberSize = Member (1);
176                 }
177                 break;
178
179             case TOK_TAG:
180                 NextTok ();
181                 Struct = ParseScopedSymTable ();
182                 if (Struct == 0) {
183                     ErrorSkip ("Unknown struct/union");
184                 } else if (GetSymTabType (Struct) != ST_STRUCT) {
185                     ErrorSkip ("Not a struct/union");
186                 } else {
187                     SymEntry* SizeSym = GetSizeOfScope (Struct);
188                     if (!SymIsDef (SizeSym) || !SymIsConst (SizeSym, &MemberSize)) {
189                         ErrorSkip ("Size of struct/union is unknown");
190                     }
191                 }
192                 MemberSize = Member (MemberSize);
193                 break;
194
195             case TOK_STRUCT:
196                 NextTok ();
197                 MemberSize = DoStructInternal (Offs, STRUCT);
198                 break;
199
200             case TOK_UNION:
201                 NextTok ();
202                 MemberSize = DoStructInternal (Offs, UNION);
203                 break;
204
205             default:
206                 if (!CheckConditionals ()) {
207                     /* Not a conditional directive */
208                     ErrorSkip ("Invalid storage allocator in struct/union");
209                 }
210         }
211
212         /* Assign the size to the member if it has a name */
213         if (Sym) {
214             DefSizeOfSymbol (Sym, MemberSize);
215         }
216
217         /* Next member */
218         if (Type == STRUCT) {
219             /* Struct */
220             Offs += MemberSize;
221             Size += MemberSize;
222         } else {
223             /* Union */
224             if (MemberSize > Size) {
225                 Size = MemberSize;
226             }
227         }
228
229         /* Expect end of line */
230         ConsumeSep ();
231     }
232
233     /* If this is not a anon struct, enter a special symbol named ".size"
234      * into the symbol table of the struct that holds the size of the
235      * struct. Since the symbol starts with a dot, it cannot be accessed
236      * by user code.
237      * Leave the struct scope level.
238      */
239     if (!Anon) {
240         /* Add a symbol */
241         SymEntry* SizeSym = GetSizeOfScope (CurrentScope);
242         SymDef (SizeSym, GenLiteralExpr (Size), ADDR_SIZE_DEFAULT, SF_NONE);
243
244         /* Close the struct scope */
245         SymLeaveLevel ();
246     }
247
248     /* End of struct/union definition */
249     if (Type == STRUCT) {
250         Consume (TOK_ENDSTRUCT, "`.ENDSTRUCT' expected");
251     } else {
252         Consume (TOK_ENDUNION, "`.ENDUNION' expected");
253     }
254
255     /* Return the size of the struct */
256     return Size;
257 }
258
259
260
261 long GetStructSize (SymTable* Struct)
262 /* Get the size of a struct or union */
263 {
264     SymEntry* Sym = SymFind (Struct, &SizeEntryName, SYM_FIND_EXISTING);
265     if (Sym == 0) {
266         Error ("Size of struct/union is unknown");
267         return 0;
268     } else {
269         return GetSymVal (Sym);
270     }
271 }
272
273
274
275 void DoStruct (void)
276 /* Handle the .STRUCT command */
277 {
278     DoStructInternal (0, STRUCT);
279 }
280
281
282
283 void DoUnion (void)
284 /* Handle the .UNION command */
285 {
286     DoStructInternal (0, UNION);
287 }
288
289
290