2 /******************************************************************************
4 * Module Name: aslcodegen - AML code generation
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
13 * All rights reserved.
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
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
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;
38 * The above copyright and patent license is granted only if the following
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.
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
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
73 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
81 * 4. Disclaimer and Export Compliance
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
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
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.
115 *****************************************************************************/
118 #include "aslcompiler.h"
119 #include "aslcompiler.y.h"
122 #define _COMPONENT ACPI_COMPILER
123 ACPI_MODULE_NAME ("aslcodegen")
125 /* Local prototypes */
129 ACPI_PARSE_OBJECT *Op,
134 CgLocalWriteAmlData (
135 ACPI_PARSE_OBJECT *Op,
141 ACPI_PARSE_OBJECT *Op);
145 ACPI_PARSE_OBJECT *Op);
153 ACPI_PARSE_OBJECT *Op);
156 /*******************************************************************************
158 * FUNCTION: CgGenerateAmlOutput
164 * DESCRIPTION: Generate AML code. Currently generates the listing file
167 ******************************************************************************/
170 CgGenerateAmlOutput (
174 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
176 /* Generate the AML output file */
178 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
180 Gbl_NextError = Gbl_ErrorLog;
182 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
183 CgAmlWriteWalk, NULL, NULL);
188 /*******************************************************************************
190 * FUNCTION: CgAmlWriteWalk
192 * PARAMETERS: ASL_WALK_CALLBACK
196 * DESCRIPTION: Parse tree walk to generate the AML code.
198 ******************************************************************************/
202 ACPI_PARSE_OBJECT *Op,
208 * Print header at level 0. Alignment assumes 32-bit pointers
212 DbgPrint (ASL_TREE_OUTPUT,
213 "Final parse tree used for AML output:\n");
214 DbgPrint (ASL_TREE_OUTPUT,
215 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr Child Parent Flags AcTyp Final Col L\n",
221 DbgPrint (ASL_TREE_OUTPUT,
222 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
223 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
225 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG ||
226 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
227 Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
229 DbgPrint (ASL_TREE_OUTPUT,
230 "%10.32s ", Op->Asl.ExternalName);
234 DbgPrint (ASL_TREE_OUTPUT, " ");
237 DbgPrint (ASL_TREE_OUTPUT,
238 "%08X %04X %04X %01X %04X %04X %04X %04X %08X %08X %08X %08X %08X %04X %02d %02d\n",
239 /* 1 */ (UINT32) Op->Asl.Value.Integer,
240 /* 2 */ Op->Asl.ParseOpcode,
241 /* 3 */ Op->Asl.AmlOpcode,
242 /* 4 */ Op->Asl.AmlOpcodeLength,
243 /* 5 */ Op->Asl.AmlPkgLenBytes,
244 /* 6 */ Op->Asl.AmlLength,
245 /* 7 */ Op->Asl.AmlSubtreeLength,
246 /* 8 */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
248 /* 10 */ Op->Asl.Child,
249 /* 11 */ Op->Asl.Parent,
250 /* 12 */ Op->Asl.CompileFlags,
251 /* 13 */ Op->Asl.AcpiBtype,
252 /* 14 */ Op->Asl.FinalAmlLength,
253 /* 15 */ Op->Asl.Column,
254 /* 16 */ Op->Asl.LineNumber);
256 /* Generate the AML for this node */
263 /*******************************************************************************
265 * FUNCTION: CgLocalWriteAmlData
267 * PARAMETERS: Op - Current parse op
268 * Buffer - Buffer to write
269 * Length - Size of data in buffer
273 * DESCRIPTION: Write a buffer of AML data to the AML output file.
275 ******************************************************************************/
278 CgLocalWriteAmlData (
279 ACPI_PARSE_OBJECT *Op,
284 /* Write the raw data to the AML file */
286 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
288 /* Update the final AML length for this node (used for listings) */
292 Op->Asl.FinalAmlLength += Length;
297 /*******************************************************************************
299 * FUNCTION: CgWriteAmlOpcode
301 * PARAMETERS: Op - Parse node with an AML opcode
305 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
307 ******************************************************************************/
311 ACPI_PARSE_OBJECT *Op)
313 UINT8 PkgLenFirstByte;
317 UINT8 OpcodeBytes[2];
325 /* We expect some DEFAULT_ARGs, just ignore them */
327 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
332 switch (Op->Asl.AmlOpcode)
334 case AML_UNASSIGNED_OPCODE:
336 /* These opcodes should not get here */
338 printf ("Found a node with an unassigned AML opcode\n");
339 fprintf (stderr, "Found a node with an unassigned AML opcode\n");
342 case AML_INT_RESERVEDFIELD_OP:
344 /* Special opcodes for within a field definition */
349 case AML_INT_ACCESSFIELD_OP:
355 Aml.Opcode = Op->Asl.AmlOpcode;
362 case AML_PACKAGE_LENGTH:
364 /* Value is the length to be encoded (Used in field definitions) */
366 PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
371 /* Check for two-byte opcode */
373 if (Aml.Opcode > 0x00FF)
375 /* Write the high byte first */
377 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
380 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
382 /* Subtreelength doesn't include length of package length bytes */
384 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
388 /* Does this opcode have an associated "PackageLength" field? */
390 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
392 if (Op->Asl.AmlPkgLenBytes == 1)
394 /* Simplest case -- no bytes to follow, just write the count */
396 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
398 else if (Op->Asl.AmlPkgLenBytes != 0)
401 * Encode the "bytes to follow" in the first byte, top two bits.
402 * The low-order nybble of the length is in the bottom 4 bits
404 PkgLenFirstByte = (UINT8)
405 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
406 (PkgLen.LenBytes[0] & 0x0F));
408 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
411 * Shift the length over by the 4 bits we just stuffed
416 /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
418 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
420 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
429 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
434 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
439 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
444 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
449 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
453 /* All data opcodes must appear above */
459 /*******************************************************************************
461 * FUNCTION: CgWriteTableHeader
463 * PARAMETERS: Op - The DEFINITIONBLOCK node
467 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
469 ******************************************************************************/
473 ACPI_PARSE_OBJECT *Op)
475 ACPI_PARSE_OBJECT *Child;
480 Child = Op->Asl.Child;
484 Child = Child->Asl.Next;
485 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
489 Child = Child->Asl.Next;
490 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
492 /* Command-line Revision override */
494 if (Gbl_RevisionOverride)
496 TableHeader.Revision = Gbl_RevisionOverride;
501 Child = Child->Asl.Next;
502 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
506 Child = Child->Asl.Next;
507 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
511 Child = Child->Asl.Next;
512 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
516 strncpy (TableHeader.AslCompilerId, ASL_CREATOR_ID, 4);
518 /* Compiler version */
520 TableHeader.AslCompilerRevision = ASL_REVISION;
522 /* Table length. Checksum zero for now, will rewrite later */
524 TableHeader.Length = Gbl_TableLength;
525 TableHeader.Checksum = 0;
527 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
531 /*******************************************************************************
533 * FUNCTION: CgCloseTable
539 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
540 * re-writing the header.
542 ******************************************************************************/
552 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
555 /* Calculate the checksum over the entire file */
557 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
559 Sum = (signed char) (Sum + FileByte);
562 /* Re-write the table header with the checksum */
564 TableHeader.Checksum = (UINT8) (0 - Sum);
566 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
567 CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
571 /*******************************************************************************
573 * FUNCTION: CgWriteNode
575 * PARAMETERS: Op - Parse node to write.
579 * DESCRIPTION: Write the AML that corresponds to a parse node.
581 ******************************************************************************/
585 ACPI_PARSE_OBJECT *Op)
587 ASL_RESOURCE_NODE *Rnode;
590 /* Always check for DEFAULT_ARG and other "Noop" nodes */
591 /* TBD: this may not be the best place for this check */
593 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) ||
594 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) ||
595 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) ||
596 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
601 Op->Asl.FinalAmlLength = 0;
603 switch (Op->Asl.AmlOpcode)
605 case AML_RAW_DATA_BYTE:
606 case AML_RAW_DATA_WORD:
607 case AML_RAW_DATA_DWORD:
608 case AML_RAW_DATA_QWORD:
610 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
614 case AML_RAW_DATA_BUFFER:
616 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
620 case AML_RAW_DATA_CHAIN:
622 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
625 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
631 /* Internal data opcodes must all appear above */
635 switch (Op->Asl.ParseOpcode)
637 case PARSEOP_DEFAULT_ARG:
641 case PARSEOP_DEFINITIONBLOCK:
643 CgWriteTableHeader (Op);
646 case PARSEOP_NAMESEG:
647 case PARSEOP_NAMESTRING:
648 case PARSEOP_METHODCALL:
650 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
655 CgWriteAmlOpcode (Op);