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