Home | History | Annotate | Line # | Download | only in compiler
cvcompiler.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 "aslcompiler.y.h"
     46  1.1  christos #include "amlcode.h"
     47  1.1  christos #include "acapps.h"
     48  1.1  christos #include "acconvert.h"
     49  1.1  christos 
     50  1.1  christos 
     51  1.1  christos /*******************************************************************************
     52  1.1  christos  *
     53  1.1  christos  * FUNCTION:    CvProcessComment
     54  1.1  christos  *
     55  1.1  christos  * PARAMETERS:  CurrentState      Current comment parse state
     56  1.1  christos  *              StringBuffer      Buffer containing the comment being processed
     57  1.1  christos  *              c1                Current input
     58  1.1  christos  *
     59  1.1  christos  * RETURN:      none
     60  1.1  christos  *
     61  1.1  christos  * DESCRIPTION: Process a single line comment of a c Style comment. This
     62  1.1  christos  *              function captures a line of a c style comment in a char* and
     63  1.1  christos  *              places the comment in the approperiate global buffer.
     64  1.1  christos  *
     65  1.1  christos  ******************************************************************************/
     66  1.1  christos 
     67  1.1  christos void
     68  1.1  christos CvProcessComment (
     69  1.1  christos     ASL_COMMENT_STATE       CurrentState,
     70  1.1  christos     char                    *StringBuffer,
     71  1.1  christos     int                     c1)
     72  1.1  christos {
     73  1.1  christos     UINT64                  i;
     74  1.1  christos     char                    *LineToken;
     75  1.1  christos     char                    *FinalLineToken;
     76  1.1  christos     BOOLEAN                 CharStart;
     77  1.1  christos     char                    *CommentString;
     78  1.1  christos     char                    *FinalCommentString;
     79  1.1  christos 
     80  1.1  christos 
     81  1.1  christos     if (Gbl_CaptureComments && CurrentState.CaptureComments)
     82  1.1  christos     {
     83  1.1  christos         *StringBuffer = (char) c1;
     84  1.1  christos         ++StringBuffer;
     85  1.1  christos         *StringBuffer = 0;
     86  1.1  christos         CvDbgPrint ("Multi-line comment\n");
     87  1.1  christos         CommentString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
     88  1.1  christos         strcpy (CommentString, MsgBuffer);
     89  1.1  christos 
     90  1.1  christos         CvDbgPrint ("CommentString: %s\n", CommentString);
     91  1.1  christos 
     92  1.1  christos         /*
     93  1.1  christos          * Determine whether if this comment spans multiple lines.
     94  1.1  christos          * If so, break apart the comment by line so that it can be
     95  1.1  christos          * properly indented.
     96  1.1  christos          */
     97  1.1  christos         if (strchr (CommentString, '\n') != NULL)
     98  1.1  christos         {
     99  1.1  christos             /*
    100  1.1  christos              * Get the first token. The for loop pads subsequent lines
    101  1.1  christos              * for comments similar to the style of this comment.
    102  1.1  christos              */
    103  1.1  christos             LineToken = strtok (CommentString, "\n");
    104  1.1  christos             FinalLineToken = UtStringCacheCalloc (strlen (LineToken) + 1);
    105  1.1  christos             strcpy (FinalLineToken, LineToken);
    106  1.1  christos 
    107  1.1  christos             /* Get rid of any carriage returns */
    108  1.1  christos 
    109  1.1  christos             if (FinalLineToken[strlen (FinalLineToken) - 1] == 0x0D)
    110  1.1  christos             {
    111  1.1  christos                 FinalLineToken[strlen(FinalLineToken)-1] = 0;
    112  1.1  christos             }
    113  1.1  christos             CvAddToCommentList (FinalLineToken);
    114  1.1  christos             LineToken = strtok (NULL, "\n");
    115  1.1  christos             while (LineToken != NULL)
    116  1.1  christos             {
    117  1.1  christos                 /*
    118  1.1  christos                  * It is assumed that each line has some sort of indentation.
    119  1.1  christos                  * This means that we need to find the first character that is not
    120  1.1  christos                  * a white space within each line.
    121  1.1  christos                  */
    122  1.1  christos                 CharStart = FALSE;
    123  1.1  christos                 for (i = 0; (i < (strlen (LineToken) + 1)) && !CharStart; i++)
    124  1.1  christos                 {
    125  1.1  christos                     if (LineToken[i] != ' ' && LineToken[i] != '\t')
    126  1.1  christos                     {
    127  1.1  christos                         CharStart = TRUE;
    128  1.1  christos                         LineToken += i-1;
    129  1.1  christos                         LineToken [0] = ' '; /* Pad for Formatting */
    130  1.1  christos                     }
    131  1.1  christos                 }
    132  1.1  christos                 FinalLineToken = UtStringCacheCalloc (strlen (LineToken) + 1);
    133  1.1  christos                 strcat (FinalLineToken, LineToken);
    134  1.1  christos 
    135  1.1  christos                 /* Get rid of any carriage returns */
    136  1.1  christos 
    137  1.1  christos                 if (FinalLineToken[strlen (FinalLineToken) - 1] == 0x0D)
    138  1.1  christos                 {
    139  1.1  christos                     FinalLineToken[strlen(FinalLineToken) - 1] = 0;
    140  1.1  christos                 }
    141  1.1  christos                 CvAddToCommentList (FinalLineToken);
    142  1.1  christos                 LineToken = strtok (NULL,"\n");
    143  1.1  christos             }
    144  1.1  christos         }
    145  1.1  christos 
    146  1.1  christos         /*
    147  1.1  christos          * If this only spans a single line, check to see whether if this comment
    148  1.1  christos          * appears on the same line as a line of code. If does, retain it's
    149  1.1  christos          * position for stylistic reasons. If it doesn't, add it to the comment
    150  1.1  christos          * List so that it can be associated with the next node that's created.
    151  1.1  christos          */
    152  1.1  christos         else
    153  1.1  christos         {
    154  1.1  christos            /*
    155  1.1  christos             * if this is not a regular comment, pad with extra spaces that appeared
    156  1.1  christos             * in the original source input to retain the original spacing.
    157  1.1  christos             */
    158  1.1  christos             FinalCommentString = UtStringCacheCalloc (strlen (CommentString) + CurrentState.SpacesBefore + 1);
    159  1.1  christos             for (i=0; (CurrentState.CommentType != ASL_COMMENT_STANDARD) &&
    160  1.1  christos                 (i < CurrentState.SpacesBefore); ++i)
    161  1.1  christos             {
    162  1.1  christos                  FinalCommentString[i] = ' ';
    163  1.1  christos             }
    164  1.1  christos             strcat (FinalCommentString, CommentString);
    165  1.1  christos             CvPlaceComment (CurrentState.CommentType, FinalCommentString);
    166  1.1  christos         }
    167  1.1  christos     }
    168  1.1  christos }
    169  1.1  christos 
    170  1.1  christos 
    171  1.1  christos /*******************************************************************************
    172  1.1  christos  *
    173  1.1  christos  * FUNCTION:    CvProcessCommentType2
    174  1.1  christos  *
    175  1.1  christos  * PARAMETERS:  CurrentState      Current comment parse state
    176  1.1  christos  *              StringBuffer      Buffer containing the comment being processed
    177  1.1  christos  *
    178  1.1  christos  * RETURN:      none
    179  1.1  christos  *
    180  1.1  christos  * DESCRIPTION: Process a single line comment. This function captures a comment
    181  1.1  christos  *              in a char* and places the comment in the approperiate global
    182  1.1  christos  *              buffer through CvPlaceComment
    183  1.1  christos  *
    184  1.1  christos  ******************************************************************************/
    185  1.1  christos 
    186  1.1  christos void
    187  1.1  christos CvProcessCommentType2 (
    188  1.1  christos     ASL_COMMENT_STATE       CurrentState,
    189  1.1  christos     char                    *StringBuffer)
    190  1.1  christos {
    191  1.1  christos     UINT32                  i;
    192  1.1  christos     char                    *CommentString;
    193  1.1  christos     char                    *FinalCommentString;
    194  1.1  christos 
    195  1.1  christos 
    196  1.1  christos     if (Gbl_CaptureComments && CurrentState.CaptureComments)
    197  1.1  christos     {
    198  1.1  christos         *StringBuffer = 0; /* null terminate */
    199  1.1  christos         CvDbgPrint ("Single-line comment\n");
    200  1.1  christos         CommentString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
    201  1.1  christos         strcpy (CommentString, MsgBuffer);
    202  1.1  christos 
    203  1.1  christos         /* If this comment lies on the same line as the latest parse node,
    204  1.1  christos          * assign it to that node's CommentAfter field. Saving in this field
    205  1.1  christos          * will allow us to support comments that come after code on the same
    206  1.1  christos          * line as the code itself. For example,
    207  1.1  christos          * Name(A,"") //comment
    208  1.1  christos          *
    209  1.1  christos          * will be retained rather than transformed into
    210  1.1  christos          *
    211  1.1  christos          * Name(A,"")
    212  1.1  christos          * //comment
    213  1.1  christos          *
    214  1.1  christos          * For this case, we only need to add one comment since
    215  1.1  christos          *
    216  1.1  christos          * Name(A,"") //comment1 //comment2 ... more comments here.
    217  1.1  christos          *
    218  1.1  christos          * would be lexically analyzed as a single comment.
    219  1.1  christos          *
    220  1.1  christos          * Create a new string with the approperiate spaces. Since we need
    221  1.1  christos          * to account for the proper spacing, the actual comment,
    222  1.1  christos          * extra 2 spaces so that this comment can be converted to the "/ *"
    223  1.1  christos          * style and the null terminator, the string would look something like
    224  1.1  christos          *
    225  1.1  christos          * [ (spaces) (comment)  ( * /) ('\0') ]
    226  1.1  christos          *
    227  1.1  christos          */
    228  1.1  christos         FinalCommentString = UtStringCacheCalloc (CurrentState.SpacesBefore + strlen (CommentString) + 3 + 1);
    229  1.1  christos         for (i=0; (CurrentState.CommentType!=1) && (i<CurrentState.SpacesBefore); ++i)
    230  1.1  christos         {
    231  1.1  christos             FinalCommentString[i] = ' ';
    232  1.1  christos         }
    233  1.1  christos         strcat (FinalCommentString, CommentString);
    234  1.1  christos 
    235  1.1  christos         /* convert to a "/ *" style comment  */
    236  1.1  christos 
    237  1.1  christos         strcat (FinalCommentString, " */");
    238  1.1  christos         FinalCommentString [CurrentState.SpacesBefore + strlen (CommentString) + 3] = 0;
    239  1.1  christos 
    240  1.1  christos         /* get rid of the carriage return */
    241  1.1  christos 
    242  1.1  christos         if (FinalCommentString[strlen (FinalCommentString) - 1] == 0x0D)
    243  1.1  christos         {
    244  1.1  christos             FinalCommentString[strlen(FinalCommentString)-1] = 0;
    245  1.1  christos         }
    246  1.1  christos         CvPlaceComment (CurrentState.CommentType, FinalCommentString);
    247  1.1  christos     }
    248  1.1  christos }
    249  1.1  christos 
    250  1.1  christos 
    251  1.1  christos /*******************************************************************************
    252  1.1  christos  *
    253  1.1  christos  * FUNCTION:    CgCalculateCommentLengths
    254  1.1  christos  *
    255  1.1  christos  * PARAMETERS:  Op                 - Calculate all comments of this Op
    256  1.1  christos  *
    257  1.1  christos  * RETURN:      TotalCommentLength - Length of all comments within this node.
    258  1.1  christos  *
    259  1.1  christos  * DESCRIPTION: calculate the length that the each comment takes up within Op.
    260  1.1  christos  *              Comments look like the follwoing: [0xA9 OptionBtye comment 0x00]
    261  1.1  christos  *              therefore, we add 1 + 1 + strlen (comment) + 1 to get the actual
    262  1.1  christos  *              length of this comment.
    263  1.1  christos  *
    264  1.1  christos  ******************************************************************************/
    265  1.1  christos 
    266  1.1  christos UINT32
    267  1.1  christos CvCalculateCommentLengths(
    268  1.1  christos    ACPI_PARSE_OBJECT        *Op)
    269  1.1  christos {
    270  1.1  christos     UINT32                  CommentLength = 0;
    271  1.1  christos     UINT32                  TotalCommentLength = 0;
    272  1.1  christos     ACPI_COMMENT_NODE       *Current = NULL;
    273  1.1  christos 
    274  1.1  christos 
    275  1.1  christos     if (!Gbl_CaptureComments)
    276  1.1  christos     {
    277  1.1  christos         return (0);
    278  1.1  christos     }
    279  1.1  christos 
    280  1.1  christos     CvDbgPrint ("==Calculating comment lengths for %s\n",  Op->Asl.ParseOpName);
    281  1.1  christos     if (Op->Asl.FileChanged)
    282  1.1  christos     {
    283  1.1  christos         TotalCommentLength += strlen (Op->Asl.Filename) + 3;
    284  1.1  christos 
    285  1.1  christos         if (Op->Asl.ParentFilename &&
    286  1.1  christos             AcpiUtStricmp (Op->Asl.Filename, Op->Asl.ParentFilename))
    287  1.1  christos         {
    288  1.1  christos             TotalCommentLength += strlen (Op->Asl.ParentFilename) + 3;
    289  1.1  christos         }
    290  1.1  christos     }
    291  1.1  christos     if (Op->Asl.CommentList)
    292  1.1  christos     {
    293  1.1  christos         Current = Op->Asl.CommentList;
    294  1.1  christos         while (Current)
    295  1.1  christos         {
    296  1.1  christos             CommentLength = strlen (Current->Comment)+3;
    297  1.1  christos             CvDbgPrint ("Length of standard comment: %d\n", CommentLength);
    298  1.1  christos             CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
    299  1.1  christos             TotalCommentLength += CommentLength;
    300  1.1  christos             Current = Current->Next;
    301  1.1  christos         }
    302  1.1  christos     }
    303  1.1  christos     if (Op->Asl.EndBlkComment)
    304  1.1  christos     {
    305  1.1  christos         Current = Op->Asl.EndBlkComment;
    306  1.1  christos         while (Current)
    307  1.1  christos         {
    308  1.1  christos             CommentLength = strlen (Current->Comment)+3;
    309  1.1  christos             CvDbgPrint ("Length of endblkcomment: %d\n", CommentLength);
    310  1.1  christos             CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
    311  1.1  christos             TotalCommentLength += CommentLength;
    312  1.1  christos             Current = Current->Next;
    313  1.1  christos         }
    314  1.1  christos     }
    315  1.1  christos     if (Op->Asl.InlineComment)
    316  1.1  christos     {
    317  1.1  christos         CommentLength = strlen (Op->Asl.InlineComment)+3;
    318  1.1  christos         CvDbgPrint ("Length of inline comment: %d\n", CommentLength);
    319  1.1  christos         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.InlineComment);
    320  1.1  christos         TotalCommentLength += CommentLength;
    321  1.1  christos     }
    322  1.1  christos     if (Op->Asl.EndNodeComment)
    323  1.1  christos     {
    324  1.1  christos         CommentLength = strlen(Op->Asl.EndNodeComment)+3;
    325  1.1  christos         CvDbgPrint ("Length of end node comment +3: %d\n", CommentLength);
    326  1.1  christos         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.EndNodeComment);
    327  1.1  christos         TotalCommentLength += CommentLength;
    328  1.1  christos     }
    329  1.1  christos 
    330  1.1  christos     if (Op->Asl.CloseBraceComment)
    331  1.1  christos     {
    332  1.1  christos         CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
    333  1.1  christos         CvDbgPrint ("Length of close brace comment: %d\n", CommentLength);
    334  1.1  christos         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
    335  1.1  christos         TotalCommentLength += CommentLength;
    336  1.1  christos     }
    337  1.1  christos 
    338  1.1  christos     CvDbgPrint("\n\n");
    339  1.1  christos 
    340  1.1  christos     return TotalCommentLength;
    341  1.1  christos 
    342  1.1  christos }
    343  1.1  christos 
    344  1.1  christos 
    345  1.1  christos /*******************************************************************************
    346  1.1  christos  *
    347  1.1  christos  * FUNCTION:    CgWriteAmlDefBlockComment
    348  1.1  christos  *
    349  1.1  christos  * PARAMETERS:  Op              - Current parse op
    350  1.1  christos  *
    351  1.1  christos  * RETURN:      None
    352  1.1  christos  *
    353  1.1  christos  * DESCRIPTION: Write all comments for a particular definition block.
    354  1.1  christos  *              For definition blocks, the comments need to come after the
    355  1.1  christos  *              definition block header. The regular comments above the
    356  1.1  christos  *              definition block would be categorized as
    357  1.1  christos  *              STD_DEFBLK_COMMENT and comments after the closing brace
    358  1.1  christos  *              is categorized as END_DEFBLK_COMMENT.
    359  1.1  christos  *
    360  1.1  christos  ******************************************************************************/
    361  1.1  christos 
    362  1.1  christos void
    363  1.1  christos CgWriteAmlDefBlockComment(
    364  1.1  christos     ACPI_PARSE_OBJECT       *Op)
    365  1.1  christos {
    366  1.1  christos     UINT8                   CommentOption;
    367  1.1  christos     ACPI_COMMENT_NODE       *Current;
    368  1.1  christos     char                    *NewFilename;
    369  1.1  christos     char                    *Position;
    370  1.1  christos     char                    *DirectoryPosition;
    371  1.1  christos 
    372  1.1  christos 
    373  1.1  christos     if (!Gbl_CaptureComments ||
    374  1.1  christos         (Op->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
    375  1.1  christos     {
    376  1.1  christos         return;
    377  1.1  christos     }
    378  1.1  christos 
    379  1.1  christos     CvDbgPrint ("Printing comments for a definition block..\n");
    380  1.1  christos 
    381  1.1  christos     /* first, print the file name comment after changing .asl to .dsl */
    382  1.1  christos 
    383  1.1  christos     NewFilename = UtStringCacheCalloc (strlen (Op->Asl.Filename));
    384  1.1  christos     strcpy (NewFilename, Op->Asl.Filename);
    385  1.1  christos     DirectoryPosition = strrchr (NewFilename, '/');
    386  1.1  christos     Position = strrchr (NewFilename, '.');
    387  1.1  christos 
    388  1.1  christos     if (Position && (Position > DirectoryPosition))
    389  1.1  christos     {
    390  1.1  christos         /* Tack on the new suffix */
    391  1.1  christos 
    392  1.1  christos         Position++;
    393  1.1  christos         *Position = 0;
    394  1.1  christos         strcat (Position, FILE_SUFFIX_DISASSEMBLY);
    395  1.1  christos     }
    396  1.1  christos     else
    397  1.1  christos     {
    398  1.1  christos         /* No dot, add one and then the suffix */
    399  1.1  christos 
    400  1.1  christos         strcat (NewFilename, ".");
    401  1.1  christos         strcat (NewFilename, FILE_SUFFIX_DISASSEMBLY);
    402  1.1  christos     }
    403  1.1  christos 
    404  1.1  christos     CommentOption = FILENAME_COMMENT;
    405  1.1  christos     CgWriteOneAmlComment(Op, NewFilename, CommentOption);
    406  1.1  christos 
    407  1.1  christos     Current = Op->Asl.CommentList;
    408  1.1  christos     CommentOption = STD_DEFBLK_COMMENT;
    409  1.1  christos     while (Current)
    410  1.1  christos     {
    411  1.1  christos         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
    412  1.1  christos         CvDbgPrint ("Printing comment: %s\n", Current->Comment);
    413  1.1  christos         Current = Current->Next;
    414  1.1  christos     }
    415  1.1  christos     Op->Asl.CommentList = NULL;
    416  1.1  christos 
    417  1.1  christos     /* print any Inline comments associated with this node */
    418  1.1  christos 
    419  1.1  christos     if (Op->Asl.CloseBraceComment)
    420  1.1  christos     {
    421  1.1  christos         CommentOption = END_DEFBLK_COMMENT;
    422  1.1  christos         CgWriteOneAmlComment(Op, Op->Asl.CloseBraceComment, CommentOption);
    423  1.1  christos         Op->Asl.CloseBraceComment = NULL;
    424  1.1  christos     }
    425  1.1  christos }
    426  1.1  christos 
    427  1.1  christos 
    428  1.1  christos /*******************************************************************************
    429  1.1  christos  *
    430  1.1  christos  * FUNCTION:    CgWriteOneAmlComment
    431  1.1  christos  *
    432  1.1  christos  * PARAMETERS:  Op              - Current parse op
    433  1.1  christos  *              CommentToPrint  - Comment that's printed
    434  1.1  christos  *              InputOption     - Denotes the comment option.
    435  1.1  christos  *
    436  1.1  christos  * RETURN:      None
    437  1.1  christos  *
    438  1.1  christos  * DESCRIPTION: write a single comment.
    439  1.1  christos  *
    440  1.1  christos  ******************************************************************************/
    441  1.1  christos 
    442  1.1  christos void
    443  1.1  christos CgWriteOneAmlComment(
    444  1.1  christos     ACPI_PARSE_OBJECT       *Op,
    445  1.1  christos     char*                   CommentToPrint,
    446  1.1  christos     UINT8                   InputOption)
    447  1.1  christos {
    448  1.1  christos     UINT8 CommentOption = InputOption;
    449  1.1  christos     UINT8 CommentOpcode = (UINT8)AML_COMMENT_OP;
    450  1.1  christos 
    451  1.1  christos     CgLocalWriteAmlData (Op, &CommentOpcode, 1);
    452  1.1  christos     CgLocalWriteAmlData (Op, &CommentOption, 1);
    453  1.1  christos 
    454  1.1  christos     /* The strlen (..) + 1 is to include the null terminator */
    455  1.1  christos 
    456  1.1  christos     CgLocalWriteAmlData (Op, CommentToPrint, strlen (CommentToPrint) + 1);
    457  1.1  christos }
    458  1.1  christos 
    459  1.1  christos 
    460  1.1  christos /*******************************************************************************
    461  1.1  christos  *
    462  1.1  christos  * FUNCTION:    CgWriteAmlComment
    463  1.1  christos  *
    464  1.1  christos  * PARAMETERS:  Op              - Current parse op
    465  1.1  christos  *
    466  1.1  christos  * RETURN:      None
    467  1.1  christos  *
    468  1.1  christos  * DESCRIPTION: write all comments pertaining to the
    469  1.1  christos  *              current parse op
    470  1.1  christos  *
    471  1.1  christos  ******************************************************************************/
    472  1.1  christos 
    473  1.1  christos void
    474  1.1  christos CgWriteAmlComment(
    475  1.1  christos     ACPI_PARSE_OBJECT       *Op)
    476  1.1  christos {
    477  1.1  christos     ACPI_COMMENT_NODE       *Current;
    478  1.1  christos     UINT8                   CommentOption;
    479  1.1  christos     char                    *NewFilename;
    480  1.1  christos     char                    *ParentFilename;
    481  1.1  christos 
    482  1.1  christos 
    483  1.1  christos     if ((Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK) ||
    484  1.1  christos          !Gbl_CaptureComments)
    485  1.1  christos     {
    486  1.1  christos         return;
    487  1.1  christos     }
    488  1.1  christos 
    489  1.1  christos     /* Print out the filename comment if needed */
    490  1.1  christos 
    491  1.1  christos     if (Op->Asl.FileChanged)
    492  1.1  christos     {
    493  1.1  christos 
    494  1.1  christos         /* first, print the file name comment after changing .asl to .dsl */
    495  1.1  christos 
    496  1.1  christos         NewFilename =
    497  1.1  christos             FlGenerateFilename (Op->Asl.Filename, FILE_SUFFIX_DISASSEMBLY);
    498  1.1  christos         CvDbgPrint ("Writing file comment, \"%s\" for %s\n",
    499  1.1  christos             NewFilename, Op->Asl.ParseOpName);
    500  1.1  christos         CgWriteOneAmlComment(Op, NewFilename, FILENAME_COMMENT);
    501  1.1  christos 
    502  1.1  christos         if (Op->Asl.ParentFilename &&
    503  1.1  christos             AcpiUtStricmp (Op->Asl.ParentFilename, Op->Asl.Filename))
    504  1.1  christos         {
    505  1.1  christos             ParentFilename = FlGenerateFilename (Op->Asl.ParentFilename,
    506  1.1  christos                 FILE_SUFFIX_DISASSEMBLY);
    507  1.1  christos             CgWriteOneAmlComment(Op, ParentFilename, PARENTFILENAME_COMMENT);
    508  1.1  christos         }
    509  1.1  christos 
    510  1.1  christos         /* prevent multiple writes of the same comment */
    511  1.1  christos 
    512  1.1  christos         Op->Asl.FileChanged = FALSE;
    513  1.1  christos     }
    514  1.1  christos 
    515  1.1  christos     /*
    516  1.1  christos      * Regular comments are stored in a list of comments within an Op.
    517  1.1  christos      * If there is a such list in this node, print out the comment
    518  1.1  christos      * as byte code.
    519  1.1  christos      */
    520  1.1  christos     Current = Op->Asl.CommentList;
    521  1.1  christos     if (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)
    522  1.1  christos     {
    523  1.1  christos         CommentOption = INCLUDE_COMMENT;
    524  1.1  christos     }
    525  1.1  christos     else
    526  1.1  christos     {
    527  1.1  christos         CommentOption = STANDARD_COMMENT;
    528  1.1  christos     }
    529  1.1  christos 
    530  1.1  christos     while (Current)
    531  1.1  christos     {
    532  1.1  christos         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
    533  1.1  christos         Current = Current->Next;
    534  1.1  christos     }
    535  1.1  christos     Op->Asl.CommentList = NULL;
    536  1.1  christos 
    537  1.1  christos     Current = Op->Asl.EndBlkComment;
    538  1.1  christos     CommentOption = ENDBLK_COMMENT;
    539  1.1  christos     while (Current)
    540  1.1  christos     {
    541  1.1  christos         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
    542  1.1  christos         Current = Current->Next;
    543  1.1  christos     }
    544  1.1  christos     Op->Asl.EndBlkComment = NULL;
    545  1.1  christos 
    546  1.1  christos     /* print any Inline comments associated with this node */
    547  1.1  christos 
    548  1.1  christos     if (Op->Asl.InlineComment)
    549  1.1  christos     {
    550  1.1  christos         CommentOption = INLINE_COMMENT;
    551  1.1  christos         CgWriteOneAmlComment(Op, Op->Asl.InlineComment, CommentOption);
    552  1.1  christos         Op->Asl.InlineComment = NULL;
    553  1.1  christos     }
    554  1.1  christos 
    555  1.1  christos     if (Op->Asl.EndNodeComment)
    556  1.1  christos     {
    557  1.1  christos         CommentOption = ENDNODE_COMMENT;
    558  1.1  christos         CgWriteOneAmlComment(Op, Op->Asl.EndNodeComment, CommentOption);
    559  1.1  christos         Op->Asl.EndNodeComment = NULL;
    560  1.1  christos     }
    561  1.1  christos 
    562  1.1  christos     if (Op->Asl.CloseBraceComment)
    563  1.1  christos     {
    564  1.1  christos         CommentOption = CLOSE_BRACE_COMMENT;
    565  1.1  christos         CgWriteOneAmlComment(Op, Op->Asl.CloseBraceComment, CommentOption);
    566  1.1  christos         Op->Asl.CloseBraceComment = NULL;
    567  1.1  christos     }
    568  1.1  christos }
    569  1.1  christos 
    570  1.1  christos 
    571  1.1  christos /*******************************************************************************
    572  1.1  christos  *
    573  1.1  christos  * FUNCTION:    CvCommentNodeCalloc
    574  1.1  christos  *
    575  1.1  christos  * PARAMETERS:  none
    576  1.1  christos  *
    577  1.1  christos  * RETURN:      Pointer to the comment node. Aborts on allocation failure
    578  1.1  christos  *
    579  1.1  christos  * DESCRIPTION: Allocate a string node buffer.
    580  1.1  christos  *
    581  1.1  christos  ******************************************************************************/
    582  1.1  christos 
    583  1.1  christos ACPI_COMMENT_NODE*
    584  1.1  christos CvCommentNodeCalloc (
    585  1.1  christos     void)
    586  1.1  christos {
    587  1.1  christos    ACPI_COMMENT_NODE        *NewCommentNode;
    588  1.1  christos 
    589  1.1  christos 
    590  1.1  christos    NewCommentNode =
    591  1.1  christos        (ACPI_COMMENT_NODE*) UtLocalCalloc (sizeof(ACPI_COMMENT_NODE));
    592  1.1  christos    NewCommentNode->Next = NULL;
    593  1.1  christos    return NewCommentNode;
    594  1.1  christos }
    595  1.1  christos 
    596  1.1  christos 
    597  1.1  christos /*******************************************************************************
    598  1.1  christos  *
    599  1.1  christos  * FUNCTION:    CvParseOpBlockType
    600  1.1  christos  *
    601  1.1  christos  * PARAMETERS:  Op              - Object to be examined
    602  1.1  christos  *
    603  1.1  christos  * RETURN:      BlockType - not a block, parens, braces, or even both.
    604  1.1  christos  *
    605  1.1  christos  * DESCRIPTION: Type of block for this ASL parseop (parens or braces)
    606  1.1  christos  *              keep this in sync with aslprimaries.y, aslresources.y and
    607  1.1  christos  *              aslrules.y
    608  1.1  christos  *
    609  1.1  christos  ******************************************************************************/
    610  1.1  christos 
    611  1.1  christos UINT32
    612  1.1  christos CvParseOpBlockType (
    613  1.1  christos     ACPI_PARSE_OBJECT       *Op)
    614  1.1  christos {
    615  1.1  christos     if (!Op)
    616  1.1  christos     {
    617  1.1  christos         return (BLOCK_NONE);
    618  1.1  christos     }
    619  1.1  christos 
    620  1.1  christos     switch (Op->Asl.ParseOpcode)
    621  1.1  christos     {
    622  1.1  christos 
    623  1.1  christos     /* from aslprimaries.y */
    624  1.1  christos 
    625  1.1  christos     case PARSEOP_VAR_PACKAGE:
    626  1.1  christos     case PARSEOP_BANKFIELD:
    627  1.1  christos     case PARSEOP_BUFFER:
    628  1.1  christos     case PARSEOP_CASE:
    629  1.1  christos     case PARSEOP_DEVICE:
    630  1.1  christos     case PARSEOP_FIELD:
    631  1.1  christos     case PARSEOP_FOR:
    632  1.1  christos     case PARSEOP_FUNCTION:
    633  1.1  christos     case PARSEOP_IF:
    634  1.1  christos     case PARSEOP_ELSEIF:
    635  1.1  christos     case PARSEOP_INDEXFIELD:
    636  1.1  christos     case PARSEOP_METHOD:
    637  1.1  christos     case PARSEOP_POWERRESOURCE:
    638  1.1  christos     case PARSEOP_PROCESSOR:
    639  1.1  christos     case PARSEOP_DATABUFFER:
    640  1.1  christos     case PARSEOP_SCOPE:
    641  1.1  christos     case PARSEOP_SWITCH:
    642  1.1  christos     case PARSEOP_THERMALZONE:
    643  1.1  christos     case PARSEOP_WHILE:
    644  1.1  christos 
    645  1.1  christos     /* from aslresources.y */
    646  1.1  christos 
    647  1.1  christos     case PARSEOP_RESOURCETEMPLATE: /* optional parens */
    648  1.1  christos     case PARSEOP_VENDORLONG:
    649  1.1  christos     case PARSEOP_VENDORSHORT:
    650  1.1  christos     case PARSEOP_INTERRUPT:
    651  1.1  christos     case PARSEOP_IRQNOFLAGS:
    652  1.1  christos     case PARSEOP_IRQ:
    653  1.1  christos     case PARSEOP_GPIO_INT:
    654  1.1  christos     case PARSEOP_GPIO_IO:
    655  1.1  christos     case PARSEOP_DMA:
    656  1.1  christos 
    657  1.1  christos     /*from aslrules.y */
    658  1.1  christos 
    659  1.1  christos     case PARSEOP_DEFINITION_BLOCK:
    660  1.1  christos         return (BLOCK_PAREN | BLOCK_BRACE);
    661  1.1  christos 
    662  1.1  christos     default:
    663  1.1  christos 
    664  1.1  christos         return (BLOCK_NONE);
    665  1.1  christos     }
    666  1.1  christos }
    667  1.1  christos 
    668  1.1  christos 
    669  1.1  christos /*******************************************************************************
    670  1.1  christos  *
    671  1.1  christos  * FUNCTION:    CvProcessCommentState
    672  1.1  christos  *
    673  1.1  christos  * PARAMETERS:  char
    674  1.1  christos  *
    675  1.1  christos  * RETURN:      None
    676  1.1  christos  *
    677  1.1  christos  * DESCRIPTION: Take the given input. If this character is
    678  1.1  christos  *              defined as a comment table entry, then update the state
    679  1.1  christos  *              accordingly.
    680  1.1  christos  *
    681  1.1  christos  ******************************************************************************/
    682  1.1  christos 
    683  1.1  christos void
    684  1.1  christos CvProcessCommentState (
    685  1.1  christos     char                    input)
    686  1.1  christos {
    687  1.1  christos 
    688  1.1  christos     if (input != ' ')
    689  1.1  christos     {
    690  1.1  christos         Gbl_CommentState.SpacesBefore = 0;
    691  1.1  christos     }
    692  1.1  christos 
    693  1.1  christos     switch (input)
    694  1.1  christos     {
    695  1.1  christos     case '\n':
    696  1.1  christos 
    697  1.1  christos         Gbl_CommentState.CommentType = ASL_COMMENT_STANDARD;
    698  1.1  christos         break;
    699  1.1  christos 
    700  1.1  christos     case ' ':
    701  1.1  christos 
    702  1.1  christos         /* Keep the CommentType the same */
    703  1.1  christos 
    704  1.1  christos         Gbl_CommentState.SpacesBefore++;
    705  1.1  christos         break;
    706  1.1  christos 
    707  1.1  christos     case '(':
    708  1.1  christos 
    709  1.1  christos         Gbl_CommentState.CommentType = ASL_COMMENT_OPEN_PAREN;
    710  1.1  christos         break;
    711  1.1  christos 
    712  1.1  christos     case ')':
    713  1.1  christos 
    714  1.1  christos         Gbl_CommentState.CommentType = ASL_COMMENT_CLOSE_PAREN;
    715  1.1  christos         break;
    716  1.1  christos 
    717  1.1  christos     case '{':
    718  1.1  christos 
    719  1.1  christos         Gbl_CommentState.CommentType = ASL_COMMENT_STANDARD;
    720  1.1  christos         Gbl_CommentState.ParsingParenBraceNode = NULL;
    721  1.1  christos         CvDbgPrint ("End Parsing paren/Brace node!\n");
    722  1.1  christos         break;
    723  1.1  christos 
    724  1.1  christos     case '}':
    725  1.1  christos 
    726  1.1  christos         Gbl_CommentState.CommentType = ASL_COMMENT_CLOSE_BRACE;
    727  1.1  christos         break;
    728  1.1  christos 
    729  1.1  christos     case ',':
    730  1.1  christos 
    731  1.1  christos         Gbl_CommentState.CommentType = ASLCOMMENT_INLINE;
    732  1.1  christos         break;
    733  1.1  christos 
    734  1.1  christos     default:
    735  1.1  christos 
    736  1.1  christos         Gbl_CommentState.CommentType = ASLCOMMENT_INLINE;
    737  1.1  christos         break;
    738  1.1  christos 
    739  1.1  christos     }
    740  1.1  christos }
    741  1.1  christos 
    742  1.1  christos 
    743  1.1  christos /*******************************************************************************
    744  1.1  christos  *
    745  1.1  christos  * FUNCTION:    CvAddToCommentList
    746  1.1  christos  *
    747  1.1  christos  * PARAMETERS:  toAdd              - Contains the comment to be inserted
    748  1.1  christos  *
    749  1.1  christos  * RETURN:      None
    750  1.1  christos  *
    751  1.1  christos  * DESCRIPTION: Add the given char* to a list of comments in the global list
    752  1.1  christos  *              of comments.
    753  1.1  christos  *
    754  1.1  christos  ******************************************************************************/
    755  1.1  christos 
    756  1.1  christos void
    757  1.1  christos CvAddToCommentList (
    758  1.1  christos     char*                   ToAdd)
    759  1.1  christos {
    760  1.1  christos    if (Gbl_Comment_List_Head)
    761  1.1  christos    {
    762  1.1  christos        Gbl_Comment_List_Tail->Next = CvCommentNodeCalloc ();
    763  1.1  christos        Gbl_Comment_List_Tail = Gbl_Comment_List_Tail->Next;
    764  1.1  christos    }
    765  1.1  christos    else
    766  1.1  christos    {
    767  1.1  christos        Gbl_Comment_List_Head = CvCommentNodeCalloc ();
    768  1.1  christos        Gbl_Comment_List_Tail = Gbl_Comment_List_Head;
    769  1.1  christos    }
    770  1.1  christos 
    771  1.1  christos    Gbl_Comment_List_Tail->Comment = ToAdd;
    772  1.1  christos 
    773  1.1  christos    return;
    774  1.1  christos }
    775  1.1  christos 
    776  1.1  christos /*******************************************************************************
    777  1.1  christos  *
    778  1.1  christos  * FUNCTION:    CvAppendInlineComment
    779  1.1  christos  *
    780  1.1  christos  * PARAMETERS:  InlineComment      - Append to the end of this string.
    781  1.1  christos  *              toAdd              - Contains the comment to be inserted
    782  1.1  christos  *
    783  1.1  christos  * RETURN:      Str                - toAdd appended to InlineComment
    784  1.1  christos  *
    785  1.1  christos  * DESCRIPTION: Concatenate ToAdd to InlineComment
    786  1.1  christos  *
    787  1.1  christos  ******************************************************************************/
    788  1.1  christos 
    789  1.1  christos char*
    790  1.1  christos CvAppendInlineComment (
    791  1.1  christos     char                    *InlineComment,
    792  1.1  christos     char                    *ToAdd)
    793  1.1  christos {
    794  1.1  christos     char*                   Str;
    795  1.1  christos     UINT32                  Size = 0;
    796  1.1  christos 
    797  1.1  christos 
    798  1.1  christos     if (!InlineComment)
    799  1.1  christos     {
    800  1.1  christos         return ToAdd;
    801  1.1  christos     }
    802  1.1  christos     if (ToAdd)
    803  1.1  christos     {
    804  1.1  christos         Size = strlen (ToAdd);
    805  1.1  christos     }
    806  1.1  christos     Size += strlen (InlineComment);
    807  1.1  christos     Str = UtStringCacheCalloc (Size+1);
    808  1.1  christos     strcpy (Str, InlineComment);
    809  1.1  christos     strcat (Str, ToAdd);
    810  1.1  christos     Str[Size+1] = 0;
    811  1.1  christos 
    812  1.1  christos     return Str;
    813  1.1  christos }
    814  1.1  christos 
    815  1.1  christos 
    816  1.1  christos /*******************************************************************************
    817  1.1  christos  *
    818  1.1  christos  * FUNCTION:    CvPlaceComment
    819  1.1  christos  *
    820  1.1  christos  * PARAMETERS:  Int           - Type
    821  1.1  christos  *              char*         - CommentString
    822  1.1  christos  *
    823  1.1  christos  * RETURN:      None
    824  1.1  christos  *
    825  1.1  christos  * DESCRIPTION: Given type and CommentString, this function places the
    826  1.1  christos  *              CommentString in the approperiate global comment list or char*
    827  1.1  christos  *
    828  1.1  christos  ******************************************************************************/
    829  1.1  christos 
    830  1.1  christos void
    831  1.1  christos CvPlaceComment(
    832  1.1  christos     UINT8                   Type,
    833  1.1  christos     char                    *CommentString)
    834  1.1  christos {
    835  1.1  christos     ACPI_PARSE_OBJECT       *LatestParseNode;
    836  1.1  christos     ACPI_PARSE_OBJECT       *ParenBraceNode;
    837  1.1  christos 
    838  1.1  christos 
    839  1.1  christos     LatestParseNode = Gbl_CommentState.Latest_Parse_Node;
    840  1.1  christos     ParenBraceNode  = Gbl_CommentState.ParsingParenBraceNode;
    841  1.1  christos     CvDbgPrint ("Placing comment %s for type %d\n", CommentString, Type);
    842  1.1  christos 
    843  1.1  christos     switch (Type)
    844  1.1  christos     {
    845  1.1  christos     case ASL_COMMENT_STANDARD:
    846  1.1  christos 
    847  1.1  christos         CvAddToCommentList (CommentString);
    848  1.1  christos         break;
    849  1.1  christos 
    850  1.1  christos     case ASLCOMMENT_INLINE:
    851  1.1  christos 
    852  1.1  christos         LatestParseNode->Asl.InlineComment =
    853  1.1  christos             CvAppendInlineComment (LatestParseNode->Asl.InlineComment,
    854  1.1  christos             CommentString);
    855  1.1  christos         break;
    856  1.1  christos 
    857  1.1  christos     case ASL_COMMENT_OPEN_PAREN:
    858  1.1  christos 
    859  1.1  christos         Gbl_Inline_Comment_Buffer =
    860  1.1  christos             CvAppendInlineComment(Gbl_Inline_Comment_Buffer,
    861  1.1  christos             CommentString);
    862  1.1  christos         break;
    863  1.1  christos 
    864  1.1  christos     case ASL_COMMENT_CLOSE_PAREN:
    865  1.1  christos 
    866  1.1  christos         if (ParenBraceNode)
    867  1.1  christos         {
    868  1.1  christos             ParenBraceNode->Asl.EndNodeComment =
    869  1.1  christos                 CvAppendInlineComment (ParenBraceNode->Asl.EndNodeComment,
    870  1.1  christos                 CommentString);
    871  1.1  christos         }
    872  1.1  christos         else
    873  1.1  christos         {
    874  1.1  christos             LatestParseNode->Asl.EndNodeComment =
    875  1.1  christos                 CvAppendInlineComment (LatestParseNode->Asl.EndNodeComment,
    876  1.1  christos                 CommentString);
    877  1.1  christos         }
    878  1.1  christos         break;
    879  1.1  christos 
    880  1.1  christos     case ASL_COMMENT_CLOSE_BRACE:
    881  1.1  christos 
    882  1.1  christos         LatestParseNode->Asl.CloseBraceComment = CommentString;
    883  1.1  christos         break;
    884  1.1  christos 
    885  1.1  christos     default:
    886  1.1  christos 
    887  1.1  christos         break;
    888  1.1  christos 
    889  1.1  christos     }
    890  1.1  christos }
    891