1 /******************************************************************************
3 * Module Name: prutils - Preprocessor utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2012, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
48 #define _COMPONENT ASL_PREPROCESSOR
49 ACPI_MODULE_NAME ("prutils")
52 /*******************************************************************************
54 * FUNCTION: PrSetLineNumber
56 * PARAMETERS: OriginalLineNumber - Line number in original source file,
58 * PreprocessorLineNumber - Line number in the preprocessed file
62 * DESCRIPTION: Insert this mapping into the mapping data structure, for use
63 * in possible error/warning messages.
65 * Line number mapping functions.
66 * For error messages, we need to keep track of the line number in the
67 * original file, versus the preprocessed (.i) file.
69 ******************************************************************************/
73 UINT32 OriginalLineNumber,
74 UINT32 PreprocessorLineNumber)
77 PR_LINE_MAPPING *Block;
82 Entry = PreprocessorLineNumber / PR_LINES_PER_BLOCK;
83 Index = PreprocessorLineNumber % PR_LINES_PER_BLOCK;
84 Block = Gbl_MapBlockHead;
86 for (i = 0; i < Entry; i++)
88 /* Allocate new mapping blocks as necessary */
92 Block->Next = UtLocalCalloc (sizeof (PR_LINE_MAPPING));
93 Block->Next->Map = UtLocalCalloc (PR_LINES_PER_BLOCK * sizeof (UINT32));
99 Block->Map[Index] = OriginalLineNumber;
103 /*******************************************************************************
105 * FUNCTION: PrGetLineNumber
107 * PARAMETERS: PreprocessorLineNumber - Line number in the preprocessed file
108 * (or, the "logical line number)
110 * RETURN: The line number in the original source file or include file.
112 * DESCRIPTION: Return the mapped value of a line number in the preprocessed
113 * source file to the actual line number in the original source
116 ******************************************************************************/
120 UINT32 PreprocessorLineNumber)
123 PR_LINE_MAPPING *Block;
128 Entry = PreprocessorLineNumber / PR_LINES_PER_BLOCK;
129 Index = PreprocessorLineNumber % PR_LINES_PER_BLOCK;
130 Block = Gbl_MapBlockHead;
132 for (i = 0; i < Entry; i++)
137 /* Bad error, should not happen */
142 return (Block->Map[Index]);
146 /******************************************************************************
148 * FUNCTION: PrGetNextToken
150 * PARAMETERS: Buffer - Current line buffer
151 * MatchString - String with valid token delimiters
152 * Next - Set to next possible token in buffer
154 * RETURN: Next token (null-terminated). Modifies the input line.
155 * Remainder of line is stored in *Next.
157 * DESCRIPTION: Local implementation of strtok() with local storage for the
158 * next pointer. Not only thread-safe, but allows multiple
159 * parsing of substrings such as expressions.
161 *****************************************************************************/
174 /* Use Next if it is valid */
183 /* Skip any leading delimiters */
187 if (strchr (MatchString, *Buffer))
197 /* Anything left on the line? */
207 /* Find the end of this token */
211 if (strchr (MatchString, *Buffer))
229 /*******************************************************************************
233 * PARAMETERS: Level - Seriousness (Warning/error, etc.)
234 * MessageId - Index into global message buffer
235 * Column - Column in current line
239 * DESCRIPTION: Preprocessor error reporting. Front end to AslCommonError2
241 ******************************************************************************/
250 AcpiOsPrintf ("%s (%u) : %s", Gbl_Files[ASL_FILE_INPUT].Filename,
251 Gbl_CurrentLineNumber, Gbl_CurrentLineBuffer);
260 /* TBD: Need Logical line number? */
262 AslCommonError2 (Level, MessageId,
263 Gbl_CurrentLineNumber, Column,
264 Gbl_CurrentLineBuffer,
265 Gbl_Files[ASL_FILE_INPUT].Filename, "Preprocessor");
267 Gbl_PreprocessorError = TRUE;
271 /*******************************************************************************
273 * FUNCTION: PrReplaceData
275 * PARAMETERS: Buffer - Original(target) buffer pointer
276 * LengthToRemove - Length to be removed from target buffer
277 * BufferToAdd - Data to be inserted into target buffer
278 * LengthToAdd - Length of BufferToAdd
282 * DESCRIPTION: Generic buffer data replacement.
284 ******************************************************************************/
289 UINT32 LengthToRemove,
296 /* Buffer is a string, so the length must include the terminating zero */
298 BufferLength = strlen (Buffer) + 1;
300 if (LengthToRemove != LengthToAdd)
303 * Move some of the existing data
304 * 1) If adding more bytes than removing, make room for the new data
305 * 2) if removing more bytes than adding, delete the extra space
307 if (LengthToRemove > 0)
309 memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
310 (BufferLength - LengthToRemove));
314 /* Now we can move in the new data */
318 memmove (Buffer, BufferToAdd, LengthToAdd);
323 /*******************************************************************************
325 * FUNCTION: PrOpenIncludeFile
327 * PARAMETERS: Filename - Filename or pathname for include file
331 * DESCRIPTION: Open an include file and push it on the input file stack.
333 ******************************************************************************/
340 ASL_INCLUDE_DIR *NextDir;
344 * start the actual include file on the next line
346 Gbl_CurrentLineOffset++;
348 /* Attempt to open the include file */
350 /* If the file specifies an absolute path, just open it */
352 if ((Filename[0] == '/') ||
353 (Filename[0] == '\\') ||
354 (Filename[1] == ':'))
356 IncludeFile = PrOpenIncludeWithPrefix ("", Filename);
365 * The include filename is not an absolute path.
367 * First, search for the file within the "local" directory -- meaning
368 * the same directory that contains the source file.
370 * Construct the file pathname from the global directory name.
372 IncludeFile = PrOpenIncludeWithPrefix (Gbl_DirectoryPath, Filename);
379 * Second, search for the file within the (possibly multiple)
380 * directories specified by the -I option on the command line.
382 NextDir = Gbl_IncludeDirList;
385 IncludeFile = PrOpenIncludeWithPrefix (NextDir->Dir, Filename);
391 NextDir = NextDir->Next;
394 /* We could not open the include file after trying very hard */
397 sprintf (Gbl_MainTokenBuffer, "%s, %s", Filename, strerror (errno));
398 PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
402 /*******************************************************************************
404 * FUNCTION: FlOpenIncludeWithPrefix
406 * PARAMETERS: PrefixDir - Prefix directory pathname. Can be a zero
408 * Filename - The include filename from the source ASL.
410 * RETURN: Valid file descriptor if successful. Null otherwise.
412 * DESCRIPTION: Open an include file and push it on the input file stack.
414 ******************************************************************************/
417 PrOpenIncludeWithPrefix (
425 /* Build the full pathname to the file */
427 Pathname = ACPI_ALLOCATE (strlen (PrefixDir) + strlen (Filename) + 1);
429 strcpy (Pathname, PrefixDir);
430 strcat (Pathname, Filename);
432 DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
433 "Opening include file: path %s\n",
434 Gbl_CurrentLineNumber, Pathname);
436 /* Attempt to open the file, push if successful */
438 IncludeFile = fopen (Pathname, "r");
441 /* Push the include file on the open input file stack */
443 PrPushInputFileStack (IncludeFile, Pathname);
444 return (IncludeFile);
447 ACPI_FREE (Pathname);
452 /*******************************************************************************
454 * FUNCTION: AslPushInputFileStack
456 * PARAMETERS: InputFile - Open file pointer
457 * Filename - Name of the file
461 * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
462 * to this file. Called when an include file is successfully
465 ******************************************************************************/
468 PrPushInputFileStack (
475 /* Save the current state in an Fnode */
477 Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
479 Fnode->File = Gbl_Files[ASL_FILE_INPUT].Handle;
480 Fnode->Next = Gbl_InputFileList;
481 Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
482 Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
484 /* Push it on the stack */
486 Gbl_InputFileList = Fnode;
488 DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
489 "Push InputFile Stack, returning %p\n\n",
490 Gbl_CurrentLineNumber, InputFile);
492 /* Reset the global line count and filename */
494 Gbl_Files[ASL_FILE_INPUT].Filename = Filename;
495 Gbl_Files[ASL_FILE_INPUT].Handle = InputFile;
496 Gbl_CurrentLineNumber = 1;
500 /*******************************************************************************
502 * FUNCTION: AslPopInputFileStack
506 * RETURN: 0 if a node was popped, -1 otherwise
508 * DESCRIPTION: Pop the top of the input file stack and point the parser to
509 * the saved parse buffer contained in the fnode. Also, set the
510 * global line counters to the saved values. This function is
511 * called when an include file reaches EOF.
513 ******************************************************************************/
516 PrPopInputFileStack (
522 Fnode = Gbl_InputFileList;
523 DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
524 "Pop InputFile Stack, Fnode %p\n\n",
525 Gbl_CurrentLineNumber, Fnode);
532 /* Close the current include file */
534 fclose (Gbl_Files[ASL_FILE_INPUT].Handle);
536 /* Update the top-of-stack */
538 Gbl_InputFileList = Fnode->Next;
540 /* Reset global line counter and filename */
542 Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
543 Gbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
544 Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
546 /* All done with this node */