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