1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000-2009, 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"
66 /*****************************************************************************/
68 /*****************************************************************************/
72 static void Parse (void)
73 /* Top level parser routine. */
82 /* Parse until end of input */
83 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 /* Read declarations for this type */
131 /* Read the next declaration */
132 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
133 if (Decl.Ident[0] == '\0') {
138 /* Check if we must reserve storage for the variable. We do this,
139 * if it is not a typedef or function, if we don't had a storage
140 * class given ("int i") or if the storage class is explicitly
141 * specified as static. This means that "extern int i" will not
142 * get storage allocated.
144 if ((Decl.StorageClass & SC_FUNC) != SC_FUNC &&
145 (Decl.StorageClass & SC_TYPEDEF) != SC_TYPEDEF &&
146 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
147 (Decl.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC)) {
149 /* We will allocate storage */
150 Decl.StorageClass |= SC_STORAGE | SC_DEF;
153 /* If this is a function declarator that is not followed by a comma
154 * or semicolon, it must be followed by a function body. If this is
155 * the case, convert an empty parameter list into one accepting no
156 * parameters (same as void) as required by the standard.
158 if ((Decl.StorageClass & SC_FUNC) != 0 &&
159 (CurTok.Tok != TOK_COMMA) &&
160 (CurTok.Tok != TOK_SEMI)) {
162 FuncDesc* D = GetFuncDesc (Decl.Type);
163 if (D->Flags & FD_EMPTY) {
164 D->Flags = (D->Flags & ~(FD_EMPTY | FD_VARIADIC)) | FD_VOID_PARAM;
168 /* Add an entry to the symbol table */
169 Entry = AddGlobalSym (Decl.Ident, Decl.Type, Decl.StorageClass);
171 /* Reserve storage for the variable if we need to */
172 if (Decl.StorageClass & SC_STORAGE) {
174 /* Get the size of the variable */
175 unsigned Size = SizeOf (Decl.Type);
177 /* Allow initialization */
178 if (CurTok.Tok == TOK_ASSIGN) {
180 /* We cannot initialize types of unknown size, or
181 * void types in non ANSI mode.
184 if (!IsTypeVoid (Decl.Type)) {
185 if (!IsTypeArray (Decl.Type)) {
186 /* Size is unknown and not an array */
187 Error ("Variable `%s' has unknown size", Decl.Ident);
189 } else if (IS_Get (&Standard) != STD_CC65) {
190 /* We cannot declare variables of type void */
191 Error ("Illegal type for variable `%s'", Decl.Ident);
195 /* Switch to the data or rodata segment. For arrays, check
196 * the element qualifiers, since not the array but its
197 * elements are const.
199 if (IsQualConst (Decl.Type) ||
200 (IsTypeArray (Decl.Type) &&
201 IsQualConst (GetElementType (Decl.Type)))) {
208 g_defgloblabel (Entry->Name);
213 /* Parse the initialization */
214 ParseInit (Entry->Type);
217 if (IsTypeVoid (Decl.Type)) {
218 /* We cannot declare variables of type void */
219 Error ("Illegal type for variable `%s'", Decl.Ident);
220 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
221 } else if (Size == 0) {
222 /* Size is unknown. Is it an array? */
223 if (!IsTypeArray (Decl.Type)) {
224 Error ("Variable `%s' has unknown size", Decl.Ident);
226 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
229 /* Allocate storage if it is still needed */
230 if (Entry->Flags & SC_STORAGE) {
232 /* Switch to the BSS segment */
236 g_defgloblabel (Entry->Name);
238 /* Allocate space for uninitialized variable */
245 /* Check for end of declaration list */
246 if (CurTok.Tok == TOK_COMMA) {
254 /* Function declaration? */
255 if (Entry && IsTypeFunc (Entry->Type)) {
259 if (CurTok.Tok == TOK_SEMI) {
264 /* Function body. Check for duplicate function definitions */
265 if (SymIsDef (Entry)) {
266 Error ("Body for function `%s' has already been defined",
270 /* Parse the function body */
277 /* Must be followed by a semicolon */
286 void Compile (const char* FileName)
287 /* Top level compile routine. Will setup things and call the parser. */
294 /* Since strftime is locale dependent, we need the abbreviated month names
297 static const char MonthNames[12][4] = {
298 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
299 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
302 /* Add macros that are always defined */
303 DefineNumericMacro ("__CC65__", GetVersionAsNumber ());
305 /* Language standard that is supported */
306 DefineNumericMacro ("__CC65_STD_C89__", STD_C89);
307 DefineNumericMacro ("__CC65_STD_C99__", STD_C99);
308 DefineNumericMacro ("__CC65_STD_CC65__", STD_CC65);
309 DefineNumericMacro ("__CC65_STD__", IS_Get (&Standard));
311 /* Optimization macros. Since no source code has been parsed for now, the
312 * IS_Get functions access the values in effect now, regardless of any
313 * changes using #pragma later.
315 if (IS_Get (&Optimize)) {
316 long CodeSize = IS_Get (&CodeSizeFactor);
317 DefineNumericMacro ("__OPT__", 1);
318 if (CodeSize > 100) {
319 DefineNumericMacro ("__OPT_i__", CodeSize);
321 if (IS_Get (&EnableRegVars)) {
322 DefineNumericMacro ("__OPT_r__", 1);
324 if (IS_Get (&InlineStdFuncs)) {
325 DefineNumericMacro ("__OPT_s__", 1);
329 /* __TIME__ and __DATE__ macros */
331 TM = localtime (&Time);
332 xsprintf (DateStr, sizeof (DateStr), "\"%s %2d %d\"",
333 MonthNames[TM->tm_mon], TM->tm_mday, TM->tm_year + 1900);
334 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
335 DefineTextMacro ("__DATE__", DateStr);
336 DefineTextMacro ("__TIME__", TimeStr);
338 /* Initialize the literal pool */
341 /* Create the base lexical level */
344 /* Generate the code generator preamble */
347 /* Open the input file */
348 OpenMainFile (FileName);
350 /* Are we supposed to compile or just preprocess the input? */
351 if (PreprocessOnly) {
356 /* Preprocess each line and write it to the output file */
357 while (NextLine ()) {
359 WriteOutput ("%.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
362 /* Close the output file */
366 PrintMacroStats (stdout);
371 /* Ok, start the ball rolling... */
374 /* Dump the literal pool. */
377 /* Write imported/exported symbols */
381 PrintLiteralPoolStats (stdout);
382 PrintMacroStats (stdout);
387 /* Leave the main lexical level */
390 /* Print an error report */