Home | History | Annotate | Line # | Download | only in compiler
aslcodegen.c revision 1.1.1.12
      1       1.1    jruoho /******************************************************************************
      2       1.1    jruoho  *
      3       1.1    jruoho  * Module Name: aslcodegen - AML code generation
      4       1.1    jruoho  *
      5       1.1    jruoho  *****************************************************************************/
      6       1.1    jruoho 
      7   1.1.1.2    jruoho /*
      8  1.1.1.12  christos  * Copyright (C) 2000 - 2018, Intel Corp.
      9       1.1    jruoho  * All rights reserved.
     10       1.1    jruoho  *
     11   1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12   1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13   1.1.1.2    jruoho  * are met:
     14   1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15   1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16   1.1.1.2    jruoho  *    without modification.
     17   1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18   1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19   1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20   1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21   1.1.1.2    jruoho  *    binary redistribution.
     22   1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23   1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24   1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25   1.1.1.2    jruoho  *
     26   1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27   1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28   1.1.1.2    jruoho  * Software Foundation.
     29   1.1.1.2    jruoho  *
     30   1.1.1.2    jruoho  * NO WARRANTY
     31   1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32   1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33   1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34   1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35   1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36   1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37   1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38   1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40   1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41   1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42   1.1.1.2    jruoho  */
     43       1.1    jruoho 
     44       1.1    jruoho #include "aslcompiler.h"
     45       1.1    jruoho #include "aslcompiler.y.h"
     46       1.1    jruoho #include "amlcode.h"
     47  1.1.1.10  christos #include "acconvert.h"
     48       1.1    jruoho 
     49       1.1    jruoho #define _COMPONENT          ACPI_COMPILER
     50       1.1    jruoho         ACPI_MODULE_NAME    ("aslcodegen")
     51       1.1    jruoho 
     52       1.1    jruoho /* Local prototypes */
     53       1.1    jruoho 
     54       1.1    jruoho static ACPI_STATUS
     55       1.1    jruoho CgAmlWriteWalk (
     56       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
     57       1.1    jruoho     UINT32                  Level,
     58       1.1    jruoho     void                    *Context);
     59       1.1    jruoho 
     60       1.1    jruoho static void
     61       1.1    jruoho CgWriteAmlOpcode (
     62       1.1    jruoho     ACPI_PARSE_OBJECT       *Op);
     63       1.1    jruoho 
     64       1.1    jruoho static void
     65       1.1    jruoho CgWriteTableHeader (
     66       1.1    jruoho     ACPI_PARSE_OBJECT       *Op);
     67       1.1    jruoho 
     68       1.1    jruoho static void
     69       1.1    jruoho CgCloseTable (
     70       1.1    jruoho     void);
     71       1.1    jruoho 
     72       1.1    jruoho static void
     73       1.1    jruoho CgWriteNode (
     74       1.1    jruoho     ACPI_PARSE_OBJECT       *Op);
     75       1.1    jruoho 
     76       1.1    jruoho 
     77       1.1    jruoho /*******************************************************************************
     78       1.1    jruoho  *
     79       1.1    jruoho  * FUNCTION:    CgGenerateAmlOutput
     80       1.1    jruoho  *
     81       1.1    jruoho  * PARAMETERS:  None.
     82       1.1    jruoho  *
     83       1.1    jruoho  * RETURN:      None
     84       1.1    jruoho  *
     85   1.1.1.3  christos  * DESCRIPTION: Generate AML code. Currently generates the listing file
     86       1.1    jruoho  *              simultaneously.
     87       1.1    jruoho  *
     88       1.1    jruoho  ******************************************************************************/
     89       1.1    jruoho 
     90       1.1    jruoho void
     91       1.1    jruoho CgGenerateAmlOutput (
     92       1.1    jruoho     void)
     93       1.1    jruoho {
     94       1.1    jruoho 
     95       1.1    jruoho     /* Generate the AML output file */
     96       1.1    jruoho 
     97       1.1    jruoho     FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
     98       1.1    jruoho     Gbl_SourceLine = 0;
     99       1.1    jruoho     Gbl_NextError = Gbl_ErrorLog;
    100       1.1    jruoho 
    101   1.1.1.8  christos     TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_DOWNWARD,
    102       1.1    jruoho         CgAmlWriteWalk, NULL, NULL);
    103   1.1.1.5  christos 
    104   1.1.1.8  christos     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
    105       1.1    jruoho     CgCloseTable ();
    106       1.1    jruoho }
    107       1.1    jruoho 
    108       1.1    jruoho 
    109       1.1    jruoho /*******************************************************************************
    110       1.1    jruoho  *
    111       1.1    jruoho  * FUNCTION:    CgAmlWriteWalk
    112       1.1    jruoho  *
    113       1.1    jruoho  * PARAMETERS:  ASL_WALK_CALLBACK
    114       1.1    jruoho  *
    115       1.1    jruoho  * RETURN:      Status
    116       1.1    jruoho  *
    117       1.1    jruoho  * DESCRIPTION: Parse tree walk to generate the AML code.
    118       1.1    jruoho  *
    119       1.1    jruoho  ******************************************************************************/
    120       1.1    jruoho 
    121       1.1    jruoho static ACPI_STATUS
    122       1.1    jruoho CgAmlWriteWalk (
    123       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    124       1.1    jruoho     UINT32                  Level,
    125       1.1    jruoho     void                    *Context)
    126       1.1    jruoho {
    127       1.1    jruoho 
    128   1.1.1.8  christos     /* Generate the AML for this node */
    129   1.1.1.8  christos 
    130   1.1.1.8  christos     CgWriteNode (Op);
    131   1.1.1.8  christos 
    132   1.1.1.8  christos     if (!Gbl_DebugFlag)
    133       1.1    jruoho     {
    134   1.1.1.8  christos         return (AE_OK);
    135       1.1    jruoho     }
    136       1.1    jruoho 
    137   1.1.1.8  christos     /* Print header at level 0. Alignment assumes 32-bit pointers */
    138       1.1    jruoho 
    139   1.1.1.8  christos     if (!Level)
    140       1.1    jruoho     {
    141       1.1    jruoho         DbgPrint (ASL_TREE_OUTPUT,
    142   1.1.1.8  christos             "\nFinal parse tree used for AML output:\n");
    143   1.1.1.8  christos         DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
    144       1.1    jruoho     }
    145   1.1.1.8  christos 
    146   1.1.1.8  christos     /* Dump ParseOp name and possible value */
    147   1.1.1.8  christos 
    148   1.1.1.8  christos     switch (Op->Asl.ParseOpcode)
    149       1.1    jruoho     {
    150   1.1.1.8  christos     case PARSEOP_NAMESEG:
    151   1.1.1.8  christos     case PARSEOP_NAMESTRING:
    152   1.1.1.8  christos     case PARSEOP_METHODCALL:
    153   1.1.1.8  christos     case PARSEOP_STRING_LITERAL:
    154   1.1.1.8  christos 
    155   1.1.1.8  christos         UtDumpStringOp (Op, Level);
    156   1.1.1.8  christos         break;
    157   1.1.1.8  christos 
    158   1.1.1.8  christos     default:
    159   1.1.1.8  christos 
    160   1.1.1.8  christos         UtDumpBasicOp (Op, Level);
    161   1.1.1.8  christos         break;
    162       1.1    jruoho     }
    163       1.1    jruoho 
    164   1.1.1.8  christos     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_DEBUG2,
    165   1.1.1.7  christos         /* 1  */ (UINT32) Op->Asl.Value.Integer,
    166   1.1.1.7  christos         /* 2  */ Op->Asl.ParseOpcode,
    167   1.1.1.7  christos         /* 3  */ Op->Asl.AmlOpcode,
    168   1.1.1.7  christos         /* 4  */ Op->Asl.AmlOpcodeLength,
    169   1.1.1.7  christos         /* 5  */ Op->Asl.AmlPkgLenBytes,
    170   1.1.1.7  christos         /* 6  */ Op->Asl.AmlLength,
    171   1.1.1.7  christos         /* 7  */ Op->Asl.AmlSubtreeLength,
    172   1.1.1.7  christos         /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
    173   1.1.1.7  christos         /* 9  */ Op,
    174   1.1.1.7  christos         /* 10 */ Op->Asl.Parent,
    175   1.1.1.7  christos         /* 11 */ Op->Asl.Child,
    176   1.1.1.7  christos         /* 12 */ Op->Asl.Next,
    177   1.1.1.7  christos         /* 13 */ Op->Asl.CompileFlags,
    178   1.1.1.7  christos         /* 14 */ Op->Asl.AcpiBtype,
    179   1.1.1.7  christos         /* 15 */ Op->Asl.FinalAmlLength,
    180   1.1.1.7  christos         /* 16 */ Op->Asl.Column,
    181   1.1.1.7  christos         /* 17 */ Op->Asl.LineNumber,
    182   1.1.1.7  christos         /* 18 */ Op->Asl.EndLine,
    183   1.1.1.7  christos         /* 19 */ Op->Asl.LogicalLineNumber,
    184   1.1.1.7  christos         /* 20 */ Op->Asl.EndLogicalLine);
    185       1.1    jruoho 
    186  1.1.1.11  christos     TrPrintOpFlags (Op->Asl.CompileFlags, ASL_TREE_OUTPUT);
    187  1.1.1.11  christos     DbgPrint (ASL_TREE_OUTPUT, "\n");
    188       1.1    jruoho     return (AE_OK);
    189       1.1    jruoho }
    190       1.1    jruoho 
    191       1.1    jruoho 
    192       1.1    jruoho /*******************************************************************************
    193       1.1    jruoho  *
    194       1.1    jruoho  * FUNCTION:    CgLocalWriteAmlData
    195       1.1    jruoho  *
    196       1.1    jruoho  * PARAMETERS:  Op              - Current parse op
    197       1.1    jruoho  *              Buffer          - Buffer to write
    198       1.1    jruoho  *              Length          - Size of data in buffer
    199       1.1    jruoho  *
    200       1.1    jruoho  * RETURN:      None
    201       1.1    jruoho  *
    202       1.1    jruoho  * DESCRIPTION: Write a buffer of AML data to the AML output file.
    203       1.1    jruoho  *
    204       1.1    jruoho  ******************************************************************************/
    205       1.1    jruoho 
    206  1.1.1.10  christos void
    207       1.1    jruoho CgLocalWriteAmlData (
    208       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    209       1.1    jruoho     void                    *Buffer,
    210       1.1    jruoho     UINT32                  Length)
    211       1.1    jruoho {
    212       1.1    jruoho 
    213       1.1    jruoho     /* Write the raw data to the AML file */
    214       1.1    jruoho 
    215       1.1    jruoho     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
    216       1.1    jruoho 
    217       1.1    jruoho     /* Update the final AML length for this node (used for listings) */
    218       1.1    jruoho 
    219       1.1    jruoho     if (Op)
    220       1.1    jruoho     {
    221       1.1    jruoho         Op->Asl.FinalAmlLength += Length;
    222       1.1    jruoho     }
    223       1.1    jruoho }
    224       1.1    jruoho 
    225       1.1    jruoho 
    226       1.1    jruoho /*******************************************************************************
    227       1.1    jruoho  *
    228       1.1    jruoho  * FUNCTION:    CgWriteAmlOpcode
    229       1.1    jruoho  *
    230       1.1    jruoho  * PARAMETERS:  Op            - Parse node with an AML opcode
    231       1.1    jruoho  *
    232       1.1    jruoho  * RETURN:      None.
    233       1.1    jruoho  *
    234       1.1    jruoho  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
    235       1.1    jruoho  *
    236       1.1    jruoho  ******************************************************************************/
    237       1.1    jruoho 
    238       1.1    jruoho static void
    239       1.1    jruoho CgWriteAmlOpcode (
    240       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    241       1.1    jruoho {
    242       1.1    jruoho     UINT8                   PkgLenFirstByte;
    243       1.1    jruoho     UINT32                  i;
    244       1.1    jruoho     union {
    245       1.1    jruoho         UINT16                  Opcode;
    246       1.1    jruoho         UINT8                   OpcodeBytes[2];
    247       1.1    jruoho     } Aml;
    248       1.1    jruoho     union {
    249       1.1    jruoho         UINT32                  Len;
    250       1.1    jruoho         UINT8                   LenBytes[4];
    251       1.1    jruoho     } PkgLen;
    252       1.1    jruoho 
    253       1.1    jruoho 
    254       1.1    jruoho     /* We expect some DEFAULT_ARGs, just ignore them */
    255       1.1    jruoho 
    256       1.1    jruoho     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
    257       1.1    jruoho     {
    258       1.1    jruoho         return;
    259       1.1    jruoho     }
    260       1.1    jruoho 
    261  1.1.1.10  christos     /*
    262  1.1.1.10  christos      * Before printing the bytecode, generate comment byte codes
    263  1.1.1.10  christos      * associated with this node.
    264  1.1.1.10  christos      */
    265  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    266  1.1.1.10  christos     {
    267  1.1.1.10  christos         CgWriteAmlComment(Op);
    268  1.1.1.10  christos     }
    269  1.1.1.10  christos 
    270       1.1    jruoho     switch (Op->Asl.AmlOpcode)
    271       1.1    jruoho     {
    272       1.1    jruoho     case AML_UNASSIGNED_OPCODE:
    273       1.1    jruoho 
    274       1.1    jruoho         /* These opcodes should not get here */
    275       1.1    jruoho 
    276       1.1    jruoho         printf ("Found a node with an unassigned AML opcode\n");
    277   1.1.1.7  christos         FlPrintFile (ASL_FILE_STDERR,
    278   1.1.1.7  christos             "Found a node with an unassigned AML opcode\n");
    279       1.1    jruoho         return;
    280       1.1    jruoho 
    281       1.1    jruoho     case AML_INT_RESERVEDFIELD_OP:
    282       1.1    jruoho 
    283       1.1    jruoho         /* Special opcodes for within a field definition */
    284       1.1    jruoho 
    285   1.1.1.3  christos         Aml.Opcode = AML_FIELD_OFFSET_OP;
    286       1.1    jruoho         break;
    287       1.1    jruoho 
    288       1.1    jruoho     case AML_INT_ACCESSFIELD_OP:
    289       1.1    jruoho 
    290   1.1.1.3  christos         Aml.Opcode = AML_FIELD_ACCESS_OP;
    291   1.1.1.3  christos         break;
    292   1.1.1.3  christos 
    293   1.1.1.3  christos     case AML_INT_CONNECTION_OP:
    294   1.1.1.3  christos 
    295   1.1.1.3  christos         Aml.Opcode = AML_FIELD_CONNECTION_OP;
    296       1.1    jruoho         break;
    297       1.1    jruoho 
    298       1.1    jruoho     default:
    299   1.1.1.3  christos 
    300       1.1    jruoho         Aml.Opcode = Op->Asl.AmlOpcode;
    301       1.1    jruoho         break;
    302       1.1    jruoho     }
    303       1.1    jruoho 
    304       1.1    jruoho 
    305       1.1    jruoho     switch (Aml.Opcode)
    306       1.1    jruoho     {
    307       1.1    jruoho     case AML_PACKAGE_LENGTH:
    308       1.1    jruoho 
    309       1.1    jruoho         /* Value is the length to be encoded (Used in field definitions) */
    310       1.1    jruoho 
    311       1.1    jruoho         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
    312       1.1    jruoho         break;
    313       1.1    jruoho 
    314       1.1    jruoho     default:
    315       1.1    jruoho 
    316       1.1    jruoho         /* Check for two-byte opcode */
    317       1.1    jruoho 
    318       1.1    jruoho         if (Aml.Opcode > 0x00FF)
    319       1.1    jruoho         {
    320       1.1    jruoho             /* Write the high byte first */
    321       1.1    jruoho 
    322       1.1    jruoho             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
    323       1.1    jruoho         }
    324       1.1    jruoho 
    325       1.1    jruoho         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
    326       1.1    jruoho 
    327       1.1    jruoho         /* Subtreelength doesn't include length of package length bytes */
    328       1.1    jruoho 
    329       1.1    jruoho         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
    330       1.1    jruoho         break;
    331       1.1    jruoho     }
    332       1.1    jruoho 
    333       1.1    jruoho     /* Does this opcode have an associated "PackageLength" field? */
    334       1.1    jruoho 
    335  1.1.1.11  christos     if (Op->Asl.CompileFlags & OP_AML_PACKAGE)
    336       1.1    jruoho     {
    337       1.1    jruoho         if (Op->Asl.AmlPkgLenBytes == 1)
    338       1.1    jruoho         {
    339       1.1    jruoho             /* Simplest case -- no bytes to follow, just write the count */
    340       1.1    jruoho 
    341       1.1    jruoho             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
    342       1.1    jruoho         }
    343       1.1    jruoho         else if (Op->Asl.AmlPkgLenBytes != 0)
    344       1.1    jruoho         {
    345       1.1    jruoho             /*
    346       1.1    jruoho              * Encode the "bytes to follow" in the first byte, top two bits.
    347       1.1    jruoho              * The low-order nybble of the length is in the bottom 4 bits
    348       1.1    jruoho              */
    349       1.1    jruoho             PkgLenFirstByte = (UINT8)
    350       1.1    jruoho                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
    351       1.1    jruoho                 (PkgLen.LenBytes[0] & 0x0F));
    352       1.1    jruoho 
    353       1.1    jruoho             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
    354       1.1    jruoho 
    355       1.1    jruoho             /*
    356       1.1    jruoho              * Shift the length over by the 4 bits we just stuffed
    357       1.1    jruoho              * in the first byte
    358       1.1    jruoho              */
    359       1.1    jruoho             PkgLen.Len >>= 4;
    360       1.1    jruoho 
    361   1.1.1.7  christos             /*
    362   1.1.1.7  christos              * Now we can write the remaining bytes -
    363   1.1.1.7  christos              * either 1, 2, or 3 bytes
    364   1.1.1.7  christos              */
    365       1.1    jruoho             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
    366       1.1    jruoho             {
    367       1.1    jruoho                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
    368       1.1    jruoho             }
    369       1.1    jruoho         }
    370       1.1    jruoho     }
    371       1.1    jruoho 
    372       1.1    jruoho     switch (Aml.Opcode)
    373       1.1    jruoho     {
    374       1.1    jruoho     case AML_BYTE_OP:
    375       1.1    jruoho 
    376       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
    377       1.1    jruoho         break;
    378       1.1    jruoho 
    379       1.1    jruoho     case AML_WORD_OP:
    380       1.1    jruoho 
    381       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
    382       1.1    jruoho        break;
    383       1.1    jruoho 
    384       1.1    jruoho     case AML_DWORD_OP:
    385       1.1    jruoho 
    386       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
    387       1.1    jruoho         break;
    388       1.1    jruoho 
    389       1.1    jruoho     case AML_QWORD_OP:
    390       1.1    jruoho 
    391       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
    392       1.1    jruoho         break;
    393       1.1    jruoho 
    394       1.1    jruoho     case AML_STRING_OP:
    395       1.1    jruoho 
    396       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    397       1.1    jruoho         break;
    398       1.1    jruoho 
    399       1.1    jruoho     default:
    400   1.1.1.3  christos 
    401       1.1    jruoho         /* All data opcodes must appear above */
    402   1.1.1.3  christos 
    403       1.1    jruoho         break;
    404       1.1    jruoho     }
    405       1.1    jruoho }
    406       1.1    jruoho 
    407       1.1    jruoho 
    408       1.1    jruoho /*******************************************************************************
    409       1.1    jruoho  *
    410       1.1    jruoho  * FUNCTION:    CgWriteTableHeader
    411       1.1    jruoho  *
    412       1.1    jruoho  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
    413       1.1    jruoho  *
    414       1.1    jruoho  * RETURN:      None
    415       1.1    jruoho  *
    416       1.1    jruoho  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
    417       1.1    jruoho  *
    418       1.1    jruoho  ******************************************************************************/
    419       1.1    jruoho 
    420       1.1    jruoho static void
    421       1.1    jruoho CgWriteTableHeader (
    422       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    423       1.1    jruoho {
    424       1.1    jruoho     ACPI_PARSE_OBJECT       *Child;
    425  1.1.1.10  christos     UINT32                  CommentLength;
    426  1.1.1.10  christos     ACPI_COMMENT_NODE       *Current;
    427       1.1    jruoho 
    428       1.1    jruoho 
    429       1.1    jruoho     /* AML filename */
    430       1.1    jruoho 
    431       1.1    jruoho     Child = Op->Asl.Child;
    432       1.1    jruoho 
    433       1.1    jruoho     /* Signature */
    434       1.1    jruoho 
    435       1.1    jruoho     Child = Child->Asl.Next;
    436  1.1.1.10  christos 
    437  1.1.1.10  christos     /*
    438  1.1.1.10  christos      * For ASL-/ASL+ converter: replace the table signature with
    439  1.1.1.10  christos      * "XXXX" and save the original table signature. This results in an AML
    440  1.1.1.10  christos      * file with the signature "XXXX". The converter should remove this AML
    441  1.1.1.10  christos      * file. In the event where this AML file does not get deleted, the
    442  1.1.1.10  christos      * "XXXX" table signature prevents this AML file from running on the AML
    443  1.1.1.10  christos      * interpreter.
    444  1.1.1.10  christos      */
    445  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    446  1.1.1.10  christos     {
    447  1.1.1.12  christos         strncpy(AcpiGbl_TableSig, Child->Asl.Value.String, ACPI_NAME_SIZE);
    448  1.1.1.10  christos         Child->Asl.Value.String = ACPI_SIG_XXXX;
    449  1.1.1.10  christos     }
    450  1.1.1.10  christos 
    451  1.1.1.12  christos     strncpy (TableHeader.Signature, Child->Asl.Value.String, ACPI_NAME_SIZE);
    452       1.1    jruoho 
    453       1.1    jruoho     /* Revision */
    454       1.1    jruoho 
    455       1.1    jruoho     Child = Child->Asl.Next;
    456       1.1    jruoho     TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
    457       1.1    jruoho 
    458       1.1    jruoho     /* Command-line Revision override */
    459       1.1    jruoho 
    460       1.1    jruoho     if (Gbl_RevisionOverride)
    461       1.1    jruoho     {
    462       1.1    jruoho         TableHeader.Revision = Gbl_RevisionOverride;
    463       1.1    jruoho     }
    464       1.1    jruoho 
    465       1.1    jruoho     /* OEMID */
    466       1.1    jruoho 
    467       1.1    jruoho     Child = Child->Asl.Next;
    468  1.1.1.12  christos     strncpy (TableHeader.OemId, Child->Asl.Value.String, ACPI_OEM_ID_SIZE);
    469       1.1    jruoho 
    470       1.1    jruoho     /* OEM TableID */
    471       1.1    jruoho 
    472       1.1    jruoho     Child = Child->Asl.Next;
    473  1.1.1.12  christos     strncpy (TableHeader.OemTableId, Child->Asl.Value.String, ACPI_OEM_TABLE_ID_SIZE);
    474       1.1    jruoho 
    475       1.1    jruoho     /* OEM Revision */
    476       1.1    jruoho 
    477       1.1    jruoho     Child = Child->Asl.Next;
    478       1.1    jruoho     TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
    479       1.1    jruoho 
    480       1.1    jruoho     /* Compiler ID */
    481       1.1    jruoho 
    482   1.1.1.3  christos     ACPI_MOVE_NAME (TableHeader.AslCompilerId, ASL_CREATOR_ID);
    483       1.1    jruoho 
    484       1.1    jruoho     /* Compiler version */
    485       1.1    jruoho 
    486   1.1.1.5  christos     TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
    487       1.1    jruoho 
    488       1.1    jruoho     /* Table length. Checksum zero for now, will rewrite later */
    489       1.1    jruoho 
    490   1.1.1.7  christos     TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
    491   1.1.1.7  christos         Op->Asl.AmlSubtreeLength;
    492  1.1.1.10  christos 
    493  1.1.1.10  christos     /* Calculate the comment lengths for this definition block parseOp */
    494  1.1.1.10  christos 
    495  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    496  1.1.1.10  christos     {
    497  1.1.1.10  christos         CvDbgPrint ("Calculating comment lengths for %s in write header\n",
    498  1.1.1.10  christos             Op->Asl.ParseOpName);
    499  1.1.1.10  christos 
    500  1.1.1.10  christos         /*
    501  1.1.1.10  christos          * Take the filename without extensions, add 3 for the new extension
    502  1.1.1.10  christos          * and another 3 for the a908 bytecode and null terminator.
    503  1.1.1.10  christos          */
    504  1.1.1.10  christos         TableHeader.Length += strrchr (Gbl_ParseTreeRoot->Asl.Filename, '.')
    505  1.1.1.10  christos             - Gbl_ParseTreeRoot->Asl.Filename + 1 + 3 + 3;
    506  1.1.1.10  christos         Op->Asl.AmlSubtreeLength +=
    507  1.1.1.10  christos             strlen (Gbl_ParseTreeRoot->Asl.Filename) + 3;
    508  1.1.1.10  christos     CvDbgPrint ("     Length: %lu\n",
    509  1.1.1.10  christos             strlen (Gbl_ParseTreeRoot->Asl.Filename) + 3);
    510  1.1.1.10  christos 
    511  1.1.1.10  christos         if (Op->Asl.CommentList)
    512  1.1.1.10  christos         {
    513  1.1.1.10  christos             Current = Op->Asl.CommentList;
    514  1.1.1.10  christos             while (Current)
    515  1.1.1.10  christos             {
    516  1.1.1.10  christos                 CommentLength = strlen (Current->Comment)+3;
    517  1.1.1.10  christos                 CvDbgPrint ("Length of standard comment): %d\n", CommentLength);
    518  1.1.1.10  christos                 CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
    519  1.1.1.10  christos                 TableHeader.Length += CommentLength;
    520  1.1.1.10  christos                 Op->Asl.AmlSubtreeLength += CommentLength;
    521  1.1.1.10  christos                 Current = Current->Next;
    522  1.1.1.10  christos             CvDbgPrint ("    Length: %u\n", CommentLength);
    523  1.1.1.10  christos             }
    524  1.1.1.10  christos         }
    525  1.1.1.10  christos         if (Op->Asl.CloseBraceComment)
    526  1.1.1.10  christos         {
    527  1.1.1.10  christos             CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
    528  1.1.1.10  christos             CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength);
    529  1.1.1.10  christos             CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
    530  1.1.1.10  christos             TableHeader.Length += CommentLength;
    531  1.1.1.10  christos             Op->Asl.AmlSubtreeLength += CommentLength;
    532  1.1.1.10  christos         CvDbgPrint ("    Length: %u\n", CommentLength);
    533  1.1.1.10  christos         }
    534  1.1.1.10  christos     }
    535  1.1.1.10  christos 
    536       1.1    jruoho     TableHeader.Checksum = 0;
    537       1.1    jruoho 
    538   1.1.1.7  christos     Op->Asl.FinalAmlOffset = ftell (Gbl_Files[ASL_FILE_AML_OUTPUT].Handle);
    539   1.1.1.7  christos 
    540   1.1.1.7  christos     /* Write entire header and clear the table header global */
    541   1.1.1.7  christos 
    542       1.1    jruoho     CgLocalWriteAmlData (Op, &TableHeader, sizeof (ACPI_TABLE_HEADER));
    543   1.1.1.7  christos     memset (&TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
    544       1.1    jruoho }
    545       1.1    jruoho 
    546       1.1    jruoho 
    547       1.1    jruoho /*******************************************************************************
    548       1.1    jruoho  *
    549   1.1.1.7  christos  * FUNCTION:    CgUpdateHeader
    550       1.1    jruoho  *
    551   1.1.1.7  christos  * PARAMETERS:  Op                  - Op for the Definition Block
    552       1.1    jruoho  *
    553       1.1    jruoho  * RETURN:      None.
    554       1.1    jruoho  *
    555       1.1    jruoho  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
    556   1.1.1.7  christos  *              re-writing the header for the input definition block
    557       1.1    jruoho  *
    558       1.1    jruoho  ******************************************************************************/
    559       1.1    jruoho 
    560       1.1    jruoho static void
    561   1.1.1.7  christos CgUpdateHeader (
    562  1.1.1.10  christos     ACPI_PARSE_OBJECT       *Op)
    563       1.1    jruoho {
    564  1.1.1.10  christos     signed char             Sum;
    565  1.1.1.10  christos     UINT32                  i;
    566  1.1.1.10  christos     UINT32                  Length;
    567  1.1.1.10  christos     UINT8                   FileByte;
    568  1.1.1.10  christos     UINT8                   Checksum;
    569       1.1    jruoho 
    570       1.1    jruoho 
    571   1.1.1.7  christos     /* Calculate the checksum over the entire definition block */
    572       1.1    jruoho 
    573   1.1.1.7  christos     Sum = 0;
    574   1.1.1.7  christos     Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
    575   1.1.1.7  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
    576       1.1    jruoho 
    577   1.1.1.7  christos     for (i = 0; i < Length; i++)
    578       1.1    jruoho     {
    579   1.1.1.7  christos         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
    580   1.1.1.7  christos         {
    581   1.1.1.7  christos             printf ("EOF while reading checksum bytes\n");
    582   1.1.1.7  christos             return;
    583   1.1.1.7  christos         }
    584   1.1.1.7  christos 
    585       1.1    jruoho         Sum = (signed char) (Sum + FileByte);
    586       1.1    jruoho     }
    587       1.1    jruoho 
    588   1.1.1.7  christos     Checksum = (UINT8) (0 - Sum);
    589   1.1.1.7  christos 
    590   1.1.1.7  christos     /* Re-write the the checksum byte */
    591       1.1    jruoho 
    592   1.1.1.7  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
    593   1.1.1.7  christos         ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
    594       1.1    jruoho 
    595   1.1.1.7  christos     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
    596   1.1.1.7  christos }
    597   1.1.1.7  christos 
    598   1.1.1.7  christos 
    599   1.1.1.7  christos /*******************************************************************************
    600   1.1.1.7  christos  *
    601   1.1.1.7  christos  * FUNCTION:    CgCloseTable
    602   1.1.1.7  christos  *
    603   1.1.1.7  christos  * PARAMETERS:  None.
    604   1.1.1.7  christos  *
    605   1.1.1.7  christos  * RETURN:      None.
    606   1.1.1.7  christos  *
    607   1.1.1.7  christos  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
    608   1.1.1.7  christos  *              re-writing each table header. This allows support for
    609   1.1.1.7  christos  *              multiple definition blocks in a single source file.
    610   1.1.1.7  christos  *
    611   1.1.1.7  christos  ******************************************************************************/
    612   1.1.1.7  christos 
    613   1.1.1.7  christos static void
    614   1.1.1.7  christos CgCloseTable (
    615   1.1.1.7  christos     void)
    616   1.1.1.7  christos {
    617   1.1.1.7  christos     ACPI_PARSE_OBJECT   *Op;
    618   1.1.1.7  christos 
    619   1.1.1.7  christos 
    620   1.1.1.7  christos     /* Process all definition blocks */
    621   1.1.1.7  christos 
    622   1.1.1.8  christos     Op = Gbl_ParseTreeRoot->Asl.Child;
    623   1.1.1.7  christos     while (Op)
    624   1.1.1.7  christos     {
    625   1.1.1.7  christos         CgUpdateHeader (Op);
    626   1.1.1.7  christos         Op = Op->Asl.Next;
    627   1.1.1.7  christos     }
    628       1.1    jruoho }
    629       1.1    jruoho 
    630       1.1    jruoho 
    631       1.1    jruoho /*******************************************************************************
    632       1.1    jruoho  *
    633       1.1    jruoho  * FUNCTION:    CgWriteNode
    634       1.1    jruoho  *
    635       1.1    jruoho  * PARAMETERS:  Op            - Parse node to write.
    636       1.1    jruoho  *
    637       1.1    jruoho  * RETURN:      None.
    638       1.1    jruoho  *
    639       1.1    jruoho  * DESCRIPTION: Write the AML that corresponds to a parse node.
    640       1.1    jruoho  *
    641       1.1    jruoho  ******************************************************************************/
    642       1.1    jruoho 
    643       1.1    jruoho static void
    644       1.1    jruoho CgWriteNode (
    645       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    646       1.1    jruoho {
    647       1.1    jruoho     ASL_RESOURCE_NODE       *Rnode;
    648       1.1    jruoho 
    649       1.1    jruoho 
    650  1.1.1.10  christos     /* Write all comments here. */
    651  1.1.1.12  christos 
    652  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    653  1.1.1.10  christos     {
    654  1.1.1.10  christos         CgWriteAmlComment(Op);
    655  1.1.1.10  christos     }
    656  1.1.1.10  christos 
    657       1.1    jruoho     /* Always check for DEFAULT_ARG and other "Noop" nodes */
    658       1.1    jruoho     /* TBD: this may not be the best place for this check */
    659       1.1    jruoho 
    660       1.1    jruoho     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
    661       1.1    jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
    662       1.1    jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
    663       1.1    jruoho     {
    664       1.1    jruoho         return;
    665       1.1    jruoho     }
    666       1.1    jruoho 
    667   1.1.1.8  christos     if ((Op->Asl.ParseOpcode == PARSEOP_EXTERNAL) &&
    668   1.1.1.8  christos         Gbl_DoExternals == FALSE)
    669   1.1.1.8  christos     {
    670   1.1.1.8  christos         return;
    671   1.1.1.8  christos     }
    672   1.1.1.8  christos 
    673       1.1    jruoho     Op->Asl.FinalAmlLength = 0;
    674       1.1    jruoho 
    675       1.1    jruoho     switch (Op->Asl.AmlOpcode)
    676       1.1    jruoho     {
    677       1.1    jruoho     case AML_RAW_DATA_BYTE:
    678       1.1    jruoho     case AML_RAW_DATA_WORD:
    679       1.1    jruoho     case AML_RAW_DATA_DWORD:
    680       1.1    jruoho     case AML_RAW_DATA_QWORD:
    681       1.1    jruoho 
    682       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
    683       1.1    jruoho         return;
    684       1.1    jruoho 
    685       1.1    jruoho 
    686       1.1    jruoho     case AML_RAW_DATA_BUFFER:
    687       1.1    jruoho 
    688       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
    689       1.1    jruoho         return;
    690       1.1    jruoho 
    691       1.1    jruoho 
    692       1.1    jruoho     case AML_RAW_DATA_CHAIN:
    693       1.1    jruoho 
    694       1.1    jruoho         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
    695       1.1    jruoho         while (Rnode)
    696       1.1    jruoho         {
    697       1.1    jruoho             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
    698       1.1    jruoho             Rnode = Rnode->Next;
    699       1.1    jruoho         }
    700       1.1    jruoho         return;
    701       1.1    jruoho 
    702       1.1    jruoho     default:
    703   1.1.1.3  christos 
    704       1.1    jruoho         /* Internal data opcodes must all appear above */
    705   1.1.1.3  christos 
    706       1.1    jruoho         break;
    707       1.1    jruoho     }
    708       1.1    jruoho 
    709       1.1    jruoho     switch (Op->Asl.ParseOpcode)
    710       1.1    jruoho     {
    711       1.1    jruoho     case PARSEOP_DEFAULT_ARG:
    712       1.1    jruoho 
    713       1.1    jruoho         break;
    714       1.1    jruoho 
    715   1.1.1.7  christos     case PARSEOP_DEFINITION_BLOCK:
    716       1.1    jruoho 
    717       1.1    jruoho         CgWriteTableHeader (Op);
    718  1.1.1.12  christos         if (AcpiGbl_CaptureComments)
    719  1.1.1.10  christos         {
    720  1.1.1.10  christos             CgWriteAmlDefBlockComment (Op);
    721  1.1.1.10  christos         }
    722       1.1    jruoho         break;
    723       1.1    jruoho 
    724       1.1    jruoho     case PARSEOP_NAMESEG:
    725       1.1    jruoho     case PARSEOP_NAMESTRING:
    726       1.1    jruoho     case PARSEOP_METHODCALL:
    727       1.1    jruoho 
    728       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    729       1.1    jruoho         break;
    730       1.1    jruoho 
    731       1.1    jruoho     default:
    732       1.1    jruoho 
    733       1.1    jruoho         CgWriteAmlOpcode (Op);
    734       1.1    jruoho         break;
    735       1.1    jruoho     }
    736       1.1    jruoho }
    737