1 /******************************************************************************
3 * Module Name: aslcodegen - AML code generation
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include <contrib/dev/acpica/compiler/aslcompiler.h>
46 #include "aslcompiler.y.h"
47 #include <contrib/dev/acpica/include/amlcode.h>
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("aslcodegen")
52 /* Local prototypes */
56 ACPI_PARSE_OBJECT *Op,
62 ACPI_PARSE_OBJECT *Op,
68 ACPI_PARSE_OBJECT *Op);
72 ACPI_PARSE_OBJECT *Op);
80 ACPI_PARSE_OBJECT *Op);
83 /*******************************************************************************
85 * FUNCTION: CgGenerateAmlOutput
91 * DESCRIPTION: Generate AML code. Currently generates the listing file
94 ******************************************************************************/
101 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
103 /* Generate the AML output file */
105 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
107 Gbl_NextError = Gbl_ErrorLog;
109 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
110 CgAmlWriteWalk, NULL, NULL);
115 /*******************************************************************************
117 * FUNCTION: CgAmlWriteWalk
119 * PARAMETERS: ASL_WALK_CALLBACK
123 * DESCRIPTION: Parse tree walk to generate the AML code.
125 ******************************************************************************/
129 ACPI_PARSE_OBJECT *Op,
135 * Print header at level 0. Alignment assumes 32-bit pointers
139 DbgPrint (ASL_TREE_OUTPUT,
140 "Final parse tree used for AML output:\n");
141 DbgPrint (ASL_TREE_OUTPUT,
142 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr Child Parent Flags AcTyp Final Col L\n",
148 DbgPrint (ASL_TREE_OUTPUT,
149 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
150 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
152 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG ||
153 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
154 Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
156 DbgPrint (ASL_TREE_OUTPUT,
157 "%10.32s ", Op->Asl.ExternalName);
161 DbgPrint (ASL_TREE_OUTPUT, " ");
164 DbgPrint (ASL_TREE_OUTPUT,
165 "%08X %04X %04X %01X %04X %04X %04X %04X %08X %08X %08X %08X %08X %04X %02d %02d\n",
166 /* 1 */ (UINT32) Op->Asl.Value.Integer,
167 /* 2 */ Op->Asl.ParseOpcode,
168 /* 3 */ Op->Asl.AmlOpcode,
169 /* 4 */ Op->Asl.AmlOpcodeLength,
170 /* 5 */ Op->Asl.AmlPkgLenBytes,
171 /* 6 */ Op->Asl.AmlLength,
172 /* 7 */ Op->Asl.AmlSubtreeLength,
173 /* 8 */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
175 /* 10 */ Op->Asl.Child,
176 /* 11 */ Op->Asl.Parent,
177 /* 12 */ Op->Asl.CompileFlags,
178 /* 13 */ Op->Asl.AcpiBtype,
179 /* 14 */ Op->Asl.FinalAmlLength,
180 /* 15 */ Op->Asl.Column,
181 /* 16 */ Op->Asl.LineNumber);
183 /* Generate the AML for this node */
190 /*******************************************************************************
192 * FUNCTION: CgLocalWriteAmlData
194 * PARAMETERS: Op - Current parse op
195 * Buffer - Buffer to write
196 * Length - Size of data in buffer
200 * DESCRIPTION: Write a buffer of AML data to the AML output file.
202 ******************************************************************************/
205 CgLocalWriteAmlData (
206 ACPI_PARSE_OBJECT *Op,
211 /* Write the raw data to the AML file */
213 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
215 /* Update the final AML length for this node (used for listings) */
219 Op->Asl.FinalAmlLength += Length;
224 /*******************************************************************************
226 * FUNCTION: CgWriteAmlOpcode
228 * PARAMETERS: Op - Parse node with an AML opcode
232 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
234 ******************************************************************************/
238 ACPI_PARSE_OBJECT *Op)
240 UINT8 PkgLenFirstByte;
244 UINT8 OpcodeBytes[2];
252 /* We expect some DEFAULT_ARGs, just ignore them */
254 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
259 switch (Op->Asl.AmlOpcode)
261 case AML_UNASSIGNED_OPCODE:
263 /* These opcodes should not get here */
265 printf ("Found a node with an unassigned AML opcode\n");
266 FlPrintFile (ASL_FILE_STDERR, "Found a node with an unassigned AML opcode\n");
269 case AML_INT_RESERVEDFIELD_OP:
271 /* Special opcodes for within a field definition */
273 Aml.Opcode = AML_FIELD_OFFSET_OP;
276 case AML_INT_ACCESSFIELD_OP:
278 Aml.Opcode = AML_FIELD_ACCESS_OP;
281 case AML_INT_CONNECTION_OP:
283 Aml.Opcode = AML_FIELD_CONNECTION_OP;
288 Aml.Opcode = Op->Asl.AmlOpcode;
295 case AML_PACKAGE_LENGTH:
297 /* Value is the length to be encoded (Used in field definitions) */
299 PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
304 /* Check for two-byte opcode */
306 if (Aml.Opcode > 0x00FF)
308 /* Write the high byte first */
310 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
313 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
315 /* Subtreelength doesn't include length of package length bytes */
317 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
321 /* Does this opcode have an associated "PackageLength" field? */
323 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
325 if (Op->Asl.AmlPkgLenBytes == 1)
327 /* Simplest case -- no bytes to follow, just write the count */
329 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
331 else if (Op->Asl.AmlPkgLenBytes != 0)
334 * Encode the "bytes to follow" in the first byte, top two bits.
335 * The low-order nybble of the length is in the bottom 4 bits
337 PkgLenFirstByte = (UINT8)
338 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
339 (PkgLen.LenBytes[0] & 0x0F));
341 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
344 * Shift the length over by the 4 bits we just stuffed
349 /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
351 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
353 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
362 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
367 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
372 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
377 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
382 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
387 /* All data opcodes must appear above */
394 /*******************************************************************************
396 * FUNCTION: CgWriteTableHeader
398 * PARAMETERS: Op - The DEFINITIONBLOCK node
402 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
404 ******************************************************************************/
408 ACPI_PARSE_OBJECT *Op)
410 ACPI_PARSE_OBJECT *Child;
415 Child = Op->Asl.Child;
419 Child = Child->Asl.Next;
420 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
424 Child = Child->Asl.Next;
425 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
427 /* Command-line Revision override */
429 if (Gbl_RevisionOverride)
431 TableHeader.Revision = Gbl_RevisionOverride;
436 Child = Child->Asl.Next;
437 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
441 Child = Child->Asl.Next;
442 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
446 Child = Child->Asl.Next;
447 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
451 ACPI_MOVE_NAME (TableHeader.AslCompilerId, ASL_CREATOR_ID);
453 /* Compiler version */
455 TableHeader.AslCompilerRevision = ASL_REVISION;
457 /* Table length. Checksum zero for now, will rewrite later */
459 TableHeader.Length = Gbl_TableLength;
460 TableHeader.Checksum = 0;
462 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
466 /*******************************************************************************
468 * FUNCTION: CgCloseTable
474 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
475 * re-writing the header.
477 ******************************************************************************/
487 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
490 /* Calculate the checksum over the entire file */
492 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
494 Sum = (signed char) (Sum + FileByte);
497 /* Re-write the table header with the checksum */
499 TableHeader.Checksum = (UINT8) (0 - Sum);
501 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
502 CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
506 /*******************************************************************************
508 * FUNCTION: CgWriteNode
510 * PARAMETERS: Op - Parse node to write.
514 * DESCRIPTION: Write the AML that corresponds to a parse node.
516 ******************************************************************************/
520 ACPI_PARSE_OBJECT *Op)
522 ASL_RESOURCE_NODE *Rnode;
525 /* Always check for DEFAULT_ARG and other "Noop" nodes */
526 /* TBD: this may not be the best place for this check */
528 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) ||
529 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) ||
530 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) ||
531 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
536 Op->Asl.FinalAmlLength = 0;
538 switch (Op->Asl.AmlOpcode)
540 case AML_RAW_DATA_BYTE:
541 case AML_RAW_DATA_WORD:
542 case AML_RAW_DATA_DWORD:
543 case AML_RAW_DATA_QWORD:
545 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
549 case AML_RAW_DATA_BUFFER:
551 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
555 case AML_RAW_DATA_CHAIN:
557 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
560 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
567 /* Internal data opcodes must all appear above */
572 switch (Op->Asl.ParseOpcode)
574 case PARSEOP_DEFAULT_ARG:
578 case PARSEOP_DEFINITIONBLOCK:
580 CgWriteTableHeader (Op);
583 case PARSEOP_NAMESEG:
584 case PARSEOP_NAMESTRING:
585 case PARSEOP_METHODCALL:
587 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
592 CgWriteAmlOpcode (Op);