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) == 0 &&
145 (Decl.StorageClass & SC_TYPEDEF) == 0 &&
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 */
196 if (IsQualConst (Decl.Type)) {
203 g_defgloblabel (Entry->Name);
208 /* Parse the initialization */
209 ParseInit (Entry->Type);
212 if (IsTypeVoid (Decl.Type)) {
213 /* We cannot declare variables of type void */
214 Error ("Illegal type for variable `%s'", Decl.Ident);
215 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
216 } else if (Size == 0) {
217 /* Size is unknown. Is it an array? */
218 if (!IsTypeArray (Decl.Type)) {
219 Error ("Variable `%s' has unknown size", Decl.Ident);
221 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
224 /* Allocate storage if it is still needed */
225 if (Entry->Flags & SC_STORAGE) {
227 /* Switch to the BSS segment */
231 g_defgloblabel (Entry->Name);
233 /* Allocate space for uninitialized variable */
240 /* Check for end of declaration list */
241 if (CurTok.Tok == TOK_COMMA) {
249 /* Function declaration? */
250 if (Entry && IsTypeFunc (Entry->Type)) {
254 if (CurTok.Tok == TOK_SEMI) {
259 /* Function body. Check for duplicate function definitions */
260 if (SymIsDef (Entry)) {
261 Error ("Body for function `%s' has already been defined",
265 /* Parse the function body */
272 /* Must be followed by a semicolon */
281 void Compile (const char* FileName)
282 /* Top level compile routine. Will setup things and call the parser. */
289 /* Since strftime is locale dependent, we need the abbreviated month names
292 static const char MonthNames[12][4] = {
293 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
294 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
297 /* Add macros that are always defined */
298 DefineNumericMacro ("__CC65__", VERSION);
300 /* Language standard that is supported */
301 DefineNumericMacro ("__CC65_STD_C89__", STD_C89);
302 DefineNumericMacro ("__CC65_STD_C99__", STD_C99);
303 DefineNumericMacro ("__CC65_STD_CC65__", STD_CC65);
304 DefineNumericMacro ("__CC65_STD__", IS_Get (&Standard));
306 /* Optimization macros. Since no source code has been parsed for now, the
307 * IS_Get functions access the values in effect now, regardless of any
308 * changes using #pragma later.
310 if (IS_Get (&Optimize)) {
311 long CodeSize = IS_Get (&CodeSizeFactor);
312 DefineNumericMacro ("__OPT__", 1);
313 if (CodeSize > 100) {
314 DefineNumericMacro ("__OPT_i__", CodeSize);
316 if (IS_Get (&EnableRegVars)) {
317 DefineNumericMacro ("__OPT_r__", 1);
319 if (IS_Get (&InlineStdFuncs)) {
320 DefineNumericMacro ("__OPT_s__", 1);
324 /* __TIME__ and __DATE__ macros */
326 TM = localtime (&Time);
327 xsprintf (DateStr, sizeof (DateStr), "\"%s %2d %d\"",
328 MonthNames[TM->tm_mon], TM->tm_mday, TM->tm_year + 1900);
329 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
330 DefineTextMacro ("__DATE__", DateStr);
331 DefineTextMacro ("__TIME__", TimeStr);
333 /* Initialize the literal pool */
336 /* Create the base lexical level */
339 /* Generate the code generator preamble */
342 /* Open the input file */
343 OpenMainFile (FileName);
345 /* Are we supposed to compile or just preprocess the input? */
346 if (PreprocessOnly) {
351 /* Preprocess each line and write it to the output file */
352 while (NextLine ()) {
354 WriteOutput ("%.*s\n", SB_GetLen (Line), SB_GetConstBuf (Line));
357 /* Close the output file */
361 PrintMacroStats (stdout);
366 /* Ok, start the ball rolling... */
369 /* Dump the literal pool. */
372 /* Write imported/exported symbols */
376 PrintLiteralPoolStats (stdout);
377 PrintMacroStats (stdout);
382 /* Leave the main lexical level */
385 /* Print an error report */