1 /******************************************************************************
2 *
3 * Module Name: prscan - Preprocessor start-up and file scan module
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2023, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
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.
20 *
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
27 *
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;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
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.
53 *
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.
65 *
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.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
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.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
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.
89 *
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.
98 *
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.
113 *
114 *****************************************************************************
115 *
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
118 *
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.
133 *
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.
145 *
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.
149 *
150 *****************************************************************************/
151
152 #define _DECLARE_PR_GLOBALS
153
154 #include "aslcompiler.h"
155
156 /*
157 * TBDs:
158 *
159 * No nested macros, maybe never
160 * Implement ASL "Include" as well as "#include" here?
161 */
162 #define _COMPONENT ASL_PREPROCESSOR
163 ACPI_MODULE_NAME ("prscan")
164
165
166 /* Local prototypes */
167
168 static void
169 PrPreprocessInputFile (
170 void);
171
172 static void
173 PrDoDirective (
174 char *DirectiveToken,
175 char **Next);
176
177 static void
178 PrGetNextLineInit (
179 void);
180
181 static UINT32
182 PrGetNextLine (
183 FILE *Handle);
184
185 static int
186 PrMatchDirective (
187 char *Directive);
188
189 static void
190 PrPushDirective (
191 int Directive,
192 char *Argument);
193
194 static ACPI_STATUS
195 PrPopDirective (
196 void);
197
198 static void
199 PrDbgPrint (
200 char *Action,
201 char *DirectiveName);
202
203 static void
204 PrDoIncludeBuffer (
205 char *Pathname,
206 char *BufferName);
207
208 static void
209 PrDoIncludeFile (
210 char *Pathname);
211
212
213 /*
214 * Supported preprocessor directives
215 * Each entry is of the form "Name, ArgumentCount"
216 */
217 static const PR_DIRECTIVE_INFO AslGbl_DirectiveInfo[] =
218 {
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}
234 };
235
236 /* This table must match ordering of above table exactly */
237
238 enum Gbl_DirectiveIndexes
239 {
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
254 };
255
256 #define ASL_DIRECTIVE_NOT_FOUND -1
257
258
259 /*******************************************************************************
260 *
261 * FUNCTION: PrInitializePreprocessor
262 *
263 * PARAMETERS: None
264 *
265 * RETURN: None
266 *
267 * DESCRIPTION: Startup initialization for the Preprocessor.
268 *
269 ******************************************************************************/
270
271 void
PrInitializePreprocessor(void)272 PrInitializePreprocessor (
273 void)
274 {
275 /* Init globals and the list of #defines */
276
277 PrInitializeGlobals ();
278 AslGbl_DefineList = NULL;
279 }
280
281
282 /*******************************************************************************
283 *
284 * FUNCTION: PrInitializeGlobals
285 *
286 * PARAMETERS: None
287 *
288 * RETURN: None
289 *
290 * DESCRIPTION: Initialize globals for the Preprocessor. Used for startuup
291 * initialization and re-initialization between compiles during
292 * a multiple source file compile.
293 *
294 ******************************************************************************/
295
296 void
PrInitializeGlobals(void)297 PrInitializeGlobals (
298 void)
299 {
300 /* Init globals */
301
302 AslGbl_InputFileList = NULL;
303 AslGbl_CurrentLineNumber = 1;
304 AslGbl_PreprocessorLineNumber = 1;
305 AslGbl_PreprocessorError = FALSE;
306
307 /* These are used to track #if/#else blocks (possibly nested) */
308
309 AslGbl_IfDepth = 0;
310 AslGbl_IgnoringThisCodeBlock = FALSE;
311 AslGbl_DirectiveStack = NULL;
312 }
313
314
315 /*******************************************************************************
316 *
317 * FUNCTION: PrTerminatePreprocessor
318 *
319 * PARAMETERS: None
320 *
321 * RETURN: None
322 *
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.
326 *
327 ******************************************************************************/
328
329 void
PrTerminatePreprocessor(void)330 PrTerminatePreprocessor (
331 void)
332 {
333 PR_DEFINE_INFO *DefineInfo;
334
335
336 /*
337 * The persistent defines (created on the command line) are always at the
338 * end of the list. We save them.
339 */
340 while ((AslGbl_DefineList) && (!AslGbl_DefineList->Persist))
341 {
342 DefineInfo = AslGbl_DefineList;
343 AslGbl_DefineList = DefineInfo->Next;
344
345 ACPI_FREE (DefineInfo->Replacement);
346 ACPI_FREE (DefineInfo->Identifier);
347 ACPI_FREE (DefineInfo);
348 }
349 }
350
351
352 /*******************************************************************************
353 *
354 * FUNCTION: PrDoPreprocess
355 *
356 * PARAMETERS: None
357 *
358 * RETURN: None
359 *
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.
363 *
364 ******************************************************************************/
365
366 void
PrDoPreprocess(void)367 PrDoPreprocess (
368 void)
369 {
370 BOOLEAN MoreInputFiles;
371
372
373 DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
374
375
376 FlSeekFile (ASL_FILE_INPUT, 0);
377 PrDumpPredefinedNames ();
378
379 /* Main preprocessor loop, handles include files */
380
381 do
382 {
383 PrPreprocessInputFile ();
384 MoreInputFiles = PrPopInputFileStack ();
385
386 } while (MoreInputFiles);
387
388 /* Point compiler input to the new preprocessor output file (.pre) */
389
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;
393
394 /* Reset globals to allow compiler to run */
395
396 FlSeekFile (ASL_FILE_INPUT, 0);
397 if (!AslGbl_PreprocessOnly)
398 {
399 AslGbl_CurrentLineNumber = 0;
400 }
401
402 DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
403 }
404
405
406 /*******************************************************************************
407 *
408 * FUNCTION: PrPreprocessInputFile
409 *
410 * PARAMETERS: None
411 *
412 * RETURN: None
413 *
414 * DESCRIPTION: Preprocess one entire file, line-by-line.
415 *
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
419 *
420 ******************************************************************************/
421
422 static void
PrPreprocessInputFile(void)423 PrPreprocessInputFile (
424 void)
425 {
426 UINT32 Status;
427 char *Token;
428 char *ReplaceString;
429 PR_DEFINE_INFO *DefineInfo;
430 ACPI_SIZE TokenOffset;
431 char *Next;
432 int OffsetAdjust;
433
434
435 PrGetNextLineInit ();
436
437 /* Scan source line-by-line and process directives. Then write the .i file */
438
439 while ((Status = PrGetNextLine (AslGbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
440 {
441 AslGbl_CurrentLineNumber++;
442 AslGbl_LogicalLineNumber++;
443
444 if (Status == ASL_IGNORE_LINE)
445 {
446 goto WriteEntireLine;
447 }
448
449 /* Need a copy of the input line for strok() */
450
451 strcpy (AslGbl_MainTokenBuffer, AslGbl_CurrentLineBuffer);
452 Token = PrGetNextToken (AslGbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
453 OffsetAdjust = 0;
454
455 /* All preprocessor directives must begin with '#' */
456
457 if (Token && (*Token == '#'))
458 {
459 if (strlen (Token) == 1)
460 {
461 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
462 }
463 else
464 {
465 Token++; /* Skip leading # */
466 }
467
468 /* Execute the directive, do not write line to output file */
469
470 PrDoDirective (Token, &Next);
471 continue;
472 }
473
474 /*
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.
478 */
479 if (AslGbl_IgnoringThisCodeBlock)
480 {
481 continue;
482 }
483
484 /* Match and replace all #defined names within this source line */
485
486 while (Token)
487 {
488 DefineInfo = PrMatchDefine (Token);
489 if (DefineInfo)
490 {
491 if (DefineInfo->Body)
492 {
493 /* This is a macro */
494
495 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
496 "Matched Macro: %s->%s\n",
497 AslGbl_CurrentLineNumber, DefineInfo->Identifier,
498 DefineInfo->Replacement);
499
500 PrDoMacroInvocation (AslGbl_MainTokenBuffer, Token,
501 DefineInfo, &Next);
502 }
503 else
504 {
505 ReplaceString = DefineInfo->Replacement;
506
507 /* Replace the name in the original line buffer */
508
509 TokenOffset = Token - AslGbl_MainTokenBuffer + OffsetAdjust;
510 PrReplaceData (
511 &AslGbl_CurrentLineBuffer[TokenOffset], strlen (Token),
512 ReplaceString, strlen (ReplaceString));
513
514 /* Adjust for length difference between old and new name length */
515
516 OffsetAdjust += strlen (ReplaceString) - strlen (Token);
517
518 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
519 "Matched #define: %s->%s\n",
520 AslGbl_CurrentLineNumber, Token,
521 *ReplaceString ? ReplaceString : "(NULL STRING)");
522 }
523 }
524
525 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
526 }
527
528 AslGbl_PreprocessorLineNumber++;
529
530
531 WriteEntireLine:
532 /*
533 * Now we can write the possibly modified source line to the
534 * preprocessor file(s).
535 */
536 FlWriteFile (ASL_FILE_PREPROCESSOR, AslGbl_CurrentLineBuffer,
537 strlen (AslGbl_CurrentLineBuffer));
538 }
539 }
540
541
542 /*******************************************************************************
543 *
544 * FUNCTION: PrDoDirective
545 *
546 * PARAMETERS: Directive - Pointer to directive name token
547 * Next - "Next" buffer from GetNextToken
548 *
549 * RETURN: None.
550 *
551 * DESCRIPTION: Main processing for all preprocessor directives
552 *
553 ******************************************************************************/
554
555 static void
PrDoDirective(char * DirectiveToken,char ** Next)556 PrDoDirective (
557 char *DirectiveToken,
558 char **Next)
559 {
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;
567
568
569 if (!DirectiveToken)
570 {
571 goto SyntaxError;
572 }
573
574 Directive = PrMatchDirective (DirectiveToken);
575 if (Directive == ASL_DIRECTIVE_NOT_FOUND)
576 {
577 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
578 THIS_TOKEN_OFFSET (DirectiveToken));
579
580 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
581 "#%s: Unknown directive\n",
582 AslGbl_CurrentLineNumber, DirectiveToken);
583 return;
584 }
585
586 /*
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.
591 */
592 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\" // #%s\n",
593 AslGbl_CurrentLineNumber, AslGbl_Files[ASL_FILE_INPUT].Filename,
594 AslGbl_DirectiveInfo[Directive].Name);
595
596 /*
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.
600 */
601 if (AslGbl_IgnoringThisCodeBlock)
602 {
603 switch (Directive)
604 {
605 case PR_DIRECTIVE_ELSE:
606 case PR_DIRECTIVE_ELIF:
607
608 if (AslGbl_DirectiveStack &&
609 AslGbl_DirectiveStack->IgnoringThisCodeBlock)
610 {
611 PrDbgPrint ("Ignoring", AslGbl_DirectiveInfo[Directive].Name);
612 return;
613 }
614 break;
615
616 default:
617 break;
618 }
619 }
620
621 /*
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.
625 */
626 switch (Directive)
627 {
628 case PR_DIRECTIVE_ELSE:
629
630 AslGbl_IgnoringThisCodeBlock = !(AslGbl_IgnoringThisCodeBlock);
631 PrDbgPrint ("Executing", "else block");
632 return;
633
634 case PR_DIRECTIVE_ELIF:
635
636 AslGbl_IgnoringThisCodeBlock = !(AslGbl_IgnoringThisCodeBlock);
637 Directive = PR_DIRECTIVE_IF;
638
639 if (AslGbl_IgnoringThisCodeBlock == TRUE)
640 {
641 /* Not executing the ELSE part -- all done here */
642 PrDbgPrint ("Ignoring", "elif block");
643 return;
644 }
645
646 /*
647 * After this, we will execute the IF part further below.
648 * First, however, pop off the original #if directive.
649 */
650 if (ACPI_FAILURE (PrPopDirective ()))
651 {
652 PrError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
653 THIS_TOKEN_OFFSET (DirectiveToken));
654 }
655
656 PrDbgPrint ("Executing", "elif block");
657 break;
658
659 case PR_DIRECTIVE_ENDIF:
660
661 PrDbgPrint ("Executing", "endif");
662
663 /* Pop the owning #if/#ifdef/#ifndef */
664
665 if (ACPI_FAILURE (PrPopDirective ()))
666 {
667 PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
668 THIS_TOKEN_OFFSET (DirectiveToken));
669 }
670 return;
671
672 default:
673 break;
674 }
675
676 /* Most directives have at least one argument */
677
678 if (AslGbl_DirectiveInfo[Directive].ArgCount >= 1)
679 {
680 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
681 if (!Token)
682 {
683 goto SyntaxError;
684 }
685 }
686
687 if (AslGbl_DirectiveInfo[Directive].ArgCount >= 2)
688 {
689 Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
690 if (!Token2)
691 {
692 goto SyntaxError;
693 }
694 }
695
696 /*
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
701 */
702 if (AslGbl_IgnoringThisCodeBlock)
703 {
704 switch (Directive)
705 {
706 case PR_DIRECTIVE_IF:
707 case PR_DIRECTIVE_IFDEF:
708 case PR_DIRECTIVE_IFNDEF:
709
710 PrPushDirective (Directive, Token);
711 PrDbgPrint ("Ignoring", AslGbl_DirectiveInfo[Directive].Name);
712 break;
713
714 default:
715 break;
716 }
717
718 return;
719 }
720
721 /*
722 * Execute the directive
723 */
724 PrDbgPrint ("Begin execution", AslGbl_DirectiveInfo[Directive].Name);
725
726 switch (Directive)
727 {
728 case PR_DIRECTIVE_IF:
729
730 TokenOffset = Token - AslGbl_MainTokenBuffer;
731
732 /* Need to expand #define macros in the expression string first */
733
734 Status = PrResolveIntegerExpression (
735 &AslGbl_CurrentLineBuffer[TokenOffset-1], &Value);
736 if (ACPI_FAILURE (Status))
737 {
738 return;
739 }
740
741 PrPushDirective (Directive, Token);
742 if (!Value)
743 {
744 AslGbl_IgnoringThisCodeBlock = TRUE;
745 }
746
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;
752
753 case PR_DIRECTIVE_IFDEF:
754
755 PrPushDirective (Directive, Token);
756 if (!PrMatchDefine (Token))
757 {
758 AslGbl_IgnoringThisCodeBlock = TRUE;
759 }
760
761 PrDbgPrint ("Evaluated", "ifdef");
762 break;
763
764 case PR_DIRECTIVE_IFNDEF:
765
766 PrPushDirective (Directive, Token);
767 if (PrMatchDefine (Token))
768 {
769 AslGbl_IgnoringThisCodeBlock = TRUE;
770 }
771
772 PrDbgPrint ("Evaluated", "ifndef");
773 break;
774
775 case PR_DIRECTIVE_DEFINE:
776 /*
777 * By definition, if first char after the name is a paren,
778 * this is a function macro.
779 */
780 TokenOffset = Token - AslGbl_MainTokenBuffer + strlen (Token);
781 if (*(&AslGbl_CurrentLineBuffer[TokenOffset]) == '(')
782 {
783
784 #ifdef MACROS_SUPPORTED
785 AcpiOsPrintf(
786 "%s ERROR - line %u: #define macros are not supported yet\n",
787 AslGbl_CurrentLineBuffer, AslGbl_LogicalLineNumber);
788 exit(1);
789 #else
790 PrAddMacro (Token, Next);
791 #endif
792 }
793
794
795 else
796 {
797 /* Use the remainder of the line for the #define */
798
799 Token2 = *Next;
800 if (Token2)
801 {
802 while ((*Token2 == ' ') || (*Token2 == '\t'))
803 {
804 Token2++;
805 }
806
807 End = Token2;
808 while (*End != '\n')
809 {
810 End++;
811 }
812
813 *End = 0;
814 }
815 else
816 {
817 Token2 = "";
818 }
819 #if 0
820 Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next);
821 if (!Token2)
822 {
823 Token2 = "";
824 }
825 #endif
826 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
827 "New #define: %s->%s\n",
828 AslGbl_LogicalLineNumber, Token, Token2);
829
830 PrAddDefine (Token, Token2, FALSE);
831 }
832 break;
833
834 case PR_DIRECTIVE_ERROR:
835
836 /* Note: No macro expansion */
837
838 PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE,
839 THIS_TOKEN_OFFSET (Token));
840
841 AslGbl_SourceLine = 0;
842 AslGbl_NextError = AslGbl_ErrorLog;
843 CmCleanupAndExit ();
844 exit(1);
845
846 case PR_DIRECTIVE_INCLUDE:
847
848 Token = PrGetNextToken (NULL, " \"<>", Next);
849 if (!Token)
850 {
851 goto SyntaxError;
852 }
853
854 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
855 "Start #include file \"%s\"\n", AslGbl_CurrentLineNumber,
856 Token);
857
858 PrDoIncludeFile (Token);
859 break;
860
861 case PR_DIRECTIVE_INCLUDEBUFFER:
862
863 Token = PrGetNextToken (NULL, " \"<>", Next);
864 if (!Token)
865 {
866 goto SyntaxError;
867 }
868
869 Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
870 if (!Token2)
871 {
872 goto SyntaxError;
873 }
874
875 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
876 "Start #includebuffer input from file \"%s\", buffer name %s\n",
877 AslGbl_CurrentLineNumber, Token, Token2);
878
879 PrDoIncludeBuffer (Token, Token2);
880 break;
881
882 case PR_DIRECTIVE_LINE:
883
884 TokenOffset = Token - AslGbl_MainTokenBuffer;
885
886 Status = PrResolveIntegerExpression (
887 &AslGbl_CurrentLineBuffer[TokenOffset-1], &Value);
888 if (ACPI_FAILURE (Status))
889 {
890 return;
891 }
892
893 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
894 "User #line invocation %s\n", AslGbl_CurrentLineNumber,
895 Token);
896
897 AslGbl_CurrentLineNumber = (UINT32) Value;
898
899 /* Emit #line into the preprocessor file */
900
901 FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
902 AslGbl_CurrentLineNumber, AslGbl_Files[ASL_FILE_INPUT].Filename);
903 break;
904
905 case PR_DIRECTIVE_PRAGMA:
906
907 if (!strcmp (Token, "disable"))
908 {
909 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
910 if (!Token)
911 {
912 goto SyntaxError;
913 }
914
915 TokenOffset = Token - AslGbl_MainTokenBuffer;
916 AslDisableException (&AslGbl_CurrentLineBuffer[TokenOffset]);
917 }
918 else if (!strcmp (Token, "message"))
919 {
920 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
921 if (!Token)
922 {
923 goto SyntaxError;
924 }
925
926 TokenOffset = Token - AslGbl_MainTokenBuffer;
927 AcpiOsPrintf ("%s\n", &AslGbl_CurrentLineBuffer[TokenOffset]);
928 }
929 else
930 {
931 PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
932 THIS_TOKEN_OFFSET (Token));
933 return;
934 }
935
936 break;
937
938 case PR_DIRECTIVE_UNDEF:
939
940 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
941 "#undef: %s\n", AslGbl_CurrentLineNumber, Token);
942
943 PrRemoveDefine (Token);
944 break;
945
946 case PR_DIRECTIVE_WARNING:
947
948 PrError (ASL_WARNING, ASL_MSG_WARNING_DIRECTIVE,
949 THIS_TOKEN_OFFSET (Token));
950
951 AslGbl_SourceLine = 0;
952 AslGbl_NextError = AslGbl_ErrorLog;
953 break;
954
955 default:
956
957 /* Should never get here */
958 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
959 "Unrecognized directive: %u\n",
960 AslGbl_CurrentLineNumber, Directive);
961 break;
962 }
963
964 return;
965
966 SyntaxError:
967
968 PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
969 THIS_TOKEN_OFFSET (DirectiveToken));
970 return;
971 }
972
973
974 /*******************************************************************************
975 *
976 * FUNCTION: PrGetNextLine, PrGetNextLineInit
977 *
978 * PARAMETERS: Handle - Open file handle for the source file
979 *
980 * RETURN: Status of the GetLine operation:
981 * AE_OK - Normal line, OK status
982 * ASL_IGNORE_LINE - Line is blank or part of a multi-line
983 * comment
984 * ASL_EOF - End-of-file reached
985 *
986 * DESCRIPTION: Get the next text line from the input file. Does not strip
987 * comments.
988 *
989 ******************************************************************************/
990
991 #define PR_NORMAL_TEXT 0
992 #define PR_MULTI_LINE_COMMENT 1
993 #define PR_SINGLE_LINE_COMMENT 2
994 #define PR_QUOTED_STRING 3
995
996 static UINT8 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
997
998 static void
PrGetNextLineInit(void)999 PrGetNextLineInit (
1000 void)
1001 {
1002 AcpiGbl_LineScanState = 0;
1003 }
1004
1005 static UINT32
PrGetNextLine(FILE * Handle)1006 PrGetNextLine (
1007 FILE *Handle)
1008 {
1009 UINT32 i;
1010 int c = 0;
1011 int PreviousChar;
1012
1013
1014 /* Always clear the global line buffer */
1015
1016 memset (AslGbl_CurrentLineBuffer, 0, AslGbl_LineBufferSize);
1017 for (i = 0; ;)
1018 {
1019 /*
1020 * If line is too long, expand the line buffers. Also increases
1021 * AslGbl_LineBufferSize.
1022 */
1023 if (i >= AslGbl_LineBufferSize)
1024 {
1025 UtExpandLineBuffers ();
1026 }
1027
1028 PreviousChar = c;
1029 c = getc (Handle);
1030 if (c == EOF)
1031 {
1032 /*
1033 * On EOF: If there is anything in the line buffer, terminate
1034 * it with a newline, and catch the EOF on the next call
1035 * to this function.
1036 */
1037 if (i > 0)
1038 {
1039 AslGbl_CurrentLineBuffer[i] = '\n';
1040 return (AE_OK);
1041 }
1042
1043 return (ASL_EOF);
1044 }
1045
1046 /* Update state machine as necessary */
1047
1048 switch (AcpiGbl_LineScanState)
1049 {
1050 case PR_NORMAL_TEXT:
1051
1052 /* Check for multi-line comment start */
1053
1054 if ((PreviousChar == '/') && (c == '*'))
1055 {
1056 AcpiGbl_LineScanState = PR_MULTI_LINE_COMMENT;
1057 }
1058
1059 /* Check for single-line comment start */
1060
1061 else if ((PreviousChar == '/') && (c == '/'))
1062 {
1063 AcpiGbl_LineScanState = PR_SINGLE_LINE_COMMENT;
1064 }
1065
1066 /* Check for quoted string start */
1067
1068 else if (PreviousChar == '"')
1069 {
1070 AcpiGbl_LineScanState = PR_QUOTED_STRING;
1071 }
1072 break;
1073
1074 case PR_QUOTED_STRING:
1075
1076 if (PreviousChar == '"')
1077 {
1078 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1079 }
1080 break;
1081
1082 case PR_MULTI_LINE_COMMENT:
1083
1084 /* Check for multi-line comment end */
1085
1086 if ((PreviousChar == '*') && (c == '/'))
1087 {
1088 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1089 }
1090 break;
1091
1092 case PR_SINGLE_LINE_COMMENT: /* Just ignore text until EOL */
1093 default:
1094 break;
1095 }
1096
1097 /* Always copy the character into line buffer */
1098
1099 AslGbl_CurrentLineBuffer[i] = (char) c;
1100 i++;
1101
1102 /* Always exit on end-of-line */
1103
1104 if (c == '\n')
1105 {
1106 /* Handle multi-line comments */
1107
1108 if (AcpiGbl_LineScanState == PR_MULTI_LINE_COMMENT)
1109 {
1110 return (ASL_IGNORE_LINE);
1111 }
1112
1113 /* End of single-line comment */
1114
1115 if (AcpiGbl_LineScanState == PR_SINGLE_LINE_COMMENT)
1116 {
1117 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1118 return (AE_OK);
1119 }
1120
1121 /* Blank line */
1122
1123 if (i == 1)
1124 {
1125 return (ASL_IGNORE_LINE);
1126 }
1127
1128 return (AE_OK);
1129 }
1130 }
1131 }
1132
1133
1134 /*******************************************************************************
1135 *
1136 * FUNCTION: PrMatchDirective
1137 *
1138 * PARAMETERS: Directive - Pointer to directive name token
1139 *
1140 * RETURN: Index into command array, -1 if not found
1141 *
1142 * DESCRIPTION: Lookup the incoming directive in the known directives table.
1143 *
1144 ******************************************************************************/
1145
1146 static int
PrMatchDirective(char * Directive)1147 PrMatchDirective (
1148 char *Directive)
1149 {
1150 int i;
1151
1152
1153 if (!Directive || Directive[0] == 0)
1154 {
1155 return (ASL_DIRECTIVE_NOT_FOUND);
1156 }
1157
1158 for (i = 0; AslGbl_DirectiveInfo[i].Name; i++)
1159 {
1160 if (!strcmp (AslGbl_DirectiveInfo[i].Name, Directive))
1161 {
1162 return (i);
1163 }
1164 }
1165
1166 return (ASL_DIRECTIVE_NOT_FOUND); /* Command not recognized */
1167 }
1168
1169
1170 /*******************************************************************************
1171 *
1172 * FUNCTION: PrPushDirective
1173 *
1174 * PARAMETERS: Directive - Encoded directive ID
1175 * Argument - String containing argument to the
1176 * directive
1177 *
1178 * RETURN: None
1179 *
1180 * DESCRIPTION: Push an item onto the directive stack. Used for processing
1181 * nested #if/#else type conditional compilation directives.
1182 * Specifically: Used on detection of #if/#ifdef/#ifndef to open
1183 * a block.
1184 *
1185 ******************************************************************************/
1186
1187 static void
PrPushDirective(int Directive,char * Argument)1188 PrPushDirective (
1189 int Directive,
1190 char *Argument)
1191 {
1192 DIRECTIVE_INFO *Info;
1193
1194
1195 /* Allocate and populate a stack info item */
1196
1197 Info = ACPI_CAST_PTR (DIRECTIVE_INFO,
1198 UtLocalCacheCalloc (sizeof (DIRECTIVE_INFO)));
1199
1200 Info->Next = AslGbl_DirectiveStack;
1201 Info->Directive = Directive;
1202 Info->IgnoringThisCodeBlock = AslGbl_IgnoringThisCodeBlock;
1203 AcpiUtSafeStrncpy (Info->Argument, Argument, MAX_ARGUMENT_LENGTH);
1204
1205 DbgPrint (ASL_DEBUG_OUTPUT,
1206 "Pr(%.4u) - [%u %s] %*s Pushed [#%s %s]: IgnoreFlag = %s\n",
1207 AslGbl_CurrentLineNumber, AslGbl_IfDepth,
1208 AslGbl_IgnoringThisCodeBlock ? "I" : "E",
1209 AslGbl_IfDepth * 4, " ",
1210 AslGbl_DirectiveInfo[Directive].Name,
1211 Argument, AslGbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
1212
1213 /* Push new item */
1214
1215 AslGbl_DirectiveStack = Info;
1216 AslGbl_IfDepth++;
1217 }
1218
1219
1220 /*******************************************************************************
1221 *
1222 * FUNCTION: PrPopDirective
1223 *
1224 * PARAMETERS: None
1225 *
1226 * RETURN: Status. Error if the stack is empty.
1227 *
1228 * DESCRIPTION: Pop an item off the directive stack. Used for processing
1229 * nested #if/#else type conditional compilation directives.
1230 * Specifically: Used on detection of #elif and #endif to remove
1231 * the original #if/#ifdef/#ifndef from the stack and close
1232 * the block.
1233 *
1234 ******************************************************************************/
1235
1236 static ACPI_STATUS
PrPopDirective(void)1237 PrPopDirective (
1238 void)
1239 {
1240 DIRECTIVE_INFO *Info;
1241
1242
1243 /* Check for empty stack */
1244
1245 Info = AslGbl_DirectiveStack;
1246 if (!Info)
1247 {
1248 return (AE_ERROR);
1249 }
1250
1251 /* Pop one item, keep globals up-to-date */
1252
1253 AslGbl_IfDepth--;
1254 AslGbl_IgnoringThisCodeBlock = Info->IgnoringThisCodeBlock;
1255 AslGbl_DirectiveStack = Info->Next;
1256
1257 DbgPrint (ASL_DEBUG_OUTPUT,
1258 "Pr(%.4u) - [%u %s] %*s Popped [#%s %s]: IgnoreFlag now = %s\n",
1259 AslGbl_CurrentLineNumber, AslGbl_IfDepth,
1260 AslGbl_IgnoringThisCodeBlock ? "I" : "E",
1261 AslGbl_IfDepth * 4, " ",
1262 AslGbl_DirectiveInfo[Info->Directive].Name,
1263 Info->Argument, AslGbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
1264
1265 return (AE_OK);
1266 }
1267
1268
1269 /*******************************************************************************
1270 *
1271 * FUNCTION: PrDbgPrint
1272 *
1273 * PARAMETERS: Action - Action being performed
1274 * DirectiveName - Directive being processed
1275 *
1276 * RETURN: None
1277 *
1278 * DESCRIPTION: Special debug print for directive processing.
1279 *
1280 ******************************************************************************/
1281
1282 static void
PrDbgPrint(char * Action,char * DirectiveName)1283 PrDbgPrint (
1284 char *Action,
1285 char *DirectiveName)
1286 {
1287
1288 DbgPrint (ASL_DEBUG_OUTPUT, "Pr(%.4u) - [%u %s] "
1289 "%*s %s #%s, IfDepth %u\n",
1290 AslGbl_CurrentLineNumber, AslGbl_IfDepth,
1291 AslGbl_IgnoringThisCodeBlock ? "I" : "E",
1292 AslGbl_IfDepth * 4, " ",
1293 Action, DirectiveName, AslGbl_IfDepth);
1294 }
1295
1296
1297 /*******************************************************************************
1298 *
1299 * FUNCTION: PrDoIncludeFile
1300 *
1301 * PARAMETERS: Pathname - Name of the input file
1302 *
1303 * RETURN: None.
1304 *
1305 * DESCRIPTION: Open an include file, from #include.
1306 *
1307 ******************************************************************************/
1308
1309 static void
PrDoIncludeFile(char * Pathname)1310 PrDoIncludeFile (
1311 char *Pathname)
1312 {
1313 char *FullPathname;
1314
1315
1316 (void) PrOpenIncludeFile (Pathname, "r", &FullPathname);
1317 }
1318
1319
1320 /*******************************************************************************
1321 *
1322 * FUNCTION: PrDoIncludeBuffer
1323 *
1324 * PARAMETERS: Pathname - Name of the input binary file
1325 * BufferName - ACPI namepath of the buffer
1326 *
1327 * RETURN: None.
1328 *
1329 * DESCRIPTION: Create an ACPI buffer object from a binary file. The contents
1330 * of the file are emitted into the buffer object as ascii
1331 * hex data. From #includebuffer.
1332 *
1333 ******************************************************************************/
1334
1335 static void
PrDoIncludeBuffer(char * Pathname,char * BufferName)1336 PrDoIncludeBuffer (
1337 char *Pathname,
1338 char *BufferName)
1339 {
1340 char *FullPathname;
1341 FILE *BinaryBufferFile;
1342 UINT32 i = 0;
1343 UINT8 c;
1344
1345
1346 BinaryBufferFile = PrOpenIncludeFile (Pathname, "rb", &FullPathname);
1347 if (!BinaryBufferFile)
1348 {
1349 return;
1350 }
1351
1352 /* Emit "Name (XXXX, Buffer() {" header */
1353
1354 FlPrintFile (ASL_FILE_PREPROCESSOR, "Name (%s, Buffer()\n{", BufferName);
1355
1356 /* Dump the entire file in ascii hex format */
1357
1358 while (fread (&c, 1, 1, BinaryBufferFile))
1359 {
1360 if (!(i % 8))
1361 {
1362 FlPrintFile (ASL_FILE_PREPROCESSOR, "\n ");
1363 }
1364
1365 FlPrintFile (ASL_FILE_PREPROCESSOR, " 0x%2.2X,", c);
1366 i++;
1367 }
1368
1369 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
1370 "#includebuffer: read %u bytes from %s\n",
1371 AslGbl_CurrentLineNumber, i, FullPathname);
1372
1373 /* Close the Name() operator */
1374
1375 FlPrintFile (ASL_FILE_PREPROCESSOR, "\n})\n");
1376 fclose (BinaryBufferFile);
1377 }
1378