/* */
/* */
/* (C) 1998-2003 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* Römerstraße 52 */
+/* D-70794 Filderstadt */
+/* EMail: uz@cc65.org */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* common */
#include "check.h"
#include "hashstr.h"
+#include "hashtab.h"
#include "xmalloc.h"
/* ca65 */
+/*****************************************************************************/
+/* Forwards */
+/*****************************************************************************/
+
+
+
+static unsigned HT_GenHash (const void* Key);
+/* Generate the hash over a key. */
+
+static const void* HT_GetKey (void* Entry);
+/* Given a pointer to the user entry data, return a pointer to the key */
+
+static HashNode* HT_GetHashNode (void* Entry);
+/* Given a pointer to the user entry data, return a pointer to the hash node */
+
+static int HT_Compare (const void* Key1, const void* Key2);
+/* Compare two keys. The function must return a value less than zero if
+ * Key1 is smaller than Key2, zero if both are equal, and a value greater
+ * than zero if Key1 is greater then Key2.
+ */
+
+
+
/*****************************************************************************/
/* Data */
/*****************************************************************************/
/* Struct that describes an identifer (macro param, local list) */
-typedef struct IdDesc_ IdDesc;
-struct IdDesc_ {
- IdDesc* Next; /* Linked list */
+typedef struct IdDesc IdDesc;
+struct IdDesc {
+ IdDesc* Next; /* Linked list */
char Id [1]; /* Identifier, dynamically allocated */
};
/* Struct that describes a macro definition */
-typedef struct Macro_ Macro;
-struct Macro_ {
- Macro* Next; /* Next macro with same hash */
+typedef struct Macro Macro;
+struct Macro {
+ HashNode Node; /* Hash list node */
Macro* List; /* List of all macros */
unsigned LocalCount; /* Count of local symbols */
IdDesc* Locals; /* List of local symbols */
char Name [1]; /* Macro name, dynamically allocated */
};
+/* Hash table functions */
+static const HashFunctions HashFunc = {
+ HT_GenHash,
+ HT_GetKey,
+ HT_GetHashNode,
+ HT_Compare
+};
+
/* Macro hash table */
-#define HASHTAB_SIZE 117
-static Macro* MacroTab [HASHTAB_SIZE];
+static HashTable MacroTab = STATIC_HASHTABLE_INITIALIZER (117, &HashFunc);
/* Global macro data */
static Macro* MacroRoot = 0; /* List of all macros */
/* Structs that holds data for a macro expansion */
-typedef struct MacExp_ MacExp;
-struct MacExp_ {
- MacExp* Next; /* Pointer to next expansion */
- Macro* M; /* Which macro do we expand? */
- unsigned IfSP; /* .IF stack pointer at start of expansion */
+typedef struct MacExp MacExp;
+struct MacExp {
+ MacExp* Next; /* Pointer to next expansion */
+ Macro* M; /* Which macro do we expand? */
+ unsigned IfSP; /* .IF stack pointer at start of expansion */
TokNode* Exp; /* Pointer to current token */
- TokNode* Final; /* Pointer to final token */
+ TokNode* Final; /* Pointer to final token */
unsigned LocalStart; /* Start of counter for local symbol names */
- unsigned ParamCount; /* Number of actual parameters */
+ unsigned ParamCount; /* Number of actual parameters */
TokNode** Params; /* List of actual parameters */
- TokNode* ParamExp; /* Node for expanding parameters */
+ TokNode* ParamExp; /* Node for expanding parameters */
};
/* Number of active macro expansions */
+/*****************************************************************************/
+/* Hash table functions */
+/*****************************************************************************/
+
+
+
+static unsigned HT_GenHash (const void* Key)
+/* Generate the hash over a key. */
+{
+ return HashStr (Key);
+}
+
+
+
+static const void* HT_GetKey (void* Entry)
+/* Given a pointer to the user entry data, return a pointer to the index */
+{
+ return ((Macro*) Entry)->Name;
+}
+
+
+
+static HashNode* HT_GetHashNode (void* Entry)
+/* Given a pointer to the user entry data, return a pointer to the hash node */
+{
+ return &((Macro*) Entry)->Node;
+}
+
+
+
+static int HT_Compare (const void* Key1, const void* Key2)
+/* Compare two keys. The function must return a value less than zero if
+ * Key1 is smaller than Key2, zero if both are equal, and a value greater
+ * than zero if Key1 is greater then Key2.
+ */
+{
+ return strcmp (Key1, Key2);
+}
+
+
+
/*****************************************************************************/
/* Code */
/*****************************************************************************/
-static Macro* NewMacro (const char* Name, unsigned HashVal, unsigned char Style)
+static Macro* NewMacro (const char* Name, unsigned char Style)
/* Generate a new macro entry, initialize and return it */
{
/* Allocate memory */
Macro* M = xmalloc (sizeof (Macro) + Len);
/* Initialize the macro struct */
+ InitHashNode (&M->Node, M);
M->LocalCount = 0;
M->Locals = 0;
M->ParamCount = 0;
M->TokRoot = 0;
M->TokLast = 0;
M->Style = Style;
- memcpy (M->Name, Name, Len);
- M->Name [Len] = '\0';
+ memcpy (M->Name, Name, Len+1);
/* Insert the macro into the global macro list */
M->List = MacroRoot;
MacroRoot = M;
/* Insert the macro into the hash table */
- M->Next = MacroTab [HashVal];
- MacroTab [HashVal] = M;
+ HT_Insert (&MacroTab, &M->Node);
/* Return the new macro struct */
return M;
if (Tok != TOK_EOF) {
SkipUntilSep ();
} else {
- Error (ERR_ENDMACRO_EXPECTED);
+ Error ("`.ENDMACRO' expected");
}
} else {
/* Skip until end of line */
-static Macro* MacFind (const char* Name, unsigned HashVal)
-/* Search for a macro in the hash table */
-{
- /* Search for the identifier */
- Macro* M = MacroTab [HashVal];
- while (M) {
- if (strcmp (Name, M->Name) == 0) {
- return M;
- }
- M = M->Next;
- }
- return 0;
-}
-
-
-
void MacDef (unsigned Style)
/* Parse a macro definition */
{
Macro* M;
TokNode* T;
- unsigned HashVal;
int HaveParams;
/* We expect a macro name here */
if (Tok != TOK_IDENT) {
- Error (ERR_IDENT_EXPECTED);
+ Error ("Identifier expected");
MacSkipDef (Style);
return;
}
- /* Generate the hash value */
- HashVal = HashStr (SVal) % HASHTAB_SIZE;
-
/* Did we already define that macro? */
- if (MacFind (SVal, HashVal) != 0) {
+ if (HT_Find (&MacroTab, SVal) != 0) {
/* Macro is already defined */
- Error (ERR_SYM_ALREADY_DEFINED, SVal);
+ Error ("A macro named `%s' is already defined", SVal);
/* Skip tokens until we reach the final .endmacro */
MacSkipDef (Style);
return;
}
/* Define the macro */
- M = NewMacro (SVal, HashVal, Style);
+ M = NewMacro (SVal, Style);
/* Switch to raw token mode and skip the macro name */
EnterRawTokenMode ();
IdDesc* List = M->Params;
while (1) {
if (strcmp (List->Id, SVal) == 0) {
- Error (ERR_SYM_ALREADY_DEFINED, SVal);
- }
+ Error ("Duplicate symbol `%s'", SVal);
+ }
if (List->Next == 0) {
break;
} else {
}
/* May not have end of file in a macro definition */
if (Tok == TOK_EOF) {
- Error (ERR_ENDMACRO_EXPECTED);
+ Error ("`.ENDMACRO' expected");
goto Done;
}
} else {
/* Need an identifer */
if (Tok != TOK_IDENT) {
- Error (ERR_IDENT_EXPECTED);
+ Error ("Identifier expected");
SkipUntilSep ();
break;
}
/* Check for maximum parameter count */
if (E->ParamCount >= M->ParamCount) {
- Error (ERR_TOO_MANY_PARAMS);
- SkipUntilSep ();
+ Error ("Too many macro parameters");
+ SkipUntilSep ();
break;
}
/* Check for end of file */
if (Tok == TOK_EOF) {
- Error (ERR_UNEXPECTED_EOF);
+ Error ("Unexpected end of file");
return;
}
/* Check if there is really a parameter */
if (TokIsSep (Tok) || Tok == TOK_COMMA) {
- Error (ERR_MACRO_PARAM_EXPECTED);
+ Error ("Macro parameter expected");
SkipUntilSep ();
return;
}
if (Tok == TOK_COMMA) {
NextTok ();
} else {
- Error (ERR_COMMA_EXPECTED);
+ Error ("`,' expected");
}
}
}
/* Start expanding the macro in SVal */
{
/* Search for the macro */
- Macro* M = MacFind (SVal, HashStr (SVal) % HASHTAB_SIZE);
+ Macro* M = HT_FindEntry (&MacroTab, SVal);
CHECK (M != 0);
/* Call the apropriate subroutine */
int IsMacro (const char* Name)
/* Return true if the given name is the name of a macro */
{
- return MacFind (Name, HashStr (Name) % HASHTAB_SIZE) != 0;
+ return (HT_Find (&MacroTab, Name) != 0);
}
int IsDefine (const char* Name)
/* Return true if the given name is the name of a define style macro */
{
- Macro* M = MacFind (Name, HashStr (Name) % HASHTAB_SIZE);
+ Macro* M = HT_FindEntry (&MacroTab, Name);
return (M != 0 && M->Style == MAC_STYLE_DEFINE);
}