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