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"
56 /*****************************************************************************/
58 /*****************************************************************************/
62 static void Parse (void)
63 /* Top level parser routine. */
68 NextToken (); /* "prime" the pump */
70 while (curtok != TOK_CEOF) {
76 /* Check for empty statements */
77 if (curtok == TOK_SEMI) {
82 /* Check for an ASM statement (which is allowed also on global level) */
83 if (curtok == TOK_ASM) {
89 /* Check for a #pragma */
90 if (curtok == TOK_PRAGMA) {
95 /* Read variable defs and functions */
96 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
98 /* Don't accept illegal storage classes */
99 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
100 Error (ERR_ILLEGAL_STORAGE_CLASS);
101 Spec.StorageClass = SC_EXTERN | SC_STATIC;
104 /* Check if this is only a type declaration */
105 if (curtok == TOK_SEMI) {
106 CheckEmptyDecl (&Spec);
111 /* Check if we must reserve storage for the variable. We do
112 * this if we don't had a storage class given ("int i") or
113 * if the storage class is explicitly specified as static.
114 * This means that "extern int i" will not get storage
117 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
118 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
119 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
121 /* Read declarations for this type */
128 /* Read the next declaration */
129 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
130 if (Decl.Ident[0] == '\0') {
135 /* Get the symbol flags */
136 SymFlags = Spec.StorageClass;
137 if (IsFunc (Decl.Type)) {
141 /* We will allocate storage, variable is defined */
142 SymFlags |= SC_STORAGE | SC_DEF;
146 /* Add an entry to the symbol table */
147 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
149 /* Reserve storage for the variable if we need to */
150 if (SymFlags & SC_STORAGE) {
152 /* Get the size of the variable */
153 unsigned Size = SizeOf (Decl.Type);
155 /* Allow initialization */
156 if (curtok == TOK_ASSIGN) {
158 /* We cannot initialize types of unknown size, or
159 * void types in non ANSI mode.
162 if (!IsVoid (Decl.Type)) {
163 if (!IsArray (Decl.Type)) {
164 /* Size is unknown and not an array */
165 Error (ERR_UNKNOWN_SIZE);
168 /* We cannot declare variables of type void */
169 Error (ERR_ILLEGAL_TYPE);
173 /* Switch to the data segment */
177 g_defgloblabel (Entry->Name);
182 /* Parse the initialization */
183 ParseInit (Entry->Type);
186 if (IsVoid (Decl.Type)) {
187 /* We cannot declare variables of type void */
188 Error (ERR_ILLEGAL_TYPE);
189 } else if (Size == 0) {
190 /* Size is unknown */
191 Error (ERR_UNKNOWN_SIZE);
194 /* Switch to the BSS segment */
198 g_defgloblabel (Entry->Name);
200 /* Allocate space for uninitialized variable */
201 g_res (SizeOf (Entry->Type));
206 /* Check for end of declaration list */
207 if (curtok == TOK_COMMA) {
215 /* Function declaration? */
216 if (IsFunc (Decl.Type)) {
221 if (curtok == TOK_SEMI) {
235 /* Must be followed by a semicolon */
245 /* Top level compile routine. Will setup things and call the parser. */
250 /* Setup variables */
251 LiteralLabel = GetLabel ();
253 /* Add some standard paths to the include search path */
254 AddIncludePath ("", INC_USER); /* Current directory */
255 AddIncludePath ("include", INC_SYS);
257 AddIncludePath (CC65_INC, INC_SYS);
259 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
261 Path = getenv ("CC65_INC");
263 AddIncludePath (Path, INC_SYS | INC_USER);
266 /* Add macros that are always defined */
267 AddNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
269 /* Strict ANSI macro */
271 AddNumericMacro ("__STRICT_ANSI__", 1);
274 /* Optimization macros */
276 AddNumericMacro ("__OPT__", 1);
277 if (FavourSize == 0) {
278 AddNumericMacro ("__OPT_i__", 1);
281 AddNumericMacro ("__OPT_r__", 1);
283 if (InlineStdFuncs) {
284 AddNumericMacro ("__OPT_s__", 1);
288 /* Create the base lexical level */
291 /* Generate the code generator preamble */
294 /* Ok, start the ball rolling... */
297 /* Dump literal pool. */
300 /* Write imported/exported symbols */
304 PrintLiteralStats (stdout);
305 PrintMacroStats (stdout);
308 /* Leave the main lexical level */
311 /* Print an error report */