Home | History | Annotate | Line # | Download | only in compiler
prmacros.c revision 1.1.1.8.2.1
      1          1.1  christos /******************************************************************************
      2          1.1  christos  *
      3          1.1  christos  * Module Name: prmacros - Preprocessor #define macro support
      4          1.1  christos  *
      5          1.1  christos  *****************************************************************************/
      6          1.1  christos 
      7          1.1  christos /*
      8  1.1.1.8.2.1  christos  * Copyright (C) 2000 - 2019, 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 
     46          1.1  christos #define _COMPONENT          ASL_PREPROCESSOR
     47          1.1  christos         ACPI_MODULE_NAME    ("prmacros")
     48          1.1  christos 
     49          1.1  christos 
     50          1.1  christos /*******************************************************************************
     51          1.1  christos  *
     52          1.1  christos  * FUNCTION:    PrDumpPredefinedNames
     53          1.1  christos  *
     54          1.1  christos  * PARAMETERS:  None
     55          1.1  christos  *
     56          1.1  christos  * RETURN:      None
     57          1.1  christos  *
     58          1.1  christos  * DESCRIPTION: Dump the list of #defines. Used as the preprocessor starts, to
     59          1.1  christos  *              display the names that were defined on the command line.
     60          1.1  christos  *              Debug information only.
     61          1.1  christos  *
     62          1.1  christos  ******************************************************************************/
     63          1.1  christos 
     64          1.1  christos void
     65          1.1  christos PrDumpPredefinedNames (
     66          1.1  christos     void)
     67          1.1  christos {
     68          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
     69          1.1  christos 
     70          1.1  christos 
     71  1.1.1.8.2.1  christos     DefineInfo = AslGbl_DefineList;
     72          1.1  christos     while (DefineInfo)
     73          1.1  christos     {
     74          1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
     75          1.1  christos             "Predefined #define: %s->%s\n",
     76          1.1  christos             0, DefineInfo->Identifier, DefineInfo->Replacement);
     77          1.1  christos 
     78          1.1  christos         DefineInfo = DefineInfo->Next;
     79          1.1  christos     }
     80          1.1  christos }
     81          1.1  christos 
     82          1.1  christos 
     83          1.1  christos /*******************************************************************************
     84          1.1  christos  *
     85          1.1  christos  * FUNCTION:    PrAddDefine
     86          1.1  christos  *
     87          1.1  christos  * PARAMETERS:  Identifier          - Name to be replaced
     88          1.1  christos  *              Replacement         - Replacement for Identifier
     89          1.1  christos  *              Persist             - Keep define across multiple compiles?
     90          1.1  christos  *
     91          1.1  christos  * RETURN:      A new define_info struct. NULL on error.
     92          1.1  christos  *
     93          1.1  christos  * DESCRIPTION: Add a new #define to the global list
     94          1.1  christos  *
     95          1.1  christos  ******************************************************************************/
     96          1.1  christos 
     97          1.1  christos PR_DEFINE_INFO *
     98          1.1  christos PrAddDefine (
     99          1.1  christos     char                    *Identifier,
    100          1.1  christos     char                    *Replacement,
    101          1.1  christos     BOOLEAN                 Persist)
    102          1.1  christos {
    103          1.1  christos     char                    *IdentifierString;
    104          1.1  christos     char                    *ReplacementString;
    105          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    106          1.1  christos 
    107          1.1  christos 
    108          1.1  christos     if (!Replacement)
    109          1.1  christos     {
    110          1.1  christos         Replacement = "";
    111          1.1  christos     }
    112          1.1  christos 
    113          1.1  christos     /* Check for already-defined first */
    114          1.1  christos 
    115          1.1  christos     DefineInfo = PrMatchDefine (Identifier);
    116          1.1  christos     if (DefineInfo)
    117          1.1  christos     {
    118          1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID,
    119          1.1  christos             "#define: name already exists: %s\n",
    120  1.1.1.8.2.1  christos             AslGbl_CurrentLineNumber, Identifier);
    121          1.1  christos 
    122          1.1  christos         /*
    123          1.1  christos          * Name already exists. This is only an error if the target name
    124          1.1  christos          * is different.
    125          1.1  christos          */
    126          1.1  christos         if (strcmp (Replacement, DefineInfo->Replacement))
    127          1.1  christos         {
    128          1.1  christos             PrError (ASL_ERROR, ASL_MSG_EXISTING_NAME,
    129          1.1  christos                 THIS_TOKEN_OFFSET (Identifier));
    130          1.1  christos 
    131          1.1  christos             return (NULL);
    132          1.1  christos         }
    133          1.1  christos 
    134          1.1  christos         return (DefineInfo);
    135          1.1  christos     }
    136          1.1  christos 
    137          1.1  christos     /* Copy input strings */
    138          1.1  christos 
    139          1.1  christos     IdentifierString = UtLocalCalloc (strlen (Identifier) + 1);
    140          1.1  christos     strcpy (IdentifierString, Identifier);
    141          1.1  christos 
    142          1.1  christos     ReplacementString = UtLocalCalloc (strlen (Replacement) + 1);
    143          1.1  christos     strcpy (ReplacementString, Replacement);
    144          1.1  christos 
    145          1.1  christos     /* Init and link new define info struct */
    146          1.1  christos 
    147          1.1  christos     DefineInfo = UtLocalCalloc (sizeof (PR_DEFINE_INFO));
    148          1.1  christos     DefineInfo->Replacement = ReplacementString;
    149          1.1  christos     DefineInfo->Identifier = IdentifierString;
    150          1.1  christos     DefineInfo->Persist = Persist;
    151          1.1  christos 
    152  1.1.1.8.2.1  christos     if (AslGbl_DefineList)
    153          1.1  christos     {
    154  1.1.1.8.2.1  christos         AslGbl_DefineList->Previous = DefineInfo;
    155          1.1  christos     }
    156          1.1  christos 
    157  1.1.1.8.2.1  christos     DefineInfo->Next = AslGbl_DefineList;
    158  1.1.1.8.2.1  christos     AslGbl_DefineList = DefineInfo;
    159          1.1  christos     return (DefineInfo);
    160          1.1  christos }
    161          1.1  christos 
    162          1.1  christos 
    163          1.1  christos /*******************************************************************************
    164          1.1  christos  *
    165          1.1  christos  * FUNCTION:    PrRemoveDefine
    166          1.1  christos  *
    167          1.1  christos  * PARAMETERS:  DefineName          - Name of define to be removed
    168          1.1  christos  *
    169          1.1  christos  * RETURN:      None
    170          1.1  christos  *
    171          1.1  christos  * DESCRIPTION: Implements #undef. Remove a #define if found in the global
    172          1.1  christos  *              list. No error if the target of the #undef does not exist,
    173          1.1  christos  *              as per the C #undef definition.
    174          1.1  christos  *
    175          1.1  christos  ******************************************************************************/
    176          1.1  christos 
    177          1.1  christos void
    178          1.1  christos PrRemoveDefine (
    179          1.1  christos     char                    *DefineName)
    180          1.1  christos {
    181          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    182          1.1  christos 
    183          1.1  christos 
    184          1.1  christos     /* Match name and delete the node */
    185          1.1  christos 
    186  1.1.1.8.2.1  christos     DefineInfo = AslGbl_DefineList;
    187          1.1  christos     while (DefineInfo)
    188          1.1  christos     {
    189          1.1  christos         if (!strcmp (DefineName, DefineInfo->Identifier))
    190          1.1  christos         {
    191          1.1  christos             /* Remove from linked list */
    192          1.1  christos 
    193          1.1  christos             if (DefineInfo->Previous)
    194          1.1  christos             {
    195          1.1  christos                 (DefineInfo->Previous)->Next = DefineInfo->Next;
    196          1.1  christos             }
    197          1.1  christos             else
    198          1.1  christos             {
    199  1.1.1.8.2.1  christos                 AslGbl_DefineList = DefineInfo->Next;
    200          1.1  christos             }
    201          1.1  christos 
    202          1.1  christos             if (DefineInfo->Next)
    203          1.1  christos             {
    204          1.1  christos                 (DefineInfo->Next)->Previous = DefineInfo->Previous;
    205          1.1  christos             }
    206          1.1  christos 
    207          1.1  christos             free (DefineInfo);
    208          1.1  christos             return;
    209          1.1  christos         }
    210          1.1  christos 
    211          1.1  christos         DefineInfo = DefineInfo->Next;
    212          1.1  christos     }
    213          1.1  christos 
    214          1.1  christos     /*
    215          1.1  christos      * Name was not found. By definition of #undef, this is not
    216          1.1  christos      * an error, however.
    217          1.1  christos      */
    218          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    219          1.1  christos         "#undef: could not find %s\n",
    220  1.1.1.8.2.1  christos         AslGbl_CurrentLineNumber, DefineName);
    221          1.1  christos }
    222          1.1  christos 
    223          1.1  christos 
    224          1.1  christos /*******************************************************************************
    225          1.1  christos  *
    226          1.1  christos  * FUNCTION:    PrMatchDefine
    227          1.1  christos  *
    228          1.1  christos  * PARAMETERS:  MatchString         - Name associated with the #define
    229          1.1  christos  *
    230          1.1  christos  * RETURN:      Matched string if found. NULL otherwise.
    231          1.1  christos  *
    232          1.1  christos  * DESCRIPTION: Find a name in global #define list
    233          1.1  christos  *
    234          1.1  christos  ******************************************************************************/
    235          1.1  christos 
    236          1.1  christos PR_DEFINE_INFO *
    237          1.1  christos PrMatchDefine (
    238          1.1  christos     char                    *MatchString)
    239          1.1  christos {
    240          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    241          1.1  christos 
    242          1.1  christos 
    243  1.1.1.8.2.1  christos     DefineInfo = AslGbl_DefineList;
    244          1.1  christos     while (DefineInfo)
    245          1.1  christos     {
    246          1.1  christos         if (!strcmp (MatchString, DefineInfo->Identifier))
    247          1.1  christos         {
    248          1.1  christos             return (DefineInfo);
    249          1.1  christos         }
    250          1.1  christos 
    251          1.1  christos         DefineInfo = DefineInfo->Next;
    252          1.1  christos     }
    253          1.1  christos 
    254          1.1  christos     return (NULL);
    255          1.1  christos }
    256          1.1  christos 
    257          1.1  christos 
    258          1.1  christos /*******************************************************************************
    259          1.1  christos  *
    260          1.1  christos  * FUNCTION:    PrAddMacro
    261          1.1  christos  *
    262          1.1  christos  * PARAMETERS:  Name                - Start of the macro definition
    263          1.1  christos  *              Next                - "Next" buffer from GetNextToken
    264          1.1  christos  *
    265          1.1  christos  * RETURN:      None
    266          1.1  christos  *
    267          1.1  christos  * DESCRIPTION: Add a new macro to the list of #defines. Handles argument
    268          1.1  christos  *              processing.
    269          1.1  christos  *
    270          1.1  christos  ******************************************************************************/
    271          1.1  christos 
    272          1.1  christos void
    273          1.1  christos PrAddMacro (
    274          1.1  christos     char                    *Name,
    275          1.1  christos     char                    **Next)
    276          1.1  christos {
    277          1.1  christos     char                    *Token = NULL;
    278          1.1  christos     ACPI_SIZE               TokenOffset;
    279          1.1  christos     ACPI_SIZE               MacroBodyOffset;
    280          1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    281          1.1  christos     PR_MACRO_ARG            *Args;
    282          1.1  christos     char                    *Body;
    283          1.1  christos     char                    *BodyInSource;
    284          1.1  christos     UINT32                  i;
    285          1.1  christos     UINT16                  UseCount = 0;
    286          1.1  christos     UINT16                  ArgCount = 0;
    287          1.1  christos     UINT32                  Depth = 1;
    288          1.1  christos     UINT32                  EndOfArgList;
    289          1.1  christos     char                    BufferChar;
    290          1.1  christos 
    291          1.1  christos 
    292          1.1  christos     /* Find the end of the arguments list */
    293          1.1  christos 
    294  1.1.1.8.2.1  christos     TokenOffset = Name - AslGbl_MainTokenBuffer + strlen (Name) + 1;
    295          1.1  christos     while (1)
    296          1.1  christos     {
    297  1.1.1.8.2.1  christos         BufferChar = AslGbl_CurrentLineBuffer[TokenOffset];
    298          1.1  christos         if (BufferChar == '(')
    299          1.1  christos         {
    300          1.1  christos             Depth++;
    301          1.1  christos         }
    302          1.1  christos         else if (BufferChar == ')')
    303          1.1  christos         {
    304          1.1  christos             Depth--;
    305          1.1  christos         }
    306          1.1  christos         else if (BufferChar == 0)
    307          1.1  christos         {
    308          1.1  christos             PrError (ASL_ERROR, ASL_MSG_MACRO_SYNTAX, TokenOffset);
    309          1.1  christos             return;
    310          1.1  christos         }
    311          1.1  christos 
    312          1.1  christos         if (Depth == 0)
    313          1.1  christos         {
    314          1.1  christos             /* Found arg list end */
    315          1.1  christos 
    316          1.1  christos             EndOfArgList = TokenOffset;
    317          1.1  christos             break;
    318          1.1  christos         }
    319          1.1  christos 
    320          1.1  christos         TokenOffset++;
    321          1.1  christos     }
    322          1.1  christos 
    323          1.1  christos     /* At this point, we know that we have a reasonable argument list */
    324          1.1  christos 
    325          1.1  christos     Args = UtLocalCalloc (sizeof (PR_MACRO_ARG) * PR_MAX_MACRO_ARGS);
    326          1.1  christos 
    327          1.1  christos     /* Get the macro argument names */
    328          1.1  christos 
    329          1.1  christos     for (i = 0; i < PR_MAX_MACRO_ARGS; i++)
    330          1.1  christos     {
    331          1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    332          1.1  christos         if (!Token)
    333          1.1  christos         {
    334          1.1  christos             /* This is the case for a NULL macro body */
    335          1.1  christos 
    336          1.1  christos             BodyInSource = "";
    337          1.1  christos             goto AddMacroToList;
    338          1.1  christos         }
    339          1.1  christos 
    340          1.1  christos         /* Don't go beyond the argument list */
    341          1.1  christos 
    342  1.1.1.8.2.1  christos         TokenOffset = Token - AslGbl_MainTokenBuffer + strlen (Token);
    343          1.1  christos         if (TokenOffset > EndOfArgList)
    344          1.1  christos         {
    345          1.1  christos             break;
    346          1.1  christos         }
    347          1.1  christos 
    348          1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    349          1.1  christos             "Macro arg: %s \n",
    350  1.1.1.8.2.1  christos             AslGbl_CurrentLineNumber, Token);
    351          1.1  christos 
    352          1.1  christos         Args[i].Name = UtLocalCalloc (strlen (Token) + 1);
    353          1.1  christos         strcpy (Args[i].Name, Token);
    354          1.1  christos 
    355          1.1  christos         Args[i].UseCount = 0;
    356          1.1  christos 
    357          1.1  christos         ArgCount++;
    358          1.1  christos         if (ArgCount >= PR_MAX_MACRO_ARGS)
    359          1.1  christos         {
    360          1.1  christos             PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS, TokenOffset);
    361      1.1.1.2  christos             goto ErrorExit;
    362          1.1  christos         }
    363          1.1  christos     }
    364          1.1  christos 
    365          1.1  christos     /* Get the macro body. Token now points to start of body */
    366          1.1  christos 
    367  1.1.1.8.2.1  christos     MacroBodyOffset = Token - AslGbl_MainTokenBuffer;
    368          1.1  christos 
    369          1.1  christos     /* Match each method arg in the macro body for later use */
    370          1.1  christos 
    371          1.1  christos     Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    372          1.1  christos     while (Token)
    373          1.1  christos     {
    374          1.1  christos         /* Search the macro arg list for matching arg */
    375          1.1  christos 
    376      1.1.1.6  christos         for (i = 0; ((i < PR_MAX_MACRO_ARGS) && Args[i].Name); i++)
    377          1.1  christos         {
    378          1.1  christos             /*
    379          1.1  christos              * Save argument offset within macro body. This is the mechanism
    380          1.1  christos              * used to expand the macro upon invocation.
    381          1.1  christos              *
    382          1.1  christos              * Handles multiple instances of the same argument
    383          1.1  christos              */
    384          1.1  christos             if (!strcmp (Token, Args[i].Name))
    385          1.1  christos             {
    386          1.1  christos                 UseCount = Args[i].UseCount;
    387          1.1  christos 
    388      1.1.1.4  christos                 Args[i].Offset[UseCount] =
    389  1.1.1.8.2.1  christos                     (Token - AslGbl_MainTokenBuffer) - MacroBodyOffset;
    390          1.1  christos 
    391          1.1  christos                 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    392          1.1  christos                     "Macro Arg #%u: %s UseCount %u Offset %u \n",
    393  1.1.1.8.2.1  christos                     AslGbl_CurrentLineNumber, i, Token,
    394          1.1  christos                     UseCount+1, Args[i].Offset[UseCount]);
    395          1.1  christos 
    396          1.1  christos                 Args[i].UseCount++;
    397          1.1  christos                 if (Args[i].UseCount >= PR_MAX_ARG_INSTANCES)
    398          1.1  christos                 {
    399          1.1  christos                     PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS,
    400          1.1  christos                         THIS_TOKEN_OFFSET (Token));
    401          1.1  christos 
    402      1.1.1.2  christos                     goto ErrorExit;
    403          1.1  christos                 }
    404          1.1  christos                 break;
    405          1.1  christos             }
    406          1.1  christos         }
    407          1.1  christos 
    408          1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    409          1.1  christos     }
    410          1.1  christos 
    411  1.1.1.8.2.1  christos     BodyInSource = &AslGbl_CurrentLineBuffer[MacroBodyOffset];
    412          1.1  christos 
    413          1.1  christos 
    414          1.1  christos AddMacroToList:
    415          1.1  christos 
    416          1.1  christos     /* Check if name is already defined first */
    417          1.1  christos 
    418          1.1  christos     DefineInfo = PrMatchDefine (Name);
    419          1.1  christos     if (DefineInfo)
    420          1.1  christos     {
    421          1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    422          1.1  christos             "#define: macro name already exists: %s\n",
    423  1.1.1.8.2.1  christos             AslGbl_CurrentLineNumber, Name);
    424          1.1  christos 
    425          1.1  christos         /* Error only if not exactly the same macro */
    426          1.1  christos 
    427          1.1  christos         if (strcmp (DefineInfo->Body, BodyInSource) ||
    428          1.1  christos             (DefineInfo->ArgCount != ArgCount))
    429          1.1  christos         {
    430          1.1  christos             PrError (ASL_ERROR, ASL_MSG_EXISTING_NAME,
    431          1.1  christos                 THIS_TOKEN_OFFSET (Name));
    432          1.1  christos         }
    433          1.1  christos 
    434      1.1.1.2  christos         goto ErrorExit;
    435          1.1  christos     }
    436          1.1  christos 
    437          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    438          1.1  christos         "Macro body: %s \n",
    439  1.1.1.8.2.1  christos         AslGbl_CurrentLineNumber, BodyInSource);
    440          1.1  christos 
    441          1.1  christos     /* Add macro to the #define list */
    442          1.1  christos 
    443          1.1  christos     DefineInfo = PrAddDefine (Name, BodyInSource, FALSE);
    444          1.1  christos     if (DefineInfo)
    445          1.1  christos     {
    446          1.1  christos         Body = UtLocalCalloc (strlen (BodyInSource) + 1);
    447          1.1  christos         strcpy (Body, BodyInSource);
    448          1.1  christos 
    449          1.1  christos         DefineInfo->Body = Body;
    450          1.1  christos         DefineInfo->Args = Args;
    451          1.1  christos         DefineInfo->ArgCount = ArgCount;
    452          1.1  christos     }
    453      1.1.1.2  christos 
    454      1.1.1.2  christos     return;
    455      1.1.1.2  christos 
    456      1.1.1.2  christos 
    457      1.1.1.2  christos ErrorExit:
    458      1.1.1.2  christos     ACPI_FREE (Args);
    459      1.1.1.2  christos     return;
    460          1.1  christos }
    461          1.1  christos 
    462          1.1  christos 
    463          1.1  christos /*******************************************************************************
    464          1.1  christos  *
    465          1.1  christos  * FUNCTION:    PrDoMacroInvocation
    466          1.1  christos  *
    467          1.1  christos  * PARAMETERS:  TokenBuffer         - Current line buffer
    468          1.1  christos  *              MacroStart          - Start of the macro invocation within
    469          1.1  christos  *                                    the token buffer
    470          1.1  christos  *              DefineInfo          - Info for this macro
    471          1.1  christos  *              Next                - "Next" buffer from GetNextToken
    472          1.1  christos  *
    473          1.1  christos  * RETURN:      None
    474          1.1  christos  *
    475          1.1  christos  * DESCRIPTION: Expand a macro invocation
    476          1.1  christos  *
    477          1.1  christos  ******************************************************************************/
    478          1.1  christos 
    479          1.1  christos void
    480          1.1  christos PrDoMacroInvocation (
    481          1.1  christos     char                    *TokenBuffer,
    482          1.1  christos     char                    *MacroStart,
    483          1.1  christos     PR_DEFINE_INFO          *DefineInfo,
    484          1.1  christos     char                    **Next)
    485          1.1  christos {
    486          1.1  christos     PR_MACRO_ARG            *Args;
    487          1.1  christos     char                    *Token = NULL;
    488          1.1  christos     UINT32                  TokenOffset;
    489          1.1  christos     UINT32                  Length;
    490          1.1  christos     UINT32                  i;
    491          1.1  christos 
    492          1.1  christos 
    493          1.1  christos     /* Take a copy of the macro body for expansion */
    494          1.1  christos 
    495  1.1.1.8.2.1  christos     strcpy (AslGbl_MacroTokenBuffer, DefineInfo->Body);
    496          1.1  christos 
    497          1.1  christos     /* Replace each argument within the prototype body */
    498          1.1  christos 
    499          1.1  christos     Args = DefineInfo->Args;
    500          1.1  christos     if (!Args->Name)
    501          1.1  christos     {
    502          1.1  christos         /* This macro has no arguments */
    503          1.1  christos 
    504          1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_ARGUMENTS, Next);
    505          1.1  christos         if (!Token)
    506          1.1  christos         {
    507          1.1  christos             goto BadInvocation;
    508          1.1  christos         }
    509          1.1  christos 
    510          1.1  christos         TokenOffset = (MacroStart - TokenBuffer);
    511          1.1  christos         Length = Token - MacroStart + strlen (Token) + 1;
    512          1.1  christos 
    513          1.1  christos         PrReplaceData (
    514  1.1.1.8.2.1  christos             &AslGbl_CurrentLineBuffer[TokenOffset], Length,
    515  1.1.1.8.2.1  christos             AslGbl_MacroTokenBuffer, strlen (AslGbl_MacroTokenBuffer));
    516          1.1  christos         return;
    517          1.1  christos     }
    518          1.1  christos 
    519          1.1  christos     while (Args->Name)
    520          1.1  christos     {
    521          1.1  christos         /* Get the next argument from macro invocation */
    522          1.1  christos 
    523          1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    524          1.1  christos         if (!Token)
    525          1.1  christos         {
    526          1.1  christos             goto BadInvocation;
    527          1.1  christos         }
    528          1.1  christos 
    529          1.1  christos         /* Replace all instances of this argument */
    530          1.1  christos 
    531          1.1  christos         for (i = 0; i < Args->UseCount; i++)
    532          1.1  christos         {
    533          1.1  christos             /* Offset zero indicates "arg not used" */
    534          1.1  christos             /* TBD: Not really needed now, with UseCount available */
    535          1.1  christos 
    536          1.1  christos             if (Args->Offset[i] == 0)
    537          1.1  christos             {
    538          1.1  christos                 break;
    539          1.1  christos             }
    540          1.1  christos 
    541          1.1  christos             PrReplaceData (
    542  1.1.1.8.2.1  christos                 &AslGbl_MacroTokenBuffer[Args->Offset[i]], strlen (Args->Name),
    543          1.1  christos                 Token, strlen (Token));
    544          1.1  christos 
    545          1.1  christos             DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    546          1.1  christos                 "ExpandArg: %s \n",
    547  1.1.1.8.2.1  christos                 AslGbl_CurrentLineNumber, AslGbl_MacroTokenBuffer);
    548          1.1  christos         }
    549          1.1  christos 
    550          1.1  christos         Args++;
    551          1.1  christos     }
    552          1.1  christos 
    553          1.1  christos     /* TBD: need to make sure macro was not invoked with too many arguments */
    554          1.1  christos 
    555          1.1  christos     if (!Token)
    556          1.1  christos     {
    557          1.1  christos         return;
    558          1.1  christos     }
    559          1.1  christos 
    560          1.1  christos     /* Replace the entire macro invocation with the expanded macro */
    561          1.1  christos 
    562          1.1  christos     TokenOffset = (MacroStart - TokenBuffer);
    563          1.1  christos     Length = Token - MacroStart + strlen (Token) + 1;
    564          1.1  christos 
    565          1.1  christos     PrReplaceData (
    566  1.1.1.8.2.1  christos         &AslGbl_CurrentLineBuffer[TokenOffset], Length,
    567  1.1.1.8.2.1  christos         AslGbl_MacroTokenBuffer, strlen (AslGbl_MacroTokenBuffer));
    568          1.1  christos 
    569          1.1  christos     return;
    570          1.1  christos 
    571          1.1  christos 
    572          1.1  christos BadInvocation:
    573          1.1  christos     PrError (ASL_ERROR, ASL_MSG_INVALID_INVOCATION,
    574          1.1  christos         THIS_TOKEN_OFFSET (MacroStart));
    575          1.1  christos 
    576          1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    577          1.1  christos         "Bad macro invocation: %s \n",
    578  1.1.1.8.2.1  christos         AslGbl_CurrentLineNumber, AslGbl_MacroTokenBuffer);
    579          1.1  christos     return;
    580          1.1  christos }
    581