2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* C pre-processor functions */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2001-01-05 19:24:47 +00:00
|
|
|
/* common */
|
|
|
|
#include "chartype.h"
|
2001-03-10 10:21:03 +00:00
|
|
|
#include "print.h"
|
2001-01-05 19:24:47 +00:00
|
|
|
#include "xmalloc.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
|
2001-01-05 19:24:47 +00:00
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "codegen.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "ident.h"
|
2000-06-14 08:18:19 +00:00
|
|
|
#include "incpath.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
#include "input.h"
|
2001-07-14 15:56:17 +00:00
|
|
|
#include "lineinfo.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "macrotab.h"
|
|
|
|
#include "scanner.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "preproc.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Forwards */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int Pass1 (const char* From, char* To);
|
|
|
|
/* Preprocessor pass 1. Remove whitespace and comments. */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-06-14 21:01:37 +00:00
|
|
|
/* data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Set when the pp calls expr() recursively */
|
|
|
|
unsigned char Preprocessing = 0;
|
|
|
|
|
|
|
|
/* Management data for #if */
|
2000-06-21 21:02:44 +00:00
|
|
|
#define N_IFDEF 16
|
2000-05-28 13:40:48 +00:00
|
|
|
static int i_ifdef = -1;
|
|
|
|
static char s_ifdef[N_IFDEF];
|
|
|
|
|
|
|
|
/* Buffer for macro expansion */
|
|
|
|
static char mlinebuf [LINESIZE];
|
|
|
|
static char* mline = mlinebuf;
|
|
|
|
static char* mptr;
|
|
|
|
|
|
|
|
/* Flag: Expand macros in this line */
|
|
|
|
static int ExpandMacros = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int keepch (char c)
|
|
|
|
/* Put character c into translation buffer. */
|
|
|
|
{
|
|
|
|
return (*mptr++ = c);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void keepstr (const char* S)
|
|
|
|
/* Put string str into translation buffer. */
|
|
|
|
{
|
|
|
|
while (*S) {
|
|
|
|
keepch (*S++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static void Comment (void)
|
|
|
|
/* Remove a C comment from line. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Remember the current line number, so we can output better error
|
|
|
|
* messages if the comment is not terminated in the current file.
|
|
|
|
*/
|
2000-06-14 21:01:37 +00:00
|
|
|
unsigned StartingLine = GetCurrentLine();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Skip the start of comment chars */
|
|
|
|
NextChar ();
|
|
|
|
NextChar ();
|
|
|
|
|
|
|
|
/* Skip the comment */
|
|
|
|
while (CurC != '*' || NextC != '/') {
|
|
|
|
if (CurC == '\0') {
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("End-of-file reached in comment starting at line %u",
|
|
|
|
StartingLine);
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '/' && NextC == '*') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPWarning ("`/*' found inside a comment");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
/* Skip the end of comment chars */
|
|
|
|
NextChar ();
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static void SkipBlank (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Skip blanks and tabs in the input stream. */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
while (IsBlank (CurC)) {
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static char* CopyQuotedString (char* Target)
|
|
|
|
/* Copy a single or double quoted string from the input to Target. Return the
|
2000-05-28 13:40:48 +00:00
|
|
|
* new target pointer. Target will not be terminated after the copy.
|
|
|
|
*/
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Remember the quote character, copy it to the target buffer and skip it */
|
|
|
|
char Quote = CurC;
|
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Copy the characters inside the string */
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0' && CurC != Quote) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Keep an escaped char */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '\\') {
|
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
/* Copy the character */
|
2000-06-21 21:02:44 +00:00
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we had a terminating quote, copy it */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC != '\0') {
|
|
|
|
*Target++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the new target pointer */
|
|
|
|
return Target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Macro stuff */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int MacName (char* Ident)
|
2000-06-15 19:03:01 +00:00
|
|
|
/* Get macro symbol name. If error, print message and clear line. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsSym (Ident) == 0) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Identifier expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ExpandMacroArgs (Macro* M)
|
|
|
|
/* Preprocessor pass 2. Perform macro substitution. */
|
|
|
|
{
|
|
|
|
ident Ident;
|
|
|
|
const char* Replacement;
|
2000-06-21 21:02:44 +00:00
|
|
|
const char* SavePtr;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Save the current line pointer and setup the new ones */
|
|
|
|
SavePtr = lptr;
|
2000-06-21 21:02:44 +00:00
|
|
|
InitLine (M->Replacement);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Copy the macro replacement checking for parameters to replace */
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* If the next token is an identifier, check for a macro arg */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsIdent (CurC)) {
|
|
|
|
SymName (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
Replacement = FindMacroArg (M, Ident);
|
|
|
|
if (Replacement) {
|
|
|
|
/* Macro arg, keep the replacement */
|
|
|
|
keepstr (Replacement);
|
|
|
|
} else {
|
|
|
|
/* No macro argument, keep the original identifier */
|
|
|
|
keepstr (Ident);
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (CurC == '#' && IsIdent (NextC)) {
|
|
|
|
NextChar ();
|
|
|
|
SymName (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
Replacement = FindMacroArg (M, Ident);
|
|
|
|
if (Replacement) {
|
|
|
|
keepch ('\"');
|
|
|
|
keepstr (Replacement);
|
|
|
|
keepch ('\"');
|
|
|
|
} else {
|
|
|
|
keepch ('#');
|
|
|
|
keepstr (Ident);
|
|
|
|
}
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
mptr = CopyQuotedString (mptr);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
*mptr++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the line pointer */
|
2000-06-21 21:02:44 +00:00
|
|
|
InitLine (SavePtr);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int MacroCall (Macro* M)
|
|
|
|
/* Process a function like macro */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
unsigned ArgCount; /* Macro argument count */
|
2000-05-28 13:40:48 +00:00
|
|
|
unsigned ParCount; /* Number of open parenthesis */
|
|
|
|
char Buf[LINESIZE]; /* Argument buffer */
|
|
|
|
const char* ArgStart;
|
|
|
|
char* B;
|
|
|
|
|
|
|
|
/* Expect an argument list */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC != '(') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Illegal macro call");
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Eat the left paren */
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the actual macro arguments and store pointers to these arguments
|
|
|
|
* into the array of actual arguments in the macro definition.
|
|
|
|
*/
|
|
|
|
ArgCount = 0;
|
|
|
|
ParCount = 0;
|
|
|
|
ArgStart = Buf;
|
|
|
|
B = Buf;
|
|
|
|
while (1) {
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '(') {
|
|
|
|
/* Nested parenthesis */
|
|
|
|
*B++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
++ParCount;
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
B = CopyQuotedString (B);
|
|
|
|
} else if (CurC == ',' || CurC == ')') {
|
2000-05-28 13:40:48 +00:00
|
|
|
if (ParCount == 0) {
|
2000-06-21 21:02:44 +00:00
|
|
|
/* End of actual argument */
|
2000-05-28 13:40:48 +00:00
|
|
|
*B++ = '\0';
|
2000-06-21 21:02:44 +00:00
|
|
|
while (IsBlank(*ArgStart)) {
|
|
|
|
++ArgStart;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
if (ArgCount < M->ArgCount) {
|
|
|
|
M->ActualArgs[ArgCount++] = ArgStart;
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (CurC != ')' || *ArgStart != '\0' || M->ArgCount > 0) {
|
|
|
|
/* Be sure not to count the single empty argument for a
|
|
|
|
* macro that does not have arguments.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
++ArgCount;
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for end of macro param list */
|
|
|
|
if (CurC == ')') {
|
|
|
|
NextChar ();
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Start the next param */
|
2000-05-28 13:40:48 +00:00
|
|
|
ArgStart = B;
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Comma or right paren inside nested parenthesis */
|
|
|
|
if (CurC == ')') {
|
2000-05-28 13:40:48 +00:00
|
|
|
--ParCount;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
*B++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
} else if (IsBlank (CurC)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Squeeze runs of blanks */
|
|
|
|
*B++ = ' ';
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
} else if (CurC == '\0') {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* End of line inside macro argument list - read next line */
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Just copy the character */
|
2000-06-21 21:02:44 +00:00
|
|
|
*B++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Compare formal argument count with actual */
|
|
|
|
if (M->ArgCount != ArgCount) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Macro argument count mismatch");
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Be sure to make enough empty arguments available */
|
|
|
|
while (ArgCount < M->ArgCount) {
|
|
|
|
M->ActualArgs [ArgCount++] = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Preprocess the line, replacing macro parameters */
|
|
|
|
ExpandMacroArgs (M);
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ExpandMacro (Macro* M)
|
|
|
|
/* Expand a macro */
|
|
|
|
{
|
|
|
|
/* Check if this is a function like macro */
|
|
|
|
if (M->ArgCount >= 0) {
|
|
|
|
/* Function like macro */
|
|
|
|
if (MacroCall (M) == 0) {
|
2000-06-15 19:03:01 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Just copy the replacement text */
|
|
|
|
keepstr (M->Replacement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void addmac (void)
|
|
|
|
/* Add a macro to the macro table. */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
char* saveptr;
|
|
|
|
ident Ident;
|
|
|
|
char Buf[LINESIZE];
|
|
|
|
Macro* M;
|
2000-10-10 21:20:34 +00:00
|
|
|
Macro* Existing;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the macro name */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (!MacName (Ident)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-10-10 21:20:34 +00:00
|
|
|
/* Get an existing macro definition with this name */
|
|
|
|
Existing = FindMacro (Ident);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Create a new macro definition */
|
|
|
|
M = NewMacro (Ident);
|
|
|
|
|
|
|
|
/* Check if this is a function like macro */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '(') {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip the left paren */
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Set the marker that this is a function like macro */
|
|
|
|
M->ArgCount = 0;
|
|
|
|
|
|
|
|
/* Read the formal parameter list */
|
|
|
|
while (1) {
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC == ')')
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
if (MacName (Ident) == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
AddMacroArg (M, Ident);
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC != ',')
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
/* Check for a right paren and eat it if we find one */
|
|
|
|
if (CurC != ')') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`)' expected");
|
2000-06-15 19:03:01 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert the macro into the macro table and allocate the ActualArgs array */
|
|
|
|
InsertMacro (M);
|
|
|
|
|
|
|
|
/* Remove whitespace and comments from the line, store the preprocessed
|
|
|
|
* line into Buf.
|
|
|
|
*/
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
saveptr = mptr;
|
|
|
|
Pass1 (lptr, Buf);
|
|
|
|
mptr = saveptr;
|
|
|
|
|
|
|
|
/* Create a copy of the replacement */
|
|
|
|
M->Replacement = xstrdup (Buf);
|
2000-10-10 21:20:34 +00:00
|
|
|
|
|
|
|
/* If we have an existing macro, check if the redefinition is identical.
|
|
|
|
* Print a diagnostic if not.
|
|
|
|
*/
|
|
|
|
if (Existing) {
|
|
|
|
if (MacroCmp (M, Existing) != 0) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Macro redefinition is not identical");
|
2000-10-10 21:20:34 +00:00
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int Pass1 (const char* From, char* To)
|
|
|
|
/* Preprocessor pass 1. Remove whitespace and comments. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
int done;
|
|
|
|
ident Ident;
|
|
|
|
int HaveParen;
|
|
|
|
|
|
|
|
/* Initialize reading from "From" */
|
|
|
|
InitLine (From);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Target is "To" */
|
|
|
|
mptr = To;
|
|
|
|
|
|
|
|
/* Loop removing ws and comments */
|
2000-05-28 13:40:48 +00:00
|
|
|
done = 1;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
|
|
|
if (IsBlank (CurC)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
keepch (' ');
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
} else if (IsIdent (CurC)) {
|
|
|
|
SymName (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Preprocessing && strcmp(Ident, "defined") == 0) {
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Handle the "defined" operator */
|
|
|
|
SkipBlank();
|
|
|
|
HaveParen = 0;
|
|
|
|
if (CurC == '(') {
|
|
|
|
HaveParen = 1;
|
|
|
|
NextChar ();
|
|
|
|
SkipBlank();
|
|
|
|
}
|
|
|
|
if (!IsIdent (CurC)) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Identifier expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
*mptr++ = '0';
|
|
|
|
} else {
|
|
|
|
SymName (Ident);
|
|
|
|
*mptr++ = IsMacro (Ident)? '1' : '0';
|
|
|
|
if (HaveParen) {
|
|
|
|
SkipBlank();
|
|
|
|
if (CurC != ')') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`)' expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
} else {
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
if (MaybeMacro (Ident[0])) {
|
|
|
|
done = 0;
|
|
|
|
}
|
|
|
|
keepstr (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
mptr = CopyQuotedString (mptr);
|
|
|
|
} else if (CurC == '/' && NextC == '*') {
|
2000-05-28 13:40:48 +00:00
|
|
|
keepch (' ');
|
2000-06-21 21:02:44 +00:00
|
|
|
Comment ();
|
|
|
|
} else if (ANSI == 0 && CurC == '/' && NextC == '/') {
|
2000-05-28 13:40:48 +00:00
|
|
|
keepch (' ');
|
|
|
|
/* Beware: Because line continuation chars are handled when reading
|
|
|
|
* lines, we may only skip til the end of the source line, which
|
|
|
|
* may not be the same as the end of the input line. The end of the
|
|
|
|
* source line is denoted by a lf (\n) character.
|
|
|
|
*/
|
|
|
|
do {
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
} while (CurC != '\n' && CurC != '\0');
|
|
|
|
if (CurC == '\n') {
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
*mptr++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
keepch ('\0');
|
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int Pass2 (const char* From, char* To)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Preprocessor pass 2. Perform macro substitution. */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
int no_chg;
|
|
|
|
ident Ident;
|
|
|
|
Macro* M;
|
|
|
|
|
|
|
|
/* Initialize reading from "From" */
|
|
|
|
InitLine (From);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Target is "To" */
|
|
|
|
mptr = To;
|
|
|
|
|
|
|
|
/* Loop substituting macros */
|
2000-05-28 13:40:48 +00:00
|
|
|
no_chg = 1;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* If we have an identifier, check if it's a macro */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsIdent (CurC)) {
|
|
|
|
SymName (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
M = FindMacro (Ident);
|
|
|
|
if (M) {
|
2000-06-21 21:02:44 +00:00
|
|
|
ExpandMacro (M);
|
|
|
|
no_chg = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
keepstr (Ident);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2001-01-05 19:26:25 +00:00
|
|
|
} else if (IsQuote (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
mptr = CopyQuotedString (mptr);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-21 21:02:44 +00:00
|
|
|
*mptr++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return no_chg;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void xlateline (void)
|
|
|
|
/* Translate one line. */
|
|
|
|
{
|
|
|
|
int cnt;
|
|
|
|
int Done;
|
|
|
|
|
|
|
|
Done = Pass1 (line, mline);
|
|
|
|
if (ExpandMacros == 0) {
|
|
|
|
Done = 1;
|
|
|
|
ExpandMacros = 1; /* Reset to default */
|
|
|
|
}
|
|
|
|
cnt = 5;
|
|
|
|
do {
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Swap mline and line */
|
|
|
|
char* p = line;
|
2000-05-28 13:40:48 +00:00
|
|
|
line = mline;
|
|
|
|
mline = p;
|
|
|
|
if (Done)
|
|
|
|
break;
|
|
|
|
Done = Pass2 (line, mline);
|
|
|
|
keepch ('\0');
|
|
|
|
} while (--cnt);
|
2000-06-21 21:02:44 +00:00
|
|
|
|
|
|
|
/* Reinitialize line parsing */
|
|
|
|
InitLine (line);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void doundef (void)
|
|
|
|
/* Process #undef directive */
|
|
|
|
{
|
|
|
|
ident Ident;
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (MacName (Ident)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
UndefineMacro (Ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int setmflag (int skip, int flag, int cond)
|
|
|
|
/* setmflag( skip, flag, cond ) */
|
|
|
|
{
|
|
|
|
if (skip) {
|
|
|
|
s_ifdef[++i_ifdef] = 3;
|
|
|
|
return (1);
|
|
|
|
} else {
|
|
|
|
s_ifdef[++i_ifdef] = 6;
|
|
|
|
return (flag ^ cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int doiff (int skip)
|
|
|
|
/* Process #if directive */
|
|
|
|
{
|
2001-07-17 21:24:19 +00:00
|
|
|
ExprDesc lval;
|
2000-05-28 13:40:48 +00:00
|
|
|
char* S;
|
|
|
|
|
|
|
|
/* We're about to abuse the compiler expression parser to evaluate the
|
|
|
|
* #if expression. Save the current tokens to come back here later.
|
2001-07-14 15:56:17 +00:00
|
|
|
* NOTE: Yes, this is a hack, but it saves a complete separate expression
|
|
|
|
* evaluation for the preprocessor.
|
2000-05-28 13:40:48 +00:00
|
|
|
*/
|
|
|
|
Token sv1 = CurTok;
|
|
|
|
Token sv2 = NextTok;
|
|
|
|
|
2001-07-14 15:56:17 +00:00
|
|
|
/* Make sure the line infos for the tokens won't get removed */
|
|
|
|
if (sv1.LI) {
|
|
|
|
UseLineInfo (sv1.LI);
|
|
|
|
}
|
|
|
|
if (sv2.LI) {
|
|
|
|
UseLineInfo (sv2.LI);
|
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Remove the #if from the line and add two semicolons as sentinels */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
S = line;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC != '\0') {
|
|
|
|
*S++ = CurC;
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
*S++ = ';';
|
|
|
|
*S++ = ';';
|
|
|
|
*S = '\0';
|
|
|
|
|
|
|
|
/* Start over parsing from line */
|
|
|
|
InitLine (line);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Switch into special preprocessing mode */
|
|
|
|
Preprocessing = 1;
|
|
|
|
|
|
|
|
/* Expand macros in this line */
|
|
|
|
xlateline ();
|
|
|
|
|
|
|
|
/* Prime the token pump (remove old tokens from the stream) */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Call the expression parser */
|
|
|
|
constexpr (&lval);
|
|
|
|
|
|
|
|
/* End preprocessing mode */
|
|
|
|
Preprocessing = 0;
|
|
|
|
|
|
|
|
/* Reset the old tokens */
|
|
|
|
CurTok = sv1;
|
|
|
|
NextTok = sv2;
|
|
|
|
|
|
|
|
/* Set the #if condition according to the expression result */
|
2001-07-17 21:53:17 +00:00
|
|
|
return (setmflag (skip, 1, lval.ConstVal != 0));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int doifdef (int skip, int flag)
|
|
|
|
/* Process #ifdef if flag == 1, or #ifndef if flag == 0. */
|
|
|
|
{
|
|
|
|
ident Ident;
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (MacName (Ident) == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return setmflag (skip, flag, IsMacro(Ident));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void doinclude (void)
|
|
|
|
/* Open an include file. */
|
|
|
|
{
|
2000-06-14 21:01:37 +00:00
|
|
|
char RTerm;
|
|
|
|
unsigned DirSpec;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-14 21:01:37 +00:00
|
|
|
|
|
|
|
/* Skip blanks */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-14 21:01:37 +00:00
|
|
|
/* Get the next char and check for a valid file name terminator. Setup
|
|
|
|
* the include directory spec (SYS/USR) by looking at the terminator.
|
|
|
|
*/
|
2000-06-21 21:02:44 +00:00
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
case '\"':
|
|
|
|
RTerm = '\"';
|
|
|
|
DirSpec = INC_USER;
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
case '<':
|
|
|
|
RTerm = '>';
|
|
|
|
DirSpec = INC_SYS;
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
default:
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`\"' or `<' expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
goto Done;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Copy the filename into mline. Since mline has the same size as the
|
|
|
|
* input line, we don't need to check for an overflow here.
|
|
|
|
*/
|
|
|
|
mptr = mline;
|
|
|
|
while (CurC != '\0' && CurC != RTerm) {
|
|
|
|
*mptr++ = CurC;
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
*mptr = '\0';
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Check if we got a terminator */
|
|
|
|
if (CurC != RTerm) {
|
|
|
|
/* No terminator found */
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Missing terminator or file name too long");
|
2000-06-21 21:02:44 +00:00
|
|
|
goto Done;
|
|
|
|
}
|
2000-06-14 21:01:37 +00:00
|
|
|
|
|
|
|
/* Open the include file */
|
2000-06-21 21:02:44 +00:00
|
|
|
OpenIncludeFile (mline, DirSpec);
|
2000-06-14 21:01:37 +00:00
|
|
|
|
|
|
|
Done:
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Clear the remaining line so the next input will come from the new
|
|
|
|
* file (if open)
|
2000-06-15 19:03:01 +00:00
|
|
|
*/
|
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void doerror (void)
|
|
|
|
/* Print an error */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
|
|
|
if (CurC == '\0') {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Invalid #error directive");
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("#error: %s", lptr);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear rest of line */
|
2000-06-15 19:03:01 +00:00
|
|
|
ClearLine ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* C preprocessor. */
|
|
|
|
|
|
|
|
/* stuff used to bum the keyword dispatching stuff */
|
|
|
|
enum {
|
2000-06-21 21:02:44 +00:00
|
|
|
PP_DEFINE,
|
|
|
|
PP_ELSE,
|
|
|
|
PP_ENDIF,
|
|
|
|
PP_ERROR,
|
|
|
|
PP_IF,
|
|
|
|
PP_IFDEF,
|
|
|
|
PP_IFNDEF,
|
|
|
|
PP_INCLUDE,
|
|
|
|
PP_LINE,
|
|
|
|
PP_PRAGMA,
|
|
|
|
PP_UNDEF,
|
2000-07-18 22:39:35 +00:00
|
|
|
PP_ILLEGAL
|
2000-05-28 13:40:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct tok_elt pre_toks[] = {
|
2000-06-21 21:02:44 +00:00
|
|
|
{ "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 }
|
2000-05-28 13:40:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int searchtok (const char *sym, const struct tok_elt *toks)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Search a token in a table */
|
|
|
|
{
|
|
|
|
while (toks->toknam && strcmp (toks->toknam, sym))
|
|
|
|
++toks;
|
|
|
|
return (toks->toknbr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
void Preprocess (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Preprocess a line */
|
|
|
|
{
|
|
|
|
int Skip;
|
2000-06-21 21:02:44 +00:00
|
|
|
ident Directive;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip white space at the beginning of the line */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for stuff to skip */
|
|
|
|
Skip = 0;
|
2000-06-21 21:02:44 +00:00
|
|
|
while (CurC == '\0' || CurC == '#' || Skip) {
|
|
|
|
|
|
|
|
/* Check for preprocessor lines lines */
|
|
|
|
if (CurC == '#') {
|
|
|
|
NextChar ();
|
|
|
|
SkipBlank ();
|
|
|
|
if (CurC == '\0') {
|
|
|
|
/* Ignore the empty preprocessor directive */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!IsSym (Directive)) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Preprocessor directive expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
|
|
|
} else {
|
|
|
|
switch (searchtok (Directive, pre_toks)) {
|
|
|
|
|
|
|
|
case PP_DEFINE:
|
|
|
|
if (!Skip) {
|
|
|
|
addmac ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_ELSE:
|
|
|
|
if (s_ifdef[i_ifdef] & 2) {
|
|
|
|
if (s_ifdef[i_ifdef] & 4) {
|
|
|
|
Skip = !Skip;
|
|
|
|
}
|
|
|
|
s_ifdef[i_ifdef] ^= 2;
|
|
|
|
} else {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Unexpected `#else'");
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_ENDIF:
|
|
|
|
if (i_ifdef >= 0) {
|
|
|
|
Skip = s_ifdef[i_ifdef--] & 1;
|
|
|
|
} else {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Unexpected `#endif'");
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_ERROR:
|
|
|
|
if (!Skip) {
|
|
|
|
doerror ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_IF:
|
|
|
|
Skip = doiff (Skip);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_IFDEF:
|
|
|
|
Skip = doifdef (Skip, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_IFNDEF:
|
|
|
|
Skip = doifdef (Skip, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_INCLUDE:
|
|
|
|
if (!Skip) {
|
|
|
|
doinclude ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_LINE:
|
|
|
|
/* Not allowed in strict ANSI mode */
|
|
|
|
if (ANSI) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Preprocessor directive expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_PRAGMA:
|
|
|
|
if (!Skip) {
|
|
|
|
/* Don't expand macros in this line */
|
|
|
|
ExpandMacros = 0;
|
|
|
|
/* #pragma is handled on the scanner level */
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PP_UNDEF:
|
|
|
|
if (!Skip) {
|
|
|
|
doundef ();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("Preprocessor directive expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
ClearLine ();
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
if (i_ifdef >= 0) {
|
2000-10-15 19:52:01 +00:00
|
|
|
PPError ("`#endif' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipBlank ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Done:
|
|
|
|
xlateline ();
|
2001-03-10 10:21:03 +00:00
|
|
|
Print (stdout, 2, "line: %s\n", line);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|