xref: /freebsd/sys/contrib/dev/acpica/compiler/aslcodegen.c (revision 722b16673c40aedf280895f2f2f676bb494518d7)
153289f6aSNate Lawson /******************************************************************************
253289f6aSNate Lawson  *
353289f6aSNate Lawson  * Module Name: aslcodegen - AML code generation
453289f6aSNate Lawson  *
553289f6aSNate Lawson  *****************************************************************************/
653289f6aSNate Lawson 
70d84335fSJung-uk Kim /******************************************************************************
80d84335fSJung-uk Kim  *
90d84335fSJung-uk Kim  * 1. Copyright Notice
100d84335fSJung-uk Kim  *
11*722b1667SJung-uk Kim  * Some or all of this work - Copyright (c) 1999 - 2023, Intel Corp.
1253289f6aSNate Lawson  * All rights reserved.
1353289f6aSNate Lawson  *
140d84335fSJung-uk Kim  * 2. License
150d84335fSJung-uk Kim  *
160d84335fSJung-uk Kim  * 2.1. This is your license from Intel Corp. under its intellectual property
170d84335fSJung-uk Kim  * rights. You may have additional license terms from the party that provided
180d84335fSJung-uk Kim  * you this software, covering your right to use that party's intellectual
190d84335fSJung-uk Kim  * property rights.
200d84335fSJung-uk Kim  *
210d84335fSJung-uk Kim  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220d84335fSJung-uk Kim  * copy of the source code appearing in this file ("Covered Code") an
230d84335fSJung-uk Kim  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240d84335fSJung-uk Kim  * base code distributed originally by Intel ("Original Intel Code") to copy,
250d84335fSJung-uk Kim  * make derivatives, distribute, use and display any portion of the Covered
260d84335fSJung-uk Kim  * Code in any form, with the right to sublicense such rights; and
270d84335fSJung-uk Kim  *
280d84335fSJung-uk Kim  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290d84335fSJung-uk Kim  * license (with the right to sublicense), under only those claims of Intel
300d84335fSJung-uk Kim  * patents that are infringed by the Original Intel Code, to make, use, sell,
310d84335fSJung-uk Kim  * offer to sell, and import the Covered Code and derivative works thereof
320d84335fSJung-uk Kim  * solely to the minimum extent necessary to exercise the above copyright
330d84335fSJung-uk Kim  * license, and in no event shall the patent license extend to any additions
340d84335fSJung-uk Kim  * to or modifications of the Original Intel Code. No other license or right
350d84335fSJung-uk Kim  * is granted directly or by implication, estoppel or otherwise;
360d84335fSJung-uk Kim  *
370d84335fSJung-uk Kim  * The above copyright and patent license is granted only if the following
380d84335fSJung-uk Kim  * conditions are met:
390d84335fSJung-uk Kim  *
400d84335fSJung-uk Kim  * 3. Conditions
410d84335fSJung-uk Kim  *
420d84335fSJung-uk Kim  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
440d84335fSJung-uk Kim  * Code or modification with rights to further distribute source must include
450d84335fSJung-uk Kim  * the above Copyright Notice, the above License, this list of Conditions,
460d84335fSJung-uk Kim  * and the following Disclaimer and Export Compliance provision. In addition,
470d84335fSJung-uk Kim  * Licensee must cause all Covered Code to which Licensee contributes to
480d84335fSJung-uk Kim  * contain a file documenting the changes Licensee made to create that Covered
490d84335fSJung-uk Kim  * Code and the date of any change. Licensee must include in that file the
500d84335fSJung-uk Kim  * documentation of any changes made by any predecessor Licensee. Licensee
510d84335fSJung-uk Kim  * must include a prominent statement that the modification is derived,
520d84335fSJung-uk Kim  * directly or indirectly, from Original Intel Code.
530d84335fSJung-uk Kim  *
540d84335fSJung-uk Kim  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550d84335fSJung-uk Kim  * Redistribution of source code of any substantial portion of the Covered
560d84335fSJung-uk Kim  * Code or modification without rights to further distribute source must
570d84335fSJung-uk Kim  * include the following Disclaimer and Export Compliance provision in the
580d84335fSJung-uk Kim  * documentation and/or other materials provided with distribution. In
590d84335fSJung-uk Kim  * addition, Licensee may not authorize further sublicense of source of any
600d84335fSJung-uk Kim  * portion of the Covered Code, and must include terms to the effect that the
610d84335fSJung-uk Kim  * license from Licensee to its licensee is limited to the intellectual
620d84335fSJung-uk Kim  * property embodied in the software Licensee provides to its licensee, and
630d84335fSJung-uk Kim  * not to intellectual property embodied in modifications its licensee may
640d84335fSJung-uk Kim  * make.
650d84335fSJung-uk Kim  *
660d84335fSJung-uk Kim  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670d84335fSJung-uk Kim  * substantial portion of the Covered Code or modification must reproduce the
680d84335fSJung-uk Kim  * above Copyright Notice, and the following Disclaimer and Export Compliance
690d84335fSJung-uk Kim  * provision in the documentation and/or other materials provided with the
700d84335fSJung-uk Kim  * distribution.
710d84335fSJung-uk Kim  *
720d84335fSJung-uk Kim  * 3.4. Intel retains all right, title, and interest in and to the Original
730d84335fSJung-uk Kim  * Intel Code.
740d84335fSJung-uk Kim  *
750d84335fSJung-uk Kim  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760d84335fSJung-uk Kim  * Intel shall be used in advertising or otherwise to promote the sale, use or
770d84335fSJung-uk Kim  * other dealings in products derived from or relating to the Covered Code
780d84335fSJung-uk Kim  * without prior written authorization from Intel.
790d84335fSJung-uk Kim  *
800d84335fSJung-uk Kim  * 4. Disclaimer and Export Compliance
810d84335fSJung-uk Kim  *
820d84335fSJung-uk Kim  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830d84335fSJung-uk Kim  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840d84335fSJung-uk Kim  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850d84335fSJung-uk Kim  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860d84335fSJung-uk Kim  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870d84335fSJung-uk Kim  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880d84335fSJung-uk Kim  * PARTICULAR PURPOSE.
890d84335fSJung-uk Kim  *
900d84335fSJung-uk Kim  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910d84335fSJung-uk Kim  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920d84335fSJung-uk Kim  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930d84335fSJung-uk Kim  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940d84335fSJung-uk Kim  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950d84335fSJung-uk Kim  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960d84335fSJung-uk Kim  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970d84335fSJung-uk Kim  * LIMITED REMEDY.
980d84335fSJung-uk Kim  *
990d84335fSJung-uk Kim  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000d84335fSJung-uk Kim  * software or system incorporating such software without first obtaining any
1010d84335fSJung-uk Kim  * required license or other approval from the U. S. Department of Commerce or
1020d84335fSJung-uk Kim  * any other agency or department of the United States Government. In the
1030d84335fSJung-uk Kim  * event Licensee exports any such software from the United States or
1040d84335fSJung-uk Kim  * re-exports any such software from a foreign destination, Licensee shall
1050d84335fSJung-uk Kim  * ensure that the distribution and export/re-export of the software is in
1060d84335fSJung-uk Kim  * compliance with all laws, regulations, orders, or other restrictions of the
1070d84335fSJung-uk Kim  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080d84335fSJung-uk Kim  * any of its subsidiaries will export/re-export any technical data, process,
1090d84335fSJung-uk Kim  * software, or service, directly or indirectly, to any country for which the
1100d84335fSJung-uk Kim  * United States government or any agency thereof requires an export license,
1110d84335fSJung-uk Kim  * other governmental approval, or letter of assurance, without first obtaining
1120d84335fSJung-uk Kim  * such license, approval or letter.
1130d84335fSJung-uk Kim  *
1140d84335fSJung-uk Kim  *****************************************************************************
1150d84335fSJung-uk Kim  *
1160d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
1170d84335fSJung-uk Kim  * following license:
1180d84335fSJung-uk Kim  *
119d244b227SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
120d244b227SJung-uk Kim  * modification, are permitted provided that the following conditions
121d244b227SJung-uk Kim  * are met:
122d244b227SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
123d244b227SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
124d244b227SJung-uk Kim  *    without modification.
125d244b227SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126d244b227SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
127d244b227SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
128d244b227SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
129d244b227SJung-uk Kim  *    binary redistribution.
130d244b227SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
131d244b227SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
132d244b227SJung-uk Kim  *    from this software without specific prior written permission.
13353289f6aSNate Lawson  *
1340d84335fSJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1350d84335fSJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1360d84335fSJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1370d84335fSJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1380d84335fSJung-uk Kim  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1390d84335fSJung-uk Kim  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
1400d84335fSJung-uk Kim  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1410d84335fSJung-uk Kim  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1420d84335fSJung-uk Kim  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1430d84335fSJung-uk Kim  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
1440d84335fSJung-uk Kim  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1450d84335fSJung-uk Kim  *
1460d84335fSJung-uk Kim  * Alternatively, you may choose to be licensed under the terms of the
147d244b227SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
148d244b227SJung-uk Kim  * Software Foundation.
14953289f6aSNate Lawson  *
1500d84335fSJung-uk Kim  *****************************************************************************/
15153289f6aSNate Lawson 
152ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
15353289f6aSNate Lawson #include "aslcompiler.y.h"
154ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/amlcode.h>
1550d84335fSJung-uk Kim #include <contrib/dev/acpica/include/acconvert.h>
1569a4bc520SJung-uk Kim #include <contrib/dev/acpica/include/actbinfo.h>
15753289f6aSNate Lawson 
15853289f6aSNate Lawson #define _COMPONENT          ACPI_COMPILER
15953289f6aSNate Lawson         ACPI_MODULE_NAME    ("aslcodegen")
16053289f6aSNate Lawson 
161fba7fc7eSJung-uk Kim /* Local prototypes */
162fba7fc7eSJung-uk Kim 
163fba7fc7eSJung-uk Kim static ACPI_STATUS
164fba7fc7eSJung-uk Kim CgAmlWriteWalk (
165fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
166fba7fc7eSJung-uk Kim     UINT32                  Level,
167fba7fc7eSJung-uk Kim     void                    *Context);
168fba7fc7eSJung-uk Kim 
169fba7fc7eSJung-uk Kim static void
170fba7fc7eSJung-uk Kim CgWriteAmlOpcode (
171fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
172fba7fc7eSJung-uk Kim 
173fba7fc7eSJung-uk Kim static void
174fba7fc7eSJung-uk Kim CgWriteTableHeader (
175fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
176fba7fc7eSJung-uk Kim 
177fba7fc7eSJung-uk Kim static void
17844b0f624SJung-uk Kim CgWriteNode (
17944b0f624SJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
180fba7fc7eSJung-uk Kim 
181fba7fc7eSJung-uk Kim static void
18244b0f624SJung-uk Kim CgUpdateHeader (
183fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
184fba7fc7eSJung-uk Kim 
1859a4bc520SJung-uk Kim static void
1869a4bc520SJung-uk Kim CgUpdateCdatHeader (
1879a4bc520SJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
1889a4bc520SJung-uk Kim 
18953289f6aSNate Lawson 
19053289f6aSNate Lawson /*******************************************************************************
19153289f6aSNate Lawson  *
19253289f6aSNate Lawson  * FUNCTION:    CgGenerateAmlOutput
19353289f6aSNate Lawson  *
19453289f6aSNate Lawson  * PARAMETERS:  None.
19553289f6aSNate Lawson  *
19653289f6aSNate Lawson  * RETURN:      None
19753289f6aSNate Lawson  *
19853289f6aSNate Lawson  * DESCRIPTION: Generate AML code. Currently generates the listing file
19953289f6aSNate Lawson  *              simultaneously.
20053289f6aSNate Lawson  *
20153289f6aSNate Lawson  ******************************************************************************/
20253289f6aSNate Lawson 
20353289f6aSNate Lawson void
CgGenerateAmlOutput(void)204fba7fc7eSJung-uk Kim CgGenerateAmlOutput (
205fba7fc7eSJung-uk Kim     void)
20653289f6aSNate Lawson {
20753289f6aSNate Lawson 
20853289f6aSNate Lawson     /* Generate the AML output file */
20953289f6aSNate Lawson 
21044b0f624SJung-uk Kim     TrWalkParseTree (AslGbl_CurrentDB,
21144b0f624SJung-uk Kim         ASL_WALK_VISIT_DOWNWARD | ASL_WALK_VISIT_DB_SEPARATELY,
212fba7fc7eSJung-uk Kim         CgAmlWriteWalk, NULL, NULL);
2131c0e1b6dSJung-uk Kim 
214f8146b88SJung-uk Kim     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
2159a4bc520SJung-uk Kim     if (AcpiGbl_CDAT)
2169a4bc520SJung-uk Kim     {
2179a4bc520SJung-uk Kim         CgUpdateCdatHeader (AslGbl_CurrentDB);
2189a4bc520SJung-uk Kim     }
2199a4bc520SJung-uk Kim     else
2209a4bc520SJung-uk Kim     {
22144b0f624SJung-uk Kim         CgUpdateHeader (AslGbl_CurrentDB);
22253289f6aSNate Lawson     }
2239a4bc520SJung-uk Kim }
22453289f6aSNate Lawson 
22553289f6aSNate Lawson 
22653289f6aSNate Lawson /*******************************************************************************
22753289f6aSNate Lawson  *
22853289f6aSNate Lawson  * FUNCTION:    CgAmlWriteWalk
22953289f6aSNate Lawson  *
23053289f6aSNate Lawson  * PARAMETERS:  ASL_WALK_CALLBACK
23153289f6aSNate Lawson  *
23253289f6aSNate Lawson  * RETURN:      Status
23353289f6aSNate Lawson  *
23453289f6aSNate Lawson  * DESCRIPTION: Parse tree walk to generate the AML code.
23553289f6aSNate Lawson  *
23653289f6aSNate Lawson  ******************************************************************************/
23753289f6aSNate Lawson 
238fba7fc7eSJung-uk Kim static ACPI_STATUS
CgAmlWriteWalk(ACPI_PARSE_OBJECT * Op,UINT32 Level,void * Context)23953289f6aSNate Lawson CgAmlWriteWalk (
24053289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
24153289f6aSNate Lawson     UINT32                  Level,
24253289f6aSNate Lawson     void                    *Context)
24353289f6aSNate Lawson {
24453289f6aSNate Lawson 
245f8146b88SJung-uk Kim     /* Generate the AML for this node */
246f8146b88SJung-uk Kim 
247f8146b88SJung-uk Kim     CgWriteNode (Op);
248f8146b88SJung-uk Kim 
2496f1f1a63SJung-uk Kim     if (!AslGbl_DebugFlag)
250f8146b88SJung-uk Kim     {
251f8146b88SJung-uk Kim         return (AE_OK);
252f8146b88SJung-uk Kim     }
253f8146b88SJung-uk Kim 
254f8146b88SJung-uk Kim     /* Print header at level 0. Alignment assumes 32-bit pointers */
255f8146b88SJung-uk Kim 
2561a39cfb0SJung-uk Kim     if (!Level)
2571a39cfb0SJung-uk Kim     {
2581a39cfb0SJung-uk Kim         DbgPrint (ASL_TREE_OUTPUT,
259f8146b88SJung-uk Kim             "\nFinal parse tree used for AML output:\n");
260f8146b88SJung-uk Kim         DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
2611a39cfb0SJung-uk Kim     }
2621a39cfb0SJung-uk Kim 
263f8146b88SJung-uk Kim     /* Dump ParseOp name and possible value */
264fba7fc7eSJung-uk Kim 
265f8146b88SJung-uk Kim     switch (Op->Asl.ParseOpcode)
26653289f6aSNate Lawson     {
267f8146b88SJung-uk Kim     case PARSEOP_NAMESEG:
268f8146b88SJung-uk Kim     case PARSEOP_NAMESTRING:
269f8146b88SJung-uk Kim     case PARSEOP_METHODCALL:
270f8146b88SJung-uk Kim     case PARSEOP_STRING_LITERAL:
271f8146b88SJung-uk Kim 
272f8146b88SJung-uk Kim         UtDumpStringOp (Op, Level);
273f8146b88SJung-uk Kim         break;
274f8146b88SJung-uk Kim 
275f8146b88SJung-uk Kim     default:
276f8146b88SJung-uk Kim 
277f8146b88SJung-uk Kim         UtDumpBasicOp (Op, Level);
278f8146b88SJung-uk Kim         break;
27953289f6aSNate Lawson     }
28053289f6aSNate Lawson 
281f8146b88SJung-uk Kim     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_DEBUG2,
2821a39cfb0SJung-uk Kim         /* 1  */ (UINT32) Op->Asl.Value.Integer,
2831a39cfb0SJung-uk Kim         /* 2  */ Op->Asl.ParseOpcode,
2841a39cfb0SJung-uk Kim         /* 3  */ Op->Asl.AmlOpcode,
2851a39cfb0SJung-uk Kim         /* 4  */ Op->Asl.AmlOpcodeLength,
2861a39cfb0SJung-uk Kim         /* 5  */ Op->Asl.AmlPkgLenBytes,
2871a39cfb0SJung-uk Kim         /* 6  */ Op->Asl.AmlLength,
2881a39cfb0SJung-uk Kim         /* 7  */ Op->Asl.AmlSubtreeLength,
2891a39cfb0SJung-uk Kim         /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
2901a39cfb0SJung-uk Kim         /* 9  */ Op,
2911c0e1b6dSJung-uk Kim         /* 10 */ Op->Asl.Parent,
2921c0e1b6dSJung-uk Kim         /* 11 */ Op->Asl.Child,
2931c0e1b6dSJung-uk Kim         /* 12 */ Op->Asl.Next,
2941c0e1b6dSJung-uk Kim         /* 13 */ Op->Asl.CompileFlags,
2951c0e1b6dSJung-uk Kim         /* 14 */ Op->Asl.AcpiBtype,
2961c0e1b6dSJung-uk Kim         /* 15 */ Op->Asl.FinalAmlLength,
2971c0e1b6dSJung-uk Kim         /* 16 */ Op->Asl.Column,
2985ef50723SJung-uk Kim         /* 17 */ Op->Asl.LineNumber,
2995ef50723SJung-uk Kim         /* 18 */ Op->Asl.EndLine,
3005ef50723SJung-uk Kim         /* 19 */ Op->Asl.LogicalLineNumber,
3015ef50723SJung-uk Kim         /* 20 */ Op->Asl.EndLogicalLine);
30253289f6aSNate Lawson 
3035f9b24faSJung-uk Kim     TrPrintOpFlags (Op->Asl.CompileFlags, ASL_TREE_OUTPUT);
3045f9b24faSJung-uk Kim     DbgPrint (ASL_TREE_OUTPUT, "\n");
30553289f6aSNate Lawson     return (AE_OK);
30653289f6aSNate Lawson }
30753289f6aSNate Lawson 
30853289f6aSNate Lawson 
30953289f6aSNate Lawson /*******************************************************************************
31053289f6aSNate Lawson  *
31153289f6aSNate Lawson  * FUNCTION:    CgLocalWriteAmlData
31253289f6aSNate Lawson  *
313fba7fc7eSJung-uk Kim  * PARAMETERS:  Op              - Current parse op
314fba7fc7eSJung-uk Kim  *              Buffer          - Buffer to write
31553289f6aSNate Lawson  *              Length          - Size of data in buffer
31653289f6aSNate Lawson  *
31753289f6aSNate Lawson  * RETURN:      None
31853289f6aSNate Lawson  *
31953289f6aSNate Lawson  * DESCRIPTION: Write a buffer of AML data to the AML output file.
32053289f6aSNate Lawson  *
32153289f6aSNate Lawson  ******************************************************************************/
32253289f6aSNate Lawson 
3230d84335fSJung-uk Kim void
CgLocalWriteAmlData(ACPI_PARSE_OBJECT * Op,void * Buffer,UINT32 Length)32453289f6aSNate Lawson CgLocalWriteAmlData (
32553289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
32653289f6aSNate Lawson     void                    *Buffer,
32753289f6aSNate Lawson     UINT32                  Length)
32853289f6aSNate Lawson {
32953289f6aSNate Lawson 
33053289f6aSNate Lawson     /* Write the raw data to the AML file */
33153289f6aSNate Lawson 
33253289f6aSNate Lawson     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
33353289f6aSNate Lawson 
33453289f6aSNate Lawson     /* Update the final AML length for this node (used for listings) */
33553289f6aSNate Lawson 
33653289f6aSNate Lawson     if (Op)
33753289f6aSNate Lawson     {
33853289f6aSNate Lawson         Op->Asl.FinalAmlLength += Length;
33953289f6aSNate Lawson     }
34053289f6aSNate Lawson }
34153289f6aSNate Lawson 
34253289f6aSNate Lawson 
34353289f6aSNate Lawson /*******************************************************************************
34453289f6aSNate Lawson  *
34553289f6aSNate Lawson  * FUNCTION:    CgWriteAmlOpcode
34653289f6aSNate Lawson  *
34753289f6aSNate Lawson  * PARAMETERS:  Op            - Parse node with an AML opcode
34853289f6aSNate Lawson  *
34953289f6aSNate Lawson  * RETURN:      None.
35053289f6aSNate Lawson  *
35153289f6aSNate Lawson  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
35253289f6aSNate Lawson  *
35353289f6aSNate Lawson  ******************************************************************************/
35453289f6aSNate Lawson 
355fba7fc7eSJung-uk Kim static void
CgWriteAmlOpcode(ACPI_PARSE_OBJECT * Op)35653289f6aSNate Lawson CgWriteAmlOpcode (
35753289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op)
35853289f6aSNate Lawson {
359fba7fc7eSJung-uk Kim     UINT8                   PkgLenFirstByte;
360fba7fc7eSJung-uk Kim     UINT32                  i;
36153289f6aSNate Lawson     union {
36253289f6aSNate Lawson         UINT16                  Opcode;
36353289f6aSNate Lawson         UINT8                   OpcodeBytes[2];
36453289f6aSNate Lawson     } Aml;
36553289f6aSNate Lawson     union {
36653289f6aSNate Lawson         UINT32                  Len;
36753289f6aSNate Lawson         UINT8                   LenBytes[4];
36853289f6aSNate Lawson     } PkgLen;
36953289f6aSNate Lawson 
37053289f6aSNate Lawson 
37153289f6aSNate Lawson     /* We expect some DEFAULT_ARGs, just ignore them */
37253289f6aSNate Lawson 
37353289f6aSNate Lawson     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
37453289f6aSNate Lawson     {
37553289f6aSNate Lawson         return;
37653289f6aSNate Lawson     }
37753289f6aSNate Lawson 
3780d84335fSJung-uk Kim     /*
3790d84335fSJung-uk Kim      * Before printing the bytecode, generate comment byte codes
3800d84335fSJung-uk Kim      * associated with this node.
3810d84335fSJung-uk Kim      */
382f1db5ef7SJung-uk Kim     if (AcpiGbl_CaptureComments)
3830d84335fSJung-uk Kim     {
3840d84335fSJung-uk Kim         CgWriteAmlComment(Op);
3850d84335fSJung-uk Kim     }
3860d84335fSJung-uk Kim 
38753289f6aSNate Lawson     switch (Op->Asl.AmlOpcode)
38853289f6aSNate Lawson     {
38953289f6aSNate Lawson     case AML_UNASSIGNED_OPCODE:
39053289f6aSNate Lawson 
39153289f6aSNate Lawson         /* These opcodes should not get here */
39253289f6aSNate Lawson 
39353289f6aSNate Lawson         printf ("Found a node with an unassigned AML opcode\n");
394f8146b88SJung-uk Kim         FlPrintFile (ASL_FILE_STDERR,
395f8146b88SJung-uk Kim             "Found a node with an unassigned AML opcode\n");
39653289f6aSNate Lawson         return;
39753289f6aSNate Lawson 
39853289f6aSNate Lawson     case AML_INT_RESERVEDFIELD_OP:
39953289f6aSNate Lawson 
40053289f6aSNate Lawson         /* Special opcodes for within a field definition */
40153289f6aSNate Lawson 
4023f0275a0SJung-uk Kim         Aml.Opcode = AML_FIELD_OFFSET_OP;
40353289f6aSNate Lawson         break;
40453289f6aSNate Lawson 
40553289f6aSNate Lawson     case AML_INT_ACCESSFIELD_OP:
40653289f6aSNate Lawson 
4073f0275a0SJung-uk Kim         Aml.Opcode = AML_FIELD_ACCESS_OP;
4083f0275a0SJung-uk Kim         break;
4093f0275a0SJung-uk Kim 
4103f0275a0SJung-uk Kim     case AML_INT_CONNECTION_OP:
4113f0275a0SJung-uk Kim 
4123f0275a0SJung-uk Kim         Aml.Opcode = AML_FIELD_CONNECTION_OP;
41353289f6aSNate Lawson         break;
41453289f6aSNate Lawson 
41553289f6aSNate Lawson     default:
416a9d8d09cSJung-uk Kim 
41753289f6aSNate Lawson         Aml.Opcode = Op->Asl.AmlOpcode;
41853289f6aSNate Lawson         break;
41953289f6aSNate Lawson     }
42053289f6aSNate Lawson 
42153289f6aSNate Lawson 
42253289f6aSNate Lawson     switch (Aml.Opcode)
42353289f6aSNate Lawson     {
42453289f6aSNate Lawson     case AML_PACKAGE_LENGTH:
42553289f6aSNate Lawson 
42653289f6aSNate Lawson         /* Value is the length to be encoded (Used in field definitions) */
42753289f6aSNate Lawson 
42853289f6aSNate Lawson         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
42953289f6aSNate Lawson         break;
43053289f6aSNate Lawson 
43153289f6aSNate Lawson     default:
43253289f6aSNate Lawson 
43353289f6aSNate Lawson         /* Check for two-byte opcode */
43453289f6aSNate Lawson 
43553289f6aSNate Lawson         if (Aml.Opcode > 0x00FF)
43653289f6aSNate Lawson         {
43753289f6aSNate Lawson             /* Write the high byte first */
43853289f6aSNate Lawson 
43953289f6aSNate Lawson             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
44053289f6aSNate Lawson         }
44153289f6aSNate Lawson 
44253289f6aSNate Lawson         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
44353289f6aSNate Lawson 
44453289f6aSNate Lawson         /* Subtreelength doesn't include length of package length bytes */
44553289f6aSNate Lawson 
44653289f6aSNate Lawson         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
44753289f6aSNate Lawson         break;
44853289f6aSNate Lawson     }
44953289f6aSNate Lawson 
45053289f6aSNate Lawson     /* Does this opcode have an associated "PackageLength" field? */
45153289f6aSNate Lawson 
4525f9b24faSJung-uk Kim     if (Op->Asl.CompileFlags & OP_AML_PACKAGE)
45353289f6aSNate Lawson     {
45453289f6aSNate Lawson         if (Op->Asl.AmlPkgLenBytes == 1)
45553289f6aSNate Lawson         {
45653289f6aSNate Lawson             /* Simplest case -- no bytes to follow, just write the count */
45753289f6aSNate Lawson 
45853289f6aSNate Lawson             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
45953289f6aSNate Lawson         }
4601a39cfb0SJung-uk Kim         else if (Op->Asl.AmlPkgLenBytes != 0)
46153289f6aSNate Lawson         {
46253289f6aSNate Lawson             /*
46353289f6aSNate Lawson              * Encode the "bytes to follow" in the first byte, top two bits.
46453289f6aSNate Lawson              * The low-order nybble of the length is in the bottom 4 bits
46553289f6aSNate Lawson              */
466fba7fc7eSJung-uk Kim             PkgLenFirstByte = (UINT8)
467fba7fc7eSJung-uk Kim                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
46853289f6aSNate Lawson                 (PkgLen.LenBytes[0] & 0x0F));
46953289f6aSNate Lawson 
47053289f6aSNate Lawson             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
47153289f6aSNate Lawson 
472fba7fc7eSJung-uk Kim             /*
473fba7fc7eSJung-uk Kim              * Shift the length over by the 4 bits we just stuffed
474fba7fc7eSJung-uk Kim              * in the first byte
475fba7fc7eSJung-uk Kim              */
47653289f6aSNate Lawson             PkgLen.Len >>= 4;
47753289f6aSNate Lawson 
478f8146b88SJung-uk Kim             /*
479f8146b88SJung-uk Kim              * Now we can write the remaining bytes -
480f8146b88SJung-uk Kim              * either 1, 2, or 3 bytes
481f8146b88SJung-uk Kim              */
48253289f6aSNate Lawson             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
48353289f6aSNate Lawson             {
48453289f6aSNate Lawson                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
48553289f6aSNate Lawson             }
48653289f6aSNate Lawson         }
48753289f6aSNate Lawson     }
48853289f6aSNate Lawson 
48953289f6aSNate Lawson     switch (Aml.Opcode)
49053289f6aSNate Lawson     {
49153289f6aSNate Lawson     case AML_BYTE_OP:
49253289f6aSNate Lawson 
493fba7fc7eSJung-uk Kim         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
49453289f6aSNate Lawson         break;
49553289f6aSNate Lawson 
49653289f6aSNate Lawson     case AML_WORD_OP:
49753289f6aSNate Lawson 
498fba7fc7eSJung-uk Kim         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
49953289f6aSNate Lawson        break;
50053289f6aSNate Lawson 
50153289f6aSNate Lawson     case AML_DWORD_OP:
50253289f6aSNate Lawson 
503fba7fc7eSJung-uk Kim         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
50453289f6aSNate Lawson         break;
50553289f6aSNate Lawson 
50653289f6aSNate Lawson     case AML_QWORD_OP:
50753289f6aSNate Lawson 
50853289f6aSNate Lawson         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
50953289f6aSNate Lawson         break;
51053289f6aSNate Lawson 
51153289f6aSNate Lawson     case AML_STRING_OP:
51253289f6aSNate Lawson 
51353289f6aSNate Lawson         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
51453289f6aSNate Lawson         break;
51553289f6aSNate Lawson 
51653289f6aSNate Lawson     default:
517a9d8d09cSJung-uk Kim 
51853289f6aSNate Lawson         /* All data opcodes must appear above */
519a9d8d09cSJung-uk Kim 
52053289f6aSNate Lawson         break;
52153289f6aSNate Lawson     }
52253289f6aSNate Lawson }
52353289f6aSNate Lawson 
52453289f6aSNate Lawson 
52553289f6aSNate Lawson /*******************************************************************************
52653289f6aSNate Lawson  *
52753289f6aSNate Lawson  * FUNCTION:    CgWriteTableHeader
52853289f6aSNate Lawson  *
52953289f6aSNate Lawson  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
53053289f6aSNate Lawson  *
531fba7fc7eSJung-uk Kim  * RETURN:      None
53253289f6aSNate Lawson  *
53353289f6aSNate Lawson  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
53453289f6aSNate Lawson  *
535278f0de6SJung-uk Kim  * NOTE: Input strings should be validated before this function is invoked.
536278f0de6SJung-uk Kim  *
53753289f6aSNate Lawson  ******************************************************************************/
53853289f6aSNate Lawson 
539fba7fc7eSJung-uk Kim static void
CgWriteTableHeader(ACPI_PARSE_OBJECT * Op)54053289f6aSNate Lawson CgWriteTableHeader (
54153289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op)
54253289f6aSNate Lawson {
54353289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Child;
5440d84335fSJung-uk Kim     UINT32                  CommentLength;
5450d84335fSJung-uk Kim     ACPI_COMMENT_NODE       *Current;
54653289f6aSNate Lawson 
54753289f6aSNate Lawson 
548278f0de6SJung-uk Kim     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
549278f0de6SJung-uk Kim 
55053289f6aSNate Lawson     /* AML filename */
55153289f6aSNate Lawson 
55253289f6aSNate Lawson     Child = Op->Asl.Child;
55353289f6aSNate Lawson 
55453289f6aSNate Lawson     /* Signature */
55553289f6aSNate Lawson 
55653289f6aSNate Lawson     Child = Child->Asl.Next;
5570d84335fSJung-uk Kim 
5580d84335fSJung-uk Kim     /*
5590d84335fSJung-uk Kim      * For ASL-/ASL+ converter: replace the table signature with
5600d84335fSJung-uk Kim      * "XXXX" and save the original table signature. This results in an AML
5610d84335fSJung-uk Kim      * file with the signature "XXXX". The converter should remove this AML
5620d84335fSJung-uk Kim      * file. In the event where this AML file does not get deleted, the
5630d84335fSJung-uk Kim      * "XXXX" table signature prevents this AML file from running on the AML
5640d84335fSJung-uk Kim      * interpreter.
5650d84335fSJung-uk Kim      */
566f1db5ef7SJung-uk Kim     if (AcpiGbl_CaptureComments)
5670d84335fSJung-uk Kim     {
568278f0de6SJung-uk Kim         ACPI_COPY_NAMESEG (AcpiGbl_TableSig, Child->Asl.Value.String);
5690d84335fSJung-uk Kim         Child->Asl.Value.String = ACPI_SIG_XXXX;
5700d84335fSJung-uk Kim     }
5710d84335fSJung-uk Kim 
572278f0de6SJung-uk Kim     ACPI_COPY_NAMESEG (AslGbl_TableHeader.Signature, Child->Asl.Value.String);
57353289f6aSNate Lawson 
57453289f6aSNate Lawson     /* Revision */
57553289f6aSNate Lawson 
57653289f6aSNate Lawson     Child = Child->Asl.Next;
5776f1f1a63SJung-uk Kim     AslGbl_TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
57853289f6aSNate Lawson 
579c8466860SMark Santcroos     /* Command-line Revision override */
580c8466860SMark Santcroos 
5816f1f1a63SJung-uk Kim     if (AslGbl_RevisionOverride)
582c8466860SMark Santcroos     {
5836f1f1a63SJung-uk Kim         AslGbl_TableHeader.Revision = AslGbl_RevisionOverride;
584c8466860SMark Santcroos     }
585c8466860SMark Santcroos 
58653289f6aSNate Lawson     /* OEMID */
58753289f6aSNate Lawson 
58853289f6aSNate Lawson     Child = Child->Asl.Next;
589278f0de6SJung-uk Kim     memcpy (AslGbl_TableHeader.OemId, Child->Asl.Value.String,
590278f0de6SJung-uk Kim         strlen (Child->Asl.Value.String));
59153289f6aSNate Lawson 
59253289f6aSNate Lawson     /* OEM TableID */
59353289f6aSNate Lawson 
59453289f6aSNate Lawson     Child = Child->Asl.Next;
595278f0de6SJung-uk Kim     memcpy (AslGbl_TableHeader.OemTableId, Child->Asl.Value.String,
596278f0de6SJung-uk Kim         strlen (Child->Asl.Value.String));
59753289f6aSNate Lawson 
59853289f6aSNate Lawson     /* OEM Revision */
59953289f6aSNate Lawson 
60053289f6aSNate Lawson     Child = Child->Asl.Next;
6016f1f1a63SJung-uk Kim     AslGbl_TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
60253289f6aSNate Lawson 
60353289f6aSNate Lawson     /* Compiler ID */
60453289f6aSNate Lawson 
605278f0de6SJung-uk Kim     ACPI_COPY_NAMESEG (AslGbl_TableHeader.AslCompilerId, ASL_CREATOR_ID);
60653289f6aSNate Lawson 
60753289f6aSNate Lawson     /* Compiler version */
60853289f6aSNate Lawson 
6096f1f1a63SJung-uk Kim     AslGbl_TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
61053289f6aSNate Lawson 
61153289f6aSNate Lawson     /* Table length. Checksum zero for now, will rewrite later */
61253289f6aSNate Lawson 
6136f1f1a63SJung-uk Kim     AslGbl_TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
614f8146b88SJung-uk Kim         Op->Asl.AmlSubtreeLength;
6150d84335fSJung-uk Kim 
6160d84335fSJung-uk Kim     /* Calculate the comment lengths for this definition block parseOp */
6170d84335fSJung-uk Kim 
618f1db5ef7SJung-uk Kim     if (AcpiGbl_CaptureComments)
6190d84335fSJung-uk Kim     {
6200d84335fSJung-uk Kim         CvDbgPrint ("Calculating comment lengths for %s in write header\n",
6210d84335fSJung-uk Kim             Op->Asl.ParseOpName);
6220d84335fSJung-uk Kim 
6230d84335fSJung-uk Kim         /*
6240d84335fSJung-uk Kim          * Take the filename without extensions, add 3 for the new extension
6250d84335fSJung-uk Kim          * and another 3 for the a908 bytecode and null terminator.
6260d84335fSJung-uk Kim          */
6276f1f1a63SJung-uk Kim         AslGbl_TableHeader.Length += strrchr (AslGbl_ParseTreeRoot->Asl.Filename, '.')
6286f1f1a63SJung-uk Kim             - AslGbl_ParseTreeRoot->Asl.Filename + 1 + 3 + 3;
6296f1f1a63SJung-uk Kim 
6300d84335fSJung-uk Kim         Op->Asl.AmlSubtreeLength +=
6316f1f1a63SJung-uk Kim             strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3;
6326f1f1a63SJung-uk Kim 
633a009b7dcSJung-uk Kim         CvDbgPrint ("     Length: %u\n",
634a009b7dcSJung-uk Kim             (UINT32) strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3);
6350d84335fSJung-uk Kim 
6360d84335fSJung-uk Kim         if (Op->Asl.CommentList)
6370d84335fSJung-uk Kim         {
6380d84335fSJung-uk Kim             Current = Op->Asl.CommentList;
6390d84335fSJung-uk Kim             while (Current)
6400d84335fSJung-uk Kim             {
6410d84335fSJung-uk Kim                 CommentLength = strlen (Current->Comment)+3;
6420d84335fSJung-uk Kim                 CvDbgPrint ("Length of standard comment): %d\n", CommentLength);
6430d84335fSJung-uk Kim                 CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
6446f1f1a63SJung-uk Kim                 AslGbl_TableHeader.Length += CommentLength;
6450d84335fSJung-uk Kim                 Op->Asl.AmlSubtreeLength += CommentLength;
6460d84335fSJung-uk Kim                 Current = Current->Next;
6470d84335fSJung-uk Kim                 CvDbgPrint ("    Length: %u\n", CommentLength);
6480d84335fSJung-uk Kim             }
6490d84335fSJung-uk Kim         }
6500d84335fSJung-uk Kim         if (Op->Asl.CloseBraceComment)
6510d84335fSJung-uk Kim         {
6520d84335fSJung-uk Kim             CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
6530d84335fSJung-uk Kim             CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength);
6540d84335fSJung-uk Kim             CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
6556f1f1a63SJung-uk Kim             AslGbl_TableHeader.Length += CommentLength;
6560d84335fSJung-uk Kim             Op->Asl.AmlSubtreeLength += CommentLength;
6570d84335fSJung-uk Kim             CvDbgPrint ("    Length: %u\n", CommentLength);
6580d84335fSJung-uk Kim         }
6590d84335fSJung-uk Kim     }
6600d84335fSJung-uk Kim 
6616f1f1a63SJung-uk Kim     AslGbl_TableHeader.Checksum = 0;
6626f1f1a63SJung-uk Kim     Op->Asl.FinalAmlOffset = ftell (AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle);
663f8146b88SJung-uk Kim 
664f8146b88SJung-uk Kim     /* Write entire header and clear the table header global */
665f8146b88SJung-uk Kim 
6666f1f1a63SJung-uk Kim     CgLocalWriteAmlData (Op, &AslGbl_TableHeader, sizeof (ACPI_TABLE_HEADER));
6676f1f1a63SJung-uk Kim     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
668f8146b88SJung-uk Kim }
669f8146b88SJung-uk Kim 
670f8146b88SJung-uk Kim 
671f8146b88SJung-uk Kim /*******************************************************************************
672f8146b88SJung-uk Kim  *
6739a4bc520SJung-uk Kim  * FUNCTION:    CgUpdateCdatHeader
6749a4bc520SJung-uk Kim  *
6759a4bc520SJung-uk Kim  * PARAMETERS:  Op                  - Op for the Definition Block
6769a4bc520SJung-uk Kim  *
6779a4bc520SJung-uk Kim  * RETURN:      None.
6789a4bc520SJung-uk Kim  *
6799a4bc520SJung-uk Kim  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
6809a4bc520SJung-uk Kim  *              re-writing the header for the input definition block
6819a4bc520SJung-uk Kim  *
6829a4bc520SJung-uk Kim  ******************************************************************************/
6839a4bc520SJung-uk Kim 
6849a4bc520SJung-uk Kim static void
CgUpdateCdatHeader(ACPI_PARSE_OBJECT * Op)6859a4bc520SJung-uk Kim CgUpdateCdatHeader (
6869a4bc520SJung-uk Kim     ACPI_PARSE_OBJECT       *Op)
6879a4bc520SJung-uk Kim {
6889a4bc520SJung-uk Kim     signed char             Sum;
6899a4bc520SJung-uk Kim     UINT32                  i;
6909a4bc520SJung-uk Kim     UINT32                  Length;
6919a4bc520SJung-uk Kim     UINT8                   FileByte;
6929a4bc520SJung-uk Kim     UINT8                   Checksum;
6939a4bc520SJung-uk Kim 
6949a4bc520SJung-uk Kim 
6959a4bc520SJung-uk Kim     /* Calculate the checksum over the entire definition block */
6969a4bc520SJung-uk Kim 
6979a4bc520SJung-uk Kim     Sum = 0;
6989a4bc520SJung-uk Kim     Length = sizeof (ACPI_TABLE_CDAT) + Op->Asl.AmlSubtreeLength;
6999a4bc520SJung-uk Kim     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
7009a4bc520SJung-uk Kim 
7019a4bc520SJung-uk Kim     for (i = 0; i < Length; i++)
7029a4bc520SJung-uk Kim     {
7039a4bc520SJung-uk Kim         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
7049a4bc520SJung-uk Kim         {
7059a4bc520SJung-uk Kim             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
7069a4bc520SJung-uk Kim                 "Table length is greater than size of the input file");
7079a4bc520SJung-uk Kim             return;
7089a4bc520SJung-uk Kim         }
7099a4bc520SJung-uk Kim 
7109a4bc520SJung-uk Kim         Sum = (signed char) (Sum + FileByte);
7119a4bc520SJung-uk Kim     }
7129a4bc520SJung-uk Kim 
7139a4bc520SJung-uk Kim     Checksum = (UINT8) (0 - Sum);
7149a4bc520SJung-uk Kim 
7159a4bc520SJung-uk Kim     DbgPrint (ASL_DEBUG_OUTPUT, "Computed checksum = %X\n", Checksum);
7169a4bc520SJung-uk Kim 
7179a4bc520SJung-uk Kim     /* Re-write the checksum byte */
7189a4bc520SJung-uk Kim 
7199a4bc520SJung-uk Kim     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
7209a4bc520SJung-uk Kim         ACPI_CDAT_OFFSET (Checksum));
7219a4bc520SJung-uk Kim 
7229a4bc520SJung-uk Kim     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
7239a4bc520SJung-uk Kim 
7249a4bc520SJung-uk Kim     /*
7259a4bc520SJung-uk Kim      * Seek to the end of the file. This is done to support multiple file
7269a4bc520SJung-uk Kim      * compilation. Doing this simplifies other parts of the codebase because
7279a4bc520SJung-uk Kim      * it eliminates the need to seek for a different starting place.
7289a4bc520SJung-uk Kim      */
7299a4bc520SJung-uk Kim     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset + Length);
7309a4bc520SJung-uk Kim }
7319a4bc520SJung-uk Kim 
7329a4bc520SJung-uk Kim /*******************************************************************************
7339a4bc520SJung-uk Kim  *
734f8146b88SJung-uk Kim  * FUNCTION:    CgUpdateHeader
735f8146b88SJung-uk Kim  *
736f8146b88SJung-uk Kim  * PARAMETERS:  Op                  - Op for the Definition Block
737f8146b88SJung-uk Kim  *
738f8146b88SJung-uk Kim  * RETURN:      None.
739f8146b88SJung-uk Kim  *
740f8146b88SJung-uk Kim  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
741f8146b88SJung-uk Kim  *              re-writing the header for the input definition block
742f8146b88SJung-uk Kim  *
743f8146b88SJung-uk Kim  ******************************************************************************/
744f8146b88SJung-uk Kim 
745f8146b88SJung-uk Kim static void
CgUpdateHeader(ACPI_PARSE_OBJECT * Op)746f8146b88SJung-uk Kim CgUpdateHeader (
747f8146b88SJung-uk Kim     ACPI_PARSE_OBJECT       *Op)
748f8146b88SJung-uk Kim {
749f8146b88SJung-uk Kim     signed char             Sum;
750f8146b88SJung-uk Kim     UINT32                  i;
751f8146b88SJung-uk Kim     UINT32                  Length;
752f8146b88SJung-uk Kim     UINT8                   FileByte;
753f8146b88SJung-uk Kim     UINT8                   Checksum;
754f8146b88SJung-uk Kim 
755f8146b88SJung-uk Kim 
756f8146b88SJung-uk Kim     /* Calculate the checksum over the entire definition block */
757f8146b88SJung-uk Kim 
758f8146b88SJung-uk Kim     Sum = 0;
759f8146b88SJung-uk Kim     Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
760f8146b88SJung-uk Kim     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
761f8146b88SJung-uk Kim 
762f8146b88SJung-uk Kim     for (i = 0; i < Length; i++)
763f8146b88SJung-uk Kim     {
764f8146b88SJung-uk Kim         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
765f8146b88SJung-uk Kim         {
7669ad8b64eSJung-uk Kim             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
7679ad8b64eSJung-uk Kim                 "Table length is greater than size of the input file");
768f8146b88SJung-uk Kim             return;
769f8146b88SJung-uk Kim         }
770f8146b88SJung-uk Kim 
771f8146b88SJung-uk Kim         Sum = (signed char) (Sum + FileByte);
772f8146b88SJung-uk Kim     }
773f8146b88SJung-uk Kim 
774f8146b88SJung-uk Kim     Checksum = (UINT8) (0 - Sum);
775f8146b88SJung-uk Kim 
776fbde3477SJung-uk Kim     /* Re-write the checksum byte */
777f8146b88SJung-uk Kim 
778f8146b88SJung-uk Kim     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
779f8146b88SJung-uk Kim         ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
780f8146b88SJung-uk Kim 
781f8146b88SJung-uk Kim     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
78253289f6aSNate Lawson 
78344b0f624SJung-uk Kim     /*
78444b0f624SJung-uk Kim      * Seek to the end of the file. This is done to support multiple file
78544b0f624SJung-uk Kim      * compilation. Doing this simplifies other parts of the codebase because
78644b0f624SJung-uk Kim      * it eliminates the need to seek for a different starting place.
78744b0f624SJung-uk Kim      */
78844b0f624SJung-uk Kim     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset + Length);
78953289f6aSNate Lawson }
79053289f6aSNate Lawson 
79153289f6aSNate Lawson 
79253289f6aSNate Lawson /*******************************************************************************
79353289f6aSNate Lawson  *
79453289f6aSNate Lawson  * FUNCTION:    CgWriteNode
79553289f6aSNate Lawson  *
79653289f6aSNate Lawson  * PARAMETERS:  Op            - Parse node to write.
79753289f6aSNate Lawson  *
79853289f6aSNate Lawson  * RETURN:      None.
79953289f6aSNate Lawson  *
80053289f6aSNate Lawson  * DESCRIPTION: Write the AML that corresponds to a parse node.
80153289f6aSNate Lawson  *
80253289f6aSNate Lawson  ******************************************************************************/
80353289f6aSNate Lawson 
804fba7fc7eSJung-uk Kim static void
CgWriteNode(ACPI_PARSE_OBJECT * Op)80553289f6aSNate Lawson CgWriteNode (
80653289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op)
80753289f6aSNate Lawson {
80853289f6aSNate Lawson     ASL_RESOURCE_NODE       *Rnode;
80953289f6aSNate Lawson 
81053289f6aSNate Lawson 
8110d84335fSJung-uk Kim     /* Write all comments here. */
812f1db5ef7SJung-uk Kim 
813f1db5ef7SJung-uk Kim     if (AcpiGbl_CaptureComments)
8140d84335fSJung-uk Kim     {
8150d84335fSJung-uk Kim         CgWriteAmlComment(Op);
8160d84335fSJung-uk Kim     }
8170d84335fSJung-uk Kim 
81853289f6aSNate Lawson     /* Always check for DEFAULT_ARG and other "Noop" nodes */
81953289f6aSNate Lawson     /* TBD: this may not be the best place for this check */
82053289f6aSNate Lawson 
82153289f6aSNate Lawson     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
82253289f6aSNate Lawson         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
82353289f6aSNate Lawson         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
82453289f6aSNate Lawson     {
82553289f6aSNate Lawson         return;
82653289f6aSNate Lawson     }
82753289f6aSNate Lawson 
8281a39cfb0SJung-uk Kim     Op->Asl.FinalAmlLength = 0;
8291a39cfb0SJung-uk Kim 
83053289f6aSNate Lawson     switch (Op->Asl.AmlOpcode)
83153289f6aSNate Lawson     {
83253289f6aSNate Lawson     case AML_RAW_DATA_BYTE:
83353289f6aSNate Lawson     case AML_RAW_DATA_WORD:
83453289f6aSNate Lawson     case AML_RAW_DATA_DWORD:
83553289f6aSNate Lawson     case AML_RAW_DATA_QWORD:
83653289f6aSNate Lawson 
83753289f6aSNate Lawson         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
83853289f6aSNate Lawson         return;
83953289f6aSNate Lawson 
84053289f6aSNate Lawson 
84153289f6aSNate Lawson     case AML_RAW_DATA_BUFFER:
84253289f6aSNate Lawson 
84353289f6aSNate Lawson         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
84453289f6aSNate Lawson         return;
84553289f6aSNate Lawson 
84653289f6aSNate Lawson 
84753289f6aSNate Lawson     case AML_RAW_DATA_CHAIN:
84853289f6aSNate Lawson 
84953289f6aSNate Lawson         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
85053289f6aSNate Lawson         while (Rnode)
85153289f6aSNate Lawson         {
85253289f6aSNate Lawson             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
85353289f6aSNate Lawson             Rnode = Rnode->Next;
85453289f6aSNate Lawson         }
85553289f6aSNate Lawson         return;
85653289f6aSNate Lawson 
85753289f6aSNate Lawson     default:
858a9d8d09cSJung-uk Kim 
85953289f6aSNate Lawson         /* Internal data opcodes must all appear above */
860a9d8d09cSJung-uk Kim 
86153289f6aSNate Lawson         break;
86253289f6aSNate Lawson     }
86353289f6aSNate Lawson 
86453289f6aSNate Lawson     switch (Op->Asl.ParseOpcode)
86553289f6aSNate Lawson     {
86653289f6aSNate Lawson     case PARSEOP_DEFAULT_ARG:
86753289f6aSNate Lawson 
86853289f6aSNate Lawson         break;
86953289f6aSNate Lawson 
870f8146b88SJung-uk Kim     case PARSEOP_DEFINITION_BLOCK:
87153289f6aSNate Lawson 
87253289f6aSNate Lawson         CgWriteTableHeader (Op);
873f1db5ef7SJung-uk Kim         if (AcpiGbl_CaptureComments)
8740d84335fSJung-uk Kim         {
8750d84335fSJung-uk Kim             CgWriteAmlDefBlockComment (Op);
8760d84335fSJung-uk Kim         }
87753289f6aSNate Lawson         break;
87853289f6aSNate Lawson 
87953289f6aSNate Lawson     case PARSEOP_NAMESEG:
88053289f6aSNate Lawson     case PARSEOP_NAMESTRING:
88153289f6aSNate Lawson     case PARSEOP_METHODCALL:
88253289f6aSNate Lawson 
88353289f6aSNate Lawson         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
88453289f6aSNate Lawson         break;
88553289f6aSNate Lawson 
88653289f6aSNate Lawson     default:
88753289f6aSNate Lawson 
88853289f6aSNate Lawson         CgWriteAmlOpcode (Op);
88953289f6aSNate Lawson         break;
89053289f6aSNate Lawson     }
89153289f6aSNate Lawson }
892