Home | History | Annotate | Line # | Download | only in compiler
prutils.c revision 1.7.4.1
      1      1.1  christos /******************************************************************************
      2      1.1  christos  *
      3      1.1  christos  * Module Name: prutils - Preprocessor utilities
      4      1.1  christos  *
      5      1.1  christos  *****************************************************************************/
      6      1.1  christos 
      7      1.1  christos /*
      8  1.7.4.1    bouyer  * Copyright (C) 2000 - 2017, 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 #include "aslcompiler.h"
     45      1.1  christos #include "dtcompiler.h"
     46      1.1  christos 
     47      1.1  christos 
     48      1.1  christos #define _COMPONENT          ASL_PREPROCESSOR
     49      1.1  christos         ACPI_MODULE_NAME    ("prutils")
     50      1.1  christos 
     51      1.1  christos 
     52      1.1  christos /******************************************************************************
     53      1.1  christos  *
     54      1.1  christos  * FUNCTION:    PrGetNextToken
     55      1.1  christos  *
     56      1.1  christos  * PARAMETERS:  Buffer              - Current line buffer
     57      1.1  christos  *              MatchString         - String with valid token delimiters
     58      1.1  christos  *              Next                - Set to next possible token in buffer
     59      1.1  christos  *
     60      1.1  christos  * RETURN:      Next token (null-terminated). Modifies the input line.
     61      1.1  christos  *              Remainder of line is stored in *Next.
     62      1.1  christos  *
     63      1.1  christos  * DESCRIPTION: Local implementation of strtok() with local storage for the
     64      1.1  christos  *              next pointer. Not only thread-safe, but allows multiple
     65      1.1  christos  *              parsing of substrings such as expressions.
     66      1.1  christos  *
     67      1.1  christos  *****************************************************************************/
     68      1.1  christos 
     69      1.1  christos char *
     70      1.1  christos PrGetNextToken (
     71      1.1  christos     char                    *Buffer,
     72      1.1  christos     char                    *MatchString,
     73      1.1  christos     char                    **Next)
     74      1.1  christos {
     75      1.1  christos     char                    *TokenStart;
     76      1.1  christos 
     77      1.1  christos 
     78      1.1  christos     if (!Buffer)
     79      1.1  christos     {
     80      1.1  christos         /* Use Next if it is valid */
     81      1.1  christos 
     82      1.1  christos         Buffer = *Next;
     83      1.1  christos         if (!(*Next))
     84      1.1  christos         {
     85      1.1  christos             return (NULL);
     86      1.1  christos         }
     87      1.1  christos     }
     88      1.1  christos 
     89      1.1  christos     /* Skip any leading delimiters */
     90      1.1  christos 
     91      1.1  christos     while (*Buffer)
     92      1.1  christos     {
     93      1.1  christos         if (strchr (MatchString, *Buffer))
     94      1.1  christos         {
     95      1.1  christos             Buffer++;
     96      1.1  christos         }
     97      1.1  christos         else
     98      1.1  christos         {
     99      1.1  christos             break;
    100      1.1  christos         }
    101      1.1  christos     }
    102      1.1  christos 
    103      1.1  christos     /* Anything left on the line? */
    104      1.1  christos 
    105      1.1  christos     if (!(*Buffer))
    106      1.1  christos     {
    107      1.1  christos         *Next = NULL;
    108      1.1  christos         return (NULL);
    109      1.1  christos     }
    110      1.1  christos 
    111      1.1  christos     TokenStart = Buffer;
    112      1.1  christos 
    113      1.1  christos     /* Find the end of this token */
    114      1.1  christos 
    115      1.1  christos     while (*Buffer)
    116      1.1  christos     {
    117      1.1  christos         if (strchr (MatchString, *Buffer))
    118      1.1  christos         {
    119      1.1  christos             *Buffer = 0;
    120      1.1  christos             *Next = Buffer+1;
    121      1.1  christos             if (!**Next)
    122      1.1  christos             {
    123      1.1  christos                 *Next = NULL;
    124      1.1  christos             }
    125      1.7  christos 
    126      1.1  christos             return (TokenStart);
    127      1.1  christos         }
    128      1.7  christos 
    129      1.1  christos         Buffer++;
    130      1.1  christos     }
    131      1.1  christos 
    132      1.1  christos     *Next = NULL;
    133      1.1  christos     return (TokenStart);
    134      1.1  christos }
    135      1.1  christos 
    136      1.1  christos 
    137      1.1  christos /*******************************************************************************
    138      1.1  christos  *
    139      1.1  christos  * FUNCTION:    PrError
    140      1.1  christos  *
    141      1.1  christos  * PARAMETERS:  Level               - Seriousness (Warning/error, etc.)
    142      1.1  christos  *              MessageId           - Index into global message buffer
    143      1.1  christos  *              Column              - Column in current line
    144      1.1  christos  *
    145      1.1  christos  * RETURN:      None
    146      1.1  christos  *
    147      1.1  christos  * DESCRIPTION: Preprocessor error reporting. Front end to AslCommonError2
    148      1.1  christos  *
    149      1.1  christos  ******************************************************************************/
    150      1.1  christos 
    151      1.1  christos void
    152      1.1  christos PrError (
    153      1.1  christos     UINT8                   Level,
    154      1.4  christos     UINT16                  MessageId,
    155      1.1  christos     UINT32                  Column)
    156      1.1  christos {
    157      1.1  christos #if 0
    158      1.1  christos     AcpiOsPrintf ("%s (%u) : %s", Gbl_Files[ASL_FILE_INPUT].Filename,
    159      1.1  christos         Gbl_CurrentLineNumber, Gbl_CurrentLineBuffer);
    160      1.1  christos #endif
    161      1.1  christos 
    162      1.1  christos 
    163      1.1  christos     if (Column > 120)
    164      1.1  christos     {
    165      1.1  christos         Column = 0;
    166      1.1  christos     }
    167      1.1  christos 
    168      1.1  christos     /* TBD: Need Logical line number? */
    169      1.1  christos 
    170      1.1  christos     AslCommonError2 (Level, MessageId,
    171      1.1  christos         Gbl_CurrentLineNumber, Column,
    172      1.1  christos         Gbl_CurrentLineBuffer,
    173      1.1  christos         Gbl_Files[ASL_FILE_INPUT].Filename, "Preprocessor");
    174      1.1  christos 
    175      1.1  christos     Gbl_PreprocessorError = TRUE;
    176      1.1  christos }
    177      1.1  christos 
    178      1.1  christos 
    179      1.1  christos /*******************************************************************************
    180      1.1  christos  *
    181      1.1  christos  * FUNCTION:    PrReplaceData
    182      1.1  christos  *
    183      1.1  christos  * PARAMETERS:  Buffer              - Original(target) buffer pointer
    184      1.1  christos  *              LengthToRemove      - Length to be removed from target buffer
    185      1.1  christos  *              BufferToAdd         - Data to be inserted into target buffer
    186      1.1  christos  *              LengthToAdd         - Length of BufferToAdd
    187      1.1  christos  *
    188      1.1  christos  * RETURN:      None
    189      1.1  christos  *
    190      1.1  christos  * DESCRIPTION: Generic buffer data replacement.
    191      1.1  christos  *
    192      1.1  christos  ******************************************************************************/
    193      1.1  christos 
    194      1.1  christos void
    195      1.1  christos PrReplaceData (
    196      1.1  christos     char                    *Buffer,
    197      1.1  christos     UINT32                  LengthToRemove,
    198      1.1  christos     char                    *BufferToAdd,
    199      1.1  christos     UINT32                  LengthToAdd)
    200      1.1  christos {
    201      1.1  christos     UINT32                  BufferLength;
    202      1.1  christos 
    203      1.1  christos 
    204      1.1  christos     /* Buffer is a string, so the length must include the terminating zero */
    205      1.1  christos 
    206      1.1  christos     BufferLength = strlen (Buffer) + 1;
    207      1.1  christos 
    208      1.1  christos     if (LengthToRemove != LengthToAdd)
    209      1.1  christos     {
    210      1.1  christos         /*
    211      1.1  christos          * Move some of the existing data
    212      1.1  christos          * 1) If adding more bytes than removing, make room for the new data
    213      1.1  christos          * 2) if removing more bytes than adding, delete the extra space
    214      1.1  christos          */
    215      1.1  christos         if (LengthToRemove > 0)
    216      1.1  christos         {
    217      1.1  christos             memmove ((Buffer + LengthToAdd), (Buffer + LengthToRemove),
    218      1.1  christos                 (BufferLength - LengthToRemove));
    219      1.1  christos         }
    220      1.1  christos     }
    221      1.1  christos 
    222      1.1  christos     /* Now we can move in the new data */
    223      1.1  christos 
    224      1.1  christos     if (LengthToAdd > 0)
    225      1.1  christos     {
    226      1.1  christos         memmove (Buffer, BufferToAdd, LengthToAdd);
    227      1.1  christos     }
    228      1.1  christos }
    229      1.1  christos 
    230      1.1  christos 
    231      1.1  christos /*******************************************************************************
    232      1.1  christos  *
    233      1.1  christos  * FUNCTION:    PrOpenIncludeFile
    234      1.1  christos  *
    235      1.1  christos  * PARAMETERS:  Filename            - Filename or pathname for include file
    236      1.1  christos  *
    237      1.1  christos  * RETURN:      None.
    238      1.1  christos  *
    239      1.1  christos  * DESCRIPTION: Open an include file and push it on the input file stack.
    240      1.1  christos  *
    241      1.1  christos  ******************************************************************************/
    242      1.1  christos 
    243      1.6  christos FILE *
    244      1.1  christos PrOpenIncludeFile (
    245      1.6  christos     char                    *Filename,
    246      1.6  christos     char                    *OpenMode,
    247      1.6  christos     char                    **FullPathname)
    248      1.1  christos {
    249      1.1  christos     FILE                    *IncludeFile;
    250      1.1  christos     ASL_INCLUDE_DIR         *NextDir;
    251      1.1  christos 
    252      1.1  christos 
    253      1.1  christos     /* Start the actual include file on the next line */
    254      1.1  christos 
    255      1.1  christos     Gbl_CurrentLineOffset++;
    256      1.1  christos 
    257      1.1  christos     /* Attempt to open the include file */
    258      1.1  christos     /* If the file specifies an absolute path, just open it */
    259      1.1  christos 
    260      1.1  christos     if ((Filename[0] == '/')  ||
    261      1.1  christos         (Filename[0] == '\\') ||
    262      1.1  christos         (Filename[1] == ':'))
    263      1.1  christos     {
    264      1.6  christos         IncludeFile = PrOpenIncludeWithPrefix (
    265      1.6  christos             "", Filename, OpenMode, FullPathname);
    266      1.1  christos         if (!IncludeFile)
    267      1.1  christos         {
    268      1.1  christos             goto ErrorExit;
    269      1.1  christos         }
    270      1.6  christos         return (IncludeFile);
    271      1.1  christos     }
    272      1.1  christos 
    273      1.1  christos     /*
    274      1.1  christos      * The include filename is not an absolute path.
    275      1.1  christos      *
    276      1.1  christos      * First, search for the file within the "local" directory -- meaning
    277      1.1  christos      * the same directory that contains the source file.
    278      1.1  christos      *
    279      1.1  christos      * Construct the file pathname from the global directory name.
    280      1.1  christos      */
    281      1.6  christos     IncludeFile = PrOpenIncludeWithPrefix (
    282      1.6  christos         Gbl_DirectoryPath, Filename, OpenMode, FullPathname);
    283      1.1  christos     if (IncludeFile)
    284      1.1  christos     {
    285      1.6  christos         return (IncludeFile);
    286      1.1  christos     }
    287      1.1  christos 
    288      1.1  christos     /*
    289      1.1  christos      * Second, search for the file within the (possibly multiple)
    290      1.1  christos      * directories specified by the -I option on the command line.
    291      1.1  christos      */
    292      1.1  christos     NextDir = Gbl_IncludeDirList;
    293      1.1  christos     while (NextDir)
    294      1.1  christos     {
    295      1.6  christos         IncludeFile = PrOpenIncludeWithPrefix (
    296      1.6  christos             NextDir->Dir, Filename, OpenMode, FullPathname);
    297      1.1  christos         if (IncludeFile)
    298      1.1  christos         {
    299      1.6  christos             return (IncludeFile);
    300      1.1  christos         }
    301      1.1  christos 
    302      1.1  christos         NextDir = NextDir->Next;
    303      1.1  christos     }
    304      1.1  christos 
    305      1.1  christos     /* We could not open the include file after trying very hard */
    306      1.1  christos 
    307      1.1  christos ErrorExit:
    308      1.3      tron     snprintf (Gbl_MainTokenBuffer, ASL_DEFAULT_LINE_BUFFER_SIZE, "%s, %s",
    309      1.2  christos 	Filename, strerror (errno));
    310      1.1  christos     PrError (ASL_ERROR, ASL_MSG_INCLUDE_FILE_OPEN, 0);
    311      1.6  christos     return (NULL);
    312      1.1  christos }
    313      1.1  christos 
    314      1.1  christos 
    315      1.1  christos /*******************************************************************************
    316      1.1  christos  *
    317      1.1  christos  * FUNCTION:    FlOpenIncludeWithPrefix
    318      1.1  christos  *
    319      1.1  christos  * PARAMETERS:  PrefixDir       - Prefix directory pathname. Can be a zero
    320      1.1  christos  *                                length string.
    321      1.1  christos  *              Filename        - The include filename from the source ASL.
    322      1.1  christos  *
    323      1.1  christos  * RETURN:      Valid file descriptor if successful. Null otherwise.
    324      1.1  christos  *
    325      1.1  christos  * DESCRIPTION: Open an include file and push it on the input file stack.
    326      1.1  christos  *
    327      1.1  christos  ******************************************************************************/
    328      1.1  christos 
    329      1.1  christos FILE *
    330      1.1  christos PrOpenIncludeWithPrefix (
    331      1.1  christos     char                    *PrefixDir,
    332      1.6  christos     char                    *Filename,
    333      1.6  christos     char                    *OpenMode,
    334      1.6  christos     char                    **FullPathname)
    335      1.1  christos {
    336      1.1  christos     FILE                    *IncludeFile;
    337      1.1  christos     char                    *Pathname;
    338      1.1  christos 
    339      1.1  christos 
    340      1.1  christos     /* Build the full pathname to the file */
    341      1.1  christos 
    342      1.1  christos     Pathname = FlMergePathnames (PrefixDir, Filename);
    343      1.1  christos 
    344      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    345      1.1  christos         "Include: Opening file - \"%s\"\n",
    346      1.1  christos         Gbl_CurrentLineNumber, Pathname);
    347      1.1  christos 
    348      1.1  christos     /* Attempt to open the file, push if successful */
    349      1.1  christos 
    350      1.6  christos     IncludeFile = fopen (Pathname, OpenMode);
    351      1.1  christos     if (!IncludeFile)
    352      1.1  christos     {
    353      1.1  christos         fprintf (stderr, "Could not open include file %s\n", Pathname);
    354      1.1  christos         return (NULL);
    355      1.1  christos     }
    356      1.1  christos 
    357      1.1  christos     /* Push the include file on the open input file stack */
    358      1.1  christos 
    359      1.1  christos     PrPushInputFileStack (IncludeFile, Pathname);
    360      1.6  christos     *FullPathname = Pathname;
    361      1.1  christos     return (IncludeFile);
    362      1.1  christos }
    363      1.1  christos 
    364      1.1  christos 
    365      1.1  christos /*******************************************************************************
    366      1.1  christos  *
    367      1.1  christos  * FUNCTION:    AslPushInputFileStack
    368      1.1  christos  *
    369      1.1  christos  * PARAMETERS:  InputFile           - Open file pointer
    370      1.1  christos  *              Filename            - Name of the file
    371      1.1  christos  *
    372      1.1  christos  * RETURN:      None
    373      1.1  christos  *
    374      1.1  christos  * DESCRIPTION: Push the InputFile onto the file stack, and point the parser
    375      1.1  christos  *              to this file. Called when an include file is successfully
    376      1.1  christos  *              opened.
    377      1.1  christos  *
    378      1.1  christos  ******************************************************************************/
    379      1.1  christos 
    380      1.1  christos void
    381      1.1  christos PrPushInputFileStack (
    382      1.1  christos     FILE                    *InputFile,
    383      1.1  christos     char                    *Filename)
    384      1.1  christos {
    385      1.1  christos     PR_FILE_NODE            *Fnode;
    386      1.1  christos 
    387      1.1  christos 
    388      1.6  christos     Gbl_HasIncludeFiles = TRUE;
    389      1.6  christos 
    390      1.1  christos     /* Save the current state in an Fnode */
    391      1.1  christos 
    392      1.1  christos     Fnode = UtLocalCalloc (sizeof (PR_FILE_NODE));
    393      1.1  christos 
    394      1.1  christos     Fnode->File = Gbl_Files[ASL_FILE_INPUT].Handle;
    395      1.1  christos     Fnode->Next = Gbl_InputFileList;
    396      1.1  christos     Fnode->Filename = Gbl_Files[ASL_FILE_INPUT].Filename;
    397      1.1  christos     Fnode->CurrentLineNumber = Gbl_CurrentLineNumber;
    398      1.1  christos 
    399      1.1  christos     /* Push it on the stack */
    400      1.1  christos 
    401      1.1  christos     Gbl_InputFileList = Fnode;
    402      1.1  christos 
    403      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    404      1.1  christos         "Push InputFile Stack: handle %p\n\n",
    405      1.1  christos         Gbl_CurrentLineNumber, InputFile);
    406      1.1  christos 
    407      1.1  christos     /* Reset the global line count and filename */
    408      1.1  christos 
    409      1.4  christos     Gbl_Files[ASL_FILE_INPUT].Filename =
    410      1.4  christos         UtStringCacheCalloc (strlen (Filename) + 1);
    411      1.4  christos     strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
    412      1.4  christos 
    413      1.1  christos     Gbl_Files[ASL_FILE_INPUT].Handle = InputFile;
    414      1.6  christos     Gbl_CurrentLineNumber = 1;
    415      1.1  christos 
    416      1.1  christos     /* Emit a new #line directive for the include file */
    417      1.1  christos 
    418      1.4  christos     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n", 1, Filename);
    419      1.1  christos }
    420      1.1  christos 
    421      1.1  christos 
    422      1.1  christos /*******************************************************************************
    423      1.1  christos  *
    424      1.1  christos  * FUNCTION:    AslPopInputFileStack
    425      1.1  christos  *
    426      1.1  christos  * PARAMETERS:  None
    427      1.1  christos  *
    428      1.1  christos  * RETURN:      0 if a node was popped, -1 otherwise
    429      1.1  christos  *
    430      1.1  christos  * DESCRIPTION: Pop the top of the input file stack and point the parser to
    431      1.1  christos  *              the saved parse buffer contained in the fnode. Also, set the
    432      1.1  christos  *              global line counters to the saved values. This function is
    433      1.1  christos  *              called when an include file reaches EOF.
    434      1.1  christos  *
    435      1.1  christos  ******************************************************************************/
    436      1.1  christos 
    437      1.1  christos BOOLEAN
    438      1.1  christos PrPopInputFileStack (
    439      1.1  christos     void)
    440      1.1  christos {
    441      1.1  christos     PR_FILE_NODE            *Fnode;
    442      1.1  christos 
    443      1.1  christos 
    444      1.1  christos     Fnode = Gbl_InputFileList;
    445      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT, "\n" PR_PREFIX_ID
    446      1.1  christos         "Pop InputFile Stack, Fnode %p\n\n",
    447      1.1  christos         Gbl_CurrentLineNumber, Fnode);
    448      1.1  christos 
    449      1.1  christos     if (!Fnode)
    450      1.1  christos     {
    451      1.1  christos         return (FALSE);
    452      1.1  christos     }
    453      1.1  christos 
    454      1.1  christos     /* Close the current include file */
    455      1.1  christos 
    456      1.1  christos     fclose (Gbl_Files[ASL_FILE_INPUT].Handle);
    457      1.1  christos 
    458      1.1  christos     /* Update the top-of-stack */
    459      1.1  christos 
    460      1.1  christos     Gbl_InputFileList = Fnode->Next;
    461      1.1  christos 
    462      1.1  christos     /* Reset global line counter and filename */
    463      1.1  christos 
    464      1.1  christos     Gbl_Files[ASL_FILE_INPUT].Filename = Fnode->Filename;
    465      1.1  christos     Gbl_Files[ASL_FILE_INPUT].Handle = Fnode->File;
    466      1.1  christos     Gbl_CurrentLineNumber = Fnode->CurrentLineNumber;
    467      1.1  christos 
    468      1.1  christos     /* Emit a new #line directive after the include file */
    469      1.1  christos 
    470      1.1  christos     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
    471      1.6  christos         Gbl_CurrentLineNumber, Fnode->Filename);
    472      1.1  christos 
    473      1.1  christos     /* All done with this node */
    474      1.1  christos 
    475      1.1  christos     ACPI_FREE (Fnode);
    476      1.1  christos     return (TRUE);
    477      1.1  christos }
    478