]> git.sur5r.net Git - cc65/blob - src/cc65/compile.c
Added the lineinfo module. Changed the complete code generation to use the
[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 "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     /* Go... */
71     NextToken ();
72     NextToken ();
73
74     /* Parse until end of input */
75     while (CurTok.Tok != TOK_CEOF) {
76
77         DeclSpec        Spec;
78         Declaration     Decl;
79         int             NeedStorage;
80
81         /* Check for empty statements */
82         if (CurTok.Tok == TOK_SEMI) {
83             NextToken ();
84             continue;
85         }
86
87         /* Check for an ASM statement (which is allowed also on global level) */
88         if (CurTok.Tok == TOK_ASM) {
89             doasm ();
90             ConsumeSemi ();
91             continue;
92         }
93
94         /* Check for a #pragma */
95         if (CurTok.Tok == TOK_PRAGMA) {
96             DoPragma ();
97             continue;
98         }
99
100         /* Read variable defs and functions */
101         ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
102
103         /* Don't accept illegal storage classes */
104         if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
105             Error ("Illegal storage class");
106             Spec.StorageClass = SC_EXTERN | SC_STATIC;
107         }
108
109         /* Check if this is only a type declaration */
110         if (CurTok.Tok == TOK_SEMI) {
111             CheckEmptyDecl (&Spec);
112             NextToken ();
113             continue;
114         }
115
116         /* Check if we must reserve storage for the variable. We do
117          * this if we don't had a storage class given ("int i") or
118          * if the storage class is explicitly specified as static.
119          * This means that "extern int i" will not get storage
120          * allocated.
121          */
122         NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
123                       ((Spec.Flags & DS_DEF_STORAGE) != 0  ||
124                       (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
125
126         /* Read declarations for this type */
127         Entry = 0;
128         comma = 0;
129         while (1) {
130
131             unsigned SymFlags;
132
133             /* Read the next declaration */
134             ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
135             if (Decl.Ident[0] == '\0') {
136                 NextToken ();
137                 break;
138             }
139
140             /* Get the symbol flags */
141             SymFlags = Spec.StorageClass;
142             if (IsTypeFunc (Decl.Type)) {
143                 SymFlags |= SC_FUNC;
144             } else {
145                 if (NeedStorage) {
146                     /* We will allocate storage, variable is defined */
147                     SymFlags |= SC_STORAGE | SC_DEF;
148                 }
149             }
150
151             /* Add an entry to the symbol table */
152             Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
153
154             /* Reserve storage for the variable if we need to */
155             if (SymFlags & SC_STORAGE) {
156
157                 /* Get the size of the variable */
158                 unsigned Size = SizeOf (Decl.Type);
159
160                 /* Allow initialization */
161                 if (CurTok.Tok == TOK_ASSIGN) {
162
163                     /* We cannot initialize types of unknown size, or
164                      * void types in non ANSI mode.
165                      */
166                     if (Size == 0) {
167                         if (!IsTypeVoid (Decl.Type)) {
168                             if (!IsTypeArray (Decl.Type)) {
169                                 /* Size is unknown and not an array */
170                                 Error ("Variable `%s' has unknown size", Decl.Ident);
171                             }
172                         } else if (ANSI) {
173                             /* We cannot declare variables of type void */
174                             Error ("Illegal type for variable `%s'", Decl.Ident);
175                         }
176                     }
177
178                     /* Switch to the data or rodata segment */
179                     if (IsQualConst (Decl.Type)) {
180                         g_userodata ();
181                     } else {
182                         g_usedata ();
183                     }
184
185                     /* Define a label */
186                     g_defgloblabel (Entry->Name);
187
188                     /* Skip the '=' */
189                     NextToken ();
190
191                     /* Parse the initialization */
192                     ParseInit (Entry->Type);
193                 } else {
194
195                     if (IsTypeVoid (Decl.Type)) {
196                         /* We cannot declare variables of type void */
197                         Error ("Illegal type for variable `%s'", Decl.Ident);
198                     } else if (Size == 0) {
199                         /* Size is unknown */
200                         Error ("Variable `%s' has unknown size", Decl.Ident);
201                     }
202
203                     /* Switch to the BSS segment */
204                     g_usebss ();
205
206                     /* Define a label */
207                     g_defgloblabel (Entry->Name);
208
209                     /* Allocate space for uninitialized variable */
210                     g_res (SizeOf (Entry->Type));
211                 }
212
213             }
214
215             /* Check for end of declaration list */
216             if (CurTok.Tok == TOK_COMMA) {
217                 NextToken ();
218                 comma = 1;
219             } else {
220                 break;
221             }
222         }
223
224         /* Function declaration? */
225         if (Entry && IsTypeFunc (Entry->Type)) {
226
227             /* Function */
228             if (!comma) {
229
230                 if (CurTok.Tok == TOK_SEMI) {
231
232                     /* Prototype only */
233                     NextToken ();
234
235                 } else {
236                     if (Entry) {
237                         NewFunc (Entry);
238                     }
239                 }
240             }
241
242         } else {
243
244             /* Must be followed by a semicolon */
245             ConsumeSemi ();
246
247         }
248     }
249 }
250
251
252
253 void Compile (void)
254 /* Top level compile routine. Will setup things and call the parser. */
255 {
256     char* Path;
257
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     /* Initialize the literal pool */
295     InitLiteralPool ();
296
297     /* Create the base lexical level */
298     EnterGlobalLevel ();
299
300     /* Generate the code generator preamble */
301     g_preamble ();
302
303     /* Ok, start the ball rolling... */
304     Parse ();
305
306     /* Dump the literal pool. */
307     DumpLiteralPool ();
308
309     /* Write imported/exported symbols */
310     EmitExternals ();
311
312     if (Debug) {
313         PrintLiteralPoolStats (stdout);
314         PrintMacroStats (stdout);
315     }
316
317     /* Leave the main lexical level */
318     LeaveGlobalLevel ();
319
320     /* Print an error report */
321     ErrorReport ();
322 }
323
324
325