xref: /freebsd/sys/contrib/dev/acpica/compiler/asloperands.c (revision 44b0f624aee0d43179bf3874c70e03b6ae49a4db)
1 /******************************************************************************
2  *
3  * Module Name: asloperands - AML operand processing
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2019, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
154 #include <contrib/dev/acpica/include/amlcode.h>
155 
156 #define _COMPONENT          ACPI_COMPILER
157         ACPI_MODULE_NAME    ("asloperands")
158 
159 /* Local prototypes */
160 
161 static void
162 OpnDoField (
163     ACPI_PARSE_OBJECT       *Op);
164 
165 static void
166 OpnDoBankField (
167     ACPI_PARSE_OBJECT       *Op);
168 
169 static void
170 OpnDoBuffer (
171     ACPI_PARSE_OBJECT       *Op);
172 
173 static void
174 OpnDoDefinitionBlock (
175     ACPI_PARSE_OBJECT       *Op);
176 
177 static void
178 OpnDoFieldCommon (
179     ACPI_PARSE_OBJECT       *FieldOp,
180     ACPI_PARSE_OBJECT       *Op);
181 
182 static void
183 OpnDoIndexField (
184     ACPI_PARSE_OBJECT       *Op);
185 
186 static void
187 OpnDoLoadTable (
188     ACPI_PARSE_OBJECT       *Op);
189 
190 static void
191 OpnDoMethod (
192     ACPI_PARSE_OBJECT       *Op);
193 
194 static void
195 OpnDoMutex (
196     ACPI_PARSE_OBJECT       *Op);
197 
198 static void
199 OpnDoRegion (
200     ACPI_PARSE_OBJECT       *Op);
201 
202 static void
203 OpnAttachNameToNode (
204     ACPI_PARSE_OBJECT       *Op);
205 
206 
207 /*******************************************************************************
208  *
209  * FUNCTION:    OpnDoMutex
210  *
211  * PARAMETERS:  Op        - The parent parse node
212  *
213  * RETURN:      None
214  *
215  * DESCRIPTION: Construct the operands for the MUTEX ASL keyword.
216  *
217  ******************************************************************************/
218 
219 static void
220 OpnDoMutex (
221     ACPI_PARSE_OBJECT       *Op)
222 {
223     ACPI_PARSE_OBJECT       *Next;
224 
225 
226     Next = Op->Asl.Child;
227     Next = Next->Asl.Next;
228 
229     if (Next->Asl.Value.Integer > 15)
230     {
231         AslError (ASL_ERROR, ASL_MSG_SYNC_LEVEL, Next, NULL);
232     }
233     return;
234 }
235 
236 
237 /*******************************************************************************
238  *
239  * FUNCTION:    OpnDoMethod
240  *
241  * PARAMETERS:  Op        - The parent parse node
242  *
243  * RETURN:      None
244  *
245  * DESCRIPTION: Construct the operands for the METHOD ASL keyword.
246  *
247  ******************************************************************************/
248 
249 static void
250 OpnDoMethod (
251     ACPI_PARSE_OBJECT       *Op)
252 {
253     ACPI_PARSE_OBJECT       *Next;
254 
255     /* Optional arguments for this opcode with defaults */
256 
257     UINT8                   NumArgs = 0;
258     UINT8                   Serialized = 0;
259     UINT8                   Concurrency = 0;
260     UINT8                   MethodFlags;
261 
262 
263     /* Opcode and package length first */
264     /* Method name */
265 
266     Next = Op->Asl.Child;
267 
268     /* Num args */
269 
270     Next = Next->Asl.Next;
271     if (Next->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
272     {
273         NumArgs = (UINT8) Next->Asl.Value.Integer;
274         Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
275     }
276 
277     /* Serialized Flag */
278 
279     Next = Next->Asl.Next;
280     if (Next->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
281     {
282         Serialized = (UINT8) Next->Asl.Value.Integer;
283         Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
284     }
285 
286     /* Concurrency value (valid values are 0-15) */
287 
288     Next = Next->Asl.Next;
289     if (Next->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
290     {
291         /* This is a ByteConstExpr, so eval the constant now */
292 
293         OpcAmlConstantWalk (Next, 0, NULL);
294 
295         if (Next->Asl.Value.Integer > 15)
296         {
297             AslError (ASL_ERROR, ASL_MSG_SYNC_LEVEL, Next, NULL);
298         }
299 
300         Concurrency = (UINT8) Next->Asl.Value.Integer;
301     }
302 
303     /* Put the bits in their proper places */
304 
305     MethodFlags = (UINT8)
306         ((NumArgs & 0x7) |
307         ((Serialized & 0x1) << 3) |
308         ((Concurrency & 0xF) << 4));
309 
310     /* Use the last node for the combined flags byte */
311 
312     Next->Asl.Value.Integer = MethodFlags;
313     Next->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
314     Next->Asl.AmlLength = 1;
315     Next->Asl.ParseOpcode = PARSEOP_RAW_DATA;
316 
317     /* Save the arg count in the first node */
318 
319     Op->Asl.Extra = NumArgs;
320 }
321 
322 
323 /*******************************************************************************
324  *
325  * FUNCTION:    OpnDoFieldCommon
326  *
327  * PARAMETERS:  FieldOp       - Node for an ASL field
328  *              Op            - The parent parse node
329  *
330  * RETURN:      None
331  *
332  * DESCRIPTION: Construct the AML operands for the various field keywords,
333  *              FIELD, BANKFIELD, INDEXFIELD
334  *
335  ******************************************************************************/
336 
337 static void
338 OpnDoFieldCommon (
339     ACPI_PARSE_OBJECT       *FieldOp,
340     ACPI_PARSE_OBJECT       *Op)
341 {
342     ACPI_PARSE_OBJECT       *Next;
343     ACPI_PARSE_OBJECT       *PkgLengthNode;
344     UINT32                  CurrentBitOffset;
345     UINT32                  NewBitOffset;
346     UINT8                   AccessType;
347     UINT8                   LockRule;
348     UINT8                   UpdateRule;
349     UINT8                   FieldFlags;
350     UINT32                  MinimumLength;
351 
352 
353     /* AccessType -- not optional, so no need to check for DEFAULT_ARG */
354 
355     AccessType = (UINT8) Op->Asl.Value.Integer;
356     Op->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
357 
358     /* Set the access type in the parent (field) node for use later */
359 
360     FieldOp->Asl.Value.Integer = AccessType;
361 
362     /* LockRule -- not optional, so no need to check for DEFAULT_ARG */
363 
364     Next = Op->Asl.Next;
365     LockRule = (UINT8) Next->Asl.Value.Integer;
366     Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
367 
368     /* UpdateRule -- not optional, so no need to check for DEFAULT_ARG */
369 
370     Next = Next->Asl.Next;
371     UpdateRule = (UINT8) Next->Asl.Value.Integer;
372 
373     /*
374      * Generate the flags byte. The various fields are already
375      * in the right bit position via translation from the
376      * keywords by the parser.
377      */
378     FieldFlags = (UINT8) (AccessType | LockRule | UpdateRule);
379 
380     /* Use the previous node to be the FieldFlags node */
381 
382     /* Set the node to RAW_DATA */
383 
384     Next->Asl.Value.Integer = FieldFlags;
385     Next->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
386     Next->Asl.AmlLength = 1;
387     Next->Asl.ParseOpcode = PARSEOP_RAW_DATA;
388 
389     /* Process the FieldUnitList */
390 
391     Next = Next->Asl.Next;
392     CurrentBitOffset = 0;
393 
394     while (Next)
395     {
396         /* Save the offset of this field unit */
397 
398         Next->Asl.ExtraValue = CurrentBitOffset;
399 
400         switch (Next->Asl.ParseOpcode)
401         {
402         case PARSEOP_ACCESSAS:
403 
404             PkgLengthNode = Next->Asl.Child;
405             AccessType = (UINT8) PkgLengthNode->Asl.Value.Integer;
406 
407             /* Nothing additional to do */
408             break;
409 
410         case PARSEOP_OFFSET:
411 
412             /* New offset into the field */
413 
414             PkgLengthNode = Next->Asl.Child;
415             NewBitOffset = ((UINT32) PkgLengthNode->Asl.Value.Integer) * 8;
416 
417             /*
418              * Examine the specified offset in relation to the
419              * current offset counter.
420              */
421             if (NewBitOffset < CurrentBitOffset)
422             {
423                 /*
424                  * Not allowed to specify a backwards offset!
425                  * Issue error and ignore this node.
426                  */
427                 AslError (ASL_ERROR, ASL_MSG_BACKWARDS_OFFSET, PkgLengthNode,
428                     NULL);
429                 Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
430                 PkgLengthNode->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
431             }
432             else if (NewBitOffset == CurrentBitOffset)
433             {
434                 /*
435                  * This Offset() operator is redundant and not needed,
436                  * because the offset value is the same as the current
437                  * offset.
438                  */
439                 AslError (ASL_REMARK, ASL_MSG_OFFSET, PkgLengthNode, NULL);
440 
441                 if (AslGbl_OptimizeTrivialParseNodes)
442                 {
443                     /*
444                      * Optimize this Offset() operator by removing/ignoring
445                      * it. Set the related nodes to default.
446                      */
447                     Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
448                     PkgLengthNode->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
449 
450                     AslError (ASL_OPTIMIZATION, ASL_MSG_OFFSET, PkgLengthNode,
451                         "Optimizer has removed statement");
452                 }
453                 else
454                 {
455                     /* Optimization is disabled, treat as a valid Offset */
456 
457                     PkgLengthNode->Asl.Value.Integer =
458                         NewBitOffset - CurrentBitOffset;
459                     CurrentBitOffset = NewBitOffset;
460                 }
461             }
462             else
463             {
464                 /*
465                  * Valid new offset - set the value to be inserted into the AML
466                  * and update the offset counter.
467                  */
468                 PkgLengthNode->Asl.Value.Integer =
469                     NewBitOffset - CurrentBitOffset;
470                 CurrentBitOffset = NewBitOffset;
471             }
472             break;
473 
474         case PARSEOP_NAMESEG:
475         case PARSEOP_RESERVED_BYTES:
476 
477             /* Named or reserved field entry */
478 
479             PkgLengthNode = Next->Asl.Child;
480             NewBitOffset = (UINT32) PkgLengthNode->Asl.Value.Integer;
481             CurrentBitOffset += NewBitOffset;
482 
483             if ((NewBitOffset == 0) &&
484                 (Next->Asl.ParseOpcode == PARSEOP_RESERVED_BYTES) &&
485                 AslGbl_OptimizeTrivialParseNodes)
486             {
487                 /*
488                  * Unnamed field with a bit length of zero. We can
489                  * safely just ignore this. However, we will not ignore
490                  * a named field of zero length, we don't want to just
491                  * toss out a name.
492                  */
493                 Next->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
494                 PkgLengthNode->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
495                 break;
496             }
497 
498             /* Save the current AccessAs value for error checking later */
499 
500             switch (AccessType)
501             {
502                 case AML_FIELD_ACCESS_ANY:
503                 case AML_FIELD_ACCESS_BYTE:
504                 case AML_FIELD_ACCESS_BUFFER:
505                 default:
506 
507                     MinimumLength = 8;
508                     break;
509 
510                 case AML_FIELD_ACCESS_WORD:
511                     MinimumLength = 16;
512                     break;
513 
514                 case AML_FIELD_ACCESS_DWORD:
515                     MinimumLength = 32;
516                     break;
517 
518                 case AML_FIELD_ACCESS_QWORD:
519                     MinimumLength = 64;
520                     break;
521             }
522 
523             PkgLengthNode->Asl.ExtraValue = MinimumLength;
524             break;
525 
526         default:
527 
528             /* All supported field opcodes must appear above */
529 
530             break;
531         }
532 
533         /* Move on to next entry in the field list */
534 
535         Next = Next->Asl.Next;
536     }
537 }
538 
539 
540 /*******************************************************************************
541  *
542  * FUNCTION:    OpnDoField
543  *
544  * PARAMETERS:  Op        - The parent parse node
545  *
546  * RETURN:      None
547  *
548  * DESCRIPTION: Construct the AML operands for the FIELD ASL keyword
549  *
550  ******************************************************************************/
551 
552 static void
553 OpnDoField (
554     ACPI_PARSE_OBJECT       *Op)
555 {
556     ACPI_PARSE_OBJECT       *Next;
557 
558 
559     /* Opcode is parent node */
560     /* First child is field name */
561 
562     Next = Op->Asl.Child;
563 
564     /* Second child is the AccessType */
565 
566     OpnDoFieldCommon (Op, Next->Asl.Next);
567 }
568 
569 
570 /*******************************************************************************
571  *
572  * FUNCTION:    OpnDoIndexField
573  *
574  * PARAMETERS:  Op        - The parent parse node
575  *
576  * RETURN:      None
577  *
578  * DESCRIPTION: Construct the AML operands for the INDEXFIELD ASL keyword
579  *
580  ******************************************************************************/
581 
582 static void
583 OpnDoIndexField (
584     ACPI_PARSE_OBJECT       *Op)
585 {
586     ACPI_PARSE_OBJECT       *Next;
587 
588 
589     /* Opcode is parent node */
590     /* First child is the index name */
591 
592     Next = Op->Asl.Child;
593 
594     /* Second child is the data name */
595 
596     Next = Next->Asl.Next;
597 
598     /* Third child is the AccessType */
599 
600     OpnDoFieldCommon (Op, Next->Asl.Next);
601 }
602 
603 
604 /*******************************************************************************
605  *
606  * FUNCTION:    OpnDoBankField
607  *
608  * PARAMETERS:  Op        - The parent parse node
609  *
610  * RETURN:      None
611  *
612  * DESCRIPTION: Construct the AML operands for the BANKFIELD ASL keyword
613  *
614  ******************************************************************************/
615 
616 static void
617 OpnDoBankField (
618     ACPI_PARSE_OBJECT       *Op)
619 {
620     ACPI_PARSE_OBJECT       *Next;
621 
622 
623     /* Opcode is parent node */
624     /* First child is the region name */
625 
626     Next = Op->Asl.Child;
627 
628     /* Second child is the bank name */
629 
630     Next = Next->Asl.Next;
631 
632     /* Third child is the bank value */
633 
634     Next = Next->Asl.Next;
635 
636     /* Fourth child is the AccessType */
637 
638     OpnDoFieldCommon (Op, Next->Asl.Next);
639 }
640 
641 
642 /*******************************************************************************
643  *
644  * FUNCTION:    OpnDoRegion
645  *
646  * PARAMETERS:  Op        - The parent parse node
647  *
648  * RETURN:      None
649  *
650  * DESCRIPTION: Tries to get the length of the region. Can only do this at
651  *              compile time if the length is a constant.
652  *
653  ******************************************************************************/
654 
655 static void
656 OpnDoRegion (
657     ACPI_PARSE_OBJECT       *Op)
658 {
659     ACPI_PARSE_OBJECT       *Next;
660     ACPI_ADR_SPACE_TYPE     SpaceId;
661 
662 
663     /* Opcode is parent node */
664     /* First child is the region name */
665 
666     Next = Op->Asl.Child;
667 
668     /* Second child is the space ID */
669 
670     Next = Next->Asl.Next;
671     SpaceId = (ACPI_ADR_SPACE_TYPE) Next->Common.Value.Integer;
672 
673     /* Third child is the region offset */
674 
675     Next = Next->Asl.Next;
676 
677     /* Fourth child is the region length */
678 
679     Next = Next->Asl.Next;
680     if (Next->Asl.ParseOpcode == PARSEOP_INTEGER)
681     {
682         /* Check for zero length */
683 
684         Op->Asl.Value.Integer = Next->Asl.Value.Integer;
685         if (!Op->Asl.Value.Integer && (SpaceId < ACPI_NUM_PREDEFINED_REGIONS))
686         {
687             AslError (ASL_ERROR, ASL_MSG_REGION_LENGTH, Op, NULL);
688         }
689     }
690     else
691     {
692         Op->Asl.Value.Integer = ACPI_UINT64_MAX;
693     }
694 }
695 
696 
697 /*******************************************************************************
698  *
699  * FUNCTION:    OpnDoBuffer
700  *
701  * PARAMETERS:  Op        - The parent parse node
702  *
703  * RETURN:      None
704  *
705  * DESCRIPTION: Construct the AML operands for the BUFFER ASL keyword. We
706  *              build a single raw byte buffer from the initialization nodes,
707  *              each parse node contains a buffer byte.
708  *
709  ******************************************************************************/
710 
711 static void
712 OpnDoBuffer (
713     ACPI_PARSE_OBJECT       *Op)
714 {
715     ACPI_PARSE_OBJECT       *InitializerOp;
716     ACPI_PARSE_OBJECT       *BufferLengthOp;
717 
718     /* Optional arguments for this opcode with defaults */
719 
720     UINT32                  BufferLength = 0;
721 
722 
723     /* Opcode and package length first */
724     /* Buffer Length is next, followed by the initializer list */
725 
726     BufferLengthOp = Op->Asl.Child;
727     InitializerOp = BufferLengthOp->Asl.Next;
728 
729     /*
730      * If the BufferLength is not an INTEGER or was not specified in the ASL
731      * (DEFAULT_ARG), it is a TermArg that is
732      * evaluated at run-time, and we are therefore finished.
733      */
734     if ((BufferLengthOp->Asl.ParseOpcode != PARSEOP_INTEGER) &&
735         (BufferLengthOp->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG))
736     {
737         return;
738     }
739 
740     /*
741      * We want to count the number of items in the initializer list, because if
742      * it is larger than the buffer length, we will define the buffer size
743      * to be the size of the initializer list (as per the ACPI Specification)
744      */
745     switch (InitializerOp->Asl.ParseOpcode)
746     {
747     case PARSEOP_INTEGER:
748     case PARSEOP_BYTECONST:
749     case PARSEOP_WORDCONST:
750     case PARSEOP_DWORDCONST:
751 
752         /* The peer list contains the byte list (if any...) */
753 
754         while (InitializerOp)
755         {
756             /* For buffers, this is a list of raw bytes */
757 
758             InitializerOp->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
759             InitializerOp->Asl.AmlLength = 1;
760             InitializerOp->Asl.ParseOpcode = PARSEOP_RAW_DATA;
761 
762             BufferLength++;
763             InitializerOp = ASL_GET_PEER_NODE (InitializerOp);
764         }
765         break;
766 
767     case PARSEOP_STRING_LITERAL:
768 
769         /*
770          * Only one initializer, the string. Buffer must be big enough to hold
771          * the string plus the null termination byte
772          */
773         BufferLength = strlen (InitializerOp->Asl.Value.String) + 1;
774 
775         InitializerOp->Asl.AmlOpcode = AML_RAW_DATA_BUFFER;
776         InitializerOp->Asl.AmlLength = BufferLength;
777         InitializerOp->Asl.ParseOpcode = PARSEOP_RAW_DATA;
778         break;
779 
780     case PARSEOP_RAW_DATA:
781 
782         /* Buffer nodes are already initialized (e.g. Unicode operator) */
783         return;
784 
785     case PARSEOP_DEFAULT_ARG:
786         break;
787 
788     default:
789 
790         AslError (ASL_ERROR, ASL_MSG_INVALID_OPERAND, InitializerOp,
791             "Unknown buffer initializer opcode");
792         printf ("Unknown buffer initializer opcode [%s]\n",
793             UtGetOpName (InitializerOp->Asl.ParseOpcode));
794         return;
795     }
796 
797     /* Check if initializer list is longer than the buffer length */
798 
799     if (BufferLengthOp->Asl.Value.Integer > BufferLength)
800     {
801         BufferLength = (UINT32) BufferLengthOp->Asl.Value.Integer;
802     }
803 
804     if (!BufferLength)
805     {
806         /* No length AND no items -- issue notice */
807 
808         AslError (ASL_REMARK, ASL_MSG_BUFFER_LENGTH, BufferLengthOp, NULL);
809 
810         /* But go ahead and put the buffer length of zero into the AML */
811     }
812 
813     /*
814      * Just set the buffer size node to be the buffer length, regardless
815      * of whether it was previously an integer or a default_arg placeholder
816      */
817     BufferLengthOp->Asl.ParseOpcode = PARSEOP_INTEGER;
818     BufferLengthOp->Asl.AmlOpcode = AML_DWORD_OP;
819     BufferLengthOp->Asl.Value.Integer = BufferLength;
820 
821     (void) OpcSetOptimalIntegerSize (BufferLengthOp);
822 
823     /* Remaining nodes are handled via the tree walk */
824 }
825 
826 
827 /*******************************************************************************
828  *
829  * FUNCTION:    OpnDoPackage
830  *
831  * PARAMETERS:  Op        - The parent parse node
832  *
833  * RETURN:      None
834  *
835  * DESCRIPTION: Construct the AML operands for the PACKAGE ASL keyword. NOTE:
836  *              can only be called after constants have been folded, to ensure
837  *              that the PackageLength operand has been fully reduced.
838  *
839  ******************************************************************************/
840 
841 void
842 OpnDoPackage (
843     ACPI_PARSE_OBJECT       *Op)
844 {
845     ACPI_PARSE_OBJECT       *InitializerOp;
846     ACPI_PARSE_OBJECT       *PackageLengthOp;
847     UINT32                  PackageLength = 0;
848 
849 
850     /* Opcode and package length first, followed by the initializer list */
851 
852     PackageLengthOp = Op->Asl.Child;
853     InitializerOp = PackageLengthOp->Asl.Next;
854 
855     /* Count the number of items in the initializer list */
856 
857     if (InitializerOp->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
858     {
859         /* The peer list contains the byte list (if any...) */
860 
861         while (InitializerOp)
862         {
863             PackageLength++;
864             InitializerOp = InitializerOp->Asl.Next;
865         }
866     }
867 
868     /* If package length is a constant, compare to the initializer list */
869 
870     if ((PackageLengthOp->Asl.ParseOpcode == PARSEOP_INTEGER)      ||
871         (PackageLengthOp->Asl.ParseOpcode == PARSEOP_QWORDCONST))
872     {
873         if (PackageLengthOp->Asl.Value.Integer > PackageLength)
874         {
875             /*
876              * Allow package length to be longer than the initializer
877              * list -- but if the length of initializer list is nonzero,
878              * issue a message since this is probably a coding error,
879              * even though technically legal.
880              */
881             if (PackageLength > 0)
882             {
883                 AslError (ASL_REMARK, ASL_MSG_LIST_LENGTH_SHORT,
884                     PackageLengthOp, NULL);
885             }
886 
887             PackageLength = (UINT32) PackageLengthOp->Asl.Value.Integer;
888         }
889         else if (PackageLengthOp->Asl.Value.Integer < PackageLength)
890         {
891             /*
892              * The package length is smaller than the length of the
893              * initializer list. This is an error as per the ACPI spec.
894              */
895             AslError (ASL_ERROR, ASL_MSG_LIST_LENGTH_LONG,
896                 PackageLengthOp, NULL);
897         }
898     }
899 
900     if (PackageLengthOp->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
901     {
902         /*
903          * This is the case if the PackageLength was left empty - Package()
904          * The package length becomes the length of the initializer list
905          */
906         Op->Asl.Child->Asl.ParseOpcode = PARSEOP_INTEGER;
907         Op->Asl.Child->Asl.Value.Integer = PackageLength;
908 
909         /* Set the AML opcode */
910 
911         (void) OpcSetOptimalIntegerSize (Op->Asl.Child);
912     }
913 
914     /* If not a variable-length package, check for a zero package length */
915 
916     if ((PackageLengthOp->Asl.ParseOpcode == PARSEOP_INTEGER)      ||
917         (PackageLengthOp->Asl.ParseOpcode == PARSEOP_QWORDCONST)   ||
918         (PackageLengthOp->Asl.ParseOpcode == PARSEOP_ZERO)         ||
919         (PackageLengthOp->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG))
920     {
921         if (!PackageLength)
922         {
923             /* No length AND no initializer list -- issue a remark */
924 
925             AslError (ASL_REMARK, ASL_MSG_PACKAGE_LENGTH,
926                 PackageLengthOp, NULL);
927 
928             /* But go ahead and put the buffer length of zero into the AML */
929         }
930     }
931 
932     /*
933      * If the PackageLength is a constant <= 255, we can change the
934      * AML opcode from VarPackage to a simple (ACPI 1.0) Package opcode.
935      */
936     if (((Op->Asl.Child->Asl.ParseOpcode == PARSEOP_INTEGER) &&
937             (Op->Asl.Child->Asl.Value.Integer <= 255))  ||
938         (Op->Asl.Child->Asl.ParseOpcode == PARSEOP_ONE) ||
939         (Op->Asl.Child->Asl.ParseOpcode == PARSEOP_ONES)||
940         (Op->Asl.Child->Asl.ParseOpcode == PARSEOP_ZERO))
941     {
942         Op->Asl.AmlOpcode = AML_PACKAGE_OP;
943         Op->Asl.ParseOpcode = PARSEOP_PACKAGE;
944 
945         /*
946          * Just set the package size node to be the package length, regardless
947          * of whether it was previously an integer or a default_arg placeholder
948          */
949         PackageLengthOp->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
950         PackageLengthOp->Asl.AmlLength = 1;
951         PackageLengthOp->Asl.ParseOpcode = PARSEOP_RAW_DATA;
952         PackageLengthOp->Asl.Value.Integer = PackageLength;
953     }
954 
955     /* Remaining nodes are handled via the tree walk */
956 }
957 
958 
959 /*******************************************************************************
960  *
961  * FUNCTION:    OpnDoLoadTable
962  *
963  * PARAMETERS:  Op        - The parent parse node
964  *
965  * RETURN:      None
966  *
967  * DESCRIPTION: Construct the AML operands for the LOADTABLE ASL keyword.
968  *
969  ******************************************************************************/
970 
971 static void
972 OpnDoLoadTable (
973     ACPI_PARSE_OBJECT       *Op)
974 {
975     ACPI_PARSE_OBJECT       *Next;
976 
977 
978     /* Opcode is parent node */
979     /* First child is the table signature */
980 
981     Next = Op->Asl.Child;
982 
983     /* Second child is the OEM ID*/
984 
985     Next = Next->Asl.Next;
986 
987     /* Third child is the OEM table ID */
988 
989     Next = Next->Asl.Next;
990 
991     /* Fourth child is the RootPath string */
992 
993     Next = Next->Asl.Next;
994     if (Next->Asl.ParseOpcode == PARSEOP_ZERO)
995     {
996         Next->Asl.ParseOpcode = PARSEOP_STRING_LITERAL;
997         Next->Asl.Value.String = "\\";
998         Next->Asl.AmlLength = 2;
999         OpcGenerateAmlOpcode (Next);
1000     }
1001 
1002 #ifdef ASL_FUTURE_IMPLEMENTATION
1003 
1004     /* TBD: NOT IMPLEMENTED */
1005     /* Fifth child is the [optional] ParameterPathString */
1006     /* Sixth child is the [optional] ParameterData */
1007 
1008     Next = Next->Asl.Next;
1009     if (Next->Asl.ParseOpcode == DEFAULT_ARG)
1010     {
1011         Next->Asl.AmlLength = 1;
1012         Next->Asl.ParseOpcode = ZERO;
1013         OpcGenerateAmlOpcode (Next);
1014     }
1015 
1016 
1017     Next = Next->Asl.Next;
1018     if (Next->Asl.ParseOpcode == DEFAULT_ARG)
1019     {
1020         Next->Asl.AmlLength = 1;
1021         Next->Asl.ParseOpcode = ZERO;
1022         OpcGenerateAmlOpcode (Next);
1023     }
1024 #endif
1025 }
1026 
1027 
1028 /*******************************************************************************
1029  *
1030  * FUNCTION:    OpnDoDefinitionBlock
1031  *
1032  * PARAMETERS:  Op        - The parent parse node
1033  *
1034  * RETURN:      None
1035  *
1036  * DESCRIPTION: Construct the AML operands for the DEFINITIONBLOCK ASL keyword
1037  *
1038  ******************************************************************************/
1039 
1040 static void
1041 OpnDoDefinitionBlock (
1042     ACPI_PARSE_OBJECT       *Op)
1043 {
1044     ACPI_PARSE_OBJECT       *Child;
1045     ACPI_SIZE               Length;
1046     UINT32                  i;
1047     char                    *Filename;
1048     ACPI_STATUS             Status;
1049 
1050 
1051     /*
1052      * These nodes get stuffed into the table header. They are special
1053      * cased when the table is written to the output file.
1054      *
1055      * Mark all of these nodes as non-usable so they won't get output
1056      * as AML opcodes!
1057      */
1058 
1059     /* Get AML filename. Use it if non-null */
1060 
1061     Child = Op->Asl.Child;
1062     if (Child->Asl.Value.Buffer  &&
1063         *Child->Asl.Value.Buffer &&
1064         (AslGbl_UseDefaultAmlFilename))
1065     {
1066         /*
1067          * The walk may traverse multiple definition blocks. Switch files
1068          * to ensure that the correct files are manipulated.
1069          */
1070         FlSwitchFileSet (Op->Asl.Filename);
1071 
1072         /*
1073          * We will use the AML filename that is embedded in the source file
1074          * for the output filename.
1075          */
1076         Filename = UtLocalCacheCalloc (strlen (AslGbl_DirectoryPath) +
1077             strlen ((char *) Child->Asl.Value.Buffer) + 1);
1078 
1079         /* Prepend the current directory path */
1080 
1081         strcpy (Filename, AslGbl_DirectoryPath);
1082         strcat (Filename, (char *) Child->Asl.Value.Buffer);
1083 
1084         AslGbl_OutputFilenamePrefix = Filename;
1085         UtConvertBackslashes (AslGbl_OutputFilenamePrefix);
1086 
1087         /*
1088          * Use the definition block file parameter instead of the input
1089          * filename. Since all files were opened previously, remove the
1090          * existing file and open a new file with the name of this
1091          * definiton block parameter. Since AML code generation has yet
1092          * to happen, the previous file can be removed without any impacts.
1093          */
1094         FlCloseFile (ASL_FILE_AML_OUTPUT);
1095         FlDeleteFile (ASL_FILE_AML_OUTPUT);
1096         Status = FlOpenAmlOutputFile (AslGbl_OutputFilenamePrefix);
1097         if (ACPI_FAILURE (Status))
1098         {
1099             AslError (ASL_ERROR, ASL_MSG_OUTPUT_FILE_OPEN, NULL, NULL);
1100             return;
1101         }
1102     }
1103 
1104     Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
1105 
1106     /* Signature */
1107 
1108     Child = Child->Asl.Next;
1109     Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
1110     if (Child->Asl.Value.String)
1111     {
1112         AslGbl_FilesList->TableSignature = Child->Asl.Value.String;
1113         AslGbl_TableSignature = Child->Asl.Value.String;
1114         if (strlen (AslGbl_TableSignature) != ACPI_NAMESEG_SIZE)
1115         {
1116             AslError (ASL_ERROR, ASL_MSG_TABLE_SIGNATURE, Child,
1117                 "Length must be exactly 4 characters");
1118         }
1119 
1120         for (i = 0; i < ACPI_NAMESEG_SIZE; i++)
1121         {
1122             if (!isalnum ((int) AslGbl_TableSignature[i]))
1123             {
1124                 AslError (ASL_ERROR, ASL_MSG_TABLE_SIGNATURE, Child,
1125                     "Contains non-alphanumeric characters");
1126             }
1127         }
1128     }
1129 
1130     /* Revision */
1131 
1132     Child = Child->Asl.Next;
1133     Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
1134 
1135     /*
1136      * We used the revision to set the integer width earlier
1137      */
1138 
1139     /* OEMID */
1140 
1141     Child = Child->Asl.Next;
1142     Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
1143     if (Child->Asl.Value.String &&
1144         strlen (Child->Asl.Value.String) > ACPI_OEM_ID_SIZE)
1145     {
1146         AslError (ASL_ERROR, ASL_MSG_OEM_ID, Child,
1147             "Length cannot exceed 6 characters");
1148     }
1149 
1150     /* OEM TableID */
1151 
1152     Child = Child->Asl.Next;
1153     Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
1154     if (Child->Asl.Value.String)
1155     {
1156         Length = strlen (Child->Asl.Value.String);
1157         if (Length > ACPI_OEM_TABLE_ID_SIZE)
1158         {
1159             AslError (ASL_ERROR, ASL_MSG_OEM_TABLE_ID, Child,
1160                 "Length cannot exceed 8 characters");
1161         }
1162 
1163         AslGbl_TableId = UtLocalCacheCalloc (Length + 1);
1164         strcpy (AslGbl_TableId, Child->Asl.Value.String);
1165         AslGbl_FilesList->TableId = AslGbl_TableId;
1166 
1167         /*
1168          * Convert anything non-alphanumeric to an underscore. This
1169          * allows us to use the TableID to generate unique C symbols.
1170          */
1171         for (i = 0; i < Length; i++)
1172         {
1173             if (!isalnum ((int) AslGbl_TableId[i]))
1174             {
1175                 AslGbl_TableId[i] = '_';
1176             }
1177         }
1178     }
1179 
1180     /* OEM Revision */
1181 
1182     Child = Child->Asl.Next;
1183     Child->Asl.ParseOpcode = PARSEOP_DEFAULT_ARG;
1184 }
1185 
1186 
1187 /*******************************************************************************
1188  *
1189  * FUNCTION:    UtGetArg
1190  *
1191  * PARAMETERS:  Op              - Get an argument for this op
1192  *              Argn            - Nth argument to get
1193  *
1194  * RETURN:      The argument (as an Op object). NULL if argument does not exist
1195  *
1196  * DESCRIPTION: Get the specified op's argument (peer)
1197  *
1198  ******************************************************************************/
1199 
1200 ACPI_PARSE_OBJECT *
1201 UtGetArg (
1202     ACPI_PARSE_OBJECT       *Op,
1203     UINT32                  Argn)
1204 {
1205     ACPI_PARSE_OBJECT       *Arg = NULL;
1206 
1207 
1208     /* Get the requested argument object */
1209 
1210     Arg = Op->Asl.Child;
1211     while (Arg && Argn)
1212     {
1213         Argn--;
1214         Arg = Arg->Asl.Next;
1215     }
1216 
1217     return (Arg);
1218 }
1219 
1220 
1221 /*******************************************************************************
1222  *
1223  * FUNCTION:    OpnAttachNameToNode
1224  *
1225  * PARAMETERS:  Op        - The parent parse node
1226  *
1227  * RETURN:      None
1228  *
1229  * DESCRIPTION: For the named ASL/AML operators, get the actual name from the
1230  *              argument list and attach it to the parent node so that we
1231  *              can get to it quickly later.
1232  *
1233  ******************************************************************************/
1234 
1235 static void
1236 OpnAttachNameToNode (
1237     ACPI_PARSE_OBJECT       *Op)
1238 {
1239     ACPI_PARSE_OBJECT       *Child = NULL;
1240 
1241 
1242     switch (Op->Asl.AmlOpcode)
1243     {
1244     case AML_DATA_REGION_OP:
1245     case AML_DEVICE_OP:
1246     case AML_EVENT_OP:
1247     case AML_EXTERNAL_OP:
1248     case AML_METHOD_OP:
1249     case AML_MUTEX_OP:
1250     case AML_REGION_OP:
1251     case AML_POWER_RESOURCE_OP:
1252     case AML_PROCESSOR_OP:
1253     case AML_THERMAL_ZONE_OP:
1254     case AML_NAME_OP:
1255     case AML_SCOPE_OP:
1256 
1257         Child = UtGetArg (Op, 0);
1258         break;
1259 
1260     case AML_ALIAS_OP:
1261 
1262         Child = UtGetArg (Op, 1);
1263         break;
1264 
1265     case AML_CREATE_BIT_FIELD_OP:
1266     case AML_CREATE_BYTE_FIELD_OP:
1267     case AML_CREATE_WORD_FIELD_OP:
1268     case AML_CREATE_DWORD_FIELD_OP:
1269     case AML_CREATE_QWORD_FIELD_OP:
1270 
1271         Child = UtGetArg (Op, 2);
1272         break;
1273 
1274     case AML_CREATE_FIELD_OP:
1275 
1276         Child = UtGetArg (Op, 3);
1277         break;
1278 
1279     case AML_BANK_FIELD_OP:
1280     case AML_INDEX_FIELD_OP:
1281     case AML_FIELD_OP:
1282 
1283         return;
1284 
1285     default:
1286 
1287         return;
1288     }
1289 
1290     if (Child)
1291     {
1292         UtAttachNamepathToOwner (Op, Child);
1293     }
1294 }
1295 
1296 
1297 /*******************************************************************************
1298  *
1299  * FUNCTION:    OpnGenerateAmlOperands
1300  *
1301  * PARAMETERS:  Op        - The parent parse node
1302  *
1303  * RETURN:      None
1304  *
1305  * DESCRIPTION: Prepare nodes to be output as AML data and operands. The more
1306  *              complex AML opcodes require processing of the child nodes
1307  *              (arguments/operands).
1308  *
1309  ******************************************************************************/
1310 
1311 void
1312 OpnGenerateAmlOperands (
1313     ACPI_PARSE_OBJECT       *Op)
1314 {
1315 
1316 
1317     if (Op->Asl.AmlOpcode == AML_RAW_DATA_BYTE)
1318     {
1319         return;
1320     }
1321 
1322     switch (Op->Asl.ParseOpcode)
1323     {
1324     case PARSEOP_DEFINITION_BLOCK:
1325 
1326         OpnDoDefinitionBlock (Op);
1327         break;
1328 
1329     case PARSEOP_METHOD:
1330 
1331         OpnDoMethod (Op);
1332         break;
1333 
1334     case PARSEOP_MUTEX:
1335 
1336         OpnDoMutex (Op);
1337         break;
1338 
1339     case PARSEOP_FIELD:
1340 
1341         OpnDoField (Op);
1342         break;
1343 
1344     case PARSEOP_INDEXFIELD:
1345 
1346         OpnDoIndexField (Op);
1347         break;
1348 
1349     case PARSEOP_BANKFIELD:
1350 
1351         OpnDoBankField (Op);
1352         break;
1353 
1354     case PARSEOP_BUFFER:
1355 
1356         OpnDoBuffer (Op);
1357         break;
1358 
1359     case PARSEOP_LOADTABLE:
1360 
1361         OpnDoLoadTable (Op);
1362         break;
1363 
1364     case PARSEOP_OPERATIONREGION:
1365 
1366         OpnDoRegion (Op);
1367         break;
1368 
1369     case PARSEOP_RESOURCETEMPLATE:
1370 
1371         RsDoResourceTemplate (Op);
1372         break;
1373 
1374     case PARSEOP_NAMESEG:
1375     case PARSEOP_NAMESTRING:
1376     case PARSEOP_METHODCALL:
1377     case PARSEOP_STRING_LITERAL:
1378     default:
1379 
1380         break;
1381     }
1382 
1383     /* TBD: move */
1384 
1385     OpnAttachNameToNode (Op);
1386 }
1387