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