Home | History | Annotate | Line # | Download | only in compiler
aslcodegen.c revision 1.1.1.18
      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.18  christos  * Copyright (C) 2000 - 2021, 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.18  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 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.1.14  christos CgWriteNode (
     70  1.1.1.14  christos     ACPI_PARSE_OBJECT       *Op);
     71       1.1    jruoho 
     72       1.1    jruoho static void
     73  1.1.1.14  christos CgUpdateHeader (
     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.1.14  christos     TrWalkParseTree (AslGbl_CurrentDB,
     98  1.1.1.14  christos         ASL_WALK_VISIT_DOWNWARD | ASL_WALK_VISIT_DB_SEPARATELY,
     99       1.1    jruoho         CgAmlWriteWalk, NULL, NULL);
    100   1.1.1.5  christos 
    101   1.1.1.8  christos     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
    102  1.1.1.14  christos     CgUpdateHeader (AslGbl_CurrentDB);
    103       1.1    jruoho }
    104       1.1    jruoho 
    105       1.1    jruoho 
    106       1.1    jruoho /*******************************************************************************
    107       1.1    jruoho  *
    108       1.1    jruoho  * FUNCTION:    CgAmlWriteWalk
    109       1.1    jruoho  *
    110       1.1    jruoho  * PARAMETERS:  ASL_WALK_CALLBACK
    111       1.1    jruoho  *
    112       1.1    jruoho  * RETURN:      Status
    113       1.1    jruoho  *
    114       1.1    jruoho  * DESCRIPTION: Parse tree walk to generate the AML code.
    115       1.1    jruoho  *
    116       1.1    jruoho  ******************************************************************************/
    117       1.1    jruoho 
    118       1.1    jruoho static ACPI_STATUS
    119       1.1    jruoho CgAmlWriteWalk (
    120       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    121       1.1    jruoho     UINT32                  Level,
    122       1.1    jruoho     void                    *Context)
    123       1.1    jruoho {
    124       1.1    jruoho 
    125   1.1.1.8  christos     /* Generate the AML for this node */
    126   1.1.1.8  christos 
    127   1.1.1.8  christos     CgWriteNode (Op);
    128   1.1.1.8  christos 
    129  1.1.1.13  christos     if (!AslGbl_DebugFlag)
    130       1.1    jruoho     {
    131   1.1.1.8  christos         return (AE_OK);
    132       1.1    jruoho     }
    133       1.1    jruoho 
    134   1.1.1.8  christos     /* Print header at level 0. Alignment assumes 32-bit pointers */
    135       1.1    jruoho 
    136   1.1.1.8  christos     if (!Level)
    137       1.1    jruoho     {
    138       1.1    jruoho         DbgPrint (ASL_TREE_OUTPUT,
    139   1.1.1.8  christos             "\nFinal parse tree used for AML output:\n");
    140   1.1.1.8  christos         DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_HEADER2);
    141       1.1    jruoho     }
    142   1.1.1.8  christos 
    143   1.1.1.8  christos     /* Dump ParseOp name and possible value */
    144   1.1.1.8  christos 
    145   1.1.1.8  christos     switch (Op->Asl.ParseOpcode)
    146       1.1    jruoho     {
    147   1.1.1.8  christos     case PARSEOP_NAMESEG:
    148   1.1.1.8  christos     case PARSEOP_NAMESTRING:
    149   1.1.1.8  christos     case PARSEOP_METHODCALL:
    150   1.1.1.8  christos     case PARSEOP_STRING_LITERAL:
    151   1.1.1.8  christos 
    152   1.1.1.8  christos         UtDumpStringOp (Op, Level);
    153   1.1.1.8  christos         break;
    154   1.1.1.8  christos 
    155   1.1.1.8  christos     default:
    156   1.1.1.8  christos 
    157   1.1.1.8  christos         UtDumpBasicOp (Op, Level);
    158   1.1.1.8  christos         break;
    159       1.1    jruoho     }
    160       1.1    jruoho 
    161   1.1.1.8  christos     DbgPrint (ASL_TREE_OUTPUT, ASL_PARSE_TREE_DEBUG2,
    162   1.1.1.7  christos         /* 1  */ (UINT32) Op->Asl.Value.Integer,
    163   1.1.1.7  christos         /* 2  */ Op->Asl.ParseOpcode,
    164   1.1.1.7  christos         /* 3  */ Op->Asl.AmlOpcode,
    165   1.1.1.7  christos         /* 4  */ Op->Asl.AmlOpcodeLength,
    166   1.1.1.7  christos         /* 5  */ Op->Asl.AmlPkgLenBytes,
    167   1.1.1.7  christos         /* 6  */ Op->Asl.AmlLength,
    168   1.1.1.7  christos         /* 7  */ Op->Asl.AmlSubtreeLength,
    169   1.1.1.7  christos         /* 8  */ Op->Asl.Parent ? Op->Asl.Parent->Asl.AmlSubtreeLength : 0,
    170   1.1.1.7  christos         /* 9  */ Op,
    171   1.1.1.7  christos         /* 10 */ Op->Asl.Parent,
    172   1.1.1.7  christos         /* 11 */ Op->Asl.Child,
    173   1.1.1.7  christos         /* 12 */ Op->Asl.Next,
    174   1.1.1.7  christos         /* 13 */ Op->Asl.CompileFlags,
    175   1.1.1.7  christos         /* 14 */ Op->Asl.AcpiBtype,
    176   1.1.1.7  christos         /* 15 */ Op->Asl.FinalAmlLength,
    177   1.1.1.7  christos         /* 16 */ Op->Asl.Column,
    178   1.1.1.7  christos         /* 17 */ Op->Asl.LineNumber,
    179   1.1.1.7  christos         /* 18 */ Op->Asl.EndLine,
    180   1.1.1.7  christos         /* 19 */ Op->Asl.LogicalLineNumber,
    181   1.1.1.7  christos         /* 20 */ Op->Asl.EndLogicalLine);
    182       1.1    jruoho 
    183  1.1.1.11  christos     TrPrintOpFlags (Op->Asl.CompileFlags, ASL_TREE_OUTPUT);
    184  1.1.1.11  christos     DbgPrint (ASL_TREE_OUTPUT, "\n");
    185       1.1    jruoho     return (AE_OK);
    186       1.1    jruoho }
    187       1.1    jruoho 
    188       1.1    jruoho 
    189       1.1    jruoho /*******************************************************************************
    190       1.1    jruoho  *
    191       1.1    jruoho  * FUNCTION:    CgLocalWriteAmlData
    192       1.1    jruoho  *
    193       1.1    jruoho  * PARAMETERS:  Op              - Current parse op
    194       1.1    jruoho  *              Buffer          - Buffer to write
    195       1.1    jruoho  *              Length          - Size of data in buffer
    196       1.1    jruoho  *
    197       1.1    jruoho  * RETURN:      None
    198       1.1    jruoho  *
    199       1.1    jruoho  * DESCRIPTION: Write a buffer of AML data to the AML output file.
    200       1.1    jruoho  *
    201       1.1    jruoho  ******************************************************************************/
    202       1.1    jruoho 
    203  1.1.1.10  christos void
    204       1.1    jruoho CgLocalWriteAmlData (
    205       1.1    jruoho     ACPI_PARSE_OBJECT       *Op,
    206       1.1    jruoho     void                    *Buffer,
    207       1.1    jruoho     UINT32                  Length)
    208       1.1    jruoho {
    209       1.1    jruoho 
    210       1.1    jruoho     /* Write the raw data to the AML file */
    211       1.1    jruoho 
    212       1.1    jruoho     FlWriteFile (ASL_FILE_AML_OUTPUT, Buffer, Length);
    213       1.1    jruoho 
    214       1.1    jruoho     /* Update the final AML length for this node (used for listings) */
    215       1.1    jruoho 
    216       1.1    jruoho     if (Op)
    217       1.1    jruoho     {
    218       1.1    jruoho         Op->Asl.FinalAmlLength += Length;
    219       1.1    jruoho     }
    220       1.1    jruoho }
    221       1.1    jruoho 
    222       1.1    jruoho 
    223       1.1    jruoho /*******************************************************************************
    224       1.1    jruoho  *
    225       1.1    jruoho  * FUNCTION:    CgWriteAmlOpcode
    226       1.1    jruoho  *
    227       1.1    jruoho  * PARAMETERS:  Op            - Parse node with an AML opcode
    228       1.1    jruoho  *
    229       1.1    jruoho  * RETURN:      None.
    230       1.1    jruoho  *
    231       1.1    jruoho  * DESCRIPTION: Write the AML opcode corresponding to a parse node.
    232       1.1    jruoho  *
    233       1.1    jruoho  ******************************************************************************/
    234       1.1    jruoho 
    235       1.1    jruoho static void
    236       1.1    jruoho CgWriteAmlOpcode (
    237       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    238       1.1    jruoho {
    239       1.1    jruoho     UINT8                   PkgLenFirstByte;
    240       1.1    jruoho     UINT32                  i;
    241       1.1    jruoho     union {
    242       1.1    jruoho         UINT16                  Opcode;
    243       1.1    jruoho         UINT8                   OpcodeBytes[2];
    244       1.1    jruoho     } Aml;
    245       1.1    jruoho     union {
    246       1.1    jruoho         UINT32                  Len;
    247       1.1    jruoho         UINT8                   LenBytes[4];
    248       1.1    jruoho     } PkgLen;
    249       1.1    jruoho 
    250       1.1    jruoho 
    251       1.1    jruoho     /* We expect some DEFAULT_ARGs, just ignore them */
    252       1.1    jruoho 
    253       1.1    jruoho     if (Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
    254       1.1    jruoho     {
    255       1.1    jruoho         return;
    256       1.1    jruoho     }
    257       1.1    jruoho 
    258  1.1.1.10  christos     /*
    259  1.1.1.10  christos      * Before printing the bytecode, generate comment byte codes
    260  1.1.1.10  christos      * associated with this node.
    261  1.1.1.10  christos      */
    262  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    263  1.1.1.10  christos     {
    264  1.1.1.10  christos         CgWriteAmlComment(Op);
    265  1.1.1.10  christos     }
    266  1.1.1.10  christos 
    267       1.1    jruoho     switch (Op->Asl.AmlOpcode)
    268       1.1    jruoho     {
    269       1.1    jruoho     case AML_UNASSIGNED_OPCODE:
    270       1.1    jruoho 
    271       1.1    jruoho         /* These opcodes should not get here */
    272       1.1    jruoho 
    273       1.1    jruoho         printf ("Found a node with an unassigned AML opcode\n");
    274   1.1.1.7  christos         FlPrintFile (ASL_FILE_STDERR,
    275   1.1.1.7  christos             "Found a node with an unassigned AML opcode\n");
    276       1.1    jruoho         return;
    277       1.1    jruoho 
    278       1.1    jruoho     case AML_INT_RESERVEDFIELD_OP:
    279       1.1    jruoho 
    280       1.1    jruoho         /* Special opcodes for within a field definition */
    281       1.1    jruoho 
    282   1.1.1.3  christos         Aml.Opcode = AML_FIELD_OFFSET_OP;
    283       1.1    jruoho         break;
    284       1.1    jruoho 
    285       1.1    jruoho     case AML_INT_ACCESSFIELD_OP:
    286       1.1    jruoho 
    287   1.1.1.3  christos         Aml.Opcode = AML_FIELD_ACCESS_OP;
    288   1.1.1.3  christos         break;
    289   1.1.1.3  christos 
    290   1.1.1.3  christos     case AML_INT_CONNECTION_OP:
    291   1.1.1.3  christos 
    292   1.1.1.3  christos         Aml.Opcode = AML_FIELD_CONNECTION_OP;
    293       1.1    jruoho         break;
    294       1.1    jruoho 
    295       1.1    jruoho     default:
    296   1.1.1.3  christos 
    297       1.1    jruoho         Aml.Opcode = Op->Asl.AmlOpcode;
    298       1.1    jruoho         break;
    299       1.1    jruoho     }
    300       1.1    jruoho 
    301       1.1    jruoho 
    302       1.1    jruoho     switch (Aml.Opcode)
    303       1.1    jruoho     {
    304       1.1    jruoho     case AML_PACKAGE_LENGTH:
    305       1.1    jruoho 
    306       1.1    jruoho         /* Value is the length to be encoded (Used in field definitions) */
    307       1.1    jruoho 
    308       1.1    jruoho         PkgLen.Len = (UINT32) Op->Asl.Value.Integer;
    309       1.1    jruoho         break;
    310       1.1    jruoho 
    311       1.1    jruoho     default:
    312       1.1    jruoho 
    313       1.1    jruoho         /* Check for two-byte opcode */
    314       1.1    jruoho 
    315       1.1    jruoho         if (Aml.Opcode > 0x00FF)
    316       1.1    jruoho         {
    317       1.1    jruoho             /* Write the high byte first */
    318       1.1    jruoho 
    319       1.1    jruoho             CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[1], 1);
    320       1.1    jruoho         }
    321       1.1    jruoho 
    322       1.1    jruoho         CgLocalWriteAmlData (Op, &Aml.OpcodeBytes[0], 1);
    323       1.1    jruoho 
    324       1.1    jruoho         /* Subtreelength doesn't include length of package length bytes */
    325       1.1    jruoho 
    326       1.1    jruoho         PkgLen.Len = Op->Asl.AmlSubtreeLength + Op->Asl.AmlPkgLenBytes;
    327       1.1    jruoho         break;
    328       1.1    jruoho     }
    329       1.1    jruoho 
    330       1.1    jruoho     /* Does this opcode have an associated "PackageLength" field? */
    331       1.1    jruoho 
    332  1.1.1.11  christos     if (Op->Asl.CompileFlags & OP_AML_PACKAGE)
    333       1.1    jruoho     {
    334       1.1    jruoho         if (Op->Asl.AmlPkgLenBytes == 1)
    335       1.1    jruoho         {
    336       1.1    jruoho             /* Simplest case -- no bytes to follow, just write the count */
    337       1.1    jruoho 
    338       1.1    jruoho             CgLocalWriteAmlData (Op, &PkgLen.LenBytes[0], 1);
    339       1.1    jruoho         }
    340       1.1    jruoho         else if (Op->Asl.AmlPkgLenBytes != 0)
    341       1.1    jruoho         {
    342       1.1    jruoho             /*
    343       1.1    jruoho              * Encode the "bytes to follow" in the first byte, top two bits.
    344       1.1    jruoho              * The low-order nybble of the length is in the bottom 4 bits
    345       1.1    jruoho              */
    346       1.1    jruoho             PkgLenFirstByte = (UINT8)
    347       1.1    jruoho                 (((UINT32) (Op->Asl.AmlPkgLenBytes - 1) << 6) |
    348       1.1    jruoho                 (PkgLen.LenBytes[0] & 0x0F));
    349       1.1    jruoho 
    350       1.1    jruoho             CgLocalWriteAmlData (Op, &PkgLenFirstByte, 1);
    351       1.1    jruoho 
    352       1.1    jruoho             /*
    353       1.1    jruoho              * Shift the length over by the 4 bits we just stuffed
    354       1.1    jruoho              * in the first byte
    355       1.1    jruoho              */
    356       1.1    jruoho             PkgLen.Len >>= 4;
    357       1.1    jruoho 
    358   1.1.1.7  christos             /*
    359   1.1.1.7  christos              * Now we can write the remaining bytes -
    360   1.1.1.7  christos              * either 1, 2, or 3 bytes
    361   1.1.1.7  christos              */
    362       1.1    jruoho             for (i = 0; i < (UINT32) (Op->Asl.AmlPkgLenBytes - 1); i++)
    363       1.1    jruoho             {
    364       1.1    jruoho                 CgLocalWriteAmlData (Op, &PkgLen.LenBytes[i], 1);
    365       1.1    jruoho             }
    366       1.1    jruoho         }
    367       1.1    jruoho     }
    368       1.1    jruoho 
    369       1.1    jruoho     switch (Aml.Opcode)
    370       1.1    jruoho     {
    371       1.1    jruoho     case AML_BYTE_OP:
    372       1.1    jruoho 
    373       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 1);
    374       1.1    jruoho         break;
    375       1.1    jruoho 
    376       1.1    jruoho     case AML_WORD_OP:
    377       1.1    jruoho 
    378       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 2);
    379       1.1    jruoho        break;
    380       1.1    jruoho 
    381       1.1    jruoho     case AML_DWORD_OP:
    382       1.1    jruoho 
    383       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 4);
    384       1.1    jruoho         break;
    385       1.1    jruoho 
    386       1.1    jruoho     case AML_QWORD_OP:
    387       1.1    jruoho 
    388       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, 8);
    389       1.1    jruoho         break;
    390       1.1    jruoho 
    391       1.1    jruoho     case AML_STRING_OP:
    392       1.1    jruoho 
    393       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    394       1.1    jruoho         break;
    395       1.1    jruoho 
    396       1.1    jruoho     default:
    397   1.1.1.3  christos 
    398       1.1    jruoho         /* All data opcodes must appear above */
    399   1.1.1.3  christos 
    400       1.1    jruoho         break;
    401       1.1    jruoho     }
    402       1.1    jruoho }
    403       1.1    jruoho 
    404       1.1    jruoho 
    405       1.1    jruoho /*******************************************************************************
    406       1.1    jruoho  *
    407       1.1    jruoho  * FUNCTION:    CgWriteTableHeader
    408       1.1    jruoho  *
    409       1.1    jruoho  * PARAMETERS:  Op        - The DEFINITIONBLOCK node
    410       1.1    jruoho  *
    411       1.1    jruoho  * RETURN:      None
    412       1.1    jruoho  *
    413       1.1    jruoho  * DESCRIPTION: Write a table header corresponding to the DEFINITIONBLOCK
    414       1.1    jruoho  *
    415  1.1.1.14  christos  * NOTE: Input strings should be validated before this function is invoked.
    416  1.1.1.14  christos  *
    417       1.1    jruoho  ******************************************************************************/
    418       1.1    jruoho 
    419       1.1    jruoho static void
    420       1.1    jruoho CgWriteTableHeader (
    421       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    422       1.1    jruoho {
    423       1.1    jruoho     ACPI_PARSE_OBJECT       *Child;
    424  1.1.1.10  christos     UINT32                  CommentLength;
    425  1.1.1.10  christos     ACPI_COMMENT_NODE       *Current;
    426       1.1    jruoho 
    427       1.1    jruoho 
    428  1.1.1.14  christos     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
    429  1.1.1.14  christos 
    430       1.1    jruoho     /* AML filename */
    431       1.1    jruoho 
    432       1.1    jruoho     Child = Op->Asl.Child;
    433       1.1    jruoho 
    434       1.1    jruoho     /* Signature */
    435       1.1    jruoho 
    436       1.1    jruoho     Child = Child->Asl.Next;
    437  1.1.1.10  christos 
    438  1.1.1.10  christos     /*
    439  1.1.1.10  christos      * For ASL-/ASL+ converter: replace the table signature with
    440  1.1.1.10  christos      * "XXXX" and save the original table signature. This results in an AML
    441  1.1.1.10  christos      * file with the signature "XXXX". The converter should remove this AML
    442  1.1.1.10  christos      * file. In the event where this AML file does not get deleted, the
    443  1.1.1.10  christos      * "XXXX" table signature prevents this AML file from running on the AML
    444  1.1.1.10  christos      * interpreter.
    445  1.1.1.10  christos      */
    446  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    447  1.1.1.10  christos     {
    448  1.1.1.14  christos         ACPI_COPY_NAMESEG (AcpiGbl_TableSig, Child->Asl.Value.String);
    449  1.1.1.10  christos         Child->Asl.Value.String = ACPI_SIG_XXXX;
    450  1.1.1.10  christos     }
    451  1.1.1.10  christos 
    452  1.1.1.14  christos     ACPI_COPY_NAMESEG (AslGbl_TableHeader.Signature, Child->Asl.Value.String);
    453       1.1    jruoho 
    454       1.1    jruoho     /* Revision */
    455       1.1    jruoho 
    456       1.1    jruoho     Child = Child->Asl.Next;
    457  1.1.1.13  christos     AslGbl_TableHeader.Revision = (UINT8) Child->Asl.Value.Integer;
    458       1.1    jruoho 
    459       1.1    jruoho     /* Command-line Revision override */
    460       1.1    jruoho 
    461  1.1.1.13  christos     if (AslGbl_RevisionOverride)
    462       1.1    jruoho     {
    463  1.1.1.13  christos         AslGbl_TableHeader.Revision = AslGbl_RevisionOverride;
    464       1.1    jruoho     }
    465       1.1    jruoho 
    466       1.1    jruoho     /* OEMID */
    467       1.1    jruoho 
    468       1.1    jruoho     Child = Child->Asl.Next;
    469  1.1.1.14  christos     memcpy (AslGbl_TableHeader.OemId, Child->Asl.Value.String,
    470  1.1.1.14  christos         strlen (Child->Asl.Value.String));
    471       1.1    jruoho 
    472       1.1    jruoho     /* OEM TableID */
    473       1.1    jruoho 
    474       1.1    jruoho     Child = Child->Asl.Next;
    475  1.1.1.14  christos     memcpy (AslGbl_TableHeader.OemTableId, Child->Asl.Value.String,
    476  1.1.1.14  christos         strlen (Child->Asl.Value.String));
    477       1.1    jruoho 
    478       1.1    jruoho     /* OEM Revision */
    479       1.1    jruoho 
    480       1.1    jruoho     Child = Child->Asl.Next;
    481  1.1.1.13  christos     AslGbl_TableHeader.OemRevision = (UINT32) Child->Asl.Value.Integer;
    482       1.1    jruoho 
    483       1.1    jruoho     /* Compiler ID */
    484       1.1    jruoho 
    485  1.1.1.14  christos     ACPI_COPY_NAMESEG (AslGbl_TableHeader.AslCompilerId, ASL_CREATOR_ID);
    486       1.1    jruoho 
    487       1.1    jruoho     /* Compiler version */
    488       1.1    jruoho 
    489  1.1.1.13  christos     AslGbl_TableHeader.AslCompilerRevision = ACPI_CA_VERSION;
    490       1.1    jruoho 
    491       1.1    jruoho     /* Table length. Checksum zero for now, will rewrite later */
    492       1.1    jruoho 
    493  1.1.1.13  christos     AslGbl_TableHeader.Length = sizeof (ACPI_TABLE_HEADER) +
    494   1.1.1.7  christos         Op->Asl.AmlSubtreeLength;
    495  1.1.1.10  christos 
    496  1.1.1.10  christos     /* Calculate the comment lengths for this definition block parseOp */
    497  1.1.1.10  christos 
    498  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    499  1.1.1.10  christos     {
    500  1.1.1.10  christos         CvDbgPrint ("Calculating comment lengths for %s in write header\n",
    501  1.1.1.10  christos             Op->Asl.ParseOpName);
    502  1.1.1.10  christos 
    503  1.1.1.10  christos         /*
    504  1.1.1.10  christos          * Take the filename without extensions, add 3 for the new extension
    505  1.1.1.10  christos          * and another 3 for the a908 bytecode and null terminator.
    506  1.1.1.10  christos          */
    507  1.1.1.13  christos         AslGbl_TableHeader.Length += strrchr (AslGbl_ParseTreeRoot->Asl.Filename, '.')
    508  1.1.1.13  christos             - AslGbl_ParseTreeRoot->Asl.Filename + 1 + 3 + 3;
    509  1.1.1.13  christos 
    510  1.1.1.10  christos         Op->Asl.AmlSubtreeLength +=
    511  1.1.1.13  christos             strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3;
    512  1.1.1.13  christos 
    513  1.1.1.15  christos         CvDbgPrint ("     Length: %u\n",
    514  1.1.1.15  christos             (UINT32) strlen (AslGbl_ParseTreeRoot->Asl.Filename) + 3);
    515  1.1.1.10  christos 
    516  1.1.1.10  christos         if (Op->Asl.CommentList)
    517  1.1.1.10  christos         {
    518  1.1.1.10  christos             Current = Op->Asl.CommentList;
    519  1.1.1.10  christos             while (Current)
    520  1.1.1.10  christos             {
    521  1.1.1.10  christos                 CommentLength = strlen (Current->Comment)+3;
    522  1.1.1.10  christos                 CvDbgPrint ("Length of standard comment): %d\n", CommentLength);
    523  1.1.1.10  christos                 CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
    524  1.1.1.13  christos                 AslGbl_TableHeader.Length += CommentLength;
    525  1.1.1.10  christos                 Op->Asl.AmlSubtreeLength += CommentLength;
    526  1.1.1.10  christos                 Current = Current->Next;
    527  1.1.1.13  christos                 CvDbgPrint ("    Length: %u\n", CommentLength);
    528  1.1.1.10  christos             }
    529  1.1.1.10  christos         }
    530  1.1.1.10  christos         if (Op->Asl.CloseBraceComment)
    531  1.1.1.10  christos         {
    532  1.1.1.10  christos             CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
    533  1.1.1.10  christos             CvDbgPrint ("Length of inline comment +3: %d\n", CommentLength);
    534  1.1.1.10  christos             CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
    535  1.1.1.13  christos             AslGbl_TableHeader.Length += CommentLength;
    536  1.1.1.10  christos             Op->Asl.AmlSubtreeLength += CommentLength;
    537  1.1.1.13  christos             CvDbgPrint ("    Length: %u\n", CommentLength);
    538  1.1.1.10  christos         }
    539  1.1.1.10  christos     }
    540  1.1.1.10  christos 
    541  1.1.1.13  christos     AslGbl_TableHeader.Checksum = 0;
    542  1.1.1.13  christos     Op->Asl.FinalAmlOffset = ftell (AslGbl_Files[ASL_FILE_AML_OUTPUT].Handle);
    543   1.1.1.7  christos 
    544   1.1.1.7  christos     /* Write entire header and clear the table header global */
    545   1.1.1.7  christos 
    546  1.1.1.13  christos     CgLocalWriteAmlData (Op, &AslGbl_TableHeader, sizeof (ACPI_TABLE_HEADER));
    547  1.1.1.13  christos     memset (&AslGbl_TableHeader, 0, sizeof (ACPI_TABLE_HEADER));
    548       1.1    jruoho }
    549       1.1    jruoho 
    550       1.1    jruoho 
    551       1.1    jruoho /*******************************************************************************
    552       1.1    jruoho  *
    553   1.1.1.7  christos  * FUNCTION:    CgUpdateHeader
    554       1.1    jruoho  *
    555   1.1.1.7  christos  * PARAMETERS:  Op                  - Op for the Definition Block
    556       1.1    jruoho  *
    557       1.1    jruoho  * RETURN:      None.
    558       1.1    jruoho  *
    559       1.1    jruoho  * DESCRIPTION: Complete the ACPI table by calculating the checksum and
    560   1.1.1.7  christos  *              re-writing the header for the input definition block
    561       1.1    jruoho  *
    562       1.1    jruoho  ******************************************************************************/
    563       1.1    jruoho 
    564       1.1    jruoho static void
    565   1.1.1.7  christos CgUpdateHeader (
    566  1.1.1.10  christos     ACPI_PARSE_OBJECT       *Op)
    567       1.1    jruoho {
    568  1.1.1.10  christos     signed char             Sum;
    569  1.1.1.10  christos     UINT32                  i;
    570  1.1.1.10  christos     UINT32                  Length;
    571  1.1.1.10  christos     UINT8                   FileByte;
    572  1.1.1.10  christos     UINT8                   Checksum;
    573       1.1    jruoho 
    574       1.1    jruoho 
    575   1.1.1.7  christos     /* Calculate the checksum over the entire definition block */
    576       1.1    jruoho 
    577   1.1.1.7  christos     Sum = 0;
    578   1.1.1.7  christos     Length = sizeof (ACPI_TABLE_HEADER) + Op->Asl.AmlSubtreeLength;
    579   1.1.1.7  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset);
    580       1.1    jruoho 
    581   1.1.1.7  christos     for (i = 0; i < Length; i++)
    582       1.1    jruoho     {
    583   1.1.1.7  christos         if (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1) != AE_OK)
    584   1.1.1.7  christos         {
    585  1.1.1.15  christos             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
    586  1.1.1.15  christos                 "Table length is greater than size of the input file");
    587   1.1.1.7  christos             return;
    588   1.1.1.7  christos         }
    589   1.1.1.7  christos 
    590       1.1    jruoho         Sum = (signed char) (Sum + FileByte);
    591       1.1    jruoho     }
    592       1.1    jruoho 
    593   1.1.1.7  christos     Checksum = (UINT8) (0 - Sum);
    594   1.1.1.7  christos 
    595  1.1.1.17  christos     /* Re-write the checksum byte */
    596       1.1    jruoho 
    597   1.1.1.7  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset +
    598   1.1.1.7  christos         ACPI_OFFSET (ACPI_TABLE_HEADER, Checksum));
    599       1.1    jruoho 
    600   1.1.1.7  christos     FlWriteFile (ASL_FILE_AML_OUTPUT, &Checksum, 1);
    601   1.1.1.7  christos 
    602  1.1.1.14  christos     /*
    603  1.1.1.14  christos      * Seek to the end of the file. This is done to support multiple file
    604  1.1.1.14  christos      * compilation. Doing this simplifies other parts of the codebase because
    605  1.1.1.14  christos      * it eliminates the need to seek for a different starting place.
    606  1.1.1.14  christos      */
    607  1.1.1.14  christos     FlSeekFile (ASL_FILE_AML_OUTPUT, Op->Asl.FinalAmlOffset + Length);
    608       1.1    jruoho }
    609       1.1    jruoho 
    610       1.1    jruoho 
    611       1.1    jruoho /*******************************************************************************
    612       1.1    jruoho  *
    613       1.1    jruoho  * FUNCTION:    CgWriteNode
    614       1.1    jruoho  *
    615       1.1    jruoho  * PARAMETERS:  Op            - Parse node to write.
    616       1.1    jruoho  *
    617       1.1    jruoho  * RETURN:      None.
    618       1.1    jruoho  *
    619       1.1    jruoho  * DESCRIPTION: Write the AML that corresponds to a parse node.
    620       1.1    jruoho  *
    621       1.1    jruoho  ******************************************************************************/
    622       1.1    jruoho 
    623       1.1    jruoho static void
    624       1.1    jruoho CgWriteNode (
    625       1.1    jruoho     ACPI_PARSE_OBJECT       *Op)
    626       1.1    jruoho {
    627       1.1    jruoho     ASL_RESOURCE_NODE       *Rnode;
    628       1.1    jruoho 
    629       1.1    jruoho 
    630  1.1.1.10  christos     /* Write all comments here. */
    631  1.1.1.12  christos 
    632  1.1.1.12  christos     if (AcpiGbl_CaptureComments)
    633  1.1.1.10  christos     {
    634  1.1.1.10  christos         CgWriteAmlComment(Op);
    635  1.1.1.10  christos     }
    636  1.1.1.10  christos 
    637       1.1    jruoho     /* Always check for DEFAULT_ARG and other "Noop" nodes */
    638       1.1    jruoho     /* TBD: this may not be the best place for this check */
    639       1.1    jruoho 
    640       1.1    jruoho     if ((Op->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)  ||
    641       1.1    jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)      ||
    642       1.1    jruoho         (Op->Asl.ParseOpcode == PARSEOP_INCLUDE_END))
    643       1.1    jruoho     {
    644       1.1    jruoho         return;
    645       1.1    jruoho     }
    646       1.1    jruoho 
    647       1.1    jruoho     Op->Asl.FinalAmlLength = 0;
    648       1.1    jruoho 
    649       1.1    jruoho     switch (Op->Asl.AmlOpcode)
    650       1.1    jruoho     {
    651       1.1    jruoho     case AML_RAW_DATA_BYTE:
    652       1.1    jruoho     case AML_RAW_DATA_WORD:
    653       1.1    jruoho     case AML_RAW_DATA_DWORD:
    654       1.1    jruoho     case AML_RAW_DATA_QWORD:
    655       1.1    jruoho 
    656       1.1    jruoho         CgLocalWriteAmlData (Op, &Op->Asl.Value.Integer, Op->Asl.AmlLength);
    657       1.1    jruoho         return;
    658       1.1    jruoho 
    659       1.1    jruoho 
    660       1.1    jruoho     case AML_RAW_DATA_BUFFER:
    661       1.1    jruoho 
    662       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.Buffer, Op->Asl.AmlLength);
    663       1.1    jruoho         return;
    664       1.1    jruoho 
    665       1.1    jruoho 
    666       1.1    jruoho     case AML_RAW_DATA_CHAIN:
    667       1.1    jruoho 
    668       1.1    jruoho         Rnode = ACPI_CAST_PTR (ASL_RESOURCE_NODE, Op->Asl.Value.Buffer);
    669       1.1    jruoho         while (Rnode)
    670       1.1    jruoho         {
    671       1.1    jruoho             CgLocalWriteAmlData (Op, Rnode->Buffer, Rnode->BufferLength);
    672       1.1    jruoho             Rnode = Rnode->Next;
    673       1.1    jruoho         }
    674       1.1    jruoho         return;
    675       1.1    jruoho 
    676       1.1    jruoho     default:
    677   1.1.1.3  christos 
    678       1.1    jruoho         /* Internal data opcodes must all appear above */
    679   1.1.1.3  christos 
    680       1.1    jruoho         break;
    681       1.1    jruoho     }
    682       1.1    jruoho 
    683       1.1    jruoho     switch (Op->Asl.ParseOpcode)
    684       1.1    jruoho     {
    685       1.1    jruoho     case PARSEOP_DEFAULT_ARG:
    686       1.1    jruoho 
    687       1.1    jruoho         break;
    688       1.1    jruoho 
    689   1.1.1.7  christos     case PARSEOP_DEFINITION_BLOCK:
    690       1.1    jruoho 
    691       1.1    jruoho         CgWriteTableHeader (Op);
    692  1.1.1.12  christos         if (AcpiGbl_CaptureComments)
    693  1.1.1.10  christos         {
    694  1.1.1.10  christos             CgWriteAmlDefBlockComment (Op);
    695  1.1.1.10  christos         }
    696       1.1    jruoho         break;
    697       1.1    jruoho 
    698       1.1    jruoho     case PARSEOP_NAMESEG:
    699       1.1    jruoho     case PARSEOP_NAMESTRING:
    700       1.1    jruoho     case PARSEOP_METHODCALL:
    701       1.1    jruoho 
    702       1.1    jruoho         CgLocalWriteAmlData (Op, Op->Asl.Value.String, Op->Asl.AmlLength);
    703       1.1    jruoho         break;
    704       1.1    jruoho 
    705       1.1    jruoho     default:
    706       1.1    jruoho 
    707       1.1    jruoho         CgWriteAmlOpcode (Op);
    708       1.1    jruoho         break;
    709       1.1    jruoho     }
    710       1.1    jruoho }
    711