1 /*****************************************************************************/
5 /* Top level compiler subroutine */
9 /* (C) 2000-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
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"
63 /*****************************************************************************/
65 /*****************************************************************************/
69 static void Parse (void)
70 /* Top level parser routine. */
79 /* Parse until end of input */
80 while (CurTok.Tok != TOK_CEOF) {
86 /* Check for empty statements */
87 if (CurTok.Tok == TOK_SEMI) {
92 /* Check for an ASM statement (which is allowed also on global level) */
93 if (CurTok.Tok == TOK_ASM) {
99 /* Check for a #pragma */
100 if (CurTok.Tok == TOK_PRAGMA) {
105 /* Read variable defs and functions */
106 ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
108 /* Don't accept illegal storage classes */
109 if ((Spec.StorageClass & SC_TYPE) == 0) {
110 if ((Spec.StorageClass & SC_AUTO) != 0 ||
111 (Spec.StorageClass & SC_REGISTER) != 0) {
112 Error ("Illegal storage class");
113 Spec.StorageClass = SC_EXTERN | SC_STATIC;
117 /* Check if this is only a type declaration */
118 if (CurTok.Tok == TOK_SEMI) {
119 CheckEmptyDecl (&Spec);
124 /* Check if we must reserve storage for the variable. We do
125 * this if we don't had a storage class given ("int i") or
126 * if the storage class is explicitly specified as static.
127 * This means that "extern int i" will not get storage
130 NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
131 ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
132 (Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
134 /* Read declarations for this type */
141 /* Read the next declaration */
142 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
143 if (Decl.Ident[0] == '\0') {
148 /* Get the symbol flags */
149 SymFlags = Spec.StorageClass;
150 if (IsTypeFunc (Decl.Type)) {
154 /* We will allocate storage, variable is defined */
155 SymFlags |= SC_STORAGE | SC_DEF;
159 /* Add an entry to the symbol table */
160 Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
162 /* Reserve storage for the variable if we need to */
163 if (SymFlags & SC_STORAGE) {
165 /* Get the size of the variable */
166 unsigned Size = SizeOf (Decl.Type);
168 /* Allow initialization */
169 if (CurTok.Tok == TOK_ASSIGN) {
171 /* We cannot initialize types of unknown size, or
172 * void types in non ANSI mode.
175 if (!IsTypeVoid (Decl.Type)) {
176 if (!IsTypeArray (Decl.Type)) {
177 /* Size is unknown and not an array */
178 Error ("Variable `%s' has unknown size", Decl.Ident);
181 /* We cannot declare variables of type void */
182 Error ("Illegal type for variable `%s'", Decl.Ident);
186 /* Switch to the data or rodata segment */
187 if (IsQualConst (Decl.Type)) {
194 g_defgloblabel (Entry->Name);
199 /* Parse the initialization */
200 ParseInit (Entry->Type);
203 if (IsTypeVoid (Decl.Type)) {
204 /* We cannot declare variables of type void */
205 Error ("Illegal type for variable `%s'", Decl.Ident);
206 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
207 } else if (Size == 0) {
208 /* Size is unknown. Is it an array? */
209 if (!IsTypeArray (Decl.Type)) {
210 Error ("Variable `%s' has unknown size", Decl.Ident);
212 Entry->Flags &= ~(SC_STORAGE | SC_DEF);
215 /* Allocate storage if it is still needed */
216 if (Entry->Flags & SC_STORAGE) {
218 /* Switch to the BSS segment */
222 g_defgloblabel (Entry->Name);
224 /* Allocate space for uninitialized variable */
231 /* Check for end of declaration list */
232 if (CurTok.Tok == TOK_COMMA) {
240 /* Function declaration? */
241 if (Entry && IsTypeFunc (Entry->Type)) {
245 if (CurTok.Tok == TOK_SEMI) {
249 /* Function body definition */
250 if (SymIsDef (Entry)) {
251 Error ("Body for function `%s' has already been defined",
260 /* Must be followed by a semicolon */
269 void Compile (const char* FileName)
270 /* Top level compile routine. Will setup things and call the parser. */
278 /* Since strftime is locale dependent, we need the abbreviated month names
281 static const char MonthNames[12][4] = {
282 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
283 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
286 /* Add macros that are always defined */
287 DefineNumericMacro ("__CC65__", VERSION);
289 /* Strict ANSI macro */
291 DefineNumericMacro ("__STRICT_ANSI__", 1);
294 /* Optimization macros */
296 DefineNumericMacro ("__OPT__", 1);
297 if (FavourSize == 0) {
298 DefineNumericMacro ("__OPT_i__", 1);
301 DefineNumericMacro ("__OPT_r__", 1);
303 if (InlineStdFuncs) {
304 DefineNumericMacro ("__OPT_s__", 1);
308 /* __TIME__ and __DATE__ macros */
310 TM = localtime (&Time);
311 strftime (Buf, sizeof (Buf), "%e %Y", TM);
312 xsprintf (DateStr, sizeof (DateStr), "\"%s %s\"", MonthNames[TM->tm_mon], Buf);
313 strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
314 DefineTextMacro ("__DATE__", DateStr);
315 DefineTextMacro ("__TIME__", TimeStr);
317 /* Initialize the literal pool */
320 /* Create the base lexical level */
323 /* Generate the code generator preamble */
326 /* Open the input file */
327 OpenMainFile (FileName);
329 /* Ok, start the ball rolling... */
332 /* Dump the literal pool. */
335 /* Write imported/exported symbols */
339 PrintLiteralPoolStats (stdout);
340 PrintMacroStats (stdout);
343 /* Leave the main lexical level */
346 /* Print an error report */