1 /******************************************************************************
3 * Module Name: aslcodegen - AML code generation
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
48 #define _COMPONENT ACPI_COMPILER
49 ACPI_MODULE_NAME ("aslcodegen")
51 /* Local prototypes */
55 ACPI_PARSE_OBJECT *Op,
61 ACPI_PARSE_OBJECT *Op,
67 ACPI_PARSE_OBJECT *Op);
71 ACPI_PARSE_OBJECT *Op);
79 ACPI_PARSE_OBJECT *Op);
82 /*******************************************************************************
84 * FUNCTION: CgGenerateAmlOutput
90 * DESCRIPTION: Generate AML code. Currently generates the listing file
93 ******************************************************************************/
100 DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
102 /* Generate the AML output file */
104 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
106 Gbl_NextError = Gbl_ErrorLog;
108 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
109 CgAmlWriteWalk, NULL, NULL);
111 DbgPrint (ASL_TREE_OUTPUT,
112 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr"
113 " Parent Child Next Flags AcTyp Final Col L# EL# LL# ELL#\n",
120 /*******************************************************************************
122 * FUNCTION: CgAmlWriteWalk
124 * PARAMETERS: ASL_WALK_CALLBACK
128 * DESCRIPTION: Parse tree walk to generate the AML code.
130 ******************************************************************************/
134 ACPI_PARSE_OBJECT *Op,
140 * Print header at level 0. Alignment assumes 32-bit pointers
144 DbgPrint (ASL_TREE_OUTPUT,
145 "Final parse tree used for AML output:\n");
146 DbgPrint (ASL_TREE_OUTPUT,
147 "%*s Value P_Op A_Op OpLen PByts Len SubLen PSubLen OpPtr"
148 " Parent Child Next Flags AcTyp Final Col L# EL# LL# ELL#\n",
154 DbgPrint (ASL_TREE_OUTPUT,
155 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
156 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
158 if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG ||
159 Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
160 Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
162 DbgPrint (ASL_TREE_OUTPUT,
163 "%10.32s ", Op->Asl.ExternalName);
167 DbgPrint (ASL_TREE_OUTPUT, " ");
170 DbgPrint (ASL_TREE_OUTPUT,
171 "%08X %04X %04X %01X %04X %04X %04X %04X "
172 "%08X %08X %08X %08X %08X %08X %04X %02d %02d %02d %02d %02d\n",
173 /* 1 */ (UINT32) Op->Asl.Value.Integer,
174 /* 2 */ Op->Asl.ParseOpcode,
175 /* 3 */ Op->Asl.AmlOpcode,
176 /* 4 */ Op->Asl.AmlOpcodeLength,
177 /* 5 */ Op->Asl.AmlPkgLenBytes,
178 /* 6 */ Op->Asl.AmlLength,
179 /* 7 */ Op->Asl.AmlSubtreeLength,
180 /* 8 */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
182 /* 10 */ Op->Asl.Parent,
183 /* 11 */ Op->Asl.Child,
184 /* 12 */ Op->Asl.Next,
185 /* 13 */ Op->Asl.CompileFlags,
186 /* 14 */ Op->Asl.AcpiBtype,
187 /* 15 */ Op->Asl.FinalAmlLength,
188 /* 16 */ Op->Asl.Column,
189 /* 17 */ Op->Asl.LineNumber,
190 /* 18 */ Op->Asl.EndLine,
191 /* 19 */ Op->Asl.LogicalLineNumber,
192 /* 20 */ Op->Asl.EndLogicalLine);
194 /* Generate the AML for this node */
201 /*******************************************************************************
203 * FUNCTION: CgLocalWriteAmlData
205 * PARAMETERS: Op - Current parse op
206 * Buffer - Buffer to write
207 * Length - Size of data in buffer
211 * DESCRIPTION: Write a buffer of AML data to the AML output file.
213 ******************************************************************************/
216 CgLocalWriteAmlData (
217 ACPI_PARSE_OBJECT *Op,
222 /* Write the raw data to the AML file */
224 FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
226 /* Update the final AML length for this node (used for listings) */
230 Op->Asl.FinalAmlLength += Length;
235 /*******************************************************************************
237 * FUNCTION: CgWriteAmlOpcode
239 * PARAMETERS: Op - Parse node with an AML opcode
243 * DESCRIPTION: Write the AML opcode corresponding to a parse node.
245 ******************************************************************************/
249 ACPI_PARSE_OBJECT *Op)
251 UINT8 PkgLenFirstByte;
255 UINT8 OpcodeBytes[2];
263 /* We expect some DEFAULT_ARGs, just ignore them */
265 if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
270 switch (Op->Asl.AmlOpcode)
272 case AML_UNASSIGNED_OPCODE:
274 /* These opcodes should not get here */
276 printf ("Found a node with an unassigned AML opcode\n");
277 FlPrintFile (ASL_FILE_STDERR,
278 "Found a node with an unassigned AML opcode\n");
281 case AML_INT_RESERVEDFIELD_OP:
283 /* Special opcodes for within a field definition */
285 Aml.Opcode = AML_FIELD_OFFSET_OP;
288 case AML_INT_ACCESSFIELD_OP:
290 Aml.Opcode = AML_FIELD_ACCESS_OP;
293 case AML_INT_CONNECTION_OP:
295 Aml.Opcode = AML_FIELD_CONNECTION_OP;
300 Aml.Opcode = Op->Asl.AmlOpcode;
307 case AML_PACKAGE_LENGTH:
309 /* Value is the length to be encoded (Used in field definitions) */
311 PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
316 /* Check for two-byte opcode */
318 if (Aml.Opcode > 0x00FF)
320 /* Write the high byte first */
322 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
325 CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
327 /* Subtreelength doesn't include length of package length bytes */
329 PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
333 /* Does this opcode have an associated "PackageLength" field? */
335 if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
337 if (Op->Asl.AmlPkgLenBytes == 1)
339 /* Simplest case -- no bytes to follow, just write the count */
341 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
343 else if (Op->Asl.AmlPkgLenBytes != 0)
346 * Encode the "bytes to follow" in the first byte, top two bits.
347 * The low-order nybble of the length is in the bottom 4 bits
349 PkgLenFirstByte = (UINT8)
350 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
351 (PkgLen.LenBytes[0] & 0x0F));
353 CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
356 * Shift the length over by the 4 bits we just stuffed
362 * Now we can write the remaining bytes -
363 * either 1, 2, or 3 bytes
365 for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
367 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
376 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
381 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
386 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
391 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
396 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
401 /* All data opcodes must appear above */
408 /*******************************************************************************
410 * FUNCTION: CgWriteTableHeader
412 * PARAMETERS: Op - The DEFINITIONBLOCK node
416 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
418 ******************************************************************************/
422 ACPI_PARSE_OBJECT *Op)
424 ACPI_PARSE_OBJECT *Child;
429 Child = Op->Asl.Child;
433 Child = Child->Asl.Next;
434 strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
438 Child = Child->Asl.Next;
439 TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
441 /* Command-line Revision override */
443 if (Gbl_RevisionOverride)
445 TableHeader.Revision = Gbl_RevisionOverride;
450 Child = Child->Asl.Next;
451 strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
455 Child = Child->Asl.Next;
456 strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
460 Child = Child->Asl.Next;
461 TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
465 ACPI_MOVE_NAME (TableHeader.AslCompilerId, ASL_CREATOR_ID);
467 /* Compiler version */
469 TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
471 /* Table length. Checksum zero for now, will rewrite later */
473 TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
474 Op->Asl.AmlSubtreeLength;
475 TableHeader.Checksum = 0;
477 Op->Asl.FinalAmlOffset = ftell (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
479 /* Write entire header and clear the table header global */
481 CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
482 memset (&TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
486 /*******************************************************************************
488 * FUNCTION: CgUpdateHeader
490 * PARAMETERS: Op - Op for the Definition Block
494 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
495 * re-writing the header for the input definition block
497 ******************************************************************************/
501 ACPI_PARSE_OBJECT *Op)
510 /* Calculate the checksum over the entire definition block */
513 Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
514 FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
516 for (i = 0; i < Length; i++)
518 if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
520 printf ("EOF while reading checksum bytes\n");
524 Sum = (signed char) (Sum + FileByte);
527 Checksum = (UINT8) (0 - Sum);
529 /* Re-write the the checksum byte */
531 FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
532 ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
534 FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
538 /*******************************************************************************
540 * FUNCTION: CgCloseTable
546 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
547 * re-writing each table header. This allows support for
548 * multiple definition blocks in a single source file.
550 ******************************************************************************/
556 ACPI_PARSE_OBJECT *Op;
559 /* Process all definition blocks */
561 Op = RootNode->Asl.Child;
570 /*******************************************************************************
572 * FUNCTION: CgWriteNode
574 * PARAMETERS: Op - Parse node to write.
578 * DESCRIPTION: Write the AML that corresponds to a parse node.
580 ******************************************************************************/
584 ACPI_PARSE_OBJECT *Op)
586 ASL_RESOURCE_NODE *Rnode;
589 /* Always check for DEFAULT_ARG and other "Noop" nodes */
590 /* TBD: this may not be the best place for this check */
592 if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG) ||
593 (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) ||
594 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE) ||
595 (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
600 Op->Asl.FinalAmlLength = 0;
602 switch (Op->Asl.AmlOpcode)
604 case AML_RAW_DATA_BYTE:
605 case AML_RAW_DATA_WORD:
606 case AML_RAW_DATA_DWORD:
607 case AML_RAW_DATA_QWORD:
609 CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
613 case AML_RAW_DATA_BUFFER:
615 CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
619 case AML_RAW_DATA_CHAIN:
621 Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
624 CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
631 /* Internal data opcodes must all appear above */
636 switch (Op->Asl.ParseOpcode)
638 case PARSEOP_DEFAULT_ARG:
642 case PARSEOP_DEFINITION_BLOCK:
644 CgWriteTableHeader (Op);
647 case PARSEOP_NAMESEG:
648 case PARSEOP_NAMESTRING:
649 case PARSEOP_METHODCALL:
651 CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
656 CgWriteAmlOpcode (Op);