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