1 /******************************************************************************
3 * Module Name: prscan - Preprocessor start-up and file scan module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #define _DECLARE_PR_GLOBALS
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include <contrib/dev/acpica/compiler/dtcompiler.h>
52 * No nested macros, maybe never
53 * Implement ASL "Include" as well as "#include" here?
55 #define _COMPONENT ASL_PREPROCESSOR
56 ACPI_MODULE_NAME ("prscan")
59 /* Local prototypes */
62 PrPreprocessInputFile (
69 BOOLEAN *IgnoringThisCodeBlock);
76 * Supported preprocessor directives
78 static const PR_DIRECTIVE_INFO Gbl_DirectiveInfo[] =
81 {"elif", 0}, /* Converted to #else..#if internally */
88 {"include", 0}, /* Argument is not standard format, so 0 */
96 enum Gbl_DirectiveIndexes
98 PR_DIRECTIVE_DEFINE = 0,
106 PR_DIRECTIVE_INCLUDE,
110 PR_DIRECTIVE_WARNING,
113 #define ASL_DIRECTIVE_NOT_FOUND -1
116 /*******************************************************************************
118 * FUNCTION: PrInitializePreprocessor
124 * DESCRIPTION: Startup initialization for the Preprocessor.
126 ******************************************************************************/
129 PrInitializePreprocessor (
132 /* Init globals and the list of #defines */
134 PrInitializeGlobals ();
135 Gbl_DefineList = NULL;
139 /*******************************************************************************
141 * FUNCTION: PrInitializeGlobals
147 * DESCRIPTION: Initialize globals for the Preprocessor. Used for startuup
148 * initialization and re-initialization between compiles during
149 * a multiple source file compile.
151 ******************************************************************************/
154 PrInitializeGlobals (
160 Gbl_InputFileList = NULL;
161 Gbl_CurrentLineNumber = 0;
162 Gbl_PreprocessorLineNumber = 1;
163 Gbl_PreprocessorError = FALSE;
167 /*******************************************************************************
169 * FUNCTION: PrTerminatePreprocessor
175 * DESCRIPTION: Termination of the preprocessor. Delete lists. Keep any
176 * defines that were specified on the command line, in order to
177 * support multiple compiles with a single compiler invocation.
179 ******************************************************************************/
182 PrTerminatePreprocessor (
185 PR_DEFINE_INFO *DefineInfo;
189 * The persistent defines (created on the command line) are always at the
190 * end of the list. We save them.
192 while ((Gbl_DefineList) && (!Gbl_DefineList->Persist))
194 DefineInfo = Gbl_DefineList;
195 Gbl_DefineList = DefineInfo->Next;
197 ACPI_FREE (DefineInfo->Replacement);
198 ACPI_FREE (DefineInfo->Identifier);
199 ACPI_FREE (DefineInfo);
204 /*******************************************************************************
206 * FUNCTION: PrDoPreprocess
210 * RETURN: Error Status. TRUE if error, FALSE if OK.
212 * DESCRIPTION: Main entry point for the iASL Preprocessor. Input file must
213 * be already open. Handles multiple input files via the
214 * #include directive.
216 ******************************************************************************/
222 BOOLEAN MoreInputFiles;
225 DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
228 FlSeekFile (ASL_FILE_INPUT, 0);
229 PrDumpPredefinedNames ();
231 /* Main preprocessor loop, handles include files */
235 PrPreprocessInputFile ();
236 MoreInputFiles = PrPopInputFileStack ();
238 } while (MoreInputFiles);
242 * TBD: is this necessary? (Do we abort on any preprocessing errors?)
244 if (Gbl_PreprocessorError)
246 /* TBD: can't use source_output file for preprocessor error reporting */
248 Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle = NULL;
249 PrTerminatePreprocessor ();
253 /* Point compiler input to the new preprocessor file (.i) */
255 FlCloseFile (ASL_FILE_INPUT);
256 Gbl_Files[ASL_FILE_INPUT].Handle = Gbl_Files[ASL_FILE_PREPROCESSOR].Handle;
257 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
259 /* Reset globals to allow compiler to run */
261 FlSeekFile (ASL_FILE_INPUT, 0);
262 Gbl_CurrentLineNumber = 1;
264 DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
269 /*******************************************************************************
271 * FUNCTION: PrPreprocessInputFile
277 * DESCRIPTION: Preprocess one entire file, line-by-line.
279 * Input: Raw user ASL from ASL_FILE_INPUT
280 * Output: Preprocessed file written to ASL_FILE_PREPROCESSOR
282 ******************************************************************************/
285 PrPreprocessInputFile (
291 PR_DEFINE_INFO *DefineInfo;
292 ACPI_SIZE TokenOffset;
293 BOOLEAN IgnoringThisCodeBlock = FALSE;
298 /* Scan line-by-line. Comments and blank lines are skipped by this function */
300 while ((Offset = DtGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
302 /* Need a copy of the input line for strok() */
304 strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer);
305 Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
308 /* All preprocessor directives must begin with '#' */
310 if (Token && (*Token == '#'))
312 if (strlen (Token) == 1)
314 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
318 Token++; /* Skip leading # */
321 /* Execute the directive, do not write line to output file */
323 PrDoDirective (Token, &Next, &IgnoringThisCodeBlock);
328 * If we are currently within the part of an IF/ELSE block that is
329 * FALSE, ignore the line and do not write it to the output file.
330 * This continues until an #else or #endif is encountered.
332 if (IgnoringThisCodeBlock == TRUE)
337 /* Match and replace all #defined names within this source line */
341 DefineInfo = PrMatchDefine (Token);
344 if (DefineInfo->Body)
346 /* This is a macro */
348 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
349 "Matched Macro: %s->%s\n",
350 Gbl_CurrentLineNumber, DefineInfo->Identifier,
351 DefineInfo->Replacement);
353 PrDoMacroInvocation (Gbl_MainTokenBuffer, Token,
358 ReplaceString = DefineInfo->Replacement;
360 /* Replace the name in the original line buffer */
362 TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust;
364 &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token),
365 ReplaceString, strlen (ReplaceString));
367 /* Adjust for length difference between old and new name length */
369 OffsetAdjust += strlen (ReplaceString) - strlen (Token);
371 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
372 "Matched #define: %s->%s\n",
373 Gbl_CurrentLineNumber, Token,
374 *ReplaceString ? ReplaceString : "(NULL STRING)");
378 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
383 FlPrintFile (ASL_FILE_PREPROCESSOR, "/* %14s %.5u i:%.5u */ ",
384 Gbl_Files[ASL_FILE_INPUT].Filename,
385 Gbl_CurrentLineNumber, Gbl_PreprocessorLineNumber);
389 * Emit a #line directive if necessary, to keep the line numbers in
390 * the (.i) file synchronized with the original source code file, so
391 * that the correct line number appears in any error messages
392 * generated by the actual compiler.
394 if (Gbl_CurrentLineNumber > (Gbl_PreviousLineNumber + 1))
396 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u\n",
397 Gbl_CurrentLineNumber);
400 Gbl_PreviousLineNumber = Gbl_CurrentLineNumber;
401 Gbl_PreprocessorLineNumber++;
404 * Now we can write the possibly modified source line to the
405 * preprocessor (.i) file
407 FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer,
408 strlen (Gbl_CurrentLineBuffer));
413 /*******************************************************************************
415 * FUNCTION: PrDoDirective
417 * PARAMETERS: Directive - Pointer to directive name token
418 * Next - "Next" buffer from GetNextToken
419 * IgnoringThisCodeBlock - Where the "ignore code" flag is
422 * RETURN: IgnoringThisCodeBlock: Set to TRUE if we are skipping the FALSE
423 * part of an #if or #else block. Set to FALSE when the
424 * corresponding #else or #endif is encountered.
426 * DESCRIPTION: Main processing for all preprocessor directives
428 ******************************************************************************/
432 char *DirectiveToken,
434 BOOLEAN *IgnoringThisCodeBlock)
436 char *Token = Gbl_MainTokenBuffer;
440 ACPI_SIZE TokenOffset;
450 Directive = PrMatchDirective (DirectiveToken);
451 if (Directive == ASL_DIRECTIVE_NOT_FOUND)
453 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
454 THIS_TOKEN_OFFSET (DirectiveToken));
456 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
457 "#%s: Unknown directive\n",
458 Gbl_CurrentLineNumber, DirectiveToken);
462 /* TBD: Need a faster way to do this: */
464 if ((Directive == PR_DIRECTIVE_ELIF) ||
465 (Directive == PR_DIRECTIVE_ELSE) ||
466 (Directive == PR_DIRECTIVE_ENDIF))
468 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Begin #%s\n",
469 Gbl_CurrentLineNumber, Gbl_DirectiveInfo[Directive].Name);
473 * Need to always check for #else, #elif, #endif regardless of
474 * whether we are ignoring the current code block, since these
475 * are conditional code block terminators.
479 case PR_DIRECTIVE_ELIF:
480 *IgnoringThisCodeBlock = !(*IgnoringThisCodeBlock);
481 if (*IgnoringThisCodeBlock == TRUE)
483 /* Not executing the ELSE part -- all done here */
487 /* Will execute the ELSE..IF part */
489 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
490 "#elif - Executing else block\n",
491 Gbl_CurrentLineNumber);
492 Directive = PR_DIRECTIVE_IF;
495 case PR_DIRECTIVE_ELSE:
496 *IgnoringThisCodeBlock = !(*IgnoringThisCodeBlock);
499 case PR_DIRECTIVE_ENDIF:
500 *IgnoringThisCodeBlock = FALSE;
504 PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
505 THIS_TOKEN_OFFSET (DirectiveToken));
515 * At this point, if we are ignoring the current code block,
516 * do not process any more directives (i.e., ignore them also.)
518 if (*IgnoringThisCodeBlock == TRUE)
523 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Begin #%s\n",
524 Gbl_CurrentLineNumber, Gbl_DirectiveInfo[Directive].Name);
526 /* Most directives have at least one argument */
528 if (Gbl_DirectiveInfo[Directive].ArgCount == 1)
530 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
539 case PR_DIRECTIVE_DEFINE:
541 * By definition, if first char after the name is a paren,
542 * this is a function macro.
544 TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token);
545 if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(')
547 #ifndef MACROS_SUPPORTED
548 AcpiOsPrintf ("%s ERROR - line %u: #define macros are not supported yet\n",
549 Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber);
552 PrAddMacro (Token, Next);
557 /* Use the remainder of the line for the #define */
562 while ((*Token2 == ' ') || (*Token2 == '\t'))
578 Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next);
584 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
585 "New #define: %s->%s\n",
586 Gbl_CurrentLineNumber, Token, Token2);
588 PrAddDefine (Token, Token2, FALSE);
592 case PR_DIRECTIVE_ERROR:
593 /* TBD compiler should abort */
594 /* Note: No macro expansion */
596 PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE,
597 THIS_TOKEN_OFFSET (Token));
600 case PR_DIRECTIVE_IF:
601 TokenOffset = Token - Gbl_MainTokenBuffer;
603 /* Need to expand #define macros in the expression string first */
605 Status = PrResolveIntegerExpression (
606 &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
607 if (ACPI_FAILURE (Status))
614 *IgnoringThisCodeBlock = TRUE;
617 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
618 "Resolved #if: %8.8X%8.8X %s\n",
619 Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value),
620 *IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>");
625 case PR_DIRECTIVE_IFDEF:
626 if (!PrMatchDefine (Token))
628 *IgnoringThisCodeBlock = TRUE;
632 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
633 "Start #ifdef %s\n", Gbl_CurrentLineNumber,
634 *IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>");
637 case PR_DIRECTIVE_IFNDEF:
638 if (PrMatchDefine (Token))
640 *IgnoringThisCodeBlock = TRUE;
644 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
645 "Start #ifndef %2.2X\n", Gbl_CurrentLineNumber,
646 *IgnoringThisCodeBlock, Gbl_CurrentLineNumber);
649 case PR_DIRECTIVE_INCLUDE:
650 Token = PrGetNextToken (NULL, " \"<>", Next);
656 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
657 "Start #include file \"%s\"\n", Gbl_CurrentLineNumber,
658 Token, Gbl_CurrentLineNumber);
660 PrOpenIncludeFile (Token);
663 case PR_DIRECTIVE_LINE:
664 TokenOffset = Token - Gbl_MainTokenBuffer;
666 Status = PrResolveIntegerExpression (
667 &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
668 if (ACPI_FAILURE (Status))
673 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
674 "User #line invocation %s\n", Gbl_CurrentLineNumber,
677 /* Update local line numbers */
679 Gbl_CurrentLineNumber = (UINT32) Value;
680 Gbl_PreviousLineNumber = 0;
682 /* Emit #line into the preprocessor file */
684 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
685 Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename);
688 case PR_DIRECTIVE_PRAGMA:
689 /* Only "#pragma message" supported at this time */
691 if (strcmp (Token, "message"))
693 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
694 THIS_TOKEN_OFFSET (Token));
698 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
704 TokenOffset = Token - Gbl_MainTokenBuffer;
705 AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]);
708 case PR_DIRECTIVE_UNDEF:
709 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
710 "#undef: %s\n", Gbl_CurrentLineNumber, Token);
712 PrRemoveDefine (Token);
715 case PR_DIRECTIVE_WARNING:
716 PrError (ASL_WARNING, ASL_MSG_ERROR_DIRECTIVE,
717 THIS_TOKEN_OFFSET (Token));
721 /* Should never get here */
722 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
723 "Unrecognized directive: %u\n",
724 Gbl_CurrentLineNumber, Directive);
733 PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
734 THIS_TOKEN_OFFSET (DirectiveToken));
739 /*******************************************************************************
741 * FUNCTION: PrMatchDirective
743 * PARAMETERS: Directive - Pointer to directive name token
745 * RETURN: Index into command array, -1 if not found
747 * DESCRIPTION: Lookup the incoming directive in the known directives table.
749 ******************************************************************************/
758 if (!Directive || Directive[0] == 0)
760 return (ASL_DIRECTIVE_NOT_FOUND);
763 for (i = 0; Gbl_DirectiveInfo[i].Name; i++)
765 if (!strcmp (Gbl_DirectiveInfo[i].Name, Directive))
771 return (ASL_DIRECTIVE_NOT_FOUND); /* Command not recognized */