1
2/******************************************************************************
3 *
4 * Module Name: asltransform - Parse tree transforms
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
21 *
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
28 *
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
54 *
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
66 *
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117
118#include <contrib/dev/acpica/compiler/aslcompiler.h>
119#include "aslcompiler.y.h"
120
121#define _COMPONENT ACPI_COMPILER
122 ACPI_MODULE_NAME ("asltransform")
123
124/* Local prototypes */
125
126static void
127TrTransformSubtree (
128 ACPI_PARSE_OBJECT *Op);
129
130static char *
131TrAmlGetNextTempName (
132 ACPI_PARSE_OBJECT *Op,
133 UINT8 *TempCount);
134
135static void
136TrAmlInitLineNumbers (
137 ACPI_PARSE_OBJECT *Op,
138 ACPI_PARSE_OBJECT *Neighbor);
139
140static void
141TrAmlInitNode (
142 ACPI_PARSE_OBJECT *Op,
143 UINT16 ParseOpcode);
144
145static void
146TrAmlSetSubtreeParent (
147 ACPI_PARSE_OBJECT *Op,
148 ACPI_PARSE_OBJECT *Parent);
149
150static void
151TrAmlInsertPeer (
152 ACPI_PARSE_OBJECT *Op,
153 ACPI_PARSE_OBJECT *NewPeer);
154
155static void
156TrDoDefinitionBlock (
157 ACPI_PARSE_OBJECT *Op);
158
159static void
160TrDoSwitch (
161 ACPI_PARSE_OBJECT *StartNode);
162
163
164/*******************************************************************************
165 *
166 * FUNCTION: TrAmlGetNextTempName
167 *
168 * PARAMETERS: Op - Current parse op
169 * TempCount - Current temporary counter. Was originally
170 * per-module; Currently per method, could be
171 * expanded to per-scope.
172 *
173 * RETURN: A pointer to name (allocated here).
174 *
175 * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
176 * reserved for use by the ASL compiler. (_T_0 through _T_Z)
177 *
178 ******************************************************************************/
179
180static char *
181TrAmlGetNextTempName (
182 ACPI_PARSE_OBJECT *Op,
183 UINT8 *TempCount)
184{
185 char *TempName;
186
187
188 if (*TempCount >= (10+26)) /* 0-35 valid: 0-9 and A-Z for TempName[3] */
189 {
190 /* Too many temps */
191
192 AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
193 return (NULL);
194 }
195
196 TempName = UtLocalCalloc (5);
197
198 if (*TempCount < 10) /* 0-9 */
199 {
200 TempName[3] = (char) (*TempCount + '0');
201 }
202 else /* 10-35: A-Z */
203 {
204 TempName[3] = (char) (*TempCount + ('A' - 10));
205 }
206 (*TempCount)++;
207
208 /* First three characters are always "_T_" */
209
210 TempName[0] = '_';
211 TempName[1] = 'T';
212 TempName[2] = '_';
213
214 return (TempName);
215}
216
217
218/*******************************************************************************
219 *
220 * FUNCTION: TrAmlInitLineNumbers
221 *
222 * PARAMETERS: Op - Op to be initialized
223 * Neighbor - Op used for initialization values
224 *
225 * RETURN: None
226 *
227 * DESCRIPTION: Initialized the various line numbers for a parse node.
228 *
229 ******************************************************************************/
230
231static void
232TrAmlInitLineNumbers (
233 ACPI_PARSE_OBJECT *Op,
234 ACPI_PARSE_OBJECT *Neighbor)
235{
236
237 Op->Asl.EndLine = Neighbor->Asl.EndLine;
238 Op->Asl.EndLogicalLine = Neighbor->Asl.EndLogicalLine;
239 Op->Asl.LineNumber = Neighbor->Asl.LineNumber;
240 Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
241 Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
242}
243
244
245/*******************************************************************************
246 *
247 * FUNCTION: TrAmlInitNode
248 *
249 * PARAMETERS: Op - Op to be initialized
250 * ParseOpcode - Opcode for this node
251 *
252 * RETURN: None
253 *
254 * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
255 *
256 ******************************************************************************/
257
258static void
259TrAmlInitNode (
260 ACPI_PARSE_OBJECT *Op,
261 UINT16 ParseOpcode)
262{
263
264 Op->Asl.ParseOpcode = ParseOpcode;
265 UtSetParseOpName (Op);
266}
267
268
269/*******************************************************************************
270 *
271 * FUNCTION: TrAmlSetSubtreeParent
272 *
273 * PARAMETERS: Op - First node in a list of peer nodes
274 * Parent - Parent of the subtree
275 *
276 * RETURN: None
277 *
278 * DESCRIPTION: Set the parent for all peer nodes in a subtree
279 *
280 ******************************************************************************/
281
282static void
283TrAmlSetSubtreeParent (
284 ACPI_PARSE_OBJECT *Op,
285 ACPI_PARSE_OBJECT *Parent)
286{
287 ACPI_PARSE_OBJECT *Next;
288
289
290 Next = Op;
291 while (Next)
292 {
293 Next->Asl.Parent = Parent;
294 Next = Next->Asl.Next;
295 }
296}
297
298
299/*******************************************************************************
300 *
301 * FUNCTION: TrAmlInsertPeer
302 *
303 * PARAMETERS: Op - First node in a list of peer nodes
304 * NewPeer - Peer node to insert
305 *
306 * RETURN: None
307 *
308 * DESCRIPTION: Insert a new peer node into a list of peers.
309 *
310 ******************************************************************************/
311
312static void
313TrAmlInsertPeer (
314 ACPI_PARSE_OBJECT *Op,
315 ACPI_PARSE_OBJECT *NewPeer)
316{
317
318 NewPeer->Asl.Next = Op->Asl.Next;
319 Op->Asl.Next = NewPeer;
320}
321
322
323/*******************************************************************************
324 *
325 * FUNCTION: TrAmlTransformWalk
326 *
327 * PARAMETERS: ASL_WALK_CALLBACK
328 *
329 * RETURN: None
330 *
331 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
332 * operands.
333 *
334 ******************************************************************************/
335
336ACPI_STATUS
337TrAmlTransformWalk (
338 ACPI_PARSE_OBJECT *Op,
339 UINT32 Level,
340 void *Context)
341{
342
343 TrTransformSubtree (Op);
344 return (AE_OK);
345}
346
347
348/*******************************************************************************
349 *
350 * FUNCTION: TrTransformSubtree
351 *
352 * PARAMETERS: Op - The parent parse node
353 *
354 * RETURN: None
355 *
356 * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
357 * complex AML opcodes require processing of the child nodes
358 * (arguments/operands).
359 *
360 ******************************************************************************/
361
362static void
363TrTransformSubtree (
364 ACPI_PARSE_OBJECT *Op)
365{
366
367 if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
368 {
369 return;
370 }
371
372 switch (Op->Asl.ParseOpcode)
373 {
374 case PARSEOP_DEFINITIONBLOCK:
375 TrDoDefinitionBlock (Op);
376 break;
377
378 case PARSEOP_SWITCH:
379 TrDoSwitch (Op);
380 break;
381
382 case PARSEOP_METHOD:
383
384 /*
385 * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
386 * however
387 */
388 Gbl_TempCount = 0;
389 break;
390
391 default:
392 /* Nothing to do here for other opcodes */
393 break;
394 }
395}
396
397
398/*******************************************************************************
399 *
400 * FUNCTION: TrDoDefinitionBlock
401 *
402 * PARAMETERS: Op - Parse node
403 *
404 * RETURN: None
405 *
406 * DESCRIPTION: Find the end of the definition block and set a global to this
407 * node. It is used by the compiler to insert compiler-generated
408 * names at the root level of the namespace.
409 *
410 ******************************************************************************/
411
412static void
413TrDoDefinitionBlock (
414 ACPI_PARSE_OBJECT *Op)
415{
416 ACPI_PARSE_OBJECT *Next;
417 UINT32 i;
418
419
420 Next = Op->Asl.Child;
421 for (i = 0; i < 5; i++)
422 {
423 Next = Next->Asl.Next;
424 if (i == 0)
425 {
426 /*
427 * This is the table signature. Only the DSDT can be assumed
428 * to be at the root of the namespace; Therefore, namepath
429 * optimization can only be performed on the DSDT.
430 */
431 if (!ACPI_COMPARE_NAME (Next->Asl.Value.String, ACPI_SIG_DSDT))
432 {
433 Gbl_ReferenceOptimizationFlag = FALSE;
434 }
435 }
436 }
437
438 Gbl_FirstLevelInsertionNode = Next;
439}
440
441
442/*******************************************************************************
443 *
444 * FUNCTION: TrDoSwitch
445 *
446 * PARAMETERS: StartNode - Parse node for SWITCH
447 *
448 * RETURN: None
449 *
450 *
451 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
452 * no actual AML opcode for SWITCH -- it must be simulated.
453 *
454 ******************************************************************************/
455
456static void
457TrDoSwitch (
458 ACPI_PARSE_OBJECT *StartNode)
459{
460 ACPI_PARSE_OBJECT *Next;
461 ACPI_PARSE_OBJECT *CaseOp = NULL;
462 ACPI_PARSE_OBJECT *CaseBlock = NULL;
463 ACPI_PARSE_OBJECT *DefaultOp = NULL;
464 ACPI_PARSE_OBJECT *CurrentParentNode;
465 ACPI_PARSE_OBJECT *Conditional = NULL;
466 ACPI_PARSE_OBJECT *Predicate;
467 ACPI_PARSE_OBJECT *Peer;
468 ACPI_PARSE_OBJECT *NewOp;
469 ACPI_PARSE_OBJECT *NewOp2;
470 ACPI_PARSE_OBJECT *MethodOp;
471 char *PredicateValueName;
472 UINT16 Index;
473 UINT32 Btype;
474
475
476 /* Start node is the Switch() node */
477
478 CurrentParentNode = StartNode;
479
480 /* Create a new temp name of the form _T_x */
481
482 PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
483 if (!PredicateValueName)
484 {
485 return;
486 }
487
488 /* First child is the Switch() predicate */
489
490 Next = StartNode->Asl.Child;
491
492 /*
493 * Examine the return type of the Switch Value -
494 * must be Integer/Buffer/String
495 */
496 Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
497 Btype = AslKeywordMapping[Index].AcpiBtype;
498 if ((Btype != ACPI_BTYPE_INTEGER) &&
499 (Btype != ACPI_BTYPE_STRING) &&
500 (Btype != ACPI_BTYPE_BUFFER))
501 {
502 AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
503 Btype = ACPI_BTYPE_INTEGER;
504 }
505
506 /* CASE statements start at next child */
507
508 Peer = Next->Asl.Next;
509 while (Peer)
510 {
511 Next = Peer;
512 Peer = Next->Asl.Next;
513
514 if (Next->Asl.ParseOpcode == PARSEOP_CASE)
515 {
516 if (CaseOp)
517 {
518 /* Add an ELSE to complete the previous CASE */
519
520 if (!Conditional)
521 {
522 return;
523 }
524 NewOp = TrCreateLeafNode (PARSEOP_ELSE);
525 NewOp->Asl.Parent = Conditional->Asl.Parent;
526 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
527
528 /* Link ELSE node as a peer to the previous IF */
529
530 TrAmlInsertPeer (Conditional, NewOp);
531 CurrentParentNode = NewOp;
532 }
533
534 CaseOp = Next;
535 Conditional = CaseOp;
536 CaseBlock = CaseOp->Asl.Child->Asl.Next;
537 Conditional->Asl.Child->Asl.Next = NULL;
538 Predicate = CaseOp->Asl.Child;
539
540 if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
541 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
542 {
543 /*
544 * Convert the package declaration to this form:
545 *
546 * If (LNotEqual (Match (Package(<size>){<data>},
547 * MEQ, _T_x, MTR, Zero, Zero), Ones))
548 */
549 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
550 Predicate->Asl.Next = NewOp2;
551 TrAmlInitLineNumbers (NewOp2, Conditional);
552
553 NewOp = NewOp2;
554 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
555 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
556 NewOp->Asl.Next = NewOp2;
557 TrAmlInitLineNumbers (NewOp2, Predicate);
558
559 NewOp = NewOp2;
560 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
561 NewOp->Asl.Next = NewOp2;
562 TrAmlInitLineNumbers (NewOp2, Predicate);
563
564 NewOp = NewOp2;
565 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
566 NewOp->Asl.Next = NewOp2;
567 TrAmlInitLineNumbers (NewOp2, Predicate);
568
569 NewOp = NewOp2;
570 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
571 NewOp->Asl.Next = NewOp2;
572 TrAmlInitLineNumbers (NewOp2, Predicate);
573
574 NewOp2 = TrCreateLeafNode (PARSEOP_MATCH);
575 NewOp2->Asl.Child = Predicate; /* PARSEOP_PACKAGE */
576 TrAmlInitLineNumbers (NewOp2, Conditional);
577 TrAmlSetSubtreeParent (Predicate, NewOp2);
578
579 NewOp = NewOp2;
580 NewOp2 = TrCreateLeafNode (PARSEOP_ONES);
581 NewOp->Asl.Next = NewOp2;
582 TrAmlInitLineNumbers (NewOp2, Conditional);
583
584 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
585 NewOp2->Asl.Child = NewOp;
586 NewOp->Asl.Parent = NewOp2;
587 TrAmlInitLineNumbers (NewOp2, Conditional);
588 TrAmlSetSubtreeParent (NewOp, NewOp2);
589
590 NewOp = NewOp2;
591 NewOp2 = TrCreateLeafNode (PARSEOP_LNOT);
592 NewOp2->Asl.Child = NewOp;
593 NewOp2->Asl.Parent = Conditional;
594 NewOp->Asl.Parent = NewOp2;
595 TrAmlInitLineNumbers (NewOp2, Conditional);
596
597 Conditional->Asl.Child = NewOp2;
598 NewOp2->Asl.Next = CaseBlock;
599 }
600 else
601 {
602 /*
603 * Integer and Buffer case.
604 *
605 * Change CaseOp() to: If (LEqual (SwitchValue, CaseValue)) {...}
606 * Note: SwitchValue is first to allow the CaseValue to be implicitly
607 * converted to the type of SwitchValue if necessary.
608 *
609 * CaseOp->Child is the case value
610 * CaseOp->Child->Peer is the beginning of the case block
611 */
612 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
613 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
614 NewOp->Asl.Next = Predicate;
615 TrAmlInitLineNumbers (NewOp, Predicate);
616
617 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
618 NewOp2->Asl.Parent = Conditional;
619 NewOp2->Asl.Child = NewOp;
620 TrAmlInitLineNumbers (NewOp2, Conditional);
621
622 TrAmlSetSubtreeParent (NewOp, NewOp2);
623
624 Predicate = NewOp2;
625 Predicate->Asl.Next = CaseBlock;
626
627 TrAmlSetSubtreeParent (Predicate, Conditional);
628 Conditional->Asl.Child = Predicate;
629 }
630
631 /* Reinitialize the CASE node to an IF node */
632
633 TrAmlInitNode (Conditional, PARSEOP_IF);
634
635 /*
636 * The first CASE(IF) is not nested under an ELSE.
637 * All other CASEs are children of a parent ELSE.
638 */
639 if (CurrentParentNode == StartNode)
640 {
641 Conditional->Asl.Parent = CurrentParentNode->Asl.Parent;
642
643 /* Link IF into the peer list */
644
645 TrAmlInsertPeer (CurrentParentNode, Conditional);
646 }
647 else
648 {
649 /*
650 * The IF is a child of previous IF/ELSE. It
651 * is therefore without peer.
652 */
653 CurrentParentNode->Asl.Child = Conditional;
654 Conditional->Asl.Parent = CurrentParentNode;
655 Conditional->Asl.Next = NULL;
656 }
657 }
658 else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
659 {
660 if (DefaultOp)
661 {
662 /*
663 * More than one Default
664 * (Parser does not catch this, must check here)
665 */
666 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
667 }
668 else
669 {
670 /* Save the DEFAULT node for later, after CASEs */
671
672 DefaultOp = Next;
673 }
674 }
675 else
676 {
677 /* Unknown peer opcode */
678
679 AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%d)\n",
680 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
681 }
682 }
683
684 /* Add the default case at the end of the if/else construct */
685
686 if (DefaultOp)
687 {
688 /* If no CASE statements, this is an error - see below */
689
690 if (CaseOp)
691 {
692 /* Convert the DEFAULT node to an ELSE */
693
694 if (!Conditional)
695 {
696 return;
697 }
698 TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
699 DefaultOp->Asl.Parent = Conditional->Asl.Parent;
700
701 /* Link ELSE node as a peer to the previous IF */
702
703 TrAmlInsertPeer (Conditional, DefaultOp);
704 }
705 }
706
707 if (!CaseOp)
708 {
709 AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
710 }
711
712
713 /*
714 * Create a Name(_T_x, ...) statement. This statement must appear at the
715 * method level, in case a loop surrounds the switch statement and could
716 * cause the name to be created twice (error).
717 */
718
719 /* Create the Name node */
720
721 Predicate = StartNode->Asl.Child;
722 NewOp = TrCreateLeafNode (PARSEOP_NAME);
723
724 /* Find the parent method */
725
726 Next = StartNode;
727 while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
728 (Next->Asl.ParseOpcode != PARSEOP_DEFINITIONBLOCK))
729 {
730 Next = Next->Asl.Parent;
731 }
732 MethodOp = Next;
733
734 NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
735 NewOp->Asl.Parent = Next;
736
737 /* Insert name after the method name and arguments */
738
739 Next = Next->Asl.Child; /* Name */
740 Next = Next->Asl.Next; /* NumArgs */
741 Next = Next->Asl.Next; /* SerializeRule */
742
743 /*
744 * If method is not Serialized, we must make is so, because of the way
745 * that Switch() must be implemented -- we cannot allow multiple threads
746 * to execute this method concurrently since we need to create local
747 * temporary name(s).
748 */
749 if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
750 {
751 AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp, "Due to use of Switch operator");
752 Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
753 }
754
755 Next = Next->Asl.Next; /* SyncLevel */
756 Next = Next->Asl.Next; /* ReturnType */
757 Next = Next->Asl.Next; /* ParameterTypes */
758
759 TrAmlInsertPeer (Next, NewOp);
760 TrAmlInitLineNumbers (NewOp, Next);
761
762 /* Create the NameSeg child for the Name node */
763
764 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
765 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
766 NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
767 NewOp->Asl.Child = NewOp2;
768
769 /* Create the initial value for the Name. Btype was already validated above */
770
771 switch (Btype)
772 {
773 case ACPI_BTYPE_INTEGER:
774 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_ZERO,
775 (ACPI_INTEGER) 0);
776 break;
777
778 case ACPI_BTYPE_STRING:
779 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL,
780 (ACPI_INTEGER) ACPI_TO_INTEGER (""));
781 break;
782
783 case ACPI_BTYPE_BUFFER:
784 (void) TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER,
785 (ACPI_INTEGER) 0));
786 Next = NewOp2->Asl.Next;
787 (void) TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO,
788 (ACPI_INTEGER) 1));
789 (void) TrLinkPeerNode (Next->Asl.Child,
790 TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (ACPI_INTEGER) 0));
791
792 TrAmlSetSubtreeParent (Next->Asl.Child, Next);
793 break;
794
795 default:
796 break;
797 }
798
799 TrAmlSetSubtreeParent (NewOp2, NewOp);
800
801 /*
802 * Transform the Switch() into a Store() node which will be used to save the
803 * Switch() value. The store is of the form: Store (Value, _T_x)
804 * where _T_x is the temp variable.
805 */
806 TrAmlInitNode (StartNode, PARSEOP_STORE);
807 StartNode->Asl.Child = NULL;
808
809 /* Complete the Store subtree */
810
811 StartNode->Asl.Child = Predicate;
812 Predicate->Asl.Parent = StartNode;
813
814 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
815 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
816 NewOp->Asl.Parent = StartNode;
817 Predicate->Asl.Next = NewOp;
818}
819
820
2/******************************************************************************
3 *
4 * Module Name: asltransform - Parse tree transforms
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999 - 2009, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
21 *
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
28 *
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
54 *
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
66 *
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117
118#include <contrib/dev/acpica/compiler/aslcompiler.h>
119#include "aslcompiler.y.h"
120
121#define _COMPONENT ACPI_COMPILER
122 ACPI_MODULE_NAME ("asltransform")
123
124/* Local prototypes */
125
126static void
127TrTransformSubtree (
128 ACPI_PARSE_OBJECT *Op);
129
130static char *
131TrAmlGetNextTempName (
132 ACPI_PARSE_OBJECT *Op,
133 UINT8 *TempCount);
134
135static void
136TrAmlInitLineNumbers (
137 ACPI_PARSE_OBJECT *Op,
138 ACPI_PARSE_OBJECT *Neighbor);
139
140static void
141TrAmlInitNode (
142 ACPI_PARSE_OBJECT *Op,
143 UINT16 ParseOpcode);
144
145static void
146TrAmlSetSubtreeParent (
147 ACPI_PARSE_OBJECT *Op,
148 ACPI_PARSE_OBJECT *Parent);
149
150static void
151TrAmlInsertPeer (
152 ACPI_PARSE_OBJECT *Op,
153 ACPI_PARSE_OBJECT *NewPeer);
154
155static void
156TrDoDefinitionBlock (
157 ACPI_PARSE_OBJECT *Op);
158
159static void
160TrDoSwitch (
161 ACPI_PARSE_OBJECT *StartNode);
162
163
164/*******************************************************************************
165 *
166 * FUNCTION: TrAmlGetNextTempName
167 *
168 * PARAMETERS: Op - Current parse op
169 * TempCount - Current temporary counter. Was originally
170 * per-module; Currently per method, could be
171 * expanded to per-scope.
172 *
173 * RETURN: A pointer to name (allocated here).
174 *
175 * DESCRIPTION: Generate an ACPI name of the form _T_x. These names are
176 * reserved for use by the ASL compiler. (_T_0 through _T_Z)
177 *
178 ******************************************************************************/
179
180static char *
181TrAmlGetNextTempName (
182 ACPI_PARSE_OBJECT *Op,
183 UINT8 *TempCount)
184{
185 char *TempName;
186
187
188 if (*TempCount >= (10+26)) /* 0-35 valid: 0-9 and A-Z for TempName[3] */
189 {
190 /* Too many temps */
191
192 AslError (ASL_ERROR, ASL_MSG_TOO_MANY_TEMPS, Op, NULL);
193 return (NULL);
194 }
195
196 TempName = UtLocalCalloc (5);
197
198 if (*TempCount < 10) /* 0-9 */
199 {
200 TempName[3] = (char) (*TempCount + '0');
201 }
202 else /* 10-35: A-Z */
203 {
204 TempName[3] = (char) (*TempCount + ('A' - 10));
205 }
206 (*TempCount)++;
207
208 /* First three characters are always "_T_" */
209
210 TempName[0] = '_';
211 TempName[1] = 'T';
212 TempName[2] = '_';
213
214 return (TempName);
215}
216
217
218/*******************************************************************************
219 *
220 * FUNCTION: TrAmlInitLineNumbers
221 *
222 * PARAMETERS: Op - Op to be initialized
223 * Neighbor - Op used for initialization values
224 *
225 * RETURN: None
226 *
227 * DESCRIPTION: Initialized the various line numbers for a parse node.
228 *
229 ******************************************************************************/
230
231static void
232TrAmlInitLineNumbers (
233 ACPI_PARSE_OBJECT *Op,
234 ACPI_PARSE_OBJECT *Neighbor)
235{
236
237 Op->Asl.EndLine = Neighbor->Asl.EndLine;
238 Op->Asl.EndLogicalLine = Neighbor->Asl.EndLogicalLine;
239 Op->Asl.LineNumber = Neighbor->Asl.LineNumber;
240 Op->Asl.LogicalByteOffset = Neighbor->Asl.LogicalByteOffset;
241 Op->Asl.LogicalLineNumber = Neighbor->Asl.LogicalLineNumber;
242}
243
244
245/*******************************************************************************
246 *
247 * FUNCTION: TrAmlInitNode
248 *
249 * PARAMETERS: Op - Op to be initialized
250 * ParseOpcode - Opcode for this node
251 *
252 * RETURN: None
253 *
254 * DESCRIPTION: Initialize a node with the parse opcode and opcode name.
255 *
256 ******************************************************************************/
257
258static void
259TrAmlInitNode (
260 ACPI_PARSE_OBJECT *Op,
261 UINT16 ParseOpcode)
262{
263
264 Op->Asl.ParseOpcode = ParseOpcode;
265 UtSetParseOpName (Op);
266}
267
268
269/*******************************************************************************
270 *
271 * FUNCTION: TrAmlSetSubtreeParent
272 *
273 * PARAMETERS: Op - First node in a list of peer nodes
274 * Parent - Parent of the subtree
275 *
276 * RETURN: None
277 *
278 * DESCRIPTION: Set the parent for all peer nodes in a subtree
279 *
280 ******************************************************************************/
281
282static void
283TrAmlSetSubtreeParent (
284 ACPI_PARSE_OBJECT *Op,
285 ACPI_PARSE_OBJECT *Parent)
286{
287 ACPI_PARSE_OBJECT *Next;
288
289
290 Next = Op;
291 while (Next)
292 {
293 Next->Asl.Parent = Parent;
294 Next = Next->Asl.Next;
295 }
296}
297
298
299/*******************************************************************************
300 *
301 * FUNCTION: TrAmlInsertPeer
302 *
303 * PARAMETERS: Op - First node in a list of peer nodes
304 * NewPeer - Peer node to insert
305 *
306 * RETURN: None
307 *
308 * DESCRIPTION: Insert a new peer node into a list of peers.
309 *
310 ******************************************************************************/
311
312static void
313TrAmlInsertPeer (
314 ACPI_PARSE_OBJECT *Op,
315 ACPI_PARSE_OBJECT *NewPeer)
316{
317
318 NewPeer->Asl.Next = Op->Asl.Next;
319 Op->Asl.Next = NewPeer;
320}
321
322
323/*******************************************************************************
324 *
325 * FUNCTION: TrAmlTransformWalk
326 *
327 * PARAMETERS: ASL_WALK_CALLBACK
328 *
329 * RETURN: None
330 *
331 * DESCRIPTION: Parse tree walk to generate both the AML opcodes and the AML
332 * operands.
333 *
334 ******************************************************************************/
335
336ACPI_STATUS
337TrAmlTransformWalk (
338 ACPI_PARSE_OBJECT *Op,
339 UINT32 Level,
340 void *Context)
341{
342
343 TrTransformSubtree (Op);
344 return (AE_OK);
345}
346
347
348/*******************************************************************************
349 *
350 * FUNCTION: TrTransformSubtree
351 *
352 * PARAMETERS: Op - The parent parse node
353 *
354 * RETURN: None
355 *
356 * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
357 * complex AML opcodes require processing of the child nodes
358 * (arguments/operands).
359 *
360 ******************************************************************************/
361
362static void
363TrTransformSubtree (
364 ACPI_PARSE_OBJECT *Op)
365{
366
367 if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
368 {
369 return;
370 }
371
372 switch (Op->Asl.ParseOpcode)
373 {
374 case PARSEOP_DEFINITIONBLOCK:
375 TrDoDefinitionBlock (Op);
376 break;
377
378 case PARSEOP_SWITCH:
379 TrDoSwitch (Op);
380 break;
381
382 case PARSEOP_METHOD:
383
384 /*
385 * TBD: Zero the tempname (_T_x) count. Probably shouldn't be a global,
386 * however
387 */
388 Gbl_TempCount = 0;
389 break;
390
391 default:
392 /* Nothing to do here for other opcodes */
393 break;
394 }
395}
396
397
398/*******************************************************************************
399 *
400 * FUNCTION: TrDoDefinitionBlock
401 *
402 * PARAMETERS: Op - Parse node
403 *
404 * RETURN: None
405 *
406 * DESCRIPTION: Find the end of the definition block and set a global to this
407 * node. It is used by the compiler to insert compiler-generated
408 * names at the root level of the namespace.
409 *
410 ******************************************************************************/
411
412static void
413TrDoDefinitionBlock (
414 ACPI_PARSE_OBJECT *Op)
415{
416 ACPI_PARSE_OBJECT *Next;
417 UINT32 i;
418
419
420 Next = Op->Asl.Child;
421 for (i = 0; i < 5; i++)
422 {
423 Next = Next->Asl.Next;
424 if (i == 0)
425 {
426 /*
427 * This is the table signature. Only the DSDT can be assumed
428 * to be at the root of the namespace; Therefore, namepath
429 * optimization can only be performed on the DSDT.
430 */
431 if (!ACPI_COMPARE_NAME (Next->Asl.Value.String, ACPI_SIG_DSDT))
432 {
433 Gbl_ReferenceOptimizationFlag = FALSE;
434 }
435 }
436 }
437
438 Gbl_FirstLevelInsertionNode = Next;
439}
440
441
442/*******************************************************************************
443 *
444 * FUNCTION: TrDoSwitch
445 *
446 * PARAMETERS: StartNode - Parse node for SWITCH
447 *
448 * RETURN: None
449 *
450 *
451 * DESCRIPTION: Translate ASL SWITCH statement to if/else pairs. There is
452 * no actual AML opcode for SWITCH -- it must be simulated.
453 *
454 ******************************************************************************/
455
456static void
457TrDoSwitch (
458 ACPI_PARSE_OBJECT *StartNode)
459{
460 ACPI_PARSE_OBJECT *Next;
461 ACPI_PARSE_OBJECT *CaseOp = NULL;
462 ACPI_PARSE_OBJECT *CaseBlock = NULL;
463 ACPI_PARSE_OBJECT *DefaultOp = NULL;
464 ACPI_PARSE_OBJECT *CurrentParentNode;
465 ACPI_PARSE_OBJECT *Conditional = NULL;
466 ACPI_PARSE_OBJECT *Predicate;
467 ACPI_PARSE_OBJECT *Peer;
468 ACPI_PARSE_OBJECT *NewOp;
469 ACPI_PARSE_OBJECT *NewOp2;
470 ACPI_PARSE_OBJECT *MethodOp;
471 char *PredicateValueName;
472 UINT16 Index;
473 UINT32 Btype;
474
475
476 /* Start node is the Switch() node */
477
478 CurrentParentNode = StartNode;
479
480 /* Create a new temp name of the form _T_x */
481
482 PredicateValueName = TrAmlGetNextTempName (StartNode, &Gbl_TempCount);
483 if (!PredicateValueName)
484 {
485 return;
486 }
487
488 /* First child is the Switch() predicate */
489
490 Next = StartNode->Asl.Child;
491
492 /*
493 * Examine the return type of the Switch Value -
494 * must be Integer/Buffer/String
495 */
496 Index = (UINT16) (Next->Asl.ParseOpcode - ASL_PARSE_OPCODE_BASE);
497 Btype = AslKeywordMapping[Index].AcpiBtype;
498 if ((Btype != ACPI_BTYPE_INTEGER) &&
499 (Btype != ACPI_BTYPE_STRING) &&
500 (Btype != ACPI_BTYPE_BUFFER))
501 {
502 AslError (ASL_WARNING, ASL_MSG_SWITCH_TYPE, Next, NULL);
503 Btype = ACPI_BTYPE_INTEGER;
504 }
505
506 /* CASE statements start at next child */
507
508 Peer = Next->Asl.Next;
509 while (Peer)
510 {
511 Next = Peer;
512 Peer = Next->Asl.Next;
513
514 if (Next->Asl.ParseOpcode == PARSEOP_CASE)
515 {
516 if (CaseOp)
517 {
518 /* Add an ELSE to complete the previous CASE */
519
520 if (!Conditional)
521 {
522 return;
523 }
524 NewOp = TrCreateLeafNode (PARSEOP_ELSE);
525 NewOp->Asl.Parent = Conditional->Asl.Parent;
526 TrAmlInitLineNumbers (NewOp, NewOp->Asl.Parent);
527
528 /* Link ELSE node as a peer to the previous IF */
529
530 TrAmlInsertPeer (Conditional, NewOp);
531 CurrentParentNode = NewOp;
532 }
533
534 CaseOp = Next;
535 Conditional = CaseOp;
536 CaseBlock = CaseOp->Asl.Child->Asl.Next;
537 Conditional->Asl.Child->Asl.Next = NULL;
538 Predicate = CaseOp->Asl.Child;
539
540 if ((Predicate->Asl.ParseOpcode == PARSEOP_PACKAGE) ||
541 (Predicate->Asl.ParseOpcode == PARSEOP_VAR_PACKAGE))
542 {
543 /*
544 * Convert the package declaration to this form:
545 *
546 * If (LNotEqual (Match (Package(<size>){<data>},
547 * MEQ, _T_x, MTR, Zero, Zero), Ones))
548 */
549 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MEQ);
550 Predicate->Asl.Next = NewOp2;
551 TrAmlInitLineNumbers (NewOp2, Conditional);
552
553 NewOp = NewOp2;
554 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
555 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
556 NewOp->Asl.Next = NewOp2;
557 TrAmlInitLineNumbers (NewOp2, Predicate);
558
559 NewOp = NewOp2;
560 NewOp2 = TrCreateLeafNode (PARSEOP_MATCHTYPE_MTR);
561 NewOp->Asl.Next = NewOp2;
562 TrAmlInitLineNumbers (NewOp2, Predicate);
563
564 NewOp = NewOp2;
565 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
566 NewOp->Asl.Next = NewOp2;
567 TrAmlInitLineNumbers (NewOp2, Predicate);
568
569 NewOp = NewOp2;
570 NewOp2 = TrCreateLeafNode (PARSEOP_ZERO);
571 NewOp->Asl.Next = NewOp2;
572 TrAmlInitLineNumbers (NewOp2, Predicate);
573
574 NewOp2 = TrCreateLeafNode (PARSEOP_MATCH);
575 NewOp2->Asl.Child = Predicate; /* PARSEOP_PACKAGE */
576 TrAmlInitLineNumbers (NewOp2, Conditional);
577 TrAmlSetSubtreeParent (Predicate, NewOp2);
578
579 NewOp = NewOp2;
580 NewOp2 = TrCreateLeafNode (PARSEOP_ONES);
581 NewOp->Asl.Next = NewOp2;
582 TrAmlInitLineNumbers (NewOp2, Conditional);
583
584 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
585 NewOp2->Asl.Child = NewOp;
586 NewOp->Asl.Parent = NewOp2;
587 TrAmlInitLineNumbers (NewOp2, Conditional);
588 TrAmlSetSubtreeParent (NewOp, NewOp2);
589
590 NewOp = NewOp2;
591 NewOp2 = TrCreateLeafNode (PARSEOP_LNOT);
592 NewOp2->Asl.Child = NewOp;
593 NewOp2->Asl.Parent = Conditional;
594 NewOp->Asl.Parent = NewOp2;
595 TrAmlInitLineNumbers (NewOp2, Conditional);
596
597 Conditional->Asl.Child = NewOp2;
598 NewOp2->Asl.Next = CaseBlock;
599 }
600 else
601 {
602 /*
603 * Integer and Buffer case.
604 *
605 * Change CaseOp() to: If (LEqual (SwitchValue, CaseValue)) {...}
606 * Note: SwitchValue is first to allow the CaseValue to be implicitly
607 * converted to the type of SwitchValue if necessary.
608 *
609 * CaseOp->Child is the case value
610 * CaseOp->Child->Peer is the beginning of the case block
611 */
612 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESTRING,
613 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
614 NewOp->Asl.Next = Predicate;
615 TrAmlInitLineNumbers (NewOp, Predicate);
616
617 NewOp2 = TrCreateLeafNode (PARSEOP_LEQUAL);
618 NewOp2->Asl.Parent = Conditional;
619 NewOp2->Asl.Child = NewOp;
620 TrAmlInitLineNumbers (NewOp2, Conditional);
621
622 TrAmlSetSubtreeParent (NewOp, NewOp2);
623
624 Predicate = NewOp2;
625 Predicate->Asl.Next = CaseBlock;
626
627 TrAmlSetSubtreeParent (Predicate, Conditional);
628 Conditional->Asl.Child = Predicate;
629 }
630
631 /* Reinitialize the CASE node to an IF node */
632
633 TrAmlInitNode (Conditional, PARSEOP_IF);
634
635 /*
636 * The first CASE(IF) is not nested under an ELSE.
637 * All other CASEs are children of a parent ELSE.
638 */
639 if (CurrentParentNode == StartNode)
640 {
641 Conditional->Asl.Parent = CurrentParentNode->Asl.Parent;
642
643 /* Link IF into the peer list */
644
645 TrAmlInsertPeer (CurrentParentNode, Conditional);
646 }
647 else
648 {
649 /*
650 * The IF is a child of previous IF/ELSE. It
651 * is therefore without peer.
652 */
653 CurrentParentNode->Asl.Child = Conditional;
654 Conditional->Asl.Parent = CurrentParentNode;
655 Conditional->Asl.Next = NULL;
656 }
657 }
658 else if (Next->Asl.ParseOpcode == PARSEOP_DEFAULT)
659 {
660 if (DefaultOp)
661 {
662 /*
663 * More than one Default
664 * (Parser does not catch this, must check here)
665 */
666 AslError (ASL_ERROR, ASL_MSG_MULTIPLE_DEFAULT, Next, NULL);
667 }
668 else
669 {
670 /* Save the DEFAULT node for later, after CASEs */
671
672 DefaultOp = Next;
673 }
674 }
675 else
676 {
677 /* Unknown peer opcode */
678
679 AcpiOsPrintf ("Unknown parse opcode for switch statement: %s (%d)\n",
680 Next->Asl.ParseOpName, Next->Asl.ParseOpcode);
681 }
682 }
683
684 /* Add the default case at the end of the if/else construct */
685
686 if (DefaultOp)
687 {
688 /* If no CASE statements, this is an error - see below */
689
690 if (CaseOp)
691 {
692 /* Convert the DEFAULT node to an ELSE */
693
694 if (!Conditional)
695 {
696 return;
697 }
698 TrAmlInitNode (DefaultOp, PARSEOP_ELSE);
699 DefaultOp->Asl.Parent = Conditional->Asl.Parent;
700
701 /* Link ELSE node as a peer to the previous IF */
702
703 TrAmlInsertPeer (Conditional, DefaultOp);
704 }
705 }
706
707 if (!CaseOp)
708 {
709 AslError (ASL_ERROR, ASL_MSG_NO_CASES, StartNode, NULL);
710 }
711
712
713 /*
714 * Create a Name(_T_x, ...) statement. This statement must appear at the
715 * method level, in case a loop surrounds the switch statement and could
716 * cause the name to be created twice (error).
717 */
718
719 /* Create the Name node */
720
721 Predicate = StartNode->Asl.Child;
722 NewOp = TrCreateLeafNode (PARSEOP_NAME);
723
724 /* Find the parent method */
725
726 Next = StartNode;
727 while ((Next->Asl.ParseOpcode != PARSEOP_METHOD) &&
728 (Next->Asl.ParseOpcode != PARSEOP_DEFINITIONBLOCK))
729 {
730 Next = Next->Asl.Parent;
731 }
732 MethodOp = Next;
733
734 NewOp->Asl.CompileFlags |= NODE_COMPILER_EMITTED;
735 NewOp->Asl.Parent = Next;
736
737 /* Insert name after the method name and arguments */
738
739 Next = Next->Asl.Child; /* Name */
740 Next = Next->Asl.Next; /* NumArgs */
741 Next = Next->Asl.Next; /* SerializeRule */
742
743 /*
744 * If method is not Serialized, we must make is so, because of the way
745 * that Switch() must be implemented -- we cannot allow multiple threads
746 * to execute this method concurrently since we need to create local
747 * temporary name(s).
748 */
749 if (Next->Asl.ParseOpcode != PARSEOP_SERIALIZERULE_SERIAL)
750 {
751 AslError (ASL_REMARK, ASL_MSG_SERIALIZED, MethodOp, "Due to use of Switch operator");
752 Next->Asl.ParseOpcode = PARSEOP_SERIALIZERULE_SERIAL;
753 }
754
755 Next = Next->Asl.Next; /* SyncLevel */
756 Next = Next->Asl.Next; /* ReturnType */
757 Next = Next->Asl.Next; /* ParameterTypes */
758
759 TrAmlInsertPeer (Next, NewOp);
760 TrAmlInitLineNumbers (NewOp, Next);
761
762 /* Create the NameSeg child for the Name node */
763
764 NewOp2 = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
765 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
766 NewOp2->Asl.CompileFlags |= NODE_IS_NAME_DECLARATION;
767 NewOp->Asl.Child = NewOp2;
768
769 /* Create the initial value for the Name. Btype was already validated above */
770
771 switch (Btype)
772 {
773 case ACPI_BTYPE_INTEGER:
774 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_ZERO,
775 (ACPI_INTEGER) 0);
776 break;
777
778 case ACPI_BTYPE_STRING:
779 NewOp2->Asl.Next = TrCreateValuedLeafNode (PARSEOP_STRING_LITERAL,
780 (ACPI_INTEGER) ACPI_TO_INTEGER (""));
781 break;
782
783 case ACPI_BTYPE_BUFFER:
784 (void) TrLinkPeerNode (NewOp2, TrCreateValuedLeafNode (PARSEOP_BUFFER,
785 (ACPI_INTEGER) 0));
786 Next = NewOp2->Asl.Next;
787 (void) TrLinkChildren (Next, 1, TrCreateValuedLeafNode (PARSEOP_ZERO,
788 (ACPI_INTEGER) 1));
789 (void) TrLinkPeerNode (Next->Asl.Child,
790 TrCreateValuedLeafNode (PARSEOP_DEFAULT_ARG, (ACPI_INTEGER) 0));
791
792 TrAmlSetSubtreeParent (Next->Asl.Child, Next);
793 break;
794
795 default:
796 break;
797 }
798
799 TrAmlSetSubtreeParent (NewOp2, NewOp);
800
801 /*
802 * Transform the Switch() into a Store() node which will be used to save the
803 * Switch() value. The store is of the form: Store (Value, _T_x)
804 * where _T_x is the temp variable.
805 */
806 TrAmlInitNode (StartNode, PARSEOP_STORE);
807 StartNode->Asl.Child = NULL;
808
809 /* Complete the Store subtree */
810
811 StartNode->Asl.Child = Predicate;
812 Predicate->Asl.Parent = StartNode;
813
814 NewOp = TrCreateValuedLeafNode (PARSEOP_NAMESEG,
815 (ACPI_INTEGER) ACPI_TO_INTEGER (PredicateValueName));
816 NewOp->Asl.Parent = StartNode;
817 Predicate->Asl.Next = NewOp;
818}
819
820