Home | History | Annotate | Line # | Download | only in compiler
aslparseop.c revision 1.1.1.9
      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.9  christos  * Copyright (C) 2000 - 2021, Intel Corp.
      9      1.1  christos  * All rights reserved.
     10      1.1  christos  *
     11      1.1  christos  * Redistribution and use in source and binary forms, with or without
     12      1.1  christos  * modification, are permitted provided that the following conditions
     13      1.1  christos  * are met:
     14      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15      1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16      1.1  christos  *    without modification.
     17      1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21      1.1  christos  *    binary redistribution.
     22      1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24      1.1  christos  *    from this software without specific prior written permission.
     25      1.1  christos  *
     26      1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27      1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.1  christos  * Software Foundation.
     29      1.1  christos  *
     30      1.1  christos  * NO WARRANTY
     31      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.9  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1  christos  */
     43      1.1  christos 
     44      1.1  christos #include "aslcompiler.h"
     45      1.1  christos #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.4  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.1.8  christos     UINT32                  i;
    284  1.1.1.8  christos     char                    *StringPtr = NULL;
    285      1.1  christos 
    286      1.1  christos 
    287      1.1  christos     Op = TrAllocateOp (ParseOpcode);
    288      1.1  christos     Op->Asl.Value.Integer = Value;
    289      1.1  christos 
    290      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    291      1.1  christos         "\nCreateValuedLeafOp  Ln/Col %u/%u NewOp %p  "
    292      1.1  christos         "Op %s  Value %8.8X%8.8X  ",
    293      1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName(ParseOpcode),
    294      1.1  christos         ACPI_FORMAT_UINT64 (Value));
    295      1.1  christos 
    296      1.1  christos     switch (ParseOpcode)
    297      1.1  christos     {
    298      1.1  christos     case PARSEOP_STRING_LITERAL:
    299      1.1  christos 
    300  1.1.1.6  christos         DbgPrint (ASL_PARSE_OUTPUT, "STRING->%s", Op->Asl.Value.String);
    301      1.1  christos         break;
    302      1.1  christos 
    303      1.1  christos     case PARSEOP_NAMESEG:
    304      1.1  christos 
    305  1.1.1.8  christos         /* Check for mixed case (or all lower case). Issue a remark in this case */
    306  1.1.1.8  christos 
    307  1.1.1.8  christos         for (i = 0; i < ACPI_NAMESEG_SIZE; i++)
    308  1.1.1.8  christos         {
    309  1.1.1.9  christos             if (islower ((int) Op->Asl.Value.Name[i]))
    310  1.1.1.8  christos             {
    311  1.1.1.8  christos                 AcpiUtStrupr (&Op->Asl.Value.Name[i]);
    312  1.1.1.8  christos                 AslError (ASL_REMARK, ASL_MSG_LOWER_CASE_NAMESEG, Op, Op->Asl.Value.Name);
    313  1.1.1.8  christos                 break;
    314  1.1.1.8  christos             }
    315  1.1.1.8  christos         }
    316  1.1.1.6  christos         DbgPrint (ASL_PARSE_OUTPUT, "NAMESEG->%s", Op->Asl.Value.String);
    317      1.1  christos         break;
    318      1.1  christos 
    319      1.1  christos     case PARSEOP_NAMESTRING:
    320      1.1  christos 
    321  1.1.1.8  christos         /* Check for mixed case (or all lower case). Issue a remark in this case */
    322  1.1.1.8  christos 
    323  1.1.1.8  christos         StringPtr = Op->Asl.Value.Name;
    324  1.1.1.8  christos         for (i = 0; *StringPtr; i++)
    325  1.1.1.8  christos         {
    326  1.1.1.9  christos             if (islower ((int) *StringPtr))
    327  1.1.1.8  christos             {
    328  1.1.1.8  christos                 AcpiUtStrupr (&Op->Asl.Value.Name[i]);
    329  1.1.1.8  christos                 AslError (ASL_REMARK, ASL_MSG_LOWER_CASE_NAMEPATH, Op, Op->Asl.Value.Name);
    330  1.1.1.8  christos                 break;
    331  1.1.1.8  christos             }
    332  1.1.1.8  christos             StringPtr++;
    333  1.1.1.8  christos         }
    334  1.1.1.6  christos         DbgPrint (ASL_PARSE_OUTPUT, "NAMESTRING->%s", Op->Asl.Value.String);
    335      1.1  christos         break;
    336      1.1  christos 
    337      1.1  christos     case PARSEOP_EISAID:
    338      1.1  christos 
    339  1.1.1.6  christos         DbgPrint (ASL_PARSE_OUTPUT, "EISAID->%s", Op->Asl.Value.String);
    340      1.1  christos         break;
    341      1.1  christos 
    342      1.1  christos     case PARSEOP_METHOD:
    343      1.1  christos 
    344      1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "METHOD");
    345      1.1  christos         break;
    346      1.1  christos 
    347      1.1  christos     case PARSEOP_INTEGER:
    348      1.1  christos 
    349      1.1  christos         DbgPrint (ASL_PARSE_OUTPUT, "INTEGER->%8.8X%8.8X",
    350      1.1  christos             ACPI_FORMAT_UINT64 (Value));
    351      1.1  christos         break;
    352      1.1  christos 
    353      1.1  christos     default:
    354      1.1  christos         break;
    355      1.1  christos     }
    356      1.1  christos 
    357      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT, "\n\n");
    358      1.1  christos     return (Op);
    359      1.1  christos }
    360      1.1  christos 
    361      1.1  christos 
    362      1.1  christos /*******************************************************************************
    363      1.1  christos  *
    364      1.1  christos  * FUNCTION:    TrCreateTargetOp
    365      1.1  christos  *
    366      1.1  christos  * PARAMETERS:  OriginalOp          - Op to be copied
    367      1.1  christos  *
    368      1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    369      1.1  christos  *
    370      1.1  christos  * DESCRIPTION: Copy an existing op (and subtree). Used in ASL+ (C-style)
    371      1.1  christos  *              expressions where the target is the same as one of the
    372      1.1  christos  *              operands. A new op and subtree must be created from the
    373      1.1  christos  *              original so that the parse tree can be linked properly.
    374      1.1  christos  *
    375      1.1  christos  * NOTE:        This code is specific to target operands that are the last
    376      1.1  christos  *              operand in an ASL/AML operator. Meaning that the top-level
    377      1.1  christos  *              parse Op in a possible subtree has a NULL Next pointer.
    378      1.1  christos  *              This simplifies the recursion.
    379      1.1  christos  *
    380      1.1  christos  *              Subtree example:
    381      1.1  christos  *                  DeRefOf (Local1) += 32
    382      1.1  christos  *
    383      1.1  christos  *              This gets converted to:
    384      1.1  christos  *                  Add (DeRefOf (Local1), 32, DeRefOf (Local1))
    385      1.1  christos  *
    386      1.1  christos  *              Each DeRefOf has a single child, Local1. Even more complex
    387      1.1  christos  *              subtrees can be created via the Index and DeRefOf operators.
    388      1.1  christos  *
    389      1.1  christos  ******************************************************************************/
    390      1.1  christos 
    391      1.1  christos ACPI_PARSE_OBJECT *
    392      1.1  christos TrCreateTargetOp (
    393      1.1  christos     ACPI_PARSE_OBJECT       *OriginalOp,
    394      1.1  christos     ACPI_PARSE_OBJECT       *ParentOp)
    395      1.1  christos {
    396      1.1  christos     ACPI_PARSE_OBJECT       *Op;
    397      1.1  christos 
    398      1.1  christos 
    399      1.1  christos     if (!OriginalOp)
    400      1.1  christos     {
    401      1.1  christos         return (NULL);
    402      1.1  christos     }
    403      1.1  christos 
    404  1.1.1.2  christos     Op = UtParseOpCacheCalloc ();
    405      1.1  christos 
    406      1.1  christos     /* Copy the pertinent values (omit link pointer fields) */
    407      1.1  christos 
    408      1.1  christos     Op->Asl.Value               = OriginalOp->Asl.Value;
    409      1.1  christos     Op->Asl.Filename            = OriginalOp->Asl.Filename;
    410      1.1  christos     Op->Asl.LineNumber          = OriginalOp->Asl.LineNumber;
    411      1.1  christos     Op->Asl.LogicalLineNumber   = OriginalOp->Asl.LogicalLineNumber;
    412      1.1  christos     Op->Asl.LogicalByteOffset   = OriginalOp->Asl.LogicalByteOffset;
    413      1.1  christos     Op->Asl.Column              = OriginalOp->Asl.Column;
    414      1.1  christos     Op->Asl.Flags               = OriginalOp->Asl.Flags;
    415      1.1  christos     Op->Asl.CompileFlags        = OriginalOp->Asl.CompileFlags;
    416      1.1  christos     Op->Asl.AmlOpcode           = OriginalOp->Asl.AmlOpcode;
    417      1.1  christos     Op->Asl.ParseOpcode         = OriginalOp->Asl.ParseOpcode;
    418      1.1  christos     Op->Asl.Parent              = ParentOp;
    419      1.1  christos 
    420      1.1  christos     UtSetParseOpName (Op);
    421      1.1  christos 
    422      1.1  christos     /* Copy a possible subtree below this op */
    423      1.1  christos 
    424      1.1  christos     if (OriginalOp->Asl.Child)
    425      1.1  christos     {
    426      1.1  christos         Op->Asl.Child = TrCreateTargetOp (OriginalOp->Asl.Child, Op);
    427      1.1  christos     }
    428      1.1  christos 
    429      1.1  christos     if (OriginalOp->Asl.Next) /* Null for top-level op */
    430      1.1  christos     {
    431      1.1  christos         Op->Asl.Next = TrCreateTargetOp (OriginalOp->Asl.Next, ParentOp);
    432      1.1  christos     }
    433      1.1  christos 
    434      1.1  christos     return (Op);
    435      1.1  christos }
    436      1.1  christos 
    437      1.1  christos 
    438      1.1  christos /*******************************************************************************
    439      1.1  christos  *
    440      1.1  christos  * FUNCTION:    TrCreateAssignmentOp
    441      1.1  christos  *
    442      1.1  christos  * PARAMETERS:  Target              - Assignment target
    443      1.1  christos  *              Source              - Assignment source
    444      1.1  christos  *
    445      1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    446      1.1  christos  *
    447      1.1  christos  * DESCRIPTION: Implements the C-style '=' operator. It changes the parse
    448      1.1  christos  *              tree if possible to utilize the last argument of the math
    449      1.1  christos  *              operators which is a target operand -- thus saving invocation
    450      1.1  christos  *              of and additional Store() operator. An optimization.
    451      1.1  christos  *
    452      1.1  christos  ******************************************************************************/
    453      1.1  christos 
    454      1.1  christos ACPI_PARSE_OBJECT *
    455      1.1  christos TrCreateAssignmentOp (
    456      1.1  christos     ACPI_PARSE_OBJECT       *Target,
    457      1.1  christos     ACPI_PARSE_OBJECT       *Source)
    458      1.1  christos {
    459      1.1  christos     ACPI_PARSE_OBJECT       *TargetOp;
    460      1.1  christos     ACPI_PARSE_OBJECT       *SourceOp1;
    461      1.1  christos     ACPI_PARSE_OBJECT       *SourceOp2;
    462      1.1  christos     ACPI_PARSE_OBJECT       *Operator;
    463      1.1  christos 
    464      1.1  christos 
    465      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    466      1.1  christos         "\nTrCreateAssignmentOp  Line [%u to %u] Source %s Target %s\n",
    467      1.1  christos         Source->Asl.LineNumber, Source->Asl.EndLine,
    468      1.1  christos         UtGetOpName (Source->Asl.ParseOpcode),
    469      1.1  christos         UtGetOpName (Target->Asl.ParseOpcode));
    470      1.1  christos 
    471      1.1  christos     TrSetOpFlags (Target, OP_IS_TARGET);
    472      1.1  christos 
    473      1.1  christos     switch (Source->Asl.ParseOpcode)
    474      1.1  christos     {
    475      1.1  christos     /*
    476      1.1  christos      * Only these operators can be optimized because they have
    477      1.1  christos      * a target operand
    478      1.1  christos      */
    479      1.1  christos     case PARSEOP_ADD:
    480      1.1  christos     case PARSEOP_AND:
    481      1.1  christos     case PARSEOP_DIVIDE:
    482      1.1  christos     case PARSEOP_INDEX:
    483      1.1  christos     case PARSEOP_MOD:
    484      1.1  christos     case PARSEOP_MULTIPLY:
    485      1.1  christos     case PARSEOP_NOT:
    486      1.1  christos     case PARSEOP_OR:
    487      1.1  christos     case PARSEOP_SHIFTLEFT:
    488      1.1  christos     case PARSEOP_SHIFTRIGHT:
    489      1.1  christos     case PARSEOP_SUBTRACT:
    490      1.1  christos     case PARSEOP_XOR:
    491      1.1  christos 
    492      1.1  christos         break;
    493      1.1  christos 
    494      1.1  christos     /* Otherwise, just create a normal Store operator */
    495      1.1  christos 
    496      1.1  christos     default:
    497      1.1  christos         goto CannotOptimize;
    498      1.1  christos     }
    499      1.1  christos 
    500      1.1  christos     /*
    501      1.1  christos      * Transform the parse tree such that the target is moved to the
    502      1.1  christos      * last operand of the operator
    503      1.1  christos      */
    504      1.1  christos     SourceOp1 = Source->Asl.Child;
    505      1.1  christos     SourceOp2 = SourceOp1->Asl.Next;
    506      1.1  christos 
    507      1.1  christos     /* NOT only has one operand, but has a target */
    508      1.1  christos 
    509      1.1  christos     if (Source->Asl.ParseOpcode == PARSEOP_NOT)
    510      1.1  christos     {
    511      1.1  christos         SourceOp2 = SourceOp1;
    512      1.1  christos     }
    513      1.1  christos 
    514      1.1  christos     /* DIVIDE has an extra target operand (remainder) */
    515      1.1  christos 
    516      1.1  christos     if (Source->Asl.ParseOpcode == PARSEOP_DIVIDE)
    517      1.1  christos     {
    518      1.1  christos         SourceOp2 = SourceOp2->Asl.Next;
    519      1.1  christos     }
    520      1.1  christos 
    521      1.1  christos     TargetOp = SourceOp2->Asl.Next;
    522      1.1  christos 
    523      1.1  christos     /*
    524      1.1  christos      * Can't perform this optimization if there already is a target
    525      1.1  christos      * for the operator (ZERO is a "no target" placeholder).
    526      1.1  christos      */
    527      1.1  christos     if (TargetOp->Asl.ParseOpcode != PARSEOP_ZERO)
    528      1.1  christos     {
    529      1.1  christos         goto CannotOptimize;
    530      1.1  christos     }
    531      1.1  christos 
    532      1.1  christos     /* Link in the target as the final operand */
    533      1.1  christos 
    534      1.1  christos     SourceOp2->Asl.Next = Target;
    535      1.1  christos     Target->Asl.Parent = Source;
    536      1.1  christos     return (Source);
    537      1.1  christos 
    538      1.1  christos 
    539      1.1  christos CannotOptimize:
    540      1.1  christos 
    541      1.1  christos     Operator = TrAllocateOp (PARSEOP_STORE);
    542      1.1  christos     TrLinkOpChildren (Operator, 2, Source, Target);
    543      1.1  christos 
    544      1.1  christos     /* Set the appropriate line numbers for the new op */
    545      1.1  christos 
    546      1.1  christos     Operator->Asl.LineNumber        = Target->Asl.LineNumber;
    547      1.1  christos     Operator->Asl.LogicalLineNumber = Target->Asl.LogicalLineNumber;
    548      1.1  christos     Operator->Asl.LogicalByteOffset = Target->Asl.LogicalByteOffset;
    549      1.1  christos     Operator->Asl.Column            = Target->Asl.Column;
    550      1.1  christos 
    551      1.1  christos     return (Operator);
    552      1.1  christos }
    553      1.1  christos 
    554      1.1  christos 
    555      1.1  christos /*******************************************************************************
    556      1.1  christos  *
    557      1.1  christos  * FUNCTION:    TrCreateNullTargetOp
    558      1.1  christos  *
    559      1.1  christos  * PARAMETERS:  None
    560      1.1  christos  *
    561      1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    562      1.1  christos  *
    563      1.1  christos  * DESCRIPTION: Create a "null" target op. This is defined by the ACPI
    564      1.1  christos  *              specification to be a zero AML opcode, and indicates that
    565      1.1  christos  *              no target has been specified for the parent operation
    566      1.1  christos  *
    567      1.1  christos  ******************************************************************************/
    568      1.1  christos 
    569      1.1  christos ACPI_PARSE_OBJECT *
    570      1.1  christos TrCreateNullTargetOp (
    571      1.1  christos     void)
    572      1.1  christos {
    573      1.1  christos     ACPI_PARSE_OBJECT       *Op;
    574      1.1  christos 
    575      1.1  christos 
    576      1.1  christos     Op = TrAllocateOp (PARSEOP_ZERO);
    577      1.1  christos     Op->Asl.CompileFlags |= (OP_IS_TARGET | OP_COMPILE_TIME_CONST);
    578      1.1  christos 
    579      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    580      1.1  christos         "\nCreateNullTargetOp  Ln/Col %u/%u NewOp %p  Op %s\n",
    581      1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op,
    582      1.1  christos         UtGetOpName (Op->Asl.ParseOpcode));
    583      1.1  christos 
    584      1.1  christos     return (Op);
    585      1.1  christos }
    586      1.1  christos 
    587      1.1  christos 
    588      1.1  christos /*******************************************************************************
    589      1.1  christos  *
    590      1.1  christos  * FUNCTION:    TrCreateConstantLeafOp
    591      1.1  christos  *
    592      1.1  christos  * PARAMETERS:  ParseOpcode         - The constant opcode
    593      1.1  christos  *
    594      1.1  christos  * RETURN:      Pointer to the new op. Aborts on allocation failure
    595      1.1  christos  *
    596      1.1  christos  * DESCRIPTION: Create a leaf op (no children or peers) for one of the
    597      1.1  christos  *              special constants - __LINE__, __FILE__, and __DATE__.
    598      1.1  christos  *
    599      1.1  christos  * Note: The fullimplemenation of __METHOD__ cannot happen here because we
    600      1.1  christos  * don't have a full parse tree at this time and cannot find the parent
    601      1.1  christos  * control method. __METHOD__ must be implemented later, after the parse
    602      1.1  christos  * tree has been fully constructed.
    603      1.1  christos  *
    604      1.1  christos  ******************************************************************************/
    605      1.1  christos 
    606      1.1  christos ACPI_PARSE_OBJECT *
    607      1.1  christos TrCreateConstantLeafOp (
    608      1.1  christos     UINT32                  ParseOpcode)
    609      1.1  christos {
    610      1.1  christos     ACPI_PARSE_OBJECT       *Op = NULL;
    611      1.1  christos     time_t                  CurrentTime;
    612      1.1  christos     char                    *StaticTimeString;
    613      1.1  christos     char                    *TimeString;
    614  1.1.1.6  christos     char                    *Filename = NULL;
    615  1.1.1.6  christos     ACPI_STATUS             Status;
    616      1.1  christos 
    617      1.1  christos 
    618      1.1  christos     switch (ParseOpcode)
    619      1.1  christos     {
    620      1.1  christos     case PARSEOP___LINE__:
    621      1.1  christos 
    622      1.1  christos         Op = TrAllocateOp (PARSEOP_INTEGER);
    623      1.1  christos         Op->Asl.Value.Integer = Op->Asl.LineNumber;
    624      1.1  christos         break;
    625      1.1  christos 
    626      1.1  christos     case PARSEOP___METHOD__:
    627      1.1  christos 
    628      1.1  christos         /* Will become a string literal later */
    629      1.1  christos 
    630      1.1  christos         Op = TrAllocateOp (PARSEOP___METHOD__);
    631      1.1  christos         Op->Asl.Value.String = NULL;
    632      1.1  christos         break;
    633      1.1  christos 
    634      1.1  christos     case PARSEOP___PATH__:
    635      1.1  christos 
    636      1.1  christos         Op = TrAllocateOp (PARSEOP_STRING_LITERAL);
    637      1.1  christos 
    638      1.1  christos         /* Op.Asl.Filename contains the full pathname to the file */
    639      1.1  christos 
    640      1.1  christos         Op->Asl.Value.String = Op->Asl.Filename;
    641      1.1  christos         break;
    642      1.1  christos 
    643      1.1  christos     case PARSEOP___FILE__:
    644      1.1  christos 
    645      1.1  christos         Op = TrAllocateOp (PARSEOP_STRING_LITERAL);
    646      1.1  christos 
    647      1.1  christos         /* Get the simple filename from the full path */
    648      1.1  christos 
    649  1.1.1.6  christos         Status = FlSplitInputPathname (Op->Asl.Filename, NULL, &Filename);
    650  1.1.1.6  christos         if (ACPI_FAILURE (Status))
    651  1.1.1.6  christos         {
    652  1.1.1.6  christos             return (NULL);
    653  1.1.1.6  christos         }
    654  1.1.1.6  christos 
    655      1.1  christos         Op->Asl.Value.String = Filename;
    656      1.1  christos         break;
    657      1.1  christos 
    658      1.1  christos     case PARSEOP___DATE__:
    659      1.1  christos 
    660      1.1  christos         Op = TrAllocateOp (PARSEOP_STRING_LITERAL);
    661      1.1  christos 
    662      1.1  christos         /* Get a copy of the current time */
    663      1.1  christos 
    664  1.1.1.7  christos         Op->Asl.Value.String = "";
    665      1.1  christos         CurrentTime = time (NULL);
    666  1.1.1.7  christos 
    667      1.1  christos         StaticTimeString = ctime (&CurrentTime);
    668  1.1.1.7  christos         if (StaticTimeString)
    669  1.1.1.7  christos         {
    670  1.1.1.7  christos             TimeString = UtLocalCalloc (strlen (StaticTimeString) + 1);
    671  1.1.1.7  christos             strcpy (TimeString, StaticTimeString);
    672      1.1  christos 
    673  1.1.1.7  christos             TimeString[strlen(TimeString) -1] = 0;  /* Remove trailing newline */
    674  1.1.1.7  christos             Op->Asl.Value.String = TimeString;
    675  1.1.1.7  christos         }
    676      1.1  christos         break;
    677      1.1  christos 
    678      1.1  christos     default: /* This would be an internal error */
    679      1.1  christos 
    680      1.1  christos         return (NULL);
    681      1.1  christos     }
    682      1.1  christos 
    683      1.1  christos     DbgPrint (ASL_PARSE_OUTPUT,
    684      1.1  christos         "\nCreateConstantLeafOp  Ln/Col %u/%u NewOp %p  "
    685      1.1  christos         "Op %s  Value %8.8X%8.8X  \n",
    686      1.1  christos         Op->Asl.LineNumber, Op->Asl.Column, Op, UtGetOpName (ParseOpcode),
    687      1.1  christos         ACPI_FORMAT_UINT64 (Op->Asl.Value.Integer));
    688      1.1  christos 
    689      1.1  christos     return (Op);
    690      1.1  christos }
    691      1.1  christos 
    692      1.1  christos 
    693      1.1  christos /*******************************************************************************
    694      1.1  christos  *
    695      1.1  christos  * FUNCTION:    TrAllocateOp
    696      1.1  christos  *
    697      1.1  christos  * PARAMETERS:  ParseOpcode         - Opcode to be assigned to the op
    698      1.1  christos  *
    699      1.1  christos  * RETURN:      New parse op. Aborts on allocation failure
    700      1.1  christos  *
    701      1.1  christos  * DESCRIPTION: Allocate and initialize a new parse op for the parse tree
    702      1.1  christos  *
    703      1.1  christos  ******************************************************************************/
    704      1.1  christos 
    705      1.1  christos ACPI_PARSE_OBJECT *
    706      1.1  christos TrAllocateOp (
    707      1.1  christos     UINT32                  ParseOpcode)
    708      1.1  christos {
    709      1.1  christos     ACPI_PARSE_OBJECT       *Op;
    710      1.1  christos     ACPI_PARSE_OBJECT       *LatestOp;
    711      1.1  christos 
    712      1.1  christos 
    713  1.1.1.2  christos     Op = UtParseOpCacheCalloc ();
    714      1.1  christos 
    715      1.1  christos     Op->Asl.ParseOpcode       = (UINT16) ParseOpcode;
    716  1.1.1.4  christos     Op->Asl.Filename          = AslGbl_Files[ASL_FILE_INPUT].Filename;
    717  1.1.1.4  christos     Op->Asl.LineNumber        = AslGbl_CurrentLineNumber;
    718  1.1.1.4  christos     Op->Asl.LogicalLineNumber = AslGbl_LogicalLineNumber;
    719  1.1.1.4  christos     Op->Asl.LogicalByteOffset = AslGbl_CurrentLineOffset;
    720  1.1.1.4  christos     Op->Asl.Column            = AslGbl_CurrentColumn;
    721      1.1  christos 
    722      1.1  christos     UtSetParseOpName (Op);
    723      1.1  christos 
    724      1.1  christos     /* The following is for capturing comments */
    725      1.1  christos 
    726  1.1.1.3  christos     if (AcpiGbl_CaptureComments)
    727      1.1  christos     {
    728  1.1.1.4  christos         LatestOp = AslGbl_CommentState.LatestParseOp;
    729      1.1  christos         Op->Asl.InlineComment     = NULL;
    730      1.1  christos         Op->Asl.EndNodeComment    = NULL;
    731      1.1  christos         Op->Asl.CommentList       = NULL;
    732      1.1  christos         Op->Asl.FileChanged       = FALSE;
    733      1.1  christos 
    734      1.1  christos         /*
    735      1.1  christos          * Check to see if the file name has changed before resetting the
    736      1.1  christos          * latest parse op.
    737      1.1  christos          */
    738      1.1  christos         if (LatestOp &&
    739      1.1  christos             (ParseOpcode != PARSEOP_INCLUDE) &&
    740      1.1  christos             (ParseOpcode != PARSEOP_INCLUDE_END) &&
    741      1.1  christos             strcmp (LatestOp->Asl.Filename, Op->Asl.Filename))
    742      1.1  christos         {
    743      1.1  christos             CvDbgPrint ("latest op: %s\n", LatestOp->Asl.ParseOpName);
    744      1.1  christos             Op->Asl.FileChanged = TRUE;
    745  1.1.1.4  christos             if (AslGbl_IncludeFileStack)
    746      1.1  christos             {
    747  1.1.1.4  christos                 Op->Asl.ParentFilename = AslGbl_IncludeFileStack->Filename;
    748      1.1  christos             }
    749      1.1  christos             else
    750      1.1  christos             {
    751      1.1  christos                 Op->Asl.ParentFilename = NULL;
    752      1.1  christos             }
    753      1.1  christos         }
    754      1.1  christos 
    755  1.1.1.4  christos         AslGbl_CommentState.LatestParseOp = Op;
    756      1.1  christos         CvDbgPrint ("TrAllocateOp=Set latest parse op to this op.\n");
    757      1.1  christos         CvDbgPrint ("           Op->Asl.ParseOpName = %s\n",
    758  1.1.1.4  christos             AslGbl_CommentState.LatestParseOp->Asl.ParseOpName);
    759      1.1  christos         CvDbgPrint ("           Op->Asl.ParseOpcode = 0x%x\n", ParseOpcode);
    760      1.1  christos 
    761      1.1  christos         if (Op->Asl.FileChanged)
    762      1.1  christos         {
    763      1.1  christos             CvDbgPrint("    file has been changed!\n");
    764      1.1  christos         }
    765      1.1  christos 
    766      1.1  christos         /*
    767      1.1  christos          * if this parse op's syntax uses () and {} (i.e. Package(1){0x00}) then
    768      1.1  christos          * set a flag in the comment state. This facilitates paring comments for
    769      1.1  christos          * these types of opcodes.
    770      1.1  christos          */
    771      1.1  christos         if ((CvParseOpBlockType(Op) == (BLOCK_PAREN | BLOCK_BRACE)) &&
    772      1.1  christos             (ParseOpcode != PARSEOP_DEFINITION_BLOCK))
    773      1.1  christos         {
    774      1.1  christos             CvDbgPrint ("Parsing paren/Brace op now!\n");
    775  1.1.1.4  christos             AslGbl_CommentState.ParsingParenBraceNode = Op;
    776      1.1  christos         }
    777      1.1  christos 
    778  1.1.1.4  christos         if (AslGbl_CommentListHead)
    779      1.1  christos         {
    780      1.1  christos             CvDbgPrint ("Transferring...\n");
    781  1.1.1.4  christos             Op->Asl.CommentList = AslGbl_CommentListHead;
    782  1.1.1.4  christos             AslGbl_CommentListHead = NULL;
    783  1.1.1.4  christos             AslGbl_CommentListTail = NULL;
    784      1.1  christos             CvDbgPrint ("    Transferred current comment list to this op.\n");
    785      1.1  christos             CvDbgPrint ("    %s\n", Op->Asl.CommentList->Comment);
    786      1.1  christos         }
    787      1.1  christos 
    788  1.1.1.4  christos         if (AslGbl_InlineCommentBuffer)
    789      1.1  christos         {
    790  1.1.1.4  christos             Op->Asl.InlineComment = AslGbl_InlineCommentBuffer;
    791  1.1.1.4  christos             AslGbl_InlineCommentBuffer = NULL;
    792      1.1  christos             CvDbgPrint ("Transferred current inline comment list to this op.\n");
    793      1.1  christos         }
    794      1.1  christos     }
    795      1.1  christos 
    796      1.1  christos     return (Op);
    797      1.1  christos }
    798      1.1  christos 
    799      1.1  christos 
    800      1.1  christos /*******************************************************************************
    801      1.1  christos  *
    802      1.1  christos  * FUNCTION:    TrPrintOpFlags
    803      1.1  christos  *
    804      1.1  christos  * PARAMETERS:  Flags               - Flags word to be decoded
    805      1.1  christos  *              OutputLevel         - Debug output level: ASL_TREE_OUTPUT etc.
    806      1.1  christos  *
    807      1.1  christos  * RETURN:      None
    808      1.1  christos  *
    809      1.1  christos  * DESCRIPTION: Decode a flags word to text. Displays all flags that are set.
    810      1.1  christos  *
    811      1.1  christos  ******************************************************************************/
    812      1.1  christos 
    813      1.1  christos void
    814      1.1  christos TrPrintOpFlags (
    815      1.1  christos     UINT32                  Flags,
    816      1.1  christos     UINT32                  OutputLevel)
    817      1.1  christos {
    818      1.1  christos     UINT32                  FlagBit = 1;
    819      1.1  christos     UINT32                  i;
    820      1.1  christos 
    821      1.1  christos 
    822      1.1  christos     for (i = 0; i < ACPI_NUM_OP_FLAGS; i++)
    823      1.1  christos     {
    824      1.1  christos         if (Flags & FlagBit)
    825      1.1  christos         {
    826  1.1.1.4  christos             DbgPrint (OutputLevel, " %s", AslGbl_OpFlagNames[i]);
    827      1.1  christos         }
    828      1.1  christos 
    829      1.1  christos         FlagBit <<= 1;
    830      1.1  christos     }
    831      1.1  christos }
    832