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 /*****************************************************************************/
40 #include "debugflag.h"
64 /*****************************************************************************/
66 /*****************************************************************************/
70 static void Parse (void)
71 /* Top level parser routine. */
80 /* Parse until end of input */
81 while (CurTok.Tok != TOK_CEOF) {
87 /* Check for empty statements */
88 if (CurTok.Tok == TOK_SEMI) {
93 /* Check for an ASM statement (which is allowed also on global level) */
94 if (CurTok.Tok == TOK_ASM) {
100 /* Check for a #pragma */
101 if (CurTok.Tok == TOK_PRAGMA) {
106 /* Read variable defs and functions */
107 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
109 /* Don't accept illegal storage classes */
110 if ((Spec.StorageClass & SC_TYPE) == 0) {
111 if ((Spec.StorageClass & SC_AUTO) != 0 ||
112 (Spec.StorageClass & SC_REGISTER) != 0) {
113 Error ("Illegal storage class");
114 Spec.StorageClass = SC_EXTERN | SC_STATIC;
118 /* Check if this is only a type declaration */
119 if (CurTok.Tok == TOK_SEMI) {
120 CheckEmptyDecl (&Spec);
125 /* Check if we must reserve storage for the variable. We do
126 * this if we don't had a storage class given ("int i") or
127 * if the storage class is explicitly specified as static.
128 * This means that "extern int i" will not get storage
131 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
132 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
133 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
135 /* Read declarations for this type */
142 /* Read the next declaration */
143 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
144 if (Decl.Ident[0] == '\0') {
149 /* Get the symbol flags */
150 SymFlags = Spec.StorageClass;
151 if (IsTypeFunc (Decl.Type)) {
155 /* We will allocate storage, variable is defined */
156 SymFlags |= SC_STORAGE | SC_DEF;
160 /* Add an entry to the symbol table */
161 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
163 /* Reserve storage for the variable if we need to */
164 if (SymFlags & SC_STORAGE) {
166 /* Get the size of the variable */
167 unsigned Size = SizeOf (Decl.Type);
169 /* Allow initialization */
170 if (CurTok.Tok == TOK_ASSIGN) {
172 /* We cannot initialize types of unknown size, or
173 * void types in non ANSI mode.
176 if (!IsTypeVoid (Decl.Type)) {
177 if (!IsTypeArray (Decl.Type)) {
178 /* Size is unknown and not an array */
179 Error ("Variable `%s' has unknown size", Decl.Ident);
182 /* We cannot declare variables of type void */
183 Error ("Illegal type for variable `%s'", Decl.Ident);
187 /* Switch to the data or rodata segment */
188 if (IsQualConst (Decl.Type)) {
195 g_defgloblabel (Entry->Name);
200 /* Parse the initialization */
201 ParseInit (Entry->Type);
204 if (IsTypeVoid (Decl.Type)) {
205 /* We cannot declare variables of type void */
206 Error ("Illegal type for variable `%s'", Decl.Ident);
207 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
208 } else if (Size == 0) {
209 /* Size is unknown. Is it an array? */
210 if (!IsTypeArray (Decl.Type)) {
211 Error ("Variable `%s' has unknown size", Decl.Ident);
213 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
216 /* Allocate storage if it is still needed */
217 if (Entry->Flags & SC_STORAGE) {
219 /* Switch to the BSS segment */
223 g_defgloblabel (Entry->Name);
225 /* Allocate space for uninitialized variable */
232 /* Check for end of declaration list */
233 if (CurTok.Tok == TOK_COMMA) {
241 /* Function declaration? */
242 if (Entry && IsTypeFunc (Entry->Type)) {
246 if (CurTok.Tok == TOK_SEMI) {
250 /* Function body definition */
251 if (SymIsDef (Entry)) {
252 Error ("Body for function `%s' has already been defined",
261 /* Must be followed by a semicolon */
270 void Compile (const char* FileName)
271 /* Top level compile routine. Will setup things and call the parser. */
280 /* Since strftime is locale dependent, we need the abbreviated month names
283 static const char MonthNames[12][4] = {
284 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
285 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
288 /* Add some standard paths to the include search path */
289 AddIncludePath ("", INC_USER); /* Current directory */
290 AddIncludePath ("include", INC_SYS);
292 AddIncludePath (CC65_INC, INC_SYS);
294 AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
296 Path = getenv ("CC65_INC");
298 AddIncludePath (Path, INC_SYS | INC_USER);
301 /* Add macros that are always defined */
302 DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
304 /* Strict ANSI macro */
306 DefineNumericMacro ("__STRICT_ANSI__", 1);
309 /* Optimization macros */
311 DefineNumericMacro ("__OPT__", 1);
312 if (FavourSize == 0) {
313 DefineNumericMacro ("__OPT_i__", 1);
316 DefineNumericMacro ("__OPT_r__", 1);
318 if (InlineStdFuncs) {
319 DefineNumericMacro ("__OPT_s__", 1);
323 /* __TIME__ and __DATE__ macros */
325 TM = localtime (&Time);
326 strftime (Buf, sizeof (Buf), "%e %Y", TM);
327 xsprintf (DateStr, sizeof (DateStr), "\"%s %s\"", MonthNames[TM->tm_mon], Buf);
328 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
329 DefineTextMacro ("__DATE__", DateStr);
330 DefineTextMacro ("__TIME__", TimeStr);
332 /* Initialize the literal pool */
335 /* Create the base lexical level */
338 /* Generate the code generator preamble */
341 /* Open the input file */
342 OpenMainFile (FileName);
344 /* Ok, start the ball rolling... */
347 /* Dump the literal pool. */
350 /* Write imported/exported symbols */
354 PrintLiteralPoolStats (stdout);
355 PrintMacroStats (stdout);
358 /* Leave the main lexical level */
361 /* Print an error report */