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