Sync ACPICA with Intel's version 20180927:
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / prscan.c
blob580b47f0a13f34942b2dd9b39c4c8477bcbe5e79
1 /******************************************************************************
3 * Module Name: prscan - Preprocessor start-up and file scan module
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #define _DECLARE_PR_GLOBALS
154 #include "aslcompiler.h"
157 * TBDs:
159 * No nested macros, maybe never
160 * Implement ASL "Include" as well as "#include" here?
162 #define _COMPONENT ASL_PREPROCESSOR
163 ACPI_MODULE_NAME ("prscan")
166 /* Local prototypes */
168 static void
169 PrPreprocessInputFile (
170 void);
172 static void
173 PrDoDirective (
174 char *DirectiveToken,
175 char **Next);
177 static void
178 PrGetNextLineInit (
179 void);
181 static UINT32
182 PrGetNextLine (
183 FILE *Handle);
185 static int
186 PrMatchDirective (
187 char *Directive);
189 static void
190 PrPushDirective (
191 int Directive,
192 char *Argument);
194 static ACPI_STATUS
195 PrPopDirective (
196 void);
198 static void
199 PrDbgPrint (
200 char *Action,
201 char *DirectiveName);
203 static void
204 PrDoIncludeBuffer (
205 char *Pathname,
206 char *BufferName);
208 static void
209 PrDoIncludeFile (
210 char *Pathname);
214 * Supported preprocessor directives
215 * Each entry is of the form "Name, ArgumentCount"
217 static const PR_DIRECTIVE_INFO AslGbl_DirectiveInfo[] =
219 {"define", 1},
220 {"elif", 0}, /* Converted to #else..#if internally */
221 {"else", 0},
222 {"endif", 0},
223 {"error", 1},
224 {"if", 1},
225 {"ifdef", 1},
226 {"ifndef", 1},
227 {"include", 0}, /* Argument is not standard format, so just use 0 here */
228 {"includebuffer", 0}, /* Argument is not standard format, so just use 0 here */
229 {"line", 1},
230 {"pragma", 1},
231 {"undef", 1},
232 {"warning", 1},
233 {NULL, 0}
236 /* This table must match ordering of above table exactly */
238 enum Gbl_DirectiveIndexes
240 PR_DIRECTIVE_DEFINE = 0,
241 PR_DIRECTIVE_ELIF,
242 PR_DIRECTIVE_ELSE,
243 PR_DIRECTIVE_ENDIF,
244 PR_DIRECTIVE_ERROR,
245 PR_DIRECTIVE_IF,
246 PR_DIRECTIVE_IFDEF,
247 PR_DIRECTIVE_IFNDEF,
248 PR_DIRECTIVE_INCLUDE,
249 PR_DIRECTIVE_INCLUDEBUFFER,
250 PR_DIRECTIVE_LINE,
251 PR_DIRECTIVE_PRAGMA,
252 PR_DIRECTIVE_UNDEF,
253 PR_DIRECTIVE_WARNING
256 #define ASL_DIRECTIVE_NOT_FOUND -1
259 /*******************************************************************************
261 * FUNCTION: PrInitializePreprocessor
263 * PARAMETERS: None
265 * RETURN: None
267 * DESCRIPTION: Startup initialization for the Preprocessor.
269 ******************************************************************************/
271 void
272 PrInitializePreprocessor (
273 void)
275 /* Init globals and the list of #defines */
277 PrInitializeGlobals ();
278 AslGbl_DefineList = NULL;
282 /*******************************************************************************
284 * FUNCTION: PrInitializeGlobals
286 * PARAMETERS: None
288 * RETURN: None
290 * DESCRIPTION: Initialize globals for the Preprocessor. Used for startuup
291 * initialization and re-initialization between compiles during
292 * a multiple source file compile.
294 ******************************************************************************/
296 void
297 PrInitializeGlobals (
298 void)
300 /* Init globals */
302 AslGbl_InputFileList = NULL;
303 AslGbl_CurrentLineNumber = 1;
304 AslGbl_PreprocessorLineNumber = 1;
305 AslGbl_PreprocessorError = FALSE;
307 /* These are used to track #if/#else blocks (possibly nested) */
309 AslGbl_IfDepth = 0;
310 AslGbl_IgnoringThisCodeBlock = FALSE;
311 AslGbl_DirectiveStack = NULL;
315 /*******************************************************************************
317 * FUNCTION: PrTerminatePreprocessor
319 * PARAMETERS: None
321 * RETURN: None
323 * DESCRIPTION: Termination of the preprocessor. Delete lists. Keep any
324 * defines that were specified on the command line, in order to
325 * support multiple compiles with a single compiler invocation.
327 ******************************************************************************/
329 void
330 PrTerminatePreprocessor (
331 void)
333 PR_DEFINE_INFO *DefineInfo;
337 * The persistent defines (created on the command line) are always at the
338 * end of the list. We save them.
340 while ((AslGbl_DefineList) && (!AslGbl_DefineList->Persist))
342 DefineInfo = AslGbl_DefineList;
343 AslGbl_DefineList = DefineInfo->Next;
345 ACPI_FREE (DefineInfo->Replacement);
346 ACPI_FREE (DefineInfo->Identifier);
347 ACPI_FREE (DefineInfo);
352 /*******************************************************************************
354 * FUNCTION: PrDoPreprocess
356 * PARAMETERS: None
358 * RETURN: None
360 * DESCRIPTION: Main entry point for the iASL Preprocessor. Input file must
361 * be already open. Handles multiple input files via the
362 * #include directive.
364 ******************************************************************************/
366 void
367 PrDoPreprocess (
368 void)
370 BOOLEAN MoreInputFiles;
373 DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
376 FlSeekFile (ASL_FILE_INPUT, 0);
377 PrDumpPredefinedNames ();
379 /* Main preprocessor loop, handles include files */
383 PrPreprocessInputFile ();
384 MoreInputFiles = PrPopInputFileStack ();
386 } while (MoreInputFiles);
388 /* Point compiler input to the new preprocessor output file (.pre) */
390 FlCloseFile (ASL_FILE_INPUT);
391 AslGbl_Files[ASL_FILE_INPUT].Handle = AslGbl_Files[ASL_FILE_PREPROCESSOR].Handle;
392 AslCompilerin = AslGbl_Files[ASL_FILE_INPUT].Handle;
394 /* Reset globals to allow compiler to run */
396 FlSeekFile (ASL_FILE_INPUT, 0);
397 if (!AslGbl_PreprocessOnly)
399 AslGbl_CurrentLineNumber = 0;
402 DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
406 /*******************************************************************************
408 * FUNCTION: PrPreprocessInputFile
410 * PARAMETERS: None
412 * RETURN: None
414 * DESCRIPTION: Preprocess one entire file, line-by-line.
416 * Input: Raw user ASL from ASL_FILE_INPUT
417 * Output: Preprocessed file written to ASL_FILE_PREPROCESSOR and
418 * (optionally) ASL_FILE_PREPROCESSOR_USER
420 ******************************************************************************/
422 static void
423 PrPreprocessInputFile (
424 void)
426 UINT32 Status;
427 char *Token;
428 char *ReplaceString;
429 PR_DEFINE_INFO *DefineInfo;
430 ACPI_SIZE TokenOffset;
431 char *Next;
432 int OffsetAdjust;
435 PrGetNextLineInit ();
437 /* Scan source line-by-line and process directives. Then write the .i file */
439 while ((Status = PrGetNextLine (AslGbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
441 AslGbl_CurrentLineNumber++;
442 AslGbl_LogicalLineNumber++;
444 if (Status == ASL_IGNORE_LINE)
446 goto WriteEntireLine;
449 /* Need a copy of the input line for strok() */
451 strcpy (AslGbl_MainTokenBuffer, AslGbl_CurrentLineBuffer);
452 Token = PrGetNextToken (AslGbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
453 OffsetAdjust = 0;
455 /* All preprocessor directives must begin with '#' */
457 if (Token && (*Token == '#'))
459 if (strlen (Token) == 1)
461 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
463 else
465 Token++; /* Skip leading # */
468 /* Execute the directive, do not write line to output file */
470 PrDoDirective (Token, &Next);
471 continue;
475 * If we are currently within the part of an IF/ELSE block that is
476 * FALSE, ignore the line and do not write it to the output file.
477 * This continues until an #else or #endif is encountered.
479 if (AslGbl_IgnoringThisCodeBlock)
481 continue;
484 /* Match and replace all #defined names within this source line */
486 while (Token)
488 DefineInfo = PrMatchDefine (Token);
489 if (DefineInfo)
491 if (DefineInfo->Body)
493 /* This is a macro */
495 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
496 "Matched Macro: %s->%s\n",
497 AslGbl_CurrentLineNumber, DefineInfo->Identifier,
498 DefineInfo->Replacement);
500 PrDoMacroInvocation (AslGbl_MainTokenBuffer, Token,
501 DefineInfo, &Next);
503 else
505 ReplaceString = DefineInfo->Replacement;
507 /* Replace the name in the original line buffer */
509 TokenOffset = Token - AslGbl_MainTokenBuffer + OffsetAdjust;
510 PrReplaceData (
511 &AslGbl_CurrentLineBuffer[TokenOffset], strlen (Token),
512 ReplaceString, strlen (ReplaceString));
514 /* Adjust for length difference between old and new name length */
516 OffsetAdjust += strlen (ReplaceString) - strlen (Token);
518 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
519 "Matched #define: %s->%s\n",
520 AslGbl_CurrentLineNumber, Token,
521 *ReplaceString ? ReplaceString : "(NULL STRING)");
525 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
528 AslGbl_PreprocessorLineNumber++;
531 WriteEntireLine:
533 * Now we can write the possibly modified source line to the
534 * preprocessor file(s).
536 FlWriteFile (ASL_FILE_PREPROCESSOR, AslGbl_CurrentLineBuffer,
537 strlen (AslGbl_CurrentLineBuffer));
542 /*******************************************************************************
544 * FUNCTION: PrDoDirective
546 * PARAMETERS: Directive - Pointer to directive name token
547 * Next - "Next" buffer from GetNextToken
549 * RETURN: None.
551 * DESCRIPTION: Main processing for all preprocessor directives
553 ******************************************************************************/
555 static void
556 PrDoDirective (
557 char *DirectiveToken,
558 char **Next)
560 char *Token = AslGbl_MainTokenBuffer;
561 char *Token2 = NULL;
562 char *End;
563 UINT64 Value;
564 ACPI_SIZE TokenOffset;
565 int Directive;
566 ACPI_STATUS Status;
569 if (!DirectiveToken)
571 goto SyntaxError;
574 Directive = PrMatchDirective (DirectiveToken);
575 if (Directive == ASL_DIRECTIVE_NOT_FOUND)
577 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
578 THIS_TOKEN_OFFSET (DirectiveToken));
580 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
581 "#%s: Unknown directive\n",
582 AslGbl_CurrentLineNumber, DirectiveToken);
583 return;
587 * Emit a line directive into the preprocessor file (.pre) after
588 * every matched directive. This is passed through to the compiler
589 * so that error/warning messages are kept in sync with the
590 * original source file.
592 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\" // #%s\n",
593 AslGbl_CurrentLineNumber, AslGbl_Files[ASL_FILE_INPUT].Filename,
594 AslGbl_DirectiveInfo[Directive].Name);
597 * If we are currently ignoring this block and we encounter a #else or
598 * #elif, we must ignore their blocks also if the parent block is also
599 * being ignored.
601 if (AslGbl_IgnoringThisCodeBlock)
603 switch (Directive)
605 case PR_DIRECTIVE_ELSE:
606 case PR_DIRECTIVE_ELIF:
608 if (AslGbl_DirectiveStack &&
609 AslGbl_DirectiveStack->IgnoringThisCodeBlock)
611 PrDbgPrint ("Ignoring", AslGbl_DirectiveInfo[Directive].Name);
612 return;
614 break;
616 default:
617 break;
622 * Need to always check for #else, #elif, #endif regardless of
623 * whether we are ignoring the current code block, since these
624 * are conditional code block terminators.
626 switch (Directive)
628 case PR_DIRECTIVE_ELSE:
630 AslGbl_IgnoringThisCodeBlock = !(AslGbl_IgnoringThisCodeBlock);
631 PrDbgPrint ("Executing", "else block");
632 return;
634 case PR_DIRECTIVE_ELIF:
636 AslGbl_IgnoringThisCodeBlock = !(AslGbl_IgnoringThisCodeBlock);
637 Directive = PR_DIRECTIVE_IF;
639 if (AslGbl_IgnoringThisCodeBlock == TRUE)
641 /* Not executing the ELSE part -- all done here */
642 PrDbgPrint ("Ignoring", "elif block");
643 return;
647 * After this, we will execute the IF part further below.
648 * First, however, pop off the original #if directive.
650 if (ACPI_FAILURE (PrPopDirective ()))
652 PrError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
653 THIS_TOKEN_OFFSET (DirectiveToken));
656 PrDbgPrint ("Executing", "elif block");
657 break;
659 case PR_DIRECTIVE_ENDIF:
661 PrDbgPrint ("Executing", "endif");
663 /* Pop the owning #if/#ifdef/#ifndef */
665 if (ACPI_FAILURE (PrPopDirective ()))
667 PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
668 THIS_TOKEN_OFFSET (DirectiveToken));
670 return;
672 default:
673 break;
676 /* Most directives have at least one argument */
678 if (AslGbl_DirectiveInfo[Directive].ArgCount >= 1)
680 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
681 if (!Token)
683 goto SyntaxError;
687 if (AslGbl_DirectiveInfo[Directive].ArgCount >= 2)
689 Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
690 if (!Token2)
692 goto SyntaxError;
697 * At this point, if we are ignoring the current code block,
698 * do not process any more directives (i.e., ignore them also.)
699 * For "if" style directives, open/push a new block anyway. We
700 * must do this to keep track of #endif directives
702 if (AslGbl_IgnoringThisCodeBlock)
704 switch (Directive)
706 case PR_DIRECTIVE_IF:
707 case PR_DIRECTIVE_IFDEF:
708 case PR_DIRECTIVE_IFNDEF:
710 PrPushDirective (Directive, Token);
711 PrDbgPrint ("Ignoring", AslGbl_DirectiveInfo[Directive].Name);
712 break;
714 default:
715 break;
718 return;
722 * Execute the directive
724 PrDbgPrint ("Begin execution", AslGbl_DirectiveInfo[Directive].Name);
726 switch (Directive)
728 case PR_DIRECTIVE_IF:
730 TokenOffset = Token - AslGbl_MainTokenBuffer;
732 /* Need to expand #define macros in the expression string first */
734 Status = PrResolveIntegerExpression (
735 &AslGbl_CurrentLineBuffer[TokenOffset-1], &Value);
736 if (ACPI_FAILURE (Status))
738 return;
741 PrPushDirective (Directive, Token);
742 if (!Value)
744 AslGbl_IgnoringThisCodeBlock = TRUE;
747 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
748 "Resolved #if: %8.8X%8.8X %s\n",
749 AslGbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value),
750 AslGbl_IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>");
751 break;
753 case PR_DIRECTIVE_IFDEF:
755 PrPushDirective (Directive, Token);
756 if (!PrMatchDefine (Token))
758 AslGbl_IgnoringThisCodeBlock = TRUE;
761 PrDbgPrint ("Evaluated", "ifdef");
762 break;
764 case PR_DIRECTIVE_IFNDEF:
766 PrPushDirective (Directive, Token);
767 if (PrMatchDefine (Token))
769 AslGbl_IgnoringThisCodeBlock = TRUE;
772 PrDbgPrint ("Evaluated", "ifndef");
773 break;
775 case PR_DIRECTIVE_DEFINE:
777 * By definition, if first char after the name is a paren,
778 * this is a function macro.
780 TokenOffset = Token - AslGbl_MainTokenBuffer + strlen (Token);
781 if (*(&AslGbl_CurrentLineBuffer[TokenOffset]) == '(')
783 #ifndef MACROS_SUPPORTED
784 AcpiOsPrintf (
785 "%s ERROR - line %u: #define macros are not supported yet\n",
786 AslGbl_CurrentLineBuffer, AslGbl_LogicalLineNumber);
787 exit(1);
788 #else
789 PrAddMacro (Token, Next);
790 #endif
792 else
794 /* Use the remainder of the line for the #define */
796 Token2 = *Next;
797 if (Token2)
799 while ((*Token2 == ' ') || (*Token2 == '\t'))
801 Token2++;
804 End = Token2;
805 while (*End != '\n')
807 End++;
810 *End = 0;
812 else
814 Token2 = "";
816 #if 0
817 Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next);
818 if (!Token2)
820 Token2 = "";
822 #endif
823 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
824 "New #define: %s->%s\n",
825 AslGbl_LogicalLineNumber, Token, Token2);
827 PrAddDefine (Token, Token2, FALSE);
829 break;
831 case PR_DIRECTIVE_ERROR:
833 /* Note: No macro expansion */
835 PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE,
836 THIS_TOKEN_OFFSET (Token));
838 AslGbl_SourceLine = 0;
839 AslGbl_NextError = AslGbl_ErrorLog;
840 CmCleanupAndExit ();
841 exit(1);
843 case PR_DIRECTIVE_INCLUDE:
845 Token = PrGetNextToken (NULL, " \"<>", Next);
846 if (!Token)
848 goto SyntaxError;
851 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
852 "Start #include file \"%s\"\n", AslGbl_CurrentLineNumber,
853 Token, AslGbl_CurrentLineNumber);
855 PrDoIncludeFile (Token);
856 break;
858 case PR_DIRECTIVE_INCLUDEBUFFER:
860 Token = PrGetNextToken (NULL, " \"<>", Next);
861 if (!Token)
863 goto SyntaxError;
866 Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
867 if (!Token2)
869 goto SyntaxError;
872 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
873 "Start #includebuffer input from file \"%s\", buffer name %s\n",
874 AslGbl_CurrentLineNumber, Token, Token2);
876 PrDoIncludeBuffer (Token, Token2);
877 break;
879 case PR_DIRECTIVE_LINE:
881 TokenOffset = Token - AslGbl_MainTokenBuffer;
883 Status = PrResolveIntegerExpression (
884 &AslGbl_CurrentLineBuffer[TokenOffset-1], &Value);
885 if (ACPI_FAILURE (Status))
887 return;
890 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
891 "User #line invocation %s\n", AslGbl_CurrentLineNumber,
892 Token);
894 AslGbl_CurrentLineNumber = (UINT32) Value;
896 /* Emit #line into the preprocessor file */
898 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
899 AslGbl_CurrentLineNumber, AslGbl_Files[ASL_FILE_INPUT].Filename);
900 break;
902 case PR_DIRECTIVE_PRAGMA:
904 if (!strcmp (Token, "disable"))
906 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
907 if (!Token)
909 goto SyntaxError;
912 TokenOffset = Token - AslGbl_MainTokenBuffer;
913 AslDisableException (&AslGbl_CurrentLineBuffer[TokenOffset]);
915 else if (!strcmp (Token, "message"))
917 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
918 if (!Token)
920 goto SyntaxError;
923 TokenOffset = Token - AslGbl_MainTokenBuffer;
924 AcpiOsPrintf ("%s\n", &AslGbl_CurrentLineBuffer[TokenOffset]);
926 else
928 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
929 THIS_TOKEN_OFFSET (Token));
930 return;
933 break;
935 case PR_DIRECTIVE_UNDEF:
937 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
938 "#undef: %s\n", AslGbl_CurrentLineNumber, Token);
940 PrRemoveDefine (Token);
941 break;
943 case PR_DIRECTIVE_WARNING:
945 PrError (ASL_WARNING, ASL_MSG_WARNING_DIRECTIVE,
946 THIS_TOKEN_OFFSET (Token));
948 AslGbl_SourceLine = 0;
949 AslGbl_NextError = AslGbl_ErrorLog;
950 break;
952 default:
954 /* Should never get here */
955 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
956 "Unrecognized directive: %u\n",
957 AslGbl_CurrentLineNumber, Directive);
958 break;
961 return;
963 SyntaxError:
965 PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
966 THIS_TOKEN_OFFSET (DirectiveToken));
967 return;
971 /*******************************************************************************
973 * FUNCTION: PrGetNextLine, PrGetNextLineInit
975 * PARAMETERS: Handle - Open file handle for the source file
977 * RETURN: Status of the GetLine operation:
978 * AE_OK - Normal line, OK status
979 * ASL_IGNORE_LINE - Line is blank or part of a multi-line
980 * comment
981 * ASL_EOF - End-of-file reached
983 * DESCRIPTION: Get the next text line from the input file. Does not strip
984 * comments.
986 ******************************************************************************/
988 #define PR_NORMAL_TEXT 0
989 #define PR_MULTI_LINE_COMMENT 1
990 #define PR_SINGLE_LINE_COMMENT 2
991 #define PR_QUOTED_STRING 3
993 static UINT8 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
995 static void
996 PrGetNextLineInit (
997 void)
999 AcpiGbl_LineScanState = 0;
1002 static UINT32
1003 PrGetNextLine (
1004 FILE *Handle)
1006 UINT32 i;
1007 int c = 0;
1008 int PreviousChar;
1011 /* Always clear the global line buffer */
1013 memset (AslGbl_CurrentLineBuffer, 0, AslGbl_LineBufferSize);
1014 for (i = 0; ;)
1017 * If line is too long, expand the line buffers. Also increases
1018 * AslGbl_LineBufferSize.
1020 if (i >= AslGbl_LineBufferSize)
1022 UtExpandLineBuffers ();
1025 PreviousChar = c;
1026 c = getc (Handle);
1027 if (c == EOF)
1030 * On EOF: If there is anything in the line buffer, terminate
1031 * it with a newline, and catch the EOF on the next call
1032 * to this function.
1034 if (i > 0)
1036 AslGbl_CurrentLineBuffer[i] = '\n';
1037 return (AE_OK);
1040 return (ASL_EOF);
1043 /* Update state machine as necessary */
1045 switch (AcpiGbl_LineScanState)
1047 case PR_NORMAL_TEXT:
1049 /* Check for multi-line comment start */
1051 if ((PreviousChar == '/') && (c == '*'))
1053 AcpiGbl_LineScanState = PR_MULTI_LINE_COMMENT;
1056 /* Check for single-line comment start */
1058 else if ((PreviousChar == '/') && (c == '/'))
1060 AcpiGbl_LineScanState = PR_SINGLE_LINE_COMMENT;
1063 /* Check for quoted string start */
1065 else if (PreviousChar == '"')
1067 AcpiGbl_LineScanState = PR_QUOTED_STRING;
1069 break;
1071 case PR_QUOTED_STRING:
1073 if (PreviousChar == '"')
1075 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1077 break;
1079 case PR_MULTI_LINE_COMMENT:
1081 /* Check for multi-line comment end */
1083 if ((PreviousChar == '*') && (c == '/'))
1085 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1087 break;
1089 case PR_SINGLE_LINE_COMMENT: /* Just ignore text until EOL */
1090 default:
1091 break;
1094 /* Always copy the character into line buffer */
1096 AslGbl_CurrentLineBuffer[i] = (char) c;
1097 i++;
1099 /* Always exit on end-of-line */
1101 if (c == '\n')
1103 /* Handle multi-line comments */
1105 if (AcpiGbl_LineScanState == PR_MULTI_LINE_COMMENT)
1107 return (ASL_IGNORE_LINE);
1110 /* End of single-line comment */
1112 if (AcpiGbl_LineScanState == PR_SINGLE_LINE_COMMENT)
1114 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1115 return (AE_OK);
1118 /* Blank line */
1120 if (i == 1)
1122 return (ASL_IGNORE_LINE);
1125 return (AE_OK);
1131 /*******************************************************************************
1133 * FUNCTION: PrMatchDirective
1135 * PARAMETERS: Directive - Pointer to directive name token
1137 * RETURN: Index into command array, -1 if not found
1139 * DESCRIPTION: Lookup the incoming directive in the known directives table.
1141 ******************************************************************************/
1143 static int
1144 PrMatchDirective (
1145 char *Directive)
1147 int i;
1150 if (!Directive || Directive[0] == 0)
1152 return (ASL_DIRECTIVE_NOT_FOUND);
1155 for (i = 0; AslGbl_DirectiveInfo[i].Name; i++)
1157 if (!strcmp (AslGbl_DirectiveInfo[i].Name, Directive))
1159 return (i);
1163 return (ASL_DIRECTIVE_NOT_FOUND); /* Command not recognized */
1167 /*******************************************************************************
1169 * FUNCTION: PrPushDirective
1171 * PARAMETERS: Directive - Encoded directive ID
1172 * Argument - String containing argument to the
1173 * directive
1175 * RETURN: None
1177 * DESCRIPTION: Push an item onto the directive stack. Used for processing
1178 * nested #if/#else type conditional compilation directives.
1179 * Specifically: Used on detection of #if/#ifdef/#ifndef to open
1180 * a block.
1182 ******************************************************************************/
1184 static void
1185 PrPushDirective (
1186 int Directive,
1187 char *Argument)
1189 DIRECTIVE_INFO *Info;
1192 /* Allocate and populate a stack info item */
1194 Info = ACPI_CAST_PTR (DIRECTIVE_INFO,
1195 UtLocalCacheCalloc (sizeof (DIRECTIVE_INFO)));
1197 Info->Next = AslGbl_DirectiveStack;
1198 Info->Directive = Directive;
1199 Info->IgnoringThisCodeBlock = AslGbl_IgnoringThisCodeBlock;
1200 AcpiUtSafeStrncpy (Info->Argument, Argument, MAX_ARGUMENT_LENGTH);
1202 DbgPrint (ASL_DEBUG_OUTPUT,
1203 "Pr(%.4u) - [%u %s] %*s Pushed [#%s %s]: IgnoreFlag = %s\n",
1204 AslGbl_CurrentLineNumber, AslGbl_IfDepth,
1205 AslGbl_IgnoringThisCodeBlock ? "I" : "E",
1206 AslGbl_IfDepth * 4, " ",
1207 AslGbl_DirectiveInfo[Directive].Name,
1208 Argument, AslGbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
1210 /* Push new item */
1212 AslGbl_DirectiveStack = Info;
1213 AslGbl_IfDepth++;
1217 /*******************************************************************************
1219 * FUNCTION: PrPopDirective
1221 * PARAMETERS: None
1223 * RETURN: Status. Error if the stack is empty.
1225 * DESCRIPTION: Pop an item off the directive stack. Used for processing
1226 * nested #if/#else type conditional compilation directives.
1227 * Specifically: Used on detection of #elif and #endif to remove
1228 * the original #if/#ifdef/#ifndef from the stack and close
1229 * the block.
1231 ******************************************************************************/
1233 static ACPI_STATUS
1234 PrPopDirective (
1235 void)
1237 DIRECTIVE_INFO *Info;
1240 /* Check for empty stack */
1242 Info = AslGbl_DirectiveStack;
1243 if (!Info)
1245 return (AE_ERROR);
1248 /* Pop one item, keep globals up-to-date */
1250 AslGbl_IfDepth--;
1251 AslGbl_IgnoringThisCodeBlock = Info->IgnoringThisCodeBlock;
1252 AslGbl_DirectiveStack = Info->Next;
1254 DbgPrint (ASL_DEBUG_OUTPUT,
1255 "Pr(%.4u) - [%u %s] %*s Popped [#%s %s]: IgnoreFlag now = %s\n",
1256 AslGbl_CurrentLineNumber, AslGbl_IfDepth,
1257 AslGbl_IgnoringThisCodeBlock ? "I" : "E",
1258 AslGbl_IfDepth * 4, " ",
1259 AslGbl_DirectiveInfo[Info->Directive].Name,
1260 Info->Argument, AslGbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
1262 ACPI_FREE (Info);
1263 return (AE_OK);
1267 /*******************************************************************************
1269 * FUNCTION: PrDbgPrint
1271 * PARAMETERS: Action - Action being performed
1272 * DirectiveName - Directive being processed
1274 * RETURN: None
1276 * DESCRIPTION: Special debug print for directive processing.
1278 ******************************************************************************/
1280 static void
1281 PrDbgPrint (
1282 char *Action,
1283 char *DirectiveName)
1286 DbgPrint (ASL_DEBUG_OUTPUT, "Pr(%.4u) - [%u %s] "
1287 "%*s %s #%s, IfDepth %u\n",
1288 AslGbl_CurrentLineNumber, AslGbl_IfDepth,
1289 AslGbl_IgnoringThisCodeBlock ? "I" : "E",
1290 AslGbl_IfDepth * 4, " ",
1291 Action, DirectiveName, AslGbl_IfDepth);
1295 /*******************************************************************************
1297 * FUNCTION: PrDoIncludeFile
1299 * PARAMETERS: Pathname - Name of the input file
1301 * RETURN: None.
1303 * DESCRIPTION: Open an include file, from #include.
1305 ******************************************************************************/
1307 static void
1308 PrDoIncludeFile (
1309 char *Pathname)
1311 char *FullPathname;
1314 (void) PrOpenIncludeFile (Pathname, "r", &FullPathname);
1318 /*******************************************************************************
1320 * FUNCTION: PrDoIncludeBuffer
1322 * PARAMETERS: Pathname - Name of the input binary file
1323 * BufferName - ACPI namepath of the buffer
1325 * RETURN: None.
1327 * DESCRIPTION: Create an ACPI buffer object from a binary file. The contents
1328 * of the file are emitted into the buffer object as ascii
1329 * hex data. From #includebuffer.
1331 ******************************************************************************/
1333 static void
1334 PrDoIncludeBuffer (
1335 char *Pathname,
1336 char *BufferName)
1338 char *FullPathname;
1339 FILE *BinaryBufferFile;
1340 UINT32 i = 0;
1341 UINT8 c;
1344 BinaryBufferFile = PrOpenIncludeFile (Pathname, "rb", &FullPathname);
1345 if (!BinaryBufferFile)
1347 return;
1350 /* Emit "Name (XXXX, Buffer() {" header */
1352 FlPrintFile (ASL_FILE_PREPROCESSOR, "Name (%s, Buffer()\n{", BufferName);
1354 /* Dump the entire file in ascii hex format */
1356 while (fread (&c, 1, 1, BinaryBufferFile))
1358 if (!(i % 8))
1360 FlPrintFile (ASL_FILE_PREPROCESSOR, "\n ", c);
1363 FlPrintFile (ASL_FILE_PREPROCESSOR, " 0x%2.2X,", c);
1364 i++;
1367 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
1368 "#includebuffer: read %u bytes from %s\n",
1369 AslGbl_CurrentLineNumber, i, FullPathname);
1371 /* Close the Name() operator */
1373 FlPrintFile (ASL_FILE_PREPROCESSOR, "\n})\n", BufferName);
1374 fclose (BinaryBufferFile);