Home | History | Annotate | Line # | Download | only in compiler
aslcodegen.c revision 1.1
      1  1.1  jruoho 
      2  1.1  jruoho /******************************************************************************
      3  1.1  jruoho  *
      4  1.1  jruoho  * Module Name: aslcodegen - AML code generation
      5  1.1  jruoho  *
      6  1.1  jruoho  *****************************************************************************/
      7  1.1  jruoho 
      8  1.1  jruoho /******************************************************************************
      9  1.1  jruoho  *
     10  1.1  jruoho  * 1. Copyright Notice
     11  1.1  jruoho  *
     12  1.1  jruoho  * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
     13  1.1  jruoho  * All rights reserved.
     14  1.1  jruoho  *
     15  1.1  jruoho  * 2. License
     16  1.1  jruoho  *
     17  1.1  jruoho  * 2.1. This is your license from Intel Corp. under its intellectual property
     18  1.1  jruoho  * rights.  You may have additional license terms from the party that provided
     19  1.1  jruoho  * you this software, covering your right to use that party's intellectual
     20  1.1  jruoho  * property rights.
     21  1.1  jruoho  *
     22  1.1  jruoho  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
     23  1.1  jruoho  * copy of the source code appearing in this file ("Covered Code") an
     24  1.1  jruoho  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
     25  1.1  jruoho  * base code distributed originally by Intel ("Original Intel Code") to copy,
     26  1.1  jruoho  * make derivatives, distribute, use and display any portion of the Covered
     27  1.1  jruoho  * Code in any form, with the right to sublicense such rights; and
     28  1.1  jruoho  *
     29  1.1  jruoho  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
     30  1.1  jruoho  * license (with the right to sublicense), under only those claims of Intel
     31  1.1  jruoho  * patents that are infringed by the Original Intel Code, to make, use, sell,
     32  1.1  jruoho  * offer to sell, and import the Covered Code and derivative works thereof
     33  1.1  jruoho  * solely to the minimum extent necessary to exercise the above copyright
     34  1.1  jruoho  * license, and in no event shall the patent license extend to any additions
     35  1.1  jruoho  * to or modifications of the Original Intel Code.  No other license or right
     36  1.1  jruoho  * is granted directly or by implication, estoppel or otherwise;
     37  1.1  jruoho  *
     38  1.1  jruoho  * The above copyright and patent license is granted only if the following
     39  1.1  jruoho  * conditions are met:
     40  1.1  jruoho  *
     41  1.1  jruoho  * 3. Conditions
     42  1.1  jruoho  *
     43  1.1  jruoho  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
     44  1.1  jruoho  * Redistribution of source code of any substantial portion of the Covered
     45  1.1  jruoho  * Code or modification with rights to further distribute source must include
     46  1.1  jruoho  * the above Copyright Notice, the above License, this list of Conditions,
     47  1.1  jruoho  * and the following Disclaimer and Export Compliance provision.  In addition,
     48  1.1  jruoho  * Licensee must cause all Covered Code to which Licensee contributes to
     49  1.1  jruoho  * contain a file documenting the changes Licensee made to create that Covered
     50  1.1  jruoho  * Code and the date of any change.  Licensee must include in that file the
     51  1.1  jruoho  * documentation of any changes made by any predecessor Licensee.  Licensee
     52  1.1  jruoho  * must include a prominent statement that the modification is derived,
     53  1.1  jruoho  * directly or indirectly, from Original Intel Code.
     54  1.1  jruoho  *
     55  1.1  jruoho  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
     56  1.1  jruoho  * Redistribution of source code of any substantial portion of the Covered
     57  1.1  jruoho  * Code or modification without rights to further distribute source must
     58  1.1  jruoho  * include the following Disclaimer and Export Compliance provision in the
     59  1.1  jruoho  * documentation and/or other materials provided with distribution.  In
     60  1.1  jruoho  * addition, Licensee may not authorize further sublicense of source of any
     61  1.1  jruoho  * portion of the Covered Code, and must include terms to the effect that the
     62  1.1  jruoho  * license from Licensee to its licensee is limited to the intellectual
     63  1.1  jruoho  * property embodied in the software Licensee provides to its licensee, and
     64  1.1  jruoho  * not to intellectual property embodied in modifications its licensee may
     65  1.1  jruoho  * make.
     66  1.1  jruoho  *
     67  1.1  jruoho  * 3.3. Redistribution of Executable. Redistribution in executable form of any
     68  1.1  jruoho  * substantial portion of the Covered Code or modification must reproduce the
     69  1.1  jruoho  * above Copyright Notice, and the following Disclaimer and Export Compliance
     70  1.1  jruoho  * provision in the documentation and/or other materials provided with the
     71  1.1  jruoho  * distribution.
     72  1.1  jruoho  *
     73  1.1  jruoho  * 3.4. Intel retains all right, title, and interest in and to the Original
     74  1.1  jruoho  * Intel Code.
     75  1.1  jruoho  *
     76  1.1  jruoho  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
     77  1.1  jruoho  * Intel shall be used in advertising or otherwise to promote the sale, use or
     78  1.1  jruoho  * other dealings in products derived from or relating to the Covered Code
     79  1.1  jruoho  * without prior written authorization from Intel.
     80  1.1  jruoho  *
     81  1.1  jruoho  * 4. Disclaimer and Export Compliance
     82  1.1  jruoho  *
     83  1.1  jruoho  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
     84  1.1  jruoho  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
     85  1.1  jruoho  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
     86  1.1  jruoho  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
     87  1.1  jruoho  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
     88  1.1  jruoho  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
     89  1.1  jruoho  * PARTICULAR PURPOSE.
     90  1.1  jruoho  *
     91  1.1  jruoho  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
     92  1.1  jruoho  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
     93  1.1  jruoho  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
     94  1.1  jruoho  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
     95  1.1  jruoho  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
     96  1.1  jruoho  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
     97  1.1  jruoho  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
     98  1.1  jruoho  * LIMITED REMEDY.
     99  1.1  jruoho  *
    100  1.1  jruoho  * 4.3. Licensee shall not export, either directly or indirectly, any of this
    101  1.1  jruoho  * software or system incorporating such software without first obtaining any
    102  1.1  jruoho  * required license or other approval from the U. S. Department of Commerce or
    103  1.1  jruoho  * any other agency or department of the United States Government.  In the
    104  1.1  jruoho  * event Licensee exports any such software from the United States or
    105  1.1  jruoho  * re-exports any such software from a foreign destination, Licensee shall
    106  1.1  jruoho  * ensure that the distribution and export/re-export of the software is in
    107  1.1  jruoho  * compliance with all laws, regulations, orders, or other restrictions of the
    108  1.1  jruoho  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
    109  1.1  jruoho  * any of its subsidiaries will export/re-export any technical data, process,
    110  1.1  jruoho  * software, or service, directly or indirectly, to any country for which the
    111  1.1  jruoho  * United States government or any agency thereof requires an export license,
    112  1.1  jruoho  * other governmental approval, or letter of assurance, without first obtaining
    113  1.1  jruoho  * such license, approval or letter.
    114  1.1  jruoho  *
    115  1.1  jruoho  *****************************************************************************/
    116  1.1  jruoho 
    117  1.1  jruoho 
    118  1.1  jruoho #include "aslcompiler.h"
    119  1.1  jruoho #include "aslcompiler.y.h"
    120  1.1  jruoho #include "amlcode.h"
    121  1.1  jruoho 
    122  1.1  jruoho #define _COMPONENT          ACPI_COMPILER
    123  1.1  jruoho         ACPI_MODULE_NAME    ("aslcodegen")
    124  1.1  jruoho 
    125  1.1  jruoho /* Local prototypes */
    126  1.1  jruoho 
    127  1.1  jruoho static ACPI_STATUS
    128  1.1  jruoho CgAmlWriteWalk (
    129  1.1  jruoho     ACPI_PARSE_OBJECT       *Op,
    130  1.1  jruoho     UINT32                  Level,
    131  1.1  jruoho     void                    *Context);
    132  1.1  jruoho 
    133  1.1  jruoho static void
    134  1.1  jruoho CgLocalWriteAmlData (
    135  1.1  jruoho     ACPI_PARSE_OBJECT       *Op,
    136  1.1  jruoho     void                    *Buffer,
    137  1.1  jruoho     UINT32                  Length);
    138  1.1  jruoho 
    139  1.1  jruoho static void
    140  1.1  jruoho CgWriteAmlOpcode (
    141  1.1  jruoho     ACPI_PARSE_OBJECT       *Op);
    142  1.1  jruoho 
    143  1.1  jruoho static void
    144  1.1  jruoho CgWriteTableHeader (
    145  1.1  jruoho     ACPI_PARSE_OBJECT       *Op);
    146  1.1  jruoho 
    147  1.1  jruoho static void
    148  1.1  jruoho CgCloseTable (
    149  1.1  jruoho     void);
    150  1.1  jruoho 
    151  1.1  jruoho static void
    152  1.1  jruoho CgWriteNode (
    153  1.1  jruoho     ACPI_PARSE_OBJECT       *Op);
    154  1.1  jruoho 
    155  1.1  jruoho 
    156  1.1  jruoho /*******************************************************************************
    157  1.1  jruoho  *
    158  1.1  jruoho  * FUNCTION:    CgGenerateAmlOutput
    159  1.1  jruoho  *
    160  1.1  jruoho  * PARAMETERS:  None.
    161  1.1  jruoho  *
    162  1.1  jruoho  * RETURN:      None
    163  1.1  jruoho  *
    164  1.1  jruoho  * DESCRIPTION: Generate AML code.  Currently generates the listing file
    165  1.1  jruoho  *              simultaneously.
    166  1.1  jruoho  *
    167  1.1  jruoho  ******************************************************************************/
    168  1.1  jruoho 
    169  1.1  jruoho void
    170  1.1  jruoho CgGenerateAmlOutput (
    171  1.1  jruoho     void)
    172  1.1  jruoho {
    173  1.1  jruoho 
    174  1.1  jruoho     DbgPrint (ASL_DEBUG_OUTPUT, "\nWriting AML\n\n");
    175  1.1  jruoho 
    176  1.1  jruoho     /* Generate the AML output file */
    177  1.1  jruoho 
    178  1.1  jruoho     FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
    179  1.1  jruoho     Gbl_SourceLine = 0;
    180  1.1  jruoho     Gbl_NextError = Gbl_ErrorLog;
    181  1.1  jruoho 
    182  1.1  jruoho     TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
    183  1.1  jruoho         CgAmlWriteWalk, NULL, NULL);
    184  1.1  jruoho     CgCloseTable ();
    185  1.1  jruoho }
    186  1.1  jruoho 
    187  1.1  jruoho 
    188  1.1  jruoho /*******************************************************************************
    189  1.1  jruoho  *
    190  1.1  jruoho  * FUNCTION:    CgAmlWriteWalk
    191  1.1  jruoho  *
    192  1.1  jruoho  * PARAMETERS:  ASL_WALK_CALLBACK
    193  1.1  jruoho  *
    194  1.1  jruoho  * RETURN:      Status
    195  1.1  jruoho  *
    196  1.1  jruoho  * DESCRIPTION: Parse tree walk to generate the AML code.
    197  1.1  jruoho  *
    198  1.1  jruoho  ******************************************************************************/
    199  1.1  jruoho 
    200  1.1  jruoho static ACPI_STATUS
    201  1.1  jruoho CgAmlWriteWalk (
    202  1.1  jruoho     ACPI_PARSE_OBJECT       *Op,
    203  1.1  jruoho     UINT32                  Level,
    204  1.1  jruoho     void                    *Context)
    205  1.1  jruoho {
    206  1.1  jruoho 
    207  1.1  jruoho     /*
    208  1.1  jruoho      * Print header at level 0. Alignment assumes 32-bit pointers
    209  1.1  jruoho      */
    210  1.1  jruoho     if (!Level)
    211  1.1  jruoho     {
    212  1.1  jruoho         DbgPrint (ASL_TREE_OUTPUT,
    213  1.1  jruoho             "Final parse tree used for AML output:\n");
    214  1.1  jruoho         DbgPrint (ASL_TREE_OUTPUT,
    215  1.1  jruoho             "%*s Value    P_Op A_Op OpLen PByts Len  SubLen PSubLen OpPtr    Child    Parent   Flags    AcTyp    Final Col L\n",
    216  1.1  jruoho             76, " ");
    217  1.1  jruoho     }
    218  1.1  jruoho 
    219  1.1  jruoho     /* Debug output */
    220  1.1  jruoho 
    221  1.1  jruoho     DbgPrint (ASL_TREE_OUTPUT,
    222  1.1  jruoho         "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
    223  1.1  jruoho     UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
    224  1.1  jruoho 
    225  1.1  jruoho     if (Op->Asl.ParseOpcode == PARSEOP_NAMESEG    ||
    226  1.1  jruoho         Op->Asl.ParseOpcode == PARSEOP_NAMESTRING ||
    227  1.1  jruoho         Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
    228  1.1  jruoho     {
    229  1.1  jruoho         DbgPrint (ASL_TREE_OUTPUT,
    230  1.1  jruoho             "%10.32s      ", Op->Asl.ExternalName);
    231  1.1  jruoho     }
    232  1.1  jruoho     else
    233  1.1  jruoho     {
    234  1.1  jruoho         DbgPrint (ASL_TREE_OUTPUT, "                ");
    235  1.1  jruoho     }
    236  1.1  jruoho 
    237  1.1  jruoho     DbgPrint (ASL_TREE_OUTPUT,
    238  1.1  jruoho     "%08X %04X %04X %01X     %04X  %04X %04X   %04X    %08X %08X %08X %08X %08X %04X  %02d  %02d\n",
    239  1.1  jruoho             /* 1  */ (UINT32) Op->Asl.Value.Integer,
    240  1.1  jruoho             /* 2  */ Op->Asl.ParseOpcode,
    241  1.1  jruoho             /* 3  */ Op->Asl.AmlOpcode,
    242  1.1  jruoho             /* 4  */ Op->Asl.AmlOpcodeLength,
    243  1.1  jruoho             /* 5  */ Op->Asl.AmlPkgLenBytes,
    244  1.1  jruoho             /* 6  */ Op->Asl.AmlLength,
    245  1.1  jruoho             /* 7  */ Op->Asl.AmlSubtreeLength,
    246  1.1  jruoho             /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
    247  1.1  jruoho             /* 9  */ Op,
    248  1.1  jruoho             /* 10 */ Op->Asl.Child,
    249  1.1  jruoho             /* 11 */ Op->Asl.Parent,
    250  1.1  jruoho             /* 12 */ Op->Asl.CompileFlags,
    251  1.1  jruoho             /* 13 */ Op->Asl.AcpiBtype,
    252  1.1  jruoho             /* 14 */ Op->Asl.FinalAmlLength,
    253  1.1  jruoho             /* 15 */ Op->Asl.Column,
    254  1.1  jruoho             /* 16 */ Op->Asl.LineNumber);
    255  1.1  jruoho 
    256  1.1  jruoho     /* Generate the AML for this node */
    257  1.1  jruoho 
    258  1.1  jruoho     CgWriteNode (Op);
    259  1.1  jruoho     return (AE_OK);
    260  1.1  jruoho }
    261  1.1  jruoho 
    262  1.1  jruoho 
    263  1.1  jruoho /*******************************************************************************
    264  1.1  jruoho  *
    265  1.1  jruoho  * FUNCTION:    CgLocalWriteAmlData
    266  1.1  jruoho  *
    267  1.1  jruoho  * PARAMETERS:  Op              - Current parse op
    268  1.1  jruoho  *              Buffer          - Buffer to write
    269  1.1  jruoho  *              Length          - Size of data in buffer
    270  1.1  jruoho  *
    271  1.1  jruoho  * RETURN:      None
    272  1.1  jruoho  *
    273  1.1  jruoho  * DESCRIPTION: Write a buffer of AML data to the AML output file.
    274  1.1  jruoho  *
    275  1.1  jruoho  ******************************************************************************/
    276  1.1  jruoho 
    277  1.1  jruoho static void
    278  1.1  jruoho CgLocalWriteAmlData (
    279  1.1  jruoho     ACPI_PARSE_OBJECT       *Op,
    280  1.1  jruoho     void                    *Buffer,
    281  1.1  jruoho     UINT32                  Length)
    282  1.1  jruoho {
    283  1.1  jruoho 
    284  1.1  jruoho     /* Write the raw data to the AML file */
    285  1.1  jruoho 
    286  1.1  jruoho     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
    287  1.1  jruoho 
    288  1.1  jruoho     /* Update the final AML length for this node (used for listings) */
    289  1.1  jruoho 
    290  1.1  jruoho     if (Op)
    291  1.1  jruoho     {
    292  1.1  jruoho         Op->Asl.FinalAmlLength += Length;
    293  1.1  jruoho     }
    294  1.1  jruoho }
    295  1.1  jruoho 
    296  1.1  jruoho 
    297  1.1  jruoho /*******************************************************************************
    298  1.1  jruoho  *
    299  1.1  jruoho  * FUNCTION:    CgWriteAmlOpcode
    300  1.1  jruoho  *
    301  1.1  jruoho  * PARAMETERS:  Op            - Parse node with an AML opcode
    302  1.1  jruoho  *
    303  1.1  jruoho  * RETURN:      None.
    304  1.1  jruoho  *
    305  1.1  jruoho  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
    306  1.1  jruoho  *
    307  1.1  jruoho  ******************************************************************************/
    308  1.1  jruoho 
    309  1.1  jruoho static void
    310  1.1  jruoho CgWriteAmlOpcode (
    311  1.1  jruoho     ACPI_PARSE_OBJECT       *Op)
    312  1.1  jruoho {
    313  1.1  jruoho     UINT8                   PkgLenFirstByte;
    314  1.1  jruoho     UINT32                  i;
    315  1.1  jruoho     union {
    316  1.1  jruoho         UINT16                  Opcode;
    317  1.1  jruoho         UINT8                   OpcodeBytes[2];
    318  1.1  jruoho     } Aml;
    319  1.1  jruoho     union {
    320  1.1  jruoho         UINT32                  Len;
    321  1.1  jruoho         UINT8                   LenBytes[4];
    322  1.1  jruoho     } PkgLen;
    323  1.1  jruoho 
    324  1.1  jruoho 
    325  1.1  jruoho     /* We expect some DEFAULT_ARGs, just ignore them */
    326  1.1  jruoho 
    327  1.1  jruoho     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
    328  1.1  jruoho     {
    329  1.1  jruoho         return;
    330  1.1  jruoho     }
    331  1.1  jruoho 
    332  1.1  jruoho     switch (Op->Asl.AmlOpcode)
    333  1.1  jruoho     {
    334  1.1  jruoho     case AML_UNASSIGNED_OPCODE:
    335  1.1  jruoho 
    336  1.1  jruoho         /* These opcodes should not get here */
    337  1.1  jruoho 
    338  1.1  jruoho         printf ("Found a node with an unassigned AML opcode\n");
    339  1.1  jruoho         fprintf (stderr, "Found a node with an unassigned AML opcode\n");
    340  1.1  jruoho         return;
    341  1.1  jruoho 
    342  1.1  jruoho     case AML_INT_RESERVEDFIELD_OP:
    343  1.1  jruoho 
    344  1.1  jruoho         /* Special opcodes for within a field definition */
    345  1.1  jruoho 
    346  1.1  jruoho         Aml.Opcode = 0x00;
    347  1.1  jruoho         break;
    348  1.1  jruoho 
    349  1.1  jruoho     case AML_INT_ACCESSFIELD_OP:
    350  1.1  jruoho 
    351  1.1  jruoho         Aml.Opcode = 0x01;
    352  1.1  jruoho         break;
    353  1.1  jruoho 
    354  1.1  jruoho     default:
    355  1.1  jruoho         Aml.Opcode = Op->Asl.AmlOpcode;
    356  1.1  jruoho         break;
    357  1.1  jruoho     }
    358  1.1  jruoho 
    359  1.1  jruoho 
    360  1.1  jruoho     switch (Aml.Opcode)
    361  1.1  jruoho     {
    362  1.1  jruoho     case AML_PACKAGE_LENGTH:
    363  1.1  jruoho 
    364  1.1  jruoho         /* Value is the length to be encoded (Used in field definitions) */
    365  1.1  jruoho 
    366  1.1  jruoho         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
    367  1.1  jruoho         break;
    368  1.1  jruoho 
    369  1.1  jruoho     default:
    370  1.1  jruoho 
    371  1.1  jruoho         /* Check for two-byte opcode */
    372  1.1  jruoho 
    373  1.1  jruoho         if (Aml.Opcode > 0x00FF)
    374  1.1  jruoho         {
    375  1.1  jruoho             /* Write the high byte first */
    376  1.1  jruoho 
    377  1.1  jruoho             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
    378  1.1  jruoho         }
    379  1.1  jruoho 
    380  1.1  jruoho         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
    381  1.1  jruoho 
    382  1.1  jruoho         /* Subtreelength doesn't include length of package length bytes */
    383  1.1  jruoho 
    384  1.1  jruoho         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
    385  1.1  jruoho         break;
    386  1.1  jruoho     }
    387  1.1  jruoho 
    388  1.1  jruoho     /* Does this opcode have an associated "PackageLength" field? */
    389  1.1  jruoho 
    390  1.1  jruoho     if (Op->Asl.CompileFlags & NODE_AML_PACKAGE)
    391  1.1  jruoho     {
    392  1.1  jruoho         if (Op->Asl.AmlPkgLenBytes == 1)
    393  1.1  jruoho         {
    394  1.1  jruoho             /* Simplest case -- no bytes to follow, just write the count */
    395  1.1  jruoho 
    396  1.1  jruoho             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
    397  1.1  jruoho         }
    398  1.1  jruoho         else if (Op->Asl.AmlPkgLenBytes != 0)
    399  1.1  jruoho         {
    400  1.1  jruoho             /*
    401  1.1  jruoho              * Encode the "bytes to follow" in the first byte, top two bits.
    402  1.1  jruoho              * The low-order nybble of the length is in the bottom 4 bits
    403  1.1  jruoho              */
    404  1.1  jruoho             PkgLenFirstByte = (UINT8)
    405  1.1  jruoho                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
    406  1.1  jruoho                 (PkgLen.LenBytes[0] & 0x0F));
    407  1.1  jruoho 
    408  1.1  jruoho             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
    409  1.1  jruoho 
    410  1.1  jruoho             /*
    411  1.1  jruoho              * Shift the length over by the 4 bits we just stuffed
    412  1.1  jruoho              * in the first byte
    413  1.1  jruoho              */
    414  1.1  jruoho             PkgLen.Len >>= 4;
    415  1.1  jruoho 
    416  1.1  jruoho             /* Now we can write the remaining bytes - either 1, 2, or 3 bytes */
    417  1.1  jruoho 
    418  1.1  jruoho             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
    419  1.1  jruoho             {
    420  1.1  jruoho                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
    421  1.1  jruoho             }
    422  1.1  jruoho         }
    423  1.1  jruoho     }
    424  1.1  jruoho 
    425  1.1  jruoho     switch (Aml.Opcode)
    426  1.1  jruoho     {
    427  1.1  jruoho     case AML_BYTE_OP:
    428  1.1  jruoho 
    429  1.1  jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
    430  1.1  jruoho         break;
    431  1.1  jruoho 
    432  1.1  jruoho     case AML_WORD_OP:
    433  1.1  jruoho 
    434  1.1  jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
    435  1.1  jruoho        break;
    436  1.1  jruoho 
    437  1.1  jruoho     case AML_DWORD_OP:
    438  1.1  jruoho 
    439  1.1  jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
    440  1.1  jruoho         break;
    441  1.1  jruoho 
    442  1.1  jruoho     case AML_QWORD_OP:
    443  1.1  jruoho 
    444  1.1  jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
    445  1.1  jruoho         break;
    446  1.1  jruoho 
    447  1.1  jruoho     case AML_STRING_OP:
    448  1.1  jruoho 
    449  1.1  jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    450  1.1  jruoho         break;
    451  1.1  jruoho 
    452  1.1  jruoho     default:
    453  1.1  jruoho         /* All data opcodes must appear above */
    454  1.1  jruoho         break;
    455  1.1  jruoho     }
    456  1.1  jruoho }
    457  1.1  jruoho 
    458  1.1  jruoho 
    459  1.1  jruoho /*******************************************************************************
    460  1.1  jruoho  *
    461  1.1  jruoho  * FUNCTION:    CgWriteTableHeader
    462  1.1  jruoho  *
    463  1.1  jruoho  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
    464  1.1  jruoho  *
    465  1.1  jruoho  * RETURN:      None
    466  1.1  jruoho  *
    467  1.1  jruoho  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
    468  1.1  jruoho  *
    469  1.1  jruoho  ******************************************************************************/
    470  1.1  jruoho 
    471  1.1  jruoho static void
    472  1.1  jruoho CgWriteTableHeader (
    473  1.1  jruoho     ACPI_PARSE_OBJECT       *Op)
    474  1.1  jruoho {
    475  1.1  jruoho     ACPI_PARSE_OBJECT       *Child;
    476  1.1  jruoho 
    477  1.1  jruoho 
    478  1.1  jruoho     /* AML filename */
    479  1.1  jruoho 
    480  1.1  jruoho     Child = Op->Asl.Child;
    481  1.1  jruoho 
    482  1.1  jruoho     /* Signature */
    483  1.1  jruoho 
    484  1.1  jruoho     Child = Child->Asl.Next;
    485  1.1  jruoho     strncpy (TableHeader.Signature, Child->Asl.Value.String, 4);
    486  1.1  jruoho 
    487  1.1  jruoho     /* Revision */
    488  1.1  jruoho 
    489  1.1  jruoho     Child = Child->Asl.Next;
    490  1.1  jruoho     TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
    491  1.1  jruoho 
    492  1.1  jruoho     /* Command-line Revision override */
    493  1.1  jruoho 
    494  1.1  jruoho     if (Gbl_RevisionOverride)
    495  1.1  jruoho     {
    496  1.1  jruoho         TableHeader.Revision = Gbl_RevisionOverride;
    497  1.1  jruoho     }
    498  1.1  jruoho 
    499  1.1  jruoho     /* OEMID */
    500  1.1  jruoho 
    501  1.1  jruoho     Child = Child->Asl.Next;
    502  1.1  jruoho     strncpy (TableHeader.OemId, Child->Asl.Value.String, 6);
    503  1.1  jruoho 
    504  1.1  jruoho     /* OEM TableID */
    505  1.1  jruoho 
    506  1.1  jruoho     Child = Child->Asl.Next;
    507  1.1  jruoho     strncpy (TableHeader.OemTableId, Child->Asl.Value.String, 8);
    508  1.1  jruoho 
    509  1.1  jruoho     /* OEM Revision */
    510  1.1  jruoho 
    511  1.1  jruoho     Child = Child->Asl.Next;
    512  1.1  jruoho     TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
    513  1.1  jruoho 
    514  1.1  jruoho     /* Compiler ID */
    515  1.1  jruoho 
    516  1.1  jruoho     strncpy (TableHeader.AslCompilerId, CompilerCreatorId, 4);
    517  1.1  jruoho 
    518  1.1  jruoho     /* Compiler version */
    519  1.1  jruoho 
    520  1.1  jruoho     TableHeader.AslCompilerRevision = CompilerCreatorRevision;
    521  1.1  jruoho 
    522  1.1  jruoho     /* Table length. Checksum zero for now, will rewrite later */
    523  1.1  jruoho 
    524  1.1  jruoho     TableHeader.Length   = Gbl_TableLength;
    525  1.1  jruoho     TableHeader.Checksum = 0;
    526  1.1  jruoho 
    527  1.1  jruoho     CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
    528  1.1  jruoho }
    529  1.1  jruoho 
    530  1.1  jruoho 
    531  1.1  jruoho /*******************************************************************************
    532  1.1  jruoho  *
    533  1.1  jruoho  * FUNCTION:    CgCloseTable
    534  1.1  jruoho  *
    535  1.1  jruoho  * PARAMETERS:  None.
    536  1.1  jruoho  *
    537  1.1  jruoho  * RETURN:      None.
    538  1.1  jruoho  *
    539  1.1  jruoho  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
    540  1.1  jruoho  *              re-writing the header.
    541  1.1  jruoho  *
    542  1.1  jruoho  ******************************************************************************/
    543  1.1  jruoho 
    544  1.1  jruoho static void
    545  1.1  jruoho CgCloseTable (
    546  1.1  jruoho     void)
    547  1.1  jruoho {
    548  1.1  jruoho     signed char         Sum;
    549  1.1  jruoho     UINT8               FileByte;
    550  1.1  jruoho 
    551  1.1  jruoho 
    552  1.1  jruoho     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    553  1.1  jruoho     Sum = 0;
    554  1.1  jruoho 
    555  1.1  jruoho     /* Calculate the checksum over the entire file */
    556  1.1  jruoho 
    557  1.1  jruoho     while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) == AE_OK)
    558  1.1  jruoho     {
    559  1.1  jruoho         Sum = (signed char) (Sum + FileByte);
    560  1.1  jruoho     }
    561  1.1  jruoho 
    562  1.1  jruoho     /* Re-write the table header with the checksum */
    563  1.1  jruoho 
    564  1.1  jruoho     TableHeader.Checksum = (UINT8) (0 - Sum);
    565  1.1  jruoho 
    566  1.1  jruoho     FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
    567  1.1  jruoho     CgLocalWriteAmlData (NULL, &TableHeader, sizeof (ACPI_TABLE_HEADER));
    568  1.1  jruoho }
    569  1.1  jruoho 
    570  1.1  jruoho 
    571  1.1  jruoho /*******************************************************************************
    572  1.1  jruoho  *
    573  1.1  jruoho  * FUNCTION:    CgWriteNode
    574  1.1  jruoho  *
    575  1.1  jruoho  * PARAMETERS:  Op            - Parse node to write.
    576  1.1  jruoho  *
    577  1.1  jruoho  * RETURN:      None.
    578  1.1  jruoho  *
    579  1.1  jruoho  * DESCRIPTION: Write the AML that corresponds to a parse node.
    580  1.1  jruoho  *
    581  1.1  jruoho  ******************************************************************************/
    582  1.1  jruoho 
    583  1.1  jruoho static void
    584  1.1  jruoho CgWriteNode (
    585  1.1  jruoho     ACPI_PARSE_OBJECT       *Op)
    586  1.1  jruoho {
    587  1.1  jruoho     ASL_RESOURCE_NODE       *Rnode;
    588  1.1  jruoho 
    589  1.1  jruoho 
    590  1.1  jruoho     /* Always check for DEFAULT_ARG and other "Noop" nodes */
    591  1.1  jruoho     /* TBD: this may not be the best place for this check */
    592  1.1  jruoho 
    593  1.1  jruoho     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
    594  1.1  jruoho         (Op->Asl.ParseOpcode == PARSEOP_EXTERNAL)     ||
    595  1.1  jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
    596  1.1  jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
    597  1.1  jruoho     {
    598  1.1  jruoho         return;
    599  1.1  jruoho     }
    600  1.1  jruoho 
    601  1.1  jruoho     Op->Asl.FinalAmlLength = 0;
    602  1.1  jruoho 
    603  1.1  jruoho     switch (Op->Asl.AmlOpcode)
    604  1.1  jruoho     {
    605  1.1  jruoho     case AML_RAW_DATA_BYTE:
    606  1.1  jruoho     case AML_RAW_DATA_WORD:
    607  1.1  jruoho     case AML_RAW_DATA_DWORD:
    608  1.1  jruoho     case AML_RAW_DATA_QWORD:
    609  1.1  jruoho 
    610  1.1  jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
    611  1.1  jruoho         return;
    612  1.1  jruoho 
    613  1.1  jruoho 
    614  1.1  jruoho     case AML_RAW_DATA_BUFFER:
    615  1.1  jruoho 
    616  1.1  jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
    617  1.1  jruoho         return;
    618  1.1  jruoho 
    619  1.1  jruoho 
    620  1.1  jruoho     case AML_RAW_DATA_CHAIN:
    621  1.1  jruoho 
    622  1.1  jruoho         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
    623  1.1  jruoho         while (Rnode)
    624  1.1  jruoho         {
    625  1.1  jruoho             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
    626  1.1  jruoho             Rnode = Rnode->Next;
    627  1.1  jruoho         }
    628  1.1  jruoho         return;
    629  1.1  jruoho 
    630  1.1  jruoho     default:
    631  1.1  jruoho         /* Internal data opcodes must all appear above */
    632  1.1  jruoho         break;
    633  1.1  jruoho     }
    634  1.1  jruoho 
    635  1.1  jruoho     switch (Op->Asl.ParseOpcode)
    636  1.1  jruoho     {
    637  1.1  jruoho     case PARSEOP_DEFAULT_ARG:
    638  1.1  jruoho 
    639  1.1  jruoho         break;
    640  1.1  jruoho 
    641  1.1  jruoho     case PARSEOP_DEFINITIONBLOCK:
    642  1.1  jruoho 
    643  1.1  jruoho         CgWriteTableHeader (Op);
    644  1.1  jruoho         break;
    645  1.1  jruoho 
    646  1.1  jruoho     case PARSEOP_NAMESEG:
    647  1.1  jruoho     case PARSEOP_NAMESTRING:
    648  1.1  jruoho     case PARSEOP_METHODCALL:
    649  1.1  jruoho 
    650  1.1  jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    651  1.1  jruoho         break;
    652  1.1  jruoho 
    653  1.1  jruoho     default:
    654  1.1  jruoho 
    655  1.1  jruoho         CgWriteAmlOpcode (Op);
    656  1.1  jruoho         break;
    657  1.1  jruoho     }
    658  1.1  jruoho }
    659  1.1  jruoho 
    660  1.1  jruoho 
    661