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;
165 Gbl_MapBlockHead = UtLocalCalloc (sizeof (PR_LINE_MAPPING));
166 Gbl_MapBlockHead->Map = UtLocalCalloc (PR_LINES_PER_BLOCK * sizeof (UINT32));
170 /*******************************************************************************
172 * FUNCTION: PrTerminatePreprocessor
178 * DESCRIPTION: Termination of the preprocessor. Delete lists. Keep any
179 * defines that were specified on the command line, in order to
180 * support multiple compiles with a single compiler invocation.
182 ******************************************************************************/
185 PrTerminatePreprocessor (
188 PR_DEFINE_INFO *DefineInfo;
189 PR_LINE_MAPPING *MapInfo;
193 * The persistent defines (created on the command line) are always at the
194 * end of the list. We save them.
196 while ((Gbl_DefineList) && (!Gbl_DefineList->Persist))
198 DefineInfo = Gbl_DefineList;
199 Gbl_DefineList = DefineInfo->Next;
201 ACPI_FREE (DefineInfo->Replacement);
202 ACPI_FREE (DefineInfo->Identifier);
203 ACPI_FREE (DefineInfo);
206 /* Clear the line number mappings */
208 while (Gbl_MapBlockHead)
210 MapInfo = Gbl_MapBlockHead;
211 Gbl_MapBlockHead = MapInfo->Next;
213 ACPI_FREE (MapInfo->Map);
219 /*******************************************************************************
221 * FUNCTION: PrDoPreprocess
225 * RETURN: Error Status. TRUE if error, FALSE if OK.
227 * DESCRIPTION: Main entry point for the iASL Preprocessor. Input file must
228 * be already open. Handles multiple input files via the
229 * #include directive.
231 ******************************************************************************/
237 BOOLEAN MoreInputFiles;
240 DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
243 FlSeekFile (ASL_FILE_INPUT, 0);
244 PrDumpPredefinedNames ();
246 /* Main preprocessor loop, handles include files */
250 PrPreprocessInputFile ();
251 MoreInputFiles = PrPopInputFileStack ();
253 } while (MoreInputFiles);
257 * TBD: is this necessary? (Do we abort on any preprocessing errors?)
259 if (Gbl_PreprocessorError)
261 /* TBD: can't use source_output file for preprocessor error reporting */
263 Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Handle = NULL;
264 PrTerminatePreprocessor ();
268 /* Point compiler input to the new preprocessor file (.i) */
270 FlCloseFile (ASL_FILE_INPUT);
271 Gbl_Files[ASL_FILE_INPUT].Handle = Gbl_Files[ASL_FILE_PREPROCESSOR].Handle;
272 AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
274 /* Reset globals to allow compiler to run */
276 FlSeekFile (ASL_FILE_INPUT, 0);
277 Gbl_CurrentLineNumber = 1;
279 DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
284 /*******************************************************************************
286 * FUNCTION: PrPreprocessInputFile
292 * DESCRIPTION: Preprocess one entire file, line-by-line.
294 * Input: Raw user ASL from ASL_FILE_INPUT
295 * Output: Preprocessed file written to ASL_FILE_PREPROCESSOR
297 ******************************************************************************/
300 PrPreprocessInputFile (
306 PR_DEFINE_INFO *DefineInfo;
307 ACPI_SIZE TokenOffset;
308 BOOLEAN IgnoringThisCodeBlock = FALSE;
313 /* Scan line-by-line. Comments and blank lines are skipped by this function */
315 while ((Offset = DtGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
317 /* Need a copy of the input line for strok() */
319 strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer);
320 Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
323 /* All preprocessor directives must begin with '#' */
325 if (Token && (*Token == '#'))
327 if (strlen (Token) == 1)
329 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
333 Token++; /* Skip leading # */
336 /* Execute the directive, do not write line to output file */
338 PrDoDirective (Token, &Next, &IgnoringThisCodeBlock);
343 * If we are currently within the part of an IF/ELSE block that is
344 * FALSE, ignore the line and do not write it to the output file.
345 * This continues until an #else or #endif is encountered.
347 if (IgnoringThisCodeBlock == TRUE)
352 /* Match and replace all #defined names within this source line */
356 DefineInfo = PrMatchDefine (Token);
359 if (DefineInfo->Body)
361 /* This is a macro */
363 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
364 "Matched Macro: %s->%s\n",
365 Gbl_CurrentLineNumber, DefineInfo->Identifier,
366 DefineInfo->Replacement);
368 PrDoMacroInvocation (Gbl_MainTokenBuffer, Token,
373 ReplaceString = DefineInfo->Replacement;
375 /* Replace the name in the original line buffer */
377 TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust;
379 &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token),
380 ReplaceString, strlen (ReplaceString));
382 /* Adjust for length difference between old and new name length */
384 OffsetAdjust += strlen (ReplaceString) - strlen (Token);
386 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
387 "Matched #define: %s->%s\n",
388 Gbl_CurrentLineNumber, Token,
389 *ReplaceString ? ReplaceString : "(NULL STRING)");
393 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
396 /* Write the possibly modified line to the .i file*/
400 FlPrintFile (ASL_FILE_PREPROCESSOR, "/* %14s %.5u i:%.5u */ ",
401 Gbl_Files[ASL_FILE_INPUT].Filename,
402 Gbl_CurrentLineNumber, Gbl_PreprocessorLineNumber);
405 FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer,
406 strlen (Gbl_CurrentLineBuffer));
408 PrSetLineNumber (Gbl_CurrentLineNumber, Gbl_PreprocessorLineNumber);
409 Gbl_PreprocessorLineNumber++;
414 /*******************************************************************************
416 * FUNCTION: PrDoDirective
418 * PARAMETERS: Directive - Pointer to directive name token
419 * Next - "Next" buffer from GetNextToken
420 * IgnoringThisCodeBlock - Where the "ignore code" flag is
423 * RETURN: IgnoringThisCodeBlock: Set to TRUE if we are skipping the FALSE
424 * part of an #if or #else block. Set to FALSE when the
425 * corresponding #else or #endif is encountered.
427 * DESCRIPTION: Main processing for all preprocessor directives
429 ******************************************************************************/
433 char *DirectiveToken,
435 BOOLEAN *IgnoringThisCodeBlock)
437 char *Token = Gbl_MainTokenBuffer;
441 ACPI_SIZE TokenOffset;
451 Directive = PrMatchDirective (DirectiveToken);
452 if (Directive == ASL_DIRECTIVE_NOT_FOUND)
454 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
455 THIS_TOKEN_OFFSET (DirectiveToken));
457 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
458 "#%s: Unknown directive\n",
459 Gbl_CurrentLineNumber, DirectiveToken);
463 /* TBD: Need a faster way to do this: */
465 if ((Directive == PR_DIRECTIVE_ELIF) ||
466 (Directive == PR_DIRECTIVE_ELSE) ||
467 (Directive == PR_DIRECTIVE_ENDIF))
469 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Begin #%s\n",
470 Gbl_CurrentLineNumber, Gbl_DirectiveInfo[Directive].Name);
474 * Need to always check for #else, #elif, #endif regardless of
475 * whether we are ignoring the current code block, since these
476 * are conditional code block terminators.
480 case PR_DIRECTIVE_ELIF:
481 *IgnoringThisCodeBlock = !(*IgnoringThisCodeBlock);
482 if (*IgnoringThisCodeBlock == TRUE)
484 /* Not executing the ELSE part -- all done here */
488 /* Will execute the ELSE..IF part */
490 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
491 "#elif - Executing else block\n",
492 Gbl_CurrentLineNumber);
493 Directive = PR_DIRECTIVE_IF;
496 case PR_DIRECTIVE_ELSE:
497 *IgnoringThisCodeBlock = !(*IgnoringThisCodeBlock);
500 case PR_DIRECTIVE_ENDIF:
501 *IgnoringThisCodeBlock = FALSE;
505 PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
506 THIS_TOKEN_OFFSET (DirectiveToken));
516 * At this point, if we are ignoring the current code block,
517 * do not process any more directives (i.e., ignore them also.)
519 if (*IgnoringThisCodeBlock == TRUE)
524 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID "Begin #%s\n",
525 Gbl_CurrentLineNumber, Gbl_DirectiveInfo[Directive].Name);
527 /* Most directives have at least one argument */
529 if (Gbl_DirectiveInfo[Directive].ArgCount == 1)
531 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
540 case PR_DIRECTIVE_DEFINE:
542 * By definition, if first char after the name is a paren,
543 * this is a function macro.
545 TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token);
546 if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(')
548 #ifndef MACROS_SUPPORTED
549 AcpiOsPrintf ("#define macros not supported\n");
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_PRAGMA:
664 /* Only "#pragma message" supported at this time */
666 if (strcmp (Token, "message"))
668 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
669 THIS_TOKEN_OFFSET (Token));
673 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
679 TokenOffset = Token - Gbl_MainTokenBuffer;
680 AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]);
683 case PR_DIRECTIVE_UNDEF:
684 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
685 "#undef: %s\n", Gbl_CurrentLineNumber, Token);
687 PrRemoveDefine (Token);
690 case PR_DIRECTIVE_WARNING:
691 PrError (ASL_WARNING, ASL_MSG_ERROR_DIRECTIVE,
692 THIS_TOKEN_OFFSET (Token));
695 case PR_DIRECTIVE_LINE:
696 /* TBD: set line number -- or, do this in main compiler */
698 /* Should never get here */
699 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
700 "Unrecognized directive: %u\n",
701 Gbl_CurrentLineNumber, Directive);
710 PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
711 THIS_TOKEN_OFFSET (DirectiveToken));
716 /*******************************************************************************
718 * FUNCTION: PrMatchDirective
720 * PARAMETERS: Directive - Pointer to directive name token
722 * RETURN: Index into command array, -1 if not found
724 * DESCRIPTION: Lookup the incoming directive in the known directives table.
726 ******************************************************************************/
735 if (!Directive || Directive[0] == 0)
737 return (ASL_DIRECTIVE_NOT_FOUND);
740 for (i = 0; Gbl_DirectiveInfo[i].Name; i++)
742 if (!strcmp (Gbl_DirectiveInfo[i].Name, Directive))
748 return (ASL_DIRECTIVE_NOT_FOUND); /* Command not recognized */