Home | History | Annotate | Line # | Download | only in compiler
prmacros.c revision 1.1.1.16
      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.15  christos  * Copyright (C) 2000 - 2023, 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.1.13  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 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.9  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.1.11  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    119       1.1  christos             "#define: name already exists: %s\n",
    120   1.1.1.9  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.9  christos     if (AslGbl_DefineList)
    153       1.1  christos     {
    154   1.1.1.9  christos         AslGbl_DefineList->Previous = DefineInfo;
    155       1.1  christos     }
    156       1.1  christos 
    157   1.1.1.9  christos     DefineInfo->Next = AslGbl_DefineList;
    158   1.1.1.9  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.9  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.9  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.9  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.9  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.1.15  christos     /*UINT32                  Depth = 1;*/
    289       1.1  christos     UINT32                  EndOfArgList;
    290       1.1  christos     char                    BufferChar;
    291       1.1  christos 
    292       1.1  christos     /* Find the end of the arguments list */
    293       1.1  christos 
    294   1.1.1.9  christos     TokenOffset = Name - AslGbl_MainTokenBuffer + strlen (Name) + 1;
    295       1.1  christos     while (1)
    296       1.1  christos     {
    297   1.1.1.9  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.1.15  christos 
    333       1.1  christos         if (!Token)
    334       1.1  christos         {
    335       1.1  christos             /* This is the case for a NULL macro body */
    336       1.1  christos 
    337       1.1  christos             BodyInSource = "";
    338       1.1  christos             goto AddMacroToList;
    339       1.1  christos         }
    340       1.1  christos 
    341       1.1  christos         /* Don't go beyond the argument list */
    342       1.1  christos 
    343   1.1.1.9  christos         TokenOffset = Token - AslGbl_MainTokenBuffer + strlen (Token);
    344       1.1  christos         if (TokenOffset > EndOfArgList)
    345       1.1  christos         {
    346       1.1  christos             break;
    347       1.1  christos         }
    348       1.1  christos 
    349       1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    350  1.1.1.16  christos             "Macro param: %s\n",
    351   1.1.1.9  christos             AslGbl_CurrentLineNumber, Token);
    352       1.1  christos 
    353       1.1  christos         Args[i].Name = UtLocalCalloc (strlen (Token) + 1);
    354       1.1  christos         strcpy (Args[i].Name, Token);
    355       1.1  christos 
    356       1.1  christos         Args[i].UseCount = 0;
    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.9  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     while (Token)
    372       1.1  christos     {
    373       1.1  christos         /* Search the macro arg list for matching arg */
    374       1.1  christos 
    375   1.1.1.6  christos         for (i = 0; ((i < PR_MAX_MACRO_ARGS) && Args[i].Name); i++)
    376       1.1  christos         {
    377       1.1  christos             /*
    378       1.1  christos              * Save argument offset within macro body. This is the mechanism
    379       1.1  christos              * used to expand the macro upon invocation.
    380       1.1  christos              *
    381       1.1  christos              * Handles multiple instances of the same argument
    382       1.1  christos              */
    383       1.1  christos             if (!strcmp (Token, Args[i].Name))
    384       1.1  christos             {
    385       1.1  christos                 UseCount = Args[i].UseCount;
    386       1.1  christos 
    387   1.1.1.4  christos                 Args[i].Offset[UseCount] =
    388   1.1.1.9  christos                     (Token - AslGbl_MainTokenBuffer) - MacroBodyOffset;
    389       1.1  christos 
    390  1.1.1.15  christos 
    391       1.1  christos                 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    392  1.1.1.16  christos                     "Macro Arg #%u: %s UseCount %u Offset %u\n",
    393   1.1.1.9  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.1.15  christos 
    398       1.1  christos                 if (Args[i].UseCount >= PR_MAX_ARG_INSTANCES)
    399       1.1  christos                 {
    400       1.1  christos                     PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS,
    401       1.1  christos                         THIS_TOKEN_OFFSET (Token));
    402       1.1  christos 
    403   1.1.1.2  christos                     goto ErrorExit;
    404       1.1  christos                 }
    405       1.1  christos                 break;
    406       1.1  christos             }
    407       1.1  christos         }
    408       1.1  christos 
    409       1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    410       1.1  christos     }
    411       1.1  christos 
    412   1.1.1.9  christos     BodyInSource = &AslGbl_CurrentLineBuffer[MacroBodyOffset];
    413       1.1  christos 
    414       1.1  christos 
    415       1.1  christos AddMacroToList:
    416       1.1  christos 
    417       1.1  christos     /* Check if name is already defined first */
    418       1.1  christos 
    419       1.1  christos     DefineInfo = PrMatchDefine (Name);
    420       1.1  christos     if (DefineInfo)
    421       1.1  christos     {
    422       1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    423       1.1  christos             "#define: macro name already exists: %s\n",
    424   1.1.1.9  christos             AslGbl_CurrentLineNumber, Name);
    425       1.1  christos 
    426       1.1  christos         /* Error only if not exactly the same macro */
    427       1.1  christos 
    428       1.1  christos         if (strcmp (DefineInfo->Body, BodyInSource) ||
    429       1.1  christos             (DefineInfo->ArgCount != ArgCount))
    430       1.1  christos         {
    431       1.1  christos             PrError (ASL_ERROR, ASL_MSG_EXISTING_NAME,
    432       1.1  christos                 THIS_TOKEN_OFFSET (Name));
    433       1.1  christos         }
    434       1.1  christos 
    435   1.1.1.2  christos         goto ErrorExit;
    436       1.1  christos     }
    437       1.1  christos 
    438       1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    439  1.1.1.16  christos         "Macro body: %s\n",
    440   1.1.1.9  christos         AslGbl_CurrentLineNumber, BodyInSource);
    441       1.1  christos 
    442       1.1  christos     /* Add macro to the #define list */
    443       1.1  christos 
    444       1.1  christos     DefineInfo = PrAddDefine (Name, BodyInSource, FALSE);
    445       1.1  christos     if (DefineInfo)
    446       1.1  christos     {
    447       1.1  christos         Body = UtLocalCalloc (strlen (BodyInSource) + 1);
    448       1.1  christos         strcpy (Body, BodyInSource);
    449       1.1  christos 
    450       1.1  christos         DefineInfo->Body = Body;
    451       1.1  christos         DefineInfo->Args = Args;
    452       1.1  christos         DefineInfo->ArgCount = ArgCount;
    453       1.1  christos     }
    454   1.1.1.2  christos 
    455   1.1.1.2  christos     return;
    456   1.1.1.2  christos 
    457   1.1.1.2  christos 
    458   1.1.1.2  christos ErrorExit:
    459   1.1.1.2  christos     ACPI_FREE (Args);
    460   1.1.1.2  christos     return;
    461       1.1  christos }
    462       1.1  christos 
    463       1.1  christos 
    464       1.1  christos /*******************************************************************************
    465       1.1  christos  *
    466       1.1  christos  * FUNCTION:    PrDoMacroInvocation
    467       1.1  christos  *
    468       1.1  christos  * PARAMETERS:  TokenBuffer         - Current line buffer
    469       1.1  christos  *              MacroStart          - Start of the macro invocation within
    470       1.1  christos  *                                    the token buffer
    471       1.1  christos  *              DefineInfo          - Info for this macro
    472       1.1  christos  *              Next                - "Next" buffer from GetNextToken
    473       1.1  christos  *
    474       1.1  christos  * RETURN:      None
    475       1.1  christos  *
    476       1.1  christos  * DESCRIPTION: Expand a macro invocation
    477       1.1  christos  *
    478       1.1  christos  ******************************************************************************/
    479       1.1  christos 
    480       1.1  christos void
    481       1.1  christos PrDoMacroInvocation (
    482       1.1  christos     char                    *TokenBuffer,
    483       1.1  christos     char                    *MacroStart,
    484       1.1  christos     PR_DEFINE_INFO          *DefineInfo,
    485       1.1  christos     char                    **Next)
    486       1.1  christos {
    487       1.1  christos     PR_MACRO_ARG            *Args;
    488       1.1  christos     char                    *Token = NULL;
    489       1.1  christos     UINT32                  TokenOffset;
    490       1.1  christos     UINT32                  Length;
    491       1.1  christos     UINT32                  i;
    492  1.1.1.15  christos     UINT32                  Diff1;
    493  1.1.1.15  christos     UINT32                  Diff2;
    494       1.1  christos 
    495       1.1  christos     /* Take a copy of the macro body for expansion */
    496       1.1  christos 
    497   1.1.1.9  christos     strcpy (AslGbl_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.1.15  christos 
    508       1.1  christos         if (!Token)
    509       1.1  christos         {
    510       1.1  christos             goto BadInvocation;
    511       1.1  christos         }
    512       1.1  christos 
    513       1.1  christos         TokenOffset = (MacroStart - TokenBuffer);
    514       1.1  christos         Length = Token - MacroStart + strlen (Token) + 1;
    515       1.1  christos 
    516       1.1  christos         PrReplaceData (
    517   1.1.1.9  christos             &AslGbl_CurrentLineBuffer[TokenOffset], Length,
    518   1.1.1.9  christos             AslGbl_MacroTokenBuffer, strlen (AslGbl_MacroTokenBuffer));
    519       1.1  christos         return;
    520       1.1  christos     }
    521       1.1  christos 
    522       1.1  christos     while (Args->Name)
    523       1.1  christos     {
    524       1.1  christos         /* Get the next argument from macro invocation */
    525       1.1  christos 
    526       1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    527       1.1  christos         if (!Token)
    528       1.1  christos         {
    529       1.1  christos             goto BadInvocation;
    530       1.1  christos         }
    531       1.1  christos 
    532  1.1.1.15  christos         /*
    533  1.1.1.15  christos          * Avoid optimizing using just 1 signed int due to specific
    534  1.1.1.15  christos          * non-portable implementations of signed ints
    535  1.1.1.15  christos          */
    536  1.1.1.15  christos         Diff1 = strlen (Args->Name) > strlen (Token) ? strlen (Args->Name) -
    537  1.1.1.15  christos             strlen (Token) : 0;
    538  1.1.1.15  christos 
    539  1.1.1.15  christos         Diff2 = strlen (Args->Name) < strlen (Token) ? strlen (Token) -
    540  1.1.1.15  christos             strlen (Args->Name) : 0;
    541  1.1.1.15  christos 
    542       1.1  christos         /* Replace all instances of this argument */
    543       1.1  christos 
    544       1.1  christos         for (i = 0; i < Args->UseCount; i++)
    545       1.1  christos         {
    546  1.1.1.15  christos             /*
    547  1.1.1.15  christos              * To test the output of the preprocessed macro function that
    548  1.1.1.15  christos              * is passed to the compiler
    549  1.1.1.15  christos              */
    550       1.1  christos 
    551  1.1.1.15  christos              /*
    552  1.1.1.15  christos               * fprintf (stderr, "Current token = %s \t Current arg_name = %s \
    553  1.1.1.15  christos               * \t strlen (Token) = %u \t strlen (Args->Name) = %u \t Offset = %u \
    554  1.1.1.15  christos               * \t UseCount = %u \t", Token, Args->Name, strlen (Token), \
    555  1.1.1.15  christos               *     strlen (Args->Name), Args->Offset[i], Args->UseCount);
    556  1.1.1.15  christos               */
    557  1.1.1.15  christos 
    558  1.1.1.15  christos             AslGbl_MacroTokenReplaceBuffer = (char *) calloc ((strlen (AslGbl_MacroTokenBuffer)), sizeof (char));
    559       1.1  christos 
    560  1.1.1.15  christos             PrReplaceResizeSubstring (Args, Diff1, Diff2, i, Token);
    561       1.1  christos 
    562       1.1  christos             DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    563  1.1.1.16  christos                 "ExpandArg: %s\n",
    564   1.1.1.9  christos                 AslGbl_CurrentLineNumber, AslGbl_MacroTokenBuffer);
    565       1.1  christos         }
    566       1.1  christos 
    567       1.1  christos         Args++;
    568       1.1  christos     }
    569       1.1  christos 
    570       1.1  christos     if (!Token)
    571       1.1  christos     {
    572       1.1  christos         return;
    573       1.1  christos     }
    574       1.1  christos 
    575       1.1  christos     /* Replace the entire macro invocation with the expanded macro */
    576       1.1  christos 
    577       1.1  christos     TokenOffset = (MacroStart - TokenBuffer);
    578       1.1  christos     Length = Token - MacroStart + strlen (Token) + 1;
    579       1.1  christos 
    580       1.1  christos     PrReplaceData (
    581   1.1.1.9  christos         &AslGbl_CurrentLineBuffer[TokenOffset], Length,
    582   1.1.1.9  christos         AslGbl_MacroTokenBuffer, strlen (AslGbl_MacroTokenBuffer));
    583       1.1  christos 
    584       1.1  christos     return;
    585       1.1  christos 
    586       1.1  christos BadInvocation:
    587       1.1  christos     PrError (ASL_ERROR, ASL_MSG_INVALID_INVOCATION,
    588       1.1  christos         THIS_TOKEN_OFFSET (MacroStart));
    589       1.1  christos 
    590       1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    591  1.1.1.16  christos         "Bad macro invocation: %s\n",
    592   1.1.1.9  christos         AslGbl_CurrentLineNumber, AslGbl_MacroTokenBuffer);
    593       1.1  christos     return;
    594       1.1  christos }
    595