static char * PrExpandMacros ( char *Line) { char *Token; char *ReplaceString; PR_DEFINE_INFO *DefineInfo; ACPI_SIZE TokenOffset; char *Next; int OffsetAdjust; strcpy (Gbl_ExpressionTokenBuffer, Gbl_CurrentLineBuffer); Token = PrGetNextToken (Gbl_ExpressionTokenBuffer, PR_EXPR_SEPARATORS, &Next); OffsetAdjust = 0; while (Token) { DefineInfo = PrMatchDefine (Token); if (DefineInfo) { if (DefineInfo->Body) { /* This is a macro. TBD: Is this allowed? */ DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Matched Macro: %s->%s\n", Gbl_CurrentLineNumber, DefineInfo->Identifier, DefineInfo->Replacement); PrDoMacroInvocation (Gbl_ExpressionTokenBuffer, Token, DefineInfo, &Next); } else { ReplaceString = DefineInfo->Replacement; /* Replace the name in the original line buffer */ TokenOffset = Token - Gbl_ExpressionTokenBuffer + OffsetAdjust; PrReplaceData ( &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token), ReplaceString, strlen (ReplaceString)); /* Adjust for length difference between old and new name length */ OffsetAdjust += strlen (ReplaceString) - strlen (Token); DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Matched #define within expression: %s->%s\n", Gbl_CurrentLineNumber, Token, *ReplaceString ? ReplaceString : "(NULL STRING)"); } } Token = PrGetNextToken (NULL, PR_EXPR_SEPARATORS, &Next); } return (Line); }
static void PrDoDirective ( char *DirectiveToken, char **Next) { char *Token = Gbl_MainTokenBuffer; char *Token2 = NULL; char *End; UINT64 Value; ACPI_SIZE TokenOffset; int Directive; ACPI_STATUS Status; if (!DirectiveToken) { goto SyntaxError; } Directive = PrMatchDirective (DirectiveToken); if (Directive == ASL_DIRECTIVE_NOT_FOUND) { PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE, THIS_TOKEN_OFFSET (DirectiveToken)); DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "#%s: Unknown directive\n", Gbl_CurrentLineNumber, DirectiveToken); return; } /* * Emit a line directive into the preprocessor file (.pre) after * every matched directive. This is passed through to the compiler * so that error/warning messages are kept in sync with the * original source file. */ FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\" // #%s\n", Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_DirectiveInfo[Directive].Name); /* * If we are currently ignoring this block and we encounter a #else or * #elif, we must ignore their blocks also if the parent block is also * being ignored. */ if (Gbl_IgnoringThisCodeBlock) { switch (Directive) { case PR_DIRECTIVE_ELSE: case PR_DIRECTIVE_ELIF: if (Gbl_DirectiveStack && Gbl_DirectiveStack->IgnoringThisCodeBlock) { PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name); return; } break; default: break; } } /* * Need to always check for #else, #elif, #endif regardless of * whether we are ignoring the current code block, since these * are conditional code block terminators. */ switch (Directive) { case PR_DIRECTIVE_ELSE: Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock); PrDbgPrint ("Executing", "else block"); return; case PR_DIRECTIVE_ELIF: Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock); Directive = PR_DIRECTIVE_IF; if (Gbl_IgnoringThisCodeBlock == TRUE) { /* Not executing the ELSE part -- all done here */ PrDbgPrint ("Ignoring", "elif block"); return; } /* * After this, we will execute the IF part further below. * First, however, pop off the original #if directive. */ if (ACPI_FAILURE (PrPopDirective ())) { PrError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, THIS_TOKEN_OFFSET (DirectiveToken)); } PrDbgPrint ("Executing", "elif block"); break; case PR_DIRECTIVE_ENDIF: PrDbgPrint ("Executing", "endif"); /* Pop the owning #if/#ifdef/#ifndef */ if (ACPI_FAILURE (PrPopDirective ())) { PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH, THIS_TOKEN_OFFSET (DirectiveToken)); } return; default: break; } /* Most directives have at least one argument */ if (Gbl_DirectiveInfo[Directive].ArgCount >= 1) { Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token) { goto SyntaxError; } } if (Gbl_DirectiveInfo[Directive].ArgCount >= 2) { Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token2) { goto SyntaxError; } } /* * At this point, if we are ignoring the current code block, * do not process any more directives (i.e., ignore them also.) * For "if" style directives, open/push a new block anyway. We * must do this to keep track of #endif directives */ if (Gbl_IgnoringThisCodeBlock) { switch (Directive) { case PR_DIRECTIVE_IF: case PR_DIRECTIVE_IFDEF: case PR_DIRECTIVE_IFNDEF: PrPushDirective (Directive, Token); PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name); break; default: break; } return; } /* * Execute the directive */ PrDbgPrint ("Begin execution", Gbl_DirectiveInfo[Directive].Name); switch (Directive) { case PR_DIRECTIVE_IF: TokenOffset = Token - Gbl_MainTokenBuffer; /* Need to expand #define macros in the expression string first */ Status = PrResolveIntegerExpression ( &Gbl_CurrentLineBuffer[TokenOffset-1], &Value); if (ACPI_FAILURE (Status)) { return; } PrPushDirective (Directive, Token); if (!Value) { Gbl_IgnoringThisCodeBlock = TRUE; } DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "Resolved #if: %8.8X%8.8X %s\n", Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value), Gbl_IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>"); break; case PR_DIRECTIVE_IFDEF: PrPushDirective (Directive, Token); if (!PrMatchDefine (Token)) { Gbl_IgnoringThisCodeBlock = TRUE; } PrDbgPrint ("Evaluated", "ifdef"); break; case PR_DIRECTIVE_IFNDEF: PrPushDirective (Directive, Token); if (PrMatchDefine (Token)) { Gbl_IgnoringThisCodeBlock = TRUE; } PrDbgPrint ("Evaluated", "ifndef"); break; case PR_DIRECTIVE_DEFINE: /* * By definition, if first char after the name is a paren, * this is a function macro. */ TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token); if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(') { #ifndef MACROS_SUPPORTED AcpiOsPrintf ( "%s ERROR - line %u: #define macros are not supported yet\n", Gbl_CurrentLineBuffer, Gbl_LogicalLineNumber); exit(1); #else PrAddMacro (Token, Next); #endif } else { /* Use the remainder of the line for the #define */ Token2 = *Next; if (Token2) { while ((*Token2 == ' ') || (*Token2 == '\t')) { Token2++; } End = Token2; while (*End != '\n') { End++; } *End = 0; } else { Token2 = ""; } #if 0 Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next); if (!Token2) { Token2 = ""; } #endif DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "New #define: %s->%s\n", Gbl_LogicalLineNumber, Token, Token2); PrAddDefine (Token, Token2, FALSE); } break; case PR_DIRECTIVE_ERROR: /* Note: No macro expansion */ PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE, THIS_TOKEN_OFFSET (Token)); Gbl_SourceLine = 0; Gbl_NextError = Gbl_ErrorLog; CmCleanupAndExit (); exit(1); case PR_DIRECTIVE_INCLUDE: Token = PrGetNextToken (NULL, " \"<>", Next); if (!Token) { goto SyntaxError; } DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "Start #include file \"%s\"\n", Gbl_CurrentLineNumber, Token, Gbl_CurrentLineNumber); PrDoIncludeFile (Token); break; case PR_DIRECTIVE_INCLUDEBUFFER: Token = PrGetNextToken (NULL, " \"<>", Next); if (!Token) { goto SyntaxError; } Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token2) { goto SyntaxError; } DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "Start #includebuffer input from file \"%s\", buffer name %s\n", Gbl_CurrentLineNumber, Token, Token2); PrDoIncludeBuffer (Token, Token2); break; case PR_DIRECTIVE_LINE: TokenOffset = Token - Gbl_MainTokenBuffer; Status = PrResolveIntegerExpression ( &Gbl_CurrentLineBuffer[TokenOffset-1], &Value); if (ACPI_FAILURE (Status)) { return; } DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "User #line invocation %s\n", Gbl_CurrentLineNumber, Token); Gbl_CurrentLineNumber = (UINT32) Value; /* Emit #line into the preprocessor file */ FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename); break; case PR_DIRECTIVE_PRAGMA: if (!strcmp (Token, "disable")) { Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token) { goto SyntaxError; } TokenOffset = Token - Gbl_MainTokenBuffer; AslDisableException (&Gbl_CurrentLineBuffer[TokenOffset]); } else if (!strcmp (Token, "message")) { Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token) { goto SyntaxError; } TokenOffset = Token - Gbl_MainTokenBuffer; AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]); } else { PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA, THIS_TOKEN_OFFSET (Token)); return; } break; case PR_DIRECTIVE_UNDEF: DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "#undef: %s\n", Gbl_CurrentLineNumber, Token); PrRemoveDefine (Token); break; case PR_DIRECTIVE_WARNING: PrError (ASL_WARNING, ASL_MSG_WARNING_DIRECTIVE, THIS_TOKEN_OFFSET (Token)); Gbl_SourceLine = 0; Gbl_NextError = Gbl_ErrorLog; break; default: /* Should never get here */ DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID "Unrecognized directive: %u\n", Gbl_CurrentLineNumber, Directive); break; } return; SyntaxError: PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX, THIS_TOKEN_OFFSET (DirectiveToken)); return; }
static void PrPreprocessInputFile ( void) { UINT32 Status; char *Token; char *ReplaceString; PR_DEFINE_INFO *DefineInfo; ACPI_SIZE TokenOffset; char *Next; int OffsetAdjust; PrGetNextLineInit (); /* Scan source line-by-line and process directives. Then write the .i file */ while ((Status = PrGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF) { Gbl_CurrentLineNumber++; Gbl_LogicalLineNumber++; if (Status == ASL_IGNORE_LINE) { goto WriteEntireLine; } /* Need a copy of the input line for strok() */ strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer); Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next); OffsetAdjust = 0; /* All preprocessor directives must begin with '#' */ if (Token && (*Token == '#')) { if (strlen (Token) == 1) { Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next); } else { Token++; /* Skip leading # */ } /* Execute the directive, do not write line to output file */ PrDoDirective (Token, &Next); continue; } /* * If we are currently within the part of an IF/ELSE block that is * FALSE, ignore the line and do not write it to the output file. * This continues until an #else or #endif is encountered. */ if (Gbl_IgnoringThisCodeBlock) { continue; } /* Match and replace all #defined names within this source line */ while (Token) { DefineInfo = PrMatchDefine (Token); if (DefineInfo) { if (DefineInfo->Body) { /* This is a macro */ DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Matched Macro: %s->%s\n", Gbl_CurrentLineNumber, DefineInfo->Identifier, DefineInfo->Replacement); PrDoMacroInvocation (Gbl_MainTokenBuffer, Token, DefineInfo, &Next); } else { ReplaceString = DefineInfo->Replacement; /* Replace the name in the original line buffer */ TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust; PrReplaceData ( &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token), ReplaceString, strlen (ReplaceString)); /* Adjust for length difference between old and new name length */ OffsetAdjust += strlen (ReplaceString) - strlen (Token); DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Matched #define: %s->%s\n", Gbl_CurrentLineNumber, Token, *ReplaceString ? ReplaceString : "(NULL STRING)"); } } Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next); } Gbl_PreprocessorLineNumber++; WriteEntireLine: /* * Now we can write the possibly modified source line to the * preprocessor file(s). */ FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer, strlen (Gbl_CurrentLineBuffer)); } }
void PrDoMacroInvocation ( char *TokenBuffer, char *MacroStart, PR_DEFINE_INFO *DefineInfo, char **Next) { PR_MACRO_ARG *Args; char *Token = NULL; UINT32 TokenOffset; UINT32 Length; UINT32 i; /* Take a copy of the macro body for expansion */ strcpy (Gbl_MacroTokenBuffer, DefineInfo->Body); /* Replace each argument within the prototype body */ Args = DefineInfo->Args; if (!Args->Name) { /* This macro has no arguments */ Token = PrGetNextToken (NULL, PR_MACRO_ARGUMENTS, Next); if (!Token) { goto BadInvocation; } TokenOffset = (MacroStart - TokenBuffer); Length = Token - MacroStart + strlen (Token) + 1; PrReplaceData ( &Gbl_CurrentLineBuffer[TokenOffset], Length, Gbl_MacroTokenBuffer, strlen (Gbl_MacroTokenBuffer)); return; } while (Args->Name) { /* Get the next argument from macro invocation */ Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next); if (!Token) { goto BadInvocation; } /* Replace all instances of this argument */ for (i = 0; i < Args->UseCount; i++) { /* Offset zero indicates "arg not used" */ /* TBD: Not really needed now, with UseCount available */ if (Args->Offset[i] == 0) { break; } PrReplaceData ( &Gbl_MacroTokenBuffer[Args->Offset[i]], strlen (Args->Name), Token, strlen (Token)); DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "ExpandArg: %s \n", Gbl_CurrentLineNumber, Gbl_MacroTokenBuffer); } Args++; } /* TBD: need to make sure macro was not invoked with too many arguments */ if (!Token) { return; } /* Replace the entire macro invocation with the expanded macro */ TokenOffset = (MacroStart - TokenBuffer); Length = Token - MacroStart + strlen (Token) + 1; PrReplaceData ( &Gbl_CurrentLineBuffer[TokenOffset], Length, Gbl_MacroTokenBuffer, strlen (Gbl_MacroTokenBuffer)); return; BadInvocation: PrError (ASL_ERROR, ASL_MSG_INVALID_INVOCATION, THIS_TOKEN_OFFSET (MacroStart)); DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Bad macro invocation: %s \n", Gbl_CurrentLineNumber, Gbl_MacroTokenBuffer); return; }
void PrAddMacro ( char *Name, char **Next) { char *Token = NULL; ACPI_SIZE TokenOffset; ACPI_SIZE MacroBodyOffset; PR_DEFINE_INFO *DefineInfo; PR_MACRO_ARG *Args; char *Body; char *BodyInSource; UINT32 i; UINT16 UseCount = 0; UINT16 ArgCount = 0; UINT32 Depth = 1; UINT32 EndOfArgList; char BufferChar; /* Find the end of the arguments list */ TokenOffset = Name - Gbl_MainTokenBuffer + strlen (Name) + 1; while (1) { BufferChar = Gbl_CurrentLineBuffer[TokenOffset]; if (BufferChar == '(') { Depth++; } else if (BufferChar == ')') { Depth--; } else if (BufferChar == 0) { PrError (ASL_ERROR, ASL_MSG_MACRO_SYNTAX, TokenOffset); return; } if (Depth == 0) { /* Found arg list end */ EndOfArgList = TokenOffset; break; } TokenOffset++; } /* At this point, we know that we have a reasonable argument list */ Args = UtLocalCalloc (sizeof (PR_MACRO_ARG) * PR_MAX_MACRO_ARGS); /* Get the macro argument names */ for (i = 0; i < PR_MAX_MACRO_ARGS; i++) { Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next); if (!Token) { /* This is the case for a NULL macro body */ BodyInSource = ""; goto AddMacroToList; } /* Don't go beyond the argument list */ TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token); if (TokenOffset > EndOfArgList) { break; } DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Macro arg: %s \n", Gbl_CurrentLineNumber, Token); Args[i].Name = UtLocalCalloc (strlen (Token) + 1); strcpy (Args[i].Name, Token); Args[i].UseCount = 0; ArgCount++; if (ArgCount >= PR_MAX_MACRO_ARGS) { PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS, TokenOffset); goto ErrorExit; } } /* Get the macro body. Token now points to start of body */ MacroBodyOffset = Token - Gbl_MainTokenBuffer; /* Match each method arg in the macro body for later use */ Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next); while (Token) { /* Search the macro arg list for matching arg */ for (i = 0; Args[i].Name && (i < PR_MAX_MACRO_ARGS); i++) { /* * Save argument offset within macro body. This is the mechanism * used to expand the macro upon invocation. * * Handles multiple instances of the same argument */ if (!strcmp (Token, Args[i].Name)) { UseCount = Args[i].UseCount; Args[i].Offset[UseCount] = (Token - Gbl_MainTokenBuffer) - MacroBodyOffset; DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Macro Arg #%u: %s UseCount %u Offset %u \n", Gbl_CurrentLineNumber, i, Token, UseCount+1, Args[i].Offset[UseCount]); Args[i].UseCount++; if (Args[i].UseCount >= PR_MAX_ARG_INSTANCES) { PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS, THIS_TOKEN_OFFSET (Token)); goto ErrorExit; } break; } } Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next); } BodyInSource = &Gbl_CurrentLineBuffer[MacroBodyOffset]; AddMacroToList: /* Check if name is already defined first */ DefineInfo = PrMatchDefine (Name); if (DefineInfo) { DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "#define: macro name already exists: %s\n", Gbl_CurrentLineNumber, Name); /* Error only if not exactly the same macro */ if (strcmp (DefineInfo->Body, BodyInSource) || (DefineInfo->ArgCount != ArgCount)) { PrError (ASL_ERROR, ASL_MSG_EXISTING_NAME, THIS_TOKEN_OFFSET (Name)); } goto ErrorExit; } DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Macro body: %s \n", Gbl_CurrentLineNumber, BodyInSource); /* Add macro to the #define list */ DefineInfo = PrAddDefine (Name, BodyInSource, FALSE); if (DefineInfo) { Body = UtLocalCalloc (strlen (BodyInSource) + 1); strcpy (Body, BodyInSource); DefineInfo->Body = Body; DefineInfo->Args = Args; DefineInfo->ArgCount = ArgCount; } return; ErrorExit: ACPI_FREE (Args); return; }
static void PrDoDirective ( char *DirectiveToken, char **Next, BOOLEAN *IgnoringThisCodeBlock) { char *Token = Gbl_MainTokenBuffer; char *Token2; char *End; UINT64 Value; ACPI_SIZE TokenOffset; int Directive; ACPI_STATUS Status; if (!DirectiveToken) { goto SyntaxError; } Directive = PrMatchDirective (DirectiveToken); if (Directive == ASL_DIRECTIVE_NOT_FOUND) { PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE, THIS_TOKEN_OFFSET (DirectiveToken)); DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "#%s: Unknown directive\n", Gbl_CurrentLineNumber, DirectiveToken); return; } /* TBD: Need a faster way to do this: */ if ((Directive == PR_DIRECTIVE_ELIF) || (Directive == PR_DIRECTIVE_ELSE) || (Directive == PR_DIRECTIVE_ENDIF)) { DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Begin #%s\n", Gbl_CurrentLineNumber, Gbl_DirectiveInfo[Directive].Name); } /* * Need to always check for #else, #elif, #endif regardless of * whether we are ignoring the current code block, since these * are conditional code block terminators. */ switch (Directive) { case PR_DIRECTIVE_ELIF: *IgnoringThisCodeBlock = !(*IgnoringThisCodeBlock); if (*IgnoringThisCodeBlock == TRUE) { /* Not executing the ELSE part -- all done here */ return; } /* Will execute the ELSE..IF part */ DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "#elif - Executing else block\n", Gbl_CurrentLineNumber); Directive = PR_DIRECTIVE_IF; break; case PR_DIRECTIVE_ELSE: *IgnoringThisCodeBlock = !(*IgnoringThisCodeBlock); return; case PR_DIRECTIVE_ENDIF: *IgnoringThisCodeBlock = FALSE; Gbl_IfDepth--; if (Gbl_IfDepth < 0) { PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH, THIS_TOKEN_OFFSET (DirectiveToken)); Gbl_IfDepth = 0; } return; default: break; } /* * At this point, if we are ignoring the current code block, * do not process any more directives (i.e., ignore them also.) */ if (*IgnoringThisCodeBlock == TRUE) { return; } DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Begin #%s\n", Gbl_CurrentLineNumber, Gbl_DirectiveInfo[Directive].Name); /* Most directives have at least one argument */ if (Gbl_DirectiveInfo[Directive].ArgCount == 1) { Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token) { goto SyntaxError; } } switch (Directive) { case PR_DIRECTIVE_DEFINE: /* * By definition, if first char after the name is a paren, * this is a function macro. */ TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token); if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(') { #ifndef MACROS_SUPPORTED AcpiOsPrintf ("%s ERROR - line %u: #define macros are not supported yet\n", Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber); exit(1); #else PrAddMacro (Token, Next); #endif } else { /* Use the remainder of the line for the #define */ Token2 = *Next; if (Token2) { while ((*Token2 == ' ') || (*Token2 == '\t')) { Token2++; } End = Token2; while (*End != '\n') { End++; } *End = 0; } else { Token2 = ""; } #if 0 Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next); if (!Token2) { Token2 = ""; } #endif DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "New #define: %s->%s\n", Gbl_CurrentLineNumber, Token, Token2); PrAddDefine (Token, Token2, FALSE); } break; case PR_DIRECTIVE_ERROR: /* TBD compiler should abort */ /* Note: No macro expansion */ PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE, THIS_TOKEN_OFFSET (Token)); break; case PR_DIRECTIVE_IF: TokenOffset = Token - Gbl_MainTokenBuffer; /* Need to expand #define macros in the expression string first */ Status = PrResolveIntegerExpression ( &Gbl_CurrentLineBuffer[TokenOffset-1], &Value); if (ACPI_FAILURE (Status)) { return; } if (!Value) { *IgnoringThisCodeBlock = TRUE; } DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Resolved #if: %8.8X%8.8X %s\n", Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value), *IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>"); Gbl_IfDepth++; break; case PR_DIRECTIVE_IFDEF: if (!PrMatchDefine (Token)) { *IgnoringThisCodeBlock = TRUE; } Gbl_IfDepth++; DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Start #ifdef %s\n", Gbl_CurrentLineNumber, *IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>"); break; case PR_DIRECTIVE_IFNDEF: if (PrMatchDefine (Token)) { *IgnoringThisCodeBlock = TRUE; } Gbl_IfDepth++; DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Start #ifndef %2.2X\n", Gbl_CurrentLineNumber, *IgnoringThisCodeBlock, Gbl_CurrentLineNumber); break; case PR_DIRECTIVE_INCLUDE: Token = PrGetNextToken (NULL, " \"<>", Next); if (!Token) { goto SyntaxError; } DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Start #include file \"%s\"\n", Gbl_CurrentLineNumber, Token, Gbl_CurrentLineNumber); PrOpenIncludeFile (Token); break; case PR_DIRECTIVE_LINE: TokenOffset = Token - Gbl_MainTokenBuffer; Status = PrResolveIntegerExpression ( &Gbl_CurrentLineBuffer[TokenOffset-1], &Value); if (ACPI_FAILURE (Status)) { return; } DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "User #line invocation %s\n", Gbl_CurrentLineNumber, Token); /* Update local line numbers */ Gbl_CurrentLineNumber = (UINT32) Value; Gbl_PreviousLineNumber = 0; /* Emit #line into the preprocessor file */ FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename); break; case PR_DIRECTIVE_PRAGMA: /* Only "#pragma message" supported at this time */ if (strcmp (Token, "message")) { PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA, THIS_TOKEN_OFFSET (Token)); return; } Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next); if (!Token) { goto SyntaxError; } TokenOffset = Token - Gbl_MainTokenBuffer; AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]); break; case PR_DIRECTIVE_UNDEF: DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "#undef: %s\n", Gbl_CurrentLineNumber, Token); PrRemoveDefine (Token); break; case PR_DIRECTIVE_WARNING: PrError (ASL_WARNING, ASL_MSG_ERROR_DIRECTIVE, THIS_TOKEN_OFFSET (Token)); break; default: /* Should never get here */ DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Unrecognized directive: %u\n", Gbl_CurrentLineNumber, Directive); break; } return; SyntaxError: PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX, THIS_TOKEN_OFFSET (DirectiveToken)); return; }
static void PrPreprocessInputFile ( void) { UINT32 Offset; char *Token; char *ReplaceString; PR_DEFINE_INFO *DefineInfo; ACPI_SIZE TokenOffset; BOOLEAN IgnoringThisCodeBlock = FALSE; char *Next; int OffsetAdjust; /* Scan line-by-line. Comments and blank lines are skipped by this function */ while ((Offset = DtGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF) { /* Need a copy of the input line for strok() */ strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer); Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next); OffsetAdjust = 0; /* All preprocessor directives must begin with '#' */ if (Token && (*Token == '#')) { if (strlen (Token) == 1) { Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next); } else { Token++; /* Skip leading # */ } /* Execute the directive, do not write line to output file */ PrDoDirective (Token, &Next, &IgnoringThisCodeBlock); continue; } /* * If we are currently within the part of an IF/ELSE block that is * FALSE, ignore the line and do not write it to the output file. * This continues until an #else or #endif is encountered. */ if (IgnoringThisCodeBlock == TRUE) { continue; } /* Match and replace all #defined names within this source line */ while (Token) { DefineInfo = PrMatchDefine (Token); if (DefineInfo) { if (DefineInfo->Body) { /* This is a macro */ DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Matched Macro: %s->%s\n", Gbl_CurrentLineNumber, DefineInfo->Identifier, DefineInfo->Replacement); PrDoMacroInvocation (Gbl_MainTokenBuffer, Token, DefineInfo, &Next); } else { ReplaceString = DefineInfo->Replacement; /* Replace the name in the original line buffer */ TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust; PrReplaceData ( &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token), ReplaceString, strlen (ReplaceString)); /* Adjust for length difference between old and new name length */ OffsetAdjust += strlen (ReplaceString) - strlen (Token); DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Matched #define: %s->%s\n", Gbl_CurrentLineNumber, Token, *ReplaceString ? ReplaceString : "(NULL STRING)"); } } Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next); } #if 0 /* Line prefix */ FlPrintFile (ASL_FILE_PREPROCESSOR, "/* %14s %.5u i:%.5u */ ", Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber, Gbl_PreprocessorLineNumber); #endif /* * Emit a #line directive if necessary, to keep the line numbers in * the (.i) file synchronized with the original source code file, so * that the correct line number appears in any error messages * generated by the actual compiler. */ if (Gbl_CurrentLineNumber > (Gbl_PreviousLineNumber + 1)) { FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u\n", Gbl_CurrentLineNumber); } Gbl_PreviousLineNumber = Gbl_CurrentLineNumber; Gbl_PreprocessorLineNumber++; /* * Now we can write the possibly modified source line to the * preprocessor (.i) file */ FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer, strlen (Gbl_CurrentLineBuffer)); } }