1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000-2001 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 /*****************************************************************************/
60 /*****************************************************************************/
62 /*****************************************************************************/
66 static void Parse (void)
67 /* Top level parser routine. */
76 /* Parse until end of input */
77 while (CurTok.Tok != TOK_CEOF) {
83 /* Check for empty statements */
84 if (CurTok.Tok == TOK_SEMI) {
89 /* Check for an ASM statement (which is allowed also on global level) */
90 if (CurTok.Tok == TOK_ASM) {
96 /* Check for a #pragma */
97 if (CurTok.Tok == TOK_PRAGMA) {
102 /* Read variable defs and functions */
103 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
105 /* Don't accept illegal storage classes */
106 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
107 Error ("Illegal storage class");
108 Spec.StorageClass = SC_EXTERN | SC_STATIC;
111 /* Check if this is only a type declaration */
112 if (CurTok.Tok == TOK_SEMI) {
113 CheckEmptyDecl (&Spec);
118 /* Check if we must reserve storage for the variable. We do
119 * this if we don't had a storage class given ("int i") or
120 * if the storage class is explicitly specified as static.
121 * This means that "extern int i" will not get storage
124 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
125 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
126 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
128 /* Read declarations for this type */
135 /* Read the next declaration */
136 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
137 if (Decl.Ident[0] == '\0') {
142 /* Get the symbol flags */
143 SymFlags = Spec.StorageClass;
144 if (IsTypeFunc (Decl.Type)) {
148 /* We will allocate storage, variable is defined */
149 SymFlags |= SC_STORAGE | SC_DEF;
153 /* Add an entry to the symbol table */
154 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
156 /* Reserve storage for the variable if we need to */
157 if (SymFlags & SC_STORAGE) {
159 /* Get the size of the variable */
160 unsigned Size = SizeOf (Decl.Type);
162 /* Allow initialization */
163 if (CurTok.Tok == TOK_ASSIGN) {
165 /* We cannot initialize types of unknown size, or
166 * void types in non ANSI mode.
169 if (!IsTypeVoid (Decl.Type)) {
170 if (!IsTypeArray (Decl.Type)) {
171 /* Size is unknown and not an array */
172 Error ("Variable `%s' has unknown size", Decl.Ident);
175 /* We cannot declare variables of type void */
176 Error ("Illegal type for variable `%s'", Decl.Ident);
180 /* Switch to the data or rodata segment */
181 if (IsQualConst (Decl.Type)) {
188 g_defgloblabel (Entry->Name);
193 /* Parse the initialization */
194 ParseInit (Entry->Type);
197 if (IsTypeVoid (Decl.Type)) {
198 /* We cannot declare variables of type void */
199 Error ("Illegal type for variable `%s'", Decl.Ident);
200 } else if (Size == 0) {
201 /* Size is unknown */
202 Error ("Variable `%s' has unknown size", Decl.Ident);
205 /* Switch to the BSS segment */
209 g_defgloblabel (Entry->Name);
211 /* Allocate space for uninitialized variable */
212 g_res (SizeOf (Entry->Type));
217 /* Check for end of declaration list */
218 if (CurTok.Tok == TOK_COMMA) {
226 /* Function declaration? */
227 if (Entry && IsTypeFunc (Entry->Type)) {
232 if (CurTok.Tok == TOK_SEMI) {
246 /* Must be followed by a semicolon */
255 void Compile (const char* FileName)
256 /* Top level compile routine. Will setup things and call the parser. */
261 /* Add some standard paths to the include search path */
262 AddIncludePath ("", INC_USER); /* Current directory */
263 AddIncludePath ("include", INC_SYS);
265 AddIncludePath (CC65_INC, INC_SYS);
267 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
269 Path = getenv ("CC65_INC");
271 AddIncludePath (Path, INC_SYS | INC_USER);
274 /* Add macros that are always defined */
275 DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
277 /* Strict ANSI macro */
279 DefineNumericMacro ("__STRICT_ANSI__", 1);
282 /* Optimization macros */
284 DefineNumericMacro ("__OPT__", 1);
285 if (FavourSize == 0) {
286 DefineNumericMacro ("__OPT_i__", 1);
289 DefineNumericMacro ("__OPT_r__", 1);
291 if (InlineStdFuncs) {
292 DefineNumericMacro ("__OPT_s__", 1);
296 /* Initialize the literal pool */
299 /* Create the base lexical level */
302 /* Generate the code generator preamble */
305 /* Open the input file */
306 OpenMainFile (FileName);
308 /* Ok, start the ball rolling... */
311 /* Dump the literal pool. */
314 /* Write imported/exported symbols */
318 PrintLiteralPoolStats (stdout);
319 PrintMacroStats (stdout);
322 /* Leave the main lexical level */
325 /* Print an error report */