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