1 /******************************************************************************
3 * Module Name: aslcodegen - AML code generation
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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"
47 #include "acconvert.h"
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("aslcodegen")
52 /* Local prototypes */
56 ACPI_PARSE_OBJECT
*Op
,
62 ACPI_PARSE_OBJECT
*Op
);
66 ACPI_PARSE_OBJECT
*Op
);
74 ACPI_PARSE_OBJECT
*Op
);
77 /*******************************************************************************
79 * FUNCTION: CgGenerateAmlOutput
85 * DESCRIPTION: Generate AML code. Currently generates the listing file
88 ******************************************************************************/
95 /* Generate the AML output file */
97 FlSeekFile (ASL_FILE_SOURCE_OUTPUT
, 0);
99 Gbl_NextError
= Gbl_ErrorLog
;
101 TrWalkParseTree (Gbl_ParseTreeRoot
, ASL_WALK_VISIT_DOWNWARD
,
102 CgAmlWriteWalk
, NULL
, NULL
);
104 DbgPrint (ASL_TREE_OUTPUT
, ASL_PARSE_TREE_HEADER2
);
109 /*******************************************************************************
111 * FUNCTION: CgAmlWriteWalk
113 * PARAMETERS: ASL_WALK_CALLBACK
117 * DESCRIPTION: Parse tree walk to generate the AML code.
119 ******************************************************************************/
123 ACPI_PARSE_OBJECT
*Op
,
128 /* Generate the AML for this node */
137 /* Print header at level 0. Alignment assumes 32-bit pointers */
141 DbgPrint (ASL_TREE_OUTPUT
,
142 "\nFinal parse tree used for AML output:\n");
143 DbgPrint (ASL_TREE_OUTPUT
, ASL_PARSE_TREE_HEADER2
);
146 /* Dump ParseOp name and possible value */
148 switch (Op
->Asl
.ParseOpcode
)
150 case PARSEOP_NAMESEG
:
151 case PARSEOP_NAMESTRING
:
152 case PARSEOP_METHODCALL
:
153 case PARSEOP_STRING_LITERAL
:
155 UtDumpStringOp (Op
, Level
);
160 UtDumpBasicOp (Op
, Level
);
164 DbgPrint (ASL_TREE_OUTPUT
, ASL_PARSE_TREE_DEBUG2
,
165 /* 1 */ (UINT32
) Op
->Asl
.Value
.Integer
,
166 /* 2 */ Op
->Asl
.ParseOpcode
,
167 /* 3 */ Op
->Asl
.AmlOpcode
,
168 /* 4 */ Op
->Asl
.AmlOpcodeLength
,
169 /* 5 */ Op
->Asl
.AmlPkgLenBytes
,
170 /* 6 */ Op
->Asl
.AmlLength
,
171 /* 7 */ Op
->Asl
.AmlSubtreeLength
,
172 /* 8 */ Op
->Asl
.Parent
? Op
->Asl
.Parent
->Asl
.AmlSubtreeLength
: 0,
174 /* 10 */ Op
->Asl
.Parent
,
175 /* 11 */ Op
->Asl
.Child
,
176 /* 12 */ Op
->Asl
.Next
,
177 /* 13 */ Op
->Asl
.CompileFlags
,
178 /* 14 */ Op
->Asl
.AcpiBtype
,
179 /* 15 */ Op
->Asl
.FinalAmlLength
,
180 /* 16 */ Op
->Asl
.Column
,
181 /* 17 */ Op
->Asl
.LineNumber
,
182 /* 18 */ Op
->Asl
.EndLine
,
183 /* 19 */ Op
->Asl
.LogicalLineNumber
,
184 /* 20 */ Op
->Asl
.EndLogicalLine
);
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
)
260 * Before printing the bytecode, generate comment byte codes
261 * associated with this node.
263 if (Gbl_CaptureComments
)
265 CgWriteAmlComment(Op
);
268 switch (Op
->Asl
.AmlOpcode
)
270 case AML_UNASSIGNED_OPCODE
:
272 /* These opcodes should not get here */
274 printf ("Found a node with an unassigned AML opcode\n");
275 FlPrintFile (ASL_FILE_STDERR
,
276 "Found a node with an unassigned AML opcode\n");
279 case AML_INT_RESERVEDFIELD_OP
:
281 /* Special opcodes for within a field definition */
283 Aml
.Opcode
= AML_FIELD_OFFSET_OP
;
286 case AML_INT_ACCESSFIELD_OP
:
288 Aml
.Opcode
= AML_FIELD_ACCESS_OP
;
291 case AML_INT_CONNECTION_OP
:
293 Aml
.Opcode
= AML_FIELD_CONNECTION_OP
;
298 Aml
.Opcode
= Op
->Asl
.AmlOpcode
;
305 case AML_PACKAGE_LENGTH
:
307 /* Value is the length to be encoded (Used in field definitions) */
309 PkgLen
.Len
= (UINT32
) Op
->Asl
.Value
.Integer
;
314 /* Check for two-byte opcode */
316 if (Aml
.Opcode
> 0x00FF)
318 /* Write the high byte first */
320 CgLocalWriteAmlData (Op
, &Aml
.OpcodeBytes
[1], 1);
323 CgLocalWriteAmlData (Op
, &Aml
.OpcodeBytes
[0], 1);
325 /* Subtreelength doesn't include length of package length bytes */
327 PkgLen
.Len
= Op
->Asl
.AmlSubtreeLength
+ Op
->Asl
.AmlPkgLenBytes
;
331 /* Does this opcode have an associated "PackageLength" field? */
333 if (Op
->Asl
.CompileFlags
& NODE_AML_PACKAGE
)
335 if (Op
->Asl
.AmlPkgLenBytes
== 1)
337 /* Simplest case -- no bytes to follow, just write the count */
339 CgLocalWriteAmlData (Op
, &PkgLen
.LenBytes
[0], 1);
341 else if (Op
->Asl
.AmlPkgLenBytes
!= 0)
344 * Encode the "bytes to follow" in the first byte, top two bits.
345 * The low-order nybble of the length is in the bottom 4 bits
347 PkgLenFirstByte
= (UINT8
)
348 (((UINT32
) (Op
->Asl
.AmlPkgLenBytes
- 1) << 6) |
349 (PkgLen
.LenBytes
[0] & 0x0F));
351 CgLocalWriteAmlData (Op
, &PkgLenFirstByte
, 1);
354 * Shift the length over by the 4 bits we just stuffed
360 * Now we can write the remaining bytes -
361 * either 1, 2, or 3 bytes
363 for (i
= 0; i
< (UINT32
) (Op
->Asl
.AmlPkgLenBytes
- 1); i
++)
365 CgLocalWriteAmlData (Op
, &PkgLen
.LenBytes
[i
], 1);
374 CgLocalWriteAmlData (Op
, &Op
->Asl
.Value
.Integer
, 1);
379 CgLocalWriteAmlData (Op
, &Op
->Asl
.Value
.Integer
, 2);
384 CgLocalWriteAmlData (Op
, &Op
->Asl
.Value
.Integer
, 4);
389 CgLocalWriteAmlData (Op
, &Op
->Asl
.Value
.Integer
, 8);
394 CgLocalWriteAmlData (Op
, Op
->Asl
.Value
.String
, Op
->Asl
.AmlLength
);
399 /* All data opcodes must appear above */
406 /*******************************************************************************
408 * FUNCTION: CgWriteTableHeader
410 * PARAMETERS: Op - The DEFINITIONBLOCK node
414 * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
416 ******************************************************************************/
420 ACPI_PARSE_OBJECT
*Op
)
422 ACPI_PARSE_OBJECT
*Child
;
423 UINT32 CommentLength
;
424 ACPI_COMMENT_NODE
*Current
;
429 Child
= Op
->Asl
.Child
;
433 Child
= Child
->Asl
.Next
;
436 * For ASL-/ASL+ converter: replace the table signature with
437 * "XXXX" and save the original table signature. This results in an AML
438 * file with the signature "XXXX". The converter should remove this AML
439 * file. In the event where this AML file does not get deleted, the
440 * "XXXX" table signature prevents this AML file from running on the AML
443 if (Gbl_CaptureComments
)
445 strncpy(AcpiGbl_TableSig
, Child
->Asl
.Value
.String
, 4);
446 Child
->Asl
.Value
.String
= ACPI_SIG_XXXX
;
449 strncpy (TableHeader
.Signature
, Child
->Asl
.Value
.String
, 4);
453 Child
= Child
->Asl
.Next
;
454 TableHeader
.Revision
= (UINT8
) Child
->Asl
.Value
.Integer
;
456 /* Command-line Revision override */
458 if (Gbl_RevisionOverride
)
460 TableHeader
.Revision
= Gbl_RevisionOverride
;
465 Child
= Child
->Asl
.Next
;
466 strncpy (TableHeader
.OemId
, Child
->Asl
.Value
.String
, 6);
470 Child
= Child
->Asl
.Next
;
471 strncpy (TableHeader
.OemTableId
, Child
->Asl
.Value
.String
, 8);
475 Child
= Child
->Asl
.Next
;
476 TableHeader
.OemRevision
= (UINT32
) Child
->Asl
.Value
.Integer
;
480 ACPI_MOVE_NAME (TableHeader
.AslCompilerId
, ASL_CREATOR_ID
);
482 /* Compiler version */
484 TableHeader
.AslCompilerRevision
= ACPI_CA_VERSION
;
486 /* Table length. Checksum zero for now, will rewrite later */
488 TableHeader
.Length
= sizeof (ACPI_TABLE_HEADER
) +
489 Op
->Asl
.AmlSubtreeLength
;
491 /* Calculate the comment lengths for this definition block parseOp */
493 if (Gbl_CaptureComments
)
495 CvDbgPrint ("Calculating comment lengths for %s in write header\n",
496 Op
->Asl
.ParseOpName
);
499 * Take the filename without extensions, add 3 for the new extension
500 * and another 3 for the a908 bytecode and null terminator.
502 TableHeader
.Length
+= strrchr (Gbl_ParseTreeRoot
->Asl
.Filename
, '.')
503 - Gbl_ParseTreeRoot
->Asl
.Filename
+ 1 + 3 + 3;
504 Op
->Asl
.AmlSubtreeLength
+=
505 strlen (Gbl_ParseTreeRoot
->Asl
.Filename
) + 3;
506 CvDbgPrint (" Length: %lu\n",
507 strlen (Gbl_ParseTreeRoot
->Asl
.Filename
) + 3);
509 if (Op
->Asl
.CommentList
)
511 Current
= Op
->Asl
.CommentList
;
514 CommentLength
= strlen (Current
->Comment
)+3;
515 CvDbgPrint ("Length of standard comment): %d\n", CommentLength
);
516 CvDbgPrint (" Comment string: %s\n\n", Current
->Comment
);
517 TableHeader
.Length
+= CommentLength
;
518 Op
->Asl
.AmlSubtreeLength
+= CommentLength
;
519 Current
= Current
->Next
;
520 CvDbgPrint (" Length: %u\n", CommentLength
);
523 if (Op
->Asl
.CloseBraceComment
)
525 CommentLength
= strlen (Op
->Asl
.CloseBraceComment
)+3;
526 CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength
);
527 CvDbgPrint (" Comment string: %s\n\n", Op
->Asl
.CloseBraceComment
);
528 TableHeader
.Length
+= CommentLength
;
529 Op
->Asl
.AmlSubtreeLength
+= CommentLength
;
530 CvDbgPrint (" Length: %u\n", CommentLength
);
534 TableHeader
.Checksum
= 0;
536 Op
->Asl
.FinalAmlOffset
= ftell (Gbl_Files
[ASL_FILE_AML_OUTPUT
].Handle
);
538 /* Write entire header and clear the table header global */
540 CgLocalWriteAmlData (Op
, &TableHeader
, sizeof (ACPI_TABLE_HEADER
));
541 memset (&TableHeader
, 0, sizeof (ACPI_TABLE_HEADER
));
545 /*******************************************************************************
547 * FUNCTION: CgUpdateHeader
549 * PARAMETERS: Op - Op for the Definition Block
553 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
554 * re-writing the header for the input definition block
556 ******************************************************************************/
560 ACPI_PARSE_OBJECT
*Op
)
569 /* Calculate the checksum over the entire definition block */
572 Length
= sizeof (ACPI_TABLE_HEADER
) + Op
->Asl
.AmlSubtreeLength
;
573 FlSeekFile (ASL_FILE_AML_OUTPUT
, Op
->Asl
.FinalAmlOffset
);
575 for (i
= 0; i
< Length
; i
++)
577 if (FlReadFile (ASL_FILE_AML_OUTPUT
, &FileByte
, 1) != AE_OK
)
579 printf ("EOF while reading checksum bytes\n");
583 Sum
= (signed char) (Sum
+ FileByte
);
586 Checksum
= (UINT8
) (0 - Sum
);
588 /* Re-write the the checksum byte */
590 FlSeekFile (ASL_FILE_AML_OUTPUT
, Op
->Asl
.FinalAmlOffset
+
591 ACPI_OFFSET (ACPI_TABLE_HEADER
, Checksum
));
593 FlWriteFile (ASL_FILE_AML_OUTPUT
, &Checksum
, 1);
597 /*******************************************************************************
599 * FUNCTION: CgCloseTable
605 * DESCRIPTION: Complete the ACPI table by calculating the checksum and
606 * re-writing each table header. This allows support for
607 * multiple definition blocks in a single source file.
609 ******************************************************************************/
615 ACPI_PARSE_OBJECT
*Op
;
618 /* Process all definition blocks */
620 Op
= Gbl_ParseTreeRoot
->Asl
.Child
;
629 /*******************************************************************************
631 * FUNCTION: CgWriteNode
633 * PARAMETERS: Op - Parse node to write.
637 * DESCRIPTION: Write the AML that corresponds to a parse node.
639 ******************************************************************************/
643 ACPI_PARSE_OBJECT
*Op
)
645 ASL_RESOURCE_NODE
*Rnode
;
648 /* Write all comments here. */
649 if (Gbl_CaptureComments
)
651 CgWriteAmlComment(Op
);
654 /* Always check for DEFAULT_ARG and other "Noop" nodes */
655 /* TBD: this may not be the best place for this check */
657 if ((Op
->Asl
.ParseOpcode
== PARSEOP_DEFAULT_ARG
) ||
658 (Op
->Asl
.ParseOpcode
== PARSEOP_INCLUDE
) ||
659 (Op
->Asl
.ParseOpcode
== PARSEOP_INCLUDE_END
))
664 if ((Op
->Asl
.ParseOpcode
== PARSEOP_EXTERNAL
) &&
665 Gbl_DoExternals
== FALSE
)
670 Op
->Asl
.FinalAmlLength
= 0;
672 switch (Op
->Asl
.AmlOpcode
)
674 case AML_RAW_DATA_BYTE
:
675 case AML_RAW_DATA_WORD
:
676 case AML_RAW_DATA_DWORD
:
677 case AML_RAW_DATA_QWORD
:
679 CgLocalWriteAmlData (Op
, &Op
->Asl
.Value
.Integer
, Op
->Asl
.AmlLength
);
683 case AML_RAW_DATA_BUFFER
:
685 CgLocalWriteAmlData (Op
, Op
->Asl
.Value
.Buffer
, Op
->Asl
.AmlLength
);
689 case AML_RAW_DATA_CHAIN
:
691 Rnode
= ACPI_CAST_PTR (ASL_RESOURCE_NODE
, Op
->Asl
.Value
.Buffer
);
694 CgLocalWriteAmlData (Op
, Rnode
->Buffer
, Rnode
->BufferLength
);
701 /* Internal data opcodes must all appear above */
706 switch (Op
->Asl
.ParseOpcode
)
708 case PARSEOP_DEFAULT_ARG
:
712 case PARSEOP_DEFINITION_BLOCK
:
714 CgWriteTableHeader (Op
);
715 if (Gbl_CaptureComments
)
717 CgWriteAmlDefBlockComment (Op
);
721 case PARSEOP_NAMESEG
:
722 case PARSEOP_NAMESTRING
:
723 case PARSEOP_METHODCALL
:
725 CgLocalWriteAmlData (Op
, Op
->Asl
.Value
.String
, Op
->Asl
.AmlLength
);
730 CgWriteAmlOpcode (Op
);