Home | History | Annotate | Line # | Download | only in compiler
prscan.c revision 1.1.1.2.2.2
      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.1.2.2.1     skrll  * Copyright (C) 2000 - 2015, 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.1.2.2.2     skrll static void
     71  1.1.1.2.2.2     skrll PrGetNextLineInit (
     72  1.1.1.2.2.2     skrll     void);
     73  1.1.1.2.2.2     skrll 
     74  1.1.1.2.2.2     skrll static UINT32
     75  1.1.1.2.2.2     skrll PrGetNextLine (
     76  1.1.1.2.2.2     skrll     FILE                    *Handle);
     77  1.1.1.2.2.2     skrll 
     78          1.1  christos static int
     79          1.1  christos PrMatchDirective (
     80          1.1  christos     char                    *Directive);
     81          1.1  christos 
     82          1.1  christos static void
     83          1.1  christos PrPushDirective (
     84          1.1  christos     int                     Directive,
     85          1.1  christos     char                    *Argument);
     86          1.1  christos 
     87          1.1  christos static ACPI_STATUS
     88          1.1  christos PrPopDirective (
     89          1.1  christos     void);
     90          1.1  christos 
     91          1.1  christos static void
     92          1.1  christos PrDbgPrint (
     93          1.1  christos     char                    *Action,
     94          1.1  christos     char                    *DirectiveName);
     95          1.1  christos 
     96  1.1.1.2.2.2     skrll static void
     97  1.1.1.2.2.2     skrll PrDoIncludeBuffer (
     98  1.1.1.2.2.2     skrll     char                    *Pathname,
     99  1.1.1.2.2.2     skrll     char                    *BufferName);
    100  1.1.1.2.2.2     skrll 
    101  1.1.1.2.2.2     skrll static void
    102  1.1.1.2.2.2     skrll PrDoIncludeFile (
    103  1.1.1.2.2.2     skrll     char                    *Pathname);
    104  1.1.1.2.2.2     skrll 
    105          1.1  christos 
    106          1.1  christos /*
    107          1.1  christos  * Supported preprocessor directives
    108  1.1.1.2.2.2     skrll  * Each entry is of the form "Name, ArgumentCount"
    109          1.1  christos  */
    110          1.1  christos static const PR_DIRECTIVE_INFO      Gbl_DirectiveInfo[] =
    111          1.1  christos {
    112  1.1.1.2.2.2     skrll     {"define",          1},
    113  1.1.1.2.2.2     skrll     {"elif",            0}, /* Converted to #else..#if internally */
    114  1.1.1.2.2.2     skrll     {"else",            0},
    115  1.1.1.2.2.2     skrll     {"endif",           0},
    116  1.1.1.2.2.2     skrll     {"error",           1},
    117  1.1.1.2.2.2     skrll     {"if",              1},
    118  1.1.1.2.2.2     skrll     {"ifdef",           1},
    119  1.1.1.2.2.2     skrll     {"ifndef",          1},
    120  1.1.1.2.2.2     skrll     {"include",         0}, /* Argument is not standard format, so just use 0 here */
    121  1.1.1.2.2.2     skrll     {"includebuffer",   0}, /* Argument is not standard format, so just use 0 here */
    122  1.1.1.2.2.2     skrll     {"line",            1},
    123  1.1.1.2.2.2     skrll     {"pragma",          1},
    124  1.1.1.2.2.2     skrll     {"undef",           1},
    125  1.1.1.2.2.2     skrll     {"warning",         1},
    126  1.1.1.2.2.2     skrll     {NULL,              0}
    127          1.1  christos };
    128          1.1  christos 
    129  1.1.1.2.2.2     skrll /* This table must match ordering of above table exactly */
    130  1.1.1.2.2.2     skrll 
    131          1.1  christos enum Gbl_DirectiveIndexes
    132          1.1  christos {
    133          1.1  christos     PR_DIRECTIVE_DEFINE = 0,
    134          1.1  christos     PR_DIRECTIVE_ELIF,
    135          1.1  christos     PR_DIRECTIVE_ELSE,
    136          1.1  christos     PR_DIRECTIVE_ENDIF,
    137          1.1  christos     PR_DIRECTIVE_ERROR,
    138          1.1  christos     PR_DIRECTIVE_IF,
    139          1.1  christos     PR_DIRECTIVE_IFDEF,
    140          1.1  christos     PR_DIRECTIVE_IFNDEF,
    141          1.1  christos     PR_DIRECTIVE_INCLUDE,
    142  1.1.1.2.2.2     skrll     PR_DIRECTIVE_INCLUDEBUFFER,
    143          1.1  christos     PR_DIRECTIVE_LINE,
    144          1.1  christos     PR_DIRECTIVE_PRAGMA,
    145          1.1  christos     PR_DIRECTIVE_UNDEF,
    146  1.1.1.2.2.2     skrll     PR_DIRECTIVE_WARNING
    147          1.1  christos };
    148          1.1  christos 
    149          1.1  christos #define ASL_DIRECTIVE_NOT_FOUND     -1
    150          1.1  christos 
    151          1.1  christos 
    152          1.1  christos /*******************************************************************************
    153          1.1  christos  *
    154          1.1  christos  * FUNCTION:    PrInitializePreprocessor
    155          1.1  christos  *
    156          1.1  christos  * PARAMETERS:  None
    157          1.1  christos  *
    158          1.1  christos  * RETURN:      None
    159          1.1  christos  *
    160          1.1  christos  * DESCRIPTION: Startup initialization for the Preprocessor.
    161          1.1  christos  *
    162          1.1  christos  ******************************************************************************/
    163          1.1  christos 
    164          1.1  christos void
    165          1.1  christos PrInitializePreprocessor (
    166          1.1  christos     void)
    167          1.1  christos {
    168          1.1  christos     /* Init globals and the list of #defines */
    169          1.1  christos 
    170          1.1  christos     PrInitializeGlobals ();
    171          1.1  christos     Gbl_DefineList = NULL;
    172          1.1  christos }
    173          1.1  christos 
    174          1.1  christos 
    175          1.1  christos /*******************************************************************************
    176          1.1  christos  *
    177          1.1  christos  * FUNCTION:    PrInitializeGlobals
    178          1.1  christos  *
    179          1.1  christos  * PARAMETERS:  None
    180          1.1  christos  *
    181          1.1  christos  * RETURN:      None
    182          1.1  christos  *
    183          1.1  christos  * DESCRIPTION: Initialize globals for the Preprocessor. Used for startuup
    184          1.1  christos  *              initialization and re-initialization between compiles during
    185          1.1  christos  *              a multiple source file compile.
    186          1.1  christos  *
    187          1.1  christos  ******************************************************************************/
    188          1.1  christos 
    189          1.1  christos void
    190          1.1  christos PrInitializeGlobals (
    191          1.1  christos     void)
    192          1.1  christos {
    193          1.1  christos     /* Init globals */
    194          1.1  christos 
    195          1.1  christos     Gbl_InputFileList = NULL;
    196  1.1.1.2.2.2     skrll     Gbl_CurrentLineNumber = 1;
    197          1.1  christos     Gbl_PreprocessorLineNumber = 1;
    198          1.1  christos     Gbl_PreprocessorError = FALSE;
    199          1.1  christos 
    200          1.1  christos     /* These are used to track #if/#else blocks (possibly nested) */
    201          1.1  christos 
    202          1.1  christos     Gbl_IfDepth = 0;
    203          1.1  christos     Gbl_IgnoringThisCodeBlock = FALSE;
    204          1.1  christos     Gbl_DirectiveStack = NULL;
    205          1.1  christos }
    206          1.1  christos 
    207          1.1  christos 
    208          1.1  christos /*******************************************************************************
    209          1.1  christos  *
    210          1.1  christos  * FUNCTION:    PrTerminatePreprocessor
    211          1.1  christos  *
    212          1.1  christos  * PARAMETERS:  None
    213          1.1  christos  *
    214          1.1  christos  * RETURN:      None
    215          1.1  christos  *
    216          1.1  christos  * DESCRIPTION: Termination of the preprocessor. Delete lists. Keep any
    217          1.1  christos  *              defines that were specified on the command line, in order to
    218          1.1  christos  *              support multiple compiles with a single compiler invocation.
    219          1.1  christos  *
    220          1.1  christos  ******************************************************************************/
    221          1.1  christos 
    222          1.1  christos void
    223          1.1  christos PrTerminatePreprocessor (
    224          1.1  christos     void)
    225          1.1  christos {
    226          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    227          1.1  christos 
    228          1.1  christos 
    229          1.1  christos     /*
    230          1.1  christos      * The persistent defines (created on the command line) are always at the
    231          1.1  christos      * end of the list. We save them.
    232          1.1  christos      */
    233          1.1  christos     while ((Gbl_DefineList) && (!Gbl_DefineList->Persist))
    234          1.1  christos     {
    235          1.1  christos         DefineInfo = Gbl_DefineList;
    236          1.1  christos         Gbl_DefineList = DefineInfo->Next;
    237          1.1  christos 
    238          1.1  christos         ACPI_FREE (DefineInfo->Replacement);
    239          1.1  christos         ACPI_FREE (DefineInfo->Identifier);
    240          1.1  christos         ACPI_FREE (DefineInfo);
    241          1.1  christos     }
    242          1.1  christos }
    243          1.1  christos 
    244          1.1  christos 
    245          1.1  christos /*******************************************************************************
    246          1.1  christos  *
    247          1.1  christos  * FUNCTION:    PrDoPreprocess
    248          1.1  christos  *
    249          1.1  christos  * PARAMETERS:  None
    250          1.1  christos  *
    251          1.1  christos  * RETURN:      None
    252          1.1  christos  *
    253          1.1  christos  * DESCRIPTION: Main entry point for the iASL Preprocessor. Input file must
    254          1.1  christos  *              be already open. Handles multiple input files via the
    255          1.1  christos  *              #include directive.
    256          1.1  christos  *
    257          1.1  christos  ******************************************************************************/
    258          1.1  christos 
    259          1.1  christos void
    260          1.1  christos PrDoPreprocess (
    261          1.1  christos     void)
    262          1.1  christos {
    263          1.1  christos     BOOLEAN                 MoreInputFiles;
    264          1.1  christos 
    265          1.1  christos 
    266          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, "Starting preprocessing phase\n\n");
    267          1.1  christos 
    268          1.1  christos 
    269          1.1  christos     FlSeekFile (ASL_FILE_INPUT, 0);
    270          1.1  christos     PrDumpPredefinedNames ();
    271          1.1  christos 
    272          1.1  christos     /* Main preprocessor loop, handles include files */
    273          1.1  christos 
    274          1.1  christos     do
    275          1.1  christos     {
    276          1.1  christos         PrPreprocessInputFile ();
    277          1.1  christos         MoreInputFiles = PrPopInputFileStack ();
    278          1.1  christos 
    279          1.1  christos     } while (MoreInputFiles);
    280          1.1  christos 
    281  1.1.1.2.2.2     skrll     /* Point compiler input to the new preprocessor output file (.pre) */
    282          1.1  christos 
    283          1.1  christos     FlCloseFile (ASL_FILE_INPUT);
    284          1.1  christos     Gbl_Files[ASL_FILE_INPUT].Handle = Gbl_Files[ASL_FILE_PREPROCESSOR].Handle;
    285          1.1  christos     AslCompilerin = Gbl_Files[ASL_FILE_INPUT].Handle;
    286          1.1  christos 
    287          1.1  christos     /* Reset globals to allow compiler to run */
    288          1.1  christos 
    289          1.1  christos     FlSeekFile (ASL_FILE_INPUT, 0);
    290  1.1.1.2.2.2     skrll     if (!Gbl_PreprocessOnly)
    291  1.1.1.2.2.2     skrll     {
    292  1.1.1.2.2.2     skrll         Gbl_CurrentLineNumber = 0;
    293  1.1.1.2.2.2     skrll     }
    294          1.1  christos 
    295          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, "Preprocessing phase complete \n\n");
    296          1.1  christos }
    297          1.1  christos 
    298          1.1  christos 
    299          1.1  christos /*******************************************************************************
    300          1.1  christos  *
    301          1.1  christos  * FUNCTION:    PrPreprocessInputFile
    302          1.1  christos  *
    303          1.1  christos  * PARAMETERS:  None
    304          1.1  christos  *
    305          1.1  christos  * RETURN:      None
    306          1.1  christos  *
    307          1.1  christos  * DESCRIPTION: Preprocess one entire file, line-by-line.
    308          1.1  christos  *
    309          1.1  christos  * Input:  Raw user ASL from ASL_FILE_INPUT
    310  1.1.1.2.2.2     skrll  * Output: Preprocessed file written to ASL_FILE_PREPROCESSOR and
    311  1.1.1.2.2.2     skrll  *         (optionally) ASL_FILE_PREPROCESSOR_USER
    312          1.1  christos  *
    313          1.1  christos  ******************************************************************************/
    314          1.1  christos 
    315          1.1  christos static void
    316          1.1  christos PrPreprocessInputFile (
    317          1.1  christos     void)
    318          1.1  christos {
    319  1.1.1.2.2.2     skrll     UINT32                  Status;
    320          1.1  christos     char                    *Token;
    321          1.1  christos     char                    *ReplaceString;
    322          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    323          1.1  christos     ACPI_SIZE               TokenOffset;
    324          1.1  christos     char                    *Next;
    325          1.1  christos     int                     OffsetAdjust;
    326          1.1  christos 
    327          1.1  christos 
    328  1.1.1.2.2.2     skrll     PrGetNextLineInit ();
    329  1.1.1.2.2.2     skrll 
    330  1.1.1.2.2.2     skrll     /* Scan source line-by-line and process directives. Then write the .i file */
    331          1.1  christos 
    332  1.1.1.2.2.2     skrll     while ((Status = PrGetNextLine (Gbl_Files[ASL_FILE_INPUT].Handle)) != ASL_EOF)
    333          1.1  christos     {
    334  1.1.1.2.2.2     skrll         Gbl_CurrentLineNumber++;
    335  1.1.1.2.2.2     skrll         Gbl_LogicalLineNumber++;
    336  1.1.1.2.2.2     skrll 
    337  1.1.1.2.2.2     skrll         if ((Status == ASL_WITHIN_COMMENT) ||
    338  1.1.1.2.2.2     skrll             (Status == ASL_BLANK_LINE))
    339  1.1.1.2.2.2     skrll         {
    340  1.1.1.2.2.2     skrll             goto WriteEntireLine;
    341  1.1.1.2.2.2     skrll         }
    342  1.1.1.2.2.2     skrll 
    343          1.1  christos         /* Need a copy of the input line for strok() */
    344          1.1  christos 
    345          1.1  christos         strcpy (Gbl_MainTokenBuffer, Gbl_CurrentLineBuffer);
    346          1.1  christos         Token = PrGetNextToken (Gbl_MainTokenBuffer, PR_TOKEN_SEPARATORS, &Next);
    347          1.1  christos         OffsetAdjust = 0;
    348          1.1  christos 
    349          1.1  christos         /* All preprocessor directives must begin with '#' */
    350          1.1  christos 
    351          1.1  christos         if (Token && (*Token == '#'))
    352          1.1  christos         {
    353          1.1  christos             if (strlen (Token) == 1)
    354          1.1  christos             {
    355          1.1  christos                 Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
    356          1.1  christos             }
    357          1.1  christos             else
    358          1.1  christos             {
    359          1.1  christos                 Token++;    /* Skip leading # */
    360          1.1  christos             }
    361          1.1  christos 
    362          1.1  christos             /* Execute the directive, do not write line to output file */
    363          1.1  christos 
    364          1.1  christos             PrDoDirective (Token, &Next);
    365          1.1  christos             continue;
    366          1.1  christos         }
    367          1.1  christos 
    368          1.1  christos         /*
    369          1.1  christos          * If we are currently within the part of an IF/ELSE block that is
    370          1.1  christos          * FALSE, ignore the line and do not write it to the output file.
    371          1.1  christos          * This continues until an #else or #endif is encountered.
    372          1.1  christos          */
    373          1.1  christos         if (Gbl_IgnoringThisCodeBlock)
    374          1.1  christos         {
    375          1.1  christos             continue;
    376          1.1  christos         }
    377          1.1  christos 
    378          1.1  christos         /* Match and replace all #defined names within this source line */
    379          1.1  christos 
    380          1.1  christos         while (Token)
    381          1.1  christos         {
    382          1.1  christos             DefineInfo = PrMatchDefine (Token);
    383          1.1  christos             if (DefineInfo)
    384          1.1  christos             {
    385          1.1  christos                 if (DefineInfo->Body)
    386          1.1  christos                 {
    387          1.1  christos                     /* This is a macro */
    388          1.1  christos 
    389          1.1  christos                     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    390          1.1  christos                         "Matched Macro: %s->%s\n",
    391          1.1  christos                         Gbl_CurrentLineNumber, DefineInfo->Identifier,
    392          1.1  christos                         DefineInfo->Replacement);
    393          1.1  christos 
    394          1.1  christos                     PrDoMacroInvocation (Gbl_MainTokenBuffer, Token,
    395          1.1  christos                         DefineInfo, &Next);
    396          1.1  christos                 }
    397          1.1  christos                 else
    398          1.1  christos                 {
    399          1.1  christos                     ReplaceString = DefineInfo->Replacement;
    400          1.1  christos 
    401          1.1  christos                     /* Replace the name in the original line buffer */
    402          1.1  christos 
    403          1.1  christos                     TokenOffset = Token - Gbl_MainTokenBuffer + OffsetAdjust;
    404          1.1  christos                     PrReplaceData (
    405          1.1  christos                         &Gbl_CurrentLineBuffer[TokenOffset], strlen (Token),
    406          1.1  christos                         ReplaceString, strlen (ReplaceString));
    407          1.1  christos 
    408          1.1  christos                     /* Adjust for length difference between old and new name length */
    409          1.1  christos 
    410          1.1  christos                     OffsetAdjust += strlen (ReplaceString) - strlen (Token);
    411          1.1  christos 
    412          1.1  christos                     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    413          1.1  christos                         "Matched #define: %s->%s\n",
    414          1.1  christos                         Gbl_CurrentLineNumber, Token,
    415          1.1  christos                         *ReplaceString ? ReplaceString : "(NULL STRING)");
    416          1.1  christos                 }
    417          1.1  christos             }
    418          1.1  christos 
    419          1.1  christos             Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, &Next);
    420          1.1  christos         }
    421          1.1  christos 
    422          1.1  christos         Gbl_PreprocessorLineNumber++;
    423          1.1  christos 
    424  1.1.1.2.2.2     skrll 
    425  1.1.1.2.2.2     skrll WriteEntireLine:
    426          1.1  christos         /*
    427          1.1  christos          * Now we can write the possibly modified source line to the
    428  1.1.1.2.2.2     skrll          * preprocessor file(s).
    429          1.1  christos          */
    430          1.1  christos         FlWriteFile (ASL_FILE_PREPROCESSOR, Gbl_CurrentLineBuffer,
    431          1.1  christos             strlen (Gbl_CurrentLineBuffer));
    432          1.1  christos     }
    433          1.1  christos }
    434          1.1  christos 
    435          1.1  christos 
    436          1.1  christos /*******************************************************************************
    437          1.1  christos  *
    438          1.1  christos  * FUNCTION:    PrDoDirective
    439          1.1  christos  *
    440          1.1  christos  * PARAMETERS:  Directive               - Pointer to directive name token
    441          1.1  christos  *              Next                    - "Next" buffer from GetNextToken
    442          1.1  christos  *
    443          1.1  christos  * RETURN:      None.
    444          1.1  christos  *
    445          1.1  christos  * DESCRIPTION: Main processing for all preprocessor directives
    446          1.1  christos  *
    447          1.1  christos  ******************************************************************************/
    448          1.1  christos 
    449          1.1  christos static void
    450          1.1  christos PrDoDirective (
    451          1.1  christos     char                    *DirectiveToken,
    452          1.1  christos     char                    **Next)
    453          1.1  christos {
    454          1.1  christos     char                    *Token = Gbl_MainTokenBuffer;
    455  1.1.1.2.2.2     skrll     char                    *Token2 = NULL;
    456          1.1  christos     char                    *End;
    457          1.1  christos     UINT64                  Value;
    458          1.1  christos     ACPI_SIZE               TokenOffset;
    459          1.1  christos     int                     Directive;
    460          1.1  christos     ACPI_STATUS             Status;
    461          1.1  christos 
    462          1.1  christos 
    463          1.1  christos     if (!DirectiveToken)
    464          1.1  christos     {
    465          1.1  christos         goto SyntaxError;
    466          1.1  christos     }
    467          1.1  christos 
    468          1.1  christos     Directive = PrMatchDirective (DirectiveToken);
    469          1.1  christos     if (Directive == ASL_DIRECTIVE_NOT_FOUND)
    470          1.1  christos     {
    471          1.1  christos         PrError (ASL_ERROR, ASL_MSG_UNKNOWN_DIRECTIVE,
    472          1.1  christos             THIS_TOKEN_OFFSET (DirectiveToken));
    473          1.1  christos 
    474  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    475          1.1  christos             "#%s: Unknown directive\n",
    476          1.1  christos             Gbl_CurrentLineNumber, DirectiveToken);
    477          1.1  christos         return;
    478          1.1  christos     }
    479          1.1  christos 
    480          1.1  christos     /*
    481  1.1.1.2.2.2     skrll      * Emit a line directive into the preprocessor file (.pre) after
    482  1.1.1.2.2.2     skrll      * every matched directive. This is passed through to the compiler
    483  1.1.1.2.2.2     skrll      * so that error/warning messages are kept in sync with the
    484  1.1.1.2.2.2     skrll      * original source file.
    485  1.1.1.2.2.2     skrll      */
    486  1.1.1.2.2.2     skrll     FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\" // #%s\n",
    487  1.1.1.2.2.2     skrll         Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename,
    488  1.1.1.2.2.2     skrll         Gbl_DirectiveInfo[Directive].Name);
    489  1.1.1.2.2.2     skrll 
    490  1.1.1.2.2.2     skrll     /*
    491          1.1  christos      * If we are currently ignoring this block and we encounter a #else or
    492          1.1  christos      * #elif, we must ignore their blocks also if the parent block is also
    493          1.1  christos      * being ignored.
    494          1.1  christos      */
    495          1.1  christos     if (Gbl_IgnoringThisCodeBlock)
    496          1.1  christos     {
    497          1.1  christos         switch (Directive)
    498          1.1  christos         {
    499          1.1  christos         case PR_DIRECTIVE_ELSE:
    500          1.1  christos         case PR_DIRECTIVE_ELIF:
    501          1.1  christos 
    502          1.1  christos             if (Gbl_DirectiveStack && Gbl_DirectiveStack->IgnoringThisCodeBlock)
    503          1.1  christos             {
    504          1.1  christos                 PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name);
    505          1.1  christos                 return;
    506          1.1  christos             }
    507          1.1  christos             break;
    508          1.1  christos 
    509          1.1  christos         default:
    510          1.1  christos             break;
    511          1.1  christos         }
    512          1.1  christos     }
    513          1.1  christos 
    514          1.1  christos     /*
    515          1.1  christos      * Need to always check for #else, #elif, #endif regardless of
    516          1.1  christos      * whether we are ignoring the current code block, since these
    517          1.1  christos      * are conditional code block terminators.
    518          1.1  christos      */
    519          1.1  christos     switch (Directive)
    520          1.1  christos     {
    521          1.1  christos     case PR_DIRECTIVE_ELSE:
    522          1.1  christos 
    523          1.1  christos         Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock);
    524          1.1  christos         PrDbgPrint ("Executing", "else block");
    525          1.1  christos         return;
    526          1.1  christos 
    527          1.1  christos     case PR_DIRECTIVE_ELIF:
    528          1.1  christos 
    529          1.1  christos         Gbl_IgnoringThisCodeBlock = !(Gbl_IgnoringThisCodeBlock);
    530          1.1  christos         Directive = PR_DIRECTIVE_IF;
    531          1.1  christos 
    532          1.1  christos         if (Gbl_IgnoringThisCodeBlock == TRUE)
    533          1.1  christos         {
    534          1.1  christos             /* Not executing the ELSE part -- all done here */
    535          1.1  christos             PrDbgPrint ("Ignoring", "elif block");
    536          1.1  christos             return;
    537          1.1  christos         }
    538          1.1  christos 
    539          1.1  christos         /*
    540          1.1  christos          * After this, we will execute the IF part further below.
    541          1.1  christos          * First, however, pop off the original #if directive.
    542          1.1  christos          */
    543          1.1  christos         if (ACPI_FAILURE (PrPopDirective ()))
    544          1.1  christos         {
    545          1.1  christos             PrError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
    546          1.1  christos                 THIS_TOKEN_OFFSET (DirectiveToken));
    547          1.1  christos         }
    548          1.1  christos 
    549          1.1  christos         PrDbgPrint ("Executing", "elif block");
    550          1.1  christos         break;
    551          1.1  christos 
    552          1.1  christos     case PR_DIRECTIVE_ENDIF:
    553          1.1  christos 
    554          1.1  christos         PrDbgPrint ("Executing", "endif");
    555          1.1  christos 
    556          1.1  christos         /* Pop the owning #if/#ifdef/#ifndef */
    557          1.1  christos 
    558          1.1  christos         if (ACPI_FAILURE (PrPopDirective ()))
    559          1.1  christos         {
    560          1.1  christos             PrError (ASL_ERROR, ASL_MSG_ENDIF_MISMATCH,
    561          1.1  christos                 THIS_TOKEN_OFFSET (DirectiveToken));
    562          1.1  christos         }
    563          1.1  christos         return;
    564          1.1  christos 
    565          1.1  christos     default:
    566          1.1  christos         break;
    567          1.1  christos     }
    568          1.1  christos 
    569          1.1  christos     /* Most directives have at least one argument */
    570          1.1  christos 
    571  1.1.1.2.2.2     skrll     if (Gbl_DirectiveInfo[Directive].ArgCount >= 1)
    572          1.1  christos     {
    573          1.1  christos         Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
    574          1.1  christos         if (!Token)
    575          1.1  christos         {
    576          1.1  christos             goto SyntaxError;
    577          1.1  christos         }
    578          1.1  christos     }
    579          1.1  christos 
    580  1.1.1.2.2.2     skrll     if (Gbl_DirectiveInfo[Directive].ArgCount >= 2)
    581  1.1.1.2.2.2     skrll     {
    582  1.1.1.2.2.2     skrll         Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
    583  1.1.1.2.2.2     skrll         if (!Token2)
    584  1.1.1.2.2.2     skrll         {
    585  1.1.1.2.2.2     skrll             goto SyntaxError;
    586  1.1.1.2.2.2     skrll         }
    587  1.1.1.2.2.2     skrll     }
    588  1.1.1.2.2.2     skrll 
    589          1.1  christos     /*
    590          1.1  christos      * At this point, if we are ignoring the current code block,
    591          1.1  christos      * do not process any more directives (i.e., ignore them also.)
    592          1.1  christos      * For "if" style directives, open/push a new block anyway. We
    593          1.1  christos      * must do this to keep track of #endif directives
    594          1.1  christos      */
    595          1.1  christos     if (Gbl_IgnoringThisCodeBlock)
    596          1.1  christos     {
    597          1.1  christos         switch (Directive)
    598          1.1  christos         {
    599          1.1  christos         case PR_DIRECTIVE_IF:
    600          1.1  christos         case PR_DIRECTIVE_IFDEF:
    601          1.1  christos         case PR_DIRECTIVE_IFNDEF:
    602          1.1  christos 
    603          1.1  christos             PrPushDirective (Directive, Token);
    604          1.1  christos             PrDbgPrint ("Ignoring", Gbl_DirectiveInfo[Directive].Name);
    605          1.1  christos             break;
    606          1.1  christos 
    607          1.1  christos         default:
    608          1.1  christos             break;
    609          1.1  christos         }
    610          1.1  christos 
    611          1.1  christos         return;
    612          1.1  christos     }
    613          1.1  christos 
    614          1.1  christos     /*
    615          1.1  christos      * Execute the directive
    616          1.1  christos      */
    617          1.1  christos     PrDbgPrint ("Begin execution", Gbl_DirectiveInfo[Directive].Name);
    618          1.1  christos 
    619          1.1  christos     switch (Directive)
    620          1.1  christos     {
    621          1.1  christos     case PR_DIRECTIVE_IF:
    622          1.1  christos 
    623          1.1  christos         TokenOffset = Token - Gbl_MainTokenBuffer;
    624          1.1  christos 
    625          1.1  christos         /* Need to expand #define macros in the expression string first */
    626          1.1  christos 
    627          1.1  christos         Status = PrResolveIntegerExpression (
    628          1.1  christos             &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
    629          1.1  christos         if (ACPI_FAILURE (Status))
    630          1.1  christos         {
    631          1.1  christos             return;
    632          1.1  christos         }
    633          1.1  christos 
    634          1.1  christos         PrPushDirective (Directive, Token);
    635          1.1  christos         if (!Value)
    636          1.1  christos         {
    637          1.1  christos             Gbl_IgnoringThisCodeBlock = TRUE;
    638          1.1  christos         }
    639          1.1  christos 
    640  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    641          1.1  christos             "Resolved #if: %8.8X%8.8X %s\n",
    642          1.1  christos             Gbl_CurrentLineNumber, ACPI_FORMAT_UINT64 (Value),
    643          1.1  christos             Gbl_IgnoringThisCodeBlock ? "<Skipping Block>" : "<Executing Block>");
    644          1.1  christos         break;
    645          1.1  christos 
    646          1.1  christos     case PR_DIRECTIVE_IFDEF:
    647          1.1  christos 
    648          1.1  christos         PrPushDirective (Directive, Token);
    649          1.1  christos         if (!PrMatchDefine (Token))
    650          1.1  christos         {
    651          1.1  christos             Gbl_IgnoringThisCodeBlock = TRUE;
    652          1.1  christos         }
    653          1.1  christos 
    654          1.1  christos         PrDbgPrint ("Evaluated", "ifdef");
    655          1.1  christos         break;
    656          1.1  christos 
    657          1.1  christos     case PR_DIRECTIVE_IFNDEF:
    658          1.1  christos 
    659          1.1  christos         PrPushDirective (Directive, Token);
    660          1.1  christos         if (PrMatchDefine (Token))
    661          1.1  christos         {
    662          1.1  christos             Gbl_IgnoringThisCodeBlock = TRUE;
    663          1.1  christos         }
    664          1.1  christos 
    665          1.1  christos         PrDbgPrint ("Evaluated", "ifndef");
    666          1.1  christos         break;
    667          1.1  christos 
    668          1.1  christos     case PR_DIRECTIVE_DEFINE:
    669          1.1  christos         /*
    670          1.1  christos          * By definition, if first char after the name is a paren,
    671          1.1  christos          * this is a function macro.
    672          1.1  christos          */
    673          1.1  christos         TokenOffset = Token - Gbl_MainTokenBuffer + strlen (Token);
    674          1.1  christos         if (*(&Gbl_CurrentLineBuffer[TokenOffset]) == '(')
    675          1.1  christos         {
    676          1.1  christos #ifndef MACROS_SUPPORTED
    677          1.1  christos             AcpiOsPrintf ("%s ERROR - line %u: #define macros are not supported yet\n",
    678  1.1.1.2.2.2     skrll                 Gbl_CurrentLineBuffer, Gbl_LogicalLineNumber);
    679          1.1  christos             exit(1);
    680          1.1  christos #else
    681          1.1  christos             PrAddMacro (Token, Next);
    682          1.1  christos #endif
    683          1.1  christos         }
    684          1.1  christos         else
    685          1.1  christos         {
    686          1.1  christos             /* Use the remainder of the line for the #define */
    687          1.1  christos 
    688          1.1  christos             Token2 = *Next;
    689          1.1  christos             if (Token2)
    690          1.1  christos             {
    691          1.1  christos                 while ((*Token2 == ' ') || (*Token2 == '\t'))
    692          1.1  christos                 {
    693          1.1  christos                     Token2++;
    694          1.1  christos                 }
    695          1.1  christos                 End = Token2;
    696          1.1  christos                 while (*End != '\n')
    697          1.1  christos                 {
    698          1.1  christos                     End++;
    699          1.1  christos                 }
    700          1.1  christos                 *End = 0;
    701          1.1  christos             }
    702          1.1  christos             else
    703          1.1  christos             {
    704          1.1  christos                 Token2 = "";
    705          1.1  christos             }
    706          1.1  christos #if 0
    707          1.1  christos             Token2 = PrGetNextToken (NULL, "\n", /*PR_TOKEN_SEPARATORS,*/ Next);
    708          1.1  christos             if (!Token2)
    709          1.1  christos             {
    710          1.1  christos                 Token2 = "";
    711          1.1  christos             }
    712          1.1  christos #endif
    713  1.1.1.2.2.2     skrll             DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    714          1.1  christos                 "New #define: %s->%s\n",
    715  1.1.1.2.2.2     skrll                 Gbl_LogicalLineNumber, Token, Token2);
    716          1.1  christos 
    717          1.1  christos             PrAddDefine (Token, Token2, FALSE);
    718          1.1  christos         }
    719          1.1  christos         break;
    720          1.1  christos 
    721          1.1  christos     case PR_DIRECTIVE_ERROR:
    722          1.1  christos 
    723          1.1  christos         /* Note: No macro expansion */
    724          1.1  christos 
    725          1.1  christos         PrError (ASL_ERROR, ASL_MSG_ERROR_DIRECTIVE,
    726          1.1  christos             THIS_TOKEN_OFFSET (Token));
    727          1.1  christos 
    728          1.1  christos         Gbl_SourceLine = 0;
    729          1.1  christos         Gbl_NextError = Gbl_ErrorLog;
    730          1.1  christos         CmCleanupAndExit ();
    731          1.1  christos         exit(1);
    732          1.1  christos 
    733          1.1  christos     case PR_DIRECTIVE_INCLUDE:
    734          1.1  christos 
    735          1.1  christos         Token = PrGetNextToken (NULL, " \"<>", Next);
    736          1.1  christos         if (!Token)
    737          1.1  christos         {
    738          1.1  christos             goto SyntaxError;
    739          1.1  christos         }
    740          1.1  christos 
    741  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    742          1.1  christos             "Start #include file \"%s\"\n", Gbl_CurrentLineNumber,
    743          1.1  christos             Token, Gbl_CurrentLineNumber);
    744          1.1  christos 
    745  1.1.1.2.2.2     skrll         PrDoIncludeFile (Token);
    746  1.1.1.2.2.2     skrll         break;
    747  1.1.1.2.2.2     skrll 
    748  1.1.1.2.2.2     skrll     case PR_DIRECTIVE_INCLUDEBUFFER:
    749  1.1.1.2.2.2     skrll 
    750  1.1.1.2.2.2     skrll         Token = PrGetNextToken (NULL, " \"<>", Next);
    751  1.1.1.2.2.2     skrll         if (!Token)
    752  1.1.1.2.2.2     skrll         {
    753  1.1.1.2.2.2     skrll             goto SyntaxError;
    754  1.1.1.2.2.2     skrll         }
    755  1.1.1.2.2.2     skrll 
    756  1.1.1.2.2.2     skrll         Token2 = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
    757  1.1.1.2.2.2     skrll         if (!Token2)
    758  1.1.1.2.2.2     skrll         {
    759  1.1.1.2.2.2     skrll             goto SyntaxError;
    760  1.1.1.2.2.2     skrll         }
    761  1.1.1.2.2.2     skrll 
    762  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    763  1.1.1.2.2.2     skrll             "Start #includebuffer input from file \"%s\", buffer name %s\n",
    764  1.1.1.2.2.2     skrll             Gbl_CurrentLineNumber, Token, Token2);
    765  1.1.1.2.2.2     skrll 
    766  1.1.1.2.2.2     skrll         PrDoIncludeBuffer (Token, Token2);
    767          1.1  christos         break;
    768          1.1  christos 
    769          1.1  christos     case PR_DIRECTIVE_LINE:
    770          1.1  christos 
    771          1.1  christos         TokenOffset = Token - Gbl_MainTokenBuffer;
    772          1.1  christos 
    773          1.1  christos         Status = PrResolveIntegerExpression (
    774          1.1  christos             &Gbl_CurrentLineBuffer[TokenOffset-1], &Value);
    775          1.1  christos         if (ACPI_FAILURE (Status))
    776          1.1  christos         {
    777          1.1  christos             return;
    778          1.1  christos         }
    779          1.1  christos 
    780  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    781          1.1  christos             "User #line invocation %s\n", Gbl_CurrentLineNumber,
    782          1.1  christos             Token);
    783          1.1  christos 
    784          1.1  christos         Gbl_CurrentLineNumber = (UINT32) Value;
    785          1.1  christos 
    786          1.1  christos         /* Emit #line into the preprocessor file */
    787          1.1  christos 
    788          1.1  christos         FlPrintFile (ASL_FILE_PREPROCESSOR, "#line %u \"%s\"\n",
    789          1.1  christos             Gbl_CurrentLineNumber, Gbl_Files[ASL_FILE_INPUT].Filename);
    790          1.1  christos         break;
    791          1.1  christos 
    792          1.1  christos     case PR_DIRECTIVE_PRAGMA:
    793          1.1  christos 
    794          1.1  christos         if (!strcmp (Token, "disable"))
    795          1.1  christos         {
    796          1.1  christos             Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
    797          1.1  christos             if (!Token)
    798          1.1  christos             {
    799          1.1  christos                 goto SyntaxError;
    800          1.1  christos             }
    801          1.1  christos 
    802          1.1  christos             TokenOffset = Token - Gbl_MainTokenBuffer;
    803          1.1  christos             AslDisableException (&Gbl_CurrentLineBuffer[TokenOffset]);
    804          1.1  christos         }
    805          1.1  christos         else if (!strcmp (Token, "message"))
    806          1.1  christos         {
    807          1.1  christos             Token = PrGetNextToken (NULL, PR_TOKEN_SEPARATORS, Next);
    808          1.1  christos             if (!Token)
    809          1.1  christos             {
    810          1.1  christos                 goto SyntaxError;
    811          1.1  christos             }
    812          1.1  christos 
    813          1.1  christos             TokenOffset = Token - Gbl_MainTokenBuffer;
    814          1.1  christos             AcpiOsPrintf ("%s\n", &Gbl_CurrentLineBuffer[TokenOffset]);
    815          1.1  christos         }
    816          1.1  christos         else
    817          1.1  christos         {
    818          1.1  christos             PrError (ASL_ERROR, ASL_MSG_UNKNOWN_PRAGMA,
    819          1.1  christos                 THIS_TOKEN_OFFSET (Token));
    820          1.1  christos             return;
    821          1.1  christos         }
    822          1.1  christos 
    823          1.1  christos         break;
    824          1.1  christos 
    825          1.1  christos     case PR_DIRECTIVE_UNDEF:
    826          1.1  christos 
    827  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    828          1.1  christos             "#undef: %s\n", Gbl_CurrentLineNumber, Token);
    829          1.1  christos 
    830          1.1  christos         PrRemoveDefine (Token);
    831          1.1  christos         break;
    832          1.1  christos 
    833          1.1  christos     case PR_DIRECTIVE_WARNING:
    834          1.1  christos 
    835          1.1  christos         PrError (ASL_WARNING, ASL_MSG_WARNING_DIRECTIVE,
    836          1.1  christos             THIS_TOKEN_OFFSET (Token));
    837  1.1.1.2.2.2     skrll 
    838  1.1.1.2.2.2     skrll         Gbl_SourceLine = 0;
    839  1.1.1.2.2.2     skrll         Gbl_NextError = Gbl_ErrorLog;
    840          1.1  christos         break;
    841          1.1  christos 
    842          1.1  christos     default:
    843          1.1  christos 
    844          1.1  christos         /* Should never get here */
    845  1.1.1.2.2.2     skrll         DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
    846          1.1  christos             "Unrecognized directive: %u\n",
    847          1.1  christos             Gbl_CurrentLineNumber, Directive);
    848          1.1  christos         break;
    849          1.1  christos     }
    850          1.1  christos 
    851          1.1  christos     return;
    852          1.1  christos 
    853          1.1  christos SyntaxError:
    854          1.1  christos 
    855          1.1  christos     PrError (ASL_ERROR, ASL_MSG_DIRECTIVE_SYNTAX,
    856          1.1  christos         THIS_TOKEN_OFFSET (DirectiveToken));
    857          1.1  christos     return;
    858          1.1  christos }
    859          1.1  christos 
    860          1.1  christos 
    861          1.1  christos /*******************************************************************************
    862          1.1  christos  *
    863  1.1.1.2.2.2     skrll  * FUNCTION:    PrGetNextLine, PrGetNextLineInit
    864  1.1.1.2.2.2     skrll  *
    865  1.1.1.2.2.2     skrll  * PARAMETERS:  Handle              - Open file handle for the source file
    866  1.1.1.2.2.2     skrll  *
    867  1.1.1.2.2.2     skrll  * RETURN:      Status of the GetLine operation:
    868  1.1.1.2.2.2     skrll  *              AE_OK               - Normal line, OK status
    869  1.1.1.2.2.2     skrll  *              ASL_WITHIN_COMMENT  - Line is part of a multi-line comment
    870  1.1.1.2.2.2     skrll  *              ASL_EOF             - End-of-file reached
    871  1.1.1.2.2.2     skrll  *
    872  1.1.1.2.2.2     skrll  * DESCRIPTION: Get the next text line from the input file. Does not strip
    873  1.1.1.2.2.2     skrll  *              comments.
    874  1.1.1.2.2.2     skrll  *
    875  1.1.1.2.2.2     skrll  ******************************************************************************/
    876  1.1.1.2.2.2     skrll 
    877  1.1.1.2.2.2     skrll #define PR_NORMAL_TEXT          0
    878  1.1.1.2.2.2     skrll #define PR_MULTI_LINE_COMMENT   1
    879  1.1.1.2.2.2     skrll #define PR_SINGLE_LINE_COMMENT  2
    880  1.1.1.2.2.2     skrll #define PR_QUOTED_STRING        3
    881  1.1.1.2.2.2     skrll 
    882  1.1.1.2.2.2     skrll static UINT8                    AcpiGbl_LineScanState = PR_NORMAL_TEXT;
    883  1.1.1.2.2.2     skrll 
    884  1.1.1.2.2.2     skrll static void
    885  1.1.1.2.2.2     skrll PrGetNextLineInit (
    886  1.1.1.2.2.2     skrll     void)
    887  1.1.1.2.2.2     skrll {
    888  1.1.1.2.2.2     skrll     AcpiGbl_LineScanState = 0;
    889  1.1.1.2.2.2     skrll }
    890  1.1.1.2.2.2     skrll 
    891  1.1.1.2.2.2     skrll static UINT32
    892  1.1.1.2.2.2     skrll PrGetNextLine (
    893  1.1.1.2.2.2     skrll     FILE                    *Handle)
    894  1.1.1.2.2.2     skrll {
    895  1.1.1.2.2.2     skrll     UINT32                  i;
    896  1.1.1.2.2.2     skrll     int                     c = 0;
    897  1.1.1.2.2.2     skrll     int                     PreviousChar;
    898  1.1.1.2.2.2     skrll 
    899  1.1.1.2.2.2     skrll 
    900  1.1.1.2.2.2     skrll     /* Always clear the global line buffer */
    901  1.1.1.2.2.2     skrll 
    902  1.1.1.2.2.2     skrll     memset (Gbl_CurrentLineBuffer, 0, Gbl_LineBufferSize);
    903  1.1.1.2.2.2     skrll     for (i = 0; ;)
    904  1.1.1.2.2.2     skrll     {
    905  1.1.1.2.2.2     skrll         /*
    906  1.1.1.2.2.2     skrll          * If line is too long, expand the line buffers. Also increases
    907  1.1.1.2.2.2     skrll          * Gbl_LineBufferSize.
    908  1.1.1.2.2.2     skrll          */
    909  1.1.1.2.2.2     skrll         if (i >= Gbl_LineBufferSize)
    910  1.1.1.2.2.2     skrll         {
    911  1.1.1.2.2.2     skrll             UtExpandLineBuffers ();
    912  1.1.1.2.2.2     skrll         }
    913  1.1.1.2.2.2     skrll 
    914  1.1.1.2.2.2     skrll         PreviousChar = c;
    915  1.1.1.2.2.2     skrll         c = getc (Handle);
    916  1.1.1.2.2.2     skrll         if (c == EOF)
    917  1.1.1.2.2.2     skrll         {
    918  1.1.1.2.2.2     skrll             return (ASL_EOF);
    919  1.1.1.2.2.2     skrll         }
    920  1.1.1.2.2.2     skrll 
    921  1.1.1.2.2.2     skrll         /* Update state machine as necessary */
    922  1.1.1.2.2.2     skrll 
    923  1.1.1.2.2.2     skrll         switch (AcpiGbl_LineScanState)
    924  1.1.1.2.2.2     skrll         {
    925  1.1.1.2.2.2     skrll         case PR_NORMAL_TEXT:
    926  1.1.1.2.2.2     skrll 
    927  1.1.1.2.2.2     skrll             /* Check for multi-line comment start */
    928  1.1.1.2.2.2     skrll 
    929  1.1.1.2.2.2     skrll             if ((PreviousChar == '/') && (c == '*'))
    930  1.1.1.2.2.2     skrll             {
    931  1.1.1.2.2.2     skrll                 AcpiGbl_LineScanState = PR_MULTI_LINE_COMMENT;
    932  1.1.1.2.2.2     skrll             }
    933  1.1.1.2.2.2     skrll 
    934  1.1.1.2.2.2     skrll             /* Check for single-line comment start */
    935  1.1.1.2.2.2     skrll 
    936  1.1.1.2.2.2     skrll             else if ((PreviousChar == '/') && (c == '/'))
    937  1.1.1.2.2.2     skrll             {
    938  1.1.1.2.2.2     skrll                 AcpiGbl_LineScanState = PR_SINGLE_LINE_COMMENT;
    939  1.1.1.2.2.2     skrll             }
    940  1.1.1.2.2.2     skrll 
    941  1.1.1.2.2.2     skrll             /* Check for quoted string start */
    942  1.1.1.2.2.2     skrll 
    943  1.1.1.2.2.2     skrll             else if (PreviousChar == '"')
    944  1.1.1.2.2.2     skrll             {
    945  1.1.1.2.2.2     skrll                 AcpiGbl_LineScanState = PR_QUOTED_STRING;
    946  1.1.1.2.2.2     skrll             }
    947  1.1.1.2.2.2     skrll             break;
    948  1.1.1.2.2.2     skrll 
    949  1.1.1.2.2.2     skrll         case PR_QUOTED_STRING:
    950  1.1.1.2.2.2     skrll 
    951  1.1.1.2.2.2     skrll             if (PreviousChar == '"')
    952  1.1.1.2.2.2     skrll             {
    953  1.1.1.2.2.2     skrll                 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
    954  1.1.1.2.2.2     skrll             }
    955  1.1.1.2.2.2     skrll             break;
    956  1.1.1.2.2.2     skrll 
    957  1.1.1.2.2.2     skrll         case PR_MULTI_LINE_COMMENT:
    958  1.1.1.2.2.2     skrll 
    959  1.1.1.2.2.2     skrll             /* Check for multi-line comment end */
    960  1.1.1.2.2.2     skrll 
    961  1.1.1.2.2.2     skrll             if ((PreviousChar == '*') && (c == '/'))
    962  1.1.1.2.2.2     skrll             {
    963  1.1.1.2.2.2     skrll                 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
    964  1.1.1.2.2.2     skrll             }
    965  1.1.1.2.2.2     skrll             break;
    966  1.1.1.2.2.2     skrll 
    967  1.1.1.2.2.2     skrll         case PR_SINGLE_LINE_COMMENT: /* Just ignore text until EOL */
    968  1.1.1.2.2.2     skrll         default:
    969  1.1.1.2.2.2     skrll             break;
    970  1.1.1.2.2.2     skrll         }
    971  1.1.1.2.2.2     skrll 
    972  1.1.1.2.2.2     skrll         /* Always copy the character into line buffer */
    973  1.1.1.2.2.2     skrll 
    974  1.1.1.2.2.2     skrll         Gbl_CurrentLineBuffer[i] = (char) c;
    975  1.1.1.2.2.2     skrll         i++;
    976  1.1.1.2.2.2     skrll 
    977  1.1.1.2.2.2     skrll         /* Always exit on end-of-line */
    978  1.1.1.2.2.2     skrll 
    979  1.1.1.2.2.2     skrll         if (c == '\n')
    980  1.1.1.2.2.2     skrll         {
    981  1.1.1.2.2.2     skrll             /* Handle multi-line comments */
    982  1.1.1.2.2.2     skrll 
    983  1.1.1.2.2.2     skrll             if (AcpiGbl_LineScanState == PR_MULTI_LINE_COMMENT)
    984  1.1.1.2.2.2     skrll             {
    985  1.1.1.2.2.2     skrll                 return (ASL_WITHIN_COMMENT);
    986  1.1.1.2.2.2     skrll             }
    987  1.1.1.2.2.2     skrll 
    988  1.1.1.2.2.2     skrll             /* End of single-line comment */
    989  1.1.1.2.2.2     skrll 
    990  1.1.1.2.2.2     skrll             if (AcpiGbl_LineScanState == PR_SINGLE_LINE_COMMENT)
    991  1.1.1.2.2.2     skrll             {
    992  1.1.1.2.2.2     skrll                 AcpiGbl_LineScanState = PR_NORMAL_TEXT;
    993  1.1.1.2.2.2     skrll                 return (AE_OK);
    994  1.1.1.2.2.2     skrll             }
    995  1.1.1.2.2.2     skrll 
    996  1.1.1.2.2.2     skrll             /* Blank line */
    997  1.1.1.2.2.2     skrll 
    998  1.1.1.2.2.2     skrll             if (i == 1)
    999  1.1.1.2.2.2     skrll             {
   1000  1.1.1.2.2.2     skrll                 return (ASL_BLANK_LINE);
   1001  1.1.1.2.2.2     skrll             }
   1002  1.1.1.2.2.2     skrll             return (AE_OK);
   1003  1.1.1.2.2.2     skrll         }
   1004  1.1.1.2.2.2     skrll     }
   1005  1.1.1.2.2.2     skrll }
   1006  1.1.1.2.2.2     skrll 
   1007  1.1.1.2.2.2     skrll 
   1008  1.1.1.2.2.2     skrll /*******************************************************************************
   1009  1.1.1.2.2.2     skrll  *
   1010          1.1  christos  * FUNCTION:    PrMatchDirective
   1011          1.1  christos  *
   1012          1.1  christos  * PARAMETERS:  Directive           - Pointer to directive name token
   1013          1.1  christos  *
   1014          1.1  christos  * RETURN:      Index into command array, -1 if not found
   1015          1.1  christos  *
   1016          1.1  christos  * DESCRIPTION: Lookup the incoming directive in the known directives table.
   1017          1.1  christos  *
   1018          1.1  christos  ******************************************************************************/
   1019          1.1  christos 
   1020          1.1  christos static int
   1021          1.1  christos PrMatchDirective (
   1022          1.1  christos     char                    *Directive)
   1023          1.1  christos {
   1024          1.1  christos     int                     i;
   1025          1.1  christos 
   1026          1.1  christos 
   1027          1.1  christos     if (!Directive || Directive[0] == 0)
   1028          1.1  christos     {
   1029          1.1  christos         return (ASL_DIRECTIVE_NOT_FOUND);
   1030          1.1  christos     }
   1031          1.1  christos 
   1032          1.1  christos     for (i = 0; Gbl_DirectiveInfo[i].Name; i++)
   1033          1.1  christos     {
   1034          1.1  christos         if (!strcmp (Gbl_DirectiveInfo[i].Name, Directive))
   1035          1.1  christos         {
   1036          1.1  christos             return (i);
   1037          1.1  christos         }
   1038          1.1  christos     }
   1039          1.1  christos 
   1040          1.1  christos     return (ASL_DIRECTIVE_NOT_FOUND);    /* Command not recognized */
   1041          1.1  christos }
   1042          1.1  christos 
   1043          1.1  christos 
   1044          1.1  christos /*******************************************************************************
   1045          1.1  christos  *
   1046          1.1  christos  * FUNCTION:    PrPushDirective
   1047          1.1  christos  *
   1048          1.1  christos  * PARAMETERS:  Directive           - Encoded directive ID
   1049          1.1  christos  *              Argument            - String containing argument to the
   1050          1.1  christos  *                                    directive
   1051          1.1  christos  *
   1052          1.1  christos  * RETURN:      None
   1053          1.1  christos  *
   1054          1.1  christos  * DESCRIPTION: Push an item onto the directive stack. Used for processing
   1055          1.1  christos  *              nested #if/#else type conditional compilation directives.
   1056          1.1  christos  *              Specifically: Used on detection of #if/#ifdef/#ifndef to open
   1057          1.1  christos  *              a block.
   1058          1.1  christos  *
   1059          1.1  christos  ******************************************************************************/
   1060          1.1  christos 
   1061          1.1  christos static void
   1062          1.1  christos PrPushDirective (
   1063          1.1  christos     int                     Directive,
   1064          1.1  christos     char                    *Argument)
   1065          1.1  christos {
   1066          1.1  christos     DIRECTIVE_INFO          *Info;
   1067          1.1  christos 
   1068          1.1  christos 
   1069          1.1  christos     /* Allocate and populate a stack info item */
   1070          1.1  christos 
   1071          1.1  christos     Info = ACPI_ALLOCATE (sizeof (DIRECTIVE_INFO));
   1072          1.1  christos 
   1073          1.1  christos     Info->Next = Gbl_DirectiveStack;
   1074          1.1  christos     Info->Directive = Directive;
   1075          1.1  christos     Info->IgnoringThisCodeBlock = Gbl_IgnoringThisCodeBlock;
   1076          1.1  christos     strncpy (Info->Argument, Argument, MAX_ARGUMENT_LENGTH);
   1077          1.1  christos 
   1078          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT,
   1079          1.1  christos         "Pr(%.4u) - [%u %s] %*s Pushed [#%s %s]: IgnoreFlag = %s\n",
   1080          1.1  christos         Gbl_CurrentLineNumber, Gbl_IfDepth,
   1081          1.1  christos         Gbl_IgnoringThisCodeBlock ? "I" : "E",
   1082          1.1  christos         Gbl_IfDepth * 4, " ",
   1083          1.1  christos         Gbl_DirectiveInfo[Directive].Name,
   1084          1.1  christos         Argument, Gbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
   1085          1.1  christos 
   1086          1.1  christos     /* Push new item */
   1087          1.1  christos 
   1088          1.1  christos     Gbl_DirectiveStack = Info;
   1089          1.1  christos     Gbl_IfDepth++;
   1090          1.1  christos }
   1091          1.1  christos 
   1092          1.1  christos 
   1093          1.1  christos /*******************************************************************************
   1094          1.1  christos  *
   1095          1.1  christos  * FUNCTION:    PrPopDirective
   1096          1.1  christos  *
   1097          1.1  christos  * PARAMETERS:  None
   1098          1.1  christos  *
   1099          1.1  christos  * RETURN:      Status. Error if the stack is empty.
   1100          1.1  christos  *
   1101          1.1  christos  * DESCRIPTION: Pop an item off the directive stack. Used for processing
   1102          1.1  christos  *              nested #if/#else type conditional compilation directives.
   1103          1.1  christos  *              Specifically: Used on detection of #elif and #endif to remove
   1104          1.1  christos  *              the original #if/#ifdef/#ifndef from the stack and close
   1105          1.1  christos  *              the block.
   1106          1.1  christos  *
   1107          1.1  christos  ******************************************************************************/
   1108          1.1  christos 
   1109          1.1  christos static ACPI_STATUS
   1110          1.1  christos PrPopDirective (
   1111          1.1  christos     void)
   1112          1.1  christos {
   1113          1.1  christos     DIRECTIVE_INFO          *Info;
   1114          1.1  christos 
   1115          1.1  christos 
   1116          1.1  christos     /* Check for empty stack */
   1117          1.1  christos 
   1118          1.1  christos     Info = Gbl_DirectiveStack;
   1119          1.1  christos     if (!Info)
   1120          1.1  christos     {
   1121          1.1  christos         return (AE_ERROR);
   1122          1.1  christos     }
   1123          1.1  christos 
   1124          1.1  christos     /* Pop one item, keep globals up-to-date */
   1125          1.1  christos 
   1126          1.1  christos     Gbl_IfDepth--;
   1127          1.1  christos     Gbl_IgnoringThisCodeBlock = Info->IgnoringThisCodeBlock;
   1128          1.1  christos     Gbl_DirectiveStack = Info->Next;
   1129          1.1  christos 
   1130          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT,
   1131          1.1  christos         "Pr(%.4u) - [%u %s] %*s Popped [#%s %s]: IgnoreFlag now = %s\n",
   1132          1.1  christos         Gbl_CurrentLineNumber, Gbl_IfDepth,
   1133          1.1  christos         Gbl_IgnoringThisCodeBlock ? "I" : "E",
   1134          1.1  christos         Gbl_IfDepth * 4, " ",
   1135          1.1  christos         Gbl_DirectiveInfo[Info->Directive].Name,
   1136          1.1  christos         Info->Argument, Gbl_IgnoringThisCodeBlock ? "TRUE" : "FALSE");
   1137          1.1  christos 
   1138          1.1  christos     ACPI_FREE (Info);
   1139          1.1  christos     return (AE_OK);
   1140          1.1  christos }
   1141          1.1  christos 
   1142          1.1  christos 
   1143          1.1  christos /*******************************************************************************
   1144          1.1  christos  *
   1145          1.1  christos  * FUNCTION:    PrDbgPrint
   1146          1.1  christos  *
   1147          1.1  christos  * PARAMETERS:  Action              - Action being performed
   1148          1.1  christos  *              DirectiveName       - Directive being processed
   1149          1.1  christos  *
   1150          1.1  christos  * RETURN:      None
   1151          1.1  christos  *
   1152          1.1  christos  * DESCRIPTION: Special debug print for directive processing.
   1153          1.1  christos  *
   1154          1.1  christos  ******************************************************************************/
   1155          1.1  christos 
   1156          1.1  christos static void
   1157          1.1  christos PrDbgPrint (
   1158          1.1  christos     char                    *Action,
   1159          1.1  christos     char                    *DirectiveName)
   1160          1.1  christos {
   1161          1.1  christos 
   1162          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, "Pr(%.4u) - [%u %s] "
   1163  1.1.1.2.2.2     skrll         "%*s %s #%s, IfDepth %u\n",
   1164          1.1  christos         Gbl_CurrentLineNumber, Gbl_IfDepth,
   1165          1.1  christos         Gbl_IgnoringThisCodeBlock ? "I" : "E",
   1166          1.1  christos         Gbl_IfDepth * 4, " ",
   1167          1.1  christos         Action, DirectiveName, Gbl_IfDepth);
   1168          1.1  christos }
   1169  1.1.1.2.2.2     skrll 
   1170  1.1.1.2.2.2     skrll 
   1171  1.1.1.2.2.2     skrll /*******************************************************************************
   1172  1.1.1.2.2.2     skrll  *
   1173  1.1.1.2.2.2     skrll  * FUNCTION:    PrDoIncludeFile
   1174  1.1.1.2.2.2     skrll  *
   1175  1.1.1.2.2.2     skrll  * PARAMETERS:  Pathname                - Name of the input file
   1176  1.1.1.2.2.2     skrll  *
   1177  1.1.1.2.2.2     skrll  * RETURN:      None.
   1178  1.1.1.2.2.2     skrll  *
   1179  1.1.1.2.2.2     skrll  * DESCRIPTION: Open an include file, from #include.
   1180  1.1.1.2.2.2     skrll  *
   1181  1.1.1.2.2.2     skrll  ******************************************************************************/
   1182  1.1.1.2.2.2     skrll 
   1183  1.1.1.2.2.2     skrll static void
   1184  1.1.1.2.2.2     skrll PrDoIncludeFile (
   1185  1.1.1.2.2.2     skrll     char                    *Pathname)
   1186  1.1.1.2.2.2     skrll {
   1187  1.1.1.2.2.2     skrll     char                    *FullPathname;
   1188  1.1.1.2.2.2     skrll 
   1189  1.1.1.2.2.2     skrll 
   1190  1.1.1.2.2.2     skrll     (void) PrOpenIncludeFile (Pathname, "r", &FullPathname);
   1191  1.1.1.2.2.2     skrll }
   1192  1.1.1.2.2.2     skrll 
   1193  1.1.1.2.2.2     skrll 
   1194  1.1.1.2.2.2     skrll /*******************************************************************************
   1195  1.1.1.2.2.2     skrll  *
   1196  1.1.1.2.2.2     skrll  * FUNCTION:    PrDoIncludeBuffer
   1197  1.1.1.2.2.2     skrll  *
   1198  1.1.1.2.2.2     skrll  * PARAMETERS:  Pathname                - Name of the input binary file
   1199  1.1.1.2.2.2     skrll  *              BufferName              - ACPI namepath of the buffer
   1200  1.1.1.2.2.2     skrll  *
   1201  1.1.1.2.2.2     skrll  * RETURN:      None.
   1202  1.1.1.2.2.2     skrll  *
   1203  1.1.1.2.2.2     skrll  * DESCRIPTION: Create an ACPI buffer object from a binary file. The contents
   1204  1.1.1.2.2.2     skrll  *              of the file are emitted into the buffer object as ascii
   1205  1.1.1.2.2.2     skrll  *              hex data. From #includebuffer.
   1206  1.1.1.2.2.2     skrll  *
   1207  1.1.1.2.2.2     skrll  ******************************************************************************/
   1208  1.1.1.2.2.2     skrll 
   1209  1.1.1.2.2.2     skrll static void
   1210  1.1.1.2.2.2     skrll PrDoIncludeBuffer (
   1211  1.1.1.2.2.2     skrll     char                    *Pathname,
   1212  1.1.1.2.2.2     skrll     char                    *BufferName)
   1213  1.1.1.2.2.2     skrll {
   1214  1.1.1.2.2.2     skrll     char                    *FullPathname;
   1215  1.1.1.2.2.2     skrll     FILE                    *BinaryBufferFile;
   1216  1.1.1.2.2.2     skrll     UINT32                  i = 0;
   1217  1.1.1.2.2.2     skrll     UINT8                   c;
   1218  1.1.1.2.2.2     skrll 
   1219  1.1.1.2.2.2     skrll 
   1220  1.1.1.2.2.2     skrll     BinaryBufferFile = PrOpenIncludeFile (Pathname, "rb", &FullPathname);
   1221  1.1.1.2.2.2     skrll     if (!BinaryBufferFile)
   1222  1.1.1.2.2.2     skrll     {
   1223  1.1.1.2.2.2     skrll         return;
   1224  1.1.1.2.2.2     skrll     }
   1225  1.1.1.2.2.2     skrll 
   1226  1.1.1.2.2.2     skrll     /* Emit "Name (XXXX, Buffer() {" header */
   1227  1.1.1.2.2.2     skrll 
   1228  1.1.1.2.2.2     skrll     FlPrintFile (ASL_FILE_PREPROCESSOR, "Name (%s, Buffer()\n{", BufferName);
   1229  1.1.1.2.2.2     skrll 
   1230  1.1.1.2.2.2     skrll     /* Dump the entire file in ascii hex format */
   1231  1.1.1.2.2.2     skrll 
   1232  1.1.1.2.2.2     skrll     while (fread (&c, 1, 1, BinaryBufferFile))
   1233  1.1.1.2.2.2     skrll     {
   1234  1.1.1.2.2.2     skrll         if (!(i % 8))
   1235  1.1.1.2.2.2     skrll         {
   1236  1.1.1.2.2.2     skrll             FlPrintFile (ASL_FILE_PREPROCESSOR, "\n   ", c);
   1237  1.1.1.2.2.2     skrll         }
   1238  1.1.1.2.2.2     skrll 
   1239  1.1.1.2.2.2     skrll         FlPrintFile (ASL_FILE_PREPROCESSOR, " 0x%2.2X,", c);
   1240  1.1.1.2.2.2     skrll         i++;
   1241  1.1.1.2.2.2     skrll     }
   1242  1.1.1.2.2.2     skrll 
   1243  1.1.1.2.2.2     skrll     DbgPrint (ASL_PARSE_OUTPUT, PR_PREFIX_ID
   1244  1.1.1.2.2.2     skrll         "#includebuffer: read %u bytes from %s\n",
   1245  1.1.1.2.2.2     skrll         Gbl_CurrentLineNumber, i, FullPathname);
   1246  1.1.1.2.2.2     skrll 
   1247  1.1.1.2.2.2     skrll     /* Close the Name() operator */
   1248  1.1.1.2.2.2     skrll 
   1249  1.1.1.2.2.2     skrll     FlPrintFile (ASL_FILE_PREPROCESSOR, "\n})\n", BufferName);
   1250  1.1.1.2.2.2     skrll     fclose (BinaryBufferFile);
   1251  1.1.1.2.2.2     skrll }
   1252