2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* symtab.c */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Symbol table management for the cc65 C compiler */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2013-02-02 22:31:26 +00:00
|
|
|
/* (C) 2000-2013, Ullrich von Bassewitz */
|
2009-08-29 17:38:53 +00:00
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2021-09-10 19:09:22 +00:00
|
|
|
#include <errno.h>
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
/* common */
|
|
|
|
#include "check.h"
|
2003-01-04 16:59:51 +00:00
|
|
|
#include "debugflag.h"
|
2011-08-12 15:36:53 +00:00
|
|
|
#include "hashfunc.h"
|
2000-08-01 15:04:35 +00:00
|
|
|
#include "xmalloc.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
/* cc65 */
|
2020-08-12 13:35:47 +00:00
|
|
|
#include "anonname.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "asmcode.h"
|
|
|
|
#include "asmlabel.h"
|
|
|
|
#include "codegen.h"
|
|
|
|
#include "datatype.h"
|
|
|
|
#include "declare.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "funcdesc.h"
|
2020-08-02 17:31:52 +00:00
|
|
|
#include "function.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "global.h"
|
2020-08-02 17:31:52 +00:00
|
|
|
#include "input.h"
|
2004-06-05 11:35:53 +00:00
|
|
|
#include "stackptr.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "symentry.h"
|
2000-08-14 22:18:26 +00:00
|
|
|
#include "typecmp.h"
|
2021-03-15 08:59:08 +00:00
|
|
|
#include "typeconv.h"
|
2023-12-09 16:47:10 +00:00
|
|
|
#include "wrappedcall.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "symtab.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
/* An empty symbol table */
|
2013-05-09 11:56:54 +00:00
|
|
|
SymTable EmptySymTab = {
|
|
|
|
0, /* PrevTab */
|
|
|
|
0, /* SymHead */
|
|
|
|
0, /* SymTail */
|
|
|
|
0, /* SymCount */
|
|
|
|
1, /* Size */
|
|
|
|
{ 0 } /* Tab[1] */
|
2000-05-28 13:40:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Symbol table sizes */
|
2013-05-09 11:56:54 +00:00
|
|
|
#define SYMTAB_SIZE_GLOBAL 211U
|
|
|
|
#define SYMTAB_SIZE_FUNCTION 29U
|
|
|
|
#define SYMTAB_SIZE_BLOCK 13U
|
|
|
|
#define SYMTAB_SIZE_STRUCT 19U
|
|
|
|
#define SYMTAB_SIZE_LABEL 7U
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* The current and root symbol tables */
|
2022-01-03 15:54:14 +00:00
|
|
|
static unsigned LexLevelDepth = 0; /* For safety checks */
|
|
|
|
static LexicalLevel* CurrentLex = 0;
|
2013-05-09 11:56:54 +00:00
|
|
|
static SymTable* SymTab0 = 0;
|
|
|
|
static SymTable* SymTab = 0;
|
|
|
|
static SymTable* TagTab0 = 0;
|
|
|
|
static SymTable* TagTab = 0;
|
2021-03-29 04:35:29 +00:00
|
|
|
static SymTable* FieldTab = 0;
|
2013-05-09 11:56:54 +00:00
|
|
|
static SymTable* LabelTab = 0;
|
2018-10-13 07:05:33 +00:00
|
|
|
static SymTable* SPAdjustTab = 0;
|
2020-07-29 10:07:16 +00:00
|
|
|
static SymTable* FailSafeTab = 0; /* For errors */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2021-09-10 19:09:22 +00:00
|
|
|
static FILE* DebugTableFile = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* struct SymTable */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static SymTable* NewSymTable (unsigned Size)
|
|
|
|
/* Create and return a symbol table for the given lexical level */
|
|
|
|
{
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
/* Allocate memory for the table */
|
|
|
|
SymTable* S = xmalloc (sizeof (SymTable) + (Size-1) * sizeof (SymEntry*));
|
|
|
|
|
|
|
|
/* Initialize the symbol table structure */
|
2013-05-09 11:56:54 +00:00
|
|
|
S->PrevTab = 0;
|
|
|
|
S->SymHead = 0;
|
|
|
|
S->SymTail = 0;
|
|
|
|
S->SymCount = 0;
|
|
|
|
S->Size = Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
for (I = 0; I < Size; ++I) {
|
2013-05-09 11:56:54 +00:00
|
|
|
S->Tab[I] = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the symbol table */
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void FreeSymTable (SymTable* S)
|
|
|
|
/* Free the given symbo table including all symbols */
|
|
|
|
{
|
|
|
|
/* Free all symbols */
|
|
|
|
SymEntry* Sym = S->SymHead;
|
|
|
|
while (Sym) {
|
2013-05-09 11:56:54 +00:00
|
|
|
SymEntry* NextSym = Sym->NextSym;
|
|
|
|
FreeSymEntry (Sym);
|
|
|
|
Sym = NextSym;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Free the table itself */
|
|
|
|
xfree (S);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Check symbols in a table */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckSymTable (SymTable* Tab)
|
|
|
|
/* Check a symbol table for open references, unused symbols ... */
|
|
|
|
{
|
|
|
|
SymEntry* Entry = Tab->SymHead;
|
|
|
|
while (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Get the storage flags for tne entry */
|
|
|
|
unsigned Flags = Entry->Flags;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Ignore typedef entries */
|
|
|
|
if (!SymIsTypeDef (Entry)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2024-01-09 20:51:59 +00:00
|
|
|
/* Check if the symbol has non-external linkage and is defined but not used */
|
|
|
|
if (!SymIsGlobal (Entry) || (Flags & SC_STORAGEMASK) == SC_STATIC) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if (SymIsDef (Entry) && !SymIsRef (Entry) &&
|
2009-10-19 10:19:26 +00:00
|
|
|
!SymHasAttr (Entry, atUnused)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if (Flags & SC_PARAM) {
|
2009-10-08 13:29:35 +00:00
|
|
|
if (IS_Get (&WarnUnusedParam)) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Warning ("Parameter '%s' is never used", Entry->Name);
|
2009-10-08 13:29:35 +00:00
|
|
|
}
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_TYPEMASK) == SC_FUNC) {
|
2021-05-07 16:38:26 +00:00
|
|
|
if (IS_Get (&WarnUnusedFunc)) {
|
|
|
|
Warning ("Function '%s' is defined but never used", Entry->Name);
|
|
|
|
}
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_TYPEMASK) == SC_NONE) {
|
|
|
|
if (IS_Get (&WarnUnusedVar) &&
|
|
|
|
!IsAnonName (Entry->Name) &&
|
|
|
|
(Flags & SC_CONST) != SC_CONST) {
|
2020-08-02 17:15:57 +00:00
|
|
|
Warning ("Variable '%s' is defined but never used", Entry->Name);
|
2009-10-08 13:29:35 +00:00
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the entry is a label, check if it was defined in the function */
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Flags & SC_TYPEMASK) == SC_LABEL) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if (!SymIsDef (Entry)) {
|
|
|
|
/* Undefined label */
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Undefined label: '%s'", Entry->Name);
|
2013-05-09 11:56:54 +00:00
|
|
|
} else if (!SymIsRef (Entry)) {
|
|
|
|
/* Defined but not used */
|
2009-10-08 13:29:35 +00:00
|
|
|
if (IS_Get (&WarnUnusedLabel)) {
|
2020-08-02 17:15:57 +00:00
|
|
|
Warning ("Label '%s' is defined but never used", Entry->Name);
|
2009-10-08 13:29:35 +00:00
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-08 20:27:05 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Next entry */
|
|
|
|
Entry = Entry->NextSym;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Handling of lexical levels */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-01-03 15:54:14 +00:00
|
|
|
unsigned GetLexicalLevelDepth (void)
|
|
|
|
/* Return the current lexical level depth */
|
|
|
|
{
|
|
|
|
return LexLevelDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-25 15:05:15 +00:00
|
|
|
unsigned GetLexicalLevel (void)
|
|
|
|
/* Return the current lexical level */
|
|
|
|
{
|
2022-01-03 15:54:14 +00:00
|
|
|
if (CurrentLex != 0) {
|
|
|
|
return CurrentLex->CurrentLevel;
|
|
|
|
}
|
|
|
|
return LEX_LEVEL_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PushLexicalLevel (unsigned NewLevel)
|
|
|
|
/* Enter the specified lexical level */
|
|
|
|
{
|
|
|
|
LexicalLevel* L = xmalloc (sizeof (LexicalLevel));
|
|
|
|
L->PrevLex = CurrentLex;
|
|
|
|
CurrentLex = L;
|
|
|
|
CurrentLex->CurrentLevel = NewLevel;
|
|
|
|
++LexLevelDepth;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PopLexicalLevel (void)
|
|
|
|
/* Exit the current lexical level */
|
|
|
|
{
|
|
|
|
LexicalLevel* L;
|
|
|
|
PRECONDITION (CurrentLex != 0 && LexLevelDepth > 0);
|
|
|
|
L = CurrentLex;
|
|
|
|
CurrentLex = L->PrevLex;
|
|
|
|
xfree (L);
|
|
|
|
--LexLevelDepth;
|
2002-11-25 15:05:15 +00:00
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void EnterGlobalLevel (void)
|
|
|
|
/* Enter the program global lexical level */
|
|
|
|
{
|
2021-09-10 19:09:22 +00:00
|
|
|
const char* OutName = NULL;
|
|
|
|
if (!SB_IsEmpty (&DebugTableName)) {
|
|
|
|
OutName = SB_GetConstBuf (&DebugTableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutName) {
|
|
|
|
/* Open the table file */
|
|
|
|
DebugTableFile = fopen (OutName, "w");
|
|
|
|
if (DebugTableFile == 0) {
|
|
|
|
Error ("Cannot create table dump file '%s': %s", OutName, strerror (errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Debug) {
|
|
|
|
DebugTableFile = stdout;
|
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Safety */
|
2022-01-03 15:54:14 +00:00
|
|
|
PRECONDITION (GetLexicalLevel () == LEX_LEVEL_NONE);
|
|
|
|
|
|
|
|
/* Enter global lexical level */
|
|
|
|
PushLexicalLevel (LEX_LEVEL_GLOBAL);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Create and assign the symbol table */
|
|
|
|
SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Create and assign the tag table */
|
|
|
|
TagTab0 = TagTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
|
2018-10-13 07:05:33 +00:00
|
|
|
|
2018-10-14 08:07:59 +00:00
|
|
|
/* Create and assign the table of SP adjustment symbols */
|
2018-10-13 07:05:33 +00:00
|
|
|
SPAdjustTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
|
2020-07-29 10:07:16 +00:00
|
|
|
|
|
|
|
/* Create and assign the table of fictitious symbols used with errors */
|
|
|
|
FailSafeTab = NewSymTable (SYMTAB_SIZE_GLOBAL);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeaveGlobalLevel (void)
|
|
|
|
/* Leave the program global lexical level */
|
|
|
|
{
|
|
|
|
/* Safety */
|
2022-01-03 15:54:14 +00:00
|
|
|
PRECONDITION (GetLexicalLevel () == LEX_LEVEL_GLOBAL);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check the tables */
|
|
|
|
CheckSymTable (SymTab0);
|
|
|
|
|
|
|
|
/* Dump the tables if requested */
|
2021-09-10 19:09:22 +00:00
|
|
|
if (DebugTableFile) {
|
|
|
|
SymEntry* Entry;
|
|
|
|
StrBuf* Header;
|
|
|
|
|
|
|
|
PrintSymTable (SymTab0, DebugTableFile, "Global symbol table");
|
|
|
|
PrintSymTable (TagTab0, DebugTableFile, "Global tag table");
|
|
|
|
|
|
|
|
Entry = TagTab0->SymHead;
|
|
|
|
if (Entry) {
|
|
|
|
fputs ("\nGlobal struct and union definitions", DebugTableFile);
|
|
|
|
fputs ("\n=========================\n", DebugTableFile);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!((Entry->Flags & SC_STRUCT) || (Entry->Flags & SC_UNION)) || !Entry->V.S.SymTab) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Header = NewStrBuf();
|
|
|
|
if(Entry->Flags & SC_STRUCT) {
|
|
|
|
SB_AppendStr (Header, "SC_STRUCT: ");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SB_AppendStr (Header, "SC_UNION: ");
|
|
|
|
}
|
|
|
|
SB_AppendStr (Header, Entry->Name);
|
|
|
|
SB_Terminate (Header);
|
|
|
|
|
|
|
|
PrintSymTable (Entry->V.S.SymTab, DebugTableFile, SB_GetConstBuf (Header));
|
|
|
|
} while ((Entry = Entry->NextSym));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the file */
|
|
|
|
if (DebugTableFile != stdout && fclose (DebugTableFile) != 0) {
|
|
|
|
Error ("Error closing table dump file '%s': %s", SB_GetConstBuf(&DebugTableName), strerror (errno));
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't delete the symbol and struct tables! */
|
2001-05-03 17:34:54 +00:00
|
|
|
SymTab = 0;
|
|
|
|
TagTab = 0;
|
2022-01-03 15:54:14 +00:00
|
|
|
|
|
|
|
/* Exit global lexical level */
|
|
|
|
PopLexicalLevel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EnterFunctionLevel (void)
|
|
|
|
/* Enter function lexical level */
|
|
|
|
{
|
|
|
|
SymTable* S;
|
|
|
|
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Enter function lexical level */
|
|
|
|
PushLexicalLevel (LEX_LEVEL_FUNCTION);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get a new symbol table and make it current */
|
|
|
|
S = NewSymTable (SYMTAB_SIZE_FUNCTION);
|
|
|
|
S->PrevTab = SymTab;
|
|
|
|
SymTab = S;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Get a new tag table and make it current */
|
2000-05-28 13:40:48 +00:00
|
|
|
S = NewSymTable (SYMTAB_SIZE_FUNCTION);
|
2000-06-12 18:31:40 +00:00
|
|
|
S->PrevTab = TagTab;
|
|
|
|
TagTab = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Create and assign a new label table */
|
2014-10-14 11:32:58 +00:00
|
|
|
S = NewSymTable (SYMTAB_SIZE_LABEL);
|
|
|
|
S->PrevTab = LabelTab;
|
|
|
|
LabelTab = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RememberFunctionLevel (struct FuncDesc* F)
|
|
|
|
/* Remember the symbol tables for the level and leave the level without checks */
|
|
|
|
{
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Safety */
|
|
|
|
PRECONDITION (GetLexicalLevel () == LEX_LEVEL_FUNCTION);
|
|
|
|
|
|
|
|
/* Leave function lexical level */
|
|
|
|
PopLexicalLevel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Remember the tables */
|
2000-06-12 18:31:40 +00:00
|
|
|
F->SymTab = SymTab;
|
|
|
|
F->TagTab = TagTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Don't delete the tables */
|
2000-06-12 18:31:40 +00:00
|
|
|
SymTab = SymTab->PrevTab;
|
|
|
|
TagTab = TagTab->PrevTab;
|
2014-10-14 11:32:58 +00:00
|
|
|
LabelTab = LabelTab->PrevTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ReenterFunctionLevel (struct FuncDesc* F)
|
|
|
|
/* Reenter the function lexical level using the existing tables from F */
|
|
|
|
{
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Enter function lexical level */
|
|
|
|
PushLexicalLevel (LEX_LEVEL_FUNCTION);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Make the tables current again */
|
|
|
|
F->SymTab->PrevTab = SymTab;
|
|
|
|
SymTab = F->SymTab;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
F->TagTab->PrevTab = TagTab;
|
|
|
|
TagTab = F->TagTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Create and assign a new label table */
|
|
|
|
LabelTab = NewSymTable (SYMTAB_SIZE_LABEL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LeaveFunctionLevel (void)
|
|
|
|
/* Leave function lexical level */
|
|
|
|
{
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Safety */
|
|
|
|
PRECONDITION (GetLexicalLevel () == LEX_LEVEL_FUNCTION);
|
|
|
|
|
|
|
|
/* Leave function lexical level */
|
|
|
|
PopLexicalLevel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check the tables */
|
|
|
|
CheckSymTable (SymTab);
|
|
|
|
CheckSymTable (LabelTab);
|
|
|
|
|
2021-09-10 19:09:22 +00:00
|
|
|
/* Dump the tables if requested */
|
|
|
|
if (DebugTableFile) {
|
|
|
|
StrBuf* SymbolHeader = NewStrBuf();
|
|
|
|
|
|
|
|
SB_AppendStr (SymbolHeader, "SC_FUNC: ");
|
|
|
|
SB_AppendStr (SymbolHeader, CurrentFunc->FuncEntry->AsmName);
|
|
|
|
SB_AppendStr (SymbolHeader, ": Symbol table");
|
|
|
|
SB_Terminate (SymbolHeader);
|
|
|
|
|
|
|
|
PrintSymTable (SymTab, DebugTableFile, SB_GetConstBuf(SymbolHeader));
|
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Drop the label table if it is empty */
|
|
|
|
if (LabelTab->SymCount == 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
FreeSymTable (LabelTab);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Don't delete the tables */
|
2000-06-12 18:31:40 +00:00
|
|
|
SymTab = SymTab->PrevTab;
|
|
|
|
TagTab = TagTab->PrevTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
LabelTab = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EnterBlockLevel (void)
|
|
|
|
/* Enter a nested block in a function */
|
|
|
|
{
|
|
|
|
SymTable* S;
|
|
|
|
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Enter block lexical level */
|
|
|
|
PushLexicalLevel (LEX_LEVEL_BLOCK);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get a new symbol table and make it current */
|
|
|
|
S = NewSymTable (SYMTAB_SIZE_BLOCK);
|
2013-05-09 11:56:54 +00:00
|
|
|
S->PrevTab = SymTab;
|
|
|
|
SymTab = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Get a new tag table and make it current */
|
2000-05-28 13:40:48 +00:00
|
|
|
S = NewSymTable (SYMTAB_SIZE_BLOCK);
|
2000-06-12 18:31:40 +00:00
|
|
|
S->PrevTab = TagTab;
|
|
|
|
TagTab = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LeaveBlockLevel (void)
|
|
|
|
/* Leave a nested block in a function */
|
|
|
|
{
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Safety */
|
|
|
|
PRECONDITION (GetLexicalLevel () == LEX_LEVEL_BLOCK);
|
|
|
|
|
|
|
|
/* Leave block lexical level */
|
|
|
|
PopLexicalLevel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check the tables */
|
|
|
|
CheckSymTable (SymTab);
|
|
|
|
|
|
|
|
/* Don't delete the tables */
|
2000-06-12 18:31:40 +00:00
|
|
|
SymTab = SymTab->PrevTab;
|
|
|
|
TagTab = TagTab->PrevTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EnterStructLevel (void)
|
|
|
|
/* Enter a nested block for a struct definition */
|
|
|
|
{
|
|
|
|
SymTable* S;
|
|
|
|
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Enter struct lexical level */
|
|
|
|
PushLexicalLevel (LEX_LEVEL_STRUCT);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Get a new symbol table and make it current. Note: Structs and enums
|
2014-06-30 09:10:35 +00:00
|
|
|
** nested in struct scope are NOT local to the struct but visible in the
|
|
|
|
** outside scope. So we will NOT create a new struct or enum table.
|
|
|
|
*/
|
2021-03-29 04:35:29 +00:00
|
|
|
S = NewSymTable (SYMTAB_SIZE_STRUCT);
|
|
|
|
S->PrevTab = FieldTab;
|
|
|
|
FieldTab = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LeaveStructLevel (void)
|
|
|
|
/* Leave a nested block for a struct definition */
|
|
|
|
{
|
2022-01-03 15:54:14 +00:00
|
|
|
/* Safety */
|
|
|
|
PRECONDITION (GetLexicalLevel () == LEX_LEVEL_STRUCT);
|
|
|
|
|
|
|
|
/* Leave struct lexical level */
|
|
|
|
PopLexicalLevel ();
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Don't delete the table */
|
2021-03-29 04:35:29 +00:00
|
|
|
FieldTab = FieldTab->PrevTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Find functions */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-16 06:08:44 +00:00
|
|
|
static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Search for an entry in one table */
|
|
|
|
{
|
|
|
|
/* Get the start of the hash chain */
|
|
|
|
SymEntry* E = T->Tab [Hash % T->Size];
|
|
|
|
while (E) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Compare the name */
|
|
|
|
if (strcmp (E->Name, Name) == 0) {
|
|
|
|
/* Found */
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
/* Not found, next entry in hash chain */
|
|
|
|
E = E->NextHash;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-12 16:46:14 +00:00
|
|
|
static SymEntry* FindVisibleSymInTree (const SymTable* Tab, const char* Name)
|
|
|
|
/* Find the visible symbol with the given name in the table tree that starts
|
|
|
|
** with Tab.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
/* Get the hash over the name */
|
|
|
|
unsigned Hash = HashStr (Name);
|
|
|
|
|
|
|
|
/* Check all symbol tables for the symbol */
|
|
|
|
while (Tab) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Try to find the symbol in this table */
|
|
|
|
SymEntry* E = FindSymInTable (Tab, Name, Hash);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2022-09-30 06:32:08 +00:00
|
|
|
while (E != 0 && (E->Flags & SC_ALIAS) == SC_ALIAS) {
|
|
|
|
/* Get the aliased entry */
|
|
|
|
E = E->V.A.Field;
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Bail out if we found it */
|
2024-01-12 16:46:14 +00:00
|
|
|
if (E != 0 && (Tab != SymTab0 || (E->Flags & SC_LOCALSCOPE) == 0)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
return E;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Repeat the search in the next higher lexical level */
|
|
|
|
Tab = Tab->PrevTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not found */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SymEntry* FindSym (const char* Name)
|
2024-01-12 16:46:14 +00:00
|
|
|
/* Find with the given name the symbol visible in the current scope */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2024-01-12 16:46:14 +00:00
|
|
|
return FindVisibleSymInTree (SymTab, Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 13:58:56 +00:00
|
|
|
SymEntry* FindGlobalSym (const char* Name)
|
|
|
|
/* Find the symbol with the given name in the global symbol table only */
|
|
|
|
{
|
|
|
|
return FindSymInTable (SymTab0, Name, HashStr (Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
SymEntry* FindLocalSym (const char* Name)
|
|
|
|
/* Find the symbol with the given name in the current symbol table only */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-12 18:31:40 +00:00
|
|
|
return FindSymInTable (SymTab, Name, HashStr (Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
SymEntry* FindTagSym (const char* Name)
|
2024-01-12 16:46:14 +00:00
|
|
|
/* Find with the given name the tag symbol visible in the current scope */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2024-01-12 16:46:14 +00:00
|
|
|
return FindVisibleSymInTree (TagTab, Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-29 07:09:53 +00:00
|
|
|
SymEntry FindStructField (const Type* T, const char* Name)
|
|
|
|
/* Find a struct/union field in the fields list.
|
|
|
|
** Return the info about the found field symbol filled in an entry struct by
|
|
|
|
** value, or an empty entry struct if the field is not found.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2022-10-11 06:14:48 +00:00
|
|
|
SymEntry* Field = 0;
|
|
|
|
SymEntry Res;
|
2020-07-29 07:09:53 +00:00
|
|
|
int Offs = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2020-07-19 05:47:48 +00:00
|
|
|
/* The given type may actually be a pointer to struct/union */
|
2008-08-31 18:23:58 +00:00
|
|
|
if (IsTypePtr (T)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
++T;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 05:47:48 +00:00
|
|
|
/* Only structs/unions have struct/union fields... */
|
2006-02-19 12:29:37 +00:00
|
|
|
if (IsClassStruct (T)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2022-10-11 06:14:48 +00:00
|
|
|
/* Get a pointer to the struct/union tag */
|
2022-10-12 05:10:17 +00:00
|
|
|
const SymEntry* TagSym = GetESUTagSym (T);
|
2022-10-11 06:14:48 +00:00
|
|
|
CHECK (TagSym != 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2020-07-19 05:47:48 +00:00
|
|
|
/* Now search in the struct/union symbol table. Beware: The table may
|
|
|
|
** not exist.
|
2014-06-30 09:10:35 +00:00
|
|
|
*/
|
2022-10-11 06:14:48 +00:00
|
|
|
if (TagSym->V.S.SymTab) {
|
|
|
|
Field = FindSymInTable (TagSym->V.S.SymTab, Name, HashStr (Name));
|
2021-02-15 10:37:01 +00:00
|
|
|
|
2022-10-11 06:14:48 +00:00
|
|
|
if (Field != 0) {
|
|
|
|
Offs = Field->V.Offs;
|
2020-07-29 07:09:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 06:14:48 +00:00
|
|
|
while (Field != 0 && (Field->Flags & SC_ALIAS) == SC_ALIAS) {
|
2020-07-29 07:09:53 +00:00
|
|
|
/* Get the real field */
|
2022-10-11 06:14:48 +00:00
|
|
|
Field = Field->V.A.Field;
|
2020-07-29 07:09:53 +00:00
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 06:14:48 +00:00
|
|
|
if (Field != 0) {
|
|
|
|
Res = *Field;
|
|
|
|
Res.V.Offs = Offs;
|
2020-07-29 07:09:53 +00:00
|
|
|
} else {
|
2022-10-11 06:14:48 +00:00
|
|
|
memset (&Res, 0, sizeof(SymEntry));
|
2020-07-29 07:09:53 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 06:14:48 +00:00
|
|
|
return Res;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add stuff to the symbol table */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-15 08:59:08 +00:00
|
|
|
static int IsDistinctRedef (const Type* lhst, const Type* rhst, typecmpcode_t Code, typecmpflag_t Flags)
|
|
|
|
/* Return if type compatibility result is "worse" than Code or if any bit of
|
|
|
|
** qualifier Flags is set.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
typecmp_t Result = TypeCmp (lhst, rhst);
|
|
|
|
if (Result.C < Code || (Result.F & TCF_MASK_QUAL & Flags) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-10-11 06:14:48 +00:00
|
|
|
static int HandleSymRedefinition (SymEntry* Sym, const Type* T, unsigned Flags)
|
2020-07-29 10:07:16 +00:00
|
|
|
/* Check and handle redefinition of existing symbols.
|
2020-08-02 17:15:57 +00:00
|
|
|
** Complete array sizes and function descriptors as well.
|
|
|
|
** Return true if there *is* an error.
|
2020-07-29 10:07:16 +00:00
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Get the type info of the existing symbol */
|
2022-10-11 06:14:48 +00:00
|
|
|
Type* E_Type = Sym->Type;
|
|
|
|
unsigned E_SCType = Sym->Flags & SC_TYPEMASK;
|
2020-07-29 10:07:16 +00:00
|
|
|
unsigned SCType = Flags & SC_TYPEMASK;
|
|
|
|
|
2020-08-12 13:35:47 +00:00
|
|
|
/* Some symbols may be redeclared if certain requirements are met */
|
2020-08-07 22:46:55 +00:00
|
|
|
if (E_SCType == SC_TYPEDEF) {
|
2020-07-29 10:07:16 +00:00
|
|
|
|
2020-08-12 13:35:47 +00:00
|
|
|
/* Existing typedefs cannot be redeclared as anything different */
|
2020-08-07 22:46:55 +00:00
|
|
|
if (SCType == SC_TYPEDEF) {
|
2021-03-15 08:59:08 +00:00
|
|
|
if (IsDistinctRedef (E_Type, T, TC_IDENTICAL, TCF_MASK_QUAL)) {
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Conflicting types for typedef '%s'", Sym->Name);
|
2022-09-29 11:38:32 +00:00
|
|
|
Note ("'%s' vs '%s'", GetFullTypeName (T), GetFullTypeName (E_Type));
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym = 0;
|
2020-07-29 10:07:16 +00:00
|
|
|
}
|
2020-08-07 22:46:55 +00:00
|
|
|
} else {
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Redefinition of typedef '%s' as different kind of symbol", Sym->Name);
|
|
|
|
Sym = 0;
|
2020-07-29 10:07:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Sym->Flags & SC_TYPEMASK) == SC_FUNC) {
|
2020-08-12 13:35:47 +00:00
|
|
|
|
|
|
|
/* In case of a function, use the new type descriptor, since it
|
|
|
|
** contains pointers to the new symbol tables that are needed if
|
|
|
|
** an actual function definition follows. Be sure not to use the
|
|
|
|
** new descriptor if it contains a function declaration with an
|
|
|
|
** empty parameter list.
|
|
|
|
*/
|
|
|
|
if (IsTypeFunc (T)) {
|
|
|
|
|
|
|
|
/* Check for duplicate function definitions */
|
2022-10-11 06:14:48 +00:00
|
|
|
if (SymIsDef (Sym) && (Flags & SC_DEF) == SC_DEF) {
|
2020-08-12 13:35:47 +00:00
|
|
|
Error ("Body for function '%s' has already been defined",
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym->Name);
|
|
|
|
Sym = 0;
|
2020-08-12 13:35:47 +00:00
|
|
|
} else {
|
|
|
|
/* New type must be compatible with the composite prototype */
|
2021-03-15 08:59:08 +00:00
|
|
|
if (IsDistinctRedef (E_Type, T, TC_EQUAL, TCF_MASK_QUAL)) {
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Conflicting function types for '%s'", Sym->Name);
|
2022-09-29 11:38:32 +00:00
|
|
|
Note ("'%s' vs '%s'", GetFullTypeName (T), GetFullTypeName (E_Type));
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym = 0;
|
2020-08-12 13:35:47 +00:00
|
|
|
} else {
|
|
|
|
/* Refine the existing composite prototype with this new
|
|
|
|
** one.
|
|
|
|
*/
|
2024-01-01 07:04:50 +00:00
|
|
|
TypeComposition (Sym->Type, T);
|
2020-08-12 13:35:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Redefinition of function '%s' as different kind of symbol", Sym->Name);
|
|
|
|
Sym = 0;
|
2020-08-12 13:35:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
} else {
|
|
|
|
|
2020-08-12 13:35:47 +00:00
|
|
|
/* Redeclarations of ESU types are checked elsewhere */
|
2020-08-07 22:46:55 +00:00
|
|
|
if (IsTypeArray (T) && IsTypeArray (E_Type)) {
|
|
|
|
|
|
|
|
/* Get the array sizes */
|
|
|
|
long Size = GetElementCount (T);
|
|
|
|
long ESize = GetElementCount (E_Type);
|
|
|
|
|
|
|
|
/* If we are handling arrays, the old entry or the new entry may be
|
|
|
|
** an incomplete declaration. Accept this, and if the exsting entry
|
|
|
|
** is incomplete, complete it.
|
|
|
|
*/
|
|
|
|
if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
|
2021-03-15 08:59:08 +00:00
|
|
|
IsDistinctRedef (E_Type + 1, T + 1, TC_IDENTICAL, TCF_MASK_QUAL)) {
|
2020-08-07 22:46:55 +00:00
|
|
|
/* Conflicting element types */
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Conflicting array types for '%s[]'", Sym->Name);
|
2022-09-29 11:38:32 +00:00
|
|
|
Note ("'%s' vs '%s'", GetFullTypeName (T), GetFullTypeName (E_Type));
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym = 0;
|
2020-08-07 22:46:55 +00:00
|
|
|
} else {
|
|
|
|
/* Check if we have a size in the existing definition */
|
|
|
|
if (ESize == UNSPECIFIED) {
|
|
|
|
/* Existing, size not given, use size from new def */
|
|
|
|
SetElementCount (E_Type, Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
} else {
|
|
|
|
|
2020-08-07 22:46:55 +00:00
|
|
|
/* New type must be equivalent */
|
2020-07-29 10:07:16 +00:00
|
|
|
if (SCType != E_SCType) {
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Redefinition of '%s' as different kind of symbol", Sym->Name);
|
|
|
|
Sym = 0;
|
2021-03-15 08:59:08 +00:00
|
|
|
} else if (IsDistinctRedef (E_Type, T, TC_EQUAL, TCF_MASK_QUAL)) {
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Conflicting types for '%s'", Sym->Name);
|
2022-09-29 11:38:32 +00:00
|
|
|
Note ("'%s' vs '%s'", GetFullTypeName (T), GetFullTypeName (E_Type));
|
2022-10-11 06:14:48 +00:00
|
|
|
Sym = 0;
|
2020-07-29 10:07:16 +00:00
|
|
|
} else if (E_SCType == SC_ENUMERATOR) {
|
2020-08-02 17:15:57 +00:00
|
|
|
/* Enumerators aren't allowed to be redeclared at all, even if
|
|
|
|
** all occurences are identical. The current code logic won't
|
|
|
|
** get here, but let's just do it.
|
|
|
|
*/
|
2022-10-11 06:14:48 +00:00
|
|
|
Error ("Redeclaration of enumerator constant '%s'", Sym->Name);
|
|
|
|
Sym = 0;
|
2020-07-29 10:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return if there are any errors */
|
2022-10-11 06:14:48 +00:00
|
|
|
return Sym == 0;
|
2020-07-29 10:07:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
static void AddSymEntry (SymTable* T, SymEntry* S)
|
|
|
|
/* Add a symbol to a symbol table */
|
|
|
|
{
|
|
|
|
/* Get the hash value for the name */
|
|
|
|
unsigned Hash = HashStr (S->Name) % T->Size;
|
|
|
|
|
|
|
|
/* Insert the symbol into the list of all symbols in this level */
|
|
|
|
if (T->SymTail) {
|
2013-05-09 11:56:54 +00:00
|
|
|
T->SymTail->NextSym = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
S->PrevSym = T->SymTail;
|
|
|
|
T->SymTail = S;
|
|
|
|
if (T->SymHead == 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* First symbol */
|
|
|
|
T->SymHead = S;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2004-05-26 13:44:53 +00:00
|
|
|
++T->SymCount;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Insert the symbol into the hash chain */
|
|
|
|
S->NextHash = T->Tab[Hash];
|
|
|
|
T->Tab[Hash] = S;
|
|
|
|
|
|
|
|
/* Tell the symbol in which table it is */
|
|
|
|
S->Owner = T;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-14 22:27:11 +00:00
|
|
|
SymEntry* AddEnumSym (const char* Name, unsigned Flags, const Type* Type, SymTable* Tab, unsigned* DSFlags)
|
2022-10-12 05:10:17 +00:00
|
|
|
/* Add an enum tag entry and return it */
|
2020-07-26 12:12:55 +00:00
|
|
|
{
|
2020-07-29 10:07:16 +00:00
|
|
|
SymTable* CurTagTab = TagTab;
|
2022-10-12 05:10:17 +00:00
|
|
|
SymEntry* TagEntry;
|
2020-08-12 13:33:46 +00:00
|
|
|
|
|
|
|
if ((Flags & SC_FICTITIOUS) == 0) {
|
|
|
|
/* Do we have an entry with this name already? */
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = FindSymInTable (CurTagTab, Name, HashStr (Name));
|
2020-08-12 13:33:46 +00:00
|
|
|
} else {
|
|
|
|
/* Add a fictitious symbol in the fail-safe table */
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = 0;
|
2020-08-12 13:33:46 +00:00
|
|
|
CurTagTab = FailSafeTab;
|
|
|
|
}
|
2020-07-29 10:07:16 +00:00
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry) {
|
2020-07-26 12:12:55 +00:00
|
|
|
|
|
|
|
/* We do have an entry. This may be a forward, so check it. */
|
2022-10-12 05:10:17 +00:00
|
|
|
if ((TagEntry->Flags & SC_TYPEMASK) != SC_ENUM) {
|
2020-07-26 12:12:55 +00:00
|
|
|
/* Existing symbol is not an enum */
|
|
|
|
Error ("Symbol '%s' is already different kind", Name);
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = 0;
|
2020-08-07 22:45:45 +00:00
|
|
|
} else if (Type != 0) {
|
2020-07-26 12:12:55 +00:00
|
|
|
/* Define the struct size if the underlying type is given. */
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry->V.E.Type != 0) {
|
2020-08-07 22:45:45 +00:00
|
|
|
/* Both are definitions. */
|
|
|
|
Error ("Multiple definition for 'enum %s'", Name);
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = 0;
|
2020-08-07 22:45:45 +00:00
|
|
|
} else {
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry->V.E.SymTab = Tab;
|
|
|
|
TagEntry->V.E.Type = Type;
|
|
|
|
TagEntry->Flags |= SC_DEF;
|
2020-08-14 22:27:11 +00:00
|
|
|
|
|
|
|
/* Remember this is the first definition of this type */
|
|
|
|
if (DSFlags != 0) {
|
|
|
|
*DSFlags |= DS_NEW_TYPE_DEF;
|
|
|
|
}
|
2020-07-26 12:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry == 0) {
|
2020-07-29 10:07:16 +00:00
|
|
|
/* Use the fail-safe table for fictitious symbols */
|
|
|
|
CurTagTab = FailSafeTab;
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 10:37:01 +00:00
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry == 0) {
|
2020-07-26 12:12:55 +00:00
|
|
|
|
|
|
|
/* Create a new entry */
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = NewSymEntry (Name, SC_ENUM);
|
2020-07-26 12:12:55 +00:00
|
|
|
|
|
|
|
/* Set the enum type data */
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry->V.E.SymTab = Tab;
|
|
|
|
TagEntry->V.E.Type = Type;
|
2020-07-26 12:12:55 +00:00
|
|
|
|
2020-08-02 17:31:52 +00:00
|
|
|
if (Type != 0) {
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry->Flags |= SC_DEF;
|
2020-08-02 17:31:52 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 22:27:11 +00:00
|
|
|
/* Remember this is the first definition of this type */
|
|
|
|
if (CurTagTab != FailSafeTab && DSFlags != 0) {
|
2022-10-12 05:10:17 +00:00
|
|
|
if ((TagEntry->Flags & SC_DEF) != 0) {
|
2020-08-14 22:27:11 +00:00
|
|
|
*DSFlags |= DS_NEW_TYPE_DEF;
|
|
|
|
}
|
|
|
|
*DSFlags |= DS_NEW_TYPE_DECL;
|
|
|
|
}
|
|
|
|
|
2020-07-26 12:12:55 +00:00
|
|
|
/* Add it to the current table */
|
2022-10-12 05:10:17 +00:00
|
|
|
AddSymEntry (CurTagTab, TagEntry);
|
2020-07-26 12:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the entry */
|
2022-10-12 05:10:17 +00:00
|
|
|
return TagEntry;
|
2020-07-26 12:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-14 22:27:11 +00:00
|
|
|
SymEntry* AddStructSym (const char* Name, unsigned Flags, unsigned Size, SymTable* Tab, unsigned* DSFlags)
|
2022-10-12 05:10:17 +00:00
|
|
|
/* Add a struct/union tag entry and return it */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2020-07-29 10:07:16 +00:00
|
|
|
SymTable* CurTagTab = TagTab;
|
2022-10-12 05:10:17 +00:00
|
|
|
SymEntry* TagEntry;
|
|
|
|
unsigned SCType = (Flags & SC_TYPEMASK);
|
2013-02-02 22:31:26 +00:00
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
/* SCType must be struct or union */
|
|
|
|
PRECONDITION (SCType == SC_STRUCT || SCType == SC_UNION);
|
2013-02-02 22:31:26 +00:00
|
|
|
|
2023-12-09 09:34:01 +00:00
|
|
|
/* Do we have an entry with this name already? */
|
|
|
|
TagEntry = FindSymInTable (CurTagTab, Name, HashStr (Name));
|
2020-08-12 13:33:46 +00:00
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We do have an entry. This may be a forward, so check it. */
|
2022-10-12 05:10:17 +00:00
|
|
|
if ((TagEntry->Flags & SC_TYPEMASK) != SCType) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Existing symbol is not a struct */
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Symbol '%s' is already different kind", Name);
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = 0;
|
|
|
|
} else if ((TagEntry->Flags & Flags & SC_DEF) == SC_DEF) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Both structs are definitions. */
|
2022-10-12 05:10:17 +00:00
|
|
|
if (SCType == SC_STRUCT) {
|
2020-08-08 12:12:04 +00:00
|
|
|
Error ("Multiple definition for 'struct %s'", Name);
|
|
|
|
} else {
|
|
|
|
Error ("Multiple definition for 'union %s'", Name);
|
|
|
|
}
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = 0;
|
2013-05-09 11:56:54 +00:00
|
|
|
} else {
|
2020-08-02 17:31:52 +00:00
|
|
|
/* Define the struct size if it is a definition */
|
|
|
|
if ((Flags & SC_DEF) == SC_DEF) {
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry->Flags = Flags;
|
|
|
|
TagEntry->V.S.SymTab = Tab;
|
|
|
|
TagEntry->V.S.Size = Size;
|
2020-08-14 22:27:11 +00:00
|
|
|
|
|
|
|
/* Remember this is the first definition of this type */
|
|
|
|
if (DSFlags != 0) {
|
|
|
|
*DSFlags |= DS_NEW_TYPE_DEF;
|
|
|
|
}
|
2023-12-09 09:34:01 +00:00
|
|
|
|
|
|
|
if ((Flags & SC_FICTITIOUS) == SC_FICTITIOUS) {
|
|
|
|
/* Add a fictitious symbol in the fail-safe table */
|
|
|
|
TagEntry = 0;
|
|
|
|
} else if (Size == 0) {
|
|
|
|
/* Empty struct is not supported now */
|
|
|
|
Error ("Empty %s type '%s' is not supported", SCType == SC_STRUCT ? "struct" : "union", Name);
|
|
|
|
TagEntry = 0;
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry == 0) {
|
2020-07-29 10:07:16 +00:00
|
|
|
/* Use the fail-safe table for fictitious symbols */
|
|
|
|
CurTagTab = FailSafeTab;
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 10:37:01 +00:00
|
|
|
|
2022-10-12 05:10:17 +00:00
|
|
|
if (TagEntry == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Create a new entry */
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry = NewSymEntry (Name, Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Set the struct data */
|
2022-10-12 05:10:17 +00:00
|
|
|
TagEntry->V.S.SymTab = Tab;
|
|
|
|
TagEntry->V.S.Size = Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2020-08-14 22:27:11 +00:00
|
|
|
/* Remember this is the first definition of this type */
|
|
|
|
if (CurTagTab != FailSafeTab && DSFlags != 0) {
|
2022-10-12 05:10:17 +00:00
|
|
|
if ((TagEntry->Flags & SC_DEF) != 0) {
|
2020-08-14 22:27:11 +00:00
|
|
|
*DSFlags |= DS_NEW_TYPE_DEF;
|
|
|
|
}
|
|
|
|
*DSFlags |= DS_NEW_TYPE_DECL;
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
/* Add it to the current tag table */
|
2022-10-12 05:10:17 +00:00
|
|
|
AddSymEntry (CurTagTab, TagEntry);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the entry */
|
2022-10-12 05:10:17 +00:00
|
|
|
return TagEntry;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-19 20:59:44 +00:00
|
|
|
SymEntry* AddBitField (const char* Name, const Type* T, unsigned Offs,
|
|
|
|
unsigned BitOffs, unsigned BitWidth, int SignednessSpecified)
|
2009-08-29 17:38:53 +00:00
|
|
|
/* Add a bit field to the local symbol table and return the symbol entry */
|
|
|
|
{
|
|
|
|
/* Do we have an entry with this name already? */
|
2021-03-29 04:35:29 +00:00
|
|
|
SymEntry* Entry = FindSymInTable (FieldTab, Name, HashStr (Name));
|
2023-10-21 15:52:14 +00:00
|
|
|
|
2009-08-29 17:38:53 +00:00
|
|
|
if (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We have a symbol with this name already */
|
2020-08-02 17:15:57 +00:00
|
|
|
Error ("Multiple definition for bit-field '%s'", Name);
|
2009-08-29 17:38:53 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Create a new entry */
|
|
|
|
Entry = NewSymEntry (Name, SC_BITFIELD);
|
2009-08-29 17:38:53 +00:00
|
|
|
|
2020-07-19 20:59:44 +00:00
|
|
|
/* Set the symbol attributes. Bit-fields are always integral types. */
|
2021-05-22 11:15:47 +00:00
|
|
|
Entry->V.Offs = Offs;
|
2009-08-29 17:38:53 +00:00
|
|
|
|
2020-07-19 20:59:44 +00:00
|
|
|
if (!SignednessSpecified) {
|
|
|
|
/* int is treated as signed int everywhere except bit-fields; switch it to unsigned,
|
|
|
|
** since this is allowed for bit-fields and avoids sign-extension, so is much faster.
|
2020-08-30 18:47:25 +00:00
|
|
|
** enums set SignednessSpecified to 1 to avoid this adjustment. Character types
|
|
|
|
** actually distinguish 3 types of char; char may either be signed or unsigned, which
|
|
|
|
** is controlled by `--signed-chars`. In bit-fields, however, we perform the same
|
|
|
|
** `char -> unsigned char` adjustment that is performed with other integral types.
|
2020-07-19 20:59:44 +00:00
|
|
|
*/
|
2022-10-12 05:10:17 +00:00
|
|
|
CHECK (IsSignSigned (T) || IsRankChar (T));
|
|
|
|
Entry->Type = NewBitFieldOf (GetUnsignedType (T), BitOffs, BitWidth);
|
|
|
|
} else {
|
|
|
|
Entry->Type = NewBitFieldOf (T, BitOffs, BitWidth);
|
2020-07-19 20:59:44 +00:00
|
|
|
}
|
2023-11-29 04:27:01 +00:00
|
|
|
Entry->Type[0].C |= GetQualifier (T) & T_MASK_QUAL;
|
2020-07-19 20:59:44 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add the entry to the symbol table */
|
2021-03-29 04:35:29 +00:00
|
|
|
AddSymEntry (FieldTab, Entry);
|
2009-08-29 17:38:53 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
SymEntry* AddConstSym (const char* Name, const Type* T, unsigned Flags, long Val)
|
2001-03-23 21:27:48 +00:00
|
|
|
/* Add an constant symbol to the symbol table and return it */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
/* Do we have an entry with this name already? */
|
2020-07-26 12:12:59 +00:00
|
|
|
SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
|
2023-10-21 15:52:14 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Entry) {
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Entry->Flags & SC_TYPEMASK) != (Flags & SC_TYPEMASK)) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Symbol '%s' is already different kind", Name);
|
2013-05-09 11:56:54 +00:00
|
|
|
} else {
|
2020-08-02 17:15:57 +00:00
|
|
|
Error ("Multiple definition for constant '%s'", Name);
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
return Entry;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new entry */
|
2024-01-09 20:51:59 +00:00
|
|
|
Entry = NewSymEntry (Name, Flags | SC_CONST);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2020-07-26 12:12:55 +00:00
|
|
|
/* We only have integer constants for now */
|
2013-05-09 11:56:54 +00:00
|
|
|
Entry->Type = TypeDup (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2020-07-26 12:12:55 +00:00
|
|
|
/* Set the constant data */
|
2001-03-23 21:27:48 +00:00
|
|
|
Entry->V.ConstVal = Val;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Add the entry to the symbol table */
|
2020-07-26 12:12:59 +00:00
|
|
|
AddSymEntry (SymTab, Entry);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
|
2018-10-13 07:05:33 +00:00
|
|
|
DefOrRef* AddDefOrRef (SymEntry* E, unsigned Flags)
|
2018-09-21 07:40:05 +00:00
|
|
|
/* Add definition or reference to the SymEntry and preserve its attributes */
|
|
|
|
{
|
|
|
|
DefOrRef *DOR;
|
|
|
|
|
|
|
|
DOR = xmalloc (sizeof (DefOrRef));
|
|
|
|
CollAppend (E->V.L.DefsOrRefs, DOR);
|
2022-11-02 06:16:15 +00:00
|
|
|
DOR->Line = GetCurrentLineNum ();
|
2021-12-12 22:36:03 +00:00
|
|
|
DOR->LocalsBlockId = (size_t)CollLast (&CurrentFunc->LocalsBlockStack);
|
2018-09-21 07:40:05 +00:00
|
|
|
DOR->Flags = Flags;
|
2018-09-25 07:56:08 +00:00
|
|
|
DOR->StackPtr = StackPtr;
|
2018-09-26 07:11:40 +00:00
|
|
|
DOR->Depth = CollCount (&CurrentFunc->LocalsBlockStack);
|
2020-08-22 17:35:11 +00:00
|
|
|
DOR->LateSP_Label = GetLocalDataLabel ();
|
2018-09-21 07:40:05 +00:00
|
|
|
|
|
|
|
return DOR;
|
|
|
|
}
|
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
|
|
|
|
|
2018-10-13 07:05:33 +00:00
|
|
|
unsigned short FindSPAdjustment (const char* Name)
|
2018-10-14 08:07:59 +00:00
|
|
|
/* Search for an entry in the table of SP adjustments */
|
2018-10-16 06:08:44 +00:00
|
|
|
{
|
2018-10-13 07:05:33 +00:00
|
|
|
SymEntry* Entry = FindSymInTable (SPAdjustTab, Name, HashStr (Name));
|
|
|
|
|
2018-10-14 08:07:59 +00:00
|
|
|
if (!Entry) {
|
|
|
|
Internal ("No SP adjustment label entry found");
|
2018-10-13 07:05:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 06:08:44 +00:00
|
|
|
return Entry->V.SPAdjustment;
|
2018-10-13 07:05:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
SymEntry* AddLabelSym (const char* Name, unsigned Flags)
|
2020-08-22 17:35:11 +00:00
|
|
|
/* Add a C goto label to the label table */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2018-09-21 07:40:05 +00:00
|
|
|
unsigned i;
|
2018-09-25 07:56:08 +00:00
|
|
|
DefOrRef *DOR, *NewDOR;
|
2018-09-29 05:11:09 +00:00
|
|
|
/* We juggle it so much that a shortcut will help with clarity */
|
|
|
|
Collection *AIC = &CurrentFunc->LocalsBlockStack;
|
2018-09-25 07:56:08 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Do we have an entry with this name already? */
|
|
|
|
SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
|
2023-10-21 15:52:14 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
if (SymIsDef (Entry) && (Flags & SC_DEF) != 0) {
|
|
|
|
/* Trying to define the label more than once */
|
2019-01-05 19:57:12 +00:00
|
|
|
Error ("Label '%s' is defined more than once", Name);
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2018-09-21 07:40:05 +00:00
|
|
|
|
2018-09-25 07:56:08 +00:00
|
|
|
NewDOR = AddDefOrRef (Entry, Flags);
|
|
|
|
|
2018-09-26 07:11:40 +00:00
|
|
|
/* Walk through all occurrences of the label so far and evaluate
|
2018-09-30 21:22:23 +00:00
|
|
|
** their relationship with the one passed to the function.
|
|
|
|
*/
|
2018-09-21 07:40:05 +00:00
|
|
|
for (i = 0; i < CollCount (Entry->V.L.DefsOrRefs); i++) {
|
|
|
|
DOR = CollAt (Entry->V.L.DefsOrRefs, i);
|
2018-09-26 07:11:40 +00:00
|
|
|
|
2020-07-21 14:41:31 +00:00
|
|
|
if ((DOR->Flags & SC_DEF) && (Flags & SC_REF) && (Flags & (SC_GOTO | SC_GOTO_IND))) {
|
2018-09-25 07:56:08 +00:00
|
|
|
/* We're processing a goto and here is its destination label.
|
2018-09-30 21:22:23 +00:00
|
|
|
** This means the difference between SP values is already known,
|
|
|
|
** so we simply emit the SP adjustment code.
|
|
|
|
*/
|
2018-09-29 05:11:09 +00:00
|
|
|
if (StackPtr != DOR->StackPtr) {
|
2018-09-26 07:11:40 +00:00
|
|
|
g_space (StackPtr - DOR->StackPtr);
|
2018-09-29 05:11:09 +00:00
|
|
|
}
|
2018-09-26 07:11:40 +00:00
|
|
|
|
2018-09-29 05:11:09 +00:00
|
|
|
/* Are we jumping into a block with initalization of an object that
|
2018-09-30 21:22:23 +00:00
|
|
|
** has automatic storage duration? Let's emit a warning.
|
|
|
|
*/
|
2021-12-12 22:36:03 +00:00
|
|
|
if ((size_t)CollLast (AIC) != DOR->LocalsBlockId &&
|
2018-09-29 05:11:09 +00:00
|
|
|
(CollCount (AIC) < DOR->Depth ||
|
2021-12-12 22:36:03 +00:00
|
|
|
(size_t)CollAt (AIC, DOR->Depth - 1) != DOR->LocalsBlockId)) {
|
2018-09-29 05:11:09 +00:00
|
|
|
Warning ("Goto at line %d to label %s jumps into a block with "
|
2018-09-30 21:22:23 +00:00
|
|
|
"initialization of an object that has automatic storage duration",
|
2022-11-02 06:16:15 +00:00
|
|
|
GetCurrentLineNum (), Name);
|
2018-09-25 07:56:08 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-21 07:40:05 +00:00
|
|
|
|
2018-09-29 05:11:09 +00:00
|
|
|
|
2020-07-21 14:41:31 +00:00
|
|
|
if ((DOR->Flags & SC_REF) && (DOR->Flags & (SC_GOTO | SC_GOTO_IND)) && (Flags & SC_DEF)) {
|
2018-09-25 07:56:08 +00:00
|
|
|
/* We're processing a label, let's update all gotos encountered
|
2018-09-30 21:22:23 +00:00
|
|
|
** so far
|
|
|
|
*/
|
2019-04-09 12:49:52 +00:00
|
|
|
if (DOR->Flags & SC_GOTO) {
|
|
|
|
SymEntry *E;
|
2019-04-13 16:43:44 +00:00
|
|
|
g_userodata ();
|
2019-04-09 12:49:52 +00:00
|
|
|
g_defdatalabel (DOR->LateSP_Label);
|
|
|
|
g_defdata (CF_CONST | CF_INT, StackPtr - DOR->StackPtr, 0);
|
|
|
|
|
|
|
|
/* Optimizer will need the information about the value of SP adjustment
|
|
|
|
** later, so let's preserve it.
|
|
|
|
*/
|
2024-01-09 20:51:59 +00:00
|
|
|
E = NewSymEntry (LocalDataLabelName (DOR->LateSP_Label), 0);
|
2019-04-09 12:49:52 +00:00
|
|
|
E->V.SPAdjustment = StackPtr - DOR->StackPtr;
|
|
|
|
AddSymEntry (SPAdjustTab, E);
|
|
|
|
}
|
2018-10-13 07:05:33 +00:00
|
|
|
|
2018-09-29 05:11:09 +00:00
|
|
|
/* Are we jumping into a block with initalization of an object that
|
2018-09-30 21:22:23 +00:00
|
|
|
** has automatic storage duration? Let's emit a warning.
|
|
|
|
*/
|
2021-12-12 22:36:03 +00:00
|
|
|
if ((size_t)CollLast (AIC) != DOR->LocalsBlockId &&
|
2018-09-29 05:11:09 +00:00
|
|
|
(CollCount (AIC) >= DOR->Depth ||
|
2021-12-12 22:36:03 +00:00
|
|
|
(size_t)CollLast (AIC) >= (size_t)DOR->Line))
|
2018-09-29 05:11:09 +00:00
|
|
|
Warning ("Goto at line %d to label %s jumps into a block with "
|
2018-09-30 21:22:23 +00:00
|
|
|
"initialization of an object that has automatic storage duration",
|
2018-09-29 05:11:09 +00:00
|
|
|
DOR->Line, Name);
|
2018-09-25 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-09-21 07:40:05 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Entry->Flags |= Flags;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Create a new entry */
|
|
|
|
Entry = NewSymEntry (Name, SC_LABEL | Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Set a new label number */
|
2018-09-21 07:40:05 +00:00
|
|
|
Entry->V.L.Label = GetLocalLabel ();
|
2019-04-10 11:46:07 +00:00
|
|
|
Entry->V.L.IndJumpFrom = NULL;
|
2018-09-21 07:40:05 +00:00
|
|
|
|
|
|
|
/* Create Collection for label definition and references */
|
|
|
|
Entry->V.L.DefsOrRefs = NewCollection ();
|
2018-09-25 07:56:08 +00:00
|
|
|
NewDOR = AddDefOrRef (Entry, Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-05-26 13:44:53 +00:00
|
|
|
/* Generate the assembler name of the label */
|
2018-09-21 07:40:05 +00:00
|
|
|
Entry->AsmName = xstrdup (LocalLabelName (Entry->V.L.Label));
|
2004-05-26 13:44:53 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add the entry to the label table */
|
|
|
|
AddSymEntry (LabelTab, Entry);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-25 07:56:08 +00:00
|
|
|
/* We are processing a goto, but the label has not yet been defined */
|
2018-09-29 19:06:33 +00:00
|
|
|
if (!SymIsDef (Entry) && (Flags & SC_REF) && (Flags & SC_GOTO)) {
|
2018-09-26 07:11:40 +00:00
|
|
|
g_lateadjustSP (NewDOR->LateSP_Label);
|
2018-09-25 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
SymEntry* AddLocalSym (const char* Name, const Type* T, unsigned Flags, int Offs)
|
2021-03-29 04:35:29 +00:00
|
|
|
/* Add a local or struct/union field symbol and return the symbol entry */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2021-03-29 04:35:29 +00:00
|
|
|
SymTable* Tab = (Flags & SC_STRUCTFIELD) == 0 ? SymTab : FieldTab;
|
2020-08-15 14:59:26 +00:00
|
|
|
ident Ident;
|
2020-07-29 10:07:16 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Do we have an entry with this name already? */
|
2023-11-27 12:42:50 +00:00
|
|
|
SymEntry* Entry;
|
|
|
|
|
|
|
|
/* HACK: only allows to add parameter symbols in a parameter list */
|
|
|
|
if ((Flags & SC_PARAM) == 0 && GetLexicalLevel () == LEX_LEVEL_PARAM_LIST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry = FindSymInTable (Tab, Name, HashStr (Name));
|
2023-10-21 15:52:14 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Entry) {
|
2023-09-17 15:47:22 +00:00
|
|
|
if ((Flags & SC_STRUCTFIELD) == 0) {
|
|
|
|
while (Entry && (Entry->Flags & SC_ALIAS) == SC_ALIAS) {
|
|
|
|
/* Get the aliased entry */
|
|
|
|
Entry = Entry->V.A.Field;
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We have a symbol with this name already */
|
2020-07-29 10:07:16 +00:00
|
|
|
if (HandleSymRedefinition (Entry, T, Flags)) {
|
|
|
|
Entry = 0;
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_TYPEMASK) != SC_TYPEDEF) {
|
2020-08-07 22:44:36 +00:00
|
|
|
/* Redefinitions are not allowed */
|
|
|
|
if (SymIsDef (Entry) && (Flags & SC_DEF) == SC_DEF) {
|
|
|
|
Error ("Multiple definition of '%s'", Entry->Name);
|
|
|
|
Entry = 0;
|
2023-10-21 15:52:14 +00:00
|
|
|
} else if ((Flags & SC_STRUCTFIELD) != 0) {
|
|
|
|
Error ("Duplicate member '%s'", Entry->Name);
|
|
|
|
Entry = 0;
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if (Entry->Owner == SymTab0) {
|
|
|
|
if ((Flags & SC_STORAGEMASK) == SC_AUTO ||
|
|
|
|
(Flags & SC_STORAGEMASK) == SC_REGISTER ||
|
|
|
|
(Flags & SC_STORAGEMASK) == SC_STATIC) {
|
|
|
|
Error ("Declaration of '%s' with no linkage follows extern declaration",
|
|
|
|
Name);
|
|
|
|
Entry = 0;
|
|
|
|
}
|
2020-08-07 22:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Entry == 0) {
|
2020-08-15 14:59:26 +00:00
|
|
|
if ((Flags & SC_PARAM) != 0) {
|
|
|
|
/* Use anonymous names */
|
|
|
|
Name = AnonName (Ident, "param");
|
|
|
|
} else {
|
|
|
|
/* Use the fail-safe table for fictitious symbols */
|
|
|
|
Tab = FailSafeTab;
|
|
|
|
}
|
2020-07-29 10:07:16 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 10:37:01 +00:00
|
|
|
|
2020-07-29 10:07:16 +00:00
|
|
|
if (Entry == 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Create a new entry */
|
|
|
|
Entry = NewSymEntry (Name, Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Set the symbol attributes */
|
|
|
|
Entry->Type = TypeDup (T);
|
2020-07-29 07:09:53 +00:00
|
|
|
|
|
|
|
if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD ||
|
2024-01-09 20:51:59 +00:00
|
|
|
(Flags & SC_TYPEMASK) == SC_TYPEDEF) {
|
2020-07-29 07:09:53 +00:00
|
|
|
if ((Flags & SC_ALIAS) != SC_ALIAS) {
|
|
|
|
Entry->V.Offs = Offs;
|
|
|
|
}
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_STORAGEMASK) == SC_AUTO) {
|
2002-11-24 23:31:54 +00:00
|
|
|
Entry->V.Offs = Offs;
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_STORAGEMASK) == SC_REGISTER) {
|
2002-11-24 23:31:54 +00:00
|
|
|
Entry->V.R.RegOffs = Offs;
|
2006-02-19 12:29:37 +00:00
|
|
|
Entry->V.R.SaveOffs = StackPtr;
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_STORAGEMASK) == SC_EXTERN ||
|
|
|
|
(Flags & SC_TYPEMASK) == SC_FUNC) {
|
2018-09-21 07:40:05 +00:00
|
|
|
Entry->V.L.Label = Offs;
|
2012-03-24 13:42:10 +00:00
|
|
|
SymSetAsmName (Entry);
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_STORAGEMASK) == SC_STATIC) {
|
2020-08-22 17:35:11 +00:00
|
|
|
/* Generate the assembler name from the data label number */
|
2018-09-21 07:40:05 +00:00
|
|
|
Entry->V.L.Label = Offs;
|
2020-08-22 17:35:11 +00:00
|
|
|
Entry->AsmName = xstrdup (LocalDataLabelName (Entry->V.L.Label));
|
2022-09-30 06:32:08 +00:00
|
|
|
} else if ((Flags & SC_ALIAS) == SC_ALIAS) {
|
|
|
|
/* Just clear the info */
|
|
|
|
Entry->V.A.Field = 0;
|
|
|
|
Entry->V.A.ANumber = 0;
|
|
|
|
Entry->V.A.Offs = 0;
|
2002-11-24 23:31:54 +00:00
|
|
|
} else {
|
|
|
|
Internal ("Invalid flags in AddLocalSym: %04X", Flags);
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add the entry to the symbol table */
|
2020-07-29 10:07:16 +00:00
|
|
|
AddSymEntry (Tab, Entry);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
SymEntry* AddGlobalSym (const char* Name, const Type* T, unsigned Flags)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Add an external or global symbol to the symbol table and return the entry */
|
|
|
|
{
|
2022-09-30 06:32:08 +00:00
|
|
|
/* Add the new declaration to the global symbol table if no errors */
|
|
|
|
SymTable* Tab = SymTab0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2022-09-30 06:32:08 +00:00
|
|
|
/* Only search this name in the local and global symbol tables */
|
|
|
|
SymEntry* Entry = 0;
|
|
|
|
SymEntry* Alias = 0;
|
|
|
|
|
|
|
|
if (SymTab != SymTab0) {
|
|
|
|
Alias = Entry = FindLocalSym (Name);
|
|
|
|
while (Entry && (Entry->Flags & SC_ALIAS) == SC_ALIAS) {
|
|
|
|
/* Get the aliased entry */
|
|
|
|
Entry = Entry->V.A.Field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Entry == 0) {
|
|
|
|
Entry = FindGlobalSym (Name);
|
|
|
|
}
|
2021-01-24 10:19:48 +00:00
|
|
|
|
2024-01-09 20:51:59 +00:00
|
|
|
/* Do we have a symbol with this name already? */
|
2022-09-30 06:32:08 +00:00
|
|
|
if (Entry) {
|
2024-01-09 20:51:59 +00:00
|
|
|
/* Check if the symbol refers to some different type of things */
|
2020-07-29 10:07:16 +00:00
|
|
|
if (HandleSymRedefinition (Entry, T, Flags)) {
|
|
|
|
Entry = 0;
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if (Entry->Owner != SymTab0) {
|
|
|
|
/* The previous declaration has no linkage. The current declaration
|
|
|
|
** has either external or internal linkage. Either way it is an
|
|
|
|
** error since the two declarations would be referring to different
|
|
|
|
** objects with the same identifier.
|
|
|
|
*/
|
2020-08-12 13:35:50 +00:00
|
|
|
Error ("Extern declaration of '%s' follows declaration with no linkage",
|
|
|
|
Name);
|
|
|
|
Entry = 0;
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Flags & SC_TYPEMASK) != SC_TYPEDEF) {
|
2024-01-12 16:46:14 +00:00
|
|
|
/* If we are adding the symbol in the file scope, it is now
|
|
|
|
** visible there.
|
|
|
|
*/
|
|
|
|
if (SymTab == SymTab0) {
|
|
|
|
Entry->Flags &= ~SC_LOCALSCOPE;
|
|
|
|
}
|
|
|
|
|
2024-01-09 20:51:59 +00:00
|
|
|
/* The C standard specifies that the result is undefined if the
|
|
|
|
** same thing has both internal and external linkage. Most
|
|
|
|
** compilers choose to either give an error at compile time, or
|
|
|
|
** remove the external linkage for a link time error if used
|
|
|
|
** outside the current translation unit. We choose to give an
|
|
|
|
** error at compile time in this case.
|
2020-07-29 10:07:16 +00:00
|
|
|
*/
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Entry->Flags & SC_STORAGEMASK) != SC_STATIC) {
|
|
|
|
/* The previous declaration is a non-static declaration of an
|
|
|
|
** object or function that has external linkage.
|
2020-08-12 13:35:50 +00:00
|
|
|
*/
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Flags & SC_STORAGEMASK) == SC_STATIC) {
|
|
|
|
/* It is a static declaration of an object or function that
|
|
|
|
** has internal linkage. Conflicted wih the previous one.
|
|
|
|
*/
|
|
|
|
Error ("Static declaration of '%s' follows non-static declaration",
|
|
|
|
Name);
|
|
|
|
Entry = 0;
|
|
|
|
}
|
|
|
|
} else if ((Flags & SC_STORAGEMASK) != SC_STATIC) {
|
|
|
|
/* The previous declaration is a static declaration of an
|
|
|
|
** object or function that has internal linkage.
|
|
|
|
*/
|
|
|
|
if ((Flags & SC_STORAGEMASK) == SC_EXTERN ||
|
|
|
|
(Flags & SC_TYPEMASK) == SC_FUNC) {
|
|
|
|
/* The C standard specifies that an extern declaration
|
|
|
|
** shall keep the previously declared internal linkage
|
|
|
|
** unchanged. For a function declaration with no storage
|
|
|
|
** class specifiers, it is treated as if with 'extern'.
|
|
|
|
** We give a warning although it is not required by the
|
|
|
|
** standard.
|
|
|
|
*/
|
|
|
|
Flags &= ~SC_STORAGEMASK;
|
|
|
|
Warning ("Extern declaration of '%s' follows static declaration",
|
|
|
|
Name);
|
|
|
|
} else if ((Flags & SC_STORAGEMASK) == SC_NONE) {
|
|
|
|
/* It is a non-extern-or-static declaration of an object in
|
|
|
|
** file scope that has external linkage. Conflicted wih the
|
|
|
|
** previous one.
|
|
|
|
*/
|
|
|
|
Error ("Non-static declaration of '%s' follows static declaration",
|
|
|
|
Name);
|
2020-08-12 13:35:50 +00:00
|
|
|
Entry = 0;
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:35:50 +00:00
|
|
|
if (Entry) {
|
|
|
|
/* Add the new flags */
|
|
|
|
Entry->Flags |= Flags;
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
2015-08-09 10:27:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-12 13:35:50 +00:00
|
|
|
if (Entry == 0) {
|
|
|
|
/* Use the fail-safe table for fictitious symbols */
|
|
|
|
Tab = FailSafeTab;
|
|
|
|
}
|
2021-01-24 10:19:48 +00:00
|
|
|
}
|
2022-09-30 06:32:08 +00:00
|
|
|
|
|
|
|
if (Entry == 0) {
|
2024-01-12 16:46:14 +00:00
|
|
|
/* Hide the symbol in the file scope if we are declaring it in a
|
|
|
|
** local scope.
|
|
|
|
*/
|
|
|
|
if (Tab == SymTab0 && SymTab != SymTab0) {
|
|
|
|
Flags |= SC_LOCALSCOPE;
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Create a new entry */
|
|
|
|
Entry = NewSymEntry (Name, Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Set the symbol attributes */
|
|
|
|
Entry->Type = TypeDup (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-05-26 13:44:53 +00:00
|
|
|
/* Add the assembler name of the symbol */
|
2012-03-24 13:42:10 +00:00
|
|
|
SymSetAsmName (Entry);
|
2004-05-26 13:44:53 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add the entry to the symbol table */
|
|
|
|
AddSymEntry (Tab, Entry);
|
2023-12-09 16:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is a function, do we wrap calls to it? */
|
|
|
|
if (IsTypeFunc (Entry->Type)) {
|
|
|
|
SymEntry* WrappedCall;
|
|
|
|
unsigned int WrappedCallData;
|
2022-09-30 06:32:08 +00:00
|
|
|
|
2023-12-09 16:47:10 +00:00
|
|
|
/* Always use the latest wrapper data for it */
|
|
|
|
GetWrappedCall ((void**)&WrappedCall, &WrappedCallData);
|
|
|
|
if (WrappedCall) {
|
|
|
|
Entry->V.F.WrappedCall = WrappedCall;
|
|
|
|
Entry->V.F.WrappedCallData = WrappedCallData;
|
|
|
|
}
|
2022-09-30 06:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an alias of the global symbol to the local symbol table */
|
|
|
|
if (Tab == SymTab0 && SymTab != SymTab0 && Entry->Owner != SymTab && Alias == 0) {
|
|
|
|
Alias = AddLocalSym (Name, T, SC_ALIAS, 0);
|
2023-11-27 12:42:50 +00:00
|
|
|
if (Alias != 0) {
|
|
|
|
Alias->V.A.Field = Entry;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SymTable* GetSymTab (void)
|
|
|
|
/* Return the current symbol table */
|
|
|
|
{
|
|
|
|
return SymTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-03 17:34:54 +00:00
|
|
|
SymTable* GetGlobalSymTab (void)
|
|
|
|
/* Return the global symbol table */
|
|
|
|
{
|
|
|
|
return SymTab0;
|
|
|
|
}
|
|
|
|
|
2021-03-29 04:35:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
SymTable* GetFieldSymTab (void)
|
|
|
|
/* Return the current field symbol table */
|
|
|
|
{
|
|
|
|
return FieldTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-10 11:20:24 +00:00
|
|
|
SymTable* GetLabelSymTab (void)
|
|
|
|
/* Return the global symbol table */
|
|
|
|
{
|
|
|
|
return LabelTab;
|
|
|
|
}
|
|
|
|
|
2001-05-03 17:34:54 +00:00
|
|
|
|
|
|
|
|
2024-01-09 20:51:59 +00:00
|
|
|
int SymIsLocal (const SymEntry* Sym)
|
|
|
|
/* Return true if the symbol is declared in the highest lexical level */
|
2000-06-12 18:31:40 +00:00
|
|
|
{
|
|
|
|
return (Sym->Owner == SymTab || Sym->Owner == TagTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-01-09 20:51:59 +00:00
|
|
|
int SymIsGlobal (const SymEntry* Sym)
|
|
|
|
/* Return true if the symbol is declared in the file scope level */
|
|
|
|
{
|
|
|
|
return (Sym->Owner == SymTab0 || Sym->Owner == TagTab0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void MakeZPSym (const char* Name)
|
|
|
|
/* Mark the given symbol as zero page symbol */
|
|
|
|
{
|
|
|
|
/* Get the symbol table entry */
|
|
|
|
SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
|
|
|
|
|
|
|
|
/* Mark the symbol as zeropage */
|
|
|
|
if (Entry) {
|
2013-05-09 11:56:54 +00:00
|
|
|
Entry->Flags |= SC_ZEROPAGE;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2023-11-27 12:39:15 +00:00
|
|
|
Error ("Undeclared symbol: '%s'", Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintSymTable (const SymTable* Tab, FILE* F, const char* Header, ...)
|
|
|
|
/* Write the symbol table to the given file */
|
|
|
|
{
|
|
|
|
unsigned Len;
|
|
|
|
const SymEntry* Entry;
|
|
|
|
|
|
|
|
/* Print the header */
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, Header);
|
|
|
|
fputc ('\n', F);
|
|
|
|
Len = vfprintf (F, Header, ap);
|
|
|
|
va_end (ap);
|
|
|
|
fputc ('\n', F);
|
|
|
|
|
|
|
|
/* Underline the header */
|
|
|
|
while (Len--) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fputc ('=', F);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
fputc ('\n', F);
|
|
|
|
|
|
|
|
/* Dump the table */
|
|
|
|
Entry = Tab->SymHead;
|
|
|
|
if (Entry == 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
fprintf (F, "(empty)\n");
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2013-05-09 11:56:54 +00:00
|
|
|
while (Entry) {
|
|
|
|
DumpSymEntry (F, Entry);
|
|
|
|
Entry = Entry->NextSym;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
fprintf (F, "\n\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EmitExternals (void)
|
|
|
|
/* Write import/export statements for external symbols */
|
|
|
|
{
|
|
|
|
SymEntry* Entry;
|
|
|
|
|
|
|
|
Entry = SymTab->SymHead;
|
|
|
|
while (Entry) {
|
2013-05-09 11:56:54 +00:00
|
|
|
unsigned Flags = Entry->Flags;
|
2024-01-09 20:51:59 +00:00
|
|
|
if (Entry->Owner == SymTab0 &&
|
|
|
|
(Flags & SC_STORAGEMASK) != SC_STATIC &&
|
|
|
|
((Flags & SC_TYPEMASK) == SC_FUNC || (Flags & SC_TYPEMASK) == SC_NONE)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Only defined or referenced externs */
|
|
|
|
if (SymIsRef (Entry) && !SymIsDef (Entry)) {
|
|
|
|
/* An import */
|
|
|
|
g_defimport (Entry->Name, Flags & SC_ZEROPAGE);
|
|
|
|
} else if (SymIsDef (Entry)) {
|
|
|
|
/* An export */
|
|
|
|
g_defexport (Entry->Name, Flags & SC_ZEROPAGE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Entry = Entry->NextSym;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-31 20:48:40 +00:00
|
|
|
void EmitDebugInfo (void)
|
|
|
|
/* Emit debug infos for the locals of the current scope */
|
|
|
|
{
|
|
|
|
const char* Head;
|
|
|
|
const SymEntry* Sym;
|
|
|
|
|
|
|
|
/* Output info for locals if enabled */
|
|
|
|
if (DebugInfo) {
|
|
|
|
/* For cosmetic reasons in the output file, we will insert two tabs
|
2014-06-30 09:10:35 +00:00
|
|
|
** on global level and just one on local level.
|
|
|
|
*/
|
2022-01-03 15:54:14 +00:00
|
|
|
if (GetLexicalLevel () == LEX_LEVEL_GLOBAL) {
|
2011-08-31 20:48:40 +00:00
|
|
|
Head = "\t.dbg\t\tsym";
|
|
|
|
} else {
|
|
|
|
Head = "\t.dbg\tsym";
|
|
|
|
}
|
|
|
|
Sym = SymTab->SymHead;
|
|
|
|
while (Sym) {
|
2024-01-09 20:51:59 +00:00
|
|
|
if ((Sym->Flags & SC_TYPEMASK) == 0) {
|
|
|
|
if ((Sym->Flags & SC_STORAGEMASK) == SC_AUTO) {
|
2011-08-31 20:48:40 +00:00
|
|
|
AddTextLine ("%s, \"%s\", \"00\", auto, %d",
|
|
|
|
Head, Sym->Name, Sym->V.Offs);
|
2024-01-09 20:51:59 +00:00
|
|
|
} else if ((Sym->Flags & SC_STORAGEMASK) == SC_REGISTER) {
|
2011-08-31 20:48:40 +00:00
|
|
|
AddTextLine ("%s, \"%s\", \"00\", register, \"regbank\", %d",
|
|
|
|
Head, Sym->Name, Sym->V.R.RegOffs);
|
|
|
|
|
|
|
|
} else if (SymIsRef (Sym) && !SymIsDef (Sym)) {
|
|
|
|
AddTextLine ("%s, \"%s\", \"00\", %s, \"%s\"",
|
|
|
|
Head, Sym->Name,
|
2024-01-09 20:51:59 +00:00
|
|
|
(Sym->Flags & SC_STORAGEMASK) != SC_STATIC ? "extern" : "static",
|
2011-08-31 20:48:40 +00:00
|
|
|
Sym->AsmName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Sym = Sym->NextSym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|