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