Home | History | Annotate | Line # | Download | only in compiler
aslparseop.c revision 1.1.1.3.2.3
      1          1.1  christos /******************************************************************************
      2          1.1  christos  *
      3          1.1  christos  * Module Name: aslparseop - Parse op create/allocate/cache interfaces
      4          1.1  christos  *
      5          1.1  christos  *****************************************************************************/
      6          1.1  christos 
      7          1.1  christos /*
      8  1.1.1.3.2.2    martin  * Copyright (C) 2000 - 2020, Intel Corp.
      9          1.1  christos  * All rights reserved.
     10          1.1  christos  *
     11          1.1  christos  * Redistribution and use in source and binary forms, with or without
     12          1.1  christos  * modification, are permitted provided that the following conditions
     13          1.1  christos  * are met:
     14          1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15          1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16          1.1  christos  *    without modification.
     17          1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18          1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19          1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20          1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21          1.1  christos  *    binary redistribution.
     22          1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23          1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24          1.1  christos  *    from this software without specific prior written permission.
     25          1.1  christos  *
     26          1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27          1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28          1.1  christos  * Software Foundation.
     29          1.1  christos  *
     30          1.1  christos  * NO WARRANTY
     31          1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32          1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33          1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34          1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35          1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36          1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37          1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38          1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39          1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40          1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41          1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42          1.1  christos  */
     43          1.1  christos 
     44          1.1  christos #include "aslcompiler.h"
     45          1.1  christos #include "aslcompiler.y.h"
     46          1.1  christos #include "acapps.h"
     47          1.1  christos #include "acconvert.h"
     48          1.1  christos 
     49          1.1  christos #define _COMPONENT          ACPI_COMPILER
     50          1.1  christos         ACPI_MODULE_NAME    ("aslparseop")
     51          1.1  christos 
     52          1.1  christos 
     53          1.1  christos /*******************************************************************************
     54          1.1  christos  *
     55          1.1  christos  * FUNCTION:    TrCreateOp
     56          1.1  christos  *
     57          1.1  christos  * PARAMETERS:  ParseOpcode         - Opcode to be assigned to the op
     58          1.1  christos  *              NumChildren         - Number of children to follow
     59          1.1  christos  *              ...                 - A list of child ops to link to the new
     60          1.1  christos  *                                    op. NumChildren long.
     61          1.1  christos  *
     62          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
     63          1.1  christos  *
     64          1.1  christos  * DESCRIPTION: Create a new parse op and link together a list of child
     65          1.1  christos  *              ops underneath the new op.
     66          1.1  christos  *
     67          1.1  christos  ******************************************************************************/
     68          1.1  christos 
     69          1.1  christos ACPI_PARSE_OBJECT *
     70          1.1  christos TrCreateOp (
     71          1.1  christos     UINT32                  ParseOpcode,
     72          1.1  christos     UINT32                  NumChildren,
     73          1.1  christos     ...)
     74          1.1  christos {
     75          1.1  christos     ACPI_PARSE_OBJECT       *Op;
     76          1.1  christos     ACPI_PARSE_OBJECT       *Child;
     77          1.1  christos     ACPI_PARSE_OBJECT       *PrevChild;
     78          1.1  christos     va_list                 ap;
     79          1.1  christos     UINT32                  i;
     80          1.1  christos     BOOLEAN                 FirstChild;
     81          1.1  christos 
     82          1.1  christos 
     83          1.1  christos     va_start (ap, NumChildren);
     84          1.1  christos 
     85          1.1  christos     /* Allocate one new op */
     86          1.1  christos 
     87          1.1  christos     Op = TrAllocateOp (ParseOpcode);
     88          1.1  christos 
     89          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
     90          1.1  christos         "\nCreateOp  Ln/Col %u/%u NewParent %p Child %u Op %s  ",
     91          1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op,
     92          1.1  christos         NumChildren, UtGetOpName(ParseOpcode));
     93          1.1  christos 
     94          1.1  christos     /* Some extra debug output based on the parse opcode */
     95          1.1  christos 
     96          1.1  christos     switch (ParseOpcode)
     97          1.1  christos     {
     98          1.1  christos     case PARSEOP_ASL_CODE:
     99          1.1  christos 
    100  1.1.1.3.2.1  christos         AslGbl_ParseTreeRoot = Op;
    101          1.1  christos         Op->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
    102          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "ASLCODE (Tree Completed)->");
    103          1.1  christos         break;
    104          1.1  christos 
    105          1.1  christos     case PARSEOP_DEFINITION_BLOCK:
    106          1.1  christos 
    107          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "DEFINITION_BLOCK (Tree Completed)->");
    108          1.1  christos         break;
    109          1.1  christos 
    110          1.1  christos     case PARSEOP_OPERATIONREGION:
    111          1.1  christos 
    112          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "OPREGION->");
    113          1.1  christos         break;
    114          1.1  christos 
    115          1.1  christos     case PARSEOP_OR:
    116          1.1  christos 
    117          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "OR->");
    118          1.1  christos         break;
    119          1.1  christos 
    120          1.1  christos     default:
    121          1.1  christos 
    122          1.1  christos         /* Nothing to do for other opcodes */
    123          1.1  christos 
    124          1.1  christos         break;
    125          1.1  christos     }
    126          1.1  christos 
    127          1.1  christos     /* Link the new op to its children */
    128          1.1  christos 
    129          1.1  christos     PrevChild = NULL;
    130          1.1  christos     FirstChild = TRUE;
    131          1.1  christos     for (i = 0; i < NumChildren; i++)
    132          1.1  christos     {
    133          1.1  christos         /* Get the next child */
    134          1.1  christos 
    135          1.1  christos         Child = va_arg (ap, ACPI_PARSE_OBJECT *);
    136          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "%p, ", Child);
    137          1.1  christos 
    138          1.1  christos         /*
    139          1.1  christos          * If child is NULL, this means that an optional argument
    140          1.1  christos          * was omitted. We must create a placeholder with a special
    141          1.1  christos          * opcode (DEFAULT_ARG) so that the code generator will know
    142          1.1  christos          * that it must emit the correct default for this argument
    143          1.1  christos          */
    144          1.1  christos         if (!Child)
    145          1.1  christos         {
    146          1.1  christos             Child = TrAllocateOp (PARSEOP_DEFAULT_ARG);
    147          1.1  christos         }
    148          1.1  christos 
    149          1.1  christos         /* Link first child to parent */
    150          1.1  christos 
    151          1.1  christos         if (FirstChild)
    152          1.1  christos         {
    153          1.1  christos             FirstChild = FALSE;
    154          1.1  christos             Op->Asl.Child = Child;
    155          1.1  christos 
    156          1.1  christos             /*
    157          1.1  christos              * For the ASL-/ASL+ converter: if the ParseOp is a Connection,
    158          1.1  christos              * External, Offset or AccessAs, it means that the comments in the
    159          1.1  christos              * FirstChild belongs to their parent due to the parsing order in
    160          1.1  christos              * the .y files. To correct this, take the comments in the
    161          1.1  christos              * FirstChild place it in the parent. This also means that
    162          1.1  christos              * legitimate comments for the child gets put to the parent.
    163          1.1  christos              */
    164      1.1.1.3  christos             if (AcpiGbl_CaptureComments &&
    165          1.1  christos                 ((ParseOpcode == PARSEOP_CONNECTION) ||
    166          1.1  christos                  (ParseOpcode == PARSEOP_EXTERNAL) ||
    167          1.1  christos                  (ParseOpcode == PARSEOP_OFFSET) ||
    168          1.1  christos                  (ParseOpcode == PARSEOP_ACCESSAS)))
    169          1.1  christos             {
    170          1.1  christos                 Op->Asl.CommentList      = Child->Asl.CommentList;
    171          1.1  christos                 Op->Asl.EndBlkComment    = Child->Asl.EndBlkComment;
    172          1.1  christos                 Op->Asl.InlineComment    = Child->Asl.InlineComment;
    173          1.1  christos                 Op->Asl.FileChanged      = Child->Asl.FileChanged;
    174          1.1  christos 
    175          1.1  christos                 Child->Asl.CommentList   = NULL;
    176          1.1  christos                 Child->Asl.EndBlkComment = NULL;
    177          1.1  christos                 Child->Asl.InlineComment = NULL;
    178          1.1  christos                 Child->Asl.FileChanged   = FALSE;
    179          1.1  christos 
    180          1.1  christos                 /*
    181          1.1  christos                  * These do not need to be "passed off". They can be copied
    182          1.1  christos                  * because the code for these opcodes should be printed in the
    183          1.1  christos                  * same file.
    184          1.1  christos                  */
    185          1.1  christos                 Op->Asl.Filename         = Child->Asl.Filename;
    186          1.1  christos                 Op->Asl.ParentFilename   = Child->Asl.ParentFilename;
    187          1.1  christos             }
    188          1.1  christos         }
    189          1.1  christos 
    190          1.1  christos         /* Point all children to parent */
    191          1.1  christos 
    192          1.1  christos         Child->Asl.Parent = Op;
    193          1.1  christos 
    194          1.1  christos         /* Link children in a peer list */
    195          1.1  christos 
    196          1.1  christos         if (PrevChild)
    197          1.1  christos         {
    198          1.1  christos             PrevChild->Asl.Next = Child;
    199          1.1  christos         };
    200          1.1  christos 
    201          1.1  christos         /* Get the comment from last child in the resource template call */
    202          1.1  christos 
    203      1.1.1.3  christos         if (AcpiGbl_CaptureComments &&
    204          1.1  christos             (Op->Asl.ParseOpcode == PARSEOP_RESOURCETEMPLATE))
    205          1.1  christos         {
    206          1.1  christos             CvDbgPrint ("Transferred current comment list to this op.\n");
    207          1.1  christos             Op->Asl.CommentList = Child->Asl.CommentList;
    208          1.1  christos             Child->Asl.CommentList = NULL;
    209          1.1  christos 
    210          1.1  christos             Op->Asl.InlineComment = Child->Asl.InlineComment;
    211          1.1  christos             Child->Asl.InlineComment = NULL;
    212          1.1  christos         }
    213          1.1  christos 
    214          1.1  christos         /*
    215          1.1  christos          * This child might be a list, point all ops in the list
    216          1.1  christos          * to the same parent
    217          1.1  christos          */
    218          1.1  christos         while (Child->Asl.Next)
    219          1.1  christos         {
    220          1.1  christos             Child = Child->Asl.Next;
    221          1.1  christos             Child->Asl.Parent = Op;
    222          1.1  christos         }
    223          1.1  christos 
    224          1.1  christos         PrevChild = Child;
    225          1.1  christos     }
    226          1.1  christos 
    227          1.1  christos     va_end(ap);
    228          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT, "\n");
    229          1.1  christos     return (Op);
    230          1.1  christos }
    231          1.1  christos 
    232          1.1  christos 
    233          1.1  christos /*******************************************************************************
    234          1.1  christos  *
    235          1.1  christos  * FUNCTION:    TrCreateLeafOp
    236          1.1  christos  *
    237          1.1  christos  * PARAMETERS:  ParseOpcode         - New opcode to be assigned to the op
    238          1.1  christos  *
    239          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    240          1.1  christos  *
    241          1.1  christos  * DESCRIPTION: Create a simple leaf op (no children or peers, and no value
    242          1.1  christos  *              assigned to the op)
    243          1.1  christos  *
    244          1.1  christos  ******************************************************************************/
    245          1.1  christos 
    246          1.1  christos ACPI_PARSE_OBJECT *
    247          1.1  christos TrCreateLeafOp (
    248          1.1  christos     UINT32                  ParseOpcode)
    249          1.1  christos {
    250          1.1  christos     ACPI_PARSE_OBJECT       *Op;
    251          1.1  christos 
    252          1.1  christos 
    253          1.1  christos     Op = TrAllocateOp (ParseOpcode);
    254          1.1  christos 
    255          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    256          1.1  christos         "\nCreateLeafOp  Ln/Col %u/%u NewOp %p  Op %s\n\n",
    257          1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName (ParseOpcode));
    258          1.1  christos 
    259          1.1  christos     return (Op);
    260          1.1  christos }
    261          1.1  christos 
    262          1.1  christos 
    263          1.1  christos /*******************************************************************************
    264          1.1  christos  *
    265          1.1  christos  * FUNCTION:    TrCreateValuedLeafOp
    266          1.1  christos  *
    267          1.1  christos  * PARAMETERS:  ParseOpcode         - New opcode to be assigned to the op
    268          1.1  christos  *              Value               - Value to be assigned to the op
    269          1.1  christos  *
    270          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    271          1.1  christos  *
    272          1.1  christos  * DESCRIPTION: Create a leaf op (no children or peers) with a value
    273          1.1  christos  *              assigned to it
    274          1.1  christos  *
    275          1.1  christos  ******************************************************************************/
    276          1.1  christos 
    277          1.1  christos ACPI_PARSE_OBJECT *
    278          1.1  christos TrCreateValuedLeafOp (
    279          1.1  christos     UINT32                  ParseOpcode,
    280          1.1  christos     UINT64                  Value)
    281          1.1  christos {
    282          1.1  christos     ACPI_PARSE_OBJECT       *Op;
    283          1.1  christos 
    284          1.1  christos 
    285          1.1  christos     Op = TrAllocateOp (ParseOpcode);
    286          1.1  christos     Op->Asl.Value.Integer = Value;
    287          1.1  christos 
    288          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    289          1.1  christos         "\nCreateValuedLeafOp  Ln/Col %u/%u NewOp %p  "
    290          1.1  christos         "Op %s  Value %8.8X%8.8X  ",
    291          1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName(ParseOpcode),
    292          1.1  christos         ACPI_FORMAT_UINT64 (Value));
    293          1.1  christos 
    294          1.1  christos     switch (ParseOpcode)
    295          1.1  christos     {
    296          1.1  christos     case PARSEOP_STRING_LITERAL:
    297          1.1  christos 
    298  1.1.1.3.2.3    martin         DbgPrint (ASL_PARSE_OUTPUT, "STRING->%s", Op->Asl.Value.String);
    299          1.1  christos         break;
    300          1.1  christos 
    301          1.1  christos     case PARSEOP_NAMESEG:
    302          1.1  christos 
    303  1.1.1.3.2.3    martin         DbgPrint (ASL_PARSE_OUTPUT, "NAMESEG->%s", Op->Asl.Value.String);
    304          1.1  christos         break;
    305          1.1  christos 
    306          1.1  christos     case PARSEOP_NAMESTRING:
    307          1.1  christos 
    308  1.1.1.3.2.3    martin         DbgPrint (ASL_PARSE_OUTPUT, "NAMESTRING->%s", Op->Asl.Value.String);
    309          1.1  christos         break;
    310          1.1  christos 
    311          1.1  christos     case PARSEOP_EISAID:
    312          1.1  christos 
    313  1.1.1.3.2.3    martin         DbgPrint (ASL_PARSE_OUTPUT, "EISAID->%s", Op->Asl.Value.String);
    314          1.1  christos         break;
    315          1.1  christos 
    316          1.1  christos     case PARSEOP_METHOD:
    317          1.1  christos 
    318          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "METHOD");
    319          1.1  christos         break;
    320          1.1  christos 
    321          1.1  christos     case PARSEOP_INTEGER:
    322          1.1  christos 
    323          1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "INTEGER->%8.8X%8.8X",
    324          1.1  christos             ACPI_FORMAT_UINT64 (Value));
    325          1.1  christos         break;
    326          1.1  christos 
    327          1.1  christos     default:
    328          1.1  christos         break;
    329          1.1  christos     }
    330          1.1  christos 
    331          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
    332          1.1  christos     return (Op);
    333          1.1  christos }
    334          1.1  christos 
    335          1.1  christos 
    336          1.1  christos /*******************************************************************************
    337          1.1  christos  *
    338          1.1  christos  * FUNCTION:    TrCreateTargetOp
    339          1.1  christos  *
    340          1.1  christos  * PARAMETERS:  OriginalOp          - Op to be copied
    341          1.1  christos  *
    342          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    343          1.1  christos  *
    344          1.1  christos  * DESCRIPTION: Copy an existing op (and subtree). Used in ASL+ (C-style)
    345          1.1  christos  *              expressions where the target is the same as one of the
    346          1.1  christos  *              operands. A new op and subtree must be created from the
    347          1.1  christos  *              original so that the parse tree can be linked properly.
    348          1.1  christos  *
    349          1.1  christos  * NOTE:        This code is specific to target operands that are the last
    350          1.1  christos  *              operand in an ASL/AML operator. Meaning that the top-level
    351          1.1  christos  *              parse Op in a possible subtree has a NULL Next pointer.
    352          1.1  christos  *              This simplifies the recursion.
    353          1.1  christos  *
    354          1.1  christos  *              Subtree example:
    355          1.1  christos  *                  DeRefOf (Local1) += 32
    356          1.1  christos  *
    357          1.1  christos  *              This gets converted to:
    358          1.1  christos  *                  Add (DeRefOf (Local1), 32, DeRefOf (Local1))
    359          1.1  christos  *
    360          1.1  christos  *              Each DeRefOf has a single child, Local1. Even more complex
    361          1.1  christos  *              subtrees can be created via the Index and DeRefOf operators.
    362          1.1  christos  *
    363          1.1  christos  ******************************************************************************/
    364          1.1  christos 
    365          1.1  christos ACPI_PARSE_OBJECT *
    366          1.1  christos TrCreateTargetOp (
    367          1.1  christos     ACPI_PARSE_OBJECT       *OriginalOp,
    368          1.1  christos     ACPI_PARSE_OBJECT       *ParentOp)
    369          1.1  christos {
    370          1.1  christos     ACPI_PARSE_OBJECT       *Op;
    371          1.1  christos 
    372          1.1  christos 
    373          1.1  christos     if (!OriginalOp)
    374          1.1  christos     {
    375          1.1  christos         return (NULL);
    376          1.1  christos     }
    377          1.1  christos 
    378      1.1.1.2  christos     Op = UtParseOpCacheCalloc ();
    379          1.1  christos 
    380          1.1  christos     /* Copy the pertinent values (omit link pointer fields) */
    381          1.1  christos 
    382          1.1  christos     Op->Asl.Value               = OriginalOp->Asl.Value;
    383          1.1  christos     Op->Asl.Filename            = OriginalOp->Asl.Filename;
    384          1.1  christos     Op->Asl.LineNumber          = OriginalOp->Asl.LineNumber;
    385          1.1  christos     Op->Asl.LogicalLineNumber   = OriginalOp->Asl.LogicalLineNumber;
    386          1.1  christos     Op->Asl.LogicalByteOffset   = OriginalOp->Asl.LogicalByteOffset;
    387          1.1  christos     Op->Asl.Column              = OriginalOp->Asl.Column;
    388          1.1  christos     Op->Asl.Flags               = OriginalOp->Asl.Flags;
    389          1.1  christos     Op->Asl.CompileFlags        = OriginalOp->Asl.CompileFlags;
    390          1.1  christos     Op->Asl.AmlOpcode           = OriginalOp->Asl.AmlOpcode;
    391          1.1  christos     Op->Asl.ParseOpcode         = OriginalOp->Asl.ParseOpcode;
    392          1.1  christos     Op->Asl.Parent              = ParentOp;
    393          1.1  christos 
    394          1.1  christos     UtSetParseOpName (Op);
    395          1.1  christos 
    396          1.1  christos     /* Copy a possible subtree below this op */
    397          1.1  christos 
    398          1.1  christos     if (OriginalOp->Asl.Child)
    399          1.1  christos     {
    400          1.1  christos         Op->Asl.Child = TrCreateTargetOp (OriginalOp->Asl.Child, Op);
    401          1.1  christos     }
    402          1.1  christos 
    403          1.1  christos     if (OriginalOp->Asl.Next) /* Null for top-level op */
    404          1.1  christos     {
    405          1.1  christos         Op->Asl.Next = TrCreateTargetOp (OriginalOp->Asl.Next, ParentOp);
    406          1.1  christos     }
    407          1.1  christos 
    408          1.1  christos     return (Op);
    409          1.1  christos }
    410          1.1  christos 
    411          1.1  christos 
    412          1.1  christos /*******************************************************************************
    413          1.1  christos  *
    414          1.1  christos  * FUNCTION:    TrCreateAssignmentOp
    415          1.1  christos  *
    416          1.1  christos  * PARAMETERS:  Target              - Assignment target
    417          1.1  christos  *              Source              - Assignment source
    418          1.1  christos  *
    419          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    420          1.1  christos  *
    421          1.1  christos  * DESCRIPTION: Implements the C-style '=' operator. It changes the parse
    422          1.1  christos  *              tree if possible to utilize the last argument of the math
    423          1.1  christos  *              operators which is a target operand -- thus saving invocation
    424          1.1  christos  *              of and additional Store() operator. An optimization.
    425          1.1  christos  *
    426          1.1  christos  ******************************************************************************/
    427          1.1  christos 
    428          1.1  christos ACPI_PARSE_OBJECT *
    429          1.1  christos TrCreateAssignmentOp (
    430          1.1  christos     ACPI_PARSE_OBJECT       *Target,
    431          1.1  christos     ACPI_PARSE_OBJECT       *Source)
    432          1.1  christos {
    433          1.1  christos     ACPI_PARSE_OBJECT       *TargetOp;
    434          1.1  christos     ACPI_PARSE_OBJECT       *SourceOp1;
    435          1.1  christos     ACPI_PARSE_OBJECT       *SourceOp2;
    436          1.1  christos     ACPI_PARSE_OBJECT       *Operator;
    437          1.1  christos 
    438          1.1  christos 
    439          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    440          1.1  christos         "\nTrCreateAssignmentOp  Line [%u to %u] Source %s Target %s\n",
    441          1.1  christos         Source->Asl.LineNumber, Source->Asl.EndLine,
    442          1.1  christos         UtGetOpName (Source->Asl.ParseOpcode),
    443          1.1  christos         UtGetOpName (Target->Asl.ParseOpcode));
    444          1.1  christos 
    445          1.1  christos     TrSetOpFlags (Target, OP_IS_TARGET);
    446          1.1  christos 
    447          1.1  christos     switch (Source->Asl.ParseOpcode)
    448          1.1  christos     {
    449          1.1  christos     /*
    450          1.1  christos      * Only these operators can be optimized because they have
    451          1.1  christos      * a target operand
    452          1.1  christos      */
    453          1.1  christos     case PARSEOP_ADD:
    454          1.1  christos     case PARSEOP_AND:
    455          1.1  christos     case PARSEOP_DIVIDE:
    456          1.1  christos     case PARSEOP_INDEX:
    457          1.1  christos     case PARSEOP_MOD:
    458          1.1  christos     case PARSEOP_MULTIPLY:
    459          1.1  christos     case PARSEOP_NOT:
    460          1.1  christos     case PARSEOP_OR:
    461          1.1  christos     case PARSEOP_SHIFTLEFT:
    462          1.1  christos     case PARSEOP_SHIFTRIGHT:
    463          1.1  christos     case PARSEOP_SUBTRACT:
    464          1.1  christos     case PARSEOP_XOR:
    465          1.1  christos 
    466          1.1  christos         break;
    467          1.1  christos 
    468          1.1  christos     /* Otherwise, just create a normal Store operator */
    469          1.1  christos 
    470          1.1  christos     default:
    471          1.1  christos         goto CannotOptimize;
    472          1.1  christos     }
    473          1.1  christos 
    474          1.1  christos     /*
    475          1.1  christos      * Transform the parse tree such that the target is moved to the
    476          1.1  christos      * last operand of the operator
    477          1.1  christos      */
    478          1.1  christos     SourceOp1 = Source->Asl.Child;
    479          1.1  christos     SourceOp2 = SourceOp1->Asl.Next;
    480          1.1  christos 
    481          1.1  christos     /* NOT only has one operand, but has a target */
    482          1.1  christos 
    483          1.1  christos     if (Source->Asl.ParseOpcode == PARSEOP_NOT)
    484          1.1  christos     {
    485          1.1  christos         SourceOp2 = SourceOp1;
    486          1.1  christos     }
    487          1.1  christos 
    488          1.1  christos     /* DIVIDE has an extra target operand (remainder) */
    489          1.1  christos 
    490          1.1  christos     if (Source->Asl.ParseOpcode == PARSEOP_DIVIDE)
    491          1.1  christos     {
    492          1.1  christos         SourceOp2 = SourceOp2->Asl.Next;
    493          1.1  christos     }
    494          1.1  christos 
    495          1.1  christos     TargetOp = SourceOp2->Asl.Next;
    496          1.1  christos 
    497          1.1  christos     /*
    498          1.1  christos      * Can't perform this optimization if there already is a target
    499          1.1  christos      * for the operator (ZERO is a "no target" placeholder).
    500          1.1  christos      */
    501          1.1  christos     if (TargetOp->Asl.ParseOpcode != PARSEOP_ZERO)
    502          1.1  christos     {
    503          1.1  christos         goto CannotOptimize;
    504          1.1  christos     }
    505          1.1  christos 
    506          1.1  christos     /* Link in the target as the final operand */
    507          1.1  christos 
    508          1.1  christos     SourceOp2->Asl.Next = Target;
    509          1.1  christos     Target->Asl.Parent = Source;
    510          1.1  christos     return (Source);
    511          1.1  christos 
    512          1.1  christos 
    513          1.1  christos CannotOptimize:
    514          1.1  christos 
    515          1.1  christos     Operator = TrAllocateOp (PARSEOP_STORE);
    516          1.1  christos     TrLinkOpChildren (Operator, 2, Source, Target);
    517          1.1  christos 
    518          1.1  christos     /* Set the appropriate line numbers for the new op */
    519          1.1  christos 
    520          1.1  christos     Operator->Asl.LineNumber        = Target->Asl.LineNumber;
    521          1.1  christos     Operator->Asl.LogicalLineNumber = Target->Asl.LogicalLineNumber;
    522          1.1  christos     Operator->Asl.LogicalByteOffset = Target->Asl.LogicalByteOffset;
    523          1.1  christos     Operator->Asl.Column            = Target->Asl.Column;
    524          1.1  christos 
    525          1.1  christos     return (Operator);
    526          1.1  christos }
    527          1.1  christos 
    528          1.1  christos 
    529          1.1  christos /*******************************************************************************
    530          1.1  christos  *
    531          1.1  christos  * FUNCTION:    TrCreateNullTargetOp
    532          1.1  christos  *
    533          1.1  christos  * PARAMETERS:  None
    534          1.1  christos  *
    535          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    536          1.1  christos  *
    537          1.1  christos  * DESCRIPTION: Create a "null" target op. This is defined by the ACPI
    538          1.1  christos  *              specification to be a zero AML opcode, and indicates that
    539          1.1  christos  *              no target has been specified for the parent operation
    540          1.1  christos  *
    541          1.1  christos  ******************************************************************************/
    542          1.1  christos 
    543          1.1  christos ACPI_PARSE_OBJECT *
    544          1.1  christos TrCreateNullTargetOp (
    545          1.1  christos     void)
    546          1.1  christos {
    547          1.1  christos     ACPI_PARSE_OBJECT       *Op;
    548          1.1  christos 
    549          1.1  christos 
    550          1.1  christos     Op = TrAllocateOp (PARSEOP_ZERO);
    551          1.1  christos     Op->Asl.CompileFlags |= (OP_IS_TARGET | OP_COMPILE_TIME_CONST);
    552          1.1  christos 
    553          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    554          1.1  christos         "\nCreateNullTargetOp  Ln/Col %u/%u NewOp %p  Op %s\n",
    555          1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op,
    556          1.1  christos         UtGetOpName (Op->Asl.ParseOpcode));
    557          1.1  christos 
    558          1.1  christos     return (Op);
    559          1.1  christos }
    560          1.1  christos 
    561          1.1  christos 
    562          1.1  christos /*******************************************************************************
    563          1.1  christos  *
    564          1.1  christos  * FUNCTION:    TrCreateConstantLeafOp
    565          1.1  christos  *
    566          1.1  christos  * PARAMETERS:  ParseOpcode         - The constant opcode
    567          1.1  christos  *
    568          1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    569          1.1  christos  *
    570          1.1  christos  * DESCRIPTION: Create a leaf op (no children or peers) for one of the
    571          1.1  christos  *              special constants - __LINE__, __FILE__, and __DATE__.
    572          1.1  christos  *
    573          1.1  christos  * Note: The fullimplemenation of __METHOD__ cannot happen here because we
    574          1.1  christos  * don't have a full parse tree at this time and cannot find the parent
    575          1.1  christos  * control method. __METHOD__ must be implemented later, after the parse
    576          1.1  christos  * tree has been fully constructed.
    577          1.1  christos  *
    578          1.1  christos  ******************************************************************************/
    579          1.1  christos 
    580          1.1  christos ACPI_PARSE_OBJECT *
    581          1.1  christos TrCreateConstantLeafOp (
    582          1.1  christos     UINT32                  ParseOpcode)
    583          1.1  christos {
    584          1.1  christos     ACPI_PARSE_OBJECT       *Op = NULL;
    585          1.1  christos     time_t                  CurrentTime;
    586          1.1  christos     char                    *StaticTimeString;
    587          1.1  christos     char                    *TimeString;
    588  1.1.1.3.2.3    martin     char                    *Filename = NULL;
    589  1.1.1.3.2.3    martin     ACPI_STATUS             Status;
    590          1.1  christos 
    591          1.1  christos 
    592          1.1  christos     switch (ParseOpcode)
    593          1.1  christos     {
    594          1.1  christos     case PARSEOP___LINE__:
    595          1.1  christos 
    596          1.1  christos         Op = TrAllocateOp (PARSEOP_INTEGER);
    597          1.1  christos         Op->Asl.Value.Integer = Op->Asl.LineNumber;
    598          1.1  christos         break;
    599          1.1  christos 
    600          1.1  christos     case PARSEOP___METHOD__:
    601          1.1  christos 
    602          1.1  christos         /* Will become a string literal later */
    603          1.1  christos 
    604          1.1  christos         Op = TrAllocateOp (PARSEOP___METHOD__);
    605          1.1  christos         Op->Asl.Value.String = NULL;
    606          1.1  christos         break;
    607          1.1  christos 
    608          1.1  christos     case PARSEOP___PATH__:
    609          1.1  christos 
    610          1.1  christos         Op = TrAllocateOp (PARSEOP_STRING_LITERAL);
    611          1.1  christos 
    612          1.1  christos         /* Op.Asl.Filename contains the full pathname to the file */
    613          1.1  christos 
    614          1.1  christos         Op->Asl.Value.String = Op->Asl.Filename;
    615          1.1  christos         break;
    616          1.1  christos 
    617          1.1  christos     case PARSEOP___FILE__:
    618          1.1  christos 
    619          1.1  christos         Op = TrAllocateOp (PARSEOP_STRING_LITERAL);
    620          1.1  christos 
    621          1.1  christos         /* Get the simple filename from the full path */
    622          1.1  christos 
    623  1.1.1.3.2.3    martin         Status = FlSplitInputPathname (Op->Asl.Filename, NULL, &Filename);
    624  1.1.1.3.2.3    martin         if (ACPI_FAILURE (Status))
    625  1.1.1.3.2.3    martin         {
    626  1.1.1.3.2.3    martin             return (NULL);
    627  1.1.1.3.2.3    martin         }
    628  1.1.1.3.2.3    martin 
    629          1.1  christos         Op->Asl.Value.String = Filename;
    630          1.1  christos         break;
    631          1.1  christos 
    632          1.1  christos     case PARSEOP___DATE__:
    633          1.1  christos 
    634          1.1  christos         Op = TrAllocateOp (PARSEOP_STRING_LITERAL);
    635          1.1  christos 
    636          1.1  christos         /* Get a copy of the current time */
    637          1.1  christos 
    638  1.1.1.3.2.2    martin         Op->Asl.Value.String = "";
    639          1.1  christos         CurrentTime = time (NULL);
    640  1.1.1.3.2.2    martin 
    641          1.1  christos         StaticTimeString = ctime (&CurrentTime);
    642  1.1.1.3.2.2    martin         if (StaticTimeString)
    643  1.1.1.3.2.2    martin         {
    644  1.1.1.3.2.2    martin             TimeString = UtLocalCalloc (strlen (StaticTimeString) + 1);
    645  1.1.1.3.2.2    martin             strcpy (TimeString, StaticTimeString);
    646          1.1  christos 
    647  1.1.1.3.2.2    martin             TimeString[strlen(TimeString) -1] = 0;  /* Remove trailing newline */
    648  1.1.1.3.2.2    martin             Op->Asl.Value.String = TimeString;
    649  1.1.1.3.2.2    martin         }
    650          1.1  christos         break;
    651          1.1  christos 
    652          1.1  christos     default: /* This would be an internal error */
    653          1.1  christos 
    654          1.1  christos         return (NULL);
    655          1.1  christos     }
    656          1.1  christos 
    657          1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    658          1.1  christos         "\nCreateConstantLeafOp  Ln/Col %u/%u NewOp %p  "
    659          1.1  christos         "Op %s  Value %8.8X%8.8X  \n",
    660          1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName (ParseOpcode),
    661          1.1  christos         ACPI_FORMAT_UINT64 (Op->Asl.Value.Integer));
    662          1.1  christos 
    663          1.1  christos     return (Op);
    664          1.1  christos }
    665          1.1  christos 
    666          1.1  christos 
    667          1.1  christos /*******************************************************************************
    668          1.1  christos  *
    669          1.1  christos  * FUNCTION:    TrAllocateOp
    670          1.1  christos  *
    671          1.1  christos  * PARAMETERS:  ParseOpcode         - Opcode to be assigned to the op
    672          1.1  christos  *
    673          1.1  christos  * RETURN:      New parse op. Aborts on allocation failure
    674          1.1  christos  *
    675          1.1  christos  * DESCRIPTION: Allocate and initialize a new parse op for the parse tree
    676          1.1  christos  *
    677          1.1  christos  ******************************************************************************/
    678          1.1  christos 
    679          1.1  christos ACPI_PARSE_OBJECT *
    680          1.1  christos TrAllocateOp (
    681          1.1  christos     UINT32                  ParseOpcode)
    682          1.1  christos {
    683          1.1  christos     ACPI_PARSE_OBJECT       *Op;
    684          1.1  christos     ACPI_PARSE_OBJECT       *LatestOp;
    685          1.1  christos 
    686          1.1  christos 
    687      1.1.1.2  christos     Op = UtParseOpCacheCalloc ();
    688          1.1  christos 
    689          1.1  christos     Op->Asl.ParseOpcode       = (UINT16) ParseOpcode;
    690  1.1.1.3.2.1  christos     Op->Asl.Filename          = AslGbl_Files[ASL_FILE_INPUT].Filename;
    691  1.1.1.3.2.1  christos     Op->Asl.LineNumber        = AslGbl_CurrentLineNumber;
    692  1.1.1.3.2.1  christos     Op->Asl.LogicalLineNumber = AslGbl_LogicalLineNumber;
    693  1.1.1.3.2.1  christos     Op->Asl.LogicalByteOffset = AslGbl_CurrentLineOffset;
    694  1.1.1.3.2.1  christos     Op->Asl.Column            = AslGbl_CurrentColumn;
    695          1.1  christos 
    696          1.1  christos     UtSetParseOpName (Op);
    697          1.1  christos 
    698          1.1  christos     /* The following is for capturing comments */
    699          1.1  christos 
    700      1.1.1.3  christos     if (AcpiGbl_CaptureComments)
    701          1.1  christos     {
    702  1.1.1.3.2.1  christos         LatestOp = AslGbl_CommentState.LatestParseOp;
    703          1.1  christos         Op->Asl.InlineComment     = NULL;
    704          1.1  christos         Op->Asl.EndNodeComment    = NULL;
    705          1.1  christos         Op->Asl.CommentList       = NULL;
    706          1.1  christos         Op->Asl.FileChanged       = FALSE;
    707          1.1  christos 
    708          1.1  christos         /*
    709          1.1  christos          * Check to see if the file name has changed before resetting the
    710          1.1  christos          * latest parse op.
    711          1.1  christos          */
    712          1.1  christos         if (LatestOp &&
    713          1.1  christos             (ParseOpcode != PARSEOP_INCLUDE) &&
    714          1.1  christos             (ParseOpcode != PARSEOP_INCLUDE_END) &&
    715          1.1  christos             strcmp (LatestOp->Asl.Filename, Op->Asl.Filename))
    716          1.1  christos         {
    717          1.1  christos             CvDbgPrint ("latest op: %s\n", LatestOp->Asl.ParseOpName);
    718          1.1  christos             Op->Asl.FileChanged = TRUE;
    719  1.1.1.3.2.1  christos             if (AslGbl_IncludeFileStack)
    720          1.1  christos             {
    721  1.1.1.3.2.1  christos                 Op->Asl.ParentFilename = AslGbl_IncludeFileStack->Filename;
    722          1.1  christos             }
    723          1.1  christos             else
    724          1.1  christos             {
    725          1.1  christos                 Op->Asl.ParentFilename = NULL;
    726          1.1  christos             }
    727          1.1  christos         }
    728          1.1  christos 
    729  1.1.1.3.2.1  christos         AslGbl_CommentState.LatestParseOp = Op;
    730          1.1  christos         CvDbgPrint ("TrAllocateOp=Set latest parse op to this op.\n");
    731          1.1  christos         CvDbgPrint ("           Op->Asl.ParseOpName = %s\n",
    732  1.1.1.3.2.1  christos             AslGbl_CommentState.LatestParseOp->Asl.ParseOpName);
    733          1.1  christos         CvDbgPrint ("           Op->Asl.ParseOpcode = 0x%x\n", ParseOpcode);
    734          1.1  christos 
    735          1.1  christos         if (Op->Asl.FileChanged)
    736          1.1  christos         {
    737          1.1  christos             CvDbgPrint("    file has been changed!\n");
    738          1.1  christos         }
    739          1.1  christos 
    740          1.1  christos         /*
    741          1.1  christos          * if this parse op's syntax uses () and {} (i.e. Package(1){0x00}) then
    742          1.1  christos          * set a flag in the comment state. This facilitates paring comments for
    743          1.1  christos          * these types of opcodes.
    744          1.1  christos          */
    745          1.1  christos         if ((CvParseOpBlockType(Op) == (BLOCK_PAREN | BLOCK_BRACE)) &&
    746          1.1  christos             (ParseOpcode != PARSEOP_DEFINITION_BLOCK))
    747          1.1  christos         {
    748          1.1  christos             CvDbgPrint ("Parsing paren/Brace op now!\n");
    749  1.1.1.3.2.1  christos             AslGbl_CommentState.ParsingParenBraceNode = Op;
    750          1.1  christos         }
    751          1.1  christos 
    752  1.1.1.3.2.1  christos         if (AslGbl_CommentListHead)
    753          1.1  christos         {
    754          1.1  christos             CvDbgPrint ("Transferring...\n");
    755  1.1.1.3.2.1  christos             Op->Asl.CommentList = AslGbl_CommentListHead;
    756  1.1.1.3.2.1  christos             AslGbl_CommentListHead = NULL;
    757  1.1.1.3.2.1  christos             AslGbl_CommentListTail = NULL;
    758          1.1  christos             CvDbgPrint ("    Transferred current comment list to this op.\n");
    759          1.1  christos             CvDbgPrint ("    %s\n", Op->Asl.CommentList->Comment);
    760          1.1  christos         }
    761          1.1  christos 
    762  1.1.1.3.2.1  christos         if (AslGbl_InlineCommentBuffer)
    763          1.1  christos         {
    764  1.1.1.3.2.1  christos             Op->Asl.InlineComment = AslGbl_InlineCommentBuffer;
    765  1.1.1.3.2.1  christos             AslGbl_InlineCommentBuffer = NULL;
    766          1.1  christos             CvDbgPrint ("Transferred current inline comment list to this op.\n");
    767          1.1  christos         }
    768          1.1  christos     }
    769          1.1  christos 
    770          1.1  christos     return (Op);
    771          1.1  christos }
    772          1.1  christos 
    773          1.1  christos 
    774          1.1  christos /*******************************************************************************
    775          1.1  christos  *
    776          1.1  christos  * FUNCTION:    TrPrintOpFlags
    777          1.1  christos  *
    778          1.1  christos  * PARAMETERS:  Flags               - Flags word to be decoded
    779          1.1  christos  *              OutputLevel         - Debug output level: ASL_TREE_OUTPUT etc.
    780          1.1  christos  *
    781          1.1  christos  * RETURN:      None
    782          1.1  christos  *
    783          1.1  christos  * DESCRIPTION: Decode a flags word to text. Displays all flags that are set.
    784          1.1  christos  *
    785          1.1  christos  ******************************************************************************/
    786          1.1  christos 
    787          1.1  christos void
    788          1.1  christos TrPrintOpFlags (
    789          1.1  christos     UINT32                  Flags,
    790          1.1  christos     UINT32                  OutputLevel)
    791          1.1  christos {
    792          1.1  christos     UINT32                  FlagBit = 1;
    793          1.1  christos     UINT32                  i;
    794          1.1  christos 
    795          1.1  christos 
    796          1.1  christos     for (i = 0; i < ACPI_NUM_OP_FLAGS; i++)
    797          1.1  christos     {
    798          1.1  christos         if (Flags & FlagBit)
    799          1.1  christos         {
    800  1.1.1.3.2.1  christos             DbgPrint (OutputLevel, " %s", AslGbl_OpFlagNames[i]);
    801          1.1  christos         }
    802          1.1  christos 
    803          1.1  christos         FlagBit <<= 1;
    804          1.1  christos     }
    805          1.1  christos }
    806