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