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 /* Process all input text.
65 * At this level, only static declarations, defines, includes, and function
66 * definitions are legal....
73 NextToken (); /* "prime" the pump */
75 while (curtok != TOK_CEOF) {
81 /* Check for empty statements */
82 if (curtok == TOK_SEMI) {
87 /* Check for an ASM statement (which is allowed also on global level) */
88 if (curtok == TOK_ASM) {
94 /* Check for a #pragma */
95 if (curtok == TOK_PRAGMA) {
100 /* Read variable defs and functions */
101 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
103 /* Don't accept illegal storage classes */
104 if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
105 Error (ERR_ILLEGAL_STORAGE_CLASS);
106 Spec.StorageClass = SC_EXTERN | SC_STATIC;
109 /* Check if this is only a type declaration */
110 if (curtok == TOK_SEMI) {
111 CheckEmptyDecl (&Spec);
116 /* Check if we must reserve storage for the variable. We do
117 * this if we don't had a storage class given ("int i") or
118 * if the storage class is explicitly specified as static.
119 * This means that "extern int i" will not get storage
122 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
123 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
124 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
126 /* Read declarations for this type */
133 /* Read the next declaration */
134 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
135 if (Decl.Ident[0] == '\0') {
140 /* Get the symbol flags */
141 SymFlags = Spec.StorageClass;
142 if (IsFunc (Decl.Type)) {
146 /* We will allocate storage, variable is defined */
147 SymFlags |= SC_STORAGE | SC_DEF;
151 /* Add an entry to the symbol table */
152 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
154 /* Reserve storage for the variable if we need to */
155 if (SymFlags & SC_STORAGE) {
157 /* Get the size of the variable */
158 unsigned Size = SizeOf (Decl.Type);
160 /* Allow initialization */
161 if (curtok == TOK_ASSIGN) {
163 /* We cannot initialize types of unknown size, or
164 * void types in non ANSI mode.
167 if (!IsVoid (Decl.Type)) {
168 if (!IsArray (Decl.Type)) {
169 /* Size is unknown and not an array */
170 Error (ERR_UNKNOWN_SIZE);
173 /* We cannot declare variables of type void */
174 Error (ERR_ILLEGAL_TYPE);
178 /* Switch to the data segment */
182 g_defgloblabel (Entry->Name);
187 /* Parse the initialization */
188 ParseInit (Entry->Type);
191 if (IsVoid (Decl.Type)) {
192 /* We cannot declare variables of type void */
193 Error (ERR_ILLEGAL_TYPE);
194 } else if (Size == 0) {
195 /* Size is unknown */
196 Error (ERR_UNKNOWN_SIZE);
199 /* Switch to the BSS segment */
203 g_defgloblabel (Entry->Name);
205 /* Allocate space for uninitialized variable */
206 g_res (SizeOf (Entry->Type));
211 /* Check for end of declaration list */
212 if (curtok == TOK_COMMA) {
220 /* Function declaration? */
221 if (IsFunc (Decl.Type)) {
226 if (curtok == TOK_SEMI) {
240 /* Must be followed by a semicolon */
250 /* Top level compile routine. Will setup things and call the parser. */
255 /* Setup variables */
256 filetab[0].f_iocb = inp;
257 LiteralLabel = GetLabel ();
259 /* Add some standard paths to the include search path */
260 AddIncludePath ("", INC_USER); /* Current directory */
261 AddIncludePath ("include", INC_SYS);
263 AddIncludePath (CC65_INC, INC_SYS);
265 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
267 Path = getenv ("CC65_INC");
269 AddIncludePath (Path, INC_SYS | INC_USER);
272 /* Add macros that are always defined */
273 AddNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
275 /* Strict ANSI macro */
277 AddNumericMacro ("__STRICT_ANSI__", 1);
280 /* Optimization macros */
282 AddNumericMacro ("__OPT__", 1);
283 if (FavourSize == 0) {
284 AddNumericMacro ("__OPT_i__", 1);
287 AddNumericMacro ("__OPT_r__", 1);
289 if (InlineStdFuncs) {
290 AddNumericMacro ("__OPT_s__", 1);
294 /* Create the base lexical level */
297 /* Generate the code generator preamble */
300 /* Ok, start the ball rolling... */
303 /* Dump literal pool. */
306 /* Write imported/exported symbols */
310 PrintLiteralStats (stdout);
311 PrintMacroStats (stdout);
314 /* Leave the main lexical level */
317 /* Print an error report */