1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000-2002 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static void Parse (void)
70 /* Top level parser routine. */
79 /* Parse until end of input */
80 while (CurTok.Tok != TOK_CEOF) {
86 /* Check for empty statements */
87 if (CurTok.Tok == TOK_SEMI) {
92 /* Check for an ASM statement (which is allowed also on global level) */
93 if (CurTok.Tok == TOK_ASM) {
99 /* Check for a #pragma */
100 if (CurTok.Tok == TOK_PRAGMA) {
105 /* Read variable defs and functions */
106 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
108 /* Don't accept illegal storage classes */
109 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
110 Error ("Illegal storage class");
111 Spec.StorageClass = SC_EXTERN | SC_STATIC;
114 /* Check if this is only a type declaration */
115 if (CurTok.Tok == TOK_SEMI) {
116 CheckEmptyDecl (&Spec);
121 /* Check if we must reserve storage for the variable. We do
122 * this if we don't had a storage class given ("int i") or
123 * if the storage class is explicitly specified as static.
124 * This means that "extern int i" will not get storage
127 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
128 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
129 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
131 /* Read declarations for this type */
138 /* Read the next declaration */
139 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
140 if (Decl.Ident[0] == '\0') {
145 /* Get the symbol flags */
146 SymFlags = Spec.StorageClass;
147 if (IsTypeFunc (Decl.Type)) {
151 /* We will allocate storage, variable is defined */
152 SymFlags |= SC_STORAGE | SC_DEF;
156 /* Add an entry to the symbol table */
157 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
159 /* Reserve storage for the variable if we need to */
160 if (SymFlags & SC_STORAGE) {
162 /* Get the size of the variable */
163 unsigned Size = SizeOf (Decl.Type);
165 /* Allow initialization */
166 if (CurTok.Tok == TOK_ASSIGN) {
168 /* We cannot initialize types of unknown size, or
169 * void types in non ANSI mode.
172 if (!IsTypeVoid (Decl.Type)) {
173 if (!IsTypeArray (Decl.Type)) {
174 /* Size is unknown and not an array */
175 Error ("Variable `%s' has unknown size", Decl.Ident);
178 /* We cannot declare variables of type void */
179 Error ("Illegal type for variable `%s'", Decl.Ident);
183 /* Switch to the data or rodata segment */
184 if (IsQualConst (Decl.Type)) {
191 g_defgloblabel (Entry->Name);
196 /* Parse the initialization */
197 ParseInit (Entry->Type);
200 if (IsTypeVoid (Decl.Type)) {
201 /* We cannot declare variables of type void */
202 Error ("Illegal type for variable `%s'", Decl.Ident);
203 } else if (Size == 0) {
204 /* Size is unknown */
205 Error ("Variable `%s' has unknown size", Decl.Ident);
208 /* Switch to the BSS segment */
212 g_defgloblabel (Entry->Name);
214 /* Allocate space for uninitialized variable */
215 g_res (SizeOf (Entry->Type));
220 /* Check for end of declaration list */
221 if (CurTok.Tok == TOK_COMMA) {
229 /* Function declaration? */
230 if (Entry && IsTypeFunc (Entry->Type)) {
235 if (CurTok.Tok == TOK_SEMI) {
249 /* Must be followed by a semicolon */
258 void Compile (const char* FileName)
259 /* Top level compile routine. Will setup things and call the parser. */
268 /* Since strftime is locale dependent, we need the abbreviated month names
271 static const char MonthNames[12][4] = {
272 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
273 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
276 /* Add some standard paths to the include search path */
277 AddIncludePath ("", INC_USER); /* Current directory */
278 AddIncludePath ("include", INC_SYS);
280 AddIncludePath (CC65_INC, INC_SYS);
282 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
284 Path = getenv ("CC65_INC");
286 AddIncludePath (Path, INC_SYS | INC_USER);
289 /* Add macros that are always defined */
290 DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
292 /* Strict ANSI macro */
294 DefineNumericMacro ("__STRICT_ANSI__", 1);
297 /* Optimization macros */
299 DefineNumericMacro ("__OPT__", 1);
300 if (FavourSize == 0) {
301 DefineNumericMacro ("__OPT_i__", 1);
304 DefineNumericMacro ("__OPT_r__", 1);
306 if (InlineStdFuncs) {
307 DefineNumericMacro ("__OPT_s__", 1);
311 /* __TIME__ and __DATE__ macros */
313 TM = localtime (&Time);
314 strftime (Buf, sizeof (Buf), "%e %Y", TM);
315 xsprintf (DateStr, sizeof (DateStr), "\"%s %s\"", MonthNames[TM->tm_mon], Buf);
316 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
317 DefineTextMacro ("__DATE__", DateStr);
318 DefineTextMacro ("__TIME__", TimeStr);
320 /* Initialize the literal pool */
323 /* Create the base lexical level */
326 /* Generate the code generator preamble */
329 /* Open the input file */
330 OpenMainFile (FileName);
332 /* Ok, start the ball rolling... */
335 /* Dump the literal pool. */
338 /* Write imported/exported symbols */
342 PrintLiteralPoolStats (stdout);
343 PrintMacroStats (stdout);
346 /* Leave the main lexical level */
349 /* Print an error report */