2 /******************************************************************************
4 * Module Name: aslcodegen - AML code generation
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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.
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include "aslcompiler.y.h"
48 #include <contrib/dev/acpica/include/amlcode.h>
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("aslcodegen")
53 /* Local prototypes */
57 ACPI_PARSE_OBJECT *Op,
63 ACPI_PARSE_OBJECT *Op,
69 ACPI_PARSE_OBJECT *Op);
73 ACPI_PARSE_OBJECT *Op);
81 ACPI_PARSE_OBJECT *Op);
84 /*******************************************************************************
86 * FUNCTION: CgGenerateAmlOutput
92 * DESCRIPTION: Generate AML code. Currently generates the listing file
95 ******************************************************************************/
102 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
104 /* Generate the AML output file */
106 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
108 Gbl_NextError = Gbl_ErrorLog;
110 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
111 CgAmlWriteWalk, NULL, NULL);
116 /*******************************************************************************
118 * FUNCTION: CgAmlWriteWalk
120 * PARAMETERS: ASL_WALK_CALLBACK
124 * DESCRIPTION: Parse tree walk to generate the AML code.
126 ******************************************************************************/
130 ACPI_PARSE_OBJECT *Op,
136 * Print header at level 0. Alignment assumes 32-bit pointers
140 DbgPrint (ASL_TREE_OUTPUT,
141 "Final parse tree used for AML output:\n");
142 DbgPrint (ASL_TREE_OUTPUT,
143 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr Child Parent Flags AcTyp Final Col L\n",
149 DbgPrint (ASL_TREE_OUTPUT,
150 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
151 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
153 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG ||
154 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
155 Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
157 DbgPrint (ASL_TREE_OUTPUT,
158 "%10.32s ", Op->Asl.ExternalName);
162 DbgPrint (ASL_TREE_OUTPUT, " ");
165 DbgPrint (ASL_TREE_OUTPUT,
166 "%08X %04X %04X %01X %04X %04X %04X %04X %08X %08X %08X %08X %08X %04X %02d %02d\n",
167 /* 1 */ (UINT32) Op->Asl.Value.Integer,
168 /* 2 */ Op->Asl.ParseOpcode,
169 /* 3 */ Op->Asl.AmlOpcode,
170 /* 4 */ Op->Asl.AmlOpcodeLength,
171 /* 5 */ Op->Asl.AmlPkgLenBytes,
172 /* 6 */ Op->Asl.AmlLength,
173 /* 7 */ Op->Asl.AmlSubtreeLength,
174 /* 8 */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
176 /* 10 */ Op->Asl.Child,
177 /* 11 */ Op->Asl.Parent,
178 /* 12 */ Op->Asl.CompileFlags,
179 /* 13 */ Op->Asl.AcpiBtype,
180 /* 14 */ Op->Asl.FinalAmlLength,
181 /* 15 */ Op->Asl.Column,
182 /* 16 */ Op->Asl.LineNumber);
184 /* Generate the AML for this node */
191 /*******************************************************************************
193 * FUNCTION: CgLocalWriteAmlData
195 * PARAMETERS: Op - Current parse op
196 * Buffer - Buffer to write
197 * Length - Size of data in buffer
201 * DESCRIPTION: Write a buffer of AML data to the AML output file.
203 ******************************************************************************/
206 CgLocalWriteAmlData (
207 ACPI_PARSE_OBJECT *Op,
212 /* Write the raw data to the AML file */
214 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
216 /* Update the final AML length for this node (used for listings) */
220 Op->Asl.FinalAmlLength += Length;
225 /*******************************************************************************
227 * FUNCTION: CgWriteAmlOpcode
229 * PARAMETERS: Op - Parse node with an AML opcode
233 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
235 ******************************************************************************/
239 ACPI_PARSE_OBJECT *Op)
241 UINT8 PkgLenFirstByte;
245 UINT8 OpcodeBytes[2];
253 /* We expect some DEFAULT_ARGs, just ignore them */
255 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
260 switch (Op->Asl.AmlOpcode)
262 case AML_UNASSIGNED_OPCODE:
264 /* These opcodes should not get here */
266 printf ("Found a node with an unassigned AML opcode\n");
267 fprintf (stderr, "Found a node with an unassigned AML opcode\n");
270 case AML_INT_RESERVEDFIELD_OP:
272 /* Special opcodes for within a field definition */
277 case AML_INT_ACCESSFIELD_OP:
283 Aml.Opcode = Op->Asl.AmlOpcode;
290 case AML_PACKAGE_LENGTH:
292 /* Value is the length to be encoded (Used in field definitions) */
294 PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
299 /* Check for two-byte opcode */
301 if (Aml.Opcode > 0x00FF)
303 /* Write the high byte first */
305 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
308 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
310 /* Subtreelength doesn't include length of package length bytes */
312 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
316 /* Does this opcode have an associated "PackageLength" field? */
318 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
320 if (Op->Asl.AmlPkgLenBytes == 1)
322 /* Simplest case -- no bytes to follow, just write the count */
324 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
326 else if (Op->Asl.AmlPkgLenBytes != 0)
329 * Encode the "bytes to follow" in the first byte, top two bits.
330 * The low-order nybble of the length is in the bottom 4 bits
332 PkgLenFirstByte = (UINT8)
333 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
334 (PkgLen.LenBytes[0] & 0x0F));
336 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
339 * Shift the length over by the 4 bits we just stuffed
344 /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
346 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
348 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
357 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
362 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
367 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
372 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
377 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
381 /* All data opcodes must appear above */
387 /*******************************************************************************
389 * FUNCTION: CgWriteTableHeader
391 * PARAMETERS: Op - The DEFINITIONBLOCK node
395 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
397 ******************************************************************************/
401 ACPI_PARSE_OBJECT *Op)
403 ACPI_PARSE_OBJECT *Child;
408 Child = Op->Asl.Child;
412 Child = Child->Asl.Next;
413 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
417 Child = Child->Asl.Next;
418 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
420 /* Command-line Revision override */
422 if (Gbl_RevisionOverride)
424 TableHeader.Revision = Gbl_RevisionOverride;
429 Child = Child->Asl.Next;
430 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
434 Child = Child->Asl.Next;
435 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
439 Child = Child->Asl.Next;
440 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
444 strncpy (TableHeader.AslCompilerId, ASL_CREATOR_ID, 4);
446 /* Compiler version */
448 TableHeader.AslCompilerRevision = ASL_REVISION;
450 /* Table length. Checksum zero for now, will rewrite later */
452 TableHeader.Length = Gbl_TableLength;
453 TableHeader.Checksum = 0;
455 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
459 /*******************************************************************************
461 * FUNCTION: CgCloseTable
467 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
468 * re-writing the header.
470 ******************************************************************************/
480 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
483 /* Calculate the checksum over the entire file */
485 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
487 Sum = (signed char) (Sum + FileByte);
490 /* Re-write the table header with the checksum */
492 TableHeader.Checksum = (UINT8) (0 - Sum);
494 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
495 CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
499 /*******************************************************************************
501 * FUNCTION: CgWriteNode
503 * PARAMETERS: Op - Parse node to write.
507 * DESCRIPTION: Write the AML that corresponds to a parse node.
509 ******************************************************************************/
513 ACPI_PARSE_OBJECT *Op)
515 ASL_RESOURCE_NODE *Rnode;
518 /* Always check for DEFAULT_ARG and other "Noop" nodes */
519 /* TBD: this may not be the best place for this check */
521 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) ||
522 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) ||
523 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) ||
524 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
529 Op->Asl.FinalAmlLength = 0;
531 switch (Op->Asl.AmlOpcode)
533 case AML_RAW_DATA_BYTE:
534 case AML_RAW_DATA_WORD:
535 case AML_RAW_DATA_DWORD:
536 case AML_RAW_DATA_QWORD:
538 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
542 case AML_RAW_DATA_BUFFER:
544 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
548 case AML_RAW_DATA_CHAIN:
550 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
553 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
559 /* Internal data opcodes must all appear above */
563 switch (Op->Asl.ParseOpcode)
565 case PARSEOP_DEFAULT_ARG:
569 case PARSEOP_DEFINITIONBLOCK:
571 CgWriteTableHeader (Op);
574 case PARSEOP_NAMESEG:
575 case PARSEOP_NAMESTRING:
576 case PARSEOP_METHODCALL:
578 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
583 CgWriteAmlOpcode (Op);