xref: /illumos-gate/usr/src/cmd/acpi/iasl/prscan.c (revision 35786f68)
1bc36eafdSMike Gerdts /******************************************************************************
2bc36eafdSMike Gerdts  *
3bc36eafdSMike Gerdts  * Module Name: prscan - Preprocessor start-up and file scan module
4bc36eafdSMike Gerdts  *
5bc36eafdSMike Gerdts  *****************************************************************************/
6bc36eafdSMike Gerdts 
7*35786f68SRobert Mustacchi /******************************************************************************
8*35786f68SRobert Mustacchi  *
9*35786f68SRobert Mustacchi  * 1. Copyright Notice
10*35786f68SRobert Mustacchi  *
11*35786f68SRobert Mustacchi  * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12bc36eafdSMike Gerdts  * All rights reserved.
13bc36eafdSMike Gerdts  *
14*35786f68SRobert Mustacchi  * 2. License
15*35786f68SRobert Mustacchi  *
16*35786f68SRobert Mustacchi  * 2.1. This is your license from Intel Corp. under its intellectual property
17*35786f68SRobert Mustacchi  * rights. You may have additional license terms from the party that provided
18*35786f68SRobert Mustacchi  * you this software, covering your right to use that party's intellectual
19*35786f68SRobert Mustacchi  * property rights.
20*35786f68SRobert Mustacchi  *
21*35786f68SRobert Mustacchi  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22*35786f68SRobert Mustacchi  * copy of the source code appearing in this file ("Covered Code") an
23*35786f68SRobert Mustacchi  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24*35786f68SRobert Mustacchi  * base code distributed originally by Intel ("Original Intel Code") to copy,
25*35786f68SRobert Mustacchi  * make derivatives, distribute, use and display any portion of the Covered
26*35786f68SRobert Mustacchi  * Code in any form, with the right to sublicense such rights; and
27*35786f68SRobert Mustacchi  *
28*35786f68SRobert Mustacchi  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29*35786f68SRobert Mustacchi  * license (with the right to sublicense), under only those claims of Intel
30*35786f68SRobert Mustacchi  * patents that are infringed by the Original Intel Code, to make, use, sell,
31*35786f68SRobert Mustacchi  * offer to sell, and import the Covered Code and derivative works thereof
32*35786f68SRobert Mustacchi  * solely to the minimum extent necessary to exercise the above copyright
33*35786f68SRobert Mustacchi  * license, and in no event shall the patent license extend to any additions
34*35786f68SRobert Mustacchi  * to or modifications of the Original Intel Code. No other license or right
35*35786f68SRobert Mustacchi  * is granted directly or by implication, estoppel or otherwise;
36*35786f68SRobert Mustacchi  *
37*35786f68SRobert Mustacchi  * The above copyright and patent license is granted only if the following
38*35786f68SRobert Mustacchi  * conditions are met:
39*35786f68SRobert Mustacchi  *
40*35786f68SRobert Mustacchi  * 3. Conditions
41*35786f68SRobert Mustacchi  *
42*35786f68SRobert Mustacchi  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43*35786f68SRobert Mustacchi  * Redistribution of source code of any substantial portion of the Covered
44*35786f68SRobert Mustacchi  * Code or modification with rights to further distribute source must include
45*35786f68SRobert Mustacchi  * the above Copyright Notice, the above License, this list of Conditions,
46*35786f68SRobert Mustacchi  * and the following Disclaimer and Export Compliance provision. In addition,
47*35786f68SRobert Mustacchi  * Licensee must cause all Covered Code to which Licensee contributes to
48*35786f68SRobert Mustacchi  * contain a file documenting the changes Licensee made to create that Covered
49*35786f68SRobert Mustacchi  * Code and the date of any change. Licensee must include in that file the
50*35786f68SRobert Mustacchi  * documentation of any changes made by any predecessor Licensee. Licensee
51*35786f68SRobert Mustacchi  * must include a prominent statement that the modification is derived,
52*35786f68SRobert Mustacchi  * directly or indirectly, from Original Intel Code.
53*35786f68SRobert Mustacchi  *
54*35786f68SRobert Mustacchi  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55*35786f68SRobert Mustacchi  * Redistribution of source code of any substantial portion of the Covered
56*35786f68SRobert Mustacchi  * Code or modification without rights to further distribute source must
57*35786f68SRobert Mustacchi  * include the following Disclaimer and Export Compliance provision in the
58*35786f68SRobert Mustacchi  * documentation and/or other materials provided with distribution. In
59*35786f68SRobert Mustacchi  * addition, Licensee may not authorize further sublicense of source of any
60*35786f68SRobert Mustacchi  * portion of the Covered Code, and must include terms to the effect that the
61*35786f68SRobert Mustacchi  * license from Licensee to its licensee is limited to the intellectual
62*35786f68SRobert Mustacchi  * property embodied in the software Licensee provides to its licensee, and
63*35786f68SRobert Mustacchi  * not to intellectual property embodied in modifications its licensee may
64*35786f68SRobert Mustacchi  * make.
65*35786f68SRobert Mustacchi  *
66*35786f68SRobert Mustacchi  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67*35786f68SRobert Mustacchi  * substantial portion of the Covered Code or modification must reproduce the
68*35786f68SRobert Mustacchi  * above Copyright Notice, and the following Disclaimer and Export Compliance
69*35786f68SRobert Mustacchi  * provision in the documentation and/or other materials provided with the
70*35786f68SRobert Mustacchi  * distribution.
71*35786f68SRobert Mustacchi  *
72*35786f68SRobert Mustacchi  * 3.4. Intel retains all right, title, and interest in and to the Original
73*35786f68SRobert Mustacchi  * Intel Code.
74*35786f68SRobert Mustacchi  *
75*35786f68SRobert Mustacchi  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76*35786f68SRobert Mustacchi  * Intel shall be used in advertising or otherwise to promote the sale, use or
77*35786f68SRobert Mustacchi  * other dealings in products derived from or relating to the Covered Code
78*35786f68SRobert Mustacchi  * without prior written authorization from Intel.
79*35786f68SRobert Mustacchi  *
80*35786f68SRobert Mustacchi  * 4. Disclaimer and Export Compliance
81*35786f68SRobert Mustacchi  *
82*35786f68SRobert Mustacchi  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83*35786f68SRobert Mustacchi  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84*35786f68SRobert Mustacchi  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85*35786f68SRobert Mustacchi  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86*35786f68SRobert Mustacchi  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87*35786f68SRobert Mustacchi  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88*35786f68SRobert Mustacchi  * PARTICULAR PURPOSE.
89*35786f68SRobert Mustacchi  *
90*35786f68SRobert Mustacchi  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91*35786f68SRobert Mustacchi  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92*35786f68SRobert Mustacchi  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93*35786f68SRobert Mustacchi  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94*35786f68SRobert Mustacchi  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95*35786f68SRobert Mustacchi  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96*35786f68SRobert Mustacchi  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97*35786f68SRobert Mustacchi  * LIMITED REMEDY.
98*35786f68SRobert Mustacchi  *
99*35786f68SRobert Mustacchi  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100*35786f68SRobert Mustacchi  * software or system incorporating such software without first obtaining any
101*35786f68SRobert Mustacchi  * required license or other approval from the U. S. Department of Commerce or
102*35786f68SRobert Mustacchi  * any other agency or department of the United States Government. In the
103*35786f68SRobert Mustacchi  * event Licensee exports any such software from the United States or
104*35786f68SRobert Mustacchi  * re-exports any such software from a foreign destination, Licensee shall
105*35786f68SRobert Mustacchi  * ensure that the distribution and export/re-export of the software is in
106*35786f68SRobert Mustacchi  * compliance with all laws, regulations, orders, or other restrictions of the
107*35786f68SRobert Mustacchi  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108*35786f68SRobert Mustacchi  * any of its subsidiaries will export/re-export any technical data, process,
109*35786f68SRobert Mustacchi  * software, or service, directly or indirectly, to any country for which the
110*35786f68SRobert Mustacchi  * United States government or any agency thereof requires an export license,
111*35786f68SRobert Mustacchi  * other governmental approval, or letter of assurance, without first obtaining
112*35786f68SRobert Mustacchi  * such license, approval or letter.
113*35786f68SRobert Mustacchi  *
114*35786f68SRobert Mustacchi  *****************************************************************************
115*35786f68SRobert Mustacchi  *
116*35786f68SRobert Mustacchi  * Alternatively, you may choose to be licensed under the terms of the
117*35786f68SRobert Mustacchi  * following license:
118*35786f68SRobert Mustacchi  *
119bc36eafdSMike Gerdts  * Redistribution and use in source and binary forms, with or without
120bc36eafdSMike Gerdts  * modification, are permitted provided that the following conditions
121bc36eafdSMike Gerdts  * are met:
122bc36eafdSMike Gerdts  * 1. Redistributions of source code must retain the above copyright
123bc36eafdSMike Gerdts  *    notice, this list of conditions, and the following disclaimer,
124bc36eafdSMike Gerdts  *    without modification.
125bc36eafdSMike Gerdts  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126bc36eafdSMike Gerdts  *    substantially similar to the "NO WARRANTY" disclaimer below
127bc36eafdSMike Gerdts  *    ("Disclaimer") and any redistribution must be conditioned upon
128bc36eafdSMike Gerdts  *    including a substantially similar Disclaimer requirement for further
129bc36eafdSMike Gerdts  *    binary redistribution.
130bc36eafdSMike Gerdts  * 3. Neither the names of the above-listed copyright holders nor the names
131bc36eafdSMike Gerdts  *    of any contributors may be used to endorse or promote products derived
132bc36eafdSMike Gerdts  *    from this software without specific prior written permission.
133bc36eafdSMike Gerdts  *
134*35786f68SRobert Mustacchi  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135*35786f68SRobert Mustacchi  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136*35786f68SRobert Mustacchi  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137*35786f68SRobert Mustacchi  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138*35786f68SRobert Mustacchi  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139*35786f68SRobert Mustacchi  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140*35786f68SRobert Mustacchi  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141*35786f68SRobert Mustacchi  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142*35786f68SRobert Mustacchi  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143*35786f68SRobert Mustacchi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144*35786f68SRobert Mustacchi  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145*35786f68SRobert Mustacchi  *
146*35786f68SRobert Mustacchi  * Alternatively, you may choose to be licensed under the terms of the
147bc36eafdSMike Gerdts  * GNU General Public License ("GPL") version 2 as published by the Free
148bc36eafdSMike Gerdts  * Software Foundation.
149bc36eafdSMike Gerdts  *
150*35786f68SRobert Mustacchi  *****************************************************************************/
151bc36eafdSMike Gerdts 
152bc36eafdSMike Gerdts #define _DECLARE_PR_GLOBALS
153bc36eafdSMike Gerdts 
154bc36eafdSMike Gerdts #include "aslcompiler.h"
155bc36eafdSMike Gerdts 
156bc36eafdSMike Gerdts /*
157bc36eafdSMike Gerdts  * TBDs:
158bc36eafdSMike Gerdts  *
159bc36eafdSMike Gerdts  * No nested macros, maybe never
160bc36eafdSMike Gerdts  * Implement ASL "Include" as well as "#include" here?
161bc36eafdSMike Gerdts  */
162bc36eafdSMike Gerdts #define _COMPONENT          ASL_PREPROCESSOR
163bc36eafdSMike Gerdts         ACPI_MODULE_NAME    ("prscan")
164bc36eafdSMike Gerdts 
165bc36eafdSMike Gerdts 
166bc36eafdSMike Gerdts /* Local prototypes */
167bc36eafdSMike Gerdts 
168bc36eafdSMike Gerdts static void
169bc36eafdSMike Gerdts PrPreprocessInputFile (
170bc36eafdSMike Gerdts     void);
171bc36eafdSMike Gerdts 
172bc36eafdSMike Gerdts static void
173bc36eafdSMike Gerdts PrDoDirective (
174bc36eafdSMike Gerdts     char                    *DirectiveToken,
175bc36eafdSMike Gerdts     char                    **Next);
176bc36eafdSMike Gerdts 
177bc36eafdSMike Gerdts static void
178bc36eafdSMike Gerdts PrGetNextLineInit (
179bc36eafdSMike Gerdts     void);
180bc36eafdSMike Gerdts 
181bc36eafdSMike Gerdts static UINT32
182bc36eafdSMike Gerdts PrGetNextLine (
183bc36eafdSMike Gerdts     FILE                    *Handle);
184bc36eafdSMike Gerdts 
185bc36eafdSMike Gerdts static int
186bc36eafdSMike Gerdts PrMatchDirective (
187bc36eafdSMike Gerdts     char                    *Directive);
188bc36eafdSMike Gerdts 
189bc36eafdSMike Gerdts static void
190bc36eafdSMike Gerdts PrPushDirective (
191bc36eafdSMike Gerdts     int                     Directive,
192bc36eafdSMike Gerdts     char                    *Argument);
193bc36eafdSMike Gerdts 
194bc36eafdSMike Gerdts static ACPI_STATUS
195bc36eafdSMike Gerdts PrPopDirective (
196bc36eafdSMike Gerdts     void);
197bc36eafdSMike Gerdts 
198bc36eafdSMike Gerdts static void
199bc36eafdSMike Gerdts PrDbgPrint (
200bc36eafdSMike Gerdts     char                    *Action,
201bc36eafdSMike Gerdts     char                    *DirectiveName);
202bc36eafdSMike Gerdts 
203bc36eafdSMike Gerdts static void
204bc36eafdSMike Gerdts PrDoIncludeBuffer (
205bc36eafdSMike Gerdts     char                    *Pathname,
206bc36eafdSMike Gerdts     char                    *BufferName);
207bc36eafdSMike Gerdts 
208bc36eafdSMike Gerdts static void
209bc36eafdSMike Gerdts PrDoIncludeFile (
210bc36eafdSMike Gerdts     char                    *Pathname);
211bc36eafdSMike Gerdts 
212bc36eafdSMike Gerdts 
213bc36eafdSMike Gerdts /*
214bc36eafdSMike Gerdts  * Supported preprocessor directives
215bc36eafdSMike Gerdts  * Each entry is of the form "Name, ArgumentCount"
216bc36eafdSMike Gerdts  */
217bc36eafdSMike Gerdts static const PR_DIRECTIVE_INFO      Gbl_DirectiveInfo[] =
218bc36eafdSMike Gerdts {
219bc36eafdSMike Gerdts     {"define",          1},
220bc36eafdSMike Gerdts     {"elif",            0}, /* Converted to #else..#if internally */
221bc36eafdSMike Gerdts     {"else",            0},
222bc36eafdSMike Gerdts     {"endif",           0},
223bc36eafdSMike Gerdts     {"error",           1},
224bc36eafdSMike Gerdts     {"if",              1},
225bc36eafdSMike Gerdts     {"ifdef",           1},
226bc36eafdSMike Gerdts     {"ifndef",          1},
227bc36eafdSMike Gerdts     {"include",         0}, /* Argument is not standard format, so just use 0 here */
228bc36eafdSMike Gerdts     {"includebuffer",   0}, /* Argument is not standard format, so just use 0 here */
229bc36eafdSMike Gerdts     {"line",            1},
230bc36eafdSMike Gerdts     {"pragma",          1},
231bc36eafdSMike Gerdts     {"undef",           1},
232bc36eafdSMike Gerdts     {"warning",         1},
233bc36eafdSMike Gerdts     {NULL,              0}
234bc36eafdSMike Gerdts };
235bc36eafdSMike Gerdts 
236bc36eafdSMike Gerdts /* This table must match ordering of above table exactly */
237bc36eafdSMike Gerdts 
238bc36eafdSMike Gerdts enum Gbl_DirectiveIndexes
239bc36eafdSMike Gerdts {
240bc36eafdSMike Gerdts     PR_DIRECTIVE_DEFINE = 0,
241bc36eafdSMike Gerdts     PR_DIRECTIVE_ELIF,
242bc36eafdSMike Gerdts     PR_DIRECTIVE_ELSE,
243bc36eafdSMike Gerdts     PR_DIRECTIVE_ENDIF,
244bc36eafdSMike Gerdts     PR_DIRECTIVE_ERROR,
245bc36eafdSMike Gerdts     PR_DIRECTIVE_IF,
246bc36eafdSMike Gerdts     PR_DIRECTIVE_IFDEF,
247bc36eafdSMike Gerdts     PR_DIRECTIVE_IFNDEF,
248bc36eafdSMike Gerdts     PR_DIRECTIVE_INCLUDE,
249bc36eafdSMike Gerdts     PR_DIRECTIVE_INCLUDEBUFFER,
250bc36eafdSMike Gerdts     PR_DIRECTIVE_LINE,
251bc36eafdSMike Gerdts     PR_DIRECTIVE_PRAGMA,
252bc36eafdSMike Gerdts     PR_DIRECTIVE_UNDEF,
253bc36eafdSMike Gerdts     PR_DIRECTIVE_WARNING
254bc36eafdSMike Gerdts };
255bc36eafdSMike Gerdts 
256bc36eafdSMike Gerdts #define ASL_DIRECTIVE_NOT_FOUND     -1
257bc36eafdSMike Gerdts 
258bc36eafdSMike Gerdts 
259bc36eafdSMike Gerdts /*******************************************************************************
260bc36eafdSMike Gerdts  *
261bc36eafdSMike Gerdts  * FUNCTION:    PrInitializePreprocessor
262bc36eafdSMike Gerdts  *
263bc36eafdSMike Gerdts  * PARAMETERS:  None
264bc36eafdSMike Gerdts  *
265bc36eafdSMike Gerdts  * RETURN:      None
266bc36eafdSMike Gerdts  *
267bc36eafdSMike Gerdts  * DESCRIPTION: Startup initialization for the Preprocessor.
268bc36eafdSMike Gerdts  *
269bc36eafdSMike Gerdts  ******************************************************************************/
270bc36eafdSMike Gerdts 
271bc36eafdSMike Gerdts void
PrInitializePreprocessor(void)272bc36eafdSMike Gerdts PrInitializePreprocessor (
273bc36eafdSMike Gerdts     void)
274bc36eafdSMike Gerdts {
275bc36eafdSMike Gerdts     /* Init globals and the list of #defines */
276bc36eafdSMike Gerdts 
277bc36eafdSMike Gerdts     PrInitializeGlobals ();
278bc36eafdSMike Gerdts     Gbl_DefineList = NULL;
279bc36eafdSMike Gerdts }
280bc36eafdSMike Gerdts 
281bc36eafdSMike Gerdts 
282bc36eafdSMike Gerdts /*******************************************************************************
283bc36eafdSMike Gerdts  *
284bc36eafdSMike Gerdts  * FUNCTION:    PrInitializeGlobals
285bc36eafdSMike Gerdts  *
286bc36eafdSMike Gerdts  * PARAMETERS:  None
287bc36eafdSMike Gerdts  *
288bc36eafdSMike Gerdts  * RETURN:      None
289bc36eafdSMike Gerdts  *
290bc36eafdSMike Gerdts  * DESCRIPTION: Initialize globals for the Preprocessor. Used for startuup
291bc36eafdSMike Gerdts  *              initialization and re-initialization between compiles during
292bc36eafdSMike Gerdts  *              a multiple source file compile.
293bc36eafdSMike Gerdts  *
294bc36eafdSMike Gerdts  ******************************************************************************/
295bc36eafdSMike Gerdts 
296bc36eafdSMike Gerdts void
PrInitializeGlobals(void)297bc36eafdSMike Gerdts PrInitializeGlobals (
298bc36eafdSMike Gerdts     void)
299bc36eafdSMike Gerdts {
300bc36eafdSMike Gerdts     /* Init globals */
301bc36eafdSMike Gerdts 
302bc36eafdSMike Gerdts     Gbl_InputFileList = NULL;
303bc36eafdSMike Gerdts     Gbl_CurrentLineNumber = 1;
304bc36eafdSMike Gerdts     Gbl_PreprocessorLineNumber = 1;
305bc36eafdSMike Gerdts     Gbl_PreprocessorError = FALSE;
306bc36eafdSMike Gerdts 
307bc36eafdSMike Gerdts     /* These are used to track #if/#else blocks (possibly nested) */
308bc36eafdSMike Gerdts 
309bc36eafdSMike Gerdts     Gbl_IfDepth = 0;
310bc36eafdSMike Gerdts     Gbl_IgnoringThisCodeBlock = FALSE;
311bc36eafdSMike Gerdts     Gbl_DirectiveStack = NULL;
312bc36eafdSMike Gerdts }
313bc36eafdSMike Gerdts 
314bc36eafdSMike Gerdts 
315bc36eafdSMike Gerdts /*******************************************************************************
316bc36eafdSMike Gerdts  *
317bc36eafdSMike Gerdts  * FUNCTION:    PrTerminatePreprocessor
318bc36eafdSMike Gerdts  *
319bc36eafdSMike Gerdts  * PARAMETERS:  None
320bc36eafdSMike Gerdts  *
321bc36eafdSMike Gerdts  * RETURN:      None
322bc36eafdSMike Gerdts  *
323bc36eafdSMike Gerdts  * DESCRIPTION: Termination of the preprocessor. Delete lists. Keep any
324bc36eafdSMike Gerdts  *              defines that were specified on the command line, in order to
325bc36eafdSMike Gerdts  *              support multiple compiles with a single compiler invocation.
326bc36eafdSMike Gerdts  *
327bc36eafdSMike Gerdts  ******************************************************************************/
328bc36eafdSMike Gerdts 
329bc36eafdSMike Gerdts void
PrTerminatePreprocessor(void)330bc36eafdSMike Gerdts PrTerminatePreprocessor (
331bc36eafdSMike Gerdts     void)
332bc36eafdSMike Gerdts {
333bc36eafdSMike Gerdts     PR_DEFINE_INFO          *DefineInfo;
334bc36eafdSMike Gerdts 
335bc36eafdSMike Gerdts 
336bc36eafdSMike Gerdts     /*
337bc36eafdSMike Gerdts      * The persistent defines (created on the command line) are always at the
338bc36eafdSMike Gerdts      * end of the list. We save them.
339bc36eafdSMike Gerdts      */
340bc36eafdSMike Gerdts     while ((Gbl_DefineList) && (!Gbl_DefineList->Persist))
341bc36eafdSMike Gerdts     {
342bc36eafdSMike Gerdts         DefineInfo = Gbl_DefineList;
343bc36eafdSMike Gerdts         Gbl_DefineList = DefineInfo->Next;
344bc36eafdSMike Gerdts 
345bc36eafdSMike Gerdts         ACPI_FREE (DefineInfo->Replacement);
346bc36eafdSMike Gerdts         ACPI_FREE (DefineInfo->Identifier);
347bc36eafdSMike Gerdts         ACPI_FREE (DefineInfo);
348bc36eafdSMike Gerdts     }
349bc36eafdSMike Gerdts }
350bc36eafdSMike Gerdts 
351bc36eafdSMike Gerdts 
352bc36eafdSMike Gerdts /*******************************************************************************
353bc36eafdSMike Gerdts  *
354bc36eafdSMike Gerdts  * FUNCTION:    PrDoPreprocess
355bc36eafdSMike Gerdts  *
356bc36eafdSMike Gerdts  * PARAMETERS:  None
357bc36eafdSMike Gerdts  *
358bc36eafdSMike Gerdts  * RETURN:      None
359bc36eafdSMike Gerdts  *
360bc36eafdSMike Gerdts  * DESCRIPTION: Main entry point for the iASL Preprocessor. Input file must
361bc36eafdSMike Gerdts  *              be already open. Handles multiple input files via the
362bc36eafdSMike Gerdts  *              #include directive.
363bc36eafdSMike Gerdts  *
364bc36eafdSMike Gerdts  ******************************************************************************/
365bc36eafdSMike Gerdts 
366bc36eafdSMike Gerdts void
PrDoPreprocess(void)367bc36eafdSMike Gerdts PrDoPreprocess (
368bc36eafdSMike Gerdts     void)
369bc36eafdSMike Gerdts {
370bc36eafdSMike Gerdts     BOOLEAN                 MoreInputFiles;
371bc36eafdSMike Gerdts 
372bc36eafdSMike Gerdts 
373bc36eafdSMike Gerdts     DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
374bc36eafdSMike Gerdts 
375bc36eafdSMike Gerdts 
376bc36eafdSMike Gerdts     FlSeekFile (ASL_FILE_INPUT, 0);
377bc36eafdSMike Gerdts     PrDumpPredefinedNames ();
378bc36eafdSMike Gerdts 
379bc36eafdSMike Gerdts     /* Main preprocessor loop, handles include files */
380bc36eafdSMike Gerdts 
381bc36eafdSMike Gerdts     do
382bc36eafdSMike Gerdts     {
383bc36eafdSMike Gerdts         PrPreprocessInputFile ();
384bc36eafdSMike Gerdts         MoreInputFiles = PrPopInputFileStack ();
385bc36eafdSMike Gerdts 
386bc36eafdSMike Gerdts     } while (MoreInputFiles);
387bc36eafdSMike Gerdts 
388bc36eafdSMike Gerdts     /* Point compiler input to the new preprocessor output file (.pre) */
389bc36eafdSMike Gerdts 
390bc36eafdSMike Gerdts     FlCloseFile (ASL_FILE_INPUT);
391bc36eafdSMike Gerdts     Gbl_Files[ASL_FILE_INPUT].Handle = Gbl_Files[ASL_FILE_PREPROCESSOR].Handle;
392bc36eafdSMike Gerdts     AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
393bc36eafdSMike Gerdts 
394bc36eafdSMike Gerdts     /* Reset globals to allow compiler to run */
395bc36eafdSMike Gerdts 
396bc36eafdSMike Gerdts     FlSeekFile (ASL_FILE_INPUT, 0);
397bc36eafdSMike Gerdts     if (!Gbl_PreprocessOnly)
398bc36eafdSMike Gerdts     {
399bc36eafdSMike Gerdts         Gbl_CurrentLineNumber = 0;
400bc36eafdSMike Gerdts     }
401bc36eafdSMike Gerdts 
402bc36eafdSMike Gerdts     DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
403bc36eafdSMike Gerdts }
404bc36eafdSMike Gerdts 
405bc36eafdSMike Gerdts 
406bc36eafdSMike Gerdts /*******************************************************************************
407bc36eafdSMike Gerdts  *
408bc36eafdSMike Gerdts  * FUNCTION:    PrPreprocessInputFile
409bc36eafdSMike Gerdts  *
410bc36eafdSMike Gerdts  * PARAMETERS:  None
411bc36eafdSMike Gerdts  *
412bc36eafdSMike Gerdts  * RETURN:      None
413bc36eafdSMike Gerdts  *
414bc36eafdSMike Gerdts  * DESCRIPTION: Preprocess one entire file, line-by-line.
415bc36eafdSMike Gerdts  *
416bc36eafdSMike Gerdts  * Input:  Raw user ASL from ASL_FILE_INPUT
417bc36eafdSMike Gerdts  * Output: Preprocessed file written to ASL_FILE_PREPROCESSOR and
418bc36eafdSMike Gerdts  *         (optionally) ASL_FILE_PREPROCESSOR_USER
419bc36eafdSMike Gerdts  *
420bc36eafdSMike Gerdts  ******************************************************************************/
421bc36eafdSMike Gerdts 
422bc36eafdSMike Gerdts static void
PrPreprocessInputFile(void)423bc36eafdSMike Gerdts PrPreprocessInputFile (
424bc36eafdSMike Gerdts     void)
425bc36eafdSMike Gerdts {
426bc36eafdSMike Gerdts     UINT32                  Status;
427bc36eafdSMike Gerdts     char                    *Token;
428bc36eafdSMike Gerdts     char                    *ReplaceString;
429bc36eafdSMike Gerdts     PR_DEFINE_INFO          *DefineInfo;
430bc36eafdSMike Gerdts     ACPI_SIZE               TokenOffset;
431bc36eafdSMike Gerdts     char                    *Next;
432bc36eafdSMike Gerdts     int                     OffsetAdjust;
433bc36eafdSMike Gerdts 
434bc36eafdSMike Gerdts 
435bc36eafdSMike Gerdts     PrGetNextLineInit ();
436bc36eafdSMike Gerdts 
437bc36eafdSMike Gerdts     /* Scan source line-by-line and process directives. Then write the .i file */
438bc36eafdSMike Gerdts 
439bc36eafdSMike Gerdts     while ((Status = PrGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
440bc36eafdSMike Gerdts     {
441bc36eafdSMike Gerdts         Gbl_CurrentLineNumber++;
442bc36eafdSMike Gerdts         Gbl_LogicalLineNumber++;
443bc36eafdSMike Gerdts 
444bc36eafdSMike Gerdts         if (Status == ASL_IGNORE_LINE)
445bc36eafdSMike Gerdts         {
446bc36eafdSMike Gerdts             goto WriteEntireLine;
447bc36eafdSMike Gerdts         }
448bc36eafdSMike Gerdts 
449bc36eafdSMike Gerdts         /* Need a copy of the input line for strok() */
450bc36eafdSMike Gerdts 
451bc36eafdSMike Gerdts         strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer);
452bc36eafdSMike Gerdts         Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
453bc36eafdSMike Gerdts         OffsetAdjust = 0;
454bc36eafdSMike Gerdts 
455bc36eafdSMike Gerdts         /* All preprocessor directives must begin with '#' */
456bc36eafdSMike Gerdts 
457bc36eafdSMike Gerdts         if (Token && (*Token == '#'))
458bc36eafdSMike Gerdts         {
459bc36eafdSMike Gerdts             if (strlen (Token) == 1)
460bc36eafdSMike Gerdts             {
461bc36eafdSMike Gerdts                 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
462bc36eafdSMike Gerdts             }
463bc36eafdSMike Gerdts             else
464bc36eafdSMike Gerdts             {
465bc36eafdSMike Gerdts                 Token++;    /* Skip leading # */
466bc36eafdSMike Gerdts             }
467bc36eafdSMike Gerdts 
468bc36eafdSMike Gerdts             /* Execute the directive, do not write line to output file */
469bc36eafdSMike Gerdts 
470bc36eafdSMike Gerdts             PrDoDirective (Token, &Next);
471bc36eafdSMike Gerdts             continue;
472bc36eafdSMike Gerdts         }
473bc36eafdSMike Gerdts 
474bc36eafdSMike Gerdts         /*
475bc36eafdSMike Gerdts          * If we are currently within the part of an IF/ELSE block that is
476bc36eafdSMike Gerdts          * FALSE, ignore the line and do not write it to the output file.
477bc36eafdSMike Gerdts          * This continues until an #else or #endif is encountered.
478bc36eafdSMike Gerdts          */
479bc36eafdSMike Gerdts         if (Gbl_IgnoringThisCodeBlock)
480bc36eafdSMike Gerdts         {
481bc36eafdSMike Gerdts             continue;
482bc36eafdSMike Gerdts         }
483bc36eafdSMike Gerdts 
484bc36eafdSMike Gerdts         /* Match and replace all #defined names within this source line */
485bc36eafdSMike Gerdts 
486bc36eafdSMike Gerdts         while (Token)
487bc36eafdSMike Gerdts         {
488bc36eafdSMike Gerdts             DefineInfo = PrMatchDefine (Token);
489bc36eafdSMike Gerdts             if (DefineInfo)
490bc36eafdSMike Gerdts             {
491bc36eafdSMike Gerdts                 if (DefineInfo->Body)
492bc36eafdSMike Gerdts                 {
493bc36eafdSMike Gerdts                     /* This is a macro */
494bc36eafdSMike Gerdts 
495bc36eafdSMike Gerdts                     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
496bc36eafdSMike Gerdts                         "Matched Macro: %s->%s\n",
497bc36eafdSMike Gerdts                         Gbl_CurrentLineNumber, DefineInfo->Identifier,
498bc36eafdSMike Gerdts                         DefineInfo->Replacement);
499bc36eafdSMike Gerdts 
500bc36eafdSMike Gerdts                     PrDoMacroInvocation (Gbl_MainTokenBuffer, Token,
501bc36eafdSMike Gerdts                         DefineInfo, &Next);
502bc36eafdSMike Gerdts                 }
503bc36eafdSMike Gerdts                 else
504bc36eafdSMike Gerdts                 {
505bc36eafdSMike Gerdts                     ReplaceString = DefineInfo->Replacement;
506bc36eafdSMike Gerdts 
507bc36eafdSMike Gerdts                     /* Replace the name in the original line buffer */
508bc36eafdSMike Gerdts 
509bc36eafdSMike Gerdts                     TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust;
510bc36eafdSMike Gerdts                     PrReplaceData (
511bc36eafdSMike Gerdts                         &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token),
512bc36eafdSMike Gerdts                         ReplaceString, strlen (ReplaceString));
513bc36eafdSMike Gerdts 
514bc36eafdSMike Gerdts                     /* Adjust for length difference between old and new name length */
515bc36eafdSMike Gerdts 
516bc36eafdSMike Gerdts                     OffsetAdjust += strlen (ReplaceString) - strlen (Token);
517bc36eafdSMike Gerdts 
518bc36eafdSMike Gerdts                     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
519bc36eafdSMike Gerdts                         "Matched #define: %s->%s\n",
520bc36eafdSMike Gerdts                         Gbl_CurrentLineNumber, Token,
521bc36eafdSMike Gerdts                         *ReplaceString ? ReplaceString : "(NULL STRING)");
522bc36eafdSMike Gerdts                 }
523bc36eafdSMike Gerdts             }
524bc36eafdSMike Gerdts 
525bc36eafdSMike Gerdts             Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
526bc36eafdSMike Gerdts         }
527bc36eafdSMike Gerdts 
528bc36eafdSMike Gerdts         Gbl_PreprocessorLineNumber++;
529bc36eafdSMike Gerdts 
530bc36eafdSMike Gerdts 
531bc36eafdSMike Gerdts WriteEntireLine:
532bc36eafdSMike Gerdts         /*
533bc36eafdSMike Gerdts          * Now we can write the possibly modified source line to the
534bc36eafdSMike Gerdts          * preprocessor file(s).
535bc36eafdSMike Gerdts          */
536bc36eafdSMike Gerdts         FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer,
537bc36eafdSMike Gerdts             strlen (Gbl_CurrentLineBuffer));
538bc36eafdSMike Gerdts     }
539bc36eafdSMike Gerdts }
540bc36eafdSMike Gerdts 
541bc36eafdSMike Gerdts 
542bc36eafdSMike Gerdts /*******************************************************************************
543bc36eafdSMike Gerdts  *
544bc36eafdSMike Gerdts  * FUNCTION:    PrDoDirective
545bc36eafdSMike Gerdts  *
546bc36eafdSMike Gerdts  * PARAMETERS:  Directive               - Pointer to directive name token
547bc36eafdSMike Gerdts  *              Next                    - "Next" buffer from GetNextToken
548bc36eafdSMike Gerdts  *
549bc36eafdSMike Gerdts  * RETURN:      None.
550bc36eafdSMike Gerdts  *
551bc36eafdSMike Gerdts  * DESCRIPTION: Main processing for all preprocessor directives
552bc36eafdSMike Gerdts  *
553bc36eafdSMike Gerdts  ******************************************************************************/
554bc36eafdSMike Gerdts 
555bc36eafdSMike Gerdts static void
PrDoDirective(char * DirectiveToken,char ** Next)556bc36eafdSMike Gerdts PrDoDirective (
557bc36eafdSMike Gerdts     char                    *DirectiveToken,
558bc36eafdSMike Gerdts     char                    **Next)
559bc36eafdSMike Gerdts {
560bc36eafdSMike Gerdts     char                    *Token = Gbl_MainTokenBuffer;
561bc36eafdSMike Gerdts     char                    *Token2 = NULL;
562bc36eafdSMike Gerdts     char                    *End;
563bc36eafdSMike Gerdts     UINT64                  Value;
564bc36eafdSMike Gerdts     ACPI_SIZE               TokenOffset;
565bc36eafdSMike Gerdts     int                     Directive;
566bc36eafdSMike Gerdts     ACPI_STATUS             Status;
567bc36eafdSMike Gerdts 
568bc36eafdSMike Gerdts 
569bc36eafdSMike Gerdts     if (!DirectiveToken)
570bc36eafdSMike Gerdts     {
571bc36eafdSMike Gerdts         goto SyntaxError;
572bc36eafdSMike Gerdts     }
573bc36eafdSMike Gerdts 
574bc36eafdSMike Gerdts     Directive = PrMatchDirective (DirectiveToken);
575bc36eafdSMike Gerdts     if (Directive == ASL_DIRECTIVE_NOT_FOUND)
576bc36eafdSMike Gerdts     {
577bc36eafdSMike Gerdts         PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
578bc36eafdSMike Gerdts             THIS_TOKEN_OFFSET (DirectiveToken));
579bc36eafdSMike Gerdts 
580bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
581bc36eafdSMike Gerdts             "#%s: Unknown directive\n",
582bc36eafdSMike Gerdts             Gbl_CurrentLineNumber, DirectiveToken);
583bc36eafdSMike Gerdts         return;
584bc36eafdSMike Gerdts     }
585bc36eafdSMike Gerdts 
586bc36eafdSMike Gerdts     /*
587bc36eafdSMike Gerdts      * Emit a line directive into the preprocessor file (.pre) after
588bc36eafdSMike Gerdts      * every matched directive. This is passed through to the compiler
589bc36eafdSMike Gerdts      * so that error/warning messages are kept in sync with the
590bc36eafdSMike Gerdts      * original source file.
591bc36eafdSMike Gerdts      */
592bc36eafdSMike Gerdts     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\" // #%s\n",
593bc36eafdSMike Gerdts         Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename,
594bc36eafdSMike Gerdts         Gbl_DirectiveInfo[Directive].Name);
595bc36eafdSMike Gerdts 
596bc36eafdSMike Gerdts     /*
597bc36eafdSMike Gerdts      * If we are currently ignoring this block and we encounter a #else or
598bc36eafdSMike Gerdts      * #elif, we must ignore their blocks also if the parent block is also
599bc36eafdSMike Gerdts      * being ignored.
600bc36eafdSMike Gerdts      */
601bc36eafdSMike Gerdts     if (Gbl_IgnoringThisCodeBlock)
602bc36eafdSMike Gerdts     {
603bc36eafdSMike Gerdts         switch (Directive)
604bc36eafdSMike Gerdts         {
605bc36eafdSMike Gerdts         case PR_DIRECTIVE_ELSE:
606bc36eafdSMike Gerdts         case PR_DIRECTIVE_ELIF:
607bc36eafdSMike Gerdts 
608bc36eafdSMike Gerdts             if (Gbl_DirectiveStack &&
609bc36eafdSMike Gerdts                 Gbl_DirectiveStack->IgnoringThisCodeBlock)
610bc36eafdSMike Gerdts             {
611bc36eafdSMike Gerdts                 PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name);
612bc36eafdSMike Gerdts                 return;
613bc36eafdSMike Gerdts             }
614bc36eafdSMike Gerdts             break;
615bc36eafdSMike Gerdts 
616bc36eafdSMike Gerdts         default:
617bc36eafdSMike Gerdts             break;
618bc36eafdSMike Gerdts         }
619bc36eafdSMike Gerdts     }
620bc36eafdSMike Gerdts 
621bc36eafdSMike Gerdts     /*
622bc36eafdSMike Gerdts      * Need to always check for #else, #elif, #endif regardless of
623bc36eafdSMike Gerdts      * whether we are ignoring the current code block, since these
624bc36eafdSMike Gerdts      * are conditional code block terminators.
625bc36eafdSMike Gerdts      */
626bc36eafdSMike Gerdts     switch (Directive)
627bc36eafdSMike Gerdts     {
628bc36eafdSMike Gerdts     case PR_DIRECTIVE_ELSE:
629bc36eafdSMike Gerdts 
630bc36eafdSMike Gerdts         Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock);
631bc36eafdSMike Gerdts         PrDbgPrint ("Executing", "else block");
632bc36eafdSMike Gerdts         return;
633bc36eafdSMike Gerdts 
634bc36eafdSMike Gerdts     case PR_DIRECTIVE_ELIF:
635bc36eafdSMike Gerdts 
636bc36eafdSMike Gerdts         Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock);
637bc36eafdSMike Gerdts         Directive = PR_DIRECTIVE_IF;
638bc36eafdSMike Gerdts 
639bc36eafdSMike Gerdts         if (Gbl_IgnoringThisCodeBlock == TRUE)
640bc36eafdSMike Gerdts         {
641bc36eafdSMike Gerdts             /* Not executing the ELSE part -- all done here */
642bc36eafdSMike Gerdts             PrDbgPrint ("Ignoring", "elif block");
643bc36eafdSMike Gerdts             return;
644bc36eafdSMike Gerdts         }
645bc36eafdSMike Gerdts 
646bc36eafdSMike Gerdts         /*
647bc36eafdSMike Gerdts          * After this, we will execute the IF part further below.
648bc36eafdSMike Gerdts          * First, however, pop off the original #if directive.
649bc36eafdSMike Gerdts          */
650bc36eafdSMike Gerdts         if (ACPI_FAILURE (PrPopDirective ()))
651bc36eafdSMike Gerdts         {
652bc36eafdSMike Gerdts             PrError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
653bc36eafdSMike Gerdts                 THIS_TOKEN_OFFSET (DirectiveToken));
654bc36eafdSMike Gerdts         }
655bc36eafdSMike Gerdts 
656bc36eafdSMike Gerdts         PrDbgPrint ("Executing", "elif block");
657bc36eafdSMike Gerdts         break;
658bc36eafdSMike Gerdts 
659bc36eafdSMike Gerdts     case PR_DIRECTIVE_ENDIF:
660bc36eafdSMike Gerdts 
661bc36eafdSMike Gerdts         PrDbgPrint ("Executing", "endif");
662bc36eafdSMike Gerdts 
663bc36eafdSMike Gerdts         /* Pop the owning #if/#ifdef/#ifndef */
664bc36eafdSMike Gerdts 
665bc36eafdSMike Gerdts         if (ACPI_FAILURE (PrPopDirective ()))
666bc36eafdSMike Gerdts         {
667bc36eafdSMike Gerdts             PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
668bc36eafdSMike Gerdts                 THIS_TOKEN_OFFSET (DirectiveToken));
669bc36eafdSMike Gerdts         }
670bc36eafdSMike Gerdts         return;
671bc36eafdSMike Gerdts 
672bc36eafdSMike Gerdts     default:
673bc36eafdSMike Gerdts         break;
674bc36eafdSMike Gerdts     }
675bc36eafdSMike Gerdts 
676bc36eafdSMike Gerdts     /* Most directives have at least one argument */
677bc36eafdSMike Gerdts 
678bc36eafdSMike Gerdts     if (Gbl_DirectiveInfo[Directive].ArgCount >= 1)
679bc36eafdSMike Gerdts     {
680bc36eafdSMike Gerdts         Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
681bc36eafdSMike Gerdts         if (!Token)
682bc36eafdSMike Gerdts         {
683bc36eafdSMike Gerdts             goto SyntaxError;
684bc36eafdSMike Gerdts         }
685bc36eafdSMike Gerdts     }
686bc36eafdSMike Gerdts 
687bc36eafdSMike Gerdts     if (Gbl_DirectiveInfo[Directive].ArgCount >= 2)
688bc36eafdSMike Gerdts     {
689bc36eafdSMike Gerdts         Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
690bc36eafdSMike Gerdts         if (!Token2)
691bc36eafdSMike Gerdts         {
692bc36eafdSMike Gerdts             goto SyntaxError;
693bc36eafdSMike Gerdts         }
694bc36eafdSMike Gerdts     }
695bc36eafdSMike Gerdts 
696bc36eafdSMike Gerdts     /*
697bc36eafdSMike Gerdts      * At this point, if we are ignoring the current code block,
698bc36eafdSMike Gerdts      * do not process any more directives (i.e., ignore them also.)
699bc36eafdSMike Gerdts      * For "if" style directives, open/push a new block anyway. We
700bc36eafdSMike Gerdts      * must do this to keep track of #endif directives
701bc36eafdSMike Gerdts      */
702bc36eafdSMike Gerdts     if (Gbl_IgnoringThisCodeBlock)
703bc36eafdSMike Gerdts     {
704bc36eafdSMike Gerdts         switch (Directive)
705bc36eafdSMike Gerdts         {
706bc36eafdSMike Gerdts         case PR_DIRECTIVE_IF:
707bc36eafdSMike Gerdts         case PR_DIRECTIVE_IFDEF:
708bc36eafdSMike Gerdts         case PR_DIRECTIVE_IFNDEF:
709bc36eafdSMike Gerdts 
710bc36eafdSMike Gerdts             PrPushDirective (Directive, Token);
711bc36eafdSMike Gerdts             PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name);
712bc36eafdSMike Gerdts             break;
713bc36eafdSMike Gerdts 
714bc36eafdSMike Gerdts         default:
715bc36eafdSMike Gerdts             break;
716bc36eafdSMike Gerdts         }
717bc36eafdSMike Gerdts 
718bc36eafdSMike Gerdts         return;
719bc36eafdSMike Gerdts     }
720bc36eafdSMike Gerdts 
721bc36eafdSMike Gerdts     /*
722bc36eafdSMike Gerdts      * Execute the directive
723bc36eafdSMike Gerdts      */
724bc36eafdSMike Gerdts     PrDbgPrint ("Begin execution", Gbl_DirectiveInfo[Directive].Name);
725bc36eafdSMike Gerdts 
726bc36eafdSMike Gerdts     switch (Directive)
727bc36eafdSMike Gerdts     {
728bc36eafdSMike Gerdts     case PR_DIRECTIVE_IF:
729bc36eafdSMike Gerdts 
730bc36eafdSMike Gerdts         TokenOffset = Token - Gbl_MainTokenBuffer;
731bc36eafdSMike Gerdts 
732bc36eafdSMike Gerdts         /* Need to expand #define macros in the expression string first */
733bc36eafdSMike Gerdts 
734bc36eafdSMike Gerdts         Status = PrResolveIntegerExpression (
735bc36eafdSMike Gerdts             &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
736bc36eafdSMike Gerdts         if (ACPI_FAILURE (Status))
737bc36eafdSMike Gerdts         {
738bc36eafdSMike Gerdts             return;
739bc36eafdSMike Gerdts         }
740bc36eafdSMike Gerdts 
741bc36eafdSMike Gerdts         PrPushDirective (Directive, Token);
742bc36eafdSMike Gerdts         if (!Value)
743bc36eafdSMike Gerdts         {
744bc36eafdSMike Gerdts             Gbl_IgnoringThisCodeBlock = TRUE;
745bc36eafdSMike Gerdts         }
746bc36eafdSMike Gerdts 
747bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
748bc36eafdSMike Gerdts             "Resolved #if: %8.8X%8.8X %s\n",
749bc36eafdSMike Gerdts             Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value),
750bc36eafdSMike Gerdts             Gbl_IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>");
751bc36eafdSMike Gerdts         break;
752bc36eafdSMike Gerdts 
753bc36eafdSMike Gerdts     case PR_DIRECTIVE_IFDEF:
754bc36eafdSMike Gerdts 
755bc36eafdSMike Gerdts         PrPushDirective (Directive, Token);
756bc36eafdSMike Gerdts         if (!PrMatchDefine (Token))
757bc36eafdSMike Gerdts         {
758bc36eafdSMike Gerdts             Gbl_IgnoringThisCodeBlock = TRUE;
759bc36eafdSMike Gerdts         }
760bc36eafdSMike Gerdts 
761bc36eafdSMike Gerdts         PrDbgPrint ("Evaluated", "ifdef");
762bc36eafdSMike Gerdts         break;
763bc36eafdSMike Gerdts 
764bc36eafdSMike Gerdts     case PR_DIRECTIVE_IFNDEF:
765bc36eafdSMike Gerdts 
766bc36eafdSMike Gerdts         PrPushDirective (Directive, Token);
767bc36eafdSMike Gerdts         if (PrMatchDefine (Token))
768bc36eafdSMike Gerdts         {
769bc36eafdSMike Gerdts             Gbl_IgnoringThisCodeBlock = TRUE;
770bc36eafdSMike Gerdts         }
771bc36eafdSMike Gerdts 
772bc36eafdSMike Gerdts         PrDbgPrint ("Evaluated", "ifndef");
773bc36eafdSMike Gerdts         break;
774bc36eafdSMike Gerdts 
775bc36eafdSMike Gerdts     case PR_DIRECTIVE_DEFINE:
776bc36eafdSMike Gerdts         /*
777bc36eafdSMike Gerdts          * By definition, if first char after the name is a paren,
778bc36eafdSMike Gerdts          * this is a function macro.
779bc36eafdSMike Gerdts          */
780bc36eafdSMike Gerdts         TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token);
781bc36eafdSMike Gerdts         if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(')
782bc36eafdSMike Gerdts         {
783bc36eafdSMike Gerdts #ifndef MACROS_SUPPORTED
784bc36eafdSMike Gerdts             AcpiOsPrintf (
785bc36eafdSMike Gerdts                 "%s ERROR - line %u: #define macros are not supported yet\n",
786bc36eafdSMike Gerdts                 Gbl_CurrentLineBuffer, Gbl_LogicalLineNumber);
787bc36eafdSMike Gerdts             exit(1);
788bc36eafdSMike Gerdts #else
789bc36eafdSMike Gerdts             PrAddMacro (Token, Next);
790bc36eafdSMike Gerdts #endif
791bc36eafdSMike Gerdts         }
792bc36eafdSMike Gerdts         else
793bc36eafdSMike Gerdts         {
794bc36eafdSMike Gerdts             /* Use the remainder of the line for the #define */
795bc36eafdSMike Gerdts 
796bc36eafdSMike Gerdts             Token2 = *Next;
797bc36eafdSMike Gerdts             if (Token2)
798bc36eafdSMike Gerdts             {
799bc36eafdSMike Gerdts                 while ((*Token2 == ' ') || (*Token2 == '\t'))
800bc36eafdSMike Gerdts                 {
801bc36eafdSMike Gerdts                     Token2++;
802bc36eafdSMike Gerdts                 }
803bc36eafdSMike Gerdts 
804bc36eafdSMike Gerdts                 End = Token2;
805bc36eafdSMike Gerdts                 while (*End != '\n')
806bc36eafdSMike Gerdts                 {
807bc36eafdSMike Gerdts                     End++;
808bc36eafdSMike Gerdts                 }
809bc36eafdSMike Gerdts 
810bc36eafdSMike Gerdts                 *End = 0;
811bc36eafdSMike Gerdts             }
812bc36eafdSMike Gerdts             else
813bc36eafdSMike Gerdts             {
814bc36eafdSMike Gerdts                 Token2 = "";
815bc36eafdSMike Gerdts             }
816bc36eafdSMike Gerdts #if 0
817bc36eafdSMike Gerdts             Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next);
818bc36eafdSMike Gerdts             if (!Token2)
819bc36eafdSMike Gerdts             {
820bc36eafdSMike Gerdts                 Token2 = "";
821bc36eafdSMike Gerdts             }
822bc36eafdSMike Gerdts #endif
823bc36eafdSMike Gerdts             DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
824bc36eafdSMike Gerdts                 "New #define: %s->%s\n",
825bc36eafdSMike Gerdts                 Gbl_LogicalLineNumber, Token, Token2);
826bc36eafdSMike Gerdts 
827bc36eafdSMike Gerdts             PrAddDefine (Token, Token2, FALSE);
828bc36eafdSMike Gerdts         }
829bc36eafdSMike Gerdts         break;
830bc36eafdSMike Gerdts 
831bc36eafdSMike Gerdts     case PR_DIRECTIVE_ERROR:
832bc36eafdSMike Gerdts 
833bc36eafdSMike Gerdts         /* Note: No macro expansion */
834bc36eafdSMike Gerdts 
835bc36eafdSMike Gerdts         PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE,
836bc36eafdSMike Gerdts             THIS_TOKEN_OFFSET (Token));
837bc36eafdSMike Gerdts 
838bc36eafdSMike Gerdts         Gbl_SourceLine = 0;
839bc36eafdSMike Gerdts         Gbl_NextError = Gbl_ErrorLog;
840bc36eafdSMike Gerdts         CmCleanupAndExit ();
841bc36eafdSMike Gerdts         exit(1);
842bc36eafdSMike Gerdts 
843bc36eafdSMike Gerdts     case PR_DIRECTIVE_INCLUDE:
844bc36eafdSMike Gerdts 
845bc36eafdSMike Gerdts         Token = PrGetNextToken (NULL, " \"<>", Next);
846bc36eafdSMike Gerdts         if (!Token)
847bc36eafdSMike Gerdts         {
848bc36eafdSMike Gerdts             goto SyntaxError;
849bc36eafdSMike Gerdts         }
850bc36eafdSMike Gerdts 
851bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
852bc36eafdSMike Gerdts             "Start #include file \"%s\"\n", Gbl_CurrentLineNumber,
853bc36eafdSMike Gerdts             Token, Gbl_CurrentLineNumber);
854bc36eafdSMike Gerdts 
855bc36eafdSMike Gerdts         PrDoIncludeFile (Token);
856bc36eafdSMike Gerdts         break;
857bc36eafdSMike Gerdts 
858bc36eafdSMike Gerdts     case PR_DIRECTIVE_INCLUDEBUFFER:
859bc36eafdSMike Gerdts 
860bc36eafdSMike Gerdts         Token = PrGetNextToken (NULL, " \"<>", Next);
861bc36eafdSMike Gerdts         if (!Token)
862bc36eafdSMike Gerdts         {
863bc36eafdSMike Gerdts             goto SyntaxError;
864bc36eafdSMike Gerdts         }
865bc36eafdSMike Gerdts 
866bc36eafdSMike Gerdts         Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
867bc36eafdSMike Gerdts         if (!Token2)
868bc36eafdSMike Gerdts         {
869bc36eafdSMike Gerdts             goto SyntaxError;
870bc36eafdSMike Gerdts         }
871bc36eafdSMike Gerdts 
872bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
873bc36eafdSMike Gerdts             "Start #includebuffer input from file \"%s\", buffer name %s\n",
874bc36eafdSMike Gerdts             Gbl_CurrentLineNumber, Token, Token2);
875bc36eafdSMike Gerdts 
876bc36eafdSMike Gerdts         PrDoIncludeBuffer (Token, Token2);
877bc36eafdSMike Gerdts         break;
878bc36eafdSMike Gerdts 
879bc36eafdSMike Gerdts     case PR_DIRECTIVE_LINE:
880bc36eafdSMike Gerdts 
881bc36eafdSMike Gerdts         TokenOffset = Token - Gbl_MainTokenBuffer;
882bc36eafdSMike Gerdts 
883bc36eafdSMike Gerdts         Status = PrResolveIntegerExpression (
884bc36eafdSMike Gerdts             &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
885bc36eafdSMike Gerdts         if (ACPI_FAILURE (Status))
886bc36eafdSMike Gerdts         {
887bc36eafdSMike Gerdts             return;
888bc36eafdSMike Gerdts         }
889bc36eafdSMike Gerdts 
890bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
891bc36eafdSMike Gerdts             "User #line invocation %s\n", Gbl_CurrentLineNumber,
892bc36eafdSMike Gerdts             Token);
893bc36eafdSMike Gerdts 
894bc36eafdSMike Gerdts         Gbl_CurrentLineNumber = (UINT32) Value;
895bc36eafdSMike Gerdts 
896bc36eafdSMike Gerdts         /* Emit #line into the preprocessor file */
897bc36eafdSMike Gerdts 
898bc36eafdSMike Gerdts         FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
899bc36eafdSMike Gerdts             Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename);
900bc36eafdSMike Gerdts         break;
901bc36eafdSMike Gerdts 
902bc36eafdSMike Gerdts     case PR_DIRECTIVE_PRAGMA:
903bc36eafdSMike Gerdts 
904bc36eafdSMike Gerdts         if (!strcmp (Token, "disable"))
905bc36eafdSMike Gerdts         {
906bc36eafdSMike Gerdts             Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
907bc36eafdSMike Gerdts             if (!Token)
908bc36eafdSMike Gerdts             {
909bc36eafdSMike Gerdts                 goto SyntaxError;
910bc36eafdSMike Gerdts             }
911bc36eafdSMike Gerdts 
912bc36eafdSMike Gerdts             TokenOffset = Token - Gbl_MainTokenBuffer;
913bc36eafdSMike Gerdts             AslDisableException (&Gbl_CurrentLineBuffer[TokenOffset]);
914bc36eafdSMike Gerdts         }
915bc36eafdSMike Gerdts         else if (!strcmp (Token, "message"))
916bc36eafdSMike Gerdts         {
917bc36eafdSMike Gerdts             Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
918bc36eafdSMike Gerdts             if (!Token)
919bc36eafdSMike Gerdts             {
920bc36eafdSMike Gerdts                 goto SyntaxError;
921bc36eafdSMike Gerdts             }
922bc36eafdSMike Gerdts 
923bc36eafdSMike Gerdts             TokenOffset = Token - Gbl_MainTokenBuffer;
924bc36eafdSMike Gerdts             AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]);
925bc36eafdSMike Gerdts         }
926bc36eafdSMike Gerdts         else
927bc36eafdSMike Gerdts         {
928bc36eafdSMike Gerdts             PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
929bc36eafdSMike Gerdts                 THIS_TOKEN_OFFSET (Token));
930bc36eafdSMike Gerdts             return;
931bc36eafdSMike Gerdts         }
932bc36eafdSMike Gerdts 
933bc36eafdSMike Gerdts         break;
934bc36eafdSMike Gerdts 
935bc36eafdSMike Gerdts     case PR_DIRECTIVE_UNDEF:
936bc36eafdSMike Gerdts 
937bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
938bc36eafdSMike Gerdts             "#undef: %s\n", Gbl_CurrentLineNumber, Token);
939bc36eafdSMike Gerdts 
940bc36eafdSMike Gerdts         PrRemoveDefine (Token);
941bc36eafdSMike Gerdts         break;
942bc36eafdSMike Gerdts 
943bc36eafdSMike Gerdts     case PR_DIRECTIVE_WARNING:
944bc36eafdSMike Gerdts 
945bc36eafdSMike Gerdts         PrError (ASL_WARNING, ASL_MSG_WARNING_DIRECTIVE,
946bc36eafdSMike Gerdts             THIS_TOKEN_OFFSET (Token));
947bc36eafdSMike Gerdts 
948bc36eafdSMike Gerdts         Gbl_SourceLine = 0;
949bc36eafdSMike Gerdts         Gbl_NextError = Gbl_ErrorLog;
950bc36eafdSMike Gerdts         break;
951bc36eafdSMike Gerdts 
952bc36eafdSMike Gerdts     default:
953bc36eafdSMike Gerdts 
954bc36eafdSMike Gerdts         /* Should never get here */
955bc36eafdSMike Gerdts         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
956bc36eafdSMike Gerdts             "Unrecognized directive: %u\n",
957bc36eafdSMike Gerdts             Gbl_CurrentLineNumber, Directive);
958bc36eafdSMike Gerdts         break;
959bc36eafdSMike Gerdts     }
960bc36eafdSMike Gerdts 
961bc36eafdSMike Gerdts     return;
962bc36eafdSMike Gerdts 
963bc36eafdSMike Gerdts SyntaxError:
964bc36eafdSMike Gerdts 
965bc36eafdSMike Gerdts     PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
966bc36eafdSMike Gerdts         THIS_TOKEN_OFFSET (DirectiveToken));
967bc36eafdSMike Gerdts     return;
968bc36eafdSMike Gerdts }
969bc36eafdSMike Gerdts 
970bc36eafdSMike Gerdts 
971bc36eafdSMike Gerdts /*******************************************************************************
972bc36eafdSMike Gerdts  *
973bc36eafdSMike Gerdts  * FUNCTION:    PrGetNextLine, PrGetNextLineInit
974bc36eafdSMike Gerdts  *
975bc36eafdSMike Gerdts  * PARAMETERS:  Handle              - Open file handle for the source file
976bc36eafdSMike Gerdts  *
977bc36eafdSMike Gerdts  * RETURN:      Status of the GetLine operation:
978bc36eafdSMike Gerdts  *              AE_OK               - Normal line, OK status
979bc36eafdSMike Gerdts  *              ASL_IGNORE_LINE     - Line is blank or part of a multi-line
980bc36eafdSMike Gerdts  *                                      comment
981bc36eafdSMike Gerdts  *              ASL_EOF             - End-of-file reached
982bc36eafdSMike Gerdts  *
983bc36eafdSMike Gerdts  * DESCRIPTION: Get the next text line from the input file. Does not strip
984bc36eafdSMike Gerdts  *              comments.
985bc36eafdSMike Gerdts  *
986bc36eafdSMike Gerdts  ******************************************************************************/
987bc36eafdSMike Gerdts 
988bc36eafdSMike Gerdts #define PR_NORMAL_TEXT          0
989bc36eafdSMike Gerdts #define PR_MULTI_LINE_COMMENT   1
990bc36eafdSMike Gerdts #define PR_SINGLE_LINE_COMMENT  2
991bc36eafdSMike Gerdts #define PR_QUOTED_STRING        3
992bc36eafdSMike Gerdts 
993bc36eafdSMike Gerdts static UINT8                    AcpiGbl_LineScanState = PR_NORMAL_TEXT;
994bc36eafdSMike Gerdts 
995bc36eafdSMike Gerdts static void
PrGetNextLineInit(void)996bc36eafdSMike Gerdts PrGetNextLineInit (
997bc36eafdSMike Gerdts     void)
998bc36eafdSMike Gerdts {
999bc36eafdSMike Gerdts     AcpiGbl_LineScanState = 0;
1000bc36eafdSMike Gerdts }
1001bc36eafdSMike Gerdts 
1002bc36eafdSMike Gerdts static UINT32
PrGetNextLine(FILE * Handle)1003bc36eafdSMike Gerdts PrGetNextLine (
1004bc36eafdSMike Gerdts     FILE                    *Handle)
1005bc36eafdSMike Gerdts {
1006bc36eafdSMike Gerdts     UINT32                  i;
1007bc36eafdSMike Gerdts     int                     c = 0;
1008bc36eafdSMike Gerdts     int                     PreviousChar;
1009bc36eafdSMike Gerdts 
1010bc36eafdSMike Gerdts 
1011bc36eafdSMike Gerdts     /* Always clear the global line buffer */
1012bc36eafdSMike Gerdts 
1013bc36eafdSMike Gerdts     memset (Gbl_CurrentLineBuffer, 0, Gbl_LineBufferSize);
1014bc36eafdSMike Gerdts     for (i = 0; ;)
1015bc36eafdSMike Gerdts     {
1016bc36eafdSMike Gerdts         /*
1017bc36eafdSMike Gerdts          * If line is too long, expand the line buffers. Also increases
1018bc36eafdSMike Gerdts          * Gbl_LineBufferSize.
1019bc36eafdSMike Gerdts          */
1020bc36eafdSMike Gerdts         if (i >= Gbl_LineBufferSize)
1021bc36eafdSMike Gerdts         {
1022bc36eafdSMike Gerdts             UtExpandLineBuffers ();
1023bc36eafdSMike Gerdts         }
1024bc36eafdSMike Gerdts 
1025bc36eafdSMike Gerdts         PreviousChar = c;
1026bc36eafdSMike Gerdts         c = getc (Handle);
1027bc36eafdSMike Gerdts         if (c == EOF)
1028bc36eafdSMike Gerdts         {
1029bc36eafdSMike Gerdts             /*
1030bc36eafdSMike Gerdts              * On EOF: If there is anything in the line buffer, terminate
1031bc36eafdSMike Gerdts              * it with a newline, and catch the EOF on the next call
1032bc36eafdSMike Gerdts              * to this function.
1033bc36eafdSMike Gerdts              */
1034bc36eafdSMike Gerdts             if (i > 0)
1035bc36eafdSMike Gerdts             {
1036bc36eafdSMike Gerdts                 Gbl_CurrentLineBuffer[i] = '\n';
1037bc36eafdSMike Gerdts                 return (AE_OK);
1038bc36eafdSMike Gerdts             }
1039bc36eafdSMike Gerdts 
1040bc36eafdSMike Gerdts             return (ASL_EOF);
1041bc36eafdSMike Gerdts         }
1042bc36eafdSMike Gerdts 
1043bc36eafdSMike Gerdts         /* Update state machine as necessary */
1044bc36eafdSMike Gerdts 
1045bc36eafdSMike Gerdts         switch (AcpiGbl_LineScanState)
1046bc36eafdSMike Gerdts         {
1047bc36eafdSMike Gerdts         case PR_NORMAL_TEXT:
1048bc36eafdSMike Gerdts 
1049bc36eafdSMike Gerdts             /* Check for multi-line comment start */
1050bc36eafdSMike Gerdts 
1051bc36eafdSMike Gerdts             if ((PreviousChar == '/') && (c == '*'))
1052bc36eafdSMike Gerdts             {
1053bc36eafdSMike Gerdts                 AcpiGbl_LineScanState = PR_MULTI_LINE_COMMENT;
1054bc36eafdSMike Gerdts             }
1055bc36eafdSMike Gerdts 
1056bc36eafdSMike Gerdts             /* Check for single-line comment start */
1057bc36eafdSMike Gerdts 
1058bc36eafdSMike Gerdts             else if ((PreviousChar == '/') && (c == '/'))
1059bc36eafdSMike Gerdts             {
1060bc36eafdSMike Gerdts                 AcpiGbl_LineScanState = PR_SINGLE_LINE_COMMENT;
1061bc36eafdSMike Gerdts             }
1062bc36eafdSMike Gerdts 
1063bc36eafdSMike Gerdts             /* Check for quoted string start */
1064bc36eafdSMike Gerdts 
1065bc36eafdSMike Gerdts             else if (PreviousChar == '"')
1066bc36eafdSMike Gerdts             {
1067bc36eafdSMike Gerdts                 AcpiGbl_LineScanState = PR_QUOTED_STRING;
1068bc36eafdSMike Gerdts             }
1069bc36eafdSMike Gerdts             break;
1070bc36eafdSMike Gerdts 
1071bc36eafdSMike Gerdts         case PR_QUOTED_STRING:
1072bc36eafdSMike Gerdts 
1073bc36eafdSMike Gerdts             if (PreviousChar == '"')
1074bc36eafdSMike Gerdts             {
1075bc36eafdSMike Gerdts                 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1076bc36eafdSMike Gerdts             }
1077bc36eafdSMike Gerdts             break;
1078bc36eafdSMike Gerdts 
1079bc36eafdSMike Gerdts         case PR_MULTI_LINE_COMMENT:
1080bc36eafdSMike Gerdts 
1081bc36eafdSMike Gerdts             /* Check for multi-line comment end */
1082bc36eafdSMike Gerdts 
1083bc36eafdSMike Gerdts             if ((PreviousChar == '*') && (c == '/'))
1084bc36eafdSMike Gerdts             {
1085bc36eafdSMike Gerdts                 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1086bc36eafdSMike Gerdts             }
1087bc36eafdSMike Gerdts             break;
1088bc36eafdSMike Gerdts 
1089bc36eafdSMike Gerdts         case PR_SINGLE_LINE_COMMENT: /* Just ignore text until EOL */
1090bc36eafdSMike Gerdts         default:
1091bc36eafdSMike Gerdts             break;
1092bc36eafdSMike Gerdts         }
1093bc36eafdSMike Gerdts 
1094bc36eafdSMike Gerdts         /* Always copy the character into line buffer */
1095bc36eafdSMike Gerdts 
1096bc36eafdSMike Gerdts         Gbl_CurrentLineBuffer[i] = (char) c;
1097bc36eafdSMike Gerdts         i++;
1098bc36eafdSMike Gerdts 
1099bc36eafdSMike Gerdts         /* Always exit on end-of-line */
1100bc36eafdSMike Gerdts 
1101bc36eafdSMike Gerdts         if (c == '\n')
1102bc36eafdSMike Gerdts         {
1103bc36eafdSMike Gerdts             /* Handle multi-line comments */
1104bc36eafdSMike Gerdts 
1105bc36eafdSMike Gerdts             if (AcpiGbl_LineScanState == PR_MULTI_LINE_COMMENT)
1106bc36eafdSMike Gerdts             {
1107bc36eafdSMike Gerdts                 return (ASL_IGNORE_LINE);
1108bc36eafdSMike Gerdts             }
1109bc36eafdSMike Gerdts 
1110bc36eafdSMike Gerdts             /* End of single-line comment */
1111bc36eafdSMike Gerdts 
1112bc36eafdSMike Gerdts             if (AcpiGbl_LineScanState == PR_SINGLE_LINE_COMMENT)
1113bc36eafdSMike Gerdts             {
1114bc36eafdSMike Gerdts                 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
1115bc36eafdSMike Gerdts                 return (AE_OK);
1116bc36eafdSMike Gerdts             }
1117bc36eafdSMike Gerdts 
1118bc36eafdSMike Gerdts             /* Blank line */
1119bc36eafdSMike Gerdts 
1120bc36eafdSMike Gerdts             if (i == 1)
1121bc36eafdSMike Gerdts             {
1122bc36eafdSMike Gerdts                 return (ASL_IGNORE_LINE);
1123bc36eafdSMike Gerdts             }
1124bc36eafdSMike Gerdts 
1125bc36eafdSMike Gerdts             return (AE_OK);
1126bc36eafdSMike Gerdts         }
1127bc36eafdSMike Gerdts     }
1128bc36eafdSMike Gerdts }
1129bc36eafdSMike Gerdts 
1130bc36eafdSMike Gerdts 
1131bc36eafdSMike Gerdts /*******************************************************************************
1132bc36eafdSMike Gerdts  *
1133bc36eafdSMike Gerdts  * FUNCTION:    PrMatchDirective
1134bc36eafdSMike Gerdts  *
1135bc36eafdSMike Gerdts  * PARAMETERS:  Directive           - Pointer to directive name token
1136bc36eafdSMike Gerdts  *
1137bc36eafdSMike Gerdts  * RETURN:      Index into command array, -1 if not found
1138bc36eafdSMike Gerdts  *
1139bc36eafdSMike Gerdts  * DESCRIPTION: Lookup the incoming directive in the known directives table.
1140bc36eafdSMike Gerdts  *
1141bc36eafdSMike Gerdts  ******************************************************************************/
1142bc36eafdSMike Gerdts 
1143bc36eafdSMike Gerdts static int
PrMatchDirective(char * Directive)1144bc36eafdSMike Gerdts PrMatchDirective (
1145bc36eafdSMike Gerdts     char                    *Directive)
1146bc36eafdSMike Gerdts {
1147bc36eafdSMike Gerdts     int                     i;
1148bc36eafdSMike Gerdts 
1149bc36eafdSMike Gerdts 
1150bc36eafdSMike Gerdts     if (!Directive || Directive[0] == 0)
1151bc36eafdSMike Gerdts     {
1152bc36eafdSMike Gerdts         return (ASL_DIRECTIVE_NOT_FOUND);
1153bc36eafdSMike Gerdts     }
1154bc36eafdSMike Gerdts 
1155bc36eafdSMike Gerdts     for (i = 0; Gbl_DirectiveInfo[i].Name; i++)
1156bc36eafdSMike Gerdts     {
1157bc36eafdSMike Gerdts         if (!strcmp (Gbl_DirectiveInfo[i].Name, Directive))
1158bc36eafdSMike Gerdts         {
1159bc36eafdSMike Gerdts             return (i);
1160bc36eafdSMike Gerdts         }
1161bc36eafdSMike Gerdts     }
1162bc36eafdSMike Gerdts 
1163bc36eafdSMike Gerdts     return (ASL_DIRECTIVE_NOT_FOUND);    /* Command not recognized */
1164bc36eafdSMike Gerdts }
1165bc36eafdSMike Gerdts 
1166bc36eafdSMike Gerdts 
1167bc36eafdSMike Gerdts /*******************************************************************************
1168bc36eafdSMike Gerdts  *
1169bc36eafdSMike Gerdts  * FUNCTION:    PrPushDirective
1170bc36eafdSMike Gerdts  *
1171bc36eafdSMike Gerdts  * PARAMETERS:  Directive           - Encoded directive ID
1172bc36eafdSMike Gerdts  *              Argument            - String containing argument to the
1173bc36eafdSMike Gerdts  *                                    directive
1174bc36eafdSMike Gerdts  *
1175bc36eafdSMike Gerdts  * RETURN:      None
1176bc36eafdSMike Gerdts  *
1177bc36eafdSMike Gerdts  * DESCRIPTION: Push an item onto the directive stack. Used for processing
1178bc36eafdSMike Gerdts  *              nested #if/#else type conditional compilation directives.
1179bc36eafdSMike Gerdts  *              Specifically: Used on detection of #if/#ifdef/#ifndef to open
1180bc36eafdSMike Gerdts  *              a block.
1181bc36eafdSMike Gerdts  *
1182bc36eafdSMike Gerdts  ******************************************************************************/
1183bc36eafdSMike Gerdts 
1184bc36eafdSMike Gerdts static void
PrPushDirective(int Directive,char * Argument)1185bc36eafdSMike Gerdts PrPushDirective (
1186bc36eafdSMike Gerdts     int                     Directive,
1187bc36eafdSMike Gerdts     char                    *Argument)
1188bc36eafdSMike Gerdts {
1189bc36eafdSMike Gerdts     DIRECTIVE_INFO          *Info;
1190bc36eafdSMike Gerdts 
1191bc36eafdSMike Gerdts 
1192bc36eafdSMike Gerdts     /* Allocate and populate a stack info item */
1193bc36eafdSMike Gerdts 
1194bc36eafdSMike Gerdts     Info = ACPI_ALLOCATE (sizeof (DIRECTIVE_INFO));
1195bc36eafdSMike Gerdts 
1196bc36eafdSMike Gerdts     Info->Next = Gbl_DirectiveStack;
1197bc36eafdSMike Gerdts     Info->Directive = Directive;
1198bc36eafdSMike Gerdts     Info->IgnoringThisCodeBlock = Gbl_IgnoringThisCodeBlock;
1199*35786f68SRobert Mustacchi     AcpiUtSafeStrncpy (Info->Argument, Argument, MAX_ARGUMENT_LENGTH);
1200bc36eafdSMike Gerdts 
1201bc36eafdSMike Gerdts     DbgPrint (ASL_DEBUG_OUTPUT,
1202bc36eafdSMike Gerdts         "Pr(%.4u) - [%u %s] %*s Pushed [#%s %s]: IgnoreFlag = %s\n",
1203bc36eafdSMike Gerdts         Gbl_CurrentLineNumber, Gbl_IfDepth,
1204bc36eafdSMike Gerdts         Gbl_IgnoringThisCodeBlock ? "I" : "E",
1205bc36eafdSMike Gerdts         Gbl_IfDepth * 4, " ",
1206bc36eafdSMike Gerdts         Gbl_DirectiveInfo[Directive].Name,
1207bc36eafdSMike Gerdts         Argument, Gbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
1208bc36eafdSMike Gerdts 
1209bc36eafdSMike Gerdts     /* Push new item */
1210bc36eafdSMike Gerdts 
1211bc36eafdSMike Gerdts     Gbl_DirectiveStack = Info;
1212bc36eafdSMike Gerdts     Gbl_IfDepth++;
1213bc36eafdSMike Gerdts }
1214bc36eafdSMike Gerdts 
1215bc36eafdSMike Gerdts 
1216bc36eafdSMike Gerdts /*******************************************************************************
1217bc36eafdSMike Gerdts  *
1218bc36eafdSMike Gerdts  * FUNCTION:    PrPopDirective
1219bc36eafdSMike Gerdts  *
1220bc36eafdSMike Gerdts  * PARAMETERS:  None
1221bc36eafdSMike Gerdts  *
1222bc36eafdSMike Gerdts  * RETURN:      Status. Error if the stack is empty.
1223bc36eafdSMike Gerdts  *
1224bc36eafdSMike Gerdts  * DESCRIPTION: Pop an item off the directive stack. Used for processing
1225bc36eafdSMike Gerdts  *              nested #if/#else type conditional compilation directives.
1226bc36eafdSMike Gerdts  *              Specifically: Used on detection of #elif and #endif to remove
1227bc36eafdSMike Gerdts  *              the original #if/#ifdef/#ifndef from the stack and close
1228bc36eafdSMike Gerdts  *              the block.
1229bc36eafdSMike Gerdts  *
1230bc36eafdSMike Gerdts  ******************************************************************************/
1231bc36eafdSMike Gerdts 
1232bc36eafdSMike Gerdts static ACPI_STATUS
PrPopDirective(void)1233bc36eafdSMike Gerdts PrPopDirective (
1234bc36eafdSMike Gerdts     void)
1235bc36eafdSMike Gerdts {
1236bc36eafdSMike Gerdts     DIRECTIVE_INFO          *Info;
1237bc36eafdSMike Gerdts 
1238bc36eafdSMike Gerdts 
1239bc36eafdSMike Gerdts     /* Check for empty stack */
1240bc36eafdSMike Gerdts 
1241bc36eafdSMike Gerdts     Info = Gbl_DirectiveStack;
1242bc36eafdSMike Gerdts     if (!Info)
1243bc36eafdSMike Gerdts     {
1244bc36eafdSMike Gerdts         return (AE_ERROR);
1245bc36eafdSMike Gerdts     }
1246bc36eafdSMike Gerdts 
1247bc36eafdSMike Gerdts     /* Pop one item, keep globals up-to-date */
1248bc36eafdSMike Gerdts 
1249bc36eafdSMike Gerdts     Gbl_IfDepth--;
1250bc36eafdSMike Gerdts     Gbl_IgnoringThisCodeBlock = Info->IgnoringThisCodeBlock;
1251bc36eafdSMike Gerdts     Gbl_DirectiveStack = Info->Next;
1252bc36eafdSMike Gerdts 
1253bc36eafdSMike Gerdts     DbgPrint (ASL_DEBUG_OUTPUT,
1254bc36eafdSMike Gerdts         "Pr(%.4u) - [%u %s] %*s Popped [#%s %s]: IgnoreFlag now = %s\n",
1255bc36eafdSMike Gerdts         Gbl_CurrentLineNumber, Gbl_IfDepth,
1256bc36eafdSMike Gerdts         Gbl_IgnoringThisCodeBlock ? "I" : "E",
1257bc36eafdSMike Gerdts         Gbl_IfDepth * 4, " ",
1258bc36eafdSMike Gerdts         Gbl_DirectiveInfo[Info->Directive].Name,
1259bc36eafdSMike Gerdts         Info->Argument, Gbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
1260bc36eafdSMike Gerdts 
1261bc36eafdSMike Gerdts     ACPI_FREE (Info);
1262bc36eafdSMike Gerdts     return (AE_OK);
1263bc36eafdSMike Gerdts }
1264bc36eafdSMike Gerdts 
1265bc36eafdSMike Gerdts 
1266bc36eafdSMike Gerdts /*******************************************************************************
1267bc36eafdSMike Gerdts  *
1268bc36eafdSMike Gerdts  * FUNCTION:    PrDbgPrint
1269bc36eafdSMike Gerdts  *
1270bc36eafdSMike Gerdts  * PARAMETERS:  Action              - Action being performed
1271bc36eafdSMike Gerdts  *              DirectiveName       - Directive being processed
1272bc36eafdSMike Gerdts  *
1273bc36eafdSMike Gerdts  * RETURN:      None
1274bc36eafdSMike Gerdts  *
1275bc36eafdSMike Gerdts  * DESCRIPTION: Special debug print for directive processing.
1276bc36eafdSMike Gerdts  *
1277bc36eafdSMike Gerdts  ******************************************************************************/
1278bc36eafdSMike Gerdts 
1279bc36eafdSMike Gerdts static void
PrDbgPrint(char * Action,char * DirectiveName)1280bc36eafdSMike Gerdts PrDbgPrint (
1281bc36eafdSMike Gerdts     char                    *Action,
1282bc36eafdSMike Gerdts     char                    *DirectiveName)
1283bc36eafdSMike Gerdts {
1284bc36eafdSMike Gerdts 
1285bc36eafdSMike Gerdts     DbgPrint (ASL_DEBUG_OUTPUT, "Pr(%.4u) - [%u %s] "
1286bc36eafdSMike Gerdts         "%*s %s #%s, IfDepth %u\n",
1287bc36eafdSMike Gerdts         Gbl_CurrentLineNumber, Gbl_IfDepth,
1288bc36eafdSMike Gerdts         Gbl_IgnoringThisCodeBlock ? "I" : "E",
1289bc36eafdSMike Gerdts         Gbl_IfDepth * 4, " ",
1290bc36eafdSMike Gerdts         Action, DirectiveName, Gbl_IfDepth);
1291bc36eafdSMike Gerdts }
1292bc36eafdSMike Gerdts 
1293bc36eafdSMike Gerdts 
1294bc36eafdSMike Gerdts /*******************************************************************************
1295bc36eafdSMike Gerdts  *
1296bc36eafdSMike Gerdts  * FUNCTION:    PrDoIncludeFile
1297bc36eafdSMike Gerdts  *
1298bc36eafdSMike Gerdts  * PARAMETERS:  Pathname                - Name of the input file
1299bc36eafdSMike Gerdts  *
1300bc36eafdSMike Gerdts  * RETURN:      None.
1301bc36eafdSMike Gerdts  *
1302bc36eafdSMike Gerdts  * DESCRIPTION: Open an include file, from #include.
1303bc36eafdSMike Gerdts  *
1304bc36eafdSMike Gerdts  ******************************************************************************/
1305bc36eafdSMike Gerdts 
1306bc36eafdSMike Gerdts static void
PrDoIncludeFile(char * Pathname)1307bc36eafdSMike Gerdts PrDoIncludeFile (
1308bc36eafdSMike Gerdts     char                    *Pathname)
1309bc36eafdSMike Gerdts {
1310bc36eafdSMike Gerdts     char                    *FullPathname;
1311bc36eafdSMike Gerdts 
1312bc36eafdSMike Gerdts 
1313bc36eafdSMike Gerdts     (void) PrOpenIncludeFile (Pathname, "r", &FullPathname);
1314bc36eafdSMike Gerdts }
1315bc36eafdSMike Gerdts 
1316bc36eafdSMike Gerdts 
1317bc36eafdSMike Gerdts /*******************************************************************************
1318bc36eafdSMike Gerdts  *
1319bc36eafdSMike Gerdts  * FUNCTION:    PrDoIncludeBuffer
1320bc36eafdSMike Gerdts  *
1321bc36eafdSMike Gerdts  * PARAMETERS:  Pathname                - Name of the input binary file
1322bc36eafdSMike Gerdts  *              BufferName              - ACPI namepath of the buffer
1323bc36eafdSMike Gerdts  *
1324bc36eafdSMike Gerdts  * RETURN:      None.
1325bc36eafdSMike Gerdts  *
1326bc36eafdSMike Gerdts  * DESCRIPTION: Create an ACPI buffer object from a binary file. The contents
1327bc36eafdSMike Gerdts  *              of the file are emitted into the buffer object as ascii
1328bc36eafdSMike Gerdts  *              hex data. From #includebuffer.
1329bc36eafdSMike Gerdts  *
1330bc36eafdSMike Gerdts  ******************************************************************************/
1331bc36eafdSMike Gerdts 
1332bc36eafdSMike Gerdts static void
PrDoIncludeBuffer(char * Pathname,char * BufferName)1333bc36eafdSMike Gerdts PrDoIncludeBuffer (
1334bc36eafdSMike Gerdts     char                    *Pathname,
1335bc36eafdSMike Gerdts     char                    *BufferName)
1336bc36eafdSMike Gerdts {
1337bc36eafdSMike Gerdts     char                    *FullPathname;
1338bc36eafdSMike Gerdts     FILE                    *BinaryBufferFile;
1339bc36eafdSMike Gerdts     UINT32                  i = 0;
1340bc36eafdSMike Gerdts     UINT8                   c;
1341bc36eafdSMike Gerdts 
1342bc36eafdSMike Gerdts 
1343bc36eafdSMike Gerdts     BinaryBufferFile = PrOpenIncludeFile (Pathname, "rb", &FullPathname);
1344bc36eafdSMike Gerdts     if (!BinaryBufferFile)
1345bc36eafdSMike Gerdts     {
1346bc36eafdSMike Gerdts         return;
1347bc36eafdSMike Gerdts     }
1348bc36eafdSMike Gerdts 
1349bc36eafdSMike Gerdts     /* Emit "Name (XXXX, Buffer() {" header */
1350bc36eafdSMike Gerdts 
1351bc36eafdSMike Gerdts     FlPrintFile (ASL_FILE_PREPROCESSOR, "Name (%s, Buffer()\n{", BufferName);
1352bc36eafdSMike Gerdts 
1353bc36eafdSMike Gerdts     /* Dump the entire file in ascii hex format */
1354bc36eafdSMike Gerdts 
1355bc36eafdSMike Gerdts     while (fread (&c, 1, 1, BinaryBufferFile))
1356bc36eafdSMike Gerdts     {
1357bc36eafdSMike Gerdts         if (!(i % 8))
1358bc36eafdSMike Gerdts         {
1359bc36eafdSMike Gerdts             FlPrintFile (ASL_FILE_PREPROCESSOR, "\n   ", c);
1360bc36eafdSMike Gerdts         }
1361bc36eafdSMike Gerdts 
1362bc36eafdSMike Gerdts         FlPrintFile (ASL_FILE_PREPROCESSOR, " 0x%2.2X,", c);
1363bc36eafdSMike Gerdts         i++;
1364bc36eafdSMike Gerdts     }
1365bc36eafdSMike Gerdts 
1366bc36eafdSMike Gerdts     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
1367bc36eafdSMike Gerdts         "#includebuffer: read %u bytes from %s\n",
1368bc36eafdSMike Gerdts         Gbl_CurrentLineNumber, i, FullPathname);
1369bc36eafdSMike Gerdts 
1370bc36eafdSMike Gerdts     /* Close the Name() operator */
1371bc36eafdSMike Gerdts 
1372bc36eafdSMike Gerdts     FlPrintFile (ASL_FILE_PREPROCESSOR, "\n})\n", BufferName);
1373bc36eafdSMike Gerdts     fclose (BinaryBufferFile);
1374bc36eafdSMike Gerdts }
1375