]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/compiler/cvcompiler.c
Import ACPICA 20170303.
[FreeBSD/FreeBSD.git] / source / compiler / cvcompiler.c
1 /******************************************************************************
2  *
3  * Module Name: cvcompiler - ASL-/ASL+ converter functions
4  *
5  *****************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************
115  *
116  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151
152 #include "aslcompiler.h"
153 #include "aslcompiler.y.h"
154 #include "amlcode.h"
155 #include "acapps.h"
156 #include "acconvert.h"
157
158
159 /*******************************************************************************
160  *
161  * FUNCTION:    CvProcessComment
162  *
163  * PARAMETERS:  CurrentState      Current comment parse state
164  *              StringBuffer      Buffer containing the comment being processed
165  *              c1                Current input
166  *
167  * RETURN:      none
168  *
169  * DESCRIPTION: Process a single line comment of a c Style comment. This
170  *              function captures a line of a c style comment in a char* and
171  *              places the comment in the approperiate global buffer.
172  *
173  ******************************************************************************/
174
175 void
176 CvProcessComment (
177     ASL_COMMENT_STATE       CurrentState,
178     char                    *StringBuffer,
179     int                     c1)
180 {
181     UINT64                  i;
182     char                    *LineToken;
183     char                    *FinalLineToken;
184     BOOLEAN                 CharStart;
185     char                    *CommentString;
186     char                    *FinalCommentString;
187
188
189     if (Gbl_CaptureComments && CurrentState.CaptureComments)
190     {
191         *StringBuffer = (char) c1;
192         ++StringBuffer;
193         *StringBuffer = 0;
194         CvDbgPrint ("Multi-line comment\n");
195         CommentString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
196         strcpy (CommentString, MsgBuffer);
197
198         CvDbgPrint ("CommentString: %s\n", CommentString);
199
200         /*
201          * Determine whether if this comment spans multiple lines.
202          * If so, break apart the comment by line so that it can be
203          * properly indented.
204          */
205         if (strchr (CommentString, '\n') != NULL)
206         {
207             /*
208              * Get the first token. The for loop pads subsequent lines
209              * for comments similar to the style of this comment.
210              */
211             LineToken = strtok (CommentString, "\n");
212             FinalLineToken = UtStringCacheCalloc (strlen (LineToken) + 1);
213             strcpy (FinalLineToken, LineToken);
214
215             /* Get rid of any carriage returns */
216
217             if (FinalLineToken[strlen (FinalLineToken) - 1] == 0x0D)
218             {
219                 FinalLineToken[strlen(FinalLineToken)-1] = 0;
220             }
221             CvAddToCommentList (FinalLineToken);
222             LineToken = strtok (NULL, "\n");
223             while (LineToken != NULL)
224             {
225                 /*
226                  * It is assumed that each line has some sort of indentation.
227                  * This means that we need to find the first character that is not
228                  * a white space within each line.
229                  */
230                 CharStart = FALSE;
231                 for (i = 0; (i < (strlen (LineToken) + 1)) && !CharStart; i++)
232                 {
233                     if (LineToken[i] != ' ' && LineToken[i] != '\t')
234                     {
235                         CharStart = TRUE;
236                         LineToken += i-1;
237                         LineToken [0] = ' '; /* Pad for Formatting */
238                     }
239                 }
240                 FinalLineToken = UtStringCacheCalloc (strlen (LineToken) + 1);
241                 strcat (FinalLineToken, LineToken);
242
243                 /* Get rid of any carriage returns */
244
245                 if (FinalLineToken[strlen (FinalLineToken) - 1] == 0x0D)
246                 {
247                     FinalLineToken[strlen(FinalLineToken) - 1] = 0;
248                 }
249                 CvAddToCommentList (FinalLineToken);
250                 LineToken = strtok (NULL,"\n");
251             }
252         }
253
254         /*
255          * If this only spans a single line, check to see whether if this comment
256          * appears on the same line as a line of code. If does, retain it's
257          * position for stylistic reasons. If it doesn't, add it to the comment
258          * List so that it can be associated with the next node that's created.
259          */
260         else
261         {
262            /*
263             * if this is not a regular comment, pad with extra spaces that appeared
264             * in the original source input to retain the original spacing.
265             */
266             FinalCommentString = UtStringCacheCalloc (strlen (CommentString) + CurrentState.SpacesBefore + 1);
267             for (i=0; (CurrentState.CommentType != ASL_COMMENT_STANDARD) &&
268                 (i < CurrentState.SpacesBefore); ++i)
269             {
270                  FinalCommentString[i] = ' ';
271             }
272             strcat (FinalCommentString, CommentString);
273             CvPlaceComment (CurrentState.CommentType, FinalCommentString);
274         }
275     }
276 }
277
278
279 /*******************************************************************************
280  *
281  * FUNCTION:    CvProcessCommentType2
282  *
283  * PARAMETERS:  CurrentState      Current comment parse state
284  *              StringBuffer      Buffer containing the comment being processed
285  *
286  * RETURN:      none
287  *
288  * DESCRIPTION: Process a single line comment. This function captures a comment
289  *              in a char* and places the comment in the approperiate global
290  *              buffer through CvPlaceComment
291  *
292  ******************************************************************************/
293
294 void
295 CvProcessCommentType2 (
296     ASL_COMMENT_STATE       CurrentState,
297     char                    *StringBuffer)
298 {
299     UINT32                  i;
300     char                    *CommentString;
301     char                    *FinalCommentString;
302
303
304     if (Gbl_CaptureComments && CurrentState.CaptureComments)
305     {
306         *StringBuffer = 0; /* null terminate */
307         CvDbgPrint ("Single-line comment\n");
308         CommentString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
309         strcpy (CommentString, MsgBuffer);
310
311         /* If this comment lies on the same line as the latest parse node,
312          * assign it to that node's CommentAfter field. Saving in this field
313          * will allow us to support comments that come after code on the same
314          * line as the code itself. For example,
315          * Name(A,"") //comment
316          *
317          * will be retained rather than transformed into
318          *
319          * Name(A,"")
320          * //comment
321          *
322          * For this case, we only need to add one comment since
323          *
324          * Name(A,"") //comment1 //comment2 ... more comments here.
325          *
326          * would be lexically analyzed as a single comment.
327          *
328          * Create a new string with the approperiate spaces. Since we need
329          * to account for the proper spacing, the actual comment,
330          * extra 2 spaces so that this comment can be converted to the "/ *"
331          * style and the null terminator, the string would look something like
332          *
333          * [ (spaces) (comment)  ( * /) ('\0') ]
334          *
335          */
336         FinalCommentString = UtStringCacheCalloc (CurrentState.SpacesBefore + strlen (CommentString) + 3 + 1);
337         for (i=0; (CurrentState.CommentType!=1) && (i<CurrentState.SpacesBefore); ++i)
338         {
339             FinalCommentString[i] = ' ';
340         }
341         strcat (FinalCommentString, CommentString);
342
343         /* convert to a "/ *" style comment  */
344
345         strcat (FinalCommentString, " */");
346         FinalCommentString [CurrentState.SpacesBefore + strlen (CommentString) + 3] = 0;
347
348         /* get rid of the carriage return */
349
350         if (FinalCommentString[strlen (FinalCommentString) - 1] == 0x0D)
351         {
352             FinalCommentString[strlen(FinalCommentString)-1] = 0;
353         }
354         CvPlaceComment (CurrentState.CommentType, FinalCommentString);
355     }
356 }
357
358
359 /*******************************************************************************
360  *
361  * FUNCTION:    CgCalculateCommentLengths
362  *
363  * PARAMETERS:  Op                 - Calculate all comments of this Op
364  *
365  * RETURN:      TotalCommentLength - Length of all comments within this node.
366  *
367  * DESCRIPTION: calculate the length that the each comment takes up within Op.
368  *              Comments look like the follwoing: [0xA9 OptionBtye comment 0x00]
369  *              therefore, we add 1 + 1 + strlen (comment) + 1 to get the actual
370  *              length of this comment.
371  *
372  ******************************************************************************/
373
374 UINT32
375 CvCalculateCommentLengths(
376    ACPI_PARSE_OBJECT        *Op)
377 {
378     UINT32                  CommentLength = 0;
379     UINT32                  TotalCommentLength = 0;
380     ACPI_COMMENT_NODE       *Current = NULL;
381
382
383     if (!Gbl_CaptureComments)
384     {
385         return (0);
386     }
387
388     CvDbgPrint ("==Calculating comment lengths for %s\n",  Op->Asl.ParseOpName);
389     if (Op->Asl.FileChanged)
390     {
391         TotalCommentLength += strlen (Op->Asl.Filename) + 3;
392
393         if (Op->Asl.ParentFilename &&
394             AcpiUtStricmp (Op->Asl.Filename, Op->Asl.ParentFilename))
395         {
396             TotalCommentLength += strlen (Op->Asl.ParentFilename) + 3;
397         }
398     }
399     if (Op->Asl.CommentList)
400     {
401         Current = Op->Asl.CommentList;
402         while (Current)
403         {
404             CommentLength = strlen (Current->Comment)+3;
405             CvDbgPrint ("Length of standard comment: %d\n", CommentLength);
406             CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
407             TotalCommentLength += CommentLength;
408             Current = Current->Next;
409         }
410     }
411     if (Op->Asl.EndBlkComment)
412     {
413         Current = Op->Asl.EndBlkComment;
414         while (Current)
415         {
416             CommentLength = strlen (Current->Comment)+3;
417             CvDbgPrint ("Length of endblkcomment: %d\n", CommentLength);
418             CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
419             TotalCommentLength += CommentLength;
420             Current = Current->Next;
421         }
422     }
423     if (Op->Asl.InlineComment)
424     {
425         CommentLength = strlen (Op->Asl.InlineComment)+3;
426         CvDbgPrint ("Length of inline comment: %d\n", CommentLength);
427         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.InlineComment);
428         TotalCommentLength += CommentLength;
429     }
430     if (Op->Asl.EndNodeComment)
431     {
432         CommentLength = strlen(Op->Asl.EndNodeComment)+3;
433         CvDbgPrint ("Length of end node comment +3: %d\n", CommentLength);
434         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.EndNodeComment);
435         TotalCommentLength += CommentLength;
436     }
437
438     if (Op->Asl.CloseBraceComment)
439     {
440         CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
441         CvDbgPrint ("Length of close brace comment: %d\n", CommentLength);
442         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
443         TotalCommentLength += CommentLength;
444     }
445
446     CvDbgPrint("\n\n");
447
448     return TotalCommentLength;
449
450 }
451
452
453 /*******************************************************************************
454  *
455  * FUNCTION:    CgWriteAmlDefBlockComment
456  *
457  * PARAMETERS:  Op              - Current parse op
458  *
459  * RETURN:      None
460  *
461  * DESCRIPTION: Write all comments for a particular definition block.
462  *              For definition blocks, the comments need to come after the
463  *              definition block header. The regular comments above the
464  *              definition block would be categorized as
465  *              STD_DEFBLK_COMMENT and comments after the closing brace
466  *              is categorized as END_DEFBLK_COMMENT.
467  *
468  ******************************************************************************/
469
470 void
471 CgWriteAmlDefBlockComment(
472     ACPI_PARSE_OBJECT       *Op)
473 {
474     UINT8                   CommentOption;
475     ACPI_COMMENT_NODE       *Current;
476     char                    *NewFilename;
477     char                    *Position;
478     char                    *DirectoryPosition;
479
480
481     if (!Gbl_CaptureComments ||
482         (Op->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
483     {
484         return;
485     }
486
487     CvDbgPrint ("Printing comments for a definition block..\n");
488
489     /* first, print the file name comment after changing .asl to .dsl */
490
491     NewFilename = UtStringCacheCalloc (strlen (Op->Asl.Filename));
492     strcpy (NewFilename, Op->Asl.Filename);
493     DirectoryPosition = strrchr (NewFilename, '/');
494     Position = strrchr (NewFilename, '.');
495
496     if (Position && (Position > DirectoryPosition))
497     {
498         /* Tack on the new suffix */
499
500         Position++;
501         *Position = 0;
502         strcat (Position, FILE_SUFFIX_DISASSEMBLY);
503     }
504     else
505     {
506         /* No dot, add one and then the suffix */
507
508         strcat (NewFilename, ".");
509         strcat (NewFilename, FILE_SUFFIX_DISASSEMBLY);
510     }
511
512     CommentOption = FILENAME_COMMENT;
513     CgWriteOneAmlComment(Op, NewFilename, CommentOption);
514
515     Current = Op->Asl.CommentList;
516     CommentOption = STD_DEFBLK_COMMENT;
517     while (Current)
518     {
519         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
520         CvDbgPrint ("Printing comment: %s\n", Current->Comment);
521         Current = Current->Next;
522     }
523     Op->Asl.CommentList = NULL;
524
525     /* print any Inline comments associated with this node */
526
527     if (Op->Asl.CloseBraceComment)
528     {
529         CommentOption = END_DEFBLK_COMMENT;
530         CgWriteOneAmlComment(Op, Op->Asl.CloseBraceComment, CommentOption);
531         Op->Asl.CloseBraceComment = NULL;
532     }
533 }
534
535
536 /*******************************************************************************
537  *
538  * FUNCTION:    CgWriteOneAmlComment
539  *
540  * PARAMETERS:  Op              - Current parse op
541  *              CommentToPrint  - Comment that's printed
542  *              InputOption     - Denotes the comment option.
543  *
544  * RETURN:      None
545  *
546  * DESCRIPTION: write a single comment.
547  *
548  ******************************************************************************/
549
550 void
551 CgWriteOneAmlComment(
552     ACPI_PARSE_OBJECT       *Op,
553     char*                   CommentToPrint,
554     UINT8                   InputOption)
555 {
556     UINT8 CommentOption = InputOption;
557     UINT8 CommentOpcode = (UINT8)AML_COMMENT_OP;
558
559     CgLocalWriteAmlData (Op, &CommentOpcode, 1);
560     CgLocalWriteAmlData (Op, &CommentOption, 1);
561
562     /* The strlen (..) + 1 is to include the null terminator */
563
564     CgLocalWriteAmlData (Op, CommentToPrint, strlen (CommentToPrint) + 1);
565 }
566
567
568 /*******************************************************************************
569  *
570  * FUNCTION:    CgWriteAmlComment
571  *
572  * PARAMETERS:  Op              - Current parse op
573  *
574  * RETURN:      None
575  *
576  * DESCRIPTION: write all comments pertaining to the
577  *              current parse op
578  *
579  ******************************************************************************/
580
581 void
582 CgWriteAmlComment(
583     ACPI_PARSE_OBJECT       *Op)
584 {
585     ACPI_COMMENT_NODE       *Current;
586     UINT8                   CommentOption;
587     char                    *NewFilename;
588     char                    *ParentFilename;
589
590
591     if ((Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK) ||
592          !Gbl_CaptureComments)
593     {
594         return;
595     }
596
597     /* Print out the filename comment if needed */
598
599     if (Op->Asl.FileChanged)
600     {
601
602         /* first, print the file name comment after changing .asl to .dsl */
603
604         NewFilename =
605             FlGenerateFilename (Op->Asl.Filename, FILE_SUFFIX_DISASSEMBLY);
606         CvDbgPrint ("Writing file comment, \"%s\" for %s\n",
607             NewFilename, Op->Asl.ParseOpName);
608         CgWriteOneAmlComment(Op, NewFilename, FILENAME_COMMENT);
609
610         if (Op->Asl.ParentFilename &&
611             AcpiUtStricmp (Op->Asl.ParentFilename, Op->Asl.Filename))
612         {
613             ParentFilename = FlGenerateFilename (Op->Asl.ParentFilename,
614                 FILE_SUFFIX_DISASSEMBLY);
615             CgWriteOneAmlComment(Op, ParentFilename, PARENTFILENAME_COMMENT);
616         }
617
618         /* prevent multiple writes of the same comment */
619
620         Op->Asl.FileChanged = FALSE;
621     }
622
623     /*
624      * Regular comments are stored in a list of comments within an Op.
625      * If there is a such list in this node, print out the comment
626      * as byte code.
627      */
628     Current = Op->Asl.CommentList;
629     if (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)
630     {
631         CommentOption = INCLUDE_COMMENT;
632     }
633     else
634     {
635         CommentOption = STANDARD_COMMENT;
636     }
637
638     while (Current)
639     {
640         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
641         Current = Current->Next;
642     }
643     Op->Asl.CommentList = NULL;
644
645     Current = Op->Asl.EndBlkComment;
646     CommentOption = ENDBLK_COMMENT;
647     while (Current)
648     {
649         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
650         Current = Current->Next;
651     }
652     Op->Asl.EndBlkComment = NULL;
653
654     /* print any Inline comments associated with this node */
655
656     if (Op->Asl.InlineComment)
657     {
658         CommentOption = INLINE_COMMENT;
659         CgWriteOneAmlComment(Op, Op->Asl.InlineComment, CommentOption);
660         Op->Asl.InlineComment = NULL;
661     }
662
663     if (Op->Asl.EndNodeComment)
664     {
665         CommentOption = ENDNODE_COMMENT;
666         CgWriteOneAmlComment(Op, Op->Asl.EndNodeComment, CommentOption);
667         Op->Asl.EndNodeComment = NULL;
668     }
669
670     if (Op->Asl.CloseBraceComment)
671     {
672         CommentOption = CLOSE_BRACE_COMMENT;
673         CgWriteOneAmlComment(Op, Op->Asl.CloseBraceComment, CommentOption);
674         Op->Asl.CloseBraceComment = NULL;
675     }
676 }
677
678
679 /*******************************************************************************
680  *
681  * FUNCTION:    CvCommentNodeCalloc
682  *
683  * PARAMETERS:  none
684  *
685  * RETURN:      Pointer to the comment node. Aborts on allocation failure
686  *
687  * DESCRIPTION: Allocate a string node buffer.
688  *
689  ******************************************************************************/
690
691 ACPI_COMMENT_NODE*
692 CvCommentNodeCalloc (
693     void)
694 {
695    ACPI_COMMENT_NODE        *NewCommentNode;
696
697
698    NewCommentNode =
699        (ACPI_COMMENT_NODE*) UtLocalCalloc (sizeof(ACPI_COMMENT_NODE));
700    NewCommentNode->Next = NULL;
701    return NewCommentNode;
702 }
703
704
705 /*******************************************************************************
706  *
707  * FUNCTION:    CvParseOpBlockType
708  *
709  * PARAMETERS:  Op              - Object to be examined
710  *
711  * RETURN:      BlockType - not a block, parens, braces, or even both.
712  *
713  * DESCRIPTION: Type of block for this ASL parseop (parens or braces)
714  *              keep this in sync with aslprimaries.y, aslresources.y and
715  *              aslrules.y
716  *
717  ******************************************************************************/
718
719 UINT32
720 CvParseOpBlockType (
721     ACPI_PARSE_OBJECT       *Op)
722 {
723     if (!Op)
724     {
725         return (BLOCK_NONE);
726     }
727
728     switch (Op->Asl.ParseOpcode)
729     {
730
731     /* from aslprimaries.y */
732
733     case PARSEOP_VAR_PACKAGE:
734     case PARSEOP_BANKFIELD:
735     case PARSEOP_BUFFER:
736     case PARSEOP_CASE:
737     case PARSEOP_DEVICE:
738     case PARSEOP_FIELD:
739     case PARSEOP_FOR:
740     case PARSEOP_FUNCTION:
741     case PARSEOP_IF:
742     case PARSEOP_ELSEIF:
743     case PARSEOP_INDEXFIELD:
744     case PARSEOP_METHOD:
745     case PARSEOP_POWERRESOURCE:
746     case PARSEOP_PROCESSOR:
747     case PARSEOP_DATABUFFER:
748     case PARSEOP_SCOPE:
749     case PARSEOP_SWITCH:
750     case PARSEOP_THERMALZONE:
751     case PARSEOP_WHILE:
752
753     /* from aslresources.y */
754
755     case PARSEOP_RESOURCETEMPLATE: /* optional parens */
756     case PARSEOP_VENDORLONG:
757     case PARSEOP_VENDORSHORT:
758     case PARSEOP_INTERRUPT:
759     case PARSEOP_IRQNOFLAGS:
760     case PARSEOP_IRQ:
761     case PARSEOP_GPIO_INT:
762     case PARSEOP_GPIO_IO:
763     case PARSEOP_DMA:
764
765     /*from aslrules.y */
766
767     case PARSEOP_DEFINITION_BLOCK:
768         return (BLOCK_PAREN | BLOCK_BRACE);
769
770     default:
771
772         return (BLOCK_NONE);
773     }
774 }
775
776
777 /*******************************************************************************
778  *
779  * FUNCTION:    CvProcessCommentState
780  *
781  * PARAMETERS:  char
782  *
783  * RETURN:      None
784  *
785  * DESCRIPTION: Take the given input. If this character is
786  *              defined as a comment table entry, then update the state
787  *              accordingly.
788  *
789  ******************************************************************************/
790
791 void
792 CvProcessCommentState (
793     char                    input)
794 {
795
796     if (input != ' ')
797     {
798         Gbl_CommentState.SpacesBefore = 0;
799     }
800
801     switch (input)
802     {
803     case '\n':
804
805         Gbl_CommentState.CommentType = ASL_COMMENT_STANDARD;
806         break;
807
808     case ' ':
809
810         /* Keep the CommentType the same */
811
812         Gbl_CommentState.SpacesBefore++;
813         break;
814
815     case '(':
816
817         Gbl_CommentState.CommentType = ASL_COMMENT_OPEN_PAREN;
818         break;
819
820     case ')':
821
822         Gbl_CommentState.CommentType = ASL_COMMENT_CLOSE_PAREN;
823         break;
824
825     case '{':
826
827         Gbl_CommentState.CommentType = ASL_COMMENT_STANDARD;
828         Gbl_CommentState.ParsingParenBraceNode = NULL;
829         CvDbgPrint ("End Parsing paren/Brace node!\n");
830         break;
831
832     case '}':
833
834         Gbl_CommentState.CommentType = ASL_COMMENT_CLOSE_BRACE;
835         break;
836
837     case ',':
838
839         Gbl_CommentState.CommentType = ASLCOMMENT_INLINE;
840         break;
841
842     default:
843
844         Gbl_CommentState.CommentType = ASLCOMMENT_INLINE;
845         break;
846
847     }
848 }
849
850
851 /*******************************************************************************
852  *
853  * FUNCTION:    CvAddToCommentList
854  *
855  * PARAMETERS:  toAdd              - Contains the comment to be inserted
856  *
857  * RETURN:      None
858  *
859  * DESCRIPTION: Add the given char* to a list of comments in the global list
860  *              of comments.
861  *
862  ******************************************************************************/
863
864 void
865 CvAddToCommentList (
866     char*                   ToAdd)
867 {
868    if (Gbl_Comment_List_Head)
869    {
870        Gbl_Comment_List_Tail->Next = CvCommentNodeCalloc ();
871        Gbl_Comment_List_Tail = Gbl_Comment_List_Tail->Next;
872    }
873    else
874    {
875        Gbl_Comment_List_Head = CvCommentNodeCalloc ();
876        Gbl_Comment_List_Tail = Gbl_Comment_List_Head;
877    }
878
879    Gbl_Comment_List_Tail->Comment = ToAdd;
880
881    return;
882 }
883
884 /*******************************************************************************
885  *
886  * FUNCTION:    CvAppendInlineComment
887  *
888  * PARAMETERS:  InlineComment      - Append to the end of this string.
889  *              toAdd              - Contains the comment to be inserted
890  *
891  * RETURN:      Str                - toAdd appended to InlineComment
892  *
893  * DESCRIPTION: Concatenate ToAdd to InlineComment
894  *
895  ******************************************************************************/
896
897 char*
898 CvAppendInlineComment (
899     char                    *InlineComment,
900     char                    *ToAdd)
901 {
902     char*                   Str;
903     UINT32                  Size = 0;
904
905
906     if (!InlineComment)
907     {
908         return ToAdd;
909     }
910     if (ToAdd)
911     {
912         Size = strlen (ToAdd);
913     }
914     Size += strlen (InlineComment);
915     Str = UtStringCacheCalloc (Size+1);
916     strcpy (Str, InlineComment);
917     strcat (Str, ToAdd);
918     Str[Size+1] = 0;
919
920     return Str;
921 }
922
923
924 /*******************************************************************************
925  *
926  * FUNCTION:    CvPlaceComment
927  *
928  * PARAMETERS:  Int           - Type
929  *              char*         - CommentString
930  *
931  * RETURN:      None
932  *
933  * DESCRIPTION: Given type and CommentString, this function places the
934  *              CommentString in the approperiate global comment list or char*
935  *
936  ******************************************************************************/
937
938 void
939 CvPlaceComment(
940     UINT8                   Type,
941     char                    *CommentString)
942 {
943     ACPI_PARSE_OBJECT       *LatestParseNode;
944     ACPI_PARSE_OBJECT       *ParenBraceNode;
945
946
947     LatestParseNode = Gbl_CommentState.Latest_Parse_Node;
948     ParenBraceNode  = Gbl_CommentState.ParsingParenBraceNode;
949     CvDbgPrint ("Placing comment %s for type %d\n", CommentString, Type);
950
951     switch (Type)
952     {
953     case ASL_COMMENT_STANDARD:
954
955         CvAddToCommentList (CommentString);
956         break;
957
958     case ASLCOMMENT_INLINE:
959
960         LatestParseNode->Asl.InlineComment =
961             CvAppendInlineComment (LatestParseNode->Asl.InlineComment,
962             CommentString);
963         break;
964
965     case ASL_COMMENT_OPEN_PAREN:
966
967         Gbl_Inline_Comment_Buffer =
968             CvAppendInlineComment(Gbl_Inline_Comment_Buffer,
969             CommentString);
970         break;
971
972     case ASL_COMMENT_CLOSE_PAREN:
973
974         if (ParenBraceNode)
975         {
976             ParenBraceNode->Asl.EndNodeComment =
977                 CvAppendInlineComment (ParenBraceNode->Asl.EndNodeComment,
978                 CommentString);
979         }
980         else
981         {
982             LatestParseNode->Asl.EndNodeComment =
983                 CvAppendInlineComment (LatestParseNode->Asl.EndNodeComment,
984                 CommentString);
985         }
986         break;
987
988     case ASL_COMMENT_CLOSE_BRACE:
989
990         LatestParseNode->Asl.CloseBraceComment = CommentString;
991         break;
992
993     default:
994
995         break;
996
997     }
998 }