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 */
|
2020-01-21 02:50:12 +00:00
|
|
|
#include "addrsize.h"
|
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"
|
2021-03-30 08:47:52 +00:00
|
|
|
#include "funcdesc.h"
|
2000-06-12 18:31:40 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "global.h"
|
2022-01-03 05:10:32 +00:00
|
|
|
#include "initdata.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"
|
2023-10-05 09:48:16 +00:00
|
|
|
#include "typecmp.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
|
|
|
{
|
2022-10-11 06:14:48 +00:00
|
|
|
SymEntry* Sym;
|
2020-08-15 01:10:17 +00:00
|
|
|
FuncDesc* FuncDef = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2022-07-26 13:10:26 +00:00
|
|
|
/* Initialization for deferred operations */
|
|
|
|
InitDeferredOps ();
|
|
|
|
|
|
|
|
/* Fill up the next token with a bogus semicolon and start the tokenizer */
|
|
|
|
NextTok.Tok = TOK_SEMI;
|
2001-05-22 09:32:24 +00:00
|
|
|
NextToken ();
|
2023-12-10 07:46:48 +00:00
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
|
|
|
|
/* 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;
|
2023-12-10 07:43:24 +00:00
|
|
|
int Comma;
|
2023-11-27 12:39:15 +00:00
|
|
|
int NeedClean = 0;
|
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) {
|
2023-12-10 07:46:48 +00:00
|
|
|
/* TODO: warn on this if we have a pedantic mode */
|
2013-05-09 11:56:54 +00:00
|
|
|
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
|
|
|
|
2020-07-26 20:16:47 +00:00
|
|
|
/* Check for a _Static_assert */
|
|
|
|
if (CurTok.Tok == TOK_STATIC_ASSERT) {
|
|
|
|
ParseStaticAssert ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-12-10 07:43:24 +00:00
|
|
|
/* Read the declaration specifier */
|
2024-01-09 20:51:59 +00:00
|
|
|
ParseDeclSpec (&Spec, TS_DEFAULT_TYPE_INT, SC_NONE);
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Don't accept illegal storage classes */
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Spec.StorageClass & SC_STORAGEMASK) == SC_AUTO ||
|
|
|
|
(Spec.StorageClass & SC_STORAGEMASK) == SC_REGISTER) {
|
|
|
|
Error ("Illegal storage class");
|
|
|
|
Spec.StorageClass &= ~SC_STORAGEMASK;
|
2003-01-20 21:02:57 +00:00
|
|
|
}
|
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
|
|
|
|
2023-12-10 07:46:48 +00:00
|
|
|
/* If we haven't got a type specifier yet, something must be wrong */
|
|
|
|
if ((Spec.Flags & DS_TYPE_MASK) == DS_NONE) {
|
|
|
|
/* Avoid extra errors if it was a failed type specifier */
|
|
|
|
if ((Spec.Flags & DS_EXTRA_TYPE) == 0) {
|
|
|
|
Error ("Declaration specifier expected");
|
|
|
|
}
|
|
|
|
NeedClean = -1;
|
|
|
|
goto EndOfDecl;
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read declarations for this type */
|
2023-12-10 07:43:24 +00:00
|
|
|
Comma = 0;
|
2013-05-09 11:56:54 +00:00
|
|
|
while (1) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2022-11-08 06:42:52 +00:00
|
|
|
Declarator Decl;
|
2009-10-18 18:31:02 +00:00
|
|
|
|
2023-12-10 07:43:24 +00:00
|
|
|
Sym = 0;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Read the next declaration */
|
2023-12-10 07:43:24 +00:00
|
|
|
NeedClean = ParseDecl (&Spec, &Decl, DM_IDENT_OR_EMPTY);
|
|
|
|
|
|
|
|
/* Bail out if there are errors */
|
|
|
|
if (NeedClean <= 0) {
|
|
|
|
break;
|
2023-11-27 12:39:15 +00:00
|
|
|
}
|
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
|
|
|
**
|
2024-01-09 20:51:59 +00:00
|
|
|
** This means that "extern int i;" will not get storage allocated
|
|
|
|
** in this translation unit.
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Decl.StorageClass & SC_TYPEMASK) != SC_FUNC &&
|
2021-03-13 19:39:05 +00:00
|
|
|
(Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
|
2024-01-09 20:51:59 +00:00
|
|
|
/* The variable is visible in the file scope */
|
|
|
|
if ((Decl.StorageClass & SC_STORAGEMASK) == SC_NONE ||
|
|
|
|
(Decl.StorageClass & SC_STORAGEMASK) == SC_STATIC ||
|
|
|
|
((Decl.StorageClass & SC_STORAGEMASK) == SC_EXTERN &&
|
2020-07-20 14:34:57 +00:00
|
|
|
CurTok.Tok == TOK_ASSIGN)) {
|
2024-01-09 20:51:59 +00:00
|
|
|
/* We will allocate storage in this translation unit */
|
|
|
|
Decl.StorageClass |= SC_TU_STORAGE;
|
2020-07-20 14:34:57 +00:00
|
|
|
}
|
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
|
|
|
*/
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Decl.StorageClass & SC_TYPEMASK) == SC_FUNC) {
|
|
|
|
/* The function is now visible in the file scope */
|
2022-11-12 04:28:27 +00:00
|
|
|
if (CurTok.Tok == TOK_LCURLY) {
|
2020-08-12 13:35:47 +00:00
|
|
|
/* 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 */
|
2023-12-13 18:34:03 +00:00
|
|
|
FuncDef = GetFuncDesc (Decl.Type);
|
|
|
|
if ((FuncDef->Flags & (FD_EMPTY | FD_OLDSTYLE)) == FD_OLDSTYLE) {
|
|
|
|
/* A parameter list without types is only allowed in a
|
|
|
|
** function definition.
|
|
|
|
*/
|
|
|
|
Error ("Parameter names without types in function declaration");
|
|
|
|
}
|
2008-08-03 20:55:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add an entry to the symbol table */
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym = 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 */
|
2022-10-11 06:14:48 +00:00
|
|
|
SymUseAttr (Sym, &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 */
|
2024-01-09 20:51:59 +00:00
|
|
|
if (Decl.StorageClass & SC_TU_STORAGE) {
|
2013-05-09 11:56:54 +00:00
|
|
|
|
|
|
|
/* Get the size of the variable */
|
|
|
|
unsigned Size = SizeOf (Decl.Type);
|
|
|
|
|
|
|
|
/* Allow initialization */
|
|
|
|
if (CurTok.Tok == TOK_ASSIGN) {
|
|
|
|
|
2022-08-19 10:48:52 +00:00
|
|
|
/* This is a definition with storage */
|
2022-10-11 06:14:48 +00:00
|
|
|
if (SymIsDef (Sym)) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Global variable '%s' has already been defined",
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym->Name);
|
2017-02-13 20:10:21 +00:00
|
|
|
}
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym->Flags |= SC_DEF;
|
2017-02-13 20:04:45 +00:00
|
|
|
|
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) {
|
2020-08-14 22:27:11 +00:00
|
|
|
if (!IsEmptiableObjectType (Decl.Type)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if (!IsTypeArray (Decl.Type)) {
|
|
|
|
/* Size is unknown and not an array */
|
2020-08-14 22:27:11 +00:00
|
|
|
Error ("Cannot initialize variable '%s' of 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 */
|
2020-08-14 22:27:11 +00:00
|
|
|
Error ("Illegal type '%s' for variable '%s'",
|
|
|
|
GetFullTypeName (Decl.Type),
|
|
|
|
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 */
|
2022-10-11 06:14:48 +00:00
|
|
|
g_defgloblabel (Sym->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 */
|
2022-10-11 06:14:48 +00:00
|
|
|
ParseInit (Sym->Type);
|
2023-11-29 16:35:30 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2022-08-19 10:48:52 +00:00
|
|
|
/* This is a declaration */
|
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);
|
2023-12-14 13:27:48 +00:00
|
|
|
Sym->Flags |= SC_DEF;
|
2022-10-11 06:14:48 +00:00
|
|
|
} else if (Size == 0 && SymIsDef (Sym) && !IsEmptiableObjectType (Decl.Type)) {
|
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);
|
2023-12-14 13:27:48 +00:00
|
|
|
Sym->Flags |= SC_DEF;
|
2003-02-19 22:58:13 +00:00
|
|
|
}
|
2017-03-21 21:35:25 +00:00
|
|
|
} else {
|
2022-08-19 10:48:52 +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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-10-11 06:23:21 +00:00
|
|
|
if (Sym->V.BssName != 0) {
|
|
|
|
if (strcmp (Sym->V.BssName, bssName) != 0) {
|
|
|
|
Error ("Global variable '%s' already was defined in the '%s' segment",
|
|
|
|
Sym->Name, Sym->V.BssName);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Sym->V.BssName = xstrdup (bssName);
|
2017-03-21 21:35:25 +00:00
|
|
|
}
|
2020-08-02 17:38:54 +00:00
|
|
|
|
2020-01-21 02:50:12 +00:00
|
|
|
/* This is to make the automatical zeropage setting of the symbol
|
|
|
|
** work right.
|
|
|
|
*/
|
|
|
|
g_usebss ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make the symbol zeropage according to the segment address size */
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Sym->Flags & SC_TYPEMASK) == SC_NONE) {
|
|
|
|
if (SymIsGlobal (Sym) ||
|
|
|
|
(Sym->Flags & SC_STORAGEMASK) == SC_STATIC ||
|
|
|
|
(Sym->Flags & SC_STORAGEMASK) == SC_REGISTER) {
|
|
|
|
if (GetSegAddrSize (GetSegName (CS->CurDSeg)) == ADDR_SIZE_ZP) {
|
|
|
|
Sym->Flags |= SC_ZEROPAGE;
|
|
|
|
}
|
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 */
|
2023-12-10 07:43:24 +00:00
|
|
|
if (CurTok.Tok != TOK_COMMA) {
|
2013-05-09 11:56:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-12-10 07:43:24 +00:00
|
|
|
Comma = 1;
|
|
|
|
Spec.Flags |= DS_NO_EMPTY_DECL;
|
|
|
|
NextToken ();
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2023-11-29 16:35:30 +00:00
|
|
|
/* Finish the declaration */
|
2023-12-10 07:43:24 +00:00
|
|
|
if (Sym && IsTypeFunc (Sym->Type) && CurTok.Tok == TOK_LCURLY) {
|
|
|
|
/* A function definition is not terminated with a semicolon */
|
|
|
|
if (IsTypeFunc (Spec.Type) && TypeCmp (Sym->Type, Spec.Type).C >= TC_EQUAL) {
|
|
|
|
/* ISO C: The type category in a function definition cannot be
|
|
|
|
** inherited from a typedef.
|
|
|
|
*/
|
|
|
|
Error ("Function cannot be defined with a typedef");
|
|
|
|
} else if (Comma) {
|
|
|
|
/* ISO C: A function definition cannot shall its return type
|
|
|
|
** specifier with other declarators.
|
|
|
|
*/
|
|
|
|
Error ("';' expected after top level declarator");
|
|
|
|
}
|
2023-10-05 09:48:16 +00:00
|
|
|
|
2023-12-10 07:43:24 +00:00
|
|
|
/* Parse the function body anyways */
|
|
|
|
NeedClean = 0;
|
|
|
|
NewFunc (Sym, FuncDef);
|
|
|
|
|
|
|
|
/* Make sure we aren't omitting any work */
|
|
|
|
CheckDeferredOpAllDone ();
|
|
|
|
} else if (NeedClean > 0) {
|
|
|
|
/* Must be followed by a semicolon */
|
|
|
|
if (CurTok.Tok != TOK_SEMI) {
|
|
|
|
Error ("',' or ';' expected after top level declarator");
|
|
|
|
NeedClean = -1;
|
2023-11-29 16:35:30 +00:00
|
|
|
} else {
|
2023-12-10 07:43:24 +00:00
|
|
|
NextToken ();
|
|
|
|
NeedClean = 0;
|
2023-11-27 12:39:15 +00:00
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2023-11-27 12:39:15 +00:00
|
|
|
|
2023-12-10 07:46:48 +00:00
|
|
|
EndOfDecl:
|
2023-11-27 12:39:15 +00:00
|
|
|
/* Try some smart error recovery */
|
2023-12-10 07:43:24 +00:00
|
|
|
if (NeedClean < 0) {
|
2023-11-29 16:35:30 +00:00
|
|
|
SmartErrorSkip (1);
|
2023-11-27 12:39:15 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
2022-07-26 13:10:26 +00:00
|
|
|
|
|
|
|
/* Done with deferred operations */
|
|
|
|
DoneDeferredOps ();
|
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
|
|
|
}
|
|
|
|
|
2022-07-24 15:19:05 +00:00
|
|
|
/* Placeholders for __FILE__, __LINE__ and __COUNTER__ macros */
|
2022-07-24 15:19:05 +00:00
|
|
|
DefineTextMacro ("__FILE__", "");
|
|
|
|
DefineTextMacro ("__LINE__", "");
|
2022-07-24 15:19:05 +00:00
|
|
|
DefineTextMacro ("__COUNTER__", "");
|
2022-07-24 15:19:05 +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
|
|
|
|
2022-07-26 13:40:19 +00:00
|
|
|
/* Stuff unsupported */
|
|
|
|
if (IS_Get (&Standard) > STD_C99) {
|
|
|
|
DefineNumericMacro ("__STDC_NO_ATOMICS__", 1);
|
|
|
|
DefineNumericMacro ("__STDC_NO_COMPLEX__", 1);
|
|
|
|
DefineNumericMacro ("__STDC_NO_THREADS__", 1);
|
|
|
|
DefineNumericMacro ("__STDC_NO_VLA__", 1);
|
|
|
|
}
|
2020-04-16 09:19:16 +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 ();
|
|
|
|
|
2020-08-22 17:35:18 +00:00
|
|
|
/* There shouldn't be needs for local labels outside a function, but the
|
|
|
|
** current code generator still tries to get some at times even though the
|
|
|
|
** code were ill-formed. So just set it up with the global segment list.
|
|
|
|
*/
|
|
|
|
UseLabelPoolFromSegments (GS);
|
|
|
|
|
2009-12-04 13:04:40 +00:00
|
|
|
/* Initialize the literal pool */
|
|
|
|
InitLiteralPool ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Generate the code generator preamble */
|
|
|
|
g_preamble ();
|
|
|
|
|
2022-07-26 13:10:38 +00:00
|
|
|
/* Init preprocessor */
|
|
|
|
InitPreprocess ();
|
|
|
|
|
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 */
|
2022-07-26 13:10:26 +00:00
|
|
|
while (PreprocessNextLine ())
|
|
|
|
{ /* Nothing */ }
|
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) {
|
2024-01-09 20:51:59 +00:00
|
|
|
/* Is it a global (with or without static) tentative declaration of
|
|
|
|
** an uninitialized variable?
|
|
|
|
*/
|
|
|
|
if ((Entry->Flags & (SC_TU_STORAGE | SC_DEF)) == SC_TU_STORAGE) {
|
2020-08-02 17:36:19 +00:00
|
|
|
/* Assembly definition of uninitialized global variable */
|
2022-10-12 05:10:17 +00:00
|
|
|
SymEntry* TagSym = GetESUTagSym (Entry->Type);
|
2020-08-02 17:36:19 +00:00
|
|
|
unsigned Size = SizeOf (Entry->Type);
|
2022-10-11 06:14:48 +00:00
|
|
|
|
2020-08-02 17:36:19 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
TagSym = GetESUTagSym (GetElementType (Entry->Type));
|
2020-08-02 17:36:19 +00:00
|
|
|
}
|
2020-08-02 17:36:19 +00:00
|
|
|
|
2020-08-02 17:36:19 +00:00
|
|
|
/* For non-ESU types, Size != 0 */
|
2022-10-11 06:14:48 +00:00
|
|
|
if (Size != 0 || (TagSym != 0 && SymIsDef (TagSym))) {
|
2020-08-02 17:36:19 +00:00
|
|
|
/* 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;
|
2023-10-27 13:51:45 +00:00
|
|
|
} else if (!IsTypeArray (Entry->Type)) {
|
2020-08-02 17:36:19 +00:00
|
|
|
/* Tentative declared variable is still of incomplete type */
|
2023-12-14 13:27:48 +00:00
|
|
|
Error ("Definition of '%s' never has its type '%s' completed",
|
2020-08-02 17:36:19 +00:00
|
|
|
Entry->Name,
|
|
|
|
GetFullTypeName (Entry->Type));
|
2020-08-02 17:36:19 +00:00
|
|
|
}
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if (!SymIsDef (Entry) && (Entry->Flags & SC_TYPEMASK) == SC_FUNC) {
|
2023-12-14 13:27:48 +00:00
|
|
|
/* Check for undefined functions */
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Entry->Flags & SC_STORAGEMASK) == SC_STATIC && SymIsRef (Entry)) {
|
2023-12-14 13:27:48 +00:00
|
|
|
Warning ("Static function '%s' used but never defined",
|
|
|
|
Entry->Name);
|
|
|
|
}
|
2020-08-02 17:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
2004-09-16 12:42:41 +00:00
|
|
|
|
2009-12-05 22:39:45 +00:00
|
|
|
}
|
2004-09-16 12:42:41 +00:00
|
|
|
|
2022-07-26 13:10:38 +00:00
|
|
|
/* Done with preprocessor */
|
|
|
|
DonePreprocess ();
|
2020-04-16 09:19:16 +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)) {
|
2020-08-22 17:35:18 +00:00
|
|
|
/* Continue with previous label numbers */
|
|
|
|
UseLabelPoolFromSegments (Entry->V.F.Seg);
|
|
|
|
|
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 ();
|
|
|
|
}
|