2000-06-12 18:31:40 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* compile.c */
|
2000-06-12 18:31:40 +00:00
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Top level compiler subroutine */
|
2000-06-12 18:31:40 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2013-02-02 22:31:26 +00:00
|
|
|
/* (C) 2000-2013, Ullrich von Bassewitz */
|
2009-09-28 20:10:01 +00:00
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* 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"
|
2019-05-18 16:16:52 +00:00
|
|
|
#include "segnames.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"
|
2009-12-05 22:39:45 +00:00
|
|
|
#include "codeopt.h"
|
2004-06-21 22:22:11 +00:00
|
|
|
#include "compile.h"
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "declare.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "global.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"
|
2009-02-23 21:25:59 +00:00
|
|
|
#include "output.h"
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "pragma.h"
|
2004-09-16 12:42:41 +00:00
|
|
|
#include "preproc.h"
|
2004-06-21 22:22:11 +00:00
|
|
|
#include "standard.h"
|
2020-07-26 20:16:47 +00:00
|
|
|
#include "staticassert.h"
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "symtab.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +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;
|
2020-08-15 01:10:17 +00:00
|
|
|
FuncDesc* FuncDef = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
DeclSpec Spec;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Check for empty statements */
|
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
|
|
|
NextToken ();
|
|
|
|
continue;
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Disallow ASM statements on global level */
|
|
|
|
if (CurTok.Tok == TOK_ASM) {
|
2012-03-24 14:28:59 +00:00
|
|
|
Error ("__asm__ is not allowed here");
|
2011-11-07 21:25:20 +00:00
|
|
|
/* Parse and remove the statement for error recovery */
|
2013-05-09 11:56:54 +00:00
|
|
|
AsmStatement ();
|
|
|
|
ConsumeSemi ();
|
2011-11-07 21:25:20 +00:00
|
|
|
RemoveGlobalCode ();
|
2013-05-09 11:56:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Check for a #pragma */
|
|
|
|
if (CurTok.Tok == TOK_PRAGMA) {
|
|
|
|
DoPragma ();
|
|
|
|
continue;
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2020-07-26 20:16:47 +00:00
|
|
|
/* Check for a _Static_assert */
|
|
|
|
if (CurTok.Tok == TOK_STATIC_ASSERT) {
|
|
|
|
ParseStaticAssert ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read variable defs and functions */
|
|
|
|
ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Don't accept illegal storage classes */
|
2020-07-20 14:34:57 +00:00
|
|
|
if ((Spec.StorageClass & SC_TYPEMASK) == 0) {
|
2003-01-20 21:02:57 +00:00
|
|
|
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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Check if this is only a type declaration */
|
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
|
|
|
CheckEmptyDecl (&Spec);
|
|
|
|
NextToken ();
|
|
|
|
continue;
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read declarations for this type */
|
|
|
|
Entry = 0;
|
|
|
|
comma = 0;
|
|
|
|
while (1) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2020-08-14 22:27:11 +00:00
|
|
|
Declaration Decl;
|
2009-10-18 18:31:02 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read the next declaration */
|
|
|
|
ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
|
2020-08-02 17:36:19 +00:00
|
|
|
|
2008-08-03 18:20:12 +00:00
|
|
|
/* Check if we must reserve storage for the variable. We do this,
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** - if it is not a typedef or function,
|
|
|
|
** - if we don't had a storage class given ("int i")
|
2017-02-15 17:51:27 +00:00
|
|
|
** - if the storage class is explicitly specified as static,
|
|
|
|
** - or if there is an initialization.
|
2014-06-30 09:10:35 +00:00
|
|
|
**
|
|
|
|
** This means that "extern int i;" will not get storage allocated.
|
|
|
|
*/
|
2020-08-14 22:27:11 +00:00
|
|
|
if ((Decl.StorageClass & SC_FUNC) != SC_FUNC &&
|
|
|
|
(Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF &&
|
|
|
|
(Decl.StorageClass & SC_FICTITIOUS) != SC_FICTITIOUS) {
|
2020-07-20 14:34:57 +00:00
|
|
|
if ((Spec.Flags & DS_DEF_STORAGE) != 0 ||
|
|
|
|
(Decl.StorageClass & (SC_EXTERN|SC_STATIC)) == SC_STATIC ||
|
|
|
|
((Decl.StorageClass & SC_EXTERN) != 0 &&
|
|
|
|
CurTok.Tok == TOK_ASSIGN)) {
|
|
|
|
/* We will allocate storage */
|
|
|
|
Decl.StorageClass |= SC_STORAGE;
|
|
|
|
} else {
|
|
|
|
/* It's a declaration */
|
|
|
|
Decl.StorageClass |= SC_DECL;
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2008-08-03 20:55:36 +00:00
|
|
|
/* If this is a function declarator that is not followed by a comma
|
2020-08-12 13:35:47 +00:00
|
|
|
** or semicolon, it must be followed by a function body.
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2020-08-12 13:35:47 +00:00
|
|
|
if ((Decl.StorageClass & SC_FUNC) != 0) {
|
|
|
|
if (CurTok.Tok != TOK_COMMA && CurTok.Tok != TOK_SEMI) {
|
|
|
|
/* A definition */
|
|
|
|
Decl.StorageClass |= SC_DEF;
|
2008-08-03 20:55:36 +00:00
|
|
|
|
2020-08-12 13:35:47 +00:00
|
|
|
/* Convert an empty parameter list into one accepting no
|
|
|
|
** parameters (same as void) as required by the standard.
|
|
|
|
*/
|
2020-08-15 01:10:17 +00:00
|
|
|
FuncDef = GetFuncDesc (Decl.Type);
|
|
|
|
if (FuncDef->Flags & FD_EMPTY) {
|
|
|
|
FuncDef->Flags = (FuncDef->Flags & ~FD_EMPTY) | FD_VOID_PARAM;
|
2020-08-12 13:35:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Just a declaration */
|
|
|
|
Decl.StorageClass |= SC_DECL;
|
2008-08-03 20:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add an entry to the symbol table */
|
|
|
|
Entry = AddGlobalSym (Decl.Ident, Decl.Type, Decl.StorageClass);
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2009-10-18 18:31:02 +00:00
|
|
|
/* Add declaration attributes */
|
2009-10-19 10:19:26 +00:00
|
|
|
SymUseAttr (Entry, &Decl);
|
2009-10-18 18:31:02 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Reserve storage for the variable if we need to */
|
|
|
|
if (Decl.StorageClass & SC_STORAGE) {
|
|
|
|
|
|
|
|
/* Get the size of the variable */
|
|
|
|
unsigned Size = SizeOf (Decl.Type);
|
|
|
|
|
|
|
|
/* Allow initialization */
|
|
|
|
if (CurTok.Tok == TOK_ASSIGN) {
|
|
|
|
|
2017-02-13 20:04:45 +00:00
|
|
|
/* This is a definition */
|
2017-02-13 20:10:21 +00:00
|
|
|
if (SymIsDef (Entry)) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Global variable '%s' has already been defined",
|
2017-02-13 20:10:21 +00:00
|
|
|
Entry->Name);
|
|
|
|
}
|
2017-02-13 20:04:45 +00:00
|
|
|
Entry->Flags |= SC_DEF;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We cannot initialize types of unknown size, or
|
2014-06-30 09:10:35 +00:00
|
|
|
** void types in ISO modes.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (Size == 0) {
|
|
|
|
if (!IsTypeVoid (Decl.Type)) {
|
|
|
|
if (!IsTypeArray (Decl.Type)) {
|
|
|
|
/* Size is unknown and not an array */
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Variable '%s' has unknown size", Decl.Ident);
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
} else if (IS_Get (&Standard) != STD_CC65) {
|
|
|
|
/* We cannot declare variables of type void */
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Illegal type for variable '%s'", Decl.Ident);
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch to the data or rodata segment. For arrays, check
|
2014-06-30 09:10:35 +00:00
|
|
|
** the element qualifiers, since not the array but its
|
|
|
|
** elements are const.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (IsQualConst (GetBaseElementType (Decl.Type))) {
|
|
|
|
g_userodata ();
|
|
|
|
} else {
|
|
|
|
g_usedata ();
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Define a label */
|
|
|
|
g_defgloblabel (Entry->Name);
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Skip the '=' */
|
|
|
|
NextToken ();
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Parse the initialization */
|
|
|
|
ParseInit (Entry->Type);
|
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
if (IsTypeVoid (Decl.Type)) {
|
|
|
|
/* We cannot declare variables of type void */
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Illegal type for variable '%s'", Decl.Ident);
|
2003-02-19 22:58:13 +00:00
|
|
|
Entry->Flags &= ~(SC_STORAGE | SC_DEF);
|
2020-08-02 17:36:19 +00:00
|
|
|
} else if (Size == 0 && SymIsDef (Entry)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Size is unknown. Is it an array? */
|
2003-02-19 22:58:13 +00:00
|
|
|
if (!IsTypeArray (Decl.Type)) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Variable '%s' has unknown size", Decl.Ident);
|
2003-02-19 22:58:13 +00:00
|
|
|
}
|
2017-03-21 21:35:25 +00:00
|
|
|
} else {
|
2017-06-17 01:53:50 +00:00
|
|
|
/* A global (including static) uninitialized variable is
|
|
|
|
** only a tentative definition. For example, this is valid:
|
2017-03-21 21:35:25 +00:00
|
|
|
** int i;
|
|
|
|
** int i;
|
|
|
|
** static int j;
|
|
|
|
** static int j = 42;
|
2017-06-17 01:53:50 +00:00
|
|
|
** Code for them will be generated by FinishCompile().
|
2017-03-21 21:35:25 +00:00
|
|
|
** For now, just save the BSS segment name
|
2017-06-17 01:53:50 +00:00
|
|
|
** (can be set by #pragma bss-name).
|
2017-03-21 21:35:25 +00:00
|
|
|
*/
|
|
|
|
const char* bssName = GetSegName (SEG_BSS);
|
2017-06-17 01:53:50 +00:00
|
|
|
|
2017-03-21 21:35:25 +00:00
|
|
|
if (Entry->V.BssName && strcmp (Entry->V.BssName, bssName) != 0) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Global variable '%s' already was defined in the '%s' segment.",
|
2017-03-21 21:35:25 +00:00
|
|
|
Entry->Name, Entry->V.BssName);
|
|
|
|
}
|
|
|
|
Entry->V.BssName = xstrdup (bssName);
|
2020-08-02 17:38:54 +00:00
|
|
|
|
|
|
|
/* Check for enum forward declaration.
|
|
|
|
** Warn about it when extensions are not allowed.
|
|
|
|
*/
|
|
|
|
if (Size == 0 && IsTypeEnum (Decl.Type)) {
|
|
|
|
if (IS_Get (&Standard) != STD_CC65) {
|
|
|
|
Warning ("ISO C forbids forward references to 'enum' types");
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Check for end of declaration list */
|
|
|
|
if (CurTok.Tok == TOK_COMMA) {
|
|
|
|
NextToken ();
|
|
|
|
comma = 1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Function declaration? */
|
|
|
|
if (Entry && IsTypeFunc (Entry->Type)) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Function */
|
|
|
|
if (!comma) {
|
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
|
|
|
/* Prototype only */
|
|
|
|
NextToken ();
|
|
|
|
} else {
|
2008-08-03 15:30:03 +00:00
|
|
|
/* Parse the function body */
|
2020-08-15 01:10:17 +00:00
|
|
|
NewFunc (Entry, FuncDef);
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Must be followed by a semicolon */
|
|
|
|
ConsumeSemi ();
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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. */
|
|
|
|
{
|
2004-12-16 22:43:52 +00:00
|
|
|
char DateStr[32];
|
|
|
|
char TimeStr[32];
|
2002-10-14 16:23:26 +00:00
|
|
|
time_t Time;
|
|
|
|
struct tm* TM;
|
|
|
|
|
|
|
|
/* Since strftime is locale dependent, we need the abbreviated month names
|
2017-02-13 20:04:45 +00:00
|
|
|
** in English.
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2002-10-14 16:23:26 +00:00
|
|
|
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 macros that are always defined */
|
2009-09-28 20:10:01 +00:00
|
|
|
DefineNumericMacro ("__CC65__", GetVersionAsNumber ());
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2004-06-21 22:22:11 +00:00
|
|
|
/* Language standard that is supported */
|
|
|
|
DefineNumericMacro ("__CC65_STD_C89__", STD_C89);
|
|
|
|
DefineNumericMacro ("__CC65_STD_C99__", STD_C99);
|
|
|
|
DefineNumericMacro ("__CC65_STD_CC65__", STD_CC65);
|
|
|
|
DefineNumericMacro ("__CC65_STD__", IS_Get (&Standard));
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2004-03-07 21:53:35 +00:00
|
|
|
/* Optimization macros. Since no source code has been parsed for now, the
|
2014-06-30 09:10:35 +00:00
|
|
|
** IS_Get functions access the values in effect now, regardless of any
|
|
|
|
** changes using #pragma later.
|
|
|
|
*/
|
2004-06-21 22:22:11 +00:00
|
|
|
if (IS_Get (&Optimize)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
DefineNumericMacro ("__OPT__", 1);
|
2017-04-03 21:20:26 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
long CodeSize = IS_Get (&CodeSizeFactor);
|
2013-05-09 11:56:54 +00:00
|
|
|
if (CodeSize > 100) {
|
|
|
|
DefineNumericMacro ("__OPT_i__", CodeSize);
|
|
|
|
}
|
2017-04-03 21:20:26 +00:00
|
|
|
}
|
|
|
|
if (IS_Get (&EnableRegVars)) {
|
|
|
|
DefineNumericMacro ("__OPT_r__", 1);
|
|
|
|
}
|
|
|
|
if (IS_Get (&InlineStdFuncs)) {
|
|
|
|
DefineNumericMacro ("__OPT_s__", 1);
|
|
|
|
}
|
|
|
|
if (IS_Get (&EagerlyInlineFuncs)) {
|
|
|
|
DefineNumericMacro ("__EAGERLY_INLINE_FUNCS__", 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);
|
2004-12-16 22:43:52 +00:00
|
|
|
xsprintf (DateStr, sizeof (DateStr), "\"%s %2d %d\"",
|
|
|
|
MonthNames[TM->tm_mon], TM->tm_mday, TM->tm_year + 1900);
|
2002-10-14 16:23:26 +00:00
|
|
|
strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
|
|
|
|
DefineTextMacro ("__DATE__", DateStr);
|
|
|
|
DefineTextMacro ("__TIME__", TimeStr);
|
2009-10-18 18:31:02 +00:00
|
|
|
|
2009-10-08 15:00:25 +00:00
|
|
|
/* Other standard macros */
|
|
|
|
/* DefineNumericMacro ("__STDC__", 1); <- not now */
|
|
|
|
DefineNumericMacro ("__STDC_HOSTED__", 1);
|
2002-09-23 21:37:15 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Create the base lexical level */
|
|
|
|
EnterGlobalLevel ();
|
|
|
|
|
2009-12-04 13:04:40 +00:00
|
|
|
/* Create the global code and data segments */
|
|
|
|
CreateGlobalSegments ();
|
|
|
|
|
|
|
|
/* Initialize the literal pool */
|
|
|
|
InitLiteralPool ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* 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
|
|
|
|
2004-09-16 12:42:41 +00:00
|
|
|
/* Are we supposed to compile or just preprocess the input? */
|
|
|
|
if (PreprocessOnly) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Open the file */
|
2009-02-23 21:25:59 +00:00
|
|
|
OpenOutputFile ();
|
|
|
|
|
|
|
|
/* Preprocess each line and write it to the output file */
|
2004-09-16 12:42:41 +00:00
|
|
|
while (NextLine ()) {
|
|
|
|
Preprocess ();
|
2009-09-28 15:59:18 +00:00
|
|
|
WriteOutput ("%.*s\n", (int) SB_GetLen (Line), SB_GetConstBuf (Line));
|
2004-09-16 12:42:41 +00:00
|
|
|
}
|
|
|
|
|
2009-02-23 21:25:59 +00:00
|
|
|
/* Close the output file */
|
|
|
|
CloseOutputFile ();
|
|
|
|
|
2004-09-16 12:42:41 +00:00
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2020-08-02 17:36:19 +00:00
|
|
|
/* Used for emitting externals */
|
|
|
|
SymEntry* Entry;
|
|
|
|
|
2004-09-16 12:42:41 +00:00
|
|
|
/* Ok, start the ball rolling... */
|
|
|
|
Parse ();
|
|
|
|
|
2020-08-02 17:36:19 +00:00
|
|
|
/* Reset the BSS segment name to its default; so that the below strcmp()
|
|
|
|
** will work as expected, at the beginning of the list of variables
|
|
|
|
*/
|
|
|
|
SetSegName (SEG_BSS, SEGNAME_BSS);
|
|
|
|
|
|
|
|
/* Walk over all global symbols and generate code for uninitialized
|
|
|
|
** global variables.
|
|
|
|
*/
|
|
|
|
for (Entry = GetGlobalSymTab ()->SymHead; Entry; Entry = Entry->NextSym) {
|
|
|
|
if ((Entry->Flags & (SC_STORAGE | SC_DEF | SC_STATIC)) == (SC_STORAGE | SC_STATIC)) {
|
|
|
|
/* Assembly definition of uninitialized global variable */
|
2020-08-02 17:36:19 +00:00
|
|
|
SymEntry* Sym = GetSymType (Entry->Type);
|
|
|
|
unsigned Size = SizeOf (Entry->Type);
|
|
|
|
if (Size == 0 && IsTypeArray (Entry->Type)) {
|
|
|
|
if (GetElementCount (Entry->Type) == UNSPECIFIED) {
|
|
|
|
/* Assume array size of 1 */
|
|
|
|
SetElementCount (Entry->Type, 1);
|
|
|
|
Size = SizeOf (Entry->Type);
|
2020-08-04 16:19:28 +00:00
|
|
|
Warning ("Incomplete array '%s[]' assumed to have one element", Entry->Name);
|
2020-08-02 17:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Sym = GetSymType (GetElementType (Entry->Type));
|
|
|
|
}
|
2020-08-02 17:36:19 +00:00
|
|
|
|
2020-08-02 17:36:19 +00:00
|
|
|
/* For non-ESU types, Size != 0 */
|
|
|
|
if (Size != 0 || (Sym != 0 && SymIsDef (Sym))) {
|
|
|
|
/* Set the segment name only when it changes */
|
|
|
|
if (strcmp (GetSegName (SEG_BSS), Entry->V.BssName) != 0) {
|
|
|
|
SetSegName (SEG_BSS, Entry->V.BssName);
|
|
|
|
g_segname (SEG_BSS);
|
|
|
|
}
|
|
|
|
g_usebss ();
|
|
|
|
g_defgloblabel (Entry->Name);
|
|
|
|
g_res (Size);
|
|
|
|
|
|
|
|
/* Mark as defined; so that it will be exported, not imported */
|
|
|
|
Entry->Flags |= SC_DEF;
|
|
|
|
} else {
|
|
|
|
/* Tentative declared variable is still of incomplete type */
|
2020-08-04 16:19:28 +00:00
|
|
|
Error ("Definition of '%s' has type '%s' that is never completed",
|
2020-08-02 17:36:19 +00:00
|
|
|
Entry->Name,
|
|
|
|
GetFullTypeName (Entry->Type));
|
2020-08-02 17:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-05 22:39:45 +00:00
|
|
|
}
|
2004-09-16 12:42:41 +00:00
|
|
|
|
2009-12-05 22:39:45 +00:00
|
|
|
if (Debug) {
|
|
|
|
PrintMacroStats (stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print an error report */
|
|
|
|
ErrorReport ();
|
|
|
|
}
|
2004-09-16 12:42:41 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2009-12-05 22:39:45 +00:00
|
|
|
|
|
|
|
void FinishCompile (void)
|
2020-08-02 17:36:19 +00:00
|
|
|
/* Emit literals, debug info, do cleanup and optimizations */
|
2009-12-05 22:39:45 +00:00
|
|
|
{
|
2017-02-13 20:04:45 +00:00
|
|
|
SymEntry* Entry;
|
|
|
|
|
2020-08-02 17:36:19 +00:00
|
|
|
/* Walk over all global symbols and do clean-up and optimizations for
|
|
|
|
** functions.
|
2017-02-13 20:04:45 +00:00
|
|
|
*/
|
|
|
|
for (Entry = GetGlobalSymTab ()->SymHead; Entry; Entry = Entry->NextSym) {
|
|
|
|
if (SymIsOutputFunc (Entry)) {
|
2009-12-05 22:39:45 +00:00
|
|
|
/* Function which is defined and referenced or extern */
|
2017-02-13 20:04:45 +00:00
|
|
|
MoveLiteralPool (Entry->V.F.LitPool);
|
|
|
|
CS_MergeLabels (Entry->V.F.Seg->Code);
|
|
|
|
RunOpt (Entry->V.F.Seg->Code);
|
2009-12-05 22:39:45 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
2009-12-08 20:35:24 +00:00
|
|
|
/* Output the literal pool */
|
2020-05-31 17:28:59 +00:00
|
|
|
OutputGlobalLiteralPool ();
|
2009-12-05 22:39:45 +00:00
|
|
|
|
2011-08-31 20:48:40 +00:00
|
|
|
/* Emit debug infos if enabled */
|
|
|
|
EmitDebugInfo ();
|
|
|
|
|
2009-12-05 22:39:45 +00:00
|
|
|
/* Write imported/exported symbols */
|
|
|
|
EmitExternals ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Leave the main lexical level */
|
|
|
|
LeaveGlobalLevel ();
|
|
|
|
}
|