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