prscan.c revision 1.1 1 1.1 christos /******************************************************************************
2 1.1 christos *
3 1.1 christos * Module Name: prscan - Preprocessor start-up and file scan module
4 1.1 christos *
5 1.1 christos *****************************************************************************/
6 1.1 christos
7 1.1 christos /*
8 1.1 christos * Copyright (C) 2000 - 2013, Intel Corp.
9 1.1 christos * All rights reserved.
10 1.1 christos *
11 1.1 christos * Redistribution and use in source and binary forms, with or without
12 1.1 christos * modification, are permitted provided that the following conditions
13 1.1 christos * are met:
14 1.1 christos * 1. Redistributions of source code must retain the above copyright
15 1.1 christos * notice, this list of conditions, and the following disclaimer,
16 1.1 christos * without modification.
17 1.1 christos * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1 christos * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1 christos * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1 christos * including a substantially similar Disclaimer requirement for further
21 1.1 christos * binary redistribution.
22 1.1 christos * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1 christos * of any contributors may be used to endorse or promote products derived
24 1.1 christos * from this software without specific prior written permission.
25 1.1 christos *
26 1.1 christos * Alternatively, this software may be distributed under the terms of the
27 1.1 christos * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1 christos * Software Foundation.
29 1.1 christos *
30 1.1 christos * NO WARRANTY
31 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1 christos * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1 christos * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1 christos * POSSIBILITY OF SUCH DAMAGES.
42 1.1 christos */
43 1.1 christos
44 1.1 christos #define _DECLARE_PR_GLOBALS
45 1.1 christos
46 1.1 christos #include "aslcompiler.h"
47 1.1 christos #include "dtcompiler.h"
48 1.1 christos
49 1.1 christos /*
50 1.1 christos * TBDs:
51 1.1 christos *
52 1.1 christos * No nested macros, maybe never
53 1.1 christos * Implement ASL "Include" as well as "#include" here?
54 1.1 christos */
55 1.1 christos #define _COMPONENT ASL_PREPROCESSOR
56 1.1 christos ACPI_MODULE_NAME ("prscan")
57 1.1 christos
58 1.1 christos
59 1.1 christos /* Local prototypes */
60 1.1 christos
61 1.1 christos static void
62 1.1 christos PrPreprocessInputFile (
63 1.1 christos void);
64 1.1 christos
65 1.1 christos static void
66 1.1 christos PrDoDirective (
67 1.1 christos char *DirectiveToken,
68 1.1 christos char **Next);
69 1.1 christos
70 1.1 christos static int
71 1.1 christos PrMatchDirective (
72 1.1 christos char *Directive);
73 1.1 christos
74 1.1 christos static void
75 1.1 christos PrPushDirective (
76 1.1 christos int Directive,
77 1.1 christos char *Argument);
78 1.1 christos
79 1.1 christos static ACPI_STATUS
80 1.1 christos PrPopDirective (
81 1.1 christos void);
82 1.1 christos
83 1.1 christos static void
84 1.1 christos PrDbgPrint (
85 1.1 christos char *Action,
86 1.1 christos char *DirectiveName);
87 1.1 christos
88 1.1 christos
89 1.1 christos /*
90 1.1 christos * Supported preprocessor directives
91 1.1 christos */
92 1.1 christos static const PR_DIRECTIVE_INFO Gbl_DirectiveInfo[] =
93 1.1 christos {
94 1.1 christos {"define", 1},
95 1.1 christos {"elif", 0}, /* Converted to #else..#if internally */
96 1.1 christos {"else", 0},
97 1.1 christos {"endif", 0},
98 1.1 christos {"error", 1},
99 1.1 christos {"if", 1},
100 1.1 christos {"ifdef", 1},
101 1.1 christos {"ifndef", 1},
102 1.1 christos {"include", 0}, /* Argument is not standard format, so 0 */
103 1.1 christos {"line", 1},
104 1.1 christos {"pragma", 1},
105 1.1 christos {"undef", 1},
106 1.1 christos {"warning", 1},
107 1.1 christos {NULL, 0}
108 1.1 christos };
109 1.1 christos
110 1.1 christos enum Gbl_DirectiveIndexes
111 1.1 christos {
112 1.1 christos PR_DIRECTIVE_DEFINE = 0,
113 1.1 christos PR_DIRECTIVE_ELIF,
114 1.1 christos PR_DIRECTIVE_ELSE,
115 1.1 christos PR_DIRECTIVE_ENDIF,
116 1.1 christos PR_DIRECTIVE_ERROR,
117 1.1 christos PR_DIRECTIVE_IF,
118 1.1 christos PR_DIRECTIVE_IFDEF,
119 1.1 christos PR_DIRECTIVE_IFNDEF,
120 1.1 christos PR_DIRECTIVE_INCLUDE,
121 1.1 christos PR_DIRECTIVE_LINE,
122 1.1 christos PR_DIRECTIVE_PRAGMA,
123 1.1 christos PR_DIRECTIVE_UNDEF,
124 1.1 christos PR_DIRECTIVE_WARNING,
125 1.1 christos };
126 1.1 christos
127 1.1 christos #define ASL_DIRECTIVE_NOT_FOUND -1
128 1.1 christos
129 1.1 christos
130 1.1 christos /*******************************************************************************
131 1.1 christos *
132 1.1 christos * FUNCTION: PrInitializePreprocessor
133 1.1 christos *
134 1.1 christos * PARAMETERS: None
135 1.1 christos *
136 1.1 christos * RETURN: None
137 1.1 christos *
138 1.1 christos * DESCRIPTION: Startup initialization for the Preprocessor.
139 1.1 christos *
140 1.1 christos ******************************************************************************/
141 1.1 christos
142 1.1 christos void
143 1.1 christos PrInitializePreprocessor (
144 1.1 christos void)
145 1.1 christos {
146 1.1 christos /* Init globals and the list of #defines */
147 1.1 christos
148 1.1 christos PrInitializeGlobals ();
149 1.1 christos Gbl_DefineList = NULL;
150 1.1 christos }
151 1.1 christos
152 1.1 christos
153 1.1 christos /*******************************************************************************
154 1.1 christos *
155 1.1 christos * FUNCTION: PrInitializeGlobals
156 1.1 christos *
157 1.1 christos * PARAMETERS: None
158 1.1 christos *
159 1.1 christos * RETURN: None
160 1.1 christos *
161 1.1 christos * DESCRIPTION: Initialize globals for the Preprocessor. Used for startuup
162 1.1 christos * initialization and re-initialization between compiles during
163 1.1 christos * a multiple source file compile.
164 1.1 christos *
165 1.1 christos ******************************************************************************/
166 1.1 christos
167 1.1 christos void
168 1.1 christos PrInitializeGlobals (
169 1.1 christos void)
170 1.1 christos {
171 1.1 christos /* Init globals */
172 1.1 christos
173 1.1 christos Gbl_InputFileList = NULL;
174 1.1 christos Gbl_CurrentLineNumber = 0;
175 1.1 christos Gbl_PreprocessorLineNumber = 1;
176 1.1 christos Gbl_PreprocessorError = FALSE;
177 1.1 christos
178 1.1 christos /* These are used to track #if/#else blocks (possibly nested) */
179 1.1 christos
180 1.1 christos Gbl_IfDepth = 0;
181 1.1 christos Gbl_IgnoringThisCodeBlock = FALSE;
182 1.1 christos Gbl_DirectiveStack = NULL;
183 1.1 christos }
184 1.1 christos
185 1.1 christos
186 1.1 christos /*******************************************************************************
187 1.1 christos *
188 1.1 christos * FUNCTION: PrTerminatePreprocessor
189 1.1 christos *
190 1.1 christos * PARAMETERS: None
191 1.1 christos *
192 1.1 christos * RETURN: None
193 1.1 christos *
194 1.1 christos * DESCRIPTION: Termination of the preprocessor. Delete lists. Keep any
195 1.1 christos * defines that were specified on the command line, in order to
196 1.1 christos * support multiple compiles with a single compiler invocation.
197 1.1 christos *
198 1.1 christos ******************************************************************************/
199 1.1 christos
200 1.1 christos void
201 1.1 christos PrTerminatePreprocessor (
202 1.1 christos void)
203 1.1 christos {
204 1.1 christos PR_DEFINE_INFO *DefineInfo;
205 1.1 christos
206 1.1 christos
207 1.1 christos /*
208 1.1 christos * The persistent defines (created on the command line) are always at the
209 1.1 christos * end of the list. We save them.
210 1.1 christos */
211 1.1 christos while ((Gbl_DefineList) && (!Gbl_DefineList->Persist))
212 1.1 christos {
213 1.1 christos DefineInfo = Gbl_DefineList;
214 1.1 christos Gbl_DefineList = DefineInfo->Next;
215 1.1 christos
216 1.1 christos ACPI_FREE (DefineInfo->Replacement);
217 1.1 christos ACPI_FREE (DefineInfo->Identifier);
218 1.1 christos ACPI_FREE (DefineInfo);
219 1.1 christos }
220 1.1 christos }
221 1.1 christos
222 1.1 christos
223 1.1 christos /*******************************************************************************
224 1.1 christos *
225 1.1 christos * FUNCTION: PrDoPreprocess
226 1.1 christos *
227 1.1 christos * PARAMETERS: None
228 1.1 christos *
229 1.1 christos * RETURN: None
230 1.1 christos *
231 1.1 christos * DESCRIPTION: Main entry point for the iASL Preprocessor. Input file must
232 1.1 christos * be already open. Handles multiple input files via the
233 1.1 christos * #include directive.
234 1.1 christos *
235 1.1 christos ******************************************************************************/
236 1.1 christos
237 1.1 christos void
238 1.1 christos PrDoPreprocess (
239 1.1 christos void)
240 1.1 christos {
241 1.1 christos BOOLEAN MoreInputFiles;
242 1.1 christos
243 1.1 christos
244 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
245 1.1 christos
246 1.1 christos
247 1.1 christos FlSeekFile (ASL_FILE_INPUT, 0);
248 1.1 christos PrDumpPredefinedNames ();
249 1.1 christos
250 1.1 christos /* Main preprocessor loop, handles include files */
251 1.1 christos
252 1.1 christos do
253 1.1 christos {
254 1.1 christos PrPreprocessInputFile ();
255 1.1 christos MoreInputFiles = PrPopInputFileStack ();
256 1.1 christos
257 1.1 christos } while (MoreInputFiles);
258 1.1 christos
259 1.1 christos /* Point compiler input to the new preprocessor output file (.i) */
260 1.1 christos
261 1.1 christos FlCloseFile (ASL_FILE_INPUT);
262 1.1 christos Gbl_Files[ASL_FILE_INPUT].Handle = Gbl_Files[ASL_FILE_PREPROCESSOR].Handle;
263 1.1 christos AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
264 1.1 christos
265 1.1 christos /* Reset globals to allow compiler to run */
266 1.1 christos
267 1.1 christos FlSeekFile (ASL_FILE_INPUT, 0);
268 1.1 christos Gbl_CurrentLineNumber = 1;
269 1.1 christos
270 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
271 1.1 christos }
272 1.1 christos
273 1.1 christos
274 1.1 christos /*******************************************************************************
275 1.1 christos *
276 1.1 christos * FUNCTION: PrPreprocessInputFile
277 1.1 christos *
278 1.1 christos * PARAMETERS: None
279 1.1 christos *
280 1.1 christos * RETURN: None
281 1.1 christos *
282 1.1 christos * DESCRIPTION: Preprocess one entire file, line-by-line.
283 1.1 christos *
284 1.1 christos * Input: Raw user ASL from ASL_FILE_INPUT
285 1.1 christos * Output: Preprocessed file written to ASL_FILE_PREPROCESSOR
286 1.1 christos *
287 1.1 christos ******************************************************************************/
288 1.1 christos
289 1.1 christos static void
290 1.1 christos PrPreprocessInputFile (
291 1.1 christos void)
292 1.1 christos {
293 1.1 christos UINT32 Offset;
294 1.1 christos char *Token;
295 1.1 christos char *ReplaceString;
296 1.1 christos PR_DEFINE_INFO *DefineInfo;
297 1.1 christos ACPI_SIZE TokenOffset;
298 1.1 christos char *Next;
299 1.1 christos int OffsetAdjust;
300 1.1 christos
301 1.1 christos
302 1.1 christos /* Scan line-by-line. Comments and blank lines are skipped by this function */
303 1.1 christos
304 1.1 christos while ((Offset = DtGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
305 1.1 christos {
306 1.1 christos /* Need a copy of the input line for strok() */
307 1.1 christos
308 1.1 christos strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer);
309 1.1 christos Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
310 1.1 christos OffsetAdjust = 0;
311 1.1 christos
312 1.1 christos /* All preprocessor directives must begin with '#' */
313 1.1 christos
314 1.1 christos if (Token && (*Token == '#'))
315 1.1 christos {
316 1.1 christos if (strlen (Token) == 1)
317 1.1 christos {
318 1.1 christos Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
319 1.1 christos }
320 1.1 christos else
321 1.1 christos {
322 1.1 christos Token++; /* Skip leading # */
323 1.1 christos }
324 1.1 christos
325 1.1 christos /* Execute the directive, do not write line to output file */
326 1.1 christos
327 1.1 christos PrDoDirective (Token, &Next);
328 1.1 christos continue;
329 1.1 christos }
330 1.1 christos
331 1.1 christos /*
332 1.1 christos * If we are currently within the part of an IF/ELSE block that is
333 1.1 christos * FALSE, ignore the line and do not write it to the output file.
334 1.1 christos * This continues until an #else or #endif is encountered.
335 1.1 christos */
336 1.1 christos if (Gbl_IgnoringThisCodeBlock)
337 1.1 christos {
338 1.1 christos continue;
339 1.1 christos }
340 1.1 christos
341 1.1 christos /* Match and replace all #defined names within this source line */
342 1.1 christos
343 1.1 christos while (Token)
344 1.1 christos {
345 1.1 christos DefineInfo = PrMatchDefine (Token);
346 1.1 christos if (DefineInfo)
347 1.1 christos {
348 1.1 christos if (DefineInfo->Body)
349 1.1 christos {
350 1.1 christos /* This is a macro */
351 1.1 christos
352 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
353 1.1 christos "Matched Macro: %s->%s\n",
354 1.1 christos Gbl_CurrentLineNumber, DefineInfo->Identifier,
355 1.1 christos DefineInfo->Replacement);
356 1.1 christos
357 1.1 christos PrDoMacroInvocation (Gbl_MainTokenBuffer, Token,
358 1.1 christos DefineInfo, &Next);
359 1.1 christos }
360 1.1 christos else
361 1.1 christos {
362 1.1 christos ReplaceString = DefineInfo->Replacement;
363 1.1 christos
364 1.1 christos /* Replace the name in the original line buffer */
365 1.1 christos
366 1.1 christos TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust;
367 1.1 christos PrReplaceData (
368 1.1 christos &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token),
369 1.1 christos ReplaceString, strlen (ReplaceString));
370 1.1 christos
371 1.1 christos /* Adjust for length difference between old and new name length */
372 1.1 christos
373 1.1 christos OffsetAdjust += strlen (ReplaceString) - strlen (Token);
374 1.1 christos
375 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
376 1.1 christos "Matched #define: %s->%s\n",
377 1.1 christos Gbl_CurrentLineNumber, Token,
378 1.1 christos *ReplaceString ? ReplaceString : "(NULL STRING)");
379 1.1 christos }
380 1.1 christos }
381 1.1 christos
382 1.1 christos Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
383 1.1 christos }
384 1.1 christos
385 1.1 christos #if 0
386 1.1 christos /* Line prefix */
387 1.1 christos FlPrintFile (ASL_FILE_PREPROCESSOR, "/* %14s %.5u i:%.5u */ ",
388 1.1 christos Gbl_Files[ASL_FILE_INPUT].Filename,
389 1.1 christos Gbl_CurrentLineNumber, Gbl_PreprocessorLineNumber);
390 1.1 christos #endif
391 1.1 christos
392 1.1 christos /*
393 1.1 christos * Emit a #line directive if necessary, to keep the line numbers in
394 1.1 christos * the (.i) file synchronized with the original source code file, so
395 1.1 christos * that the correct line number appears in any error messages
396 1.1 christos * generated by the actual compiler.
397 1.1 christos */
398 1.1 christos if (Gbl_CurrentLineNumber > (Gbl_PreviousLineNumber + 1))
399 1.1 christos {
400 1.1 christos FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u\n",
401 1.1 christos Gbl_CurrentLineNumber);
402 1.1 christos }
403 1.1 christos
404 1.1 christos Gbl_PreviousLineNumber = Gbl_CurrentLineNumber;
405 1.1 christos Gbl_PreprocessorLineNumber++;
406 1.1 christos
407 1.1 christos /*
408 1.1 christos * Now we can write the possibly modified source line to the
409 1.1 christos * preprocessor (.i) file
410 1.1 christos */
411 1.1 christos FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer,
412 1.1 christos strlen (Gbl_CurrentLineBuffer));
413 1.1 christos }
414 1.1 christos }
415 1.1 christos
416 1.1 christos
417 1.1 christos /*******************************************************************************
418 1.1 christos *
419 1.1 christos * FUNCTION: PrDoDirective
420 1.1 christos *
421 1.1 christos * PARAMETERS: Directive - Pointer to directive name token
422 1.1 christos * Next - "Next" buffer from GetNextToken
423 1.1 christos *
424 1.1 christos * RETURN: None.
425 1.1 christos *
426 1.1 christos * DESCRIPTION: Main processing for all preprocessor directives
427 1.1 christos *
428 1.1 christos ******************************************************************************/
429 1.1 christos
430 1.1 christos static void
431 1.1 christos PrDoDirective (
432 1.1 christos char *DirectiveToken,
433 1.1 christos char **Next)
434 1.1 christos {
435 1.1 christos char *Token = Gbl_MainTokenBuffer;
436 1.1 christos char *Token2;
437 1.1 christos char *End;
438 1.1 christos UINT64 Value;
439 1.1 christos ACPI_SIZE TokenOffset;
440 1.1 christos int Directive;
441 1.1 christos ACPI_STATUS Status;
442 1.1 christos
443 1.1 christos
444 1.1 christos if (!DirectiveToken)
445 1.1 christos {
446 1.1 christos goto SyntaxError;
447 1.1 christos }
448 1.1 christos
449 1.1 christos Directive = PrMatchDirective (DirectiveToken);
450 1.1 christos if (Directive == ASL_DIRECTIVE_NOT_FOUND)
451 1.1 christos {
452 1.1 christos PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
453 1.1 christos THIS_TOKEN_OFFSET (DirectiveToken));
454 1.1 christos
455 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
456 1.1 christos "#%s: Unknown directive\n",
457 1.1 christos Gbl_CurrentLineNumber, DirectiveToken);
458 1.1 christos return;
459 1.1 christos }
460 1.1 christos
461 1.1 christos /*
462 1.1 christos * If we are currently ignoring this block and we encounter a #else or
463 1.1 christos * #elif, we must ignore their blocks also if the parent block is also
464 1.1 christos * being ignored.
465 1.1 christos */
466 1.1 christos if (Gbl_IgnoringThisCodeBlock)
467 1.1 christos {
468 1.1 christos switch (Directive)
469 1.1 christos {
470 1.1 christos case PR_DIRECTIVE_ELSE:
471 1.1 christos case PR_DIRECTIVE_ELIF:
472 1.1 christos
473 1.1 christos if (Gbl_DirectiveStack && Gbl_DirectiveStack->IgnoringThisCodeBlock)
474 1.1 christos {
475 1.1 christos PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name);
476 1.1 christos return;
477 1.1 christos }
478 1.1 christos break;
479 1.1 christos
480 1.1 christos default:
481 1.1 christos break;
482 1.1 christos }
483 1.1 christos }
484 1.1 christos
485 1.1 christos /*
486 1.1 christos * Need to always check for #else, #elif, #endif regardless of
487 1.1 christos * whether we are ignoring the current code block, since these
488 1.1 christos * are conditional code block terminators.
489 1.1 christos */
490 1.1 christos switch (Directive)
491 1.1 christos {
492 1.1 christos case PR_DIRECTIVE_ELSE:
493 1.1 christos
494 1.1 christos Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock);
495 1.1 christos PrDbgPrint ("Executing", "else block");
496 1.1 christos return;
497 1.1 christos
498 1.1 christos case PR_DIRECTIVE_ELIF:
499 1.1 christos
500 1.1 christos Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock);
501 1.1 christos Directive = PR_DIRECTIVE_IF;
502 1.1 christos
503 1.1 christos if (Gbl_IgnoringThisCodeBlock == TRUE)
504 1.1 christos {
505 1.1 christos /* Not executing the ELSE part -- all done here */
506 1.1 christos PrDbgPrint ("Ignoring", "elif block");
507 1.1 christos return;
508 1.1 christos }
509 1.1 christos
510 1.1 christos /*
511 1.1 christos * After this, we will execute the IF part further below.
512 1.1 christos * First, however, pop off the original #if directive.
513 1.1 christos */
514 1.1 christos if (ACPI_FAILURE (PrPopDirective ()))
515 1.1 christos {
516 1.1 christos PrError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
517 1.1 christos THIS_TOKEN_OFFSET (DirectiveToken));
518 1.1 christos }
519 1.1 christos
520 1.1 christos PrDbgPrint ("Executing", "elif block");
521 1.1 christos break;
522 1.1 christos
523 1.1 christos case PR_DIRECTIVE_ENDIF:
524 1.1 christos
525 1.1 christos PrDbgPrint ("Executing", "endif");
526 1.1 christos
527 1.1 christos /* Pop the owning #if/#ifdef/#ifndef */
528 1.1 christos
529 1.1 christos if (ACPI_FAILURE (PrPopDirective ()))
530 1.1 christos {
531 1.1 christos PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
532 1.1 christos THIS_TOKEN_OFFSET (DirectiveToken));
533 1.1 christos }
534 1.1 christos return;
535 1.1 christos
536 1.1 christos default:
537 1.1 christos break;
538 1.1 christos }
539 1.1 christos
540 1.1 christos /* Most directives have at least one argument */
541 1.1 christos
542 1.1 christos if (Gbl_DirectiveInfo[Directive].ArgCount == 1)
543 1.1 christos {
544 1.1 christos Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
545 1.1 christos if (!Token)
546 1.1 christos {
547 1.1 christos goto SyntaxError;
548 1.1 christos }
549 1.1 christos }
550 1.1 christos
551 1.1 christos /*
552 1.1 christos * At this point, if we are ignoring the current code block,
553 1.1 christos * do not process any more directives (i.e., ignore them also.)
554 1.1 christos * For "if" style directives, open/push a new block anyway. We
555 1.1 christos * must do this to keep track of #endif directives
556 1.1 christos */
557 1.1 christos if (Gbl_IgnoringThisCodeBlock)
558 1.1 christos {
559 1.1 christos switch (Directive)
560 1.1 christos {
561 1.1 christos case PR_DIRECTIVE_IF:
562 1.1 christos case PR_DIRECTIVE_IFDEF:
563 1.1 christos case PR_DIRECTIVE_IFNDEF:
564 1.1 christos
565 1.1 christos PrPushDirective (Directive, Token);
566 1.1 christos PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name);
567 1.1 christos break;
568 1.1 christos
569 1.1 christos default:
570 1.1 christos break;
571 1.1 christos }
572 1.1 christos
573 1.1 christos return;
574 1.1 christos }
575 1.1 christos
576 1.1 christos /*
577 1.1 christos * Execute the directive
578 1.1 christos */
579 1.1 christos PrDbgPrint ("Begin execution", Gbl_DirectiveInfo[Directive].Name);
580 1.1 christos
581 1.1 christos switch (Directive)
582 1.1 christos {
583 1.1 christos case PR_DIRECTIVE_IF:
584 1.1 christos
585 1.1 christos TokenOffset = Token - Gbl_MainTokenBuffer;
586 1.1 christos
587 1.1 christos /* Need to expand #define macros in the expression string first */
588 1.1 christos
589 1.1 christos Status = PrResolveIntegerExpression (
590 1.1 christos &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
591 1.1 christos if (ACPI_FAILURE (Status))
592 1.1 christos {
593 1.1 christos return;
594 1.1 christos }
595 1.1 christos
596 1.1 christos PrPushDirective (Directive, Token);
597 1.1 christos if (!Value)
598 1.1 christos {
599 1.1 christos Gbl_IgnoringThisCodeBlock = TRUE;
600 1.1 christos }
601 1.1 christos
602 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
603 1.1 christos "Resolved #if: %8.8X%8.8X %s\n",
604 1.1 christos Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value),
605 1.1 christos Gbl_IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>");
606 1.1 christos break;
607 1.1 christos
608 1.1 christos case PR_DIRECTIVE_IFDEF:
609 1.1 christos
610 1.1 christos PrPushDirective (Directive, Token);
611 1.1 christos if (!PrMatchDefine (Token))
612 1.1 christos {
613 1.1 christos Gbl_IgnoringThisCodeBlock = TRUE;
614 1.1 christos }
615 1.1 christos
616 1.1 christos PrDbgPrint ("Evaluated", "ifdef");
617 1.1 christos break;
618 1.1 christos
619 1.1 christos case PR_DIRECTIVE_IFNDEF:
620 1.1 christos
621 1.1 christos PrPushDirective (Directive, Token);
622 1.1 christos if (PrMatchDefine (Token))
623 1.1 christos {
624 1.1 christos Gbl_IgnoringThisCodeBlock = TRUE;
625 1.1 christos }
626 1.1 christos
627 1.1 christos PrDbgPrint ("Evaluated", "ifndef");
628 1.1 christos break;
629 1.1 christos
630 1.1 christos case PR_DIRECTIVE_DEFINE:
631 1.1 christos /*
632 1.1 christos * By definition, if first char after the name is a paren,
633 1.1 christos * this is a function macro.
634 1.1 christos */
635 1.1 christos TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token);
636 1.1 christos if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(')
637 1.1 christos {
638 1.1 christos #ifndef MACROS_SUPPORTED
639 1.1 christos AcpiOsPrintf ("%s ERROR - line %u: #define macros are not supported yet\n",
640 1.1 christos Gbl_CurrentLineBuffer, Gbl_CurrentLineNumber);
641 1.1 christos exit(1);
642 1.1 christos #else
643 1.1 christos PrAddMacro (Token, Next);
644 1.1 christos #endif
645 1.1 christos }
646 1.1 christos else
647 1.1 christos {
648 1.1 christos /* Use the remainder of the line for the #define */
649 1.1 christos
650 1.1 christos Token2 = *Next;
651 1.1 christos if (Token2)
652 1.1 christos {
653 1.1 christos while ((*Token2 == ' ') || (*Token2 == '\t'))
654 1.1 christos {
655 1.1 christos Token2++;
656 1.1 christos }
657 1.1 christos End = Token2;
658 1.1 christos while (*End != '\n')
659 1.1 christos {
660 1.1 christos End++;
661 1.1 christos }
662 1.1 christos *End = 0;
663 1.1 christos }
664 1.1 christos else
665 1.1 christos {
666 1.1 christos Token2 = "";
667 1.1 christos }
668 1.1 christos #if 0
669 1.1 christos Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next);
670 1.1 christos if (!Token2)
671 1.1 christos {
672 1.1 christos Token2 = "";
673 1.1 christos }
674 1.1 christos #endif
675 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
676 1.1 christos "New #define: %s->%s\n",
677 1.1 christos Gbl_CurrentLineNumber, Token, Token2);
678 1.1 christos
679 1.1 christos PrAddDefine (Token, Token2, FALSE);
680 1.1 christos }
681 1.1 christos break;
682 1.1 christos
683 1.1 christos case PR_DIRECTIVE_ERROR:
684 1.1 christos
685 1.1 christos /* Note: No macro expansion */
686 1.1 christos
687 1.1 christos PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE,
688 1.1 christos THIS_TOKEN_OFFSET (Token));
689 1.1 christos
690 1.1 christos Gbl_SourceLine = 0;
691 1.1 christos Gbl_NextError = Gbl_ErrorLog;
692 1.1 christos CmCleanupAndExit ();
693 1.1 christos exit(1);
694 1.1 christos
695 1.1 christos case PR_DIRECTIVE_INCLUDE:
696 1.1 christos
697 1.1 christos Token = PrGetNextToken (NULL, " \"<>", Next);
698 1.1 christos if (!Token)
699 1.1 christos {
700 1.1 christos goto SyntaxError;
701 1.1 christos }
702 1.1 christos
703 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
704 1.1 christos "Start #include file \"%s\"\n", Gbl_CurrentLineNumber,
705 1.1 christos Token, Gbl_CurrentLineNumber);
706 1.1 christos
707 1.1 christos PrOpenIncludeFile (Token);
708 1.1 christos break;
709 1.1 christos
710 1.1 christos case PR_DIRECTIVE_LINE:
711 1.1 christos
712 1.1 christos TokenOffset = Token - Gbl_MainTokenBuffer;
713 1.1 christos
714 1.1 christos Status = PrResolveIntegerExpression (
715 1.1 christos &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
716 1.1 christos if (ACPI_FAILURE (Status))
717 1.1 christos {
718 1.1 christos return;
719 1.1 christos }
720 1.1 christos
721 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
722 1.1 christos "User #line invocation %s\n", Gbl_CurrentLineNumber,
723 1.1 christos Token);
724 1.1 christos
725 1.1 christos /* Update local line numbers */
726 1.1 christos
727 1.1 christos Gbl_CurrentLineNumber = (UINT32) Value;
728 1.1 christos Gbl_PreviousLineNumber = 0;
729 1.1 christos
730 1.1 christos /* Emit #line into the preprocessor file */
731 1.1 christos
732 1.1 christos FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
733 1.1 christos Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename);
734 1.1 christos break;
735 1.1 christos
736 1.1 christos case PR_DIRECTIVE_PRAGMA:
737 1.1 christos
738 1.1 christos if (!strcmp (Token, "disable"))
739 1.1 christos {
740 1.1 christos Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
741 1.1 christos if (!Token)
742 1.1 christos {
743 1.1 christos goto SyntaxError;
744 1.1 christos }
745 1.1 christos
746 1.1 christos TokenOffset = Token - Gbl_MainTokenBuffer;
747 1.1 christos AslDisableException (&Gbl_CurrentLineBuffer[TokenOffset]);
748 1.1 christos }
749 1.1 christos else if (!strcmp (Token, "message"))
750 1.1 christos {
751 1.1 christos Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
752 1.1 christos if (!Token)
753 1.1 christos {
754 1.1 christos goto SyntaxError;
755 1.1 christos }
756 1.1 christos
757 1.1 christos TokenOffset = Token - Gbl_MainTokenBuffer;
758 1.1 christos AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]);
759 1.1 christos }
760 1.1 christos else
761 1.1 christos {
762 1.1 christos PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
763 1.1 christos THIS_TOKEN_OFFSET (Token));
764 1.1 christos return;
765 1.1 christos }
766 1.1 christos
767 1.1 christos break;
768 1.1 christos
769 1.1 christos case PR_DIRECTIVE_UNDEF:
770 1.1 christos
771 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
772 1.1 christos "#undef: %s\n", Gbl_CurrentLineNumber, Token);
773 1.1 christos
774 1.1 christos PrRemoveDefine (Token);
775 1.1 christos break;
776 1.1 christos
777 1.1 christos case PR_DIRECTIVE_WARNING:
778 1.1 christos
779 1.1 christos PrError (ASL_WARNING, ASL_MSG_WARNING_DIRECTIVE,
780 1.1 christos THIS_TOKEN_OFFSET (Token));
781 1.1 christos break;
782 1.1 christos
783 1.1 christos default:
784 1.1 christos
785 1.1 christos /* Should never get here */
786 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
787 1.1 christos "Unrecognized directive: %u\n",
788 1.1 christos Gbl_CurrentLineNumber, Directive);
789 1.1 christos break;
790 1.1 christos }
791 1.1 christos
792 1.1 christos return;
793 1.1 christos
794 1.1 christos SyntaxError:
795 1.1 christos
796 1.1 christos PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
797 1.1 christos THIS_TOKEN_OFFSET (DirectiveToken));
798 1.1 christos return;
799 1.1 christos }
800 1.1 christos
801 1.1 christos
802 1.1 christos /*******************************************************************************
803 1.1 christos *
804 1.1 christos * FUNCTION: PrMatchDirective
805 1.1 christos *
806 1.1 christos * PARAMETERS: Directive - Pointer to directive name token
807 1.1 christos *
808 1.1 christos * RETURN: Index into command array, -1 if not found
809 1.1 christos *
810 1.1 christos * DESCRIPTION: Lookup the incoming directive in the known directives table.
811 1.1 christos *
812 1.1 christos ******************************************************************************/
813 1.1 christos
814 1.1 christos static int
815 1.1 christos PrMatchDirective (
816 1.1 christos char *Directive)
817 1.1 christos {
818 1.1 christos int i;
819 1.1 christos
820 1.1 christos
821 1.1 christos if (!Directive || Directive[0] == 0)
822 1.1 christos {
823 1.1 christos return (ASL_DIRECTIVE_NOT_FOUND);
824 1.1 christos }
825 1.1 christos
826 1.1 christos for (i = 0; Gbl_DirectiveInfo[i].Name; i++)
827 1.1 christos {
828 1.1 christos if (!strcmp (Gbl_DirectiveInfo[i].Name, Directive))
829 1.1 christos {
830 1.1 christos return (i);
831 1.1 christos }
832 1.1 christos }
833 1.1 christos
834 1.1 christos return (ASL_DIRECTIVE_NOT_FOUND); /* Command not recognized */
835 1.1 christos }
836 1.1 christos
837 1.1 christos
838 1.1 christos /*******************************************************************************
839 1.1 christos *
840 1.1 christos * FUNCTION: PrPushDirective
841 1.1 christos *
842 1.1 christos * PARAMETERS: Directive - Encoded directive ID
843 1.1 christos * Argument - String containing argument to the
844 1.1 christos * directive
845 1.1 christos *
846 1.1 christos * RETURN: None
847 1.1 christos *
848 1.1 christos * DESCRIPTION: Push an item onto the directive stack. Used for processing
849 1.1 christos * nested #if/#else type conditional compilation directives.
850 1.1 christos * Specifically: Used on detection of #if/#ifdef/#ifndef to open
851 1.1 christos * a block.
852 1.1 christos *
853 1.1 christos ******************************************************************************/
854 1.1 christos
855 1.1 christos static void
856 1.1 christos PrPushDirective (
857 1.1 christos int Directive,
858 1.1 christos char *Argument)
859 1.1 christos {
860 1.1 christos DIRECTIVE_INFO *Info;
861 1.1 christos
862 1.1 christos
863 1.1 christos /* Allocate and populate a stack info item */
864 1.1 christos
865 1.1 christos Info = ACPI_ALLOCATE (sizeof (DIRECTIVE_INFO));
866 1.1 christos
867 1.1 christos Info->Next = Gbl_DirectiveStack;
868 1.1 christos Info->Directive = Directive;
869 1.1 christos Info->IgnoringThisCodeBlock = Gbl_IgnoringThisCodeBlock;
870 1.1 christos strncpy (Info->Argument, Argument, MAX_ARGUMENT_LENGTH);
871 1.1 christos
872 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT,
873 1.1 christos "Pr(%.4u) - [%u %s] %*s Pushed [#%s %s]: IgnoreFlag = %s\n",
874 1.1 christos Gbl_CurrentLineNumber, Gbl_IfDepth,
875 1.1 christos Gbl_IgnoringThisCodeBlock ? "I" : "E",
876 1.1 christos Gbl_IfDepth * 4, " ",
877 1.1 christos Gbl_DirectiveInfo[Directive].Name,
878 1.1 christos Argument, Gbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
879 1.1 christos
880 1.1 christos /* Push new item */
881 1.1 christos
882 1.1 christos Gbl_DirectiveStack = Info;
883 1.1 christos Gbl_IfDepth++;
884 1.1 christos }
885 1.1 christos
886 1.1 christos
887 1.1 christos /*******************************************************************************
888 1.1 christos *
889 1.1 christos * FUNCTION: PrPopDirective
890 1.1 christos *
891 1.1 christos * PARAMETERS: None
892 1.1 christos *
893 1.1 christos * RETURN: Status. Error if the stack is empty.
894 1.1 christos *
895 1.1 christos * DESCRIPTION: Pop an item off the directive stack. Used for processing
896 1.1 christos * nested #if/#else type conditional compilation directives.
897 1.1 christos * Specifically: Used on detection of #elif and #endif to remove
898 1.1 christos * the original #if/#ifdef/#ifndef from the stack and close
899 1.1 christos * the block.
900 1.1 christos *
901 1.1 christos ******************************************************************************/
902 1.1 christos
903 1.1 christos static ACPI_STATUS
904 1.1 christos PrPopDirective (
905 1.1 christos void)
906 1.1 christos {
907 1.1 christos DIRECTIVE_INFO *Info;
908 1.1 christos
909 1.1 christos
910 1.1 christos /* Check for empty stack */
911 1.1 christos
912 1.1 christos Info = Gbl_DirectiveStack;
913 1.1 christos if (!Info)
914 1.1 christos {
915 1.1 christos return (AE_ERROR);
916 1.1 christos }
917 1.1 christos
918 1.1 christos /* Pop one item, keep globals up-to-date */
919 1.1 christos
920 1.1 christos Gbl_IfDepth--;
921 1.1 christos Gbl_IgnoringThisCodeBlock = Info->IgnoringThisCodeBlock;
922 1.1 christos Gbl_DirectiveStack = Info->Next;
923 1.1 christos
924 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT,
925 1.1 christos "Pr(%.4u) - [%u %s] %*s Popped [#%s %s]: IgnoreFlag now = %s\n",
926 1.1 christos Gbl_CurrentLineNumber, Gbl_IfDepth,
927 1.1 christos Gbl_IgnoringThisCodeBlock ? "I" : "E",
928 1.1 christos Gbl_IfDepth * 4, " ",
929 1.1 christos Gbl_DirectiveInfo[Info->Directive].Name,
930 1.1 christos Info->Argument, Gbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
931 1.1 christos
932 1.1 christos ACPI_FREE (Info);
933 1.1 christos return (AE_OK);
934 1.1 christos }
935 1.1 christos
936 1.1 christos
937 1.1 christos /*******************************************************************************
938 1.1 christos *
939 1.1 christos * FUNCTION: PrDbgPrint
940 1.1 christos *
941 1.1 christos * PARAMETERS: Action - Action being performed
942 1.1 christos * DirectiveName - Directive being processed
943 1.1 christos *
944 1.1 christos * RETURN: None
945 1.1 christos *
946 1.1 christos * DESCRIPTION: Special debug print for directive processing.
947 1.1 christos *
948 1.1 christos ******************************************************************************/
949 1.1 christos
950 1.1 christos static void
951 1.1 christos PrDbgPrint (
952 1.1 christos char *Action,
953 1.1 christos char *DirectiveName)
954 1.1 christos {
955 1.1 christos
956 1.1 christos DbgPrint (ASL_DEBUG_OUTPUT, "Pr(%.4u) - [%u %s] "
957 1.1 christos "%*s %s #%s, Depth %u\n",
958 1.1 christos Gbl_CurrentLineNumber, Gbl_IfDepth,
959 1.1 christos Gbl_IgnoringThisCodeBlock ? "I" : "E",
960 1.1 christos Gbl_IfDepth * 4, " ",
961 1.1 christos Action, DirectiveName, Gbl_IfDepth);
962 1.1 christos }
963