xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/asltransform.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /******************************************************************************
2  *
3  * Module Name: asltransform - Parse tree transforms
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2020, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43 
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 #include "acnamesp.h"
47 
48 #define _COMPONENT          ACPI_COMPILER
49         ACPI_MODULE_NAME    ("asltransform")
50 
51 /* Local prototypes */
52 
53 static void
54 TrTransformSubtree (
55     ACPI_PARSE_OBJECT       *Op);
56 
57 static char *
58 TrAmlGetNextTempName (
59     ACPI_PARSE_OBJECT       *Op,
60     UINT8                   *TempCount);
61 
62 static void
63 TrAmlInitLineNumbers (
64     ACPI_PARSE_OBJECT       *Op,
65     ACPI_PARSE_OBJECT       *Neighbor);
66 
67 static void
68 TrAmlInitNode (
69     ACPI_PARSE_OBJECT       *Op,
70     UINT16                  ParseOpcode);
71 
72 static void
73 TrAmlSetSubtreeParent (
74     ACPI_PARSE_OBJECT       *Op,
75     ACPI_PARSE_OBJECT       *Parent);
76 
77 static void
78 TrAmlInsertPeer (
79     ACPI_PARSE_OBJECT       *Op,
80     ACPI_PARSE_OBJECT       *NewPeer);
81 
82 static void
83 TrDoDefinitionBlock (
84     ACPI_PARSE_OBJECT       *Op);
85 
86 static void
87 TrDoSwitch (
88     ACPI_PARSE_OBJECT       *StartNode);
89 
90 static void
91 TrCheckForDuplicateCase (
92     ACPI_PARSE_OBJECT       *CaseOp,
93     ACPI_PARSE_OBJECT       *Predicate1);
94 
95 static BOOLEAN
96 TrCheckForBufferMatch (
97     ACPI_PARSE_OBJECT       *Next1,
98     ACPI_PARSE_OBJECT       *Next2);
99 
100 static void
101 TrDoMethod (
102     ACPI_PARSE_OBJECT       *Op);
103 
104 
105 /*******************************************************************************
106  *
107  * FUNCTION:    TrAmlGetNextTempName
108  *
109  * PARAMETERS:  Op              - Current parse op
110  *              TempCount       - Current temporary counter. Was originally
111  *                                per-module; Currently per method, could be
112  *                                expanded to per-scope.
113  *
114  * RETURN:      A pointer to name (allocated here).
115  *
116  * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
117  *              reserved for use by the ASL compiler. (_T_0 through _T_Z)
118  *
119  ******************************************************************************/
120 
121 static char *
122 TrAmlGetNextTempName (
123     ACPI_PARSE_OBJECT       *Op,
124     UINT8                   *TempCount)
125 {
126     char                    *TempName;
127 
128 
129     if (*TempCount >= (10 + 26))  /* 0-35 valid: 0-9 and A-Z for TempName[3] */
130     {
131         /* Too many temps */
132 
133         AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
134         return (NULL);
135     }
136 
137     TempName = UtLocalCalloc (5);
138 
139     if (*TempCount < 10)    /* 0-9 */
140     {
141         TempName[3] = (char) (*TempCount + '0');
142     }
143     else                    /* 10-35: A-Z */
144     {
145         TempName[3] = (char) (*TempCount + ('A' - 10));
146     }
147 
148     (*TempCount)++;
149 
150     /* First three characters are always "_T_" */
151 
152     TempName[0] = '_';
153     TempName[1] = 'T';
154     TempName[2] = '_';
155 
156     return (TempName);
157 }
158 
159 
160 /*******************************************************************************
161  *
162  * FUNCTION:    TrAmlInitLineNumbers
163  *
164  * PARAMETERS:  Op              - Op to be initialized
165  *              Neighbor        - Op used for initialization values
166  *
167  * RETURN:      None
168  *
169  * DESCRIPTION: Initialized the various line numbers for a parse node.
170  *
171  ******************************************************************************/
172 
173 static void
174 TrAmlInitLineNumbers (
175     ACPI_PARSE_OBJECT       *Op,
176     ACPI_PARSE_OBJECT       *Neighbor)
177 {
178 
179     Op->Asl.EndLine           = Neighbor->Asl.EndLine;
180     Op->Asl.EndLogicalLine    = Neighbor->Asl.EndLogicalLine;
181     Op->Asl.LineNumber        = Neighbor->Asl.LineNumber;
182     Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
183     Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
184 }
185 
186 
187 /*******************************************************************************
188  *
189  * FUNCTION:    TrAmlInitNode
190  *
191  * PARAMETERS:  Op              - Op to be initialized
192  *              ParseOpcode     - Opcode for this node
193  *
194  * RETURN:      None
195  *
196  * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
197  *
198  ******************************************************************************/
199 
200 static void
201 TrAmlInitNode (
202     ACPI_PARSE_OBJECT       *Op,
203     UINT16                  ParseOpcode)
204 {
205 
206     Op->Asl.ParseOpcode = ParseOpcode;
207     UtSetParseOpName (Op);
208 }
209 
210 
211 /*******************************************************************************
212  *
213  * FUNCTION:    TrAmlSetSubtreeParent
214  *
215  * PARAMETERS:  Op              - First node in a list of peer nodes
216  *              Parent          - Parent of the subtree
217  *
218  * RETURN:      None
219  *
220  * DESCRIPTION: Set the parent for all peer nodes in a subtree
221  *
222  ******************************************************************************/
223 
224 static void
225 TrAmlSetSubtreeParent (
226     ACPI_PARSE_OBJECT       *Op,
227     ACPI_PARSE_OBJECT       *Parent)
228 {
229     ACPI_PARSE_OBJECT       *Next;
230 
231 
232     Next = Op;
233     while (Next)
234     {
235         Next->Asl.Parent = Parent;
236         Next = Next->Asl.Next;
237     }
238 }
239 
240 
241 /*******************************************************************************
242  *
243  * FUNCTION:    TrAmlInsertPeer
244  *
245  * PARAMETERS:  Op              - First node in a list of peer nodes
246  *              NewPeer         - Peer node to insert
247  *
248  * RETURN:      None
249  *
250  * DESCRIPTION: Insert a new peer node into a list of peers.
251  *
252  ******************************************************************************/
253 
254 static void
255 TrAmlInsertPeer (
256     ACPI_PARSE_OBJECT       *Op,
257     ACPI_PARSE_OBJECT       *NewPeer)
258 {
259 
260     NewPeer->Asl.Next = Op->Asl.Next;
261     Op->Asl.Next = NewPeer;
262 }
263 
264 
265 /*******************************************************************************
266  *
267  * FUNCTION:    TrAmlTransformWalkBegin
268  *
269  * PARAMETERS:  ASL_WALK_CALLBACK
270  *
271  * RETURN:      None
272  *
273  * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
274  *              operands.
275  *
276  ******************************************************************************/
277 
278 ACPI_STATUS
279 TrAmlTransformWalkBegin (
280     ACPI_PARSE_OBJECT       *Op,
281     UINT32                  Level,
282     void                    *Context)
283 {
284 
285     TrTransformSubtree (Op);
286     return (AE_OK);
287 }
288 
289 
290 /*******************************************************************************
291  *
292  * FUNCTION:    TrAmlTransformWalkEnd
293  *
294  * PARAMETERS:  ASL_WALK_CALLBACK
295  *
296  * RETURN:      None
297  *
298  * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
299  *              operands.
300  *
301  ******************************************************************************/
302 
303 ACPI_STATUS
304 TrAmlTransformWalkEnd (
305     ACPI_PARSE_OBJECT       *Op,
306     UINT32                  Level,
307     void                    *Context)
308 {
309 
310     /* Save possible Externals list in the DefintionBlock Op */
311 
312     if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
313     {
314         Op->Asl.Value.Arg = AslGbl_ExternalsListHead;
315         AslGbl_ExternalsListHead = NULL;
316     }
317 
318     return (AE_OK);
319 }
320 
321 
322 /*******************************************************************************
323  *
324  * FUNCTION:    TrTransformSubtree
325  *
326  * PARAMETERS:  Op        - The parent parse node
327  *
328  * RETURN:      None
329  *
330  * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
331  *              complex AML opcodes require processing of the child nodes
332  *              (arguments/operands).
333  *
334  ******************************************************************************/
335 
336 static void
337 TrTransformSubtree (
338     ACPI_PARSE_OBJECT           *Op)
339 {
340     ACPI_PARSE_OBJECT           *MethodOp;
341     ACPI_NAMESTRING_INFO        Info;
342 
343 
344     if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
345     {
346         return;
347     }
348 
349     switch (Op->Asl.ParseOpcode)
350     {
351     case PARSEOP_DEFINITION_BLOCK:
352 
353         TrDoDefinitionBlock (Op);
354         break;
355 
356     case PARSEOP_SWITCH:
357 
358         TrDoSwitch (Op);
359         break;
360 
361     case PARSEOP_METHOD:
362 
363         TrDoMethod (Op);
364         break;
365 
366     case PARSEOP_EXTERNAL:
367 
368         ExDoExternal (Op);
369         break;
370 
371     case PARSEOP___METHOD__:
372 
373         /* Transform to a string op containing the parent method name */
374 
375         Op->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
376         UtSetParseOpName (Op);
377 
378         /* Find the parent control method op */
379 
380         MethodOp = Op;
381         while (MethodOp)
382         {
383             if (MethodOp->Asl.ParseOpcode == PARSEOP_METHOD)
384             {
385                 /* First child contains the method name */
386 
387                 MethodOp = MethodOp->Asl.Child;
388                 Op->Asl.Value.String = MethodOp->Asl.Value.String;
389                 return;
390             }
391 
392             MethodOp = MethodOp->Asl.Parent;
393         }
394 
395         /* At the root, invocation not within a control method */
396 
397         Op->Asl.Value.String = "\\";
398         break;
399 
400     case PARSEOP_NAMESTRING:
401         /*
402          * A NameString can be up to 255 (0xFF) individual NameSegs maximum
403          * (with 254 dot separators) - as per the ACPI specification. Note:
404          * Cannot check for NumSegments == 0 because things like
405          * Scope(\) are legal and OK.
406          */
407         Info.ExternalName = Op->Asl.Value.String;
408         AcpiNsGetInternalNameLength (&Info);
409 
410         if (Info.NumSegments > 255)
411         {
412             AslError (ASL_ERROR, ASL_MSG_NAMESTRING_LENGTH, Op, NULL);
413         }
414         break;
415 
416     case PARSEOP_UNLOAD:
417 
418         AslError (ASL_WARNING, ASL_MSG_UNLOAD, Op, NULL);
419         break;
420 
421     case PARSEOP_SLEEP:
422 
423         /* Remark for very long sleep values */
424 
425         if (Op->Asl.Child->Asl.Value.Integer > 1000)
426         {
427             AslError (ASL_REMARK, ASL_MSG_LONG_SLEEP, Op, NULL);
428         }
429         break;
430 
431     case PARSEOP_PROCESSOR:
432 
433         AslError (ASL_WARNING, ASL_MSG_LEGACY_PROCESSOR_OP, Op, Op->Asl.ExternalName);
434         break;
435 
436     default:
437 
438         /* Nothing to do here for other opcodes */
439 
440         break;
441     }
442 }
443 
444 
445 /*******************************************************************************
446  *
447  * FUNCTION:    TrDoDefinitionBlock
448  *
449  * PARAMETERS:  Op        - Parse node
450  *
451  * RETURN:      None
452  *
453  * DESCRIPTION: Find the end of the definition block and set a global to this
454  *              node. It is used by the compiler to insert compiler-generated
455  *              names at the root level of the namespace.
456  *
457  ******************************************************************************/
458 
459 static void
460 TrDoDefinitionBlock (
461     ACPI_PARSE_OBJECT       *Op)
462 {
463     ACPI_PARSE_OBJECT       *Next;
464     UINT32                  i;
465 
466 
467     /* Reset external list when starting a definition block */
468 
469     AslGbl_ExternalsListHead = NULL;
470 
471     Next = Op->Asl.Child;
472     for (i = 0; i < 5; i++)
473     {
474         Next = Next->Asl.Next;
475         if (i == 0)
476         {
477             /*
478              * This is the table signature. Only the DSDT can be assumed
479              * to be at the root of the namespace;  Therefore, namepath
480              * optimization can only be performed on the DSDT.
481              */
482             if (!ACPI_COMPARE_NAMESEG (Next->Asl.Value.String, ACPI_SIG_DSDT))
483             {
484                 AslGbl_ReferenceOptimizationFlag = FALSE;
485             }
486         }
487     }
488 
489     AslGbl_FirstLevelInsertionNode = Next;
490 }
491 
492 
493 /*******************************************************************************
494  *
495  * FUNCTION:    TrDoSwitch
496  *
497  * PARAMETERS:  StartNode        - Parse node for SWITCH
498  *
499  * RETURN:      None
500  *
501  * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
502  *              no actual AML opcode for SWITCH -- it must be simulated.
503  *
504  ******************************************************************************/
505 
506 static void
507 TrDoSwitch (
508     ACPI_PARSE_OBJECT       *StartNode)
509 {
510     ACPI_PARSE_OBJECT       *Next;
511     ACPI_PARSE_OBJECT       *CaseOp = NULL;
512     ACPI_PARSE_OBJECT       *CaseBlock = NULL;
513     ACPI_PARSE_OBJECT       *DefaultOp = NULL;
514     ACPI_PARSE_OBJECT       *CurrentParentNode;
515     ACPI_PARSE_OBJECT       *Conditional = NULL;
516     ACPI_PARSE_OBJECT       *Predicate;
517     ACPI_PARSE_OBJECT       *Peer;
518     ACPI_PARSE_OBJECT       *NewOp;
519     ACPI_PARSE_OBJECT       *NewOp2;
520     ACPI_PARSE_OBJECT       *MethodOp;
521     ACPI_PARSE_OBJECT       *StoreOp;
522     ACPI_PARSE_OBJECT       *BreakOp;
523     ACPI_PARSE_OBJECT       *BufferOp;
524     char                    *PredicateValueName;
525     UINT16                  Index;
526     UINT32                  Btype;
527 
528 
529     /* Start node is the Switch() node */
530 
531     CurrentParentNode  = StartNode;
532 
533     /* Create a new temp name of the form _T_x */
534 
535     PredicateValueName = TrAmlGetNextTempName (StartNode, &AslGbl_TempCount);
536     if (!PredicateValueName)
537     {
538         return;
539     }
540 
541     /* First child is the Switch() predicate */
542 
543     Next = StartNode->Asl.Child;
544 
545     /*
546      * Examine the return type of the Switch Value -
547      * must be Integer/Buffer/String
548      */
549     Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
550     Btype = AslKeywordMapping[Index].AcpiBtype;
551     if ((Btype != ACPI_BTYPE_INTEGER) &&
552         (Btype != ACPI_BTYPE_STRING)  &&
553         (Btype != ACPI_BTYPE_BUFFER))
554     {
555         AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
556         Btype = ACPI_BTYPE_INTEGER;
557     }
558 
559     /* CASE statements start at next child */
560 
561     Peer = Next->Asl.Next;
562     while (Peer)
563     {
564         Next = Peer;
565         Peer = Next->Asl.Next;
566 
567         if (Next->Asl.ParseOpcode == PARSEOP_CASE)
568         {
569             TrCheckForDuplicateCase (Next, Next->Asl.Child);
570 
571             if (CaseOp)
572             {
573                 /* Add an ELSE to complete the previous CASE */
574 
575                 NewOp = TrCreateLeafOp (PARSEOP_ELSE);
576                 NewOp->Asl.Parent = Conditional->Asl.Parent;
577                 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
578 
579                 /* Link ELSE node as a peer to the previous IF */
580 
581                 TrAmlInsertPeer (Conditional, NewOp);
582                 CurrentParentNode = NewOp;
583             }
584 
585             CaseOp = Next;
586             Conditional = CaseOp;
587             CaseBlock = CaseOp->Asl.Child->Asl.Next;
588             Conditional->Asl.Child->Asl.Next = NULL;
589             Predicate = CaseOp->Asl.Child;
590 
591             if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
592                 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
593             {
594                 /*
595                  * Convert the package declaration to this form:
596                  *
597                  * If (LNotEqual (Match (Package(<size>){<data>},
598                  *                       MEQ, _T_x, MTR, Zero, Zero), Ones))
599                  */
600                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MEQ);
601                 Predicate->Asl.Next = NewOp2;
602                 TrAmlInitLineNumbers (NewOp2, Conditional);
603 
604                 NewOp               = NewOp2;
605                 NewOp2              = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
606                                         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
607                 NewOp->Asl.Next     = NewOp2;
608                 TrAmlInitLineNumbers (NewOp2, Predicate);
609 
610                 NewOp               = NewOp2;
611                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCHTYPE_MTR);
612                 NewOp->Asl.Next     = NewOp2;
613                 TrAmlInitLineNumbers (NewOp2, Predicate);
614 
615                 NewOp               = NewOp2;
616                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
617                 NewOp->Asl.Next     = NewOp2;
618                 TrAmlInitLineNumbers (NewOp2, Predicate);
619 
620                 NewOp               = NewOp2;
621                 NewOp2              = TrCreateLeafOp (PARSEOP_ZERO);
622                 NewOp->Asl.Next     = NewOp2;
623                 TrAmlInitLineNumbers (NewOp2, Predicate);
624 
625                 NewOp2              = TrCreateLeafOp (PARSEOP_MATCH);
626                 NewOp2->Asl.Child   = Predicate;  /* PARSEOP_PACKAGE */
627                 TrAmlInitLineNumbers (NewOp2, Conditional);
628                 TrAmlSetSubtreeParent (Predicate, NewOp2);
629 
630                 NewOp               = NewOp2;
631                 NewOp2              = TrCreateLeafOp (PARSEOP_ONES);
632                 NewOp->Asl.Next     = NewOp2;
633                 TrAmlInitLineNumbers (NewOp2, Conditional);
634 
635                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
636                 NewOp2->Asl.Child   = NewOp;
637                 NewOp->Asl.Parent   = NewOp2;
638                 TrAmlInitLineNumbers (NewOp2, Conditional);
639                 TrAmlSetSubtreeParent (NewOp, NewOp2);
640 
641                 NewOp               = NewOp2;
642                 NewOp2              = TrCreateLeafOp (PARSEOP_LNOT);
643                 NewOp2->Asl.Child   = NewOp;
644                 NewOp2->Asl.Parent  = Conditional;
645                 NewOp->Asl.Parent   = NewOp2;
646                 TrAmlInitLineNumbers (NewOp2, Conditional);
647 
648                 Conditional->Asl.Child = NewOp2;
649                 NewOp2->Asl.Next = CaseBlock;
650             }
651             else
652             {
653                 /*
654                  * Integer and Buffer case.
655                  *
656                  * Change CaseOp() to:  If (LEqual (SwitchValue, CaseValue)) {...}
657                  * Note: SwitchValue is first to allow the CaseValue to be implicitly
658                  * converted to the type of SwitchValue if necessary.
659                  *
660                  * CaseOp->Child is the case value
661                  * CaseOp->Child->Peer is the beginning of the case block
662                  */
663                 NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESTRING,
664                     (UINT64) ACPI_TO_INTEGER (PredicateValueName));
665                 NewOp->Asl.Next = Predicate;
666                 TrAmlInitLineNumbers (NewOp, Predicate);
667 
668                 NewOp2              = TrCreateLeafOp (PARSEOP_LEQUAL);
669                 NewOp2->Asl.Parent  = Conditional;
670                 NewOp2->Asl.Child   = NewOp;
671                 TrAmlInitLineNumbers (NewOp2, Conditional);
672 
673                 TrAmlSetSubtreeParent (NewOp, NewOp2);
674 
675                 Predicate           = NewOp2;
676                 Predicate->Asl.Next = CaseBlock;
677 
678                 TrAmlSetSubtreeParent (Predicate, Conditional);
679                 Conditional->Asl.Child = Predicate;
680             }
681 
682             /* Reinitialize the CASE node to an IF node */
683 
684             TrAmlInitNode (Conditional, PARSEOP_IF);
685 
686             /*
687              * The first CASE(IF) is not nested under an ELSE.
688              * All other CASEs are children of a parent ELSE.
689              */
690             if (CurrentParentNode == StartNode)
691             {
692                 Conditional->Asl.Next = NULL;
693             }
694             else
695             {
696                 /*
697                  * The IF is a child of previous IF/ELSE. It
698                  * is therefore without peer.
699                  */
700                 CurrentParentNode->Asl.Child = Conditional;
701                 Conditional->Asl.Parent      = CurrentParentNode;
702                 Conditional->Asl.Next        = NULL;
703             }
704         }
705         else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
706         {
707             if (DefaultOp)
708             {
709                 /*
710                  * More than one Default
711                  * (Parser does not catch this, must check here)
712                  */
713                 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
714             }
715             else
716             {
717                 /* Save the DEFAULT node for later, after CASEs */
718 
719                 DefaultOp = Next;
720             }
721         }
722         else
723         {
724             /* Unknown peer opcode */
725 
726             AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%u)\n",
727                 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
728         }
729     }
730 
731     /* Add the default case at the end of the if/else construct */
732 
733     if (DefaultOp)
734     {
735         /* If no CASE statements, this is an error - see below */
736 
737         if (CaseOp)
738         {
739             /* Convert the DEFAULT node to an ELSE */
740 
741             TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
742             DefaultOp->Asl.Parent = Conditional->Asl.Parent;
743 
744             /* Link ELSE node as a peer to the previous IF */
745 
746             TrAmlInsertPeer (Conditional, DefaultOp);
747         }
748     }
749 
750     if (!CaseOp)
751     {
752         AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
753     }
754 
755 
756     /*
757      * Create a Name(_T_x, ...) statement. This statement must appear at the
758      * method level, in case a loop surrounds the switch statement and could
759      * cause the name to be created twice (error).
760      */
761 
762     /* Create the Name node */
763 
764     Predicate = StartNode->Asl.Child;
765     NewOp = TrCreateLeafOp (PARSEOP_NAME);
766     TrAmlInitLineNumbers (NewOp, StartNode);
767 
768     /* Find the parent method */
769 
770     Next = StartNode;
771     while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
772            (Next->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
773     {
774         Next = Next->Asl.Parent;
775     }
776     MethodOp = Next;
777 
778     NewOp->Asl.CompileFlags |= OP_COMPILER_EMITTED;
779     NewOp->Asl.Parent = Next;
780 
781     /* Insert name after the method name and arguments */
782 
783     Next = Next->Asl.Child; /* Name */
784     Next = Next->Asl.Next;  /* NumArgs */
785     Next = Next->Asl.Next;  /* SerializeRule */
786 
787     /*
788      * If method is not Serialized, we must make is so, because of the way
789      * that Switch() must be implemented -- we cannot allow multiple threads
790      * to execute this method concurrently since we need to create local
791      * temporary name(s).
792      */
793     if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
794     {
795         AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp,
796             "Due to use of Switch operator");
797         Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
798     }
799 
800     Next = Next->Asl.Next;  /* SyncLevel */
801     Next = Next->Asl.Next;  /* ReturnType */
802     Next = Next->Asl.Next;  /* ParameterTypes */
803 
804     TrAmlInsertPeer (Next, NewOp);
805     TrAmlInitLineNumbers (NewOp, Next);
806 
807     /* Create the NameSeg child for the Name node */
808 
809     NewOp2 = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
810         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
811     TrAmlInitLineNumbers (NewOp2, NewOp);
812     NewOp2->Asl.CompileFlags |= OP_IS_NAME_DECLARATION;
813     NewOp->Asl.Child  = NewOp2;
814 
815     /* Create the initial value for the Name. Btype was already validated above */
816 
817     switch (Btype)
818     {
819     case ACPI_BTYPE_INTEGER:
820 
821         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_ZERO,
822             (UINT64) 0);
823         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
824         break;
825 
826     case ACPI_BTYPE_STRING:
827 
828         NewOp2->Asl.Next = TrCreateValuedLeafOp (PARSEOP_STRING_LITERAL,
829             (UINT64) ACPI_TO_INTEGER (""));
830         TrAmlInitLineNumbers (NewOp2->Asl.Next, NewOp);
831         break;
832 
833     case ACPI_BTYPE_BUFFER:
834 
835         (void) TrLinkPeerOp (NewOp2, TrCreateValuedLeafOp (PARSEOP_BUFFER,
836             (UINT64) 0));
837         Next = NewOp2->Asl.Next;
838         TrAmlInitLineNumbers (Next, NewOp2);
839 
840         (void) TrLinkOpChildren (Next, 1, TrCreateValuedLeafOp (PARSEOP_ZERO,
841             (UINT64) 1));
842         TrAmlInitLineNumbers (Next->Asl.Child, Next);
843 
844         BufferOp = TrCreateValuedLeafOp (PARSEOP_DEFAULT_ARG, (UINT64) 0);
845         TrAmlInitLineNumbers (BufferOp, Next->Asl.Child);
846         (void) TrLinkPeerOp (Next->Asl.Child, BufferOp);
847 
848         TrAmlSetSubtreeParent (Next->Asl.Child, Next);
849         break;
850 
851     default:
852 
853         break;
854     }
855 
856     TrAmlSetSubtreeParent (NewOp2, NewOp);
857 
858     /*
859      * Transform the Switch() into a While(One)-Break node.
860      * And create a Store() node which will be used to save the
861      * Switch() value. The store is of the form: Store (Value, _T_x)
862      * where _T_x is the temp variable.
863      */
864     TrAmlInitNode (StartNode, PARSEOP_WHILE);
865     NewOp = TrCreateLeafOp (PARSEOP_ONE);
866     TrAmlInitLineNumbers (NewOp, StartNode);
867     NewOp->Asl.Next = Predicate->Asl.Next;
868     NewOp->Asl.Parent = StartNode;
869     StartNode->Asl.Child = NewOp;
870 
871     /* Create a Store() node */
872 
873     StoreOp = TrCreateLeafOp (PARSEOP_STORE);
874     TrAmlInitLineNumbers (StoreOp, NewOp);
875     StoreOp->Asl.Parent = StartNode;
876     TrAmlInsertPeer (NewOp, StoreOp);
877 
878     /* Complete the Store subtree */
879 
880     StoreOp->Asl.Child = Predicate;
881     Predicate->Asl.Parent = StoreOp;
882 
883     NewOp = TrCreateValuedLeafOp (PARSEOP_NAMESEG,
884         (UINT64) ACPI_TO_INTEGER (PredicateValueName));
885     TrAmlInitLineNumbers (NewOp, StoreOp);
886     NewOp->Asl.Parent    = StoreOp;
887     Predicate->Asl.Next  = NewOp;
888 
889     /* Create a Break() node and insert it into the end of While() */
890 
891     Conditional = StartNode->Asl.Child;
892     while (Conditional->Asl.Next)
893     {
894         Conditional = Conditional->Asl.Next;
895     }
896 
897     BreakOp = TrCreateLeafOp (PARSEOP_BREAK);
898     TrAmlInitLineNumbers (BreakOp, NewOp);
899     BreakOp->Asl.Parent = StartNode;
900     TrAmlInsertPeer (Conditional, BreakOp);
901 }
902 
903 
904 /*******************************************************************************
905  *
906  * FUNCTION:    TrCheckForDuplicateCase
907  *
908  * PARAMETERS:  CaseOp          - Parse node for first Case statement in list
909  *              Predicate1      - Case value for the input CaseOp
910  *
911  * RETURN:      None
912  *
913  * DESCRIPTION: Check for duplicate case values. Currently, only handles
914  *              Integers, Strings and Buffers. No support for Package objects.
915  *
916  ******************************************************************************/
917 
918 static void
919 TrCheckForDuplicateCase (
920     ACPI_PARSE_OBJECT       *CaseOp,
921     ACPI_PARSE_OBJECT       *Predicate1)
922 {
923     ACPI_PARSE_OBJECT       *Next;
924     ACPI_PARSE_OBJECT       *Predicate2;
925 
926 
927     /* Walk the list of CASE opcodes */
928 
929     Next = CaseOp->Asl.Next;
930     while (Next)
931     {
932         if (Next->Asl.ParseOpcode == PARSEOP_CASE)
933         {
934             /* Emit error only once */
935 
936             if (Next->Asl.CompileFlags & OP_IS_DUPLICATE)
937             {
938                 goto NextCase;
939             }
940 
941             /* Check for a duplicate plain integer */
942 
943             Predicate2 = Next->Asl.Child;
944             if ((Predicate1->Asl.ParseOpcode == PARSEOP_INTEGER) &&
945                 (Predicate2->Asl.ParseOpcode == PARSEOP_INTEGER))
946             {
947                 if (Predicate1->Asl.Value.Integer == Predicate2->Asl.Value.Integer)
948                 {
949                     goto FoundDuplicate;
950                 }
951             }
952 
953             /* Check for pairs of the constants ZERO, ONE, ONES */
954 
955             else if (((Predicate1->Asl.ParseOpcode == PARSEOP_ZERO) &&
956                 (Predicate2->Asl.ParseOpcode == PARSEOP_ZERO)) ||
957                 ((Predicate1->Asl.ParseOpcode == PARSEOP_ONE) &&
958                 (Predicate2->Asl.ParseOpcode == PARSEOP_ONE)) ||
959                 ((Predicate1->Asl.ParseOpcode == PARSEOP_ONES) &&
960                 (Predicate2->Asl.ParseOpcode == PARSEOP_ONES)))
961             {
962                 goto FoundDuplicate;
963             }
964 
965             /* Check for a duplicate string constant (literal) */
966 
967             else if ((Predicate1->Asl.ParseOpcode == PARSEOP_STRING_LITERAL) &&
968                 (Predicate2->Asl.ParseOpcode == PARSEOP_STRING_LITERAL))
969             {
970                 if (!strcmp (Predicate1->Asl.Value.String,
971                         Predicate2->Asl.Value.String))
972                 {
973                     goto FoundDuplicate;
974                 }
975             }
976 
977             /* Check for a duplicate buffer constant */
978 
979             else if ((Predicate1->Asl.ParseOpcode == PARSEOP_BUFFER) &&
980                 (Predicate2->Asl.ParseOpcode == PARSEOP_BUFFER))
981             {
982                 if (TrCheckForBufferMatch (Predicate1->Asl.Child,
983                         Predicate2->Asl.Child))
984                 {
985                     goto FoundDuplicate;
986                 }
987             }
988         }
989         goto NextCase;
990 
991 FoundDuplicate:
992         /* Emit error message only once */
993 
994         Next->Asl.CompileFlags |= OP_IS_DUPLICATE;
995 
996         AslDualParseOpError (ASL_ERROR, ASL_MSG_DUPLICATE_CASE, Next,
997             Next->Asl.Value.String, ASL_MSG_CASE_FOUND_HERE, CaseOp,
998             CaseOp->Asl.ExternalName);
999 
1000 NextCase:
1001         Next = Next->Asl.Next;
1002     }
1003 }
1004 
1005 /*******************************************************************************
1006  *
1007  * FUNCTION:    TrBufferIsAllZero
1008  *
1009  * PARAMETERS:  Op          - Parse node for first opcode in buffer initializer
1010  *                            list
1011  *
1012  * RETURN:      TRUE if buffer contains all zeros or a DEFAULT_ARG
1013  *
1014  * DESCRIPTION: Check for duplicate Buffer case values.
1015  *
1016  ******************************************************************************/
1017 
1018 static BOOLEAN
1019 TrBufferIsAllZero (
1020     ACPI_PARSE_OBJECT       *Op)
1021 {
1022     while (Op)
1023     {
1024         if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
1025         {
1026             return (TRUE);
1027         }
1028         else if (Op->Asl.Value.Integer != 0)
1029         {
1030             return (FALSE);
1031         }
1032 
1033         Op = Op->Asl.Next;
1034     }
1035 
1036     return (TRUE);
1037 }
1038 
1039 
1040 /*******************************************************************************
1041  *
1042  * FUNCTION:    TrCheckForBufferMatch
1043  *
1044  * PARAMETERS:  Next1       - Parse node for first opcode in first buffer list
1045  *                              (The DEFAULT_ARG or INTEGER node)
1046  *              Next2       - Parse node for first opcode in second buffer list
1047  *                              (The DEFAULT_ARG or INTEGER node)
1048  *
1049  * RETURN:      TRUE if buffers match, FALSE otherwise
1050  *
1051  * DESCRIPTION: Check for duplicate Buffer case values.
1052  *
1053  ******************************************************************************/
1054 
1055 static BOOLEAN
1056 TrCheckForBufferMatch (
1057     ACPI_PARSE_OBJECT       *NextOp1,
1058     ACPI_PARSE_OBJECT       *NextOp2)
1059 {
1060     /*
1061      * The buffer length can be a DEFAULT_ARG or INTEGER. If any of the nodes
1062      * are DEFAULT_ARG, it means that the length has yet to be computed.
1063      * However, the initializer list can be compared to determine if these two
1064      * buffers match.
1065      */
1066     if ((NextOp1->Asl.ParseOpcode == PARSEOP_INTEGER &&
1067         NextOp2->Asl.ParseOpcode == PARSEOP_INTEGER) &&
1068         NextOp1->Asl.Value.Integer != NextOp2->Asl.Value.Integer)
1069     {
1070         return (FALSE);
1071     }
1072 
1073     /*
1074      * Buffers that have explicit lengths but no initializer lists are
1075      * filled with zeros at runtime. This is equivalent to buffers that have the
1076      * same length that are filled with zeros.
1077      *
1078      * In other words, the following buffers are equivalent:
1079      *
1080      * Buffer(0x4) {}
1081      * Buffer() {0x0, 0x0, 0x0, 0x0}
1082      *
1083      * This statement checks for matches where one buffer does not have an
1084      * initializer list and another buffer contains all zeros.
1085      */
1086     if (NextOp1->Asl.ParseOpcode != NextOp2->Asl.ParseOpcode &&
1087         TrBufferIsAllZero (NextOp1->Asl.Next) &&
1088         TrBufferIsAllZero (NextOp2->Asl.Next))
1089     {
1090         return (TRUE);
1091     }
1092 
1093     /* Start at the BYTECONST initializer node list */
1094 
1095     NextOp1 = NextOp1->Asl.Next;
1096     NextOp2 = NextOp2->Asl.Next;
1097 
1098     /*
1099      * Walk both lists until either a mismatch is found, or one or more
1100      * end-of-lists are found
1101      */
1102     while (NextOp1 && NextOp2)
1103     {
1104         if ((NextOp1->Asl.ParseOpcode == PARSEOP_STRING_LITERAL) &&
1105             (NextOp2->Asl.ParseOpcode == PARSEOP_STRING_LITERAL))
1106         {
1107             if (!strcmp (NextOp1->Asl.Value.String, NextOp2->Asl.Value.String))
1108             {
1109                 return (TRUE);
1110             }
1111             else
1112             {
1113                 return (FALSE);
1114             }
1115         }
1116         if ((UINT8) NextOp1->Asl.Value.Integer != (UINT8) NextOp2->Asl.Value.Integer)
1117         {
1118             return (FALSE);
1119         }
1120 
1121         NextOp1 = NextOp1->Asl.Next;
1122         NextOp2 = NextOp2->Asl.Next;
1123     }
1124 
1125     /* Not a match if one of the lists is not at end-of-list */
1126 
1127     if (NextOp1 || NextOp2)
1128     {
1129         return (FALSE);
1130     }
1131 
1132     /* Otherwise, the buffers match */
1133 
1134     return (TRUE);
1135 }
1136 
1137 
1138 /*******************************************************************************
1139  *
1140  * FUNCTION:    TrDoMethod
1141  *
1142  * PARAMETERS:  Op               - Parse node for SWITCH
1143  *
1144  * RETURN:      None
1145  *
1146  * DESCRIPTION: Determine that parameter count of an ASL method node by
1147  *              translating the parameter count parse node from
1148  *              PARSEOP_DEFAULT_ARG to PARSEOP_BYTECONST.
1149  *
1150  ******************************************************************************/
1151 
1152 static void
1153 TrDoMethod (
1154     ACPI_PARSE_OBJECT       *Op)
1155 {
1156     ACPI_PARSE_OBJECT           *ArgCountOp;
1157     UINT8                       ArgCount;
1158     ACPI_PARSE_OBJECT           *ParameterOp;
1159 
1160 
1161     /*
1162      * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
1163      * however
1164      */
1165     AslGbl_TempCount = 0;
1166 
1167     ArgCountOp = Op->Asl.Child->Asl.Next;
1168     if (ArgCountOp->Asl.ParseOpcode == PARSEOP_BYTECONST)
1169     {
1170         /*
1171          * Parameter count for this method has already been recorded in the
1172          * method declaration.
1173          */
1174         return;
1175     }
1176 
1177     /*
1178      * Parameter count has been omitted in the method declaration.
1179      * Count the amount of arguments here.
1180      */
1181     ParameterOp = ArgCountOp->Asl.Next->Asl.Next->Asl.Next->Asl.Next;
1182     if (ParameterOp->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
1183     {
1184         ArgCount = 0;
1185         ParameterOp = ParameterOp->Asl.Child;
1186 
1187         while (ParameterOp)
1188         {
1189             ParameterOp = ParameterOp->Asl.Next;
1190             ArgCount++;
1191         }
1192 
1193         ArgCountOp->Asl.Value.Integer = ArgCount;
1194         ArgCountOp->Asl.ParseOpcode = PARSEOP_BYTECONST;
1195     }
1196     else
1197     {
1198         /*
1199          * Method parameters can be counted by analyzing the Parameter type
1200          * list. If the Parameter list contains more than 1 parameter, it
1201          * is nested under PARSEOP_DEFAULT_ARG. When there is only 1
1202          * parameter, the parse tree contains a single node representing
1203          * that type.
1204          */
1205         ArgCountOp->Asl.Value.Integer = 1;
1206         ArgCountOp->Asl.ParseOpcode = PARSEOP_BYTECONST;
1207     }
1208 }
1209