2000-06-12 18:31:40 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* compile.c */
|
|
|
|
/* */
|
|
|
|
/* Top level compiler subroutine */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2002-09-23 21:37:15 +00:00
|
|
|
/* (C) 2000-2002 Ullrich von Bassewitz */
|
2001-05-03 17:34:54 +00:00
|
|
|
/* Wacholderweg 14 */
|
|
|
|
/* D-70597 Stuttgart */
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-06-12 18:31:40 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2002-09-23 21:37:15 +00:00
|
|
|
#include <time.h>
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
/* common */
|
2003-01-04 16:59:51 +00:00
|
|
|
#include "debugflag.h"
|
2000-08-01 15:04:35 +00:00
|
|
|
#include "version.h"
|
2002-09-23 21:37:15 +00:00
|
|
|
#include "xmalloc.h"
|
2002-10-14 16:23:26 +00:00
|
|
|
#include "xsprintf.h"
|
2000-08-01 18:45:08 +00:00
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
/* cc65 */
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "asmlabel.h"
|
2001-09-09 09:33:06 +00:00
|
|
|
#include "asmstmt.h"
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "codegen.h"
|
|
|
|
#include "declare.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "global.h"
|
2000-06-14 08:18:19 +00:00
|
|
|
#include "incpath.h"
|
2001-05-22 20:03:26 +00:00
|
|
|
#include "input.h"
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "litpool.h"
|
|
|
|
#include "macrotab.h"
|
|
|
|
#include "pragma.h"
|
|
|
|
#include "symtab.h"
|
|
|
|
#include "compile.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2001-05-22 09:32:24 +00:00
|
|
|
/* Code */
|
2000-06-12 18:31:40 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Parse (void)
|
2000-06-14 21:01:37 +00:00
|
|
|
/* Top level parser routine. */
|
2000-06-12 18:31:40 +00:00
|
|
|
{
|
|
|
|
int comma;
|
|
|
|
SymEntry* Entry;
|
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
/* Go... */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
/* Parse until end of input */
|
|
|
|
while (CurTok.Tok != TOK_CEOF) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2003-01-20 21:02:57 +00:00
|
|
|
DeclSpec Spec;
|
2000-06-12 18:31:40 +00:00
|
|
|
Declaration Decl;
|
2003-01-20 21:02:57 +00:00
|
|
|
int NeedStorage;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Check for empty statements */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for an ASM statement (which is allowed also on global level) */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_ASM) {
|
2001-09-09 09:33:06 +00:00
|
|
|
AsmStatement ();
|
2000-06-12 18:31:40 +00:00
|
|
|
ConsumeSemi ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a #pragma */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_PRAGMA) {
|
2000-06-12 18:31:40 +00:00
|
|
|
DoPragma ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read variable defs and functions */
|
|
|
|
ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
|
|
|
|
|
|
|
|
/* Don't accept illegal storage classes */
|
2003-01-20 21:02:57 +00:00
|
|
|
if ((Spec.StorageClass & SC_TYPE) == 0) {
|
|
|
|
if ((Spec.StorageClass & SC_AUTO) != 0 ||
|
|
|
|
(Spec.StorageClass & SC_REGISTER) != 0) {
|
|
|
|
Error ("Illegal storage class");
|
|
|
|
Spec.StorageClass = SC_EXTERN | SC_STATIC;
|
|
|
|
}
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Check if this is only a type declaration */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
2000-06-12 18:31:40 +00:00
|
|
|
CheckEmptyDecl (&Spec);
|
|
|
|
NextToken ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we must reserve storage for the variable. We do
|
|
|
|
* this if we don't had a storage class given ("int i") or
|
|
|
|
* if the storage class is explicitly specified as static.
|
|
|
|
* This means that "extern int i" will not get storage
|
|
|
|
* allocated.
|
|
|
|
*/
|
|
|
|
NeedStorage = (Spec.StorageClass & SC_TYPEDEF) == 0 &&
|
|
|
|
((Spec.Flags & DS_DEF_STORAGE) != 0 ||
|
|
|
|
(Spec.StorageClass & (SC_STATIC | SC_EXTERN)) == SC_STATIC);
|
|
|
|
|
|
|
|
/* Read declarations for this type */
|
|
|
|
Entry = 0;
|
|
|
|
comma = 0;
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
unsigned SymFlags;
|
|
|
|
|
|
|
|
/* Read the next declaration */
|
|
|
|
ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
|
|
|
|
if (Decl.Ident[0] == '\0') {
|
|
|
|
NextToken ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the symbol flags */
|
|
|
|
SymFlags = Spec.StorageClass;
|
2000-08-01 18:45:08 +00:00
|
|
|
if (IsTypeFunc (Decl.Type)) {
|
2000-06-12 18:31:40 +00:00
|
|
|
SymFlags |= SC_FUNC;
|
|
|
|
} else {
|
|
|
|
if (NeedStorage) {
|
|
|
|
/* We will allocate storage, variable is defined */
|
|
|
|
SymFlags |= SC_STORAGE | SC_DEF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an entry to the symbol table */
|
|
|
|
Entry = AddGlobalSym (Decl.Ident, Decl.Type, SymFlags);
|
|
|
|
|
|
|
|
/* Reserve storage for the variable if we need to */
|
|
|
|
if (SymFlags & SC_STORAGE) {
|
|
|
|
|
|
|
|
/* Get the size of the variable */
|
|
|
|
unsigned Size = SizeOf (Decl.Type);
|
|
|
|
|
|
|
|
/* Allow initialization */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_ASSIGN) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* We cannot initialize types of unknown size, or
|
|
|
|
* void types in non ANSI mode.
|
|
|
|
*/
|
|
|
|
if (Size == 0) {
|
2000-08-01 18:45:08 +00:00
|
|
|
if (!IsTypeVoid (Decl.Type)) {
|
|
|
|
if (!IsTypeArray (Decl.Type)) {
|
|
|
|
/* Size is unknown and not an array */
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Variable `%s' has unknown size", Decl.Ident);
|
2000-08-01 18:45:08 +00:00
|
|
|
}
|
|
|
|
} else if (ANSI) {
|
|
|
|
/* We cannot declare variables of type void */
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal type for variable `%s'", Decl.Ident);
|
2000-08-01 18:45:08 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* Switch to the data or rodata segment */
|
2000-08-14 22:18:26 +00:00
|
|
|
if (IsQualConst (Decl.Type)) {
|
2000-08-01 18:45:08 +00:00
|
|
|
g_userodata ();
|
|
|
|
} else {
|
|
|
|
g_usedata ();
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Define a label */
|
|
|
|
g_defgloblabel (Entry->Name);
|
|
|
|
|
|
|
|
/* Skip the '=' */
|
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
/* Parse the initialization */
|
|
|
|
ParseInit (Entry->Type);
|
|
|
|
} else {
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
if (IsTypeVoid (Decl.Type)) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* We cannot declare variables of type void */
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal type for variable `%s'", Decl.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
} else if (Size == 0) {
|
|
|
|
/* Size is unknown */
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Variable `%s' has unknown size", Decl.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to the BSS segment */
|
|
|
|
g_usebss ();
|
|
|
|
|
|
|
|
/* Define a label */
|
|
|
|
g_defgloblabel (Entry->Name);
|
|
|
|
|
|
|
|
/* Allocate space for uninitialized variable */
|
|
|
|
g_res (SizeOf (Entry->Type));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for end of declaration list */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_COMMA) {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
comma = 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Function declaration? */
|
2000-12-04 20:39:17 +00:00
|
|
|
if (Entry && IsTypeFunc (Entry->Type)) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Function */
|
|
|
|
if (!comma) {
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Prototype only */
|
|
|
|
NextToken ();
|
2002-11-24 23:31:54 +00:00
|
|
|
} else if (Entry) {
|
|
|
|
/* Function body definition */
|
|
|
|
if (SymIsDef (Entry)) {
|
|
|
|
Error ("Body for function `%s' has already been defined",
|
|
|
|
Entry->Name);
|
|
|
|
}
|
|
|
|
NewFunc (Entry);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Must be followed by a semicolon */
|
|
|
|
ConsumeSemi ();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 20:03:26 +00:00
|
|
|
void Compile (const char* FileName)
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Top level compile routine. Will setup things and call the parser. */
|
|
|
|
{
|
2002-10-14 16:23:26 +00:00
|
|
|
char* Path;
|
|
|
|
char Buf[20];
|
|
|
|
char DateStr[20];
|
|
|
|
char TimeStr[20];
|
|
|
|
time_t Time;
|
|
|
|
struct tm* TM;
|
|
|
|
|
|
|
|
/* Since strftime is locale dependent, we need the abbreviated month names
|
|
|
|
* in english.
|
|
|
|
*/
|
|
|
|
static const char MonthNames[12][4] = {
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
|
|
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
};
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Add some standard paths to the include search path */
|
|
|
|
AddIncludePath ("", INC_USER); /* Current directory */
|
|
|
|
AddIncludePath ("include", INC_SYS);
|
|
|
|
#ifdef CC65_INC
|
|
|
|
AddIncludePath (CC65_INC, INC_SYS);
|
|
|
|
#else
|
|
|
|
AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
|
|
|
|
#endif
|
|
|
|
Path = getenv ("CC65_INC");
|
|
|
|
if (Path) {
|
|
|
|
AddIncludePath (Path, INC_SYS | INC_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add macros that are always defined */
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Strict ANSI macro */
|
|
|
|
if (ANSI) {
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineNumericMacro ("__STRICT_ANSI__", 1);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Optimization macros */
|
|
|
|
if (Optimize) {
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineNumericMacro ("__OPT__", 1);
|
2000-06-12 18:31:40 +00:00
|
|
|
if (FavourSize == 0) {
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineNumericMacro ("__OPT_i__", 1);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
if (EnableRegVars) {
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineNumericMacro ("__OPT_r__", 1);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
if (InlineStdFuncs) {
|
2001-09-06 10:17:52 +00:00
|
|
|
DefineNumericMacro ("__OPT_s__", 1);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-23 21:37:15 +00:00
|
|
|
/* __TIME__ and __DATE__ macros */
|
|
|
|
Time = time (0);
|
2002-10-14 16:23:26 +00:00
|
|
|
TM = localtime (&Time);
|
|
|
|
strftime (Buf, sizeof (Buf), "%e %Y", TM);
|
|
|
|
xsprintf (DateStr, sizeof (DateStr), "\"%s %s\"", MonthNames[TM->tm_mon], Buf);
|
|
|
|
strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
|
|
|
|
DefineTextMacro ("__DATE__", DateStr);
|
|
|
|
DefineTextMacro ("__TIME__", TimeStr);
|
2002-09-23 21:37:15 +00:00
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
/* Initialize the literal pool */
|
|
|
|
InitLiteralPool ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Create the base lexical level */
|
|
|
|
EnterGlobalLevel ();
|
|
|
|
|
|
|
|
/* Generate the code generator preamble */
|
|
|
|
g_preamble ();
|
|
|
|
|
2001-05-22 20:03:26 +00:00
|
|
|
/* Open the input file */
|
2001-09-06 10:17:52 +00:00
|
|
|
OpenMainFile (FileName);
|
2001-05-22 20:03:26 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Ok, start the ball rolling... */
|
|
|
|
Parse ();
|
|
|
|
|
2001-03-26 21:57:07 +00:00
|
|
|
/* Dump the literal pool. */
|
2000-06-12 18:31:40 +00:00
|
|
|
DumpLiteralPool ();
|
|
|
|
|
|
|
|
/* Write imported/exported symbols */
|
|
|
|
EmitExternals ();
|
|
|
|
|
|
|
|
if (Debug) {
|
2001-03-26 21:57:07 +00:00
|
|
|
PrintLiteralPoolStats (stdout);
|
2000-06-12 18:31:40 +00:00
|
|
|
PrintMacroStats (stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Leave the main lexical level */
|
|
|
|
LeaveGlobalLevel ();
|
|
|
|
|
|
|
|
/* Print an error report */
|
|
|
|
ErrorReport ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|