Home | History | Annotate | Line # | Download | only in compiler
      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.1.17  christos /******************************************************************************
      8  1.1.1.17  christos  *
      9  1.1.1.17  christos  * 1. Copyright Notice
     10  1.1.1.17  christos  *
     11  1.1.1.18  christos  * Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.
     12       1.1  christos  * All rights reserved.
     13       1.1  christos  *
     14  1.1.1.17  christos  * 2. License
     15  1.1.1.17  christos  *
     16  1.1.1.17  christos  * 2.1. This is your license from Intel Corp. under its intellectual property
     17  1.1.1.17  christos  * rights. You may have additional license terms from the party that provided
     18  1.1.1.17  christos  * you this software, covering your right to use that party's intellectual
     19  1.1.1.17  christos  * property rights.
     20  1.1.1.17  christos  *
     21  1.1.1.17  christos  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     22  1.1.1.17  christos  * copy of the source code appearing in this file ("Covered Code") an
     23  1.1.1.17  christos  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     24  1.1.1.17  christos  * base code distributed originally by Intel ("Original Intel Code") to copy,
     25  1.1.1.17  christos  * make derivatives, distribute, use and display any portion of the Covered
     26  1.1.1.17  christos  * Code in any form, with the right to sublicense such rights; and
     27  1.1.1.17  christos  *
     28  1.1.1.17  christos  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     29  1.1.1.17  christos  * license (with the right to sublicense), under only those claims of Intel
     30  1.1.1.17  christos  * patents that are infringed by the Original Intel Code, to make, use, sell,
     31  1.1.1.17  christos  * offer to sell, and import the Covered Code and derivative works thereof
     32  1.1.1.17  christos  * solely to the minimum extent necessary to exercise the above copyright
     33  1.1.1.17  christos  * license, and in no event shall the patent license extend to any additions
     34  1.1.1.17  christos  * to or modifications of the Original Intel Code. No other license or right
     35  1.1.1.17  christos  * is granted directly or by implication, estoppel or otherwise;
     36  1.1.1.17  christos  *
     37  1.1.1.17  christos  * The above copyright and patent license is granted only if the following
     38  1.1.1.17  christos  * conditions are met:
     39  1.1.1.17  christos  *
     40  1.1.1.17  christos  * 3. Conditions
     41  1.1.1.17  christos  *
     42  1.1.1.17  christos  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     43  1.1.1.17  christos  * Redistribution of source code of any substantial portion of the Covered
     44  1.1.1.17  christos  * Code or modification with rights to further distribute source must include
     45  1.1.1.17  christos  * the above Copyright Notice, the above License, this list of Conditions,
     46  1.1.1.17  christos  * and the following Disclaimer and Export Compliance provision. In addition,
     47  1.1.1.17  christos  * Licensee must cause all Covered Code to which Licensee contributes to
     48  1.1.1.17  christos  * contain a file documenting the changes Licensee made to create that Covered
     49  1.1.1.17  christos  * Code and the date of any change. Licensee must include in that file the
     50  1.1.1.17  christos  * documentation of any changes made by any predecessor Licensee. Licensee
     51  1.1.1.17  christos  * must include a prominent statement that the modification is derived,
     52  1.1.1.17  christos  * directly or indirectly, from Original Intel Code.
     53  1.1.1.17  christos  *
     54  1.1.1.17  christos  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     55  1.1.1.17  christos  * Redistribution of source code of any substantial portion of the Covered
     56  1.1.1.17  christos  * Code or modification without rights to further distribute source must
     57  1.1.1.17  christos  * include the following Disclaimer and Export Compliance provision in the
     58  1.1.1.17  christos  * documentation and/or other materials provided with distribution. In
     59  1.1.1.17  christos  * addition, Licensee may not authorize further sublicense of source of any
     60  1.1.1.17  christos  * portion of the Covered Code, and must include terms to the effect that the
     61  1.1.1.17  christos  * license from Licensee to its licensee is limited to the intellectual
     62  1.1.1.17  christos  * property embodied in the software Licensee provides to its licensee, and
     63  1.1.1.17  christos  * not to intellectual property embodied in modifications its licensee may
     64  1.1.1.17  christos  * make.
     65  1.1.1.17  christos  *
     66  1.1.1.17  christos  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     67  1.1.1.17  christos  * substantial portion of the Covered Code or modification must reproduce the
     68  1.1.1.17  christos  * above Copyright Notice, and the following Disclaimer and Export Compliance
     69  1.1.1.17  christos  * provision in the documentation and/or other materials provided with the
     70  1.1.1.17  christos  * distribution.
     71  1.1.1.17  christos  *
     72  1.1.1.17  christos  * 3.4. Intel retains all right, title, and interest in and to the Original
     73  1.1.1.17  christos  * Intel Code.
     74  1.1.1.17  christos  *
     75  1.1.1.17  christos  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     76  1.1.1.17  christos  * Intel shall be used in advertising or otherwise to promote the sale, use or
     77  1.1.1.17  christos  * other dealings in products derived from or relating to the Covered Code
     78  1.1.1.17  christos  * without prior written authorization from Intel.
     79  1.1.1.17  christos  *
     80  1.1.1.17  christos  * 4. Disclaimer and Export Compliance
     81  1.1.1.17  christos  *
     82  1.1.1.17  christos  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     83  1.1.1.17  christos  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     84  1.1.1.17  christos  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
     85  1.1.1.17  christos  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
     86  1.1.1.17  christos  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
     87  1.1.1.17  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     88  1.1.1.17  christos  * PARTICULAR PURPOSE.
     89  1.1.1.17  christos  *
     90  1.1.1.17  christos  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     91  1.1.1.17  christos  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     92  1.1.1.17  christos  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     93  1.1.1.17  christos  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     94  1.1.1.17  christos  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     95  1.1.1.17  christos  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
     96  1.1.1.17  christos  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     97  1.1.1.17  christos  * LIMITED REMEDY.
     98  1.1.1.17  christos  *
     99  1.1.1.17  christos  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    100  1.1.1.17  christos  * software or system incorporating such software without first obtaining any
    101  1.1.1.17  christos  * required license or other approval from the U. S. Department of Commerce or
    102  1.1.1.17  christos  * any other agency or department of the United States Government. In the
    103  1.1.1.17  christos  * event Licensee exports any such software from the United States or
    104  1.1.1.17  christos  * re-exports any such software from a foreign destination, Licensee shall
    105  1.1.1.17  christos  * ensure that the distribution and export/re-export of the software is in
    106  1.1.1.17  christos  * compliance with all laws, regulations, orders, or other restrictions of the
    107  1.1.1.17  christos  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    108  1.1.1.17  christos  * any of its subsidiaries will export/re-export any technical data, process,
    109  1.1.1.17  christos  * software, or service, directly or indirectly, to any country for which the
    110  1.1.1.17  christos  * United States government or any agency thereof requires an export license,
    111  1.1.1.17  christos  * other governmental approval, or letter of assurance, without first obtaining
    112  1.1.1.17  christos  * such license, approval or letter.
    113  1.1.1.17  christos  *
    114  1.1.1.17  christos  *****************************************************************************
    115  1.1.1.17  christos  *
    116  1.1.1.17  christos  * Alternatively, you may choose to be licensed under the terms of the
    117  1.1.1.17  christos  * following license:
    118  1.1.1.17  christos  *
    119       1.1  christos  * Redistribution and use in source and binary forms, with or without
    120       1.1  christos  * modification, are permitted provided that the following conditions
    121       1.1  christos  * are met:
    122       1.1  christos  * 1. Redistributions of source code must retain the above copyright
    123       1.1  christos  *    notice, this list of conditions, and the following disclaimer,
    124       1.1  christos  *    without modification.
    125       1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
    126       1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
    127       1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
    128       1.1  christos  *    including a substantially similar Disclaimer requirement for further
    129       1.1  christos  *    binary redistribution.
    130       1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
    131       1.1  christos  *    of any contributors may be used to endorse or promote products derived
    132       1.1  christos  *    from this software without specific prior written permission.
    133       1.1  christos  *
    134       1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    135       1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    136  1.1.1.13  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    137       1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    138  1.1.1.17  christos  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    139  1.1.1.17  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    140  1.1.1.17  christos  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    141  1.1.1.17  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    142  1.1.1.17  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    143  1.1.1.17  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    144  1.1.1.17  christos  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    145  1.1.1.17  christos  *
    146  1.1.1.17  christos  * Alternatively, you may choose to be licensed under the terms of the
    147  1.1.1.17  christos  * GNU General Public License ("GPL") version 2 as published by the Free
    148  1.1.1.17  christos  * Software Foundation.
    149  1.1.1.17  christos  *
    150  1.1.1.17  christos  *****************************************************************************/
    151       1.1  christos 
    152       1.1  christos #include "aslcompiler.h"
    153       1.1  christos 
    154       1.1  christos #define _COMPONENT          ASL_PREPROCESSOR
    155       1.1  christos         ACPI_MODULE_NAME    ("prmacros")
    156       1.1  christos 
    157       1.1  christos 
    158       1.1  christos /*******************************************************************************
    159       1.1  christos  *
    160       1.1  christos  * FUNCTION:    PrDumpPredefinedNames
    161       1.1  christos  *
    162       1.1  christos  * PARAMETERS:  None
    163       1.1  christos  *
    164       1.1  christos  * RETURN:      None
    165       1.1  christos  *
    166       1.1  christos  * DESCRIPTION: Dump the list of #defines. Used as the preprocessor starts, to
    167       1.1  christos  *              display the names that were defined on the command line.
    168       1.1  christos  *              Debug information only.
    169       1.1  christos  *
    170       1.1  christos  ******************************************************************************/
    171       1.1  christos 
    172       1.1  christos void
    173       1.1  christos PrDumpPredefinedNames (
    174       1.1  christos     void)
    175       1.1  christos {
    176       1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    177       1.1  christos 
    178       1.1  christos 
    179   1.1.1.9  christos     DefineInfo = AslGbl_DefineList;
    180       1.1  christos     while (DefineInfo)
    181       1.1  christos     {
    182       1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    183       1.1  christos             "Predefined #define: %s->%s\n",
    184       1.1  christos             0, DefineInfo->Identifier, DefineInfo->Replacement);
    185       1.1  christos 
    186       1.1  christos         DefineInfo = DefineInfo->Next;
    187       1.1  christos     }
    188       1.1  christos }
    189       1.1  christos 
    190       1.1  christos 
    191       1.1  christos /*******************************************************************************
    192       1.1  christos  *
    193       1.1  christos  * FUNCTION:    PrAddDefine
    194       1.1  christos  *
    195       1.1  christos  * PARAMETERS:  Identifier          - Name to be replaced
    196       1.1  christos  *              Replacement         - Replacement for Identifier
    197       1.1  christos  *              Persist             - Keep define across multiple compiles?
    198       1.1  christos  *
    199       1.1  christos  * RETURN:      A new define_info struct. NULL on error.
    200       1.1  christos  *
    201       1.1  christos  * DESCRIPTION: Add a new #define to the global list
    202       1.1  christos  *
    203       1.1  christos  ******************************************************************************/
    204       1.1  christos 
    205       1.1  christos PR_DEFINE_INFO *
    206       1.1  christos PrAddDefine (
    207       1.1  christos     char                    *Identifier,
    208       1.1  christos     char                    *Replacement,
    209       1.1  christos     BOOLEAN                 Persist)
    210       1.1  christos {
    211       1.1  christos     char                    *IdentifierString;
    212       1.1  christos     char                    *ReplacementString;
    213       1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    214       1.1  christos 
    215       1.1  christos 
    216       1.1  christos     if (!Replacement)
    217       1.1  christos     {
    218       1.1  christos         Replacement = "";
    219       1.1  christos     }
    220       1.1  christos 
    221       1.1  christos     /* Check for already-defined first */
    222       1.1  christos 
    223       1.1  christos     DefineInfo = PrMatchDefine (Identifier);
    224       1.1  christos     if (DefineInfo)
    225       1.1  christos     {
    226  1.1.1.11  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    227       1.1  christos             "#define: name already exists: %s\n",
    228   1.1.1.9  christos             AslGbl_CurrentLineNumber, Identifier);
    229       1.1  christos 
    230       1.1  christos         /*
    231       1.1  christos          * Name already exists. This is only an error if the target name
    232       1.1  christos          * is different.
    233       1.1  christos          */
    234       1.1  christos         if (strcmp (Replacement, DefineInfo->Replacement))
    235       1.1  christos         {
    236       1.1  christos             PrError (ASL_ERROR, ASL_MSG_EXISTING_NAME,
    237       1.1  christos                 THIS_TOKEN_OFFSET (Identifier));
    238       1.1  christos 
    239       1.1  christos             return (NULL);
    240       1.1  christos         }
    241       1.1  christos 
    242       1.1  christos         return (DefineInfo);
    243       1.1  christos     }
    244       1.1  christos 
    245       1.1  christos     /* Copy input strings */
    246       1.1  christos 
    247       1.1  christos     IdentifierString = UtLocalCalloc (strlen (Identifier) + 1);
    248       1.1  christos     strcpy (IdentifierString, Identifier);
    249       1.1  christos 
    250       1.1  christos     ReplacementString = UtLocalCalloc (strlen (Replacement) + 1);
    251       1.1  christos     strcpy (ReplacementString, Replacement);
    252       1.1  christos 
    253       1.1  christos     /* Init and link new define info struct */
    254       1.1  christos 
    255       1.1  christos     DefineInfo = UtLocalCalloc (sizeof (PR_DEFINE_INFO));
    256       1.1  christos     DefineInfo->Replacement = ReplacementString;
    257       1.1  christos     DefineInfo->Identifier = IdentifierString;
    258       1.1  christos     DefineInfo->Persist = Persist;
    259       1.1  christos 
    260   1.1.1.9  christos     if (AslGbl_DefineList)
    261       1.1  christos     {
    262   1.1.1.9  christos         AslGbl_DefineList->Previous = DefineInfo;
    263       1.1  christos     }
    264       1.1  christos 
    265   1.1.1.9  christos     DefineInfo->Next = AslGbl_DefineList;
    266   1.1.1.9  christos     AslGbl_DefineList = DefineInfo;
    267       1.1  christos     return (DefineInfo);
    268       1.1  christos }
    269       1.1  christos 
    270       1.1  christos 
    271       1.1  christos /*******************************************************************************
    272       1.1  christos  *
    273       1.1  christos  * FUNCTION:    PrRemoveDefine
    274       1.1  christos  *
    275       1.1  christos  * PARAMETERS:  DefineName          - Name of define to be removed
    276       1.1  christos  *
    277       1.1  christos  * RETURN:      None
    278       1.1  christos  *
    279       1.1  christos  * DESCRIPTION: Implements #undef. Remove a #define if found in the global
    280       1.1  christos  *              list. No error if the target of the #undef does not exist,
    281       1.1  christos  *              as per the C #undef definition.
    282       1.1  christos  *
    283       1.1  christos  ******************************************************************************/
    284       1.1  christos 
    285       1.1  christos void
    286       1.1  christos PrRemoveDefine (
    287       1.1  christos     char                    *DefineName)
    288       1.1  christos {
    289       1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    290       1.1  christos 
    291       1.1  christos 
    292       1.1  christos     /* Match name and delete the node */
    293       1.1  christos 
    294   1.1.1.9  christos     DefineInfo = AslGbl_DefineList;
    295       1.1  christos     while (DefineInfo)
    296       1.1  christos     {
    297       1.1  christos         if (!strcmp (DefineName, DefineInfo->Identifier))
    298       1.1  christos         {
    299       1.1  christos             /* Remove from linked list */
    300       1.1  christos 
    301       1.1  christos             if (DefineInfo->Previous)
    302       1.1  christos             {
    303       1.1  christos                 (DefineInfo->Previous)->Next = DefineInfo->Next;
    304       1.1  christos             }
    305       1.1  christos             else
    306       1.1  christos             {
    307   1.1.1.9  christos                 AslGbl_DefineList = DefineInfo->Next;
    308       1.1  christos             }
    309       1.1  christos 
    310       1.1  christos             if (DefineInfo->Next)
    311       1.1  christos             {
    312       1.1  christos                 (DefineInfo->Next)->Previous = DefineInfo->Previous;
    313       1.1  christos             }
    314       1.1  christos 
    315       1.1  christos             free (DefineInfo);
    316       1.1  christos             return;
    317       1.1  christos         }
    318       1.1  christos 
    319       1.1  christos         DefineInfo = DefineInfo->Next;
    320       1.1  christos     }
    321       1.1  christos 
    322       1.1  christos     /*
    323       1.1  christos      * Name was not found. By definition of #undef, this is not
    324       1.1  christos      * an error, however.
    325       1.1  christos      */
    326       1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    327       1.1  christos         "#undef: could not find %s\n",
    328   1.1.1.9  christos         AslGbl_CurrentLineNumber, DefineName);
    329       1.1  christos }
    330       1.1  christos 
    331       1.1  christos 
    332       1.1  christos /*******************************************************************************
    333       1.1  christos  *
    334       1.1  christos  * FUNCTION:    PrMatchDefine
    335       1.1  christos  *
    336       1.1  christos  * PARAMETERS:  MatchString         - Name associated with the #define
    337       1.1  christos  *
    338       1.1  christos  * RETURN:      Matched string if found. NULL otherwise.
    339       1.1  christos  *
    340       1.1  christos  * DESCRIPTION: Find a name in global #define list
    341       1.1  christos  *
    342       1.1  christos  ******************************************************************************/
    343       1.1  christos 
    344       1.1  christos PR_DEFINE_INFO *
    345       1.1  christos PrMatchDefine (
    346       1.1  christos     char                    *MatchString)
    347       1.1  christos {
    348       1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    349       1.1  christos 
    350       1.1  christos 
    351   1.1.1.9  christos     DefineInfo = AslGbl_DefineList;
    352       1.1  christos     while (DefineInfo)
    353       1.1  christos     {
    354       1.1  christos         if (!strcmp (MatchString, DefineInfo->Identifier))
    355       1.1  christos         {
    356       1.1  christos             return (DefineInfo);
    357       1.1  christos         }
    358       1.1  christos 
    359       1.1  christos         DefineInfo = DefineInfo->Next;
    360       1.1  christos     }
    361       1.1  christos 
    362       1.1  christos     return (NULL);
    363       1.1  christos }
    364       1.1  christos 
    365       1.1  christos 
    366       1.1  christos /*******************************************************************************
    367       1.1  christos  *
    368       1.1  christos  * FUNCTION:    PrAddMacro
    369       1.1  christos  *
    370       1.1  christos  * PARAMETERS:  Name                - Start of the macro definition
    371       1.1  christos  *              Next                - "Next" buffer from GetNextToken
    372       1.1  christos  *
    373       1.1  christos  * RETURN:      None
    374       1.1  christos  *
    375       1.1  christos  * DESCRIPTION: Add a new macro to the list of #defines. Handles argument
    376       1.1  christos  *              processing.
    377       1.1  christos  *
    378       1.1  christos  ******************************************************************************/
    379       1.1  christos 
    380       1.1  christos void
    381       1.1  christos PrAddMacro (
    382       1.1  christos     char                    *Name,
    383       1.1  christos     char                    **Next)
    384       1.1  christos {
    385       1.1  christos     char                    *Token = NULL;
    386       1.1  christos     ACPI_SIZE               TokenOffset;
    387       1.1  christos     ACPI_SIZE               MacroBodyOffset;
    388       1.1  christos     PR_DEFINE_INFO          *DefineInfo;
    389       1.1  christos     PR_MACRO_ARG            *Args;
    390       1.1  christos     char                    *Body;
    391       1.1  christos     char                    *BodyInSource;
    392       1.1  christos     UINT32                  i;
    393       1.1  christos     UINT16                  UseCount = 0;
    394       1.1  christos     UINT16                  ArgCount = 0;
    395       1.1  christos     UINT32                  Depth = 1;
    396  1.1.1.15  christos     /*UINT32                  Depth = 1;*/
    397       1.1  christos     UINT32                  EndOfArgList;
    398       1.1  christos     char                    BufferChar;
    399       1.1  christos 
    400       1.1  christos     /* Find the end of the arguments list */
    401       1.1  christos 
    402   1.1.1.9  christos     TokenOffset = Name - AslGbl_MainTokenBuffer + strlen (Name) + 1;
    403       1.1  christos     while (1)
    404       1.1  christos     {
    405   1.1.1.9  christos         BufferChar = AslGbl_CurrentLineBuffer[TokenOffset];
    406       1.1  christos         if (BufferChar == '(')
    407       1.1  christos         {
    408       1.1  christos             Depth++;
    409       1.1  christos         }
    410       1.1  christos         else if (BufferChar == ')')
    411       1.1  christos         {
    412       1.1  christos             Depth--;
    413       1.1  christos         }
    414       1.1  christos         else if (BufferChar == 0)
    415       1.1  christos         {
    416       1.1  christos             PrError (ASL_ERROR, ASL_MSG_MACRO_SYNTAX, TokenOffset);
    417       1.1  christos             return;
    418       1.1  christos         }
    419       1.1  christos 
    420       1.1  christos         if (Depth == 0)
    421       1.1  christos         {
    422       1.1  christos             /* Found arg list end */
    423       1.1  christos 
    424       1.1  christos             EndOfArgList = TokenOffset;
    425       1.1  christos             break;
    426       1.1  christos         }
    427       1.1  christos 
    428       1.1  christos         TokenOffset++;
    429       1.1  christos     }
    430       1.1  christos 
    431       1.1  christos     /* At this point, we know that we have a reasonable argument list */
    432       1.1  christos 
    433       1.1  christos     Args = UtLocalCalloc (sizeof (PR_MACRO_ARG) * PR_MAX_MACRO_ARGS);
    434       1.1  christos 
    435       1.1  christos     /* Get the macro argument names */
    436       1.1  christos 
    437       1.1  christos     for (i = 0; i < PR_MAX_MACRO_ARGS; i++)
    438       1.1  christos     {
    439       1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    440  1.1.1.15  christos 
    441       1.1  christos         if (!Token)
    442       1.1  christos         {
    443       1.1  christos             /* This is the case for a NULL macro body */
    444       1.1  christos 
    445       1.1  christos             BodyInSource = "";
    446       1.1  christos             goto AddMacroToList;
    447       1.1  christos         }
    448       1.1  christos 
    449       1.1  christos         /* Don't go beyond the argument list */
    450       1.1  christos 
    451   1.1.1.9  christos         TokenOffset = Token - AslGbl_MainTokenBuffer + strlen (Token);
    452       1.1  christos         if (TokenOffset > EndOfArgList)
    453       1.1  christos         {
    454       1.1  christos             break;
    455       1.1  christos         }
    456       1.1  christos 
    457       1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    458  1.1.1.16  christos             "Macro param: %s\n",
    459   1.1.1.9  christos             AslGbl_CurrentLineNumber, Token);
    460       1.1  christos 
    461       1.1  christos         Args[i].Name = UtLocalCalloc (strlen (Token) + 1);
    462       1.1  christos         strcpy (Args[i].Name, Token);
    463       1.1  christos 
    464       1.1  christos         Args[i].UseCount = 0;
    465       1.1  christos         ArgCount++;
    466       1.1  christos         if (ArgCount >= PR_MAX_MACRO_ARGS)
    467       1.1  christos         {
    468       1.1  christos             PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS, TokenOffset);
    469   1.1.1.2  christos             goto ErrorExit;
    470       1.1  christos         }
    471       1.1  christos     }
    472       1.1  christos 
    473       1.1  christos     /* Get the macro body. Token now points to start of body */
    474       1.1  christos 
    475   1.1.1.9  christos     MacroBodyOffset = Token - AslGbl_MainTokenBuffer;
    476       1.1  christos 
    477       1.1  christos     /* Match each method arg in the macro body for later use */
    478       1.1  christos 
    479       1.1  christos     while (Token)
    480       1.1  christos     {
    481       1.1  christos         /* Search the macro arg list for matching arg */
    482       1.1  christos 
    483   1.1.1.6  christos         for (i = 0; ((i < PR_MAX_MACRO_ARGS) && Args[i].Name); i++)
    484       1.1  christos         {
    485       1.1  christos             /*
    486       1.1  christos              * Save argument offset within macro body. This is the mechanism
    487       1.1  christos              * used to expand the macro upon invocation.
    488       1.1  christos              *
    489       1.1  christos              * Handles multiple instances of the same argument
    490       1.1  christos              */
    491       1.1  christos             if (!strcmp (Token, Args[i].Name))
    492       1.1  christos             {
    493       1.1  christos                 UseCount = Args[i].UseCount;
    494       1.1  christos 
    495   1.1.1.4  christos                 Args[i].Offset[UseCount] =
    496   1.1.1.9  christos                     (Token - AslGbl_MainTokenBuffer) - MacroBodyOffset;
    497       1.1  christos 
    498  1.1.1.15  christos 
    499       1.1  christos                 DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    500  1.1.1.16  christos                     "Macro Arg #%u: %s UseCount %u Offset %u\n",
    501   1.1.1.9  christos                     AslGbl_CurrentLineNumber, i, Token,
    502       1.1  christos                     UseCount+1, Args[i].Offset[UseCount]);
    503       1.1  christos 
    504       1.1  christos                 Args[i].UseCount++;
    505  1.1.1.15  christos 
    506       1.1  christos                 if (Args[i].UseCount >= PR_MAX_ARG_INSTANCES)
    507       1.1  christos                 {
    508       1.1  christos                     PrError (ASL_ERROR, ASL_MSG_TOO_MANY_ARGUMENTS,
    509       1.1  christos                         THIS_TOKEN_OFFSET (Token));
    510       1.1  christos 
    511   1.1.1.2  christos                     goto ErrorExit;
    512       1.1  christos                 }
    513       1.1  christos                 break;
    514       1.1  christos             }
    515       1.1  christos         }
    516       1.1  christos 
    517       1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    518       1.1  christos     }
    519       1.1  christos 
    520   1.1.1.9  christos     BodyInSource = &AslGbl_CurrentLineBuffer[MacroBodyOffset];
    521       1.1  christos 
    522       1.1  christos 
    523       1.1  christos AddMacroToList:
    524       1.1  christos 
    525       1.1  christos     /* Check if name is already defined first */
    526       1.1  christos 
    527       1.1  christos     DefineInfo = PrMatchDefine (Name);
    528       1.1  christos     if (DefineInfo)
    529       1.1  christos     {
    530       1.1  christos         DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    531       1.1  christos             "#define: macro name already exists: %s\n",
    532   1.1.1.9  christos             AslGbl_CurrentLineNumber, Name);
    533       1.1  christos 
    534       1.1  christos         /* Error only if not exactly the same macro */
    535       1.1  christos 
    536       1.1  christos         if (strcmp (DefineInfo->Body, BodyInSource) ||
    537       1.1  christos             (DefineInfo->ArgCount != ArgCount))
    538       1.1  christos         {
    539       1.1  christos             PrError (ASL_ERROR, ASL_MSG_EXISTING_NAME,
    540       1.1  christos                 THIS_TOKEN_OFFSET (Name));
    541       1.1  christos         }
    542       1.1  christos 
    543   1.1.1.2  christos         goto ErrorExit;
    544       1.1  christos     }
    545       1.1  christos 
    546       1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    547  1.1.1.16  christos         "Macro body: %s\n",
    548   1.1.1.9  christos         AslGbl_CurrentLineNumber, BodyInSource);
    549       1.1  christos 
    550       1.1  christos     /* Add macro to the #define list */
    551       1.1  christos 
    552       1.1  christos     DefineInfo = PrAddDefine (Name, BodyInSource, FALSE);
    553       1.1  christos     if (DefineInfo)
    554       1.1  christos     {
    555       1.1  christos         Body = UtLocalCalloc (strlen (BodyInSource) + 1);
    556       1.1  christos         strcpy (Body, BodyInSource);
    557       1.1  christos 
    558       1.1  christos         DefineInfo->Body = Body;
    559       1.1  christos         DefineInfo->Args = Args;
    560       1.1  christos         DefineInfo->ArgCount = ArgCount;
    561       1.1  christos     }
    562   1.1.1.2  christos 
    563   1.1.1.2  christos     return;
    564   1.1.1.2  christos 
    565   1.1.1.2  christos 
    566   1.1.1.2  christos ErrorExit:
    567   1.1.1.2  christos     ACPI_FREE (Args);
    568   1.1.1.2  christos     return;
    569       1.1  christos }
    570       1.1  christos 
    571       1.1  christos 
    572       1.1  christos /*******************************************************************************
    573       1.1  christos  *
    574       1.1  christos  * FUNCTION:    PrDoMacroInvocation
    575       1.1  christos  *
    576       1.1  christos  * PARAMETERS:  TokenBuffer         - Current line buffer
    577       1.1  christos  *              MacroStart          - Start of the macro invocation within
    578       1.1  christos  *                                    the token buffer
    579       1.1  christos  *              DefineInfo          - Info for this macro
    580       1.1  christos  *              Next                - "Next" buffer from GetNextToken
    581       1.1  christos  *
    582       1.1  christos  * RETURN:      None
    583       1.1  christos  *
    584       1.1  christos  * DESCRIPTION: Expand a macro invocation
    585       1.1  christos  *
    586       1.1  christos  ******************************************************************************/
    587       1.1  christos 
    588       1.1  christos void
    589       1.1  christos PrDoMacroInvocation (
    590       1.1  christos     char                    *TokenBuffer,
    591       1.1  christos     char                    *MacroStart,
    592       1.1  christos     PR_DEFINE_INFO          *DefineInfo,
    593       1.1  christos     char                    **Next)
    594       1.1  christos {
    595       1.1  christos     PR_MACRO_ARG            *Args;
    596       1.1  christos     char                    *Token = NULL;
    597       1.1  christos     UINT32                  TokenOffset;
    598       1.1  christos     UINT32                  Length;
    599       1.1  christos     UINT32                  i;
    600  1.1.1.15  christos     UINT32                  Diff1;
    601  1.1.1.15  christos     UINT32                  Diff2;
    602       1.1  christos 
    603       1.1  christos     /* Take a copy of the macro body for expansion */
    604       1.1  christos 
    605   1.1.1.9  christos     strcpy (AslGbl_MacroTokenBuffer, DefineInfo->Body);
    606       1.1  christos 
    607       1.1  christos     /* Replace each argument within the prototype body */
    608       1.1  christos 
    609       1.1  christos     Args = DefineInfo->Args;
    610       1.1  christos     if (!Args->Name)
    611       1.1  christos     {
    612       1.1  christos         /* This macro has no arguments */
    613       1.1  christos 
    614       1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_ARGUMENTS, Next);
    615  1.1.1.15  christos 
    616       1.1  christos         if (!Token)
    617       1.1  christos         {
    618       1.1  christos             goto BadInvocation;
    619       1.1  christos         }
    620       1.1  christos 
    621       1.1  christos         TokenOffset = (MacroStart - TokenBuffer);
    622       1.1  christos         Length = Token - MacroStart + strlen (Token) + 1;
    623       1.1  christos 
    624       1.1  christos         PrReplaceData (
    625   1.1.1.9  christos             &AslGbl_CurrentLineBuffer[TokenOffset], Length,
    626   1.1.1.9  christos             AslGbl_MacroTokenBuffer, strlen (AslGbl_MacroTokenBuffer));
    627       1.1  christos         return;
    628       1.1  christos     }
    629       1.1  christos 
    630       1.1  christos     while (Args->Name)
    631       1.1  christos     {
    632       1.1  christos         /* Get the next argument from macro invocation */
    633       1.1  christos 
    634       1.1  christos         Token = PrGetNextToken (NULL, PR_MACRO_SEPARATORS, Next);
    635       1.1  christos         if (!Token)
    636       1.1  christos         {
    637       1.1  christos             goto BadInvocation;
    638       1.1  christos         }
    639       1.1  christos 
    640  1.1.1.15  christos         /*
    641  1.1.1.15  christos          * Avoid optimizing using just 1 signed int due to specific
    642  1.1.1.15  christos          * non-portable implementations of signed ints
    643  1.1.1.15  christos          */
    644  1.1.1.15  christos         Diff1 = strlen (Args->Name) > strlen (Token) ? strlen (Args->Name) -
    645  1.1.1.15  christos             strlen (Token) : 0;
    646  1.1.1.15  christos 
    647  1.1.1.15  christos         Diff2 = strlen (Args->Name) < strlen (Token) ? strlen (Token) -
    648  1.1.1.15  christos             strlen (Args->Name) : 0;
    649  1.1.1.15  christos 
    650       1.1  christos         /* Replace all instances of this argument */
    651       1.1  christos 
    652       1.1  christos         for (i = 0; i < Args->UseCount; i++)
    653       1.1  christos         {
    654  1.1.1.15  christos             /*
    655  1.1.1.15  christos              * To test the output of the preprocessed macro function that
    656  1.1.1.15  christos              * is passed to the compiler
    657  1.1.1.15  christos              */
    658       1.1  christos 
    659  1.1.1.15  christos              /*
    660  1.1.1.15  christos               * fprintf (stderr, "Current token = %s \t Current arg_name = %s \
    661  1.1.1.15  christos               * \t strlen (Token) = %u \t strlen (Args->Name) = %u \t Offset = %u \
    662  1.1.1.15  christos               * \t UseCount = %u \t", Token, Args->Name, strlen (Token), \
    663  1.1.1.15  christos               *     strlen (Args->Name), Args->Offset[i], Args->UseCount);
    664  1.1.1.15  christos               */
    665  1.1.1.15  christos 
    666  1.1.1.15  christos             AslGbl_MacroTokenReplaceBuffer = (char *) calloc ((strlen (AslGbl_MacroTokenBuffer)), sizeof (char));
    667       1.1  christos 
    668  1.1.1.15  christos             PrReplaceResizeSubstring (Args, Diff1, Diff2, i, Token);
    669       1.1  christos 
    670       1.1  christos             DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    671  1.1.1.16  christos                 "ExpandArg: %s\n",
    672   1.1.1.9  christos                 AslGbl_CurrentLineNumber, AslGbl_MacroTokenBuffer);
    673       1.1  christos         }
    674       1.1  christos 
    675       1.1  christos         Args++;
    676       1.1  christos     }
    677       1.1  christos 
    678       1.1  christos     if (!Token)
    679       1.1  christos     {
    680       1.1  christos         return;
    681       1.1  christos     }
    682       1.1  christos 
    683       1.1  christos     /* Replace the entire macro invocation with the expanded macro */
    684       1.1  christos 
    685       1.1  christos     TokenOffset = (MacroStart - TokenBuffer);
    686       1.1  christos     Length = Token - MacroStart + strlen (Token) + 1;
    687       1.1  christos 
    688       1.1  christos     PrReplaceData (
    689   1.1.1.9  christos         &AslGbl_CurrentLineBuffer[TokenOffset], Length,
    690   1.1.1.9  christos         AslGbl_MacroTokenBuffer, strlen (AslGbl_MacroTokenBuffer));
    691       1.1  christos 
    692       1.1  christos     return;
    693       1.1  christos 
    694       1.1  christos BadInvocation:
    695       1.1  christos     PrError (ASL_ERROR, ASL_MSG_INVALID_INVOCATION,
    696       1.1  christos         THIS_TOKEN_OFFSET (MacroStart));
    697       1.1  christos 
    698       1.1  christos     DbgPrint (ASL_DEBUG_OUTPUT, PR_PREFIX_ID
    699  1.1.1.16  christos         "Bad macro invocation: %s\n",
    700   1.1.1.9  christos         AslGbl_CurrentLineNumber, AslGbl_MacroTokenBuffer);
    701       1.1  christos     return;
    702       1.1  christos }
    703