xref: /freebsd/sys/contrib/dev/acpica/compiler/aslcodegen.c (revision 1c0e1b6da9c3c45f81c75137dddaebc748995afc)
153289f6aSNate Lawson /******************************************************************************
253289f6aSNate Lawson  *
353289f6aSNate Lawson  * Module Name: aslcodegen - AML code generation
453289f6aSNate Lawson  *
553289f6aSNate Lawson  *****************************************************************************/
653289f6aSNate Lawson 
7d244b227SJung-uk Kim /*
8*1c0e1b6dSJung-uk Kim  * Copyright (C) 2000 - 2015, Intel Corp.
953289f6aSNate Lawson  * All rights reserved.
1053289f6aSNate Lawson  *
11d244b227SJung-uk Kim  * Redistribution and use in source and binary forms, with or without
12d244b227SJung-uk Kim  * modification, are permitted provided that the following conditions
13d244b227SJung-uk Kim  * are met:
14d244b227SJung-uk Kim  * 1. Redistributions of source code must retain the above copyright
15d244b227SJung-uk Kim  *    notice, this list of conditions, and the following disclaimer,
16d244b227SJung-uk Kim  *    without modification.
17d244b227SJung-uk Kim  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18d244b227SJung-uk Kim  *    substantially similar to the "NO WARRANTY" disclaimer below
19d244b227SJung-uk Kim  *    ("Disclaimer") and any redistribution must be conditioned upon
20d244b227SJung-uk Kim  *    including a substantially similar Disclaimer requirement for further
21d244b227SJung-uk Kim  *    binary redistribution.
22d244b227SJung-uk Kim  * 3. Neither the names of the above-listed copyright holders nor the names
23d244b227SJung-uk Kim  *    of any contributors may be used to endorse or promote products derived
24d244b227SJung-uk Kim  *    from this software without specific prior written permission.
2553289f6aSNate Lawson  *
26d244b227SJung-uk Kim  * Alternatively, this software may be distributed under the terms of the
27d244b227SJung-uk Kim  * GNU General Public License ("GPL") version 2 as published by the Free
28d244b227SJung-uk Kim  * Software Foundation.
2953289f6aSNate Lawson  *
30d244b227SJung-uk Kim  * NO WARRANTY
31d244b227SJung-uk Kim  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32d244b227SJung-uk Kim  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33d244b227SJung-uk Kim  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34d244b227SJung-uk Kim  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35d244b227SJung-uk Kim  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36d244b227SJung-uk Kim  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37d244b227SJung-uk Kim  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38d244b227SJung-uk Kim  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39d244b227SJung-uk Kim  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40d244b227SJung-uk Kim  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41d244b227SJung-uk Kim  * POSSIBILITY OF SUCH DAMAGES.
42d244b227SJung-uk Kim  */
4353289f6aSNate Lawson 
44ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/compiler/aslcompiler.h>
4553289f6aSNate Lawson #include "aslcompiler.y.h"
46ab6f3bf9SJung-uk Kim #include <contrib/dev/acpica/include/amlcode.h>
4753289f6aSNate Lawson 
4853289f6aSNate Lawson #define _COMPONENT          ACPI_COMPILER
4953289f6aSNate Lawson         ACPI_MODULE_NAME    ("aslcodegen")
5053289f6aSNate Lawson 
51fba7fc7eSJung-uk Kim /* Local prototypes */
52fba7fc7eSJung-uk Kim 
53fba7fc7eSJung-uk Kim static ACPI_STATUS
54fba7fc7eSJung-uk Kim CgAmlWriteWalk (
55fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
56fba7fc7eSJung-uk Kim     UINT32                  Level,
57fba7fc7eSJung-uk Kim     void                    *Context);
58fba7fc7eSJung-uk Kim 
59fba7fc7eSJung-uk Kim static void
60fba7fc7eSJung-uk Kim CgLocalWriteAmlData (
61fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op,
62fba7fc7eSJung-uk Kim     void                    *Buffer,
63fba7fc7eSJung-uk Kim     UINT32                  Length);
64fba7fc7eSJung-uk Kim 
65fba7fc7eSJung-uk Kim static void
66fba7fc7eSJung-uk Kim CgWriteAmlOpcode (
67fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
68fba7fc7eSJung-uk Kim 
69fba7fc7eSJung-uk Kim static void
70fba7fc7eSJung-uk Kim CgWriteTableHeader (
71fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
72fba7fc7eSJung-uk Kim 
73fba7fc7eSJung-uk Kim static void
74fba7fc7eSJung-uk Kim CgCloseTable (
75fba7fc7eSJung-uk Kim     void);
76fba7fc7eSJung-uk Kim 
77fba7fc7eSJung-uk Kim static void
78fba7fc7eSJung-uk Kim CgWriteNode (
79fba7fc7eSJung-uk Kim     ACPI_PARSE_OBJECT       *Op);
80fba7fc7eSJung-uk Kim 
8153289f6aSNate Lawson 
8253289f6aSNate Lawson /*******************************************************************************
8353289f6aSNate Lawson  *
8453289f6aSNate Lawson  * FUNCTION:    CgGenerateAmlOutput
8553289f6aSNate Lawson  *
8653289f6aSNate Lawson  * PARAMETERS:  None.
8753289f6aSNate Lawson  *
8853289f6aSNate Lawson  * RETURN:      None
8953289f6aSNate Lawson  *
9053289f6aSNate Lawson  * DESCRIPTION: Generate AML code. Currently generates the listing file
9153289f6aSNate Lawson  *              simultaneously.
9253289f6aSNate Lawson  *
9353289f6aSNate Lawson  ******************************************************************************/
9453289f6aSNate Lawson 
9553289f6aSNate Lawson void
96fba7fc7eSJung-uk Kim CgGenerateAmlOutput (
97fba7fc7eSJung-uk Kim     void)
9853289f6aSNate Lawson {
9953289f6aSNate Lawson 
10053289f6aSNate Lawson     DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
10153289f6aSNate Lawson 
10253289f6aSNate Lawson     /* Generate the AML output file */
10353289f6aSNate Lawson 
10453289f6aSNate Lawson     FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
10553289f6aSNate Lawson     Gbl_SourceLine = 0;
10653289f6aSNate Lawson     Gbl_NextError = Gbl_ErrorLog;
10753289f6aSNate Lawson 
108fba7fc7eSJung-uk Kim     TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
109fba7fc7eSJung-uk Kim         CgAmlWriteWalk, NULL, NULL);
110*1c0e1b6dSJung-uk Kim 
111*1c0e1b6dSJung-uk Kim     DbgPrint (ASL_TREE_OUTPUT,
112*1c0e1b6dSJung-uk Kim         "%*s Value    P_Op A_Op OpLen PByts Len  SubLen PSubLen OpPtr"
113*1c0e1b6dSJung-uk Kim         "    Parent   Child    Next     Flags    AcTyp    Final Col L\n",
114*1c0e1b6dSJung-uk Kim         76, " ");
115*1c0e1b6dSJung-uk Kim 
11653289f6aSNate Lawson     CgCloseTable ();
11753289f6aSNate Lawson }
11853289f6aSNate Lawson 
11953289f6aSNate Lawson 
12053289f6aSNate Lawson /*******************************************************************************
12153289f6aSNate Lawson  *
12253289f6aSNate Lawson  * FUNCTION:    CgAmlWriteWalk
12353289f6aSNate Lawson  *
12453289f6aSNate Lawson  * PARAMETERS:  ASL_WALK_CALLBACK
12553289f6aSNate Lawson  *
12653289f6aSNate Lawson  * RETURN:      Status
12753289f6aSNate Lawson  *
12853289f6aSNate Lawson  * DESCRIPTION: Parse tree walk to generate the AML code.
12953289f6aSNate Lawson  *
13053289f6aSNate Lawson  ******************************************************************************/
13153289f6aSNate Lawson 
132fba7fc7eSJung-uk Kim static ACPI_STATUS
13353289f6aSNate Lawson CgAmlWriteWalk (
13453289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
13553289f6aSNate Lawson     UINT32                  Level,
13653289f6aSNate Lawson     void                    *Context)
13753289f6aSNate Lawson {
13853289f6aSNate Lawson 
1391a39cfb0SJung-uk Kim     /*
1401a39cfb0SJung-uk Kim      * Print header at level 0. Alignment assumes 32-bit pointers
1411a39cfb0SJung-uk Kim      */
1421a39cfb0SJung-uk Kim     if (!Level)
1431a39cfb0SJung-uk Kim     {
1441a39cfb0SJung-uk Kim         DbgPrint (ASL_TREE_OUTPUT,
1451a39cfb0SJung-uk Kim             "Final parse tree used for AML output:\n");
1461a39cfb0SJung-uk Kim         DbgPrint (ASL_TREE_OUTPUT,
147*1c0e1b6dSJung-uk Kim             "%*s Value    P_Op A_Op OpLen PByts Len  SubLen PSubLen OpPtr"
148*1c0e1b6dSJung-uk Kim             "    Parent   Child    Next     Flags    AcTyp    Final Col L\n",
1491a39cfb0SJung-uk Kim             76, " ");
1501a39cfb0SJung-uk Kim     }
1511a39cfb0SJung-uk Kim 
152fba7fc7eSJung-uk Kim     /* Debug output */
153fba7fc7eSJung-uk Kim 
15453289f6aSNate Lawson     DbgPrint (ASL_TREE_OUTPUT,
15553289f6aSNate Lawson         "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
15653289f6aSNate Lawson     UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
15753289f6aSNate Lawson 
15853289f6aSNate Lawson     if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG    ||
15953289f6aSNate Lawson         Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
16053289f6aSNate Lawson         Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
16153289f6aSNate Lawson     {
16253289f6aSNate Lawson         DbgPrint (ASL_TREE_OUTPUT,
16353289f6aSNate Lawson             "%10.32s      ", Op->Asl.ExternalName);
16453289f6aSNate Lawson     }
16553289f6aSNate Lawson     else
16653289f6aSNate Lawson     {
16753289f6aSNate Lawson         DbgPrint (ASL_TREE_OUTPUT, "                ");
16853289f6aSNate Lawson     }
16953289f6aSNate Lawson 
17053289f6aSNate Lawson     DbgPrint (ASL_TREE_OUTPUT,
171*1c0e1b6dSJung-uk Kim     "%08X %04X %04X %01X     %04X  %04X %04X   %04X    "
172*1c0e1b6dSJung-uk Kim     "%08X %08X %08X %08X %08X %08X %04X  %02d  %02d\n",
1731a39cfb0SJung-uk Kim             /* 1  */ (UINT32) Op->Asl.Value.Integer,
1741a39cfb0SJung-uk Kim             /* 2  */ Op->Asl.ParseOpcode,
1751a39cfb0SJung-uk Kim             /* 3  */ Op->Asl.AmlOpcode,
1761a39cfb0SJung-uk Kim             /* 4  */ Op->Asl.AmlOpcodeLength,
1771a39cfb0SJung-uk Kim             /* 5  */ Op->Asl.AmlPkgLenBytes,
1781a39cfb0SJung-uk Kim             /* 6  */ Op->Asl.AmlLength,
1791a39cfb0SJung-uk Kim             /* 7  */ Op->Asl.AmlSubtreeLength,
1801a39cfb0SJung-uk Kim             /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
1811a39cfb0SJung-uk Kim             /* 9  */ Op,
182*1c0e1b6dSJung-uk Kim             /* 10 */ Op->Asl.Parent,
183*1c0e1b6dSJung-uk Kim             /* 11 */ Op->Asl.Child,
184*1c0e1b6dSJung-uk Kim             /* 12 */ Op->Asl.Next,
185*1c0e1b6dSJung-uk Kim             /* 13 */ Op->Asl.CompileFlags,
186*1c0e1b6dSJung-uk Kim             /* 14 */ Op->Asl.AcpiBtype,
187*1c0e1b6dSJung-uk Kim             /* 15 */ Op->Asl.FinalAmlLength,
188*1c0e1b6dSJung-uk Kim             /* 16 */ Op->Asl.Column,
189*1c0e1b6dSJung-uk Kim             /* 17 */ Op->Asl.LineNumber);
19053289f6aSNate Lawson 
191fba7fc7eSJung-uk Kim     /* Generate the AML for this node */
192fba7fc7eSJung-uk Kim 
19353289f6aSNate Lawson     CgWriteNode (Op);
19453289f6aSNate Lawson     return (AE_OK);
19553289f6aSNate Lawson }
19653289f6aSNate Lawson 
19753289f6aSNate Lawson 
19853289f6aSNate Lawson /*******************************************************************************
19953289f6aSNate Lawson  *
20053289f6aSNate Lawson  * FUNCTION:    CgLocalWriteAmlData
20153289f6aSNate Lawson  *
202fba7fc7eSJung-uk Kim  * PARAMETERS:  Op              - Current parse op
203fba7fc7eSJung-uk Kim  *              Buffer          - Buffer to write
20453289f6aSNate Lawson  *              Length          - Size of data in buffer
20553289f6aSNate Lawson  *
20653289f6aSNate Lawson  * RETURN:      None
20753289f6aSNate Lawson  *
20853289f6aSNate Lawson  * DESCRIPTION: Write a buffer of AML data to the AML output file.
20953289f6aSNate Lawson  *
21053289f6aSNate Lawson  ******************************************************************************/
21153289f6aSNate Lawson 
212fba7fc7eSJung-uk Kim static void
21353289f6aSNate Lawson CgLocalWriteAmlData (
21453289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op,
21553289f6aSNate Lawson     void                    *Buffer,
21653289f6aSNate Lawson     UINT32                  Length)
21753289f6aSNate Lawson {
21853289f6aSNate Lawson 
21953289f6aSNate Lawson     /* Write the raw data to the AML file */
22053289f6aSNate Lawson 
22153289f6aSNate Lawson     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
22253289f6aSNate Lawson 
22353289f6aSNate Lawson     /* Update the final AML length for this node (used for listings) */
22453289f6aSNate Lawson 
22553289f6aSNate Lawson     if (Op)
22653289f6aSNate Lawson     {
22753289f6aSNate Lawson         Op->Asl.FinalAmlLength += Length;
22853289f6aSNate Lawson     }
22953289f6aSNate Lawson }
23053289f6aSNate Lawson 
23153289f6aSNate Lawson 
23253289f6aSNate Lawson /*******************************************************************************
23353289f6aSNate Lawson  *
23453289f6aSNate Lawson  * FUNCTION:    CgWriteAmlOpcode
23553289f6aSNate Lawson  *
23653289f6aSNate Lawson  * PARAMETERS:  Op            - Parse node with an AML opcode
23753289f6aSNate Lawson  *
23853289f6aSNate Lawson  * RETURN:      None.
23953289f6aSNate Lawson  *
24053289f6aSNate Lawson  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
24153289f6aSNate Lawson  *
24253289f6aSNate Lawson  ******************************************************************************/
24353289f6aSNate Lawson 
244fba7fc7eSJung-uk Kim static void
24553289f6aSNate Lawson CgWriteAmlOpcode (
24653289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op)
24753289f6aSNate Lawson {
248fba7fc7eSJung-uk Kim     UINT8                   PkgLenFirstByte;
249fba7fc7eSJung-uk Kim     UINT32                  i;
25053289f6aSNate Lawson     union {
25153289f6aSNate Lawson         UINT16                  Opcode;
25253289f6aSNate Lawson         UINT8                   OpcodeBytes[2];
25353289f6aSNate Lawson     } Aml;
25453289f6aSNate Lawson     union {
25553289f6aSNate Lawson         UINT32                  Len;
25653289f6aSNate Lawson         UINT8                   LenBytes[4];
25753289f6aSNate Lawson     } PkgLen;
25853289f6aSNate Lawson 
25953289f6aSNate Lawson 
26053289f6aSNate Lawson     /* We expect some DEFAULT_ARGs, just ignore them */
26153289f6aSNate Lawson 
26253289f6aSNate Lawson     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
26353289f6aSNate Lawson     {
26453289f6aSNate Lawson         return;
26553289f6aSNate Lawson     }
26653289f6aSNate Lawson 
26753289f6aSNate Lawson     switch (Op->Asl.AmlOpcode)
26853289f6aSNate Lawson     {
26953289f6aSNate Lawson     case AML_UNASSIGNED_OPCODE:
27053289f6aSNate Lawson 
27153289f6aSNate Lawson         /* These opcodes should not get here */
27253289f6aSNate Lawson 
27353289f6aSNate Lawson         printf ("Found a node with an unassigned AML opcode\n");
2744c52cad2SJung-uk Kim         FlPrintFile (ASL_FILE_STDERR, "Found a node with an unassigned AML opcode\n");
27553289f6aSNate Lawson         return;
27653289f6aSNate Lawson 
27753289f6aSNate Lawson     case AML_INT_RESERVEDFIELD_OP:
27853289f6aSNate Lawson 
27953289f6aSNate Lawson         /* Special opcodes for within a field definition */
28053289f6aSNate Lawson 
2813f0275a0SJung-uk Kim         Aml.Opcode = AML_FIELD_OFFSET_OP;
28253289f6aSNate Lawson         break;
28353289f6aSNate Lawson 
28453289f6aSNate Lawson     case AML_INT_ACCESSFIELD_OP:
28553289f6aSNate Lawson 
2863f0275a0SJung-uk Kim         Aml.Opcode = AML_FIELD_ACCESS_OP;
2873f0275a0SJung-uk Kim         break;
2883f0275a0SJung-uk Kim 
2893f0275a0SJung-uk Kim     case AML_INT_CONNECTION_OP:
2903f0275a0SJung-uk Kim 
2913f0275a0SJung-uk Kim         Aml.Opcode = AML_FIELD_CONNECTION_OP;
29253289f6aSNate Lawson         break;
29353289f6aSNate Lawson 
29453289f6aSNate Lawson     default:
295a9d8d09cSJung-uk Kim 
29653289f6aSNate Lawson         Aml.Opcode = Op->Asl.AmlOpcode;
29753289f6aSNate Lawson         break;
29853289f6aSNate Lawson     }
29953289f6aSNate Lawson 
30053289f6aSNate Lawson 
30153289f6aSNate Lawson     switch (Aml.Opcode)
30253289f6aSNate Lawson     {
30353289f6aSNate Lawson     case AML_PACKAGE_LENGTH:
30453289f6aSNate Lawson 
30553289f6aSNate Lawson         /* Value is the length to be encoded (Used in field definitions) */
30653289f6aSNate Lawson 
30753289f6aSNate Lawson         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
30853289f6aSNate Lawson         break;
30953289f6aSNate Lawson 
31053289f6aSNate Lawson     default:
31153289f6aSNate Lawson 
31253289f6aSNate Lawson         /* Check for two-byte opcode */
31353289f6aSNate Lawson 
31453289f6aSNate Lawson         if (Aml.Opcode > 0x00FF)
31553289f6aSNate Lawson         {
31653289f6aSNate Lawson             /* Write the high byte first */
31753289f6aSNate Lawson 
31853289f6aSNate Lawson             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
31953289f6aSNate Lawson         }
32053289f6aSNate Lawson 
32153289f6aSNate Lawson         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
32253289f6aSNate Lawson 
32353289f6aSNate Lawson         /* Subtreelength doesn't include length of package length bytes */
32453289f6aSNate Lawson 
32553289f6aSNate Lawson         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
32653289f6aSNate Lawson         break;
32753289f6aSNate Lawson     }
32853289f6aSNate Lawson 
32953289f6aSNate Lawson     /* Does this opcode have an associated "PackageLength" field? */
33053289f6aSNate Lawson 
33153289f6aSNate Lawson     if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
33253289f6aSNate Lawson     {
33353289f6aSNate Lawson         if (Op->Asl.AmlPkgLenBytes == 1)
33453289f6aSNate Lawson         {
33553289f6aSNate Lawson             /* Simplest case -- no bytes to follow, just write the count */
33653289f6aSNate Lawson 
33753289f6aSNate Lawson             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
33853289f6aSNate Lawson         }
3391a39cfb0SJung-uk Kim         else if (Op->Asl.AmlPkgLenBytes != 0)
34053289f6aSNate Lawson         {
34153289f6aSNate Lawson             /*
34253289f6aSNate Lawson              * Encode the "bytes to follow" in the first byte, top two bits.
34353289f6aSNate Lawson              * The low-order nybble of the length is in the bottom 4 bits
34453289f6aSNate Lawson              */
345fba7fc7eSJung-uk Kim             PkgLenFirstByte = (UINT8)
346fba7fc7eSJung-uk Kim                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
34753289f6aSNate Lawson                 (PkgLen.LenBytes[0] & 0x0F));
34853289f6aSNate Lawson 
34953289f6aSNate Lawson             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
35053289f6aSNate Lawson 
351fba7fc7eSJung-uk Kim             /*
352fba7fc7eSJung-uk Kim              * Shift the length over by the 4 bits we just stuffed
353fba7fc7eSJung-uk Kim              * in the first byte
354fba7fc7eSJung-uk Kim              */
35553289f6aSNate Lawson             PkgLen.Len >>= 4;
35653289f6aSNate Lawson 
35753289f6aSNate Lawson             /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
35853289f6aSNate Lawson 
35953289f6aSNate Lawson             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
36053289f6aSNate Lawson             {
36153289f6aSNate Lawson                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
36253289f6aSNate Lawson             }
36353289f6aSNate Lawson         }
36453289f6aSNate Lawson     }
36553289f6aSNate Lawson 
36653289f6aSNate Lawson     switch (Aml.Opcode)
36753289f6aSNate Lawson     {
36853289f6aSNate Lawson     case AML_BYTE_OP:
36953289f6aSNate Lawson 
370fba7fc7eSJung-uk Kim         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
37153289f6aSNate Lawson         break;
37253289f6aSNate Lawson 
37353289f6aSNate Lawson     case AML_WORD_OP:
37453289f6aSNate Lawson 
375fba7fc7eSJung-uk Kim         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
37653289f6aSNate Lawson        break;
37753289f6aSNate Lawson 
37853289f6aSNate Lawson     case AML_DWORD_OP:
37953289f6aSNate Lawson 
380fba7fc7eSJung-uk Kim         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
38153289f6aSNate Lawson         break;
38253289f6aSNate Lawson 
38353289f6aSNate Lawson     case AML_QWORD_OP:
38453289f6aSNate Lawson 
38553289f6aSNate Lawson         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
38653289f6aSNate Lawson         break;
38753289f6aSNate Lawson 
38853289f6aSNate Lawson     case AML_STRING_OP:
38953289f6aSNate Lawson 
39053289f6aSNate Lawson         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
39153289f6aSNate Lawson         break;
39253289f6aSNate Lawson 
39353289f6aSNate Lawson     default:
394a9d8d09cSJung-uk Kim 
39553289f6aSNate Lawson         /* All data opcodes must appear above */
396a9d8d09cSJung-uk Kim 
39753289f6aSNate Lawson         break;
39853289f6aSNate Lawson     }
39953289f6aSNate Lawson }
40053289f6aSNate Lawson 
40153289f6aSNate Lawson 
40253289f6aSNate Lawson /*******************************************************************************
40353289f6aSNate Lawson  *
40453289f6aSNate Lawson  * FUNCTION:    CgWriteTableHeader
40553289f6aSNate Lawson  *
40653289f6aSNate Lawson  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
40753289f6aSNate Lawson  *
408fba7fc7eSJung-uk Kim  * RETURN:      None
40953289f6aSNate Lawson  *
41053289f6aSNate Lawson  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
41153289f6aSNate Lawson  *
41253289f6aSNate Lawson  ******************************************************************************/
41353289f6aSNate Lawson 
414fba7fc7eSJung-uk Kim static void
41553289f6aSNate Lawson CgWriteTableHeader (
41653289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op)
41753289f6aSNate Lawson {
41853289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Child;
41953289f6aSNate Lawson 
42053289f6aSNate Lawson 
42153289f6aSNate Lawson     /* AML filename */
42253289f6aSNate Lawson 
42353289f6aSNate Lawson     Child = Op->Asl.Child;
42453289f6aSNate Lawson 
42553289f6aSNate Lawson     /* Signature */
42653289f6aSNate Lawson 
42753289f6aSNate Lawson     Child = Child->Asl.Next;
42853289f6aSNate Lawson     strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
42953289f6aSNate Lawson 
43053289f6aSNate Lawson     /* Revision */
43153289f6aSNate Lawson 
43253289f6aSNate Lawson     Child = Child->Asl.Next;
43353289f6aSNate Lawson     TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
43453289f6aSNate Lawson 
435c8466860SMark Santcroos     /* Command-line Revision override */
436c8466860SMark Santcroos 
437c8466860SMark Santcroos     if (Gbl_RevisionOverride)
438c8466860SMark Santcroos     {
439c8466860SMark Santcroos         TableHeader.Revision = Gbl_RevisionOverride;
440c8466860SMark Santcroos     }
441c8466860SMark Santcroos 
44253289f6aSNate Lawson     /* OEMID */
44353289f6aSNate Lawson 
44453289f6aSNate Lawson     Child = Child->Asl.Next;
44553289f6aSNate Lawson     strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
44653289f6aSNate Lawson 
44753289f6aSNate Lawson     /* OEM TableID */
44853289f6aSNate Lawson 
44953289f6aSNate Lawson     Child = Child->Asl.Next;
45053289f6aSNate Lawson     strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
45153289f6aSNate Lawson 
45253289f6aSNate Lawson     /* OEM Revision */
45353289f6aSNate Lawson 
45453289f6aSNate Lawson     Child = Child->Asl.Next;
45553289f6aSNate Lawson     TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
45653289f6aSNate Lawson 
45753289f6aSNate Lawson     /* Compiler ID */
45853289f6aSNate Lawson 
4598ef1a331SJung-uk Kim     ACPI_MOVE_NAME (TableHeader.AslCompilerId, ASL_CREATOR_ID);
46053289f6aSNate Lawson 
46153289f6aSNate Lawson     /* Compiler version */
46253289f6aSNate Lawson 
4633c1812acSJung-uk Kim     TableHeader.AslCompilerRevision = ASL_REVISION;
46453289f6aSNate Lawson 
46553289f6aSNate Lawson     /* Table length. Checksum zero for now, will rewrite later */
46653289f6aSNate Lawson 
46753289f6aSNate Lawson     TableHeader.Length   = Gbl_TableLength;
46853289f6aSNate Lawson     TableHeader.Checksum = 0;
46953289f6aSNate Lawson 
47053289f6aSNate Lawson     CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
47153289f6aSNate Lawson }
47253289f6aSNate Lawson 
47353289f6aSNate Lawson 
47453289f6aSNate Lawson /*******************************************************************************
47553289f6aSNate Lawson  *
47653289f6aSNate Lawson  * FUNCTION:    CgCloseTable
47753289f6aSNate Lawson  *
47853289f6aSNate Lawson  * PARAMETERS:  None.
47953289f6aSNate Lawson  *
48053289f6aSNate Lawson  * RETURN:      None.
48153289f6aSNate Lawson  *
48253289f6aSNate Lawson  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
48353289f6aSNate Lawson  *              re-writing the header.
48453289f6aSNate Lawson  *
48553289f6aSNate Lawson  ******************************************************************************/
48653289f6aSNate Lawson 
487fba7fc7eSJung-uk Kim static void
488fba7fc7eSJung-uk Kim CgCloseTable (
489fba7fc7eSJung-uk Kim     void)
49053289f6aSNate Lawson {
49153289f6aSNate Lawson     signed char         Sum;
49253289f6aSNate Lawson     UINT8               FileByte;
49353289f6aSNate Lawson 
49453289f6aSNate Lawson 
49553289f6aSNate Lawson     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
49653289f6aSNate Lawson     Sum = 0;
49753289f6aSNate Lawson 
49853289f6aSNate Lawson     /* Calculate the checksum over the entire file */
49953289f6aSNate Lawson 
50053289f6aSNate Lawson     while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
50153289f6aSNate Lawson     {
50253289f6aSNate Lawson         Sum = (signed char) (Sum + FileByte);
50353289f6aSNate Lawson     }
50453289f6aSNate Lawson 
50553289f6aSNate Lawson     /* Re-write the table header with the checksum */
50653289f6aSNate Lawson 
50753289f6aSNate Lawson     TableHeader.Checksum = (UINT8) (0 - Sum);
50853289f6aSNate Lawson 
50953289f6aSNate Lawson     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
51053289f6aSNate Lawson     CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
51153289f6aSNate Lawson }
51253289f6aSNate Lawson 
51353289f6aSNate Lawson 
51453289f6aSNate Lawson /*******************************************************************************
51553289f6aSNate Lawson  *
51653289f6aSNate Lawson  * FUNCTION:    CgWriteNode
51753289f6aSNate Lawson  *
51853289f6aSNate Lawson  * PARAMETERS:  Op            - Parse node to write.
51953289f6aSNate Lawson  *
52053289f6aSNate Lawson  * RETURN:      None.
52153289f6aSNate Lawson  *
52253289f6aSNate Lawson  * DESCRIPTION: Write the AML that corresponds to a parse node.
52353289f6aSNate Lawson  *
52453289f6aSNate Lawson  ******************************************************************************/
52553289f6aSNate Lawson 
526fba7fc7eSJung-uk Kim static void
52753289f6aSNate Lawson CgWriteNode (
52853289f6aSNate Lawson     ACPI_PARSE_OBJECT       *Op)
52953289f6aSNate Lawson {
53053289f6aSNate Lawson     ASL_RESOURCE_NODE       *Rnode;
53153289f6aSNate Lawson 
53253289f6aSNate Lawson 
53353289f6aSNate Lawson     /* Always check for DEFAULT_ARG and other "Noop" nodes */
53453289f6aSNate Lawson     /* TBD: this may not be the best place for this check */
53553289f6aSNate Lawson 
53653289f6aSNate Lawson     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
53753289f6aSNate Lawson         (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL)     ||
53853289f6aSNate Lawson         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
53953289f6aSNate Lawson         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
54053289f6aSNate Lawson     {
54153289f6aSNate Lawson         return;
54253289f6aSNate Lawson     }
54353289f6aSNate Lawson 
5441a39cfb0SJung-uk Kim     Op->Asl.FinalAmlLength = 0;
5451a39cfb0SJung-uk Kim 
54653289f6aSNate Lawson     switch (Op->Asl.AmlOpcode)
54753289f6aSNate Lawson     {
54853289f6aSNate Lawson     case AML_RAW_DATA_BYTE:
54953289f6aSNate Lawson     case AML_RAW_DATA_WORD:
55053289f6aSNate Lawson     case AML_RAW_DATA_DWORD:
55153289f6aSNate Lawson     case AML_RAW_DATA_QWORD:
55253289f6aSNate Lawson 
55353289f6aSNate Lawson         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
55453289f6aSNate Lawson         return;
55553289f6aSNate Lawson 
55653289f6aSNate Lawson 
55753289f6aSNate Lawson     case AML_RAW_DATA_BUFFER:
55853289f6aSNate Lawson 
55953289f6aSNate Lawson         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
56053289f6aSNate Lawson         return;
56153289f6aSNate Lawson 
56253289f6aSNate Lawson 
56353289f6aSNate Lawson     case AML_RAW_DATA_CHAIN:
56453289f6aSNate Lawson 
56553289f6aSNate Lawson         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
56653289f6aSNate Lawson         while (Rnode)
56753289f6aSNate Lawson         {
56853289f6aSNate Lawson             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
56953289f6aSNate Lawson             Rnode = Rnode->Next;
57053289f6aSNate Lawson         }
57153289f6aSNate Lawson         return;
57253289f6aSNate Lawson 
57353289f6aSNate Lawson     default:
574a9d8d09cSJung-uk Kim 
57553289f6aSNate Lawson         /* Internal data opcodes must all appear above */
576a9d8d09cSJung-uk Kim 
57753289f6aSNate Lawson         break;
57853289f6aSNate Lawson     }
57953289f6aSNate Lawson 
58053289f6aSNate Lawson     switch (Op->Asl.ParseOpcode)
58153289f6aSNate Lawson     {
58253289f6aSNate Lawson     case PARSEOP_DEFAULT_ARG:
58353289f6aSNate Lawson 
58453289f6aSNate Lawson         break;
58553289f6aSNate Lawson 
58653289f6aSNate Lawson     case PARSEOP_DEFINITIONBLOCK:
58753289f6aSNate Lawson 
58853289f6aSNate Lawson         CgWriteTableHeader (Op);
58953289f6aSNate Lawson         break;
59053289f6aSNate Lawson 
59153289f6aSNate Lawson     case PARSEOP_NAMESEG:
59253289f6aSNate Lawson     case PARSEOP_NAMESTRING:
59353289f6aSNate Lawson     case PARSEOP_METHODCALL:
59453289f6aSNate Lawson 
59553289f6aSNate Lawson         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
59653289f6aSNate Lawson         break;
59753289f6aSNate Lawson 
59853289f6aSNate Lawson     default:
59953289f6aSNate Lawson 
60053289f6aSNate Lawson         CgWriteAmlOpcode (Op);
60153289f6aSNate Lawson         break;
60253289f6aSNate Lawson     }
60353289f6aSNate Lawson }
604