1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000-2008 Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
40 #include "debugflag.h"
65 /*****************************************************************************/
67 /*****************************************************************************/
71 static void Parse (void)
72 /* Top level parser routine. */
81 /* Parse until end of input */
82 while (CurTok.Tok != TOK_CEOF) {
88 /* Check for empty statements */
89 if (CurTok.Tok == TOK_SEMI) {
94 /* Check for an ASM statement (which is allowed also on global level) */
95 if (CurTok.Tok == TOK_ASM) {
101 /* Check for a #pragma */
102 if (CurTok.Tok == TOK_PRAGMA) {
107 /* Read variable defs and functions */
108 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
110 /* Don't accept illegal storage classes */
111 if ((Spec.StorageClass & SC_TYPE) == 0) {
112 if ((Spec.StorageClass & SC_AUTO) != 0 ||
113 (Spec.StorageClass & SC_REGISTER) != 0) {
114 Error ("Illegal storage class");
115 Spec.StorageClass = SC_EXTERN | SC_STATIC;
119 /* Check if this is only a type declaration */
120 if (CurTok.Tok == TOK_SEMI) {
121 CheckEmptyDecl (&Spec);
126 /* Check if we must reserve storage for the variable. We do
127 * this if we don't had a storage class given ("int i") or
128 * if the storage class is explicitly specified as static.
129 * This means that "extern int i" will not get storage
132 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
133 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
134 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
136 /* Read declarations for this type */
141 /* Read the next declaration */
142 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
143 if (Decl.Ident[0] == '\0') {
148 /* Get the symbol flags */
149 if (IsTypeFunc (Decl.Type)) {
150 Decl.StorageClass |= SC_FUNC;
151 } else if ((Decl.StorageClass & SC_TYPEDEF) == 0) {
152 if ((Spec.Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
153 Warning ("Implicit `int' is an obsolete feature");
156 /* We will allocate storage, variable is defined */
157 Decl.StorageClass |= SC_STORAGE | SC_DEF;
161 /* Add an entry to the symbol table */
162 Entry = AddGlobalSym (Decl.Ident, Decl.Type, Decl.StorageClass);
164 /* Reserve storage for the variable if we need to */
165 if (Decl.StorageClass & SC_STORAGE) {
167 /* Get the size of the variable */
168 unsigned Size = SizeOf (Decl.Type);
170 /* Allow initialization */
171 if (CurTok.Tok == TOK_ASSIGN) {
173 /* We cannot initialize types of unknown size, or
174 * void types in non ANSI mode.
177 if (!IsTypeVoid (Decl.Type)) {
178 if (!IsTypeArray (Decl.Type)) {
179 /* Size is unknown and not an array */
180 Error ("Variable `%s' has unknown size", Decl.Ident);
182 } else if (IS_Get (&Standard) != STD_CC65) {
183 /* We cannot declare variables of type void */
184 Error ("Illegal type for variable `%s'", Decl.Ident);
188 /* Switch to the data or rodata segment */
189 if (IsQualConst (Decl.Type)) {
196 g_defgloblabel (Entry->Name);
201 /* Parse the initialization */
202 ParseInit (Entry->Type);
205 if (IsTypeVoid (Decl.Type)) {
206 /* We cannot declare variables of type void */
207 Error ("Illegal type for variable `%s'", Decl.Ident);
208 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
209 } else if (Size == 0) {
210 /* Size is unknown. Is it an array? */
211 if (!IsTypeArray (Decl.Type)) {
212 Error ("Variable `%s' has unknown size", Decl.Ident);
214 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
217 /* Allocate storage if it is still needed */
218 if (Entry->Flags & SC_STORAGE) {
220 /* Switch to the BSS segment */
224 g_defgloblabel (Entry->Name);
226 /* Allocate space for uninitialized variable */
233 /* Check for end of declaration list */
234 if (CurTok.Tok == TOK_COMMA) {
242 /* Function declaration? */
243 if (Entry && IsTypeFunc (Entry->Type)) {
247 if (CurTok.Tok == TOK_SEMI) {
254 /* Function body. Check for duplicate function definitions */
255 if (SymIsDef (Entry)) {
256 Error ("Body for function `%s' has already been defined",
260 /* An empty parameter list in a function definition means
261 * that the function doesn't take any parameters. The same
262 * in a declarator means that the function can take any
263 * number of parameters. This seems weird but is necessary
264 * to support old K&R style programs.
267 if (D->Flags & FD_EMPTY) {
268 D->Flags = (D->Flags & ~(FD_EMPTY | FD_VARIADIC)) | FD_VOID_PARAM;
271 /* Parse the function body */
278 /* Must be followed by a semicolon */
287 void Compile (const char* FileName)
288 /* Top level compile routine. Will setup things and call the parser. */
295 /* Since strftime is locale dependent, we need the abbreviated month names
298 static const char MonthNames[12][4] = {
299 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
300 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
303 /* Add macros that are always defined */
304 DefineNumericMacro ("__CC65__", VERSION);
306 /* Language standard that is supported */
307 DefineNumericMacro ("__CC65_STD_C89__", STD_C89);
308 DefineNumericMacro ("__CC65_STD_C99__", STD_C99);
309 DefineNumericMacro ("__CC65_STD_CC65__", STD_CC65);
310 DefineNumericMacro ("__CC65_STD__", IS_Get (&Standard));
312 /* Optimization macros. Since no source code has been parsed for now, the
313 * IS_Get functions access the values in effect now, regardless of any
314 * changes using #pragma later.
316 if (IS_Get (&Optimize)) {
317 long CodeSize = IS_Get (&CodeSizeFactor);
318 DefineNumericMacro ("__OPT__", 1);
319 if (CodeSize > 100) {
320 DefineNumericMacro ("__OPT_i__", CodeSize);
322 if (IS_Get (&EnableRegVars)) {
323 DefineNumericMacro ("__OPT_r__", 1);
325 if (IS_Get (&InlineStdFuncs)) {
326 DefineNumericMacro ("__OPT_s__", 1);
330 /* __TIME__ and __DATE__ macros */
332 TM = localtime (&Time);
333 xsprintf (DateStr, sizeof (DateStr), "\"%s %2d %d\"",
334 MonthNames[TM->tm_mon], TM->tm_mday, TM->tm_year + 1900);
335 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
336 DefineTextMacro ("__DATE__", DateStr);
337 DefineTextMacro ("__TIME__", TimeStr);
339 /* Initialize the literal pool */
342 /* Create the base lexical level */
345 /* Generate the code generator preamble */
348 /* Open the input file */
349 OpenMainFile (FileName);
351 /* Are we supposed to compile or just preprocess the input? */
352 if (PreprocessOnly) {
354 while (NextLine ()) {
356 printf ("%.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
360 PrintMacroStats (stdout);
365 /* Ok, start the ball rolling... */
368 /* Dump the literal pool. */
371 /* Write imported/exported symbols */
375 PrintLiteralPoolStats (stdout);
376 PrintMacroStats (stdout);
381 /* Leave the main lexical level */
384 /* Print an error report */