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 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
68 static void Parse (void)
69 /* Top level parser routine. */
78 /* Parse until end of input */
79 while (CurTok.Tok != TOK_CEOF) {
85 /* Check for empty statements */
86 if (CurTok.Tok == TOK_SEMI) {
91 /* Check for an ASM statement (which is allowed also on global level) */
92 if (CurTok.Tok == TOK_ASM) {
98 /* Check for a #pragma */
99 if (CurTok.Tok == TOK_PRAGMA) {
104 /* Read variable defs and functions */
105 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
107 /* Don't accept illegal storage classes */
108 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
109 Error ("Illegal storage class");
110 Spec.StorageClass = SC_EXTERN | SC_STATIC;
113 /* Check if this is only a type declaration */
114 if (CurTok.Tok == TOK_SEMI) {
115 CheckEmptyDecl (&Spec);
120 /* Check if we must reserve storage for the variable. We do
121 * this if we don't had a storage class given ("int i") or
122 * if the storage class is explicitly specified as static.
123 * This means that "extern int i" will not get storage
126 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
127 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
128 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
130 /* Read declarations for this type */
137 /* Read the next declaration */
138 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
139 if (Decl.Ident[0] == '\0') {
144 /* Get the symbol flags */
145 SymFlags = Spec.StorageClass;
146 if (IsTypeFunc (Decl.Type)) {
150 /* We will allocate storage, variable is defined */
151 SymFlags |= SC_STORAGE | SC_DEF;
155 /* Add an entry to the symbol table */
156 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
158 /* Reserve storage for the variable if we need to */
159 if (SymFlags & SC_STORAGE) {
161 /* Get the size of the variable */
162 unsigned Size = SizeOf (Decl.Type);
164 /* Allow initialization */
165 if (CurTok.Tok == TOK_ASSIGN) {
167 /* We cannot initialize types of unknown size, or
168 * void types in non ANSI mode.
171 if (!IsTypeVoid (Decl.Type)) {
172 if (!IsTypeArray (Decl.Type)) {
173 /* Size is unknown and not an array */
174 Error ("Variable `%s' has unknown size", Decl.Ident);
177 /* We cannot declare variables of type void */
178 Error ("Illegal type for variable `%s'", Decl.Ident);
182 /* Switch to the data or rodata segment */
183 if (IsQualConst (Decl.Type)) {
190 g_defgloblabel (Entry->Name);
195 /* Parse the initialization */
196 ParseInit (Entry->Type);
199 if (IsTypeVoid (Decl.Type)) {
200 /* We cannot declare variables of type void */
201 Error ("Illegal type for variable `%s'", Decl.Ident);
202 } else if (Size == 0) {
203 /* Size is unknown */
204 Error ("Variable `%s' has unknown size", Decl.Ident);
207 /* Switch to the BSS segment */
211 g_defgloblabel (Entry->Name);
213 /* Allocate space for uninitialized variable */
214 g_res (SizeOf (Entry->Type));
219 /* Check for end of declaration list */
220 if (CurTok.Tok == TOK_COMMA) {
228 /* Function declaration? */
229 if (Entry && IsTypeFunc (Entry->Type)) {
234 if (CurTok.Tok == TOK_SEMI) {
248 /* Must be followed by a semicolon */
257 void Compile (const char* FileName)
258 /* Top level compile routine. Will setup things and call the parser. */
265 /* Add some standard paths to the include search path */
266 AddIncludePath ("", INC_USER); /* Current directory */
267 AddIncludePath ("include", INC_SYS);
269 AddIncludePath (CC65_INC, INC_SYS);
271 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
273 Path = getenv ("CC65_INC");
275 AddIncludePath (Path, INC_SYS | INC_USER);
278 /* Add macros that are always defined */
279 DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
281 /* Strict ANSI macro */
283 DefineNumericMacro ("__STRICT_ANSI__", 1);
286 /* Optimization macros */
288 DefineNumericMacro ("__OPT__", 1);
289 if (FavourSize == 0) {
290 DefineNumericMacro ("__OPT_i__", 1);
293 DefineNumericMacro ("__OPT_r__", 1);
295 if (InlineStdFuncs) {
296 DefineNumericMacro ("__OPT_s__", 1);
300 /* __TIME__ and __DATE__ macros */
302 TimeStr = ctime (&Time);
303 sprintf (Buf, "\"%.10s\"", TimeStr);
304 DefineTextMacro ("__DATE__", Buf);
305 sprintf (Buf, "\"%.15s\"", TimeStr+11);
306 DefineTextMacro ("__TIME__", Buf);
308 /* Initialize the literal pool */
311 /* Create the base lexical level */
314 /* Generate the code generator preamble */
317 /* Open the input file */
318 OpenMainFile (FileName);
320 /* Ok, start the ball rolling... */
323 /* Dump the literal pool. */
326 /* Write imported/exported symbols */
330 PrintLiteralPoolStats (stdout);
331 PrintMacroStats (stdout);
334 /* Leave the main lexical level */
337 /* Print an error report */