Renamed some functions for better readbility.

Add a binary search for preprocessor tokens (instead of the old linear one)
to speed up the compiler.


git-svn-id: svn://svn.cc65.org/cc65/trunk@857 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2001-09-06 10:17:52 +00:00
parent bf47ac26c9
commit e1b0251f04
6 changed files with 158 additions and 143 deletions

View File

@@ -271,24 +271,24 @@ void Compile (const char* FileName)
} }
/* Add macros that are always defined */ /* Add macros that are always defined */
AddNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH); DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
/* Strict ANSI macro */ /* Strict ANSI macro */
if (ANSI) { if (ANSI) {
AddNumericMacro ("__STRICT_ANSI__", 1); DefineNumericMacro ("__STRICT_ANSI__", 1);
} }
/* Optimization macros */ /* Optimization macros */
if (Optimize) { if (Optimize) {
AddNumericMacro ("__OPT__", 1); DefineNumericMacro ("__OPT__", 1);
if (FavourSize == 0) { if (FavourSize == 0) {
AddNumericMacro ("__OPT_i__", 1); DefineNumericMacro ("__OPT_i__", 1);
} }
if (EnableRegVars) { if (EnableRegVars) {
AddNumericMacro ("__OPT_r__", 1); DefineNumericMacro ("__OPT_r__", 1);
} }
if (InlineStdFuncs) { if (InlineStdFuncs) {
AddNumericMacro ("__OPT_s__", 1); DefineNumericMacro ("__OPT_s__", 1);
} }
} }

View File

@@ -114,8 +114,8 @@ void FreeMacro (Macro* M)
void AddNumericMacro (const char* Name, long Val) void DefineNumericMacro (const char* Name, long Val)
/* Add a macro for a numeric constant */ /* Define a macro for a numeric constant */
{ {
char Buf[64]; char Buf[64];
@@ -123,13 +123,13 @@ void AddNumericMacro (const char* Name, long Val)
sprintf (Buf, "%ld", Val); sprintf (Buf, "%ld", Val);
/* Handle as text macro */ /* Handle as text macro */
AddTextMacro (Name, Buf); DefineTextMacro (Name, Buf);
} }
void AddTextMacro (const char* Name, const char* Val) void DefineTextMacro (const char* Name, const char* Val)
/* Add a macro for a textual constant */ /* Define a macro for a textual constant */
{ {
/* Create a new macro */ /* Create a new macro */
Macro* M = NewMacro (Name); Macro* M = NewMacro (Name);

View File

@@ -44,8 +44,8 @@
typedef struct Macro_ Macro; typedef struct Macro Macro;
struct Macro_ { struct Macro {
Macro* Next; /* Next macro with same hash value */ Macro* Next; /* Next macro with same hash value */
int ArgCount; /* Number of parameters, -1 = no parens */ int ArgCount; /* Number of parameters, -1 = no parens */
unsigned MaxArgs; /* Size of formal argument list */ unsigned MaxArgs; /* Size of formal argument list */
@@ -73,11 +73,11 @@ void FreeMacro (Macro* M);
* table, use UndefineMacro for that. * table, use UndefineMacro for that.
*/ */
void AddNumericMacro (const char* Name, long Val); void DefineNumericMacro (const char* Name, long Val);
/* Add a macro for a numeric constant */ /* Define a macro for a numeric constant */
void AddTextMacro (const char* Name, const char* Val); void DefineTextMacro (const char* Name, const char* Val);
/* Add a macro for a textual constant */ /* Define a macro for a textual constant */
void InsertMacro (Macro* M); void InsertMacro (Macro* M);
/* Insert the given macro into the macro table. This call will also allocate /* Insert the given macro into the macro table. This call will also allocate
@@ -122,4 +122,3 @@ void PrintMacroStats (FILE* F);

View File

@@ -124,8 +124,8 @@ static void Usage (void)
static void cbmsys (const char* sys) static void cbmsys (const char* sys)
/* Define a CBM system */ /* Define a CBM system */
{ {
AddNumericMacro ("__CBM__", 1); DefineNumericMacro ("__CBM__", 1);
AddNumericMacro (sys, 1); DefineNumericMacro (sys, 1);
} }
@@ -139,7 +139,7 @@ static void SetSys (const char* Sys)
break; break;
case TGT_ATARI: case TGT_ATARI:
AddNumericMacro ("__ATARI__", 1); DefineNumericMacro ("__ATARI__", 1);
break; break;
case TGT_C64: case TGT_C64:
@@ -167,16 +167,16 @@ static void SetSys (const char* Sys)
break; break;
case TGT_BBC: case TGT_BBC:
AddNumericMacro ("__BBC__", 1); DefineNumericMacro ("__BBC__", 1);
break; break;
case TGT_APPLE2: case TGT_APPLE2:
AddNumericMacro ("__APPLE2__", 1); DefineNumericMacro ("__APPLE2__", 1);
break; break;
case TGT_GEOS: case TGT_GEOS:
/* Do not handle as a CBM system */ /* Do not handle as a CBM system */
AddNumericMacro ("__GEOS__", 1); DefineNumericMacro ("__GEOS__", 1);
break; break;
default: default:
@@ -237,7 +237,7 @@ static void DefineSym (const char* Def)
InvDef (Def); InvDef (Def);
} }
/* No value given. Define the macro with the value 1 */ /* No value given. Define the macro with the value 1 */
AddNumericMacro (Def, 1); DefineNumericMacro (Def, 1);
} else { } else {
/* We have a value, P points to the '=' character. Since the argument /* We have a value, P points to the '=' character. Since the argument
* is const, create a copy and replace the '=' in the copy by a zero * is const, create a copy and replace the '=' in the copy by a zero
@@ -251,7 +251,7 @@ static void DefineSym (const char* Def)
*Q++ = '\0'; *Q++ = '\0';
/* Define this as a macro */ /* Define this as a macro */
AddTextMacro (S, Q); DefineTextMacro (S, Q);
/* Release the allocated memory */ /* Release the allocated memory */
xfree (S); xfree (S);

View File

@@ -39,7 +39,7 @@ static int Pass1 (const char* From, char* To);
/*****************************************************************************/ /*****************************************************************************/
/* data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
@@ -63,7 +63,74 @@ static int ExpandMacros = 1;
/*****************************************************************************/ /*****************************************************************************/
/* code */ /* Low level preprocessor token handling */
/*****************************************************************************/
/* Types of preprocessor tokens */
typedef enum {
PP_DEFINE,
PP_ELSE,
PP_ENDIF,
PP_ERROR,
PP_IF,
PP_IFDEF,
PP_IFNDEF,
PP_INCLUDE,
PP_LINE,
PP_PRAGMA,
PP_UNDEF,
PP_ILLEGAL
} pptoken_t;
/* Preprocessor keyword to token mapping table */
static const struct PPToken {
const char* Key; /* Keyword */
pptoken_t Tok; /* Token */
} PPTokens[] = {
{ "define", PP_DEFINE },
{ "else", PP_ELSE },
{ "endif", PP_ENDIF },
{ "error", PP_ERROR },
{ "if", PP_IF },
{ "ifdef", PP_IFDEF },
{ "ifndef", PP_IFNDEF },
{ "include", PP_INCLUDE },
{ "line", PP_LINE },
{ "pragma", PP_PRAGMA },
{ "undef", PP_UNDEF },
};
/* Number of preprocessor tokens */
#define PPTOKEN_COUNT (sizeof(PPTokens) / sizeof(PPTokens[0]))
static int CmpToken (const void* Key, const void* Elem)
/* Compare function for bsearch */
{
return strcmp ((const char*) Key, ((const struct PPToken*) Elem)->Key);
}
static pptoken_t FindPPToken (const char* Ident)
/* Find a preprocessor token and return ut. Return PP_ILLEGAL if the identifier
* is not a valid preprocessor token.
*/
{
struct PPToken* P;
P = bsearch (Ident, PPTokens, PPTOKEN_COUNT, sizeof (PPTokens[0]), CmpToken);
return P? P->Tok : PP_ILLEGAL;
}
/*****************************************************************************/
/* Code */
/*****************************************************************************/ /*****************************************************************************/
@@ -172,7 +239,9 @@ static char* CopyQuotedString (char* Target)
static int MacName (char* Ident) static int MacName (char* Ident)
/* Get macro symbol name. If error, print message and clear line. */ /* Get macro symbol name. If we have an error, print a diagnostic message
* and clear line.
*/
{ {
if (IsSym (Ident) == 0) { if (IsSym (Ident) == 0) {
PPError ("Identifier expected"); PPError ("Identifier expected");
@@ -186,7 +255,7 @@ static int MacName (char* Ident)
static void ExpandMacroArgs (Macro* M) static void ExpandMacroArgs (Macro* M)
/* Preprocessor pass 2. Perform macro substitution. */ /* Expand the arguments of a macro */
{ {
ident Ident; ident Ident;
const char* Replacement; const char* Replacement;
@@ -353,8 +422,8 @@ static void ExpandMacro (Macro* M)
static void addmac (void) static void DefineMacro (void)
/* Add a macro to the macro table. */ /* Handle a macro definition. */
{ {
char* saveptr; char* saveptr;
ident Ident; ident Ident;
@@ -766,51 +835,6 @@ static void doerror (void)
/* C preprocessor. */
/* stuff used to bum the keyword dispatching stuff */
enum {
PP_DEFINE,
PP_ELSE,
PP_ENDIF,
PP_ERROR,
PP_IF,
PP_IFDEF,
PP_IFNDEF,
PP_INCLUDE,
PP_LINE,
PP_PRAGMA,
PP_UNDEF,
PP_ILLEGAL
};
static const struct tok_elt pre_toks[] = {
{ "define", PP_DEFINE },
{ "else", PP_ELSE },
{ "endif", PP_ENDIF },
{ "error", PP_ERROR },
{ "if", PP_IF },
{ "ifdef", PP_IFDEF },
{ "ifndef", PP_IFNDEF },
{ "include", PP_INCLUDE },
{ "line", PP_LINE },
{ "pragma", PP_PRAGMA },
{ "undef", PP_UNDEF },
{ 0, PP_ILLEGAL }
};
static int searchtok (const char *sym, const struct tok_elt *toks)
/* Search a token in a table */
{
while (toks->toknam && strcmp (toks->toknam, sym))
++toks;
return (toks->toknbr);
}
void Preprocess (void) void Preprocess (void)
/* Preprocess a line */ /* Preprocess a line */
{ {
@@ -836,11 +860,11 @@ void Preprocess (void)
PPError ("Preprocessor directive expected"); PPError ("Preprocessor directive expected");
ClearLine (); ClearLine ();
} else { } else {
switch (searchtok (Directive, pre_toks)) { switch (FindPPToken (Directive)) {
case PP_DEFINE: case PP_DEFINE:
if (!Skip) { if (!Skip) {
addmac (); DefineMacro ();
} }
break; break;

View File

@@ -17,12 +17,6 @@
/* Token table entry */
struct tok_elt {
char *toknam;
int toknbr;
};
/* Set when the pp calls expr() recursively */ /* Set when the pp calls expr() recursively */
extern unsigned char Preprocessing; extern unsigned char Preprocessing;
@@ -44,5 +38,3 @@ void Preprocess (void);