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 /*****************************************************************************/
59 /*****************************************************************************/
61 /*****************************************************************************/
65 static void Parse (void)
66 /* Top level parser routine. */
75 /* Parse until end of input */
76 while (CurTok.Tok != TOK_CEOF) {
82 /* Check for empty statements */
83 if (CurTok.Tok == TOK_SEMI) {
88 /* Check for an ASM statement (which is allowed also on global level) */
89 if (CurTok.Tok == TOK_ASM) {
95 /* Check for a #pragma */
96 if (CurTok.Tok == TOK_PRAGMA) {
101 /* Read variable defs and functions */
102 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
104 /* Don't accept illegal storage classes */
105 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
106 Error ("Illegal storage class");
107 Spec.StorageClass = SC_EXTERN | SC_STATIC;
110 /* Check if this is only a type declaration */
111 if (CurTok.Tok == TOK_SEMI) {
112 CheckEmptyDecl (&Spec);
117 /* Check if we must reserve storage for the variable. We do
118 * this if we don't had a storage class given ("int i") or
119 * if the storage class is explicitly specified as static.
120 * This means that "extern int i" will not get storage
123 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
124 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
125 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
127 /* Read declarations for this type */
134 /* Read the next declaration */
135 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
136 if (Decl.Ident[0] == '\0') {
141 /* Get the symbol flags */
142 SymFlags = Spec.StorageClass;
143 if (IsTypeFunc (Decl.Type)) {
147 /* We will allocate storage, variable is defined */
148 SymFlags |= SC_STORAGE | SC_DEF;
152 /* Add an entry to the symbol table */
153 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
155 /* Reserve storage for the variable if we need to */
156 if (SymFlags & SC_STORAGE) {
158 /* Get the size of the variable */
159 unsigned Size = SizeOf (Decl.Type);
161 /* Allow initialization */
162 if (CurTok.Tok == TOK_ASSIGN) {
164 /* We cannot initialize types of unknown size, or
165 * void types in non ANSI mode.
168 if (!IsTypeVoid (Decl.Type)) {
169 if (!IsTypeArray (Decl.Type)) {
170 /* Size is unknown and not an array */
171 Error ("Variable `%s' has unknown size", Decl.Ident);
174 /* We cannot declare variables of type void */
175 Error ("Illegal type for variable `%s'", Decl.Ident);
179 /* Switch to the data or rodata segment */
180 if (IsQualConst (Decl.Type)) {
187 g_defgloblabel (Entry->Name);
192 /* Parse the initialization */
193 ParseInit (Entry->Type);
196 if (IsTypeVoid (Decl.Type)) {
197 /* We cannot declare variables of type void */
198 Error ("Illegal type for variable `%s'", Decl.Ident);
199 } else if (Size == 0) {
200 /* Size is unknown */
201 Error ("Variable `%s' has unknown size", Decl.Ident);
204 /* Switch to the BSS segment */
208 g_defgloblabel (Entry->Name);
210 /* Allocate space for uninitialized variable */
211 g_res (SizeOf (Entry->Type));
216 /* Check for end of declaration list */
217 if (CurTok.Tok == TOK_COMMA) {
225 /* Function declaration? */
226 if (Entry && IsTypeFunc (Entry->Type)) {
231 if (CurTok.Tok == TOK_SEMI) {
245 /* Must be followed by a semicolon */
254 void Compile (const char* FileName)
255 /* Top level compile routine. Will setup things and call the parser. */
260 /* Add some standard paths to the include search path */
261 AddIncludePath ("", INC_USER); /* Current directory */
262 AddIncludePath ("include", INC_SYS);
264 AddIncludePath (CC65_INC, INC_SYS);
266 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
268 Path = getenv ("CC65_INC");
270 AddIncludePath (Path, INC_SYS | INC_USER);
273 /* Add macros that are always defined */
274 AddNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
276 /* Strict ANSI macro */
278 AddNumericMacro ("__STRICT_ANSI__", 1);
281 /* Optimization macros */
283 AddNumericMacro ("__OPT__", 1);
284 if (FavourSize == 0) {
285 AddNumericMacro ("__OPT_i__", 1);
288 AddNumericMacro ("__OPT_r__", 1);
290 if (InlineStdFuncs) {
291 AddNumericMacro ("__OPT_s__", 1);
295 /* Initialize the literal pool */
298 /* Create the base lexical level */
301 /* Generate the code generator preamble */
304 /* Open the input file */
305 OpenMainFile (FileName);
307 /* Ok, start the ball rolling... */
310 /* Dump the literal pool. */
313 /* Write imported/exported symbols */
317 PrintLiteralPoolStats (stdout);
318 PrintMacroStats (stdout);
321 /* Leave the main lexical level */
324 /* Print an error report */