]> git.sur5r.net Git - cc65/blob - src/cc65/compile.c
The check for illegal storage classes on globals was wrong
[cc65] / src / cc65 / compile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 compile.c                                 */
4 /*                                                                           */
5 /*                       Top level compiler subroutine                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2002 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 #include <stdlib.h>
37 #include <time.h>
38
39 /* common */
40 #include "debugflag.h"
41 #include "version.h"
42 #include "xmalloc.h"
43 #include "xsprintf.h"
44
45 /* cc65 */
46 #include "asmlabel.h"
47 #include "asmstmt.h"
48 #include "codegen.h"
49 #include "declare.h"
50 #include "error.h"
51 #include "expr.h"
52 #include "function.h"
53 #include "global.h"
54 #include "incpath.h"
55 #include "input.h"
56 #include "litpool.h"
57 #include "macrotab.h"
58 #include "pragma.h"
59 #include "symtab.h"
60 #include "compile.h"
61
62
63
64 /*****************************************************************************/
65 /*                                   Code                                    */
66 /*****************************************************************************/
67
68
69
70 static void Parse (void)
71 /* Top level parser routine. */
72 {
73     int comma;
74     SymEntry* Entry;
75
76     /* Go... */
77     NextToken ();
78     NextToken ();
79
80     /* Parse until end of input */
81     while (CurTok.Tok != TOK_CEOF) {
82
83         DeclSpec        Spec;
84         Declaration     Decl;
85         int             NeedStorage;
86
87         /* Check for empty statements */
88         if (CurTok.Tok == TOK_SEMI) {
89             NextToken ();
90             continue;
91         }
92
93         /* Check for an ASM statement (which is allowed also on global level) */
94         if (CurTok.Tok == TOK_ASM) {
95             AsmStatement ();
96             ConsumeSemi ();
97             continue;
98         }
99
100         /* Check for a #pragma */
101         if (CurTok.Tok == TOK_PRAGMA) {
102             DoPragma ();
103             continue;
104         }
105
106         /* Read variable defs and functions */
107         ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
108
109         /* Don't accept illegal storage classes */
110         if ((Spec.StorageClass & SC_AUTO) != 0 || 
111             (Spec.StorageClass & SC_REGISTER) != 0) {
112             Error ("Illegal storage class");
113             Spec.StorageClass = SC_EXTERN | SC_STATIC;
114         }
115
116         /* Check if this is only a type declaration */
117         if (CurTok.Tok == TOK_SEMI) {
118             CheckEmptyDecl (&Spec);
119             NextToken ();
120             continue;
121         }
122
123         /* Check if we must reserve storage for the variable. We do
124          * this if we don't had a storage class given ("int i") or
125          * if the storage class is explicitly specified as static.
126          * This means that "extern int i" will not get storage
127          * allocated.
128          */
129         NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
130                       ((Spec.Flags & DS_DEF_STORAGE) != 0  ||
131                       (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
132
133         /* Read declarations for this type */
134         Entry = 0;
135         comma = 0;
136         while (1) {
137
138             unsigned SymFlags;
139
140             /* Read the next declaration */
141             ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
142             if (Decl.Ident[0] == '\0') {
143                 NextToken ();
144                 break;
145             }
146
147             /* Get the symbol flags */
148             SymFlags = Spec.StorageClass;
149             if (IsTypeFunc (Decl.Type)) {
150                 SymFlags |= SC_FUNC;
151             } else {
152                 if (NeedStorage) {
153                     /* We will allocate storage, variable is defined */
154                     SymFlags |= SC_STORAGE | SC_DEF;
155                 }
156             }
157
158             /* Add an entry to the symbol table */
159             Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
160
161             /* Reserve storage for the variable if we need to */
162             if (SymFlags & SC_STORAGE) {
163
164                 /* Get the size of the variable */
165                 unsigned Size = SizeOf (Decl.Type);
166
167                 /* Allow initialization */
168                 if (CurTok.Tok == TOK_ASSIGN) {
169
170                     /* We cannot initialize types of unknown size, or
171                      * void types in non ANSI mode.
172                      */
173                     if (Size == 0) {
174                         if (!IsTypeVoid (Decl.Type)) {
175                             if (!IsTypeArray (Decl.Type)) {
176                                 /* Size is unknown and not an array */
177                                 Error ("Variable `%s' has unknown size", Decl.Ident);
178                             }
179                         } else if (ANSI) {
180                             /* We cannot declare variables of type void */
181                             Error ("Illegal type for variable `%s'", Decl.Ident);
182                         }
183                     }
184
185                     /* Switch to the data or rodata segment */
186                     if (IsQualConst (Decl.Type)) {
187                         g_userodata ();
188                     } else {
189                         g_usedata ();
190                     }
191
192                     /* Define a label */
193                     g_defgloblabel (Entry->Name);
194
195                     /* Skip the '=' */
196                     NextToken ();
197
198                     /* Parse the initialization */
199                     ParseInit (Entry->Type);
200                 } else {
201
202                     if (IsTypeVoid (Decl.Type)) {
203                         /* We cannot declare variables of type void */
204                         Error ("Illegal type for variable `%s'", Decl.Ident);
205                     } else if (Size == 0) {
206                         /* Size is unknown */
207                         Error ("Variable `%s' has unknown size", Decl.Ident);
208                     }
209
210                     /* Switch to the BSS segment */
211                     g_usebss ();
212
213                     /* Define a label */
214                     g_defgloblabel (Entry->Name);
215
216                     /* Allocate space for uninitialized variable */
217                     g_res (SizeOf (Entry->Type));
218                 }
219
220             }
221
222             /* Check for end of declaration list */
223             if (CurTok.Tok == TOK_COMMA) {
224                 NextToken ();
225                 comma = 1;
226             } else {
227                 break;
228             }
229         }
230
231         /* Function declaration? */
232         if (Entry && IsTypeFunc (Entry->Type)) {
233
234             /* Function */
235             if (!comma) {
236                 if (CurTok.Tok == TOK_SEMI) {
237                     /* Prototype only */
238                     NextToken ();
239                 } else if (Entry) {
240                     /* Function body definition */
241                     if (SymIsDef (Entry)) {
242                         Error ("Body for function `%s' has already been defined",
243                                Entry->Name);
244                     }
245                     NewFunc (Entry);
246                 }
247             }
248
249         } else {
250
251             /* Must be followed by a semicolon */
252             ConsumeSemi ();
253
254         }
255     }
256 }
257
258
259
260 void Compile (const char* FileName)
261 /* Top level compile routine. Will setup things and call the parser. */
262 {
263     char*       Path;
264     char        Buf[20];
265     char        DateStr[20];
266     char        TimeStr[20];
267     time_t      Time;
268     struct tm*  TM;
269
270     /* Since strftime is locale dependent, we need the abbreviated month names
271      * in english.
272      */
273     static const char MonthNames[12][4] = {
274         "Jan", "Feb", "Mar", "Apr", "May", "Jun",
275         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
276     };
277
278     /* Add some standard paths to the include search path */
279     AddIncludePath ("", INC_USER);              /* Current directory */
280     AddIncludePath ("include", INC_SYS);
281 #ifdef CC65_INC
282     AddIncludePath (CC65_INC, INC_SYS);
283 #else
284     AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
285 #endif
286     Path = getenv ("CC65_INC");
287     if (Path) {
288         AddIncludePath (Path, INC_SYS | INC_USER);
289     }
290
291     /* Add macros that are always defined */
292     DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
293
294     /* Strict ANSI macro */
295     if (ANSI) {
296         DefineNumericMacro ("__STRICT_ANSI__", 1);
297     }
298
299     /* Optimization macros */
300     if (Optimize) {
301         DefineNumericMacro ("__OPT__", 1);
302         if (FavourSize == 0) {
303             DefineNumericMacro ("__OPT_i__", 1);
304         }
305         if (EnableRegVars) {
306             DefineNumericMacro ("__OPT_r__", 1);
307         }
308         if (InlineStdFuncs) {
309             DefineNumericMacro ("__OPT_s__", 1);
310         }
311     }
312
313     /* __TIME__ and __DATE__ macros */
314     Time = time (0);
315     TM   = localtime (&Time);
316     strftime (Buf, sizeof (Buf), "%e %Y", TM);
317     xsprintf (DateStr, sizeof (DateStr), "\"%s %s\"", MonthNames[TM->tm_mon], Buf);
318     strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
319     DefineTextMacro ("__DATE__", DateStr);
320     DefineTextMacro ("__TIME__", TimeStr);
321
322     /* Initialize the literal pool */
323     InitLiteralPool ();
324
325     /* Create the base lexical level */
326     EnterGlobalLevel ();
327
328     /* Generate the code generator preamble */
329     g_preamble ();
330
331     /* Open the input file */
332     OpenMainFile (FileName);
333
334     /* Ok, start the ball rolling... */
335     Parse ();
336
337     /* Dump the literal pool. */
338     DumpLiteralPool ();
339
340     /* Write imported/exported symbols */
341     EmitExternals ();
342
343     if (Debug) {
344         PrintLiteralPoolStats (stdout);
345         PrintMacroStats (stdout);
346     }
347
348     /* Leave the main lexical level */
349     LeaveGlobalLevel ();
350
351     /* Print an error report */
352     ErrorReport ();
353 }
354
355
356