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>
|
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 */
|
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"
|
|
|
|
#include "global.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"
|
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 */
|
2013-05-09 11:56:54 +00:00
|
|
|
static unsigned LexicalLevel = 0; /* For safety checks */
|
|
|
|
static SymTable* SymTab0 = 0;
|
|
|
|
static SymTable* SymTab = 0;
|
|
|
|
static SymTable* TagTab0 = 0;
|
|
|
|
static SymTable* TagTab = 0;
|
|
|
|
static SymTable* LabelTab = 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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Check if the symbol is one with storage, and it if it was
|
2014-06-30 09:10:35 +00:00
|
|
|
** defined but not used.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (((Flags & SC_AUTO) || (Flags & SC_STATIC)) && (Flags & SC_EXTERN) == 0) {
|
|
|
|
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)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
Warning ("Parameter `%s' is never used", Entry->Name);
|
2009-10-08 13:29:35 +00:00
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
} else {
|
2009-10-08 13:29:35 +00:00
|
|
|
if (IS_Get (&WarnUnusedVar)) {
|
|
|
|
Warning ("`%s' is defined but never used", Entry->Name);
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the entry is a label, check if it was defined in the function */
|
|
|
|
if (Flags & SC_LABEL) {
|
|
|
|
if (!SymIsDef (Entry)) {
|
|
|
|
/* Undefined label */
|
|
|
|
Error ("Undefined label: `%s'", Entry->Name);
|
|
|
|
} else if (!SymIsRef (Entry)) {
|
|
|
|
/* Defined but not used */
|
2009-10-08 13:29:35 +00:00
|
|
|
if (IS_Get (&WarnUnusedLabel)) {
|
2013-05-09 11:56:54 +00:00
|
|
|
Warning ("`%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
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-11-25 15:05:15 +00:00
|
|
|
unsigned GetLexicalLevel (void)
|
|
|
|
/* Return the current lexical level */
|
|
|
|
{
|
|
|
|
return LexicalLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void EnterGlobalLevel (void)
|
|
|
|
/* Enter the program global lexical level */
|
|
|
|
{
|
|
|
|
/* Safety */
|
|
|
|
PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL);
|
|
|
|
|
|
|
|
/* 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);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LeaveGlobalLevel (void)
|
|
|
|
/* Leave the program global lexical level */
|
|
|
|
{
|
|
|
|
/* Safety */
|
|
|
|
PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL);
|
|
|
|
|
|
|
|
/* Check the tables */
|
|
|
|
CheckSymTable (SymTab0);
|
|
|
|
|
|
|
|
/* Dump the tables if requested */
|
|
|
|
if (Debug) {
|
2013-05-09 11:56:54 +00:00
|
|
|
PrintSymTable (SymTab0, stdout, "Global symbol table");
|
|
|
|
PrintSymTable (TagTab0, stdout, "Global tag table");
|
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;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void EnterFunctionLevel (void)
|
|
|
|
/* Enter function lexical level */
|
|
|
|
{
|
|
|
|
SymTable* S;
|
|
|
|
|
|
|
|
/* New lexical level */
|
2002-11-25 15:13:03 +00:00
|
|
|
++LexicalLevel;
|
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 */
|
|
|
|
{
|
|
|
|
/* Leave the lexical level */
|
2002-11-25 15:13:03 +00:00
|
|
|
--LexicalLevel;
|
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 */
|
|
|
|
{
|
|
|
|
/* New lexical level */
|
2002-11-25 15:13:03 +00:00
|
|
|
++LexicalLevel;
|
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 */
|
|
|
|
{
|
|
|
|
/* Leave the lexical level */
|
2002-11-25 15:13:03 +00:00
|
|
|
--LexicalLevel;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check the tables */
|
|
|
|
CheckSymTable (SymTab);
|
|
|
|
CheckSymTable (LabelTab);
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* New lexical level */
|
|
|
|
++LexicalLevel;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
{
|
|
|
|
/* Leave the lexical level */
|
|
|
|
--LexicalLevel;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LeaveStructLevel (void)
|
|
|
|
/* Leave a nested block for a struct definition */
|
|
|
|
{
|
|
|
|
/* Don't delete the table */
|
|
|
|
SymTab = SymTab->PrevTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Find functions */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static SymEntry* FindSymInTable (const SymTable* T, const char* Name, unsigned Hash)
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static SymEntry* FindSymInTree (const SymTable* Tab, const char* Name)
|
|
|
|
/* Find the symbol with the given name in the table tree that starts with T */
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Bail out if we found it */
|
|
|
|
if (E != 0) {
|
|
|
|
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)
|
|
|
|
/* Find the symbol with the given name */
|
|
|
|
{
|
|
|
|
return FindSymInTree (SymTab, Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
/* Find the symbol with the given name in the tag table */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-12 18:31:40 +00:00
|
|
|
return FindSymInTree (TagTab, Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
SymEntry* FindStructField (const Type* T, const char* Name)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Find a struct field in the fields list */
|
|
|
|
{
|
|
|
|
SymEntry* Field = 0;
|
|
|
|
|
|
|
|
/* The given type may actually be a pointer to struct */
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-structs do not have any struct fields... */
|
2006-02-19 12:29:37 +00:00
|
|
|
if (IsClassStruct (T)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Get a pointer to the struct/union type */
|
|
|
|
const SymEntry* Struct = GetSymEntry (T);
|
|
|
|
CHECK (Struct != 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Now search in the struct symbol table. Beware: The table may not
|
2014-06-30 09:10:35 +00:00
|
|
|
** exist.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (Struct->V.S.SymTab) {
|
|
|
|
Field = FindSymInTable (Struct->V.S.SymTab, Name, HashStr (Name));
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Field;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add stuff to the symbol table */
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-02 22:31:26 +00:00
|
|
|
SymEntry* AddStructSym (const char* Name, unsigned Type, unsigned Size, SymTable* Tab)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Add a struct/union entry and return it */
|
|
|
|
{
|
2013-02-02 22:31:26 +00:00
|
|
|
SymEntry* Entry;
|
|
|
|
|
|
|
|
/* Type must be struct or union */
|
|
|
|
PRECONDITION (Type == SC_STRUCT || Type == SC_UNION);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Do we have an entry with this name already? */
|
2013-02-02 22:31:26 +00:00
|
|
|
Entry = FindSymInTable (TagTab, Name, HashStr (Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We do have an entry. This may be a forward, so check it. */
|
|
|
|
if ((Entry->Flags & SC_TYPEMASK) != Type) {
|
|
|
|
/* Existing symbol is not a struct */
|
|
|
|
Error ("Symbol `%s' is already different kind", Name);
|
|
|
|
} else if (Size > 0 && Entry->V.S.Size > 0) {
|
|
|
|
/* Both structs are definitions. */
|
|
|
|
Error ("Multiple definition for `%s'", Name);
|
|
|
|
} else {
|
|
|
|
/* Define the struct size if it is given */
|
|
|
|
if (Size > 0) {
|
|
|
|
Entry->V.S.SymTab = Tab;
|
|
|
|
Entry->V.S.Size = Size;
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Create a new entry */
|
|
|
|
Entry = NewSymEntry (Name, Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Set the struct data */
|
|
|
|
Entry->V.S.SymTab = Tab;
|
|
|
|
Entry->V.S.Size = Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add it to the current table */
|
|
|
|
AddSymEntry (TagTab, Entry);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-08-29 17:38:53 +00:00
|
|
|
SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width)
|
|
|
|
/* Add a bit field to the local symbol table and return the symbol entry */
|
|
|
|
{
|
|
|
|
/* Do we have an entry with this name already? */
|
|
|
|
SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
|
|
|
|
if (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We have a symbol with this name already */
|
|
|
|
Error ("Multiple definition for `%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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Set the symbol attributes. Bit-fields are always of type unsigned */
|
|
|
|
Entry->Type = type_uint;
|
2009-08-29 19:04:21 +00:00
|
|
|
Entry->V.B.Offs = Offs;
|
|
|
|
Entry->V.B.BitOffs = BitOffs;
|
|
|
|
Entry->V.B.BitWidth = Width;
|
2009-08-29 17:38:53 +00:00
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add the entry to the symbol table */
|
|
|
|
AddSymEntry (SymTab, 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
|
|
|
{
|
2003-10-09 18:56:38 +00:00
|
|
|
/* Enums must be inserted in the global symbol table */
|
|
|
|
SymTable* Tab = ((Flags & SC_ENUM) == SC_ENUM)? SymTab0 : SymTab;
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Do we have an entry with this name already? */
|
2003-10-09 18:56:38 +00:00
|
|
|
SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Entry) {
|
2013-05-09 11:56:54 +00:00
|
|
|
if ((Entry->Flags & SC_CONST) != SC_CONST) {
|
|
|
|
Error ("Symbol `%s' is already different kind", Name);
|
|
|
|
} else {
|
|
|
|
Error ("Multiple definition for `%s'", Name);
|
|
|
|
}
|
|
|
|
return Entry;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new entry */
|
2001-03-23 21:27:48 +00:00
|
|
|
Entry = NewSymEntry (Name, Flags);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Enum values are ints */
|
2013-05-09 11:56:54 +00:00
|
|
|
Entry->Type = TypeDup (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Set the enum 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 */
|
2003-10-09 18:56:38 +00:00
|
|
|
AddSymEntry (Tab, Entry);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Return the entry */
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SymEntry* AddLabelSym (const char* Name, unsigned Flags)
|
|
|
|
/* Add a goto label to the label table */
|
|
|
|
{
|
|
|
|
/* Do we have an entry with this name already? */
|
|
|
|
SymEntry* Entry = FindSymInTable (LabelTab, Name, HashStr (Name));
|
|
|
|
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 */
|
|
|
|
Error ("Label `%s' is defined more than once", Name);
|
|
|
|
}
|
|
|
|
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 */
|
|
|
|
Entry->V.Label = GetLocalLabel ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2004-05-26 13:44:53 +00:00
|
|
|
/* Generate the assembler name of the label */
|
|
|
|
Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Add a local symbol and return the symbol entry */
|
|
|
|
{
|
|
|
|
/* Do we have an entry with this name already? */
|
2000-06-22 11:28:39 +00:00
|
|
|
SymEntry* Entry = FindSymInTable (SymTab, Name, HashStr (Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We have a symbol with this name already */
|
|
|
|
Error ("Multiple definition for `%s'", Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
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);
|
2002-11-24 23:31:54 +00:00
|
|
|
if ((Flags & SC_AUTO) == SC_AUTO) {
|
|
|
|
Entry->V.Offs = Offs;
|
|
|
|
} else if ((Flags & SC_REGISTER) == SC_REGISTER) {
|
|
|
|
Entry->V.R.RegOffs = Offs;
|
2006-02-19 12:29:37 +00:00
|
|
|
Entry->V.R.SaveOffs = StackPtr;
|
2007-07-03 16:02:04 +00:00
|
|
|
} else if ((Flags & SC_EXTERN) == SC_EXTERN) {
|
|
|
|
Entry->V.Label = Offs;
|
2012-03-24 13:42:10 +00:00
|
|
|
SymSetAsmName (Entry);
|
2002-11-24 23:31:54 +00:00
|
|
|
} else if ((Flags & SC_STATIC) == SC_STATIC) {
|
2004-05-26 13:44:53 +00:00
|
|
|
/* Generate the assembler name from the label number */
|
2002-11-24 23:31:54 +00:00
|
|
|
Entry->V.Label = Offs;
|
2004-05-26 13:44:53 +00:00
|
|
|
Entry->AsmName = xstrdup (LocalLabelName (Entry->V.Label));
|
2002-11-24 23:31:54 +00:00
|
|
|
} else if ((Flags & SC_STRUCTFIELD) == SC_STRUCTFIELD) {
|
|
|
|
Entry->V.Offs = Offs;
|
|
|
|
} 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 */
|
|
|
|
AddSymEntry (SymTab, 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 */
|
|
|
|
{
|
2001-05-03 17:34:54 +00:00
|
|
|
/* There is some special handling for functions, so check if it is one */
|
2006-02-19 12:29:37 +00:00
|
|
|
int IsFunc = IsTypeFunc (T);
|
2001-05-03 17:34:54 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Functions must be inserted in the global symbol table */
|
2001-05-03 17:34:54 +00:00
|
|
|
SymTable* Tab = IsFunc? SymTab0 : SymTab;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Do we have an entry with this name already? */
|
|
|
|
SymEntry* Entry = FindSymInTable (Tab, Name, HashStr (Name));
|
|
|
|
if (Entry) {
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
Type* EType;
|
|
|
|
|
|
|
|
/* We have a symbol with this name already */
|
|
|
|
if (Entry->Flags & SC_TYPE) {
|
|
|
|
Error ("Multiple definition for `%s'", Name);
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the type string of the existing symbol */
|
|
|
|
EType = Entry->Type;
|
|
|
|
|
|
|
|
/* If we are handling arrays, the old entry or the new entry may be an
|
2014-06-30 09:10:35 +00:00
|
|
|
** incomplete declaration. Accept this, and if the exsting entry is
|
|
|
|
** incomplete, complete it.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (IsTypeArray (T) && IsTypeArray (EType)) {
|
|
|
|
|
|
|
|
/* Get the array sizes */
|
|
|
|
long Size = GetElementCount (T);
|
|
|
|
long ESize = GetElementCount (EType);
|
|
|
|
|
|
|
|
if ((Size != UNSPECIFIED && ESize != UNSPECIFIED && Size != ESize) ||
|
|
|
|
TypeCmp (T + 1, EType + 1) < TC_EQUAL) {
|
|
|
|
/* Types not identical: Conflicting types */
|
|
|
|
Error ("Conflicting types for `%s'", Name);
|
|
|
|
return Entry;
|
|
|
|
} else {
|
|
|
|
/* Check if we have a size in the existing definition */
|
|
|
|
if (ESize == UNSPECIFIED) {
|
|
|
|
/* Existing, size not given, use size from new def */
|
|
|
|
SetElementCount (EType, Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* New type must be identical */
|
|
|
|
if (TypeCmp (EType, T) < TC_EQUAL) {
|
|
|
|
Error ("Conflicting types for `%s'", Name);
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In case of a function, use the new type descriptor, since it
|
2014-06-30 09:10:35 +00:00
|
|
|
** 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.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (IsFunc) {
|
|
|
|
/* Get the function descriptor from the new type */
|
|
|
|
FuncDesc* F = GetFuncDesc (T);
|
|
|
|
/* Use this new function descriptor if it doesn't contain
|
2014-06-30 09:10:35 +00:00
|
|
|
** an empty parameter list.
|
|
|
|
*/
|
2008-08-03 20:55:36 +00:00
|
|
|
if ((F->Flags & FD_EMPTY) == 0) {
|
|
|
|
Entry->V.F.Func = F;
|
|
|
|
SetFuncDesc (EType, F);
|
|
|
|
}
|
2013-05-09 11:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2015-08-10 17:39:17 +00:00
|
|
|
/* If a static declaration follows a non-static declaration, then
|
|
|
|
** warn about the conflict. (It will compile a public declaration.)
|
|
|
|
*/
|
|
|
|
if ((Flags & SC_EXTERN) == 0 && (Entry->Flags & SC_EXTERN) != 0) {
|
|
|
|
Warning ("static declaration follows non-static declaration of `%s'.", Name);
|
|
|
|
}
|
|
|
|
|
2015-08-09 10:27:05 +00:00
|
|
|
/* An extern declaration must not change the current linkage. */
|
2017-03-09 18:14:31 +00:00
|
|
|
if (IsFunc || (Flags & (SC_EXTERN | SC_STORAGE)) == SC_EXTERN) {
|
2015-08-09 10:27:05 +00:00
|
|
|
Flags &= ~SC_EXTERN;
|
|
|
|
}
|
|
|
|
|
2015-08-10 17:39:17 +00:00
|
|
|
/* If a public declaration follows a static declaration, then
|
|
|
|
** warn about the conflict. (It will compile a public declaration.)
|
|
|
|
*/
|
|
|
|
if ((Flags & SC_EXTERN) != 0 && (Entry->Flags & SC_EXTERN) == 0) {
|
|
|
|
Warning ("public declaration follows static declaration of `%s'.", Name);
|
|
|
|
}
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Add the new flags */
|
|
|
|
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, 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
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
/* If this is a function, set the function descriptor and clear
|
2014-06-30 09:10:35 +00:00
|
|
|
** additional fields.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
if (IsFunc) {
|
|
|
|
Entry->V.F.Func = GetFuncDesc (Entry->Type);
|
|
|
|
Entry->V.F.Seg = 0;
|
|
|
|
}
|
2001-05-03 17:34:54 +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);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
int SymIsLocal (SymEntry* Sym)
|
|
|
|
/* Return true if the symbol is defined in the highest lexical level */
|
|
|
|
{
|
|
|
|
return (Sym->Owner == SymTab || Sym->Owner == TagTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|
2013-05-09 11:56:54 +00:00
|
|
|
Error ("Undefined 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;
|
|
|
|
if (Flags & SC_EXTERN) {
|
|
|
|
/* 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.
|
|
|
|
*/
|
2011-08-31 20:48:40 +00:00
|
|
|
if (LexicalLevel == LEX_LEVEL_GLOBAL) {
|
|
|
|
Head = "\t.dbg\t\tsym";
|
|
|
|
} else {
|
|
|
|
Head = "\t.dbg\tsym";
|
|
|
|
}
|
|
|
|
Sym = SymTab->SymHead;
|
|
|
|
while (Sym) {
|
|
|
|
if ((Sym->Flags & (SC_CONST|SC_TYPE)) == 0) {
|
|
|
|
if (Sym->Flags & SC_AUTO) {
|
|
|
|
AddTextLine ("%s, \"%s\", \"00\", auto, %d",
|
|
|
|
Head, Sym->Name, Sym->V.Offs);
|
|
|
|
} else if (Sym->Flags & SC_REGISTER) {
|
|
|
|
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,
|
|
|
|
(Sym->Flags & SC_EXTERN)? "extern" : "static",
|
|
|
|
Sym->AsmName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Sym = Sym->NextSym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|