Home | History | Annotate | Line # | Download | only in compiler
cvdisasm.c revision 1.1
      1  1.1  christos /******************************************************************************
      2  1.1  christos  *
      3  1.1  christos  * Module Name: cvcompiler - ASL-/ASL+ converter functions
      4  1.1  christos  *
      5  1.1  christos  *****************************************************************************/
      6  1.1  christos 
      7  1.1  christos /*
      8  1.1  christos  * Copyright (C) 2000 - 2017, Intel Corp.
      9  1.1  christos  * All rights reserved.
     10  1.1  christos  *
     11  1.1  christos  * Redistribution and use in source and binary forms, with or without
     12  1.1  christos  * modification, are permitted provided that the following conditions
     13  1.1  christos  * are met:
     14  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     15  1.1  christos  *    notice, this list of conditions, and the following disclaimer,
     16  1.1  christos  *    without modification.
     17  1.1  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  1.1  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  1.1  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  1.1  christos  *    including a substantially similar Disclaimer requirement for further
     21  1.1  christos  *    binary redistribution.
     22  1.1  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23  1.1  christos  *    of any contributors may be used to endorse or promote products derived
     24  1.1  christos  *    from this software without specific prior written permission.
     25  1.1  christos  *
     26  1.1  christos  * Alternatively, this software may be distributed under the terms of the
     27  1.1  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28  1.1  christos  * Software Foundation.
     29  1.1  christos  *
     30  1.1  christos  * NO WARRANTY
     31  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  1.1  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.1  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  1.1  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.1  christos  * POSSIBILITY OF SUCH DAMAGES.
     42  1.1  christos  */
     43  1.1  christos 
     44  1.1  christos #include "aslcompiler.h"
     45  1.1  christos #include "acparser.h"
     46  1.1  christos #include "amlcode.h"
     47  1.1  christos #include "acdebug.h"
     48  1.1  christos #include "acconvert.h"
     49  1.1  christos 
     50  1.1  christos 
     51  1.1  christos static void
     52  1.1  christos CvPrintInclude(
     53  1.1  christos     ACPI_FILE_NODE          *FNode,
     54  1.1  christos     UINT32                  Level);
     55  1.1  christos 
     56  1.1  christos static BOOLEAN
     57  1.1  christos CvListIsSingleton (
     58  1.1  christos     ACPI_COMMENT_NODE       *CommentList);
     59  1.1  christos 
     60  1.1  christos 
     61  1.1  christos /*******************************************************************************
     62  1.1  christos  *
     63  1.1  christos  * FUNCTION:    CvPrintOneCommentList
     64  1.1  christos  *
     65  1.1  christos  * PARAMETERS:  CommentList
     66  1.1  christos  *              Level
     67  1.1  christos  *
     68  1.1  christos  * RETURN:      None
     69  1.1  christos  *
     70  1.1  christos  * DESCRIPTION: Prints all comments within the given list.
     71  1.1  christos  *              This is referred as ASL_CV_PRINT_ONE_COMMENT_LIST.
     72  1.1  christos  *
     73  1.1  christos  ******************************************************************************/
     74  1.1  christos 
     75  1.1  christos void
     76  1.1  christos CvPrintOneCommentList (
     77  1.1  christos     ACPI_COMMENT_NODE       *CommentList,
     78  1.1  christos     UINT32                  Level)
     79  1.1  christos {
     80  1.1  christos     ACPI_COMMENT_NODE       *Current = CommentList;
     81  1.1  christos     ACPI_COMMENT_NODE       *Previous;
     82  1.1  christos 
     83  1.1  christos 
     84  1.1  christos     while (Current)
     85  1.1  christos     {
     86  1.1  christos         Previous = Current;
     87  1.1  christos         if (Current->Comment)
     88  1.1  christos         {
     89  1.1  christos             AcpiDmIndent(Level);
     90  1.1  christos             AcpiOsPrintf("%s\n", Current->Comment);
     91  1.1  christos             Current->Comment = NULL;
     92  1.1  christos         }
     93  1.1  christos         Current = Current->Next;
     94  1.1  christos         AcpiOsReleaseObject(AcpiGbl_RegCommentCache, Previous);
     95  1.1  christos     }
     96  1.1  christos }
     97  1.1  christos 
     98  1.1  christos 
     99  1.1  christos /*******************************************************************************
    100  1.1  christos  *
    101  1.1  christos  * FUNCTION:    CvListIsSingleton
    102  1.1  christos  *
    103  1.1  christos  * PARAMETERS:  CommentList -- check to see if this is a single item list.
    104  1.1  christos  *
    105  1.1  christos  * RETURN:      BOOLEAN
    106  1.1  christos  *
    107  1.1  christos  * DESCRIPTION: Returns TRUE if CommentList only contains 1 node.
    108  1.1  christos  *
    109  1.1  christos  ******************************************************************************/
    110  1.1  christos 
    111  1.1  christos static BOOLEAN
    112  1.1  christos CvListIsSingleton (
    113  1.1  christos     ACPI_COMMENT_NODE       *CommentList)
    114  1.1  christos 
    115  1.1  christos {
    116  1.1  christos     if (!CommentList)
    117  1.1  christos     {
    118  1.1  christos         return FALSE;
    119  1.1  christos     }
    120  1.1  christos     else if (CommentList->Next)
    121  1.1  christos     {
    122  1.1  christos         return FALSE;
    123  1.1  christos     }
    124  1.1  christos 
    125  1.1  christos     return TRUE;
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos 
    129  1.1  christos /*******************************************************************************
    130  1.1  christos  *
    131  1.1  christos  * FUNCTION:    CvPrintOneCommentType
    132  1.1  christos  *
    133  1.1  christos  * PARAMETERS:  Op
    134  1.1  christos  *              CommentType
    135  1.1  christos  *              EndStr - String to print after printing the comment
    136  1.1  christos  *              Level  - indentation level for comment lists.
    137  1.1  christos  *
    138  1.1  christos  * RETURN:      None
    139  1.1  christos  *
    140  1.1  christos  * DESCRIPTION: Prints all comments of CommentType within the given Op and
    141  1.1  christos  *              clears the printed comment from the Op.
    142  1.1  christos  *              This is referred as ASL_CV_PRINT_ONE_COMMENT.
    143  1.1  christos  *
    144  1.1  christos  ******************************************************************************/
    145  1.1  christos 
    146  1.1  christos void
    147  1.1  christos CvPrintOneCommentType (
    148  1.1  christos     ACPI_PARSE_OBJECT       *Op,
    149  1.1  christos     UINT8                   CommentType,
    150  1.1  christos     char*                   EndStr,
    151  1.1  christos     UINT32                  Level)
    152  1.1  christos {
    153  1.1  christos     BOOLEAN                 CommentExists = FALSE;
    154  1.1  christos     char                    **CommentToPrint = NULL;
    155  1.1  christos 
    156  1.1  christos 
    157  1.1  christos     switch (CommentType)
    158  1.1  christos     {
    159  1.1  christos     case AML_COMMENT_STANDARD:
    160  1.1  christos 
    161  1.1  christos         if (CvListIsSingleton (Op->Common.CommentList))
    162  1.1  christos         {
    163  1.1  christos             CvPrintOneCommentList (Op->Common.CommentList, Level);
    164  1.1  christos             AcpiOsPrintf ("\n");
    165  1.1  christos         }
    166  1.1  christos         else
    167  1.1  christos         {
    168  1.1  christos             CvPrintOneCommentList (Op->Common.CommentList, Level);
    169  1.1  christos         }
    170  1.1  christos         Op->Common.CommentList = NULL;
    171  1.1  christos         return;
    172  1.1  christos 
    173  1.1  christos     case AML_COMMENT_ENDBLK:
    174  1.1  christos 
    175  1.1  christos         if (Op->Common.EndBlkComment)
    176  1.1  christos         {
    177  1.1  christos             CvPrintOneCommentList (Op->Common.EndBlkComment, Level);
    178  1.1  christos             Op->Common.EndBlkComment = NULL;
    179  1.1  christos             AcpiDmIndent(Level);
    180  1.1  christos         }
    181  1.1  christos         return;
    182  1.1  christos 
    183  1.1  christos     case AMLCOMMENT_INLINE:
    184  1.1  christos 
    185  1.1  christos         CommentToPrint = &Op->Common.InlineComment;
    186  1.1  christos         break;
    187  1.1  christos 
    188  1.1  christos     case AML_COMMENT_END_NODE:
    189  1.1  christos 
    190  1.1  christos         CommentToPrint = &Op->Common.EndNodeComment;
    191  1.1  christos         break;
    192  1.1  christos 
    193  1.1  christos     case AML_NAMECOMMENT:
    194  1.1  christos 
    195  1.1  christos         CommentToPrint = &Op->Common.NameComment;
    196  1.1  christos         break;
    197  1.1  christos 
    198  1.1  christos     case AML_COMMENT_CLOSE_BRACE:
    199  1.1  christos 
    200  1.1  christos         CommentToPrint = &Op->Common.CloseBraceComment;
    201  1.1  christos         break;
    202  1.1  christos 
    203  1.1  christos     default:
    204  1.1  christos         return;
    205  1.1  christos     }
    206  1.1  christos 
    207  1.1  christos     if (*CommentToPrint)
    208  1.1  christos     {
    209  1.1  christos         AcpiOsPrintf ("%s", *CommentToPrint);
    210  1.1  christos         *CommentToPrint = NULL;
    211  1.1  christos     }
    212  1.1  christos 
    213  1.1  christos     if (CommentExists && EndStr)
    214  1.1  christos     {
    215  1.1  christos         AcpiOsPrintf ("%s", EndStr);
    216  1.1  christos     }
    217  1.1  christos }
    218  1.1  christos 
    219  1.1  christos 
    220  1.1  christos /*******************************************************************************
    221  1.1  christos  *
    222  1.1  christos  * FUNCTION:    CvCloseBraceWriteComment
    223  1.1  christos  *
    224  1.1  christos  * PARAMETERS:  Op
    225  1.1  christos  *              Level
    226  1.1  christos  *
    227  1.1  christos  * RETURN:      none
    228  1.1  christos  *
    229  1.1  christos  * DESCRIPTION: Print a close brace } and any open brace comments associated
    230  1.1  christos  *              with this parse object.
    231  1.1  christos  *              This is referred as ASL_CV_CLOSE_BRACE.
    232  1.1  christos  *
    233  1.1  christos  ******************************************************************************/
    234  1.1  christos 
    235  1.1  christos void
    236  1.1  christos CvCloseBraceWriteComment(
    237  1.1  christos     ACPI_PARSE_OBJECT       *Op,
    238  1.1  christos     UINT32                  Level)
    239  1.1  christos {
    240  1.1  christos     if (!Gbl_CaptureComments)
    241  1.1  christos     {
    242  1.1  christos         AcpiOsPrintf ("}");
    243  1.1  christos         return;
    244  1.1  christos     }
    245  1.1  christos 
    246  1.1  christos     CvPrintOneCommentType (Op, AML_COMMENT_ENDBLK, NULL, Level);
    247  1.1  christos     AcpiOsPrintf ("}");
    248  1.1  christos     CvPrintOneCommentType (Op, AML_COMMENT_CLOSE_BRACE, NULL, Level);
    249  1.1  christos }
    250  1.1  christos 
    251  1.1  christos 
    252  1.1  christos /*******************************************************************************
    253  1.1  christos  *
    254  1.1  christos  * FUNCTION:    CvCloseParenWriteComment
    255  1.1  christos  *
    256  1.1  christos  * PARAMETERS:  Op
    257  1.1  christos  *              Level
    258  1.1  christos  *
    259  1.1  christos  * RETURN:      none
    260  1.1  christos  *
    261  1.1  christos  * DESCRIPTION: Print a closing paren ) and any end node comments associated
    262  1.1  christos  *              with this parse object.
    263  1.1  christos  *              This is referred as ASL_CV_CLOSE_PAREN.
    264  1.1  christos  *
    265  1.1  christos  ******************************************************************************/
    266  1.1  christos 
    267  1.1  christos void
    268  1.1  christos CvCloseParenWriteComment(
    269  1.1  christos     ACPI_PARSE_OBJECT       *Op,
    270  1.1  christos     UINT32                  Level)
    271  1.1  christos {
    272  1.1  christos     if (!Gbl_CaptureComments)
    273  1.1  christos     {
    274  1.1  christos         AcpiOsPrintf (")");
    275  1.1  christos         return;
    276  1.1  christos     }
    277  1.1  christos 
    278  1.1  christos     /*
    279  1.1  christos      * If this op has a BLOCK_BRACE, then output the comment when the
    280  1.1  christos      * disassembler calls CvCloseBraceWriteComment
    281  1.1  christos      */
    282  1.1  christos     if (AcpiDmBlockType (Op) == BLOCK_PAREN)
    283  1.1  christos     {
    284  1.1  christos         CvPrintOneCommentType (Op, AML_COMMENT_ENDBLK, NULL, Level);
    285  1.1  christos     }
    286  1.1  christos 
    287  1.1  christos     AcpiOsPrintf (")");
    288  1.1  christos 
    289  1.1  christos     if (Op->Common.EndNodeComment)
    290  1.1  christos     {
    291  1.1  christos         CvPrintOneCommentType (Op, AML_COMMENT_END_NODE, NULL, Level);
    292  1.1  christos     }
    293  1.1  christos     else if ((Op->Common.Parent->Common.AmlOpcode == AML_IF_OP) &&
    294  1.1  christos          Op->Common.Parent->Common.EndNodeComment)
    295  1.1  christos     {
    296  1.1  christos         CvPrintOneCommentType (Op->Common.Parent,
    297  1.1  christos             AML_COMMENT_END_NODE, NULL, Level);
    298  1.1  christos     }
    299  1.1  christos }
    300  1.1  christos 
    301  1.1  christos 
    302  1.1  christos /*******************************************************************************
    303  1.1  christos  *
    304  1.1  christos  * FUNCTION:    CvFileHasSwitched
    305  1.1  christos  *
    306  1.1  christos  * PARAMETERS:  Op
    307  1.1  christos  *
    308  1.1  christos  * RETURN:      BOOLEAN
    309  1.1  christos  *
    310  1.1  christos  * DESCRIPTION: Determine whether if a file has switched.
    311  1.1  christos  *              TRUE - file has switched.
    312  1.1  christos  *              FALSE - file has not switched.
    313  1.1  christos  *              This is referred as ASL_CV_FILE_HAS_SWITCHED.
    314  1.1  christos  *
    315  1.1  christos  ******************************************************************************/
    316  1.1  christos 
    317  1.1  christos BOOLEAN
    318  1.1  christos CvFileHasSwitched(
    319  1.1  christos     ACPI_PARSE_OBJECT       *Op)
    320  1.1  christos {
    321  1.1  christos     if (Op->Common.CvFilename   &&
    322  1.1  christos         AcpiGbl_CurrentFilename &&
    323  1.1  christos         AcpiUtStricmp(Op->Common.CvFilename, AcpiGbl_CurrentFilename))
    324  1.1  christos     {
    325  1.1  christos         return TRUE;
    326  1.1  christos     }
    327  1.1  christos     return FALSE;
    328  1.1  christos }
    329  1.1  christos 
    330  1.1  christos 
    331  1.1  christos /*******************************************************************************
    332  1.1  christos  *
    333  1.1  christos  * FUNCTION:    CvPrintInclude
    334  1.1  christos  *
    335  1.1  christos  * PARAMETERS:  FNode - Write an Include statement for the file that is pointed
    336  1.1  christos  *                      by FNode->File.
    337  1.1  christos  *              Level - indentation level
    338  1.1  christos  *
    339  1.1  christos  * RETURN:      None
    340  1.1  christos  *
    341  1.1  christos  * DESCRIPTION: Write the ASL Include statement for FNode->File in the file
    342  1.1  christos  *              indicated by FNode->Parent->File. Note this function emits
    343  1.1  christos  *              actual ASL code rather than comments. This switches the output
    344  1.1  christos  *              file to FNode->Parent->File.
    345  1.1  christos  *
    346  1.1  christos  ******************************************************************************/
    347  1.1  christos 
    348  1.1  christos static void
    349  1.1  christos CvPrintInclude(
    350  1.1  christos     ACPI_FILE_NODE          *FNode,
    351  1.1  christos     UINT32                  Level)
    352  1.1  christos {
    353  1.1  christos     if (!FNode || FNode->IncludeWritten)
    354  1.1  christos     {
    355  1.1  christos         return;
    356  1.1  christos     }
    357  1.1  christos 
    358  1.1  christos     CvDbgPrint ("Writing include for %s within %s\n", FNode->Filename, FNode->Parent->Filename);
    359  1.1  christos     AcpiOsRedirectOutput (FNode->Parent->File);
    360  1.1  christos     CvPrintOneCommentList (FNode->IncludeComment, Level);
    361  1.1  christos     AcpiDmIndent (Level);
    362  1.1  christos     AcpiOsPrintf ("Include (\"%s\")\n", FNode->Filename);
    363  1.1  christos     CvDbgPrint ("emitted the following: Include (\"%s\")\n", FNode->Filename);
    364  1.1  christos     FNode->IncludeWritten = TRUE;
    365  1.1  christos }
    366  1.1  christos 
    367  1.1  christos 
    368  1.1  christos /*******************************************************************************
    369  1.1  christos  *
    370  1.1  christos  * FUNCTION:    CvSwitchFiles
    371  1.1  christos  *
    372  1.1  christos  * PARAMETERS:  Level - indentation level
    373  1.1  christos  *              Op
    374  1.1  christos  *
    375  1.1  christos  * RETURN:      None
    376  1.1  christos  *
    377  1.1  christos  * DESCRIPTION: Switch the outputfile and write ASL Include statement. Note,
    378  1.1  christos  *              this function emits actual ASL code rather than comments.
    379  1.1  christos  *              This is referred as ASL_CV_SWITCH_FILES.
    380  1.1  christos  *
    381  1.1  christos  ******************************************************************************/
    382  1.1  christos 
    383  1.1  christos void
    384  1.1  christos CvSwitchFiles(
    385  1.1  christos     UINT32                  Level,
    386  1.1  christos     ACPI_PARSE_OBJECT       *Op)
    387  1.1  christos {
    388  1.1  christos     char                    *Filename = Op->Common.CvFilename;
    389  1.1  christos     ACPI_FILE_NODE          *FNode;
    390  1.1  christos 
    391  1.1  christos     CvDbgPrint ("Switching from %s to %s\n", AcpiGbl_CurrentFilename, Filename);
    392  1.1  christos     FNode = CvFilenameExists (Filename, AcpiGbl_FileTreeRoot);
    393  1.1  christos     if (!FNode)
    394  1.1  christos     {
    395  1.1  christos         /*
    396  1.1  christos          * At this point, each Filename should exist in AcpiGbl_FileTreeRoot
    397  1.1  christos          * if it does not exist, then abort.
    398  1.1  christos          */
    399  1.1  christos         FlDeleteFile (ASL_FILE_AML_OUTPUT);
    400  1.1  christos         sprintf (MsgBuffer, "\"Cannot find %s\" - %s", Filename, strerror (errno));
    401  1.1  christos         AslCommonError (ASL_ERROR, ASL_MSG_OPEN, 0, 0, 0, 0, NULL, MsgBuffer);
    402  1.1  christos         AslAbort ();
    403  1.1  christos     }
    404  1.1  christos 
    405  1.1  christos     /*
    406  1.1  christos      * If the previous file is a descendent of the current file,
    407  1.1  christos      * make sure that Include statements from the current file
    408  1.1  christos      * to the previous have been emitted.
    409  1.1  christos      */
    410  1.1  christos     while (FNode &&
    411  1.1  christos            FNode->Parent &&
    412  1.1  christos            AcpiUtStricmp (FNode->Filename, AcpiGbl_CurrentFilename))
    413  1.1  christos     {
    414  1.1  christos         CvPrintInclude (FNode, Level);
    415  1.1  christos         FNode = FNode->Parent;
    416  1.1  christos     }
    417  1.1  christos 
    418  1.1  christos     /* Redirect output to the Op->Common.CvFilename */
    419  1.1  christos 
    420  1.1  christos     FNode = CvFilenameExists (Filename, AcpiGbl_FileTreeRoot);
    421  1.1  christos     AcpiOsRedirectOutput (FNode->File);
    422  1.1  christos     AcpiGbl_CurrentFilename = FNode->Filename;
    423  1.1  christos }
    424