1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
38 #include "../common/version.h"
57 /*****************************************************************************/
59 /*****************************************************************************/
63 static void Parse (void)
64 /* Top level parser routine. */
70 NextToken (); /* "prime" the pump */
72 while (curtok != TOK_CEOF) {
78 /* Check for empty statements */
79 if (curtok == TOK_SEMI) {
84 /* Check for an ASM statement (which is allowed also on global level) */
85 if (curtok == TOK_ASM) {
91 /* Check for a #pragma */
92 if (curtok == TOK_PRAGMA) {
97 /* Read variable defs and functions */
98 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
100 /* Don't accept illegal storage classes */
101 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
102 Error (ERR_ILLEGAL_STORAGE_CLASS);
103 Spec.StorageClass = SC_EXTERN | SC_STATIC;
106 /* Check if this is only a type declaration */
107 if (curtok == TOK_SEMI) {
108 CheckEmptyDecl (&Spec);
113 /* Check if we must reserve storage for the variable. We do
114 * this if we don't had a storage class given ("int i") or
115 * if the storage class is explicitly specified as static.
116 * This means that "extern int i" will not get storage
119 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
120 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
121 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
123 /* Read declarations for this type */
130 /* Read the next declaration */
131 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
132 if (Decl.Ident[0] == '\0') {
137 /* Get the symbol flags */
138 SymFlags = Spec.StorageClass;
139 if (IsFunc (Decl.Type)) {
143 /* We will allocate storage, variable is defined */
144 SymFlags |= SC_STORAGE | SC_DEF;
148 /* Add an entry to the symbol table */
149 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
151 /* Reserve storage for the variable if we need to */
152 if (SymFlags & SC_STORAGE) {
154 /* Get the size of the variable */
155 unsigned Size = SizeOf (Decl.Type);
157 /* Allow initialization */
158 if (curtok == TOK_ASSIGN) {
160 /* We cannot initialize types of unknown size, or
161 * void types in non ANSI mode.
164 if (!IsVoid (Decl.Type)) {
165 if (!IsArray (Decl.Type)) {
166 /* Size is unknown and not an array */
167 Error (ERR_UNKNOWN_SIZE);
170 /* We cannot declare variables of type void */
171 Error (ERR_ILLEGAL_TYPE);
175 /* Switch to the data segment */
179 g_defgloblabel (Entry->Name);
184 /* Parse the initialization */
185 ParseInit (Entry->Type);
188 if (IsVoid (Decl.Type)) {
189 /* We cannot declare variables of type void */
190 Error (ERR_ILLEGAL_TYPE);
191 } else if (Size == 0) {
192 /* Size is unknown */
193 Error (ERR_UNKNOWN_SIZE);
196 /* Switch to the BSS segment */
200 g_defgloblabel (Entry->Name);
202 /* Allocate space for uninitialized variable */
203 g_res (SizeOf (Entry->Type));
208 /* Check for end of declaration list */
209 if (curtok == TOK_COMMA) {
217 /* Function declaration? */
218 if (IsFunc (Decl.Type)) {
223 if (curtok == TOK_SEMI) {
237 /* Must be followed by a semicolon */
247 /* Top level compile routine. Will setup things and call the parser. */
252 /* Setup variables */
253 LiteralLabel = GetLabel ();
255 /* Add some standard paths to the include search path */
256 AddIncludePath ("", INC_USER); /* Current directory */
257 AddIncludePath ("include", INC_SYS);
259 AddIncludePath (CC65_INC, INC_SYS);
261 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
263 Path = getenv ("CC65_INC");
265 AddIncludePath (Path, INC_SYS | INC_USER);
268 /* Add macros that are always defined */
269 AddNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
271 /* Strict ANSI macro */
273 AddNumericMacro ("__STRICT_ANSI__", 1);
276 /* Optimization macros */
278 AddNumericMacro ("__OPT__", 1);
279 if (FavourSize == 0) {
280 AddNumericMacro ("__OPT_i__", 1);
283 AddNumericMacro ("__OPT_r__", 1);
285 if (InlineStdFuncs) {
286 AddNumericMacro ("__OPT_s__", 1);
290 /* Create the base lexical level */
293 /* Generate the code generator preamble */
296 /* Ok, start the ball rolling... */
299 /* Dump literal pool. */
302 /* Write imported/exported symbols */
306 PrintLiteralStats (stdout);
307 PrintMacroStats (stdout);
310 /* Leave the main lexical level */
313 /* Print an error report */