Home | History | Annotate | Line # | Download | only in compiler
cvdisasm.c revision 1.1.1.10
      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.1.10  christos  * Copyright (C) 2000 - 2023, 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.1.8  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 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.1.2  christos /* Local prototypes */
     52   1.1.1.2  christos 
     53       1.1  christos static void
     54       1.1  christos CvPrintInclude(
     55       1.1  christos     ACPI_FILE_NODE          *FNode,
     56       1.1  christos     UINT32                  Level);
     57       1.1  christos 
     58       1.1  christos static BOOLEAN
     59       1.1  christos CvListIsSingleton (
     60       1.1  christos     ACPI_COMMENT_NODE       *CommentList);
     61       1.1  christos 
     62       1.1  christos 
     63       1.1  christos /*******************************************************************************
     64       1.1  christos  *
     65       1.1  christos  * FUNCTION:    CvPrintOneCommentList
     66       1.1  christos  *
     67       1.1  christos  * PARAMETERS:  CommentList
     68       1.1  christos  *              Level
     69       1.1  christos  *
     70       1.1  christos  * RETURN:      None
     71       1.1  christos  *
     72       1.1  christos  * DESCRIPTION: Prints all comments within the given list.
     73       1.1  christos  *              This is referred as ASL_CV_PRINT_ONE_COMMENT_LIST.
     74       1.1  christos  *
     75       1.1  christos  ******************************************************************************/
     76       1.1  christos 
     77       1.1  christos void
     78       1.1  christos CvPrintOneCommentList (
     79       1.1  christos     ACPI_COMMENT_NODE       *CommentList,
     80       1.1  christos     UINT32                  Level)
     81       1.1  christos {
     82       1.1  christos     ACPI_COMMENT_NODE       *Current = CommentList;
     83       1.1  christos     ACPI_COMMENT_NODE       *Previous;
     84       1.1  christos 
     85       1.1  christos 
     86       1.1  christos     while (Current)
     87       1.1  christos     {
     88       1.1  christos         Previous = Current;
     89       1.1  christos         if (Current->Comment)
     90       1.1  christos         {
     91       1.1  christos             AcpiDmIndent(Level);
     92       1.1  christos             AcpiOsPrintf("%s\n", Current->Comment);
     93       1.1  christos             Current->Comment = NULL;
     94       1.1  christos         }
     95   1.1.1.2  christos 
     96       1.1  christos         Current = Current->Next;
     97       1.1  christos         AcpiOsReleaseObject(AcpiGbl_RegCommentCache, Previous);
     98       1.1  christos     }
     99       1.1  christos }
    100       1.1  christos 
    101       1.1  christos 
    102       1.1  christos /*******************************************************************************
    103       1.1  christos  *
    104       1.1  christos  * FUNCTION:    CvListIsSingleton
    105       1.1  christos  *
    106   1.1.1.2  christos  * PARAMETERS:  CommentList     - check to see if this is a single item list.
    107       1.1  christos  *
    108       1.1  christos  * RETURN:      BOOLEAN
    109       1.1  christos  *
    110       1.1  christos  * DESCRIPTION: Returns TRUE if CommentList only contains 1 node.
    111       1.1  christos  *
    112       1.1  christos  ******************************************************************************/
    113       1.1  christos 
    114       1.1  christos static BOOLEAN
    115       1.1  christos CvListIsSingleton (
    116       1.1  christos     ACPI_COMMENT_NODE       *CommentList)
    117       1.1  christos 
    118       1.1  christos {
    119   1.1.1.2  christos 
    120       1.1  christos     if (!CommentList)
    121       1.1  christos     {
    122   1.1.1.2  christos         return (FALSE);
    123       1.1  christos     }
    124       1.1  christos     else if (CommentList->Next)
    125       1.1  christos     {
    126   1.1.1.2  christos         return (FALSE);
    127       1.1  christos     }
    128       1.1  christos 
    129   1.1.1.2  christos     return (TRUE);
    130       1.1  christos }
    131       1.1  christos 
    132       1.1  christos 
    133       1.1  christos /*******************************************************************************
    134       1.1  christos  *
    135       1.1  christos  * FUNCTION:    CvPrintOneCommentType
    136       1.1  christos  *
    137       1.1  christos  * PARAMETERS:  Op
    138       1.1  christos  *              CommentType
    139       1.1  christos  *              EndStr - String to print after printing the comment
    140       1.1  christos  *              Level  - indentation level for comment lists.
    141       1.1  christos  *
    142       1.1  christos  * RETURN:      None
    143       1.1  christos  *
    144       1.1  christos  * DESCRIPTION: Prints all comments of CommentType within the given Op and
    145       1.1  christos  *              clears the printed comment from the Op.
    146       1.1  christos  *              This is referred as ASL_CV_PRINT_ONE_COMMENT.
    147       1.1  christos  *
    148       1.1  christos  ******************************************************************************/
    149       1.1  christos 
    150       1.1  christos void
    151       1.1  christos CvPrintOneCommentType (
    152       1.1  christos     ACPI_PARSE_OBJECT       *Op,
    153       1.1  christos     UINT8                   CommentType,
    154       1.1  christos     char*                   EndStr,
    155       1.1  christos     UINT32                  Level)
    156       1.1  christos {
    157       1.1  christos     BOOLEAN                 CommentExists = FALSE;
    158       1.1  christos     char                    **CommentToPrint = NULL;
    159       1.1  christos 
    160       1.1  christos 
    161       1.1  christos     switch (CommentType)
    162       1.1  christos     {
    163       1.1  christos     case AML_COMMENT_STANDARD:
    164       1.1  christos 
    165       1.1  christos         if (CvListIsSingleton (Op->Common.CommentList))
    166       1.1  christos         {
    167       1.1  christos             CvPrintOneCommentList (Op->Common.CommentList, Level);
    168       1.1  christos             AcpiOsPrintf ("\n");
    169       1.1  christos         }
    170       1.1  christos         else
    171       1.1  christos         {
    172       1.1  christos             CvPrintOneCommentList (Op->Common.CommentList, Level);
    173       1.1  christos         }
    174   1.1.1.2  christos 
    175       1.1  christos         Op->Common.CommentList = NULL;
    176       1.1  christos         return;
    177       1.1  christos 
    178       1.1  christos     case AML_COMMENT_ENDBLK:
    179       1.1  christos 
    180       1.1  christos         if (Op->Common.EndBlkComment)
    181       1.1  christos         {
    182       1.1  christos             CvPrintOneCommentList (Op->Common.EndBlkComment, Level);
    183       1.1  christos             Op->Common.EndBlkComment = NULL;
    184       1.1  christos             AcpiDmIndent(Level);
    185       1.1  christos         }
    186       1.1  christos         return;
    187       1.1  christos 
    188       1.1  christos     case AMLCOMMENT_INLINE:
    189       1.1  christos 
    190       1.1  christos         CommentToPrint = &Op->Common.InlineComment;
    191       1.1  christos         break;
    192       1.1  christos 
    193       1.1  christos     case AML_COMMENT_END_NODE:
    194       1.1  christos 
    195       1.1  christos         CommentToPrint = &Op->Common.EndNodeComment;
    196       1.1  christos         break;
    197       1.1  christos 
    198       1.1  christos     case AML_NAMECOMMENT:
    199       1.1  christos 
    200       1.1  christos         CommentToPrint = &Op->Common.NameComment;
    201       1.1  christos         break;
    202       1.1  christos 
    203       1.1  christos     case AML_COMMENT_CLOSE_BRACE:
    204       1.1  christos 
    205       1.1  christos         CommentToPrint = &Op->Common.CloseBraceComment;
    206       1.1  christos         break;
    207       1.1  christos 
    208       1.1  christos     default:
    209       1.1  christos         return;
    210       1.1  christos     }
    211       1.1  christos 
    212       1.1  christos     if (*CommentToPrint)
    213       1.1  christos     {
    214   1.1.1.2  christos         CommentExists = TRUE;
    215       1.1  christos         AcpiOsPrintf ("%s", *CommentToPrint);
    216       1.1  christos         *CommentToPrint = NULL;
    217       1.1  christos     }
    218       1.1  christos 
    219       1.1  christos     if (CommentExists && EndStr)
    220       1.1  christos     {
    221       1.1  christos         AcpiOsPrintf ("%s", EndStr);
    222       1.1  christos     }
    223       1.1  christos }
    224       1.1  christos 
    225       1.1  christos 
    226       1.1  christos /*******************************************************************************
    227       1.1  christos  *
    228       1.1  christos  * FUNCTION:    CvCloseBraceWriteComment
    229       1.1  christos  *
    230       1.1  christos  * PARAMETERS:  Op
    231       1.1  christos  *              Level
    232       1.1  christos  *
    233   1.1.1.2  christos  * RETURN:      None
    234       1.1  christos  *
    235       1.1  christos  * DESCRIPTION: Print a close brace } and any open brace comments associated
    236       1.1  christos  *              with this parse object.
    237       1.1  christos  *              This is referred as ASL_CV_CLOSE_BRACE.
    238       1.1  christos  *
    239       1.1  christos  ******************************************************************************/
    240       1.1  christos 
    241       1.1  christos void
    242       1.1  christos CvCloseBraceWriteComment(
    243       1.1  christos     ACPI_PARSE_OBJECT       *Op,
    244       1.1  christos     UINT32                  Level)
    245       1.1  christos {
    246   1.1.1.2  christos 
    247   1.1.1.3  christos     if (!AcpiGbl_CaptureComments)
    248       1.1  christos     {
    249       1.1  christos         AcpiOsPrintf ("}");
    250       1.1  christos         return;
    251       1.1  christos     }
    252       1.1  christos 
    253       1.1  christos     CvPrintOneCommentType (Op, AML_COMMENT_ENDBLK, NULL, Level);
    254       1.1  christos     AcpiOsPrintf ("}");
    255       1.1  christos     CvPrintOneCommentType (Op, AML_COMMENT_CLOSE_BRACE, NULL, Level);
    256       1.1  christos }
    257       1.1  christos 
    258       1.1  christos 
    259       1.1  christos /*******************************************************************************
    260       1.1  christos  *
    261       1.1  christos  * FUNCTION:    CvCloseParenWriteComment
    262       1.1  christos  *
    263       1.1  christos  * PARAMETERS:  Op
    264       1.1  christos  *              Level
    265       1.1  christos  *
    266   1.1.1.2  christos  * RETURN:      None
    267       1.1  christos  *
    268       1.1  christos  * DESCRIPTION: Print a closing paren ) and any end node comments associated
    269       1.1  christos  *              with this parse object.
    270       1.1  christos  *              This is referred as ASL_CV_CLOSE_PAREN.
    271       1.1  christos  *
    272       1.1  christos  ******************************************************************************/
    273       1.1  christos 
    274       1.1  christos void
    275       1.1  christos CvCloseParenWriteComment(
    276       1.1  christos     ACPI_PARSE_OBJECT       *Op,
    277       1.1  christos     UINT32                  Level)
    278       1.1  christos {
    279   1.1.1.2  christos 
    280   1.1.1.3  christos     if (!AcpiGbl_CaptureComments)
    281       1.1  christos     {
    282       1.1  christos         AcpiOsPrintf (")");
    283       1.1  christos         return;
    284       1.1  christos     }
    285       1.1  christos 
    286       1.1  christos     /*
    287       1.1  christos      * If this op has a BLOCK_BRACE, then output the comment when the
    288       1.1  christos      * disassembler calls CvCloseBraceWriteComment
    289       1.1  christos      */
    290       1.1  christos     if (AcpiDmBlockType (Op) == BLOCK_PAREN)
    291       1.1  christos     {
    292       1.1  christos         CvPrintOneCommentType (Op, AML_COMMENT_ENDBLK, NULL, Level);
    293       1.1  christos     }
    294       1.1  christos 
    295       1.1  christos     AcpiOsPrintf (")");
    296       1.1  christos 
    297       1.1  christos     if (Op->Common.EndNodeComment)
    298       1.1  christos     {
    299       1.1  christos         CvPrintOneCommentType (Op, AML_COMMENT_END_NODE, NULL, Level);
    300       1.1  christos     }
    301       1.1  christos     else if ((Op->Common.Parent->Common.AmlOpcode == AML_IF_OP) &&
    302       1.1  christos          Op->Common.Parent->Common.EndNodeComment)
    303       1.1  christos     {
    304       1.1  christos         CvPrintOneCommentType (Op->Common.Parent,
    305       1.1  christos             AML_COMMENT_END_NODE, NULL, Level);
    306       1.1  christos     }
    307       1.1  christos }
    308       1.1  christos 
    309       1.1  christos 
    310       1.1  christos /*******************************************************************************
    311       1.1  christos  *
    312       1.1  christos  * FUNCTION:    CvFileHasSwitched
    313       1.1  christos  *
    314       1.1  christos  * PARAMETERS:  Op
    315       1.1  christos  *
    316       1.1  christos  * RETURN:      BOOLEAN
    317       1.1  christos  *
    318       1.1  christos  * DESCRIPTION: Determine whether if a file has switched.
    319       1.1  christos  *              TRUE - file has switched.
    320       1.1  christos  *              FALSE - file has not switched.
    321       1.1  christos  *              This is referred as ASL_CV_FILE_HAS_SWITCHED.
    322       1.1  christos  *
    323       1.1  christos  ******************************************************************************/
    324       1.1  christos 
    325       1.1  christos BOOLEAN
    326       1.1  christos CvFileHasSwitched(
    327       1.1  christos     ACPI_PARSE_OBJECT       *Op)
    328       1.1  christos {
    329   1.1.1.2  christos 
    330       1.1  christos     if (Op->Common.CvFilename   &&
    331       1.1  christos         AcpiGbl_CurrentFilename &&
    332       1.1  christos         AcpiUtStricmp(Op->Common.CvFilename, AcpiGbl_CurrentFilename))
    333       1.1  christos     {
    334   1.1.1.2  christos         return (TRUE);
    335       1.1  christos     }
    336   1.1.1.2  christos 
    337   1.1.1.2  christos     return (FALSE);
    338       1.1  christos }
    339       1.1  christos 
    340       1.1  christos 
    341       1.1  christos /*******************************************************************************
    342       1.1  christos  *
    343       1.1  christos  * FUNCTION:    CvPrintInclude
    344       1.1  christos  *
    345       1.1  christos  * PARAMETERS:  FNode - Write an Include statement for the file that is pointed
    346       1.1  christos  *                      by FNode->File.
    347       1.1  christos  *              Level - indentation level
    348       1.1  christos  *
    349       1.1  christos  * RETURN:      None
    350       1.1  christos  *
    351       1.1  christos  * DESCRIPTION: Write the ASL Include statement for FNode->File in the file
    352       1.1  christos  *              indicated by FNode->Parent->File. Note this function emits
    353       1.1  christos  *              actual ASL code rather than comments. This switches the output
    354       1.1  christos  *              file to FNode->Parent->File.
    355       1.1  christos  *
    356       1.1  christos  ******************************************************************************/
    357       1.1  christos 
    358       1.1  christos static void
    359       1.1  christos CvPrintInclude(
    360       1.1  christos     ACPI_FILE_NODE          *FNode,
    361       1.1  christos     UINT32                  Level)
    362       1.1  christos {
    363   1.1.1.2  christos 
    364       1.1  christos     if (!FNode || FNode->IncludeWritten)
    365       1.1  christos     {
    366       1.1  christos         return;
    367       1.1  christos     }
    368       1.1  christos 
    369   1.1.1.2  christos     CvDbgPrint ("Writing include for %s within %s\n",
    370   1.1.1.2  christos         FNode->Filename, FNode->Parent->Filename);
    371       1.1  christos     AcpiOsRedirectOutput (FNode->Parent->File);
    372       1.1  christos     CvPrintOneCommentList (FNode->IncludeComment, Level);
    373   1.1.1.2  christos 
    374       1.1  christos     AcpiDmIndent (Level);
    375       1.1  christos     AcpiOsPrintf ("Include (\"%s\")\n", FNode->Filename);
    376   1.1.1.2  christos     CvDbgPrint ("emitted the following: Include (\"%s\")\n",
    377   1.1.1.2  christos         FNode->Filename);
    378       1.1  christos     FNode->IncludeWritten = TRUE;
    379       1.1  christos }
    380       1.1  christos 
    381       1.1  christos 
    382       1.1  christos /*******************************************************************************
    383       1.1  christos  *
    384       1.1  christos  * FUNCTION:    CvSwitchFiles
    385       1.1  christos  *
    386   1.1.1.2  christos  * PARAMETERS:  Level                   - indentation level
    387       1.1  christos  *              Op
    388       1.1  christos  *
    389       1.1  christos  * RETURN:      None
    390       1.1  christos  *
    391       1.1  christos  * DESCRIPTION: Switch the outputfile and write ASL Include statement. Note,
    392       1.1  christos  *              this function emits actual ASL code rather than comments.
    393       1.1  christos  *              This is referred as ASL_CV_SWITCH_FILES.
    394       1.1  christos  *
    395       1.1  christos  ******************************************************************************/
    396       1.1  christos 
    397       1.1  christos void
    398       1.1  christos CvSwitchFiles(
    399       1.1  christos     UINT32                  Level,
    400       1.1  christos     ACPI_PARSE_OBJECT       *Op)
    401       1.1  christos {
    402       1.1  christos     char                    *Filename = Op->Common.CvFilename;
    403       1.1  christos     ACPI_FILE_NODE          *FNode;
    404   1.1.1.2  christos     ACPI_FILE_NODE          *Current;
    405       1.1  christos 
    406   1.1.1.2  christos 
    407   1.1.1.2  christos     CvDbgPrint ("Switching from %s to %s\n", AcpiGbl_CurrentFilename,
    408   1.1.1.2  christos         Filename);
    409       1.1  christos     FNode = CvFilenameExists (Filename, AcpiGbl_FileTreeRoot);
    410       1.1  christos     if (!FNode)
    411       1.1  christos     {
    412       1.1  christos         /*
    413       1.1  christos          * At this point, each Filename should exist in AcpiGbl_FileTreeRoot
    414       1.1  christos          * if it does not exist, then abort.
    415       1.1  christos          */
    416       1.1  christos         FlDeleteFile (ASL_FILE_AML_OUTPUT);
    417   1.1.1.4  christos         sprintf (AslGbl_MsgBuffer, "\"Cannot find %s\" - %s",
    418   1.1.1.2  christos             Filename, strerror (errno));
    419   1.1.1.2  christos         AslCommonError (ASL_ERROR, ASL_MSG_OPEN, 0, 0, 0, 0,
    420   1.1.1.4  christos             NULL, AslGbl_MsgBuffer);
    421       1.1  christos         AslAbort ();
    422       1.1  christos     }
    423       1.1  christos 
    424   1.1.1.2  christos     Current = FNode;
    425   1.1.1.2  christos 
    426       1.1  christos     /*
    427       1.1  christos      * If the previous file is a descendent of the current file,
    428       1.1  christos      * make sure that Include statements from the current file
    429       1.1  christos      * to the previous have been emitted.
    430       1.1  christos      */
    431   1.1.1.2  christos     while (Current &&
    432   1.1.1.2  christos            Current->Parent &&
    433   1.1.1.2  christos            AcpiUtStricmp (Current->Filename, AcpiGbl_CurrentFilename))
    434       1.1  christos     {
    435   1.1.1.2  christos         CvPrintInclude (Current, Level);
    436   1.1.1.2  christos         Current = Current->Parent;
    437       1.1  christos     }
    438       1.1  christos 
    439   1.1.1.6  christos     if (FNode)
    440   1.1.1.6  christos     {
    441   1.1.1.6  christos         /* Redirect output to Op->Common.CvFilename */
    442       1.1  christos 
    443   1.1.1.6  christos         AcpiOsRedirectOutput (FNode->File);
    444   1.1.1.6  christos         AcpiGbl_CurrentFilename = FNode->Filename;
    445   1.1.1.6  christos     }
    446       1.1  christos }
    447