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