Home | History | Annotate | Line # | Download | only in compiler
      1 /******************************************************************************
      2  *
      3  * Module Name: aslbtypes - Support for bitfield types
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2025, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #include "aslcompiler.h"
     45 #include "aslcompiler.y.h"
     46 #include "amlcode.h"
     47 
     48 
     49 #define _COMPONENT          ACPI_COMPILER
     50         ACPI_MODULE_NAME    ("aslbtypes")
     51 
     52 /* Local prototypes */
     53 
     54 static UINT32
     55 AnMapEtypeToBtype (
     56     UINT32                  Etype);
     57 
     58 
     59 /*******************************************************************************
     60  *
     61  * FUNCTION:    AnMapArgTypeToBtype
     62  *
     63  * PARAMETERS:  ArgType             - The ARGI required type(s) for this
     64  *                                    argument, from the opcode info table
     65  *
     66  * RETURN:      The corresponding Bit-encoded types
     67  *
     68  * DESCRIPTION: Convert an encoded ARGI required argument type code into a
     69  *              bitfield type code. Implements the implicit source conversion
     70  *              rules.
     71  *
     72  ******************************************************************************/
     73 
     74 UINT32
     75 AnMapArgTypeToBtype (
     76     UINT32                  ArgType)
     77 {
     78 
     79     switch (ArgType)
     80     {
     81     /* Simple types */
     82 
     83     case ARGI_ANYTYPE:
     84 
     85         return (ACPI_BTYPE_OBJECTS_AND_REFS);
     86 
     87     case ARGI_PACKAGE:
     88 
     89         return (ACPI_BTYPE_PACKAGE);
     90 
     91     case ARGI_EVENT:
     92 
     93         return (ACPI_BTYPE_EVENT);
     94 
     95     case ARGI_MUTEX:
     96 
     97         return (ACPI_BTYPE_MUTEX);
     98 
     99     case ARGI_DDBHANDLE:
    100         /*
    101          * DDBHandleObject := SuperName
    102          * ACPI_BTYPE_REFERENCE_OBJECT:
    103          *      Index reference as parameter of Load/Unload
    104          */
    105         return (ACPI_BTYPE_DDB_HANDLE | ACPI_BTYPE_REFERENCE_OBJECT);
    106 
    107     /* Interchangeable types */
    108     /*
    109      * Source conversion rules:
    110      * Integer, String, and Buffer are all interchangeable
    111      */
    112     case ARGI_INTEGER:
    113     case ARGI_STRING:
    114     case ARGI_BUFFER:
    115     case ARGI_BUFFER_OR_STRING:
    116     case ARGI_COMPUTEDATA:
    117 
    118         return (ACPI_BTYPE_COMPUTE_DATA);
    119 
    120     /* References */
    121 
    122     case ARGI_INTEGER_REF:
    123 
    124         return (ACPI_BTYPE_INTEGER);
    125 
    126     case ARGI_OBJECT_REF:
    127 
    128         return (ACPI_BTYPE_ALL_OBJECTS);
    129 
    130     case ARGI_DEVICE_REF:
    131 
    132         return (ACPI_BTYPE_DEVICE_OBJECTS);
    133 
    134     case ARGI_REFERENCE:
    135 
    136         return (ACPI_BTYPE_NAMED_REFERENCE); /* Name or Namestring */
    137 
    138     case ARGI_TARGETREF:
    139 
    140         /*
    141          * Target operand for most math and logic operators.
    142          * Package objects not allowed as target.
    143          */
    144         return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_DEBUG_OBJECT |
    145             ACPI_BTYPE_REFERENCE_OBJECT);
    146 
    147     case ARGI_STORE_TARGET:
    148 
    149         /* Special target for Store(), includes packages */
    150 
    151         return (ACPI_BTYPE_DATA | ACPI_BTYPE_DEBUG_OBJECT |
    152             ACPI_BTYPE_REFERENCE_OBJECT);
    153 
    154     case ARGI_FIXED_TARGET:
    155     case ARGI_SIMPLE_TARGET:
    156 
    157         return (ACPI_BTYPE_OBJECTS_AND_REFS);
    158 
    159     /* Complex types */
    160 
    161     case ARGI_DATAOBJECT:
    162         /*
    163          * Buffer, string, package or reference to a Op -
    164          * Used only by SizeOf operator
    165          */
    166         return (ACPI_BTYPE_STRING | ACPI_BTYPE_BUFFER |
    167             ACPI_BTYPE_PACKAGE | ACPI_BTYPE_REFERENCE_OBJECT);
    168 
    169     case ARGI_COMPLEXOBJ:
    170 
    171         /* Buffer, String, or package */
    172 
    173         return (ACPI_BTYPE_STRING | ACPI_BTYPE_BUFFER |
    174             ACPI_BTYPE_PACKAGE);
    175 
    176     case ARGI_REF_OR_STRING:
    177 
    178         /* Used by DeRefOf operator only */
    179 
    180         return (ACPI_BTYPE_STRING | ACPI_BTYPE_REFERENCE_OBJECT);
    181 
    182     case ARGI_REGION_OR_BUFFER:
    183 
    184         /* Used by Load() only. Allow buffers in addition to regions/fields */
    185 
    186         return (ACPI_BTYPE_REGION | ACPI_BTYPE_BUFFER |
    187             ACPI_BTYPE_FIELD_UNIT);
    188 
    189     case ARGI_DATAREFOBJ:
    190 
    191         /* Used by Store() only, as the source operand */
    192 
    193         return (ACPI_BTYPE_DATA_REFERENCE | ACPI_BTYPE_REFERENCE_OBJECT);
    194 
    195     default:
    196 
    197         break;
    198     }
    199 
    200     return (ACPI_BTYPE_OBJECTS_AND_REFS);
    201 }
    202 
    203 
    204 /*******************************************************************************
    205  *
    206  * FUNCTION:    AnMapEtypeToBtype
    207  *
    208  * PARAMETERS:  Etype               - Encoded ACPI Type
    209  *
    210  * RETURN:      Btype corresponding to the Etype
    211  *
    212  * DESCRIPTION: Convert an encoded ACPI type to a bitfield type applying the
    213  *              operand conversion rules. In other words, returns the type(s)
    214  *              this Etype is implicitly converted to during interpretation.
    215  *
    216  ******************************************************************************/
    217 
    218 static UINT32
    219 AnMapEtypeToBtype (
    220     UINT32                  Etype)
    221 {
    222 
    223     if (Etype == ACPI_TYPE_ANY)
    224     {
    225         return (ACPI_BTYPE_OBJECTS_AND_REFS);
    226     }
    227 
    228     /* Try the standard ACPI data types */
    229 
    230     if (Etype <= ACPI_TYPE_EXTERNAL_MAX)
    231     {
    232         /*
    233          * This switch statement implements the allowed operand conversion
    234          * rules as per the "ASL Data Types" section of the ACPI
    235          * specification.
    236          */
    237         switch (Etype)
    238         {
    239         case ACPI_TYPE_INTEGER:
    240 
    241             return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_DDB_HANDLE);
    242 
    243         case ACPI_TYPE_STRING:
    244         case ACPI_TYPE_BUFFER:
    245 
    246             return (ACPI_BTYPE_COMPUTE_DATA);
    247 
    248         case ACPI_TYPE_PACKAGE:
    249 
    250             return (ACPI_BTYPE_PACKAGE);
    251 
    252         case ACPI_TYPE_FIELD_UNIT:
    253 
    254             return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_FIELD_UNIT);
    255 
    256         case ACPI_TYPE_BUFFER_FIELD:
    257 
    258             return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_BUFFER_FIELD);
    259 
    260         case ACPI_TYPE_DDB_HANDLE:
    261 
    262             return (ACPI_BTYPE_INTEGER | ACPI_BTYPE_DDB_HANDLE);
    263 
    264         case ACPI_TYPE_DEBUG_OBJECT:
    265 
    266             /* Cannot be used as a source operand */
    267 
    268             return (0);
    269 
    270         default:
    271 
    272             return (1 << (Etype - 1));
    273         }
    274     }
    275 
    276     /* Try the internal data types */
    277 
    278     switch (Etype)
    279     {
    280     case ACPI_TYPE_LOCAL_REGION_FIELD:
    281     case ACPI_TYPE_LOCAL_BANK_FIELD:
    282     case ACPI_TYPE_LOCAL_INDEX_FIELD:
    283 
    284         /* Named fields can be either Integer/Buffer/String */
    285 
    286         return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_FIELD_UNIT);
    287 
    288     case ACPI_TYPE_LOCAL_ALIAS:
    289 
    290         return (ACPI_BTYPE_INTEGER);
    291 
    292 
    293     case ACPI_TYPE_LOCAL_RESOURCE:
    294     case ACPI_TYPE_LOCAL_RESOURCE_FIELD:
    295 
    296         return (ACPI_BTYPE_REFERENCE_OBJECT);
    297 
    298     default:
    299 
    300         printf ("Unhandled encoded type: %X\n", Etype);
    301         return (0);
    302     }
    303 }
    304 
    305 
    306 /*******************************************************************************
    307  *
    308  * FUNCTION:    AnFormatBtype
    309  *
    310  * PARAMETERS:  Btype               - Bitfield of ACPI types
    311  *              Buffer              - Where to put the ascii string
    312  *
    313  * RETURN:      None.
    314  *
    315  * DESCRIPTION: Convert a Btype to a string of ACPI types
    316  *
    317  ******************************************************************************/
    318 
    319 void
    320 AnFormatBtype (
    321     char                    *Buffer,
    322     UINT32                  Btype)
    323 {
    324     UINT32                  Type;
    325     BOOLEAN                 First = TRUE;
    326 
    327 
    328     *Buffer = 0;
    329     if (Btype == 0)
    330     {
    331         strcat (Buffer, "NoReturnValue");
    332         return;
    333     }
    334 
    335     for (Type = 1; Type <= ACPI_TYPE_EXTERNAL_MAX; Type++)
    336     {
    337         if (Btype & 0x00000001)
    338         {
    339             if (!First)
    340             {
    341                 strcat (Buffer, "|");
    342             }
    343 
    344             First = FALSE;
    345             strcat (Buffer, AcpiUtGetTypeName (Type));
    346         }
    347         Btype >>= 1;
    348     }
    349 
    350     if (Btype & 0x00000001)
    351     {
    352         if (!First)
    353         {
    354             strcat (Buffer, "|");
    355         }
    356 
    357         First = FALSE;
    358         strcat (Buffer, "Reference");
    359     }
    360 
    361     Btype >>= 1;
    362     if (Btype & 0x00000001)
    363     {
    364         if (!First)
    365         {
    366             strcat (Buffer, "|");
    367         }
    368 
    369         strcat (Buffer, "Resource");
    370     }
    371 }
    372 
    373 
    374 /*******************************************************************************
    375  *
    376  * FUNCTION:    AnGetBtype
    377  *
    378  * PARAMETERS:  Op                  - Parse node whose type will be returned.
    379  *
    380  * RETURN:      The Btype associated with the Op.
    381  *
    382  * DESCRIPTION: Get the (bitfield) ACPI type associated with the parse node.
    383  *              Handles the case where the node is a name or method call and
    384  *              the actual type must be obtained from the namespace node.
    385  *
    386  ******************************************************************************/
    387 
    388 UINT32
    389 AnGetBtype (
    390     ACPI_PARSE_OBJECT       *Op)
    391 {
    392     ACPI_NAMESPACE_NODE     *Node;
    393     ACPI_PARSE_OBJECT       *ReferencedNode;
    394     UINT32                  ThisNodeBtype = 0;
    395 
    396     ACPI_FUNCTION_NAME (AnGetBtype);
    397 
    398     if (!Op)
    399     {
    400         AcpiOsPrintf ("Null Op in %s\n",  ACPI_GET_FUNCTION_NAME);
    401         return (ACPI_UINT32_MAX);
    402     }
    403 
    404     if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)     ||
    405         (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING)  ||
    406         (Op->Asl.ParseOpcode == PARSEOP_METHODCALL))
    407     {
    408         Node = Op->Asl.Node;
    409         if (!Node)
    410         {
    411             /* These are not expected to have a node at this time */
    412 
    413             if ((Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEWORDFIELD) ||
    414                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEDWORDFIELD) ||
    415                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEQWORDFIELD) ||
    416                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEBYTEFIELD) ||
    417                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEBITFIELD) ||
    418                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEFIELD)    ||
    419                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CONDREFOF))
    420             {
    421                 return (ACPI_UINT32_MAX - 1);
    422             }
    423 
    424             DbgPrint (ASL_DEBUG_OUTPUT,
    425                 "No attached Nsnode: [%s] at line %u name [%s], "
    426                 "ignoring typecheck. Parent [%s]\n",
    427                 Op->Asl.ParseOpName, Op->Asl.LineNumber,
    428                 Op->Asl.ExternalName, Op->Asl.Parent->Asl.ParseOpName);
    429             return (ACPI_UINT32_MAX - 1);
    430         }
    431 
    432         ThisNodeBtype = AnMapEtypeToBtype (Node->Type);
    433         if (!ThisNodeBtype)
    434         {
    435             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
    436                 "could not map type");
    437         }
    438 
    439         if (Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
    440         {
    441             ReferencedNode = Node->Op;
    442             if (!ReferencedNode)
    443             {
    444                 /* Check for an internal method */
    445 
    446                 if (AnIsInternalMethod (Op))
    447                 {
    448                     return (AnGetInternalMethodReturnType (Op));
    449                 }
    450 
    451                 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
    452                     "null Op pointer");
    453                 return (ACPI_UINT32_MAX);
    454             }
    455 
    456             if (ReferencedNode->Asl.CompileFlags & OP_METHOD_TYPED)
    457             {
    458                 ThisNodeBtype = ReferencedNode->Asl.AcpiBtype;
    459             }
    460             else
    461             {
    462                 return (ACPI_UINT32_MAX -1);
    463             }
    464         }
    465     }
    466     else
    467     {
    468         ThisNodeBtype = Op->Asl.AcpiBtype;
    469     }
    470 
    471     return (ThisNodeBtype);
    472 }
    473 
    474 
    475 /*******************************************************************************
    476  *
    477  * FUNCTION:    AnMapObjTypeToBtype
    478  *
    479  * PARAMETERS:  Op                  - A parse node
    480  *
    481  * RETURN:      A Btype
    482  *
    483  * DESCRIPTION: Map object to the associated "Btype"
    484  *
    485  ******************************************************************************/
    486 
    487 UINT32
    488 AnMapObjTypeToBtype (
    489     ACPI_PARSE_OBJECT       *Op)
    490 {
    491 
    492     switch (Op->Asl.ParseOpcode)
    493     {
    494     case PARSEOP_OBJECTTYPE_BFF:        /* "BuffFieldObj" */
    495 
    496         return (ACPI_BTYPE_BUFFER_FIELD);
    497 
    498     case PARSEOP_OBJECTTYPE_BUF:        /* "BuffObj" */
    499 
    500         return (ACPI_BTYPE_BUFFER);
    501 
    502     case PARSEOP_OBJECTTYPE_DDB:        /* "DDBHandleObj" */
    503 
    504         return (ACPI_BTYPE_DDB_HANDLE);
    505 
    506     case PARSEOP_OBJECTTYPE_DEV:        /* "DeviceObj" */
    507 
    508         return (ACPI_BTYPE_DEVICE);
    509 
    510     case PARSEOP_OBJECTTYPE_EVT:        /* "EventObj" */
    511 
    512         return (ACPI_BTYPE_EVENT);
    513 
    514     case PARSEOP_OBJECTTYPE_FLD:        /* "FieldUnitObj" */
    515 
    516         return (ACPI_BTYPE_FIELD_UNIT);
    517 
    518     case PARSEOP_OBJECTTYPE_INT:        /* "IntObj" */
    519 
    520         return (ACPI_BTYPE_INTEGER);
    521 
    522     case PARSEOP_OBJECTTYPE_MTH:        /* "MethodObj" */
    523 
    524         return (ACPI_BTYPE_METHOD);
    525 
    526     case PARSEOP_OBJECTTYPE_MTX:        /* "MutexObj" */
    527 
    528         return (ACPI_BTYPE_MUTEX);
    529 
    530     case PARSEOP_OBJECTTYPE_OPR:        /* "OpRegionObj" */
    531 
    532         return (ACPI_BTYPE_REGION);
    533 
    534     case PARSEOP_OBJECTTYPE_PKG:        /* "PkgObj" */
    535 
    536         return (ACPI_BTYPE_PACKAGE);
    537 
    538     case PARSEOP_OBJECTTYPE_POW:        /* "PowerResObj" */
    539 
    540         return (ACPI_BTYPE_POWER);
    541 
    542     case PARSEOP_OBJECTTYPE_STR:        /* "StrObj" */
    543 
    544         return (ACPI_BTYPE_STRING);
    545 
    546     case PARSEOP_OBJECTTYPE_THZ:        /* "ThermalZoneObj" */
    547 
    548         return (ACPI_BTYPE_THERMAL);
    549 
    550     case PARSEOP_OBJECTTYPE_UNK:        /* "UnknownObj" */
    551 
    552         return (ACPI_BTYPE_OBJECTS_AND_REFS);
    553 
    554     default:
    555 
    556         return (0);
    557     }
    558 }
    559 
    560 
    561 #ifdef ACPI_OBSOLETE_FUNCTIONS
    562 /*******************************************************************************
    563  *
    564  * FUNCTION:    AnMapBtypeToEtype
    565  *
    566  * PARAMETERS:  Btype               - Bitfield of ACPI types
    567  *
    568  * RETURN:      The Etype corresponding the Btype
    569  *
    570  * DESCRIPTION: Convert a bitfield type to an encoded type
    571  *
    572  ******************************************************************************/
    573 
    574 UINT32
    575 AnMapBtypeToEtype (
    576     UINT32              Btype)
    577 {
    578     UINT32              i;
    579     UINT32              Etype;
    580 
    581 
    582     if (Btype == 0)
    583     {
    584         return (0);
    585     }
    586 
    587     Etype = 1;
    588     for (i = 1; i < Btype; i *= 2)
    589     {
    590         Etype++;
    591     }
    592 
    593     return (Etype);
    594 }
    595 #endif
    596