1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000-2013, 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"
67 /*****************************************************************************/
69 /*****************************************************************************/
73 static void Parse (void)
74 /* Top level parser routine. */
83 /* Parse until end of input */
84 while (CurTok.Tok != TOK_CEOF) {
88 /* Check for empty statements */
89 if (CurTok.Tok == TOK_SEMI) {
94 /* Disallow ASM statements on global level */
95 if (CurTok.Tok == TOK_ASM) {
96 Error ("__asm__ is not allowed here");
97 /* Parse and remove the statement for error recovery */
104 /* Check for a #pragma */
105 if (CurTok.Tok == TOK_PRAGMA) {
110 /* Read variable defs and functions */
111 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
113 /* Don't accept illegal storage classes */
114 if ((Spec.StorageClass & SC_TYPE) == 0) {
115 if ((Spec.StorageClass & SC_AUTO) != 0 ||
116 (Spec.StorageClass & SC_REGISTER) != 0) {
117 Error ("Illegal storage class");
118 Spec.StorageClass = SC_EXTERN | SC_STATIC;
122 /* Check if this is only a type declaration */
123 if (CurTok.Tok == TOK_SEMI) {
124 CheckEmptyDecl (&Spec);
129 /* Read declarations for this type */
136 /* Read the next declaration */
137 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
138 if (Decl.Ident[0] == '\0') {
143 /* Check if we must reserve storage for the variable. We do this,
145 * - if it is not a typedef or function,
146 * - if we don't had a storage class given ("int i")
147 * - if the storage class is explicitly specified as static,
148 * - or if there is an initialization.
150 * This means that "extern int i;" will not get storage allocated.
152 if ((Decl.StorageClass & SC_FUNC) != SC_FUNC &&
153 (Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF &&
154 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
155 (Decl.StorageClass & (SC_EXTERN|SC_STATIC)) == SC_STATIC ||
156 ((Decl.StorageClass & SC_EXTERN) != 0 &&
157 CurTok.Tok == TOK_ASSIGN))) {
159 /* We will allocate storage */
160 Decl.StorageClass |= SC_STORAGE | SC_DEF;
163 /* If this is a function declarator that is not followed by a comma
164 * or semicolon, it must be followed by a function body. If this is
165 * the case, convert an empty parameter list into one accepting no
166 * parameters (same as void) as required by the standard.
168 if ((Decl.StorageClass & SC_FUNC) != 0 &&
169 (CurTok.Tok != TOK_COMMA) &&
170 (CurTok.Tok != TOK_SEMI)) {
172 FuncDesc* D = GetFuncDesc (Decl.Type);
173 if (D->Flags & FD_EMPTY) {
174 D->Flags = (D->Flags & ~(FD_EMPTY | FD_VARIADIC)) | FD_VOID_PARAM;
178 /* Add an entry to the symbol table */
179 Entry = AddGlobalSym (Decl.Ident, Decl.Type, Decl.StorageClass);
181 /* Add declaration attributes */
182 SymUseAttr (Entry, &Decl);
184 /* Reserve storage for the variable if we need to */
185 if (Decl.StorageClass & SC_STORAGE) {
187 /* Get the size of the variable */
188 unsigned Size = SizeOf (Decl.Type);
190 /* Allow initialization */
191 if (CurTok.Tok == TOK_ASSIGN) {
193 /* We cannot initialize types of unknown size, or
194 * void types in non ANSI mode.
197 if (!IsTypeVoid (Decl.Type)) {
198 if (!IsTypeArray (Decl.Type)) {
199 /* Size is unknown and not an array */
200 Error ("Variable `%s' has unknown size", Decl.Ident);
202 } else if (IS_Get (&Standard) != STD_CC65) {
203 /* We cannot declare variables of type void */
204 Error ("Illegal type for variable `%s'", Decl.Ident);
208 /* Switch to the data or rodata segment. For arrays, check
209 * the element qualifiers, since not the array but its
210 * elements are const.
212 if (IsQualConst (GetBaseElementType (Decl.Type))) {
219 g_defgloblabel (Entry->Name);
224 /* Parse the initialization */
225 ParseInit (Entry->Type);
228 if (IsTypeVoid (Decl.Type)) {
229 /* We cannot declare variables of type void */
230 Error ("Illegal type for variable `%s'", Decl.Ident);
231 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
232 } else if (Size == 0) {
233 /* Size is unknown. Is it an array? */
234 if (!IsTypeArray (Decl.Type)) {
235 Error ("Variable `%s' has unknown size", Decl.Ident);
237 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
240 /* Allocate storage if it is still needed */
241 if (Entry->Flags & SC_STORAGE) {
243 /* Switch to the BSS segment */
247 g_defgloblabel (Entry->Name);
249 /* Allocate space for uninitialized variable */
256 /* Check for end of declaration list */
257 if (CurTok.Tok == TOK_COMMA) {
265 /* Function declaration? */
266 if (Entry && IsTypeFunc (Entry->Type)) {
270 if (CurTok.Tok == TOK_SEMI) {
275 /* Function body. Check for duplicate function definitions */
276 if (SymIsDef (Entry)) {
277 Error ("Body for function `%s' has already been defined",
281 /* Parse the function body */
288 /* Must be followed by a semicolon */
297 void Compile (const char* FileName)
298 /* Top level compile routine. Will setup things and call the parser. */
305 /* Since strftime is locale dependent, we need the abbreviated month names
308 static const char MonthNames[12][4] = {
309 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
310 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
313 /* Add macros that are always defined */
314 DefineNumericMacro ("__CC65__", GetVersionAsNumber ());
316 /* Language standard that is supported */
317 DefineNumericMacro ("__CC65_STD_C89__", STD_C89);
318 DefineNumericMacro ("__CC65_STD_C99__", STD_C99);
319 DefineNumericMacro ("__CC65_STD_CC65__", STD_CC65);
320 DefineNumericMacro ("__CC65_STD__", IS_Get (&Standard));
322 /* Optimization macros. Since no source code has been parsed for now, the
323 * IS_Get functions access the values in effect now, regardless of any
324 * changes using #pragma later.
326 if (IS_Get (&Optimize)) {
327 long CodeSize = IS_Get (&CodeSizeFactor);
328 DefineNumericMacro ("__OPT__", 1);
329 if (CodeSize > 100) {
330 DefineNumericMacro ("__OPT_i__", CodeSize);
332 if (IS_Get (&EnableRegVars)) {
333 DefineNumericMacro ("__OPT_r__", 1);
335 if (IS_Get (&InlineStdFuncs)) {
336 DefineNumericMacro ("__OPT_s__", 1);
340 /* __TIME__ and __DATE__ macros */
342 TM = localtime (&Time);
343 xsprintf (DateStr, sizeof (DateStr), "\"%s %2d %d\"",
344 MonthNames[TM->tm_mon], TM->tm_mday, TM->tm_year + 1900);
345 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
346 DefineTextMacro ("__DATE__", DateStr);
347 DefineTextMacro ("__TIME__", TimeStr);
349 /* Other standard macros */
350 /* DefineNumericMacro ("__STDC__", 1); <- not now */
351 DefineNumericMacro ("__STDC_HOSTED__", 1);
353 /* Create the base lexical level */
356 /* Create the global code and data segments */
357 CreateGlobalSegments ();
359 /* Initialize the literal pool */
362 /* Generate the code generator preamble */
365 /* Open the input file */
366 OpenMainFile (FileName);
368 /* Are we supposed to compile or just preprocess the input? */
369 if (PreprocessOnly) {
374 /* Preprocess each line and write it to the output file */
375 while (NextLine ()) {
377 WriteOutput ("%.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
380 /* Close the output file */
385 /* Ok, start the ball rolling... */
391 PrintMacroStats (stdout);
394 /* Print an error report */
400 void FinishCompile (void)
401 /* Emit literals, externals, debug info, do cleanup and optimizations */
406 /* Walk over all functions, doing cleanup, optimizations ... */
407 SymTab = GetGlobalSymTab ();
408 Func = SymTab->SymHead;
410 if (SymIsOutputFunc (Func)) {
411 /* Function which is defined and referenced or extern */
412 MoveLiteralPool (Func->V.F.LitPool);
413 CS_MergeLabels (Func->V.F.Seg->Code);
414 RunOpt (Func->V.F.Seg->Code);
416 Func = Func->NextSym;
419 /* Output the literal pool */
420 OutputLiteralPool ();
422 /* Emit debug infos if enabled */
425 /* Write imported/exported symbols */
428 /* Leave the main lexical level */