xref: /freebsd/sys/contrib/dev/acpica/compiler/cvcompiler.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
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 <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
154 #include <contrib/dev/acpica/include/amlcode.h>
155 #include <contrib/dev/acpica/include/acapps.h>
156 #include <contrib/dev/acpica/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 
195         CvDbgPrint ("Multi-line comment\n");
196         CommentString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
197         strcpy (CommentString, MsgBuffer);
198 
199         CvDbgPrint ("CommentString: %s\n", CommentString);
200 
201         /*
202          * Determine whether if this comment spans multiple lines. If so,
203          * break apart the comment by storing each line in a different node
204          * within the comment list. This allows the disassembler to
205          * properly indent a multi-line comment.
206          */
207         LineToken = strtok (CommentString, "\n");
208 
209         if (LineToken)
210         {
211             FinalLineToken = UtStringCacheCalloc (strlen (LineToken) + 1);
212             strcpy (FinalLineToken, LineToken);
213 
214             /* Get rid of any carriage returns */
215 
216             if (FinalLineToken[strlen (FinalLineToken) - 1] == 0x0D)
217             {
218                 FinalLineToken[strlen(FinalLineToken)-1] = 0;
219             }
220             CvAddToCommentList (FinalLineToken);
221             LineToken = strtok (NULL, "\n");
222             while (LineToken != NULL)
223             {
224                 /*
225                  * It is assumed that each line has some sort of indentation.
226                  * This means that we need to find the first character that is not
227                  * a white space within each line.
228                  */
229                 CharStart = FALSE;
230                 for (i = 0; (i < (strlen (LineToken) + 1)) && !CharStart; i++)
231                 {
232                     if (LineToken[i] != ' ' && LineToken[i] != '\t')
233                     {
234                         CharStart = TRUE;
235                         LineToken += i-1;
236                         LineToken [0] = ' '; /* Pad for Formatting */
237                     }
238                 }
239                 FinalLineToken = UtStringCacheCalloc (strlen (LineToken) + 1);
240                 strcat (FinalLineToken, LineToken);
241 
242                 /* Get rid of any carriage returns */
243 
244                 if (FinalLineToken[strlen (FinalLineToken) - 1] == 0x0D)
245                 {
246                     FinalLineToken[strlen(FinalLineToken) - 1] = 0;
247                 }
248                 CvAddToCommentList (FinalLineToken);
249                 LineToken = strtok (NULL,"\n");
250             }
251         }
252 
253         /*
254          * If this only spans a single line, check to see whether if this comment
255          * appears on the same line as a line of code. If does, retain it's
256          * position for stylistic reasons. If it doesn't, add it to the comment
257          * List so that it can be associated with the next node that's created.
258          */
259         else
260         {
261            /*
262             * if this is not a regular comment, pad with extra spaces that appeared
263             * in the original source input to retain the original spacing.
264             */
265             FinalCommentString =
266                 UtStringCacheCalloc (strlen (CommentString) +
267                 CurrentState.SpacesBefore + 1);
268 
269             for (i=0; (CurrentState.CommentType != ASL_COMMENT_STANDARD) &&
270                 (i < CurrentState.SpacesBefore); ++i)
271             {
272                  FinalCommentString[i] = ' ';
273             }
274             strcat (FinalCommentString, CommentString);
275             CvPlaceComment (CurrentState.CommentType, FinalCommentString);
276         }
277     }
278 }
279 
280 
281 /*******************************************************************************
282  *
283  * FUNCTION:    CvProcessCommentType2
284  *
285  * PARAMETERS:  CurrentState      Current comment parse state
286  *              StringBuffer      Buffer containing the comment being processed
287  *
288  * RETURN:      none
289  *
290  * DESCRIPTION: Process a single line comment. This function captures a comment
291  *              in a char* and places the comment in the approperiate global
292  *              buffer through CvPlaceComment
293  *
294  ******************************************************************************/
295 
296 void
297 CvProcessCommentType2 (
298     ASL_COMMENT_STATE       CurrentState,
299     char                    *StringBuffer)
300 {
301     UINT32                  i;
302     char                    *CommentString;
303     char                    *FinalCommentString;
304 
305 
306     if (Gbl_CaptureComments && CurrentState.CaptureComments)
307     {
308         *StringBuffer = 0; /* null terminate */
309         CvDbgPrint ("Single-line comment\n");
310         CommentString = UtStringCacheCalloc (strlen (MsgBuffer) + 1);
311         strcpy (CommentString, MsgBuffer);
312 
313         /* If this comment lies on the same line as the latest parse node,
314          * assign it to that node's CommentAfter field. Saving in this field
315          * will allow us to support comments that come after code on the same
316          * line as the code itself. For example,
317          * Name(A,"") //comment
318          *
319          * will be retained rather than transformed into
320          *
321          * Name(A,"")
322          * //comment
323          *
324          * For this case, we only need to add one comment since
325          *
326          * Name(A,"") //comment1 //comment2 ... more comments here.
327          *
328          * would be lexically analyzed as a single comment.
329          *
330          * Create a new string with the approperiate spaces. Since we need
331          * to account for the proper spacing, the actual comment,
332          * extra 2 spaces so that this comment can be converted to the "/ *"
333          * style and the null terminator, the string would look something like
334          *
335          * [ (spaces) (comment)  ( * /) ('\0') ]
336          *
337          */
338         FinalCommentString = UtStringCacheCalloc (CurrentState.SpacesBefore +
339             strlen (CommentString) + 3 + 1);
340 
341         for (i=0; (CurrentState.CommentType!=1) && (i<CurrentState.SpacesBefore); ++i)
342         {
343             FinalCommentString[i] = ' ';
344         }
345         strcat (FinalCommentString, CommentString);
346 
347         /* convert to a "/ *" style comment  */
348 
349         strcat (FinalCommentString, " */");
350         FinalCommentString [CurrentState.SpacesBefore + strlen (CommentString) + 3] = 0;
351 
352         /* get rid of the carriage return */
353 
354         if (FinalCommentString[strlen (FinalCommentString) - 1] == 0x0D)
355         {
356             FinalCommentString[strlen(FinalCommentString)-1] = 0;
357         }
358         CvPlaceComment (CurrentState.CommentType, FinalCommentString);
359     }
360 }
361 
362 
363 /*******************************************************************************
364  *
365  * FUNCTION:    CgCalculateCommentLengths
366  *
367  * PARAMETERS:  Op                 - Calculate all comments of this Op
368  *
369  * RETURN:      TotalCommentLength - Length of all comments within this node.
370  *
371  * DESCRIPTION: calculate the length that the each comment takes up within Op.
372  *              Comments look like the follwoing: [0xA9 OptionBtye comment 0x00]
373  *              therefore, we add 1 + 1 + strlen (comment) + 1 to get the actual
374  *              length of this comment.
375  *
376  ******************************************************************************/
377 
378 UINT32
379 CvCalculateCommentLengths(
380    ACPI_PARSE_OBJECT        *Op)
381 {
382     UINT32                  CommentLength = 0;
383     UINT32                  TotalCommentLength = 0;
384     ACPI_COMMENT_NODE       *Current = NULL;
385 
386 
387     if (!Gbl_CaptureComments)
388     {
389         return (0);
390     }
391 
392     CvDbgPrint ("==Calculating comment lengths for %s\n",  Op->Asl.ParseOpName);
393     if (Op->Asl.FileChanged)
394     {
395         TotalCommentLength += strlen (Op->Asl.Filename) + 3;
396 
397         if (Op->Asl.ParentFilename &&
398             AcpiUtStricmp (Op->Asl.Filename, Op->Asl.ParentFilename))
399         {
400             TotalCommentLength += strlen (Op->Asl.ParentFilename) + 3;
401         }
402     }
403     if (Op->Asl.CommentList)
404     {
405         Current = Op->Asl.CommentList;
406         while (Current)
407         {
408             CommentLength = strlen (Current->Comment)+3;
409             CvDbgPrint ("Length of standard comment: %d\n", CommentLength);
410             CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
411             TotalCommentLength += CommentLength;
412             Current = Current->Next;
413         }
414     }
415     if (Op->Asl.EndBlkComment)
416     {
417         Current = Op->Asl.EndBlkComment;
418         while (Current)
419         {
420             CommentLength = strlen (Current->Comment)+3;
421             CvDbgPrint ("Length of endblkcomment: %d\n", CommentLength);
422             CvDbgPrint ("    Comment string: %s\n\n", Current->Comment);
423             TotalCommentLength += CommentLength;
424             Current = Current->Next;
425         }
426     }
427     if (Op->Asl.InlineComment)
428     {
429         CommentLength = strlen (Op->Asl.InlineComment)+3;
430         CvDbgPrint ("Length of inline comment: %d\n", CommentLength);
431         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.InlineComment);
432         TotalCommentLength += CommentLength;
433     }
434     if (Op->Asl.EndNodeComment)
435     {
436         CommentLength = strlen(Op->Asl.EndNodeComment)+3;
437         CvDbgPrint ("Length of end node comment +3: %d\n", CommentLength);
438         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.EndNodeComment);
439         TotalCommentLength += CommentLength;
440     }
441 
442     if (Op->Asl.CloseBraceComment)
443     {
444         CommentLength = strlen (Op->Asl.CloseBraceComment)+3;
445         CvDbgPrint ("Length of close brace comment: %d\n", CommentLength);
446         CvDbgPrint ("    Comment string: %s\n\n", Op->Asl.CloseBraceComment);
447         TotalCommentLength += CommentLength;
448     }
449 
450     CvDbgPrint("\n\n");
451 
452     return TotalCommentLength;
453 
454 }
455 
456 
457 /*******************************************************************************
458  *
459  * FUNCTION:    CgWriteAmlDefBlockComment
460  *
461  * PARAMETERS:  Op              - Current parse op
462  *
463  * RETURN:      None
464  *
465  * DESCRIPTION: Write all comments for a particular definition block.
466  *              For definition blocks, the comments need to come after the
467  *              definition block header. The regular comments above the
468  *              definition block would be categorized as
469  *              STD_DEFBLK_COMMENT and comments after the closing brace
470  *              is categorized as END_DEFBLK_COMMENT.
471  *
472  ******************************************************************************/
473 
474 void
475 CgWriteAmlDefBlockComment(
476     ACPI_PARSE_OBJECT       *Op)
477 {
478     UINT8                   CommentOption;
479     ACPI_COMMENT_NODE       *Current;
480     char                    *NewFilename;
481     char                    *Position;
482     char                    *DirectoryPosition;
483 
484 
485     if (!Gbl_CaptureComments ||
486         (Op->Asl.ParseOpcode != PARSEOP_DEFINITION_BLOCK))
487     {
488         return;
489     }
490 
491     CvDbgPrint ("Printing comments for a definition block..\n");
492 
493     /* first, print the file name comment after changing .asl to .dsl */
494 
495     NewFilename = UtStringCacheCalloc (strlen (Op->Asl.Filename));
496     strcpy (NewFilename, Op->Asl.Filename);
497     DirectoryPosition = strrchr (NewFilename, '/');
498     Position = strrchr (NewFilename, '.');
499 
500     if (Position && (Position > DirectoryPosition))
501     {
502         /* Tack on the new suffix */
503 
504         Position++;
505         *Position = 0;
506         strcat (Position, FILE_SUFFIX_DISASSEMBLY);
507     }
508     else
509     {
510         /* No dot, add one and then the suffix */
511 
512         strcat (NewFilename, ".");
513         strcat (NewFilename, FILE_SUFFIX_DISASSEMBLY);
514     }
515 
516     CommentOption = FILENAME_COMMENT;
517     CgWriteOneAmlComment(Op, NewFilename, CommentOption);
518 
519     Current = Op->Asl.CommentList;
520     CommentOption = STD_DEFBLK_COMMENT;
521     while (Current)
522     {
523         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
524         CvDbgPrint ("Printing comment: %s\n", Current->Comment);
525         Current = Current->Next;
526     }
527     Op->Asl.CommentList = NULL;
528 
529     /* print any Inline comments associated with this node */
530 
531     if (Op->Asl.CloseBraceComment)
532     {
533         CommentOption = END_DEFBLK_COMMENT;
534         CgWriteOneAmlComment(Op, Op->Asl.CloseBraceComment, CommentOption);
535         Op->Asl.CloseBraceComment = NULL;
536     }
537 }
538 
539 
540 /*******************************************************************************
541  *
542  * FUNCTION:    CgWriteOneAmlComment
543  *
544  * PARAMETERS:  Op              - Current parse op
545  *              CommentToPrint  - Comment that's printed
546  *              InputOption     - Denotes the comment option.
547  *
548  * RETURN:      None
549  *
550  * DESCRIPTION: write a single comment.
551  *
552  ******************************************************************************/
553 
554 void
555 CgWriteOneAmlComment(
556     ACPI_PARSE_OBJECT       *Op,
557     char*                   CommentToPrint,
558     UINT8                   InputOption)
559 {
560     UINT8 CommentOption = InputOption;
561     UINT8 CommentOpcode = (UINT8)AML_COMMENT_OP;
562 
563 
564     if (!CommentToPrint)
565     {
566         return;
567     }
568 
569     CgLocalWriteAmlData (Op, &CommentOpcode, 1);
570     CgLocalWriteAmlData (Op, &CommentOption, 1);
571 
572     /* The strlen (..) + 1 is to include the null terminator */
573 
574     CgLocalWriteAmlData (Op, CommentToPrint, strlen (CommentToPrint) + 1);
575 }
576 
577 
578 /*******************************************************************************
579  *
580  * FUNCTION:    CgWriteAmlComment
581  *
582  * PARAMETERS:  Op              - Current parse op
583  *
584  * RETURN:      None
585  *
586  * DESCRIPTION: write all comments pertaining to the
587  *              current parse op
588  *
589  ******************************************************************************/
590 
591 void
592 CgWriteAmlComment(
593     ACPI_PARSE_OBJECT       *Op)
594 {
595     ACPI_COMMENT_NODE       *Current;
596     UINT8                   CommentOption;
597     char                    *NewFilename;
598     char                    *ParentFilename;
599 
600 
601     if ((Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK) ||
602          !Gbl_CaptureComments)
603     {
604         return;
605     }
606 
607     /* Print out the filename comment if needed */
608 
609     if (Op->Asl.FileChanged)
610     {
611 
612         /* first, print the file name comment after changing .asl to .dsl */
613 
614         NewFilename =
615             FlGenerateFilename (Op->Asl.Filename, FILE_SUFFIX_DISASSEMBLY);
616         if (NewFilename)
617         {
618             CvDbgPrint ("Writing file comment, \"%s\" for %s\n",
619                 NewFilename, Op->Asl.ParseOpName);
620         }
621 
622         CgWriteOneAmlComment(Op, NewFilename, FILENAME_COMMENT);
623 
624         if (Op->Asl.ParentFilename &&
625             AcpiUtStricmp (Op->Asl.ParentFilename, Op->Asl.Filename))
626         {
627             ParentFilename = FlGenerateFilename (Op->Asl.ParentFilename,
628                 FILE_SUFFIX_DISASSEMBLY);
629             CgWriteOneAmlComment(Op, ParentFilename, PARENTFILENAME_COMMENT);
630         }
631 
632         /* prevent multiple writes of the same comment */
633 
634         Op->Asl.FileChanged = FALSE;
635     }
636 
637     /*
638      * Regular comments are stored in a list of comments within an Op.
639      * If there is a such list in this node, print out the comment
640      * as byte code.
641      */
642     Current = Op->Asl.CommentList;
643     if (Op->Asl.ParseOpcode == PARSEOP_INCLUDE)
644     {
645         CommentOption = INCLUDE_COMMENT;
646     }
647     else
648     {
649         CommentOption = STANDARD_COMMENT;
650     }
651 
652     while (Current)
653     {
654         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
655         Current = Current->Next;
656     }
657     Op->Asl.CommentList = NULL;
658 
659     Current = Op->Asl.EndBlkComment;
660     CommentOption = ENDBLK_COMMENT;
661     while (Current)
662     {
663         CgWriteOneAmlComment(Op, Current->Comment, CommentOption);
664         Current = Current->Next;
665     }
666     Op->Asl.EndBlkComment = NULL;
667 
668     /* print any Inline comments associated with this node */
669 
670     if (Op->Asl.InlineComment)
671     {
672         CommentOption = INLINE_COMMENT;
673         CgWriteOneAmlComment(Op, Op->Asl.InlineComment, CommentOption);
674         Op->Asl.InlineComment = NULL;
675     }
676 
677     if (Op->Asl.EndNodeComment)
678     {
679         CommentOption = ENDNODE_COMMENT;
680         CgWriteOneAmlComment(Op, Op->Asl.EndNodeComment, CommentOption);
681         Op->Asl.EndNodeComment = NULL;
682     }
683 
684     if (Op->Asl.CloseBraceComment)
685     {
686         CommentOption = CLOSE_BRACE_COMMENT;
687         CgWriteOneAmlComment(Op, Op->Asl.CloseBraceComment, CommentOption);
688         Op->Asl.CloseBraceComment = NULL;
689     }
690 }
691 
692 
693 /*******************************************************************************
694  *
695  * FUNCTION:    CvCommentNodeCalloc
696  *
697  * PARAMETERS:  none
698  *
699  * RETURN:      Pointer to the comment node. Aborts on allocation failure
700  *
701  * DESCRIPTION: Allocate a string node buffer.
702  *
703  ******************************************************************************/
704 
705 ACPI_COMMENT_NODE *
706 CvCommentNodeCalloc (
707     void)
708 {
709    ACPI_COMMENT_NODE        *NewCommentNode;
710 
711 
712    NewCommentNode = UtLocalCalloc (sizeof (ACPI_COMMENT_NODE));
713    NewCommentNode->Next = NULL;
714    return (NewCommentNode);
715 }
716 
717 
718 /*******************************************************************************
719  *
720  * FUNCTION:    CvParseOpBlockType
721  *
722  * PARAMETERS:  Op              - Object to be examined
723  *
724  * RETURN:      BlockType - not a block, parens, braces, or even both.
725  *
726  * DESCRIPTION: Type of block for this ASL parseop (parens or braces)
727  *              keep this in sync with aslprimaries.y, aslresources.y and
728  *              aslrules.y
729  *
730  ******************************************************************************/
731 
732 UINT32
733 CvParseOpBlockType (
734     ACPI_PARSE_OBJECT       *Op)
735 {
736     if (!Op)
737     {
738         return (BLOCK_NONE);
739     }
740 
741     switch (Op->Asl.ParseOpcode)
742     {
743 
744     /* from aslprimaries.y */
745 
746     case PARSEOP_VAR_PACKAGE:
747     case PARSEOP_BANKFIELD:
748     case PARSEOP_BUFFER:
749     case PARSEOP_CASE:
750     case PARSEOP_DEVICE:
751     case PARSEOP_FIELD:
752     case PARSEOP_FOR:
753     case PARSEOP_FUNCTION:
754     case PARSEOP_IF:
755     case PARSEOP_ELSEIF:
756     case PARSEOP_INDEXFIELD:
757     case PARSEOP_METHOD:
758     case PARSEOP_POWERRESOURCE:
759     case PARSEOP_PROCESSOR:
760     case PARSEOP_DATABUFFER:
761     case PARSEOP_SCOPE:
762     case PARSEOP_SWITCH:
763     case PARSEOP_THERMALZONE:
764     case PARSEOP_WHILE:
765 
766     /* from aslresources.y */
767 
768     case PARSEOP_RESOURCETEMPLATE: /* optional parens */
769     case PARSEOP_VENDORLONG:
770     case PARSEOP_VENDORSHORT:
771     case PARSEOP_INTERRUPT:
772     case PARSEOP_IRQNOFLAGS:
773     case PARSEOP_IRQ:
774     case PARSEOP_GPIO_INT:
775     case PARSEOP_GPIO_IO:
776     case PARSEOP_DMA:
777 
778     /*from aslrules.y */
779 
780     case PARSEOP_DEFINITION_BLOCK:
781         return (BLOCK_PAREN | BLOCK_BRACE);
782 
783     default:
784 
785         return (BLOCK_NONE);
786     }
787 }
788 
789 
790 /*******************************************************************************
791  *
792  * FUNCTION:    CvProcessCommentState
793  *
794  * PARAMETERS:  char
795  *
796  * RETURN:      None
797  *
798  * DESCRIPTION: Take the given input. If this character is
799  *              defined as a comment table entry, then update the state
800  *              accordingly.
801  *
802  ******************************************************************************/
803 
804 void
805 CvProcessCommentState (
806     char                    input)
807 {
808 
809     if (input != ' ')
810     {
811         Gbl_CommentState.SpacesBefore = 0;
812     }
813 
814     switch (input)
815     {
816     case '\n':
817 
818         Gbl_CommentState.CommentType = ASL_COMMENT_STANDARD;
819         break;
820 
821     case ' ':
822 
823         /* Keep the CommentType the same */
824 
825         Gbl_CommentState.SpacesBefore++;
826         break;
827 
828     case '(':
829 
830         Gbl_CommentState.CommentType = ASL_COMMENT_OPEN_PAREN;
831         break;
832 
833     case ')':
834 
835         Gbl_CommentState.CommentType = ASL_COMMENT_CLOSE_PAREN;
836         break;
837 
838     case '{':
839 
840         Gbl_CommentState.CommentType = ASL_COMMENT_STANDARD;
841         Gbl_CommentState.ParsingParenBraceNode = NULL;
842         CvDbgPrint ("End Parsing paren/Brace node!\n");
843         break;
844 
845     case '}':
846 
847         Gbl_CommentState.CommentType = ASL_COMMENT_CLOSE_BRACE;
848         break;
849 
850     case ',':
851 
852         Gbl_CommentState.CommentType = ASLCOMMENT_INLINE;
853         break;
854 
855     default:
856 
857         Gbl_CommentState.CommentType = ASLCOMMENT_INLINE;
858         break;
859 
860     }
861 }
862 
863 
864 /*******************************************************************************
865  *
866  * FUNCTION:    CvAddToCommentList
867  *
868  * PARAMETERS:  toAdd              - Contains the comment to be inserted
869  *
870  * RETURN:      None
871  *
872  * DESCRIPTION: Add the given char* to a list of comments in the global list
873  *              of comments.
874  *
875  ******************************************************************************/
876 
877 void
878 CvAddToCommentList (
879     char*                   ToAdd)
880 {
881    if (Gbl_Comment_List_Head)
882    {
883        Gbl_Comment_List_Tail->Next = CvCommentNodeCalloc ();
884        Gbl_Comment_List_Tail = Gbl_Comment_List_Tail->Next;
885    }
886    else
887    {
888        Gbl_Comment_List_Head = CvCommentNodeCalloc ();
889        Gbl_Comment_List_Tail = Gbl_Comment_List_Head;
890    }
891 
892    Gbl_Comment_List_Tail->Comment = ToAdd;
893 
894    return;
895 }
896 
897 /*******************************************************************************
898  *
899  * FUNCTION:    CvAppendInlineComment
900  *
901  * PARAMETERS:  InlineComment      - Append to the end of this string.
902  *              toAdd              - Contains the comment to be inserted
903  *
904  * RETURN:      Str                - toAdd appended to InlineComment
905  *
906  * DESCRIPTION: Concatenate ToAdd to InlineComment
907  *
908  ******************************************************************************/
909 
910 char*
911 CvAppendInlineComment (
912     char                    *InlineComment,
913     char                    *ToAdd)
914 {
915     char*                   Str;
916     UINT32                  Size = 0;
917 
918 
919     if (!InlineComment)
920     {
921         return ToAdd;
922     }
923     if (!ToAdd)
924     {
925         return InlineComment;
926     }
927 
928     Size = strlen (ToAdd);
929     Size += strlen (InlineComment);
930     Str = UtStringCacheCalloc (Size+1);
931     strcpy (Str, InlineComment);
932     strcat (Str, ToAdd);
933     Str[Size+1] = 0;
934 
935     return Str;
936 }
937 
938 
939 /*******************************************************************************
940  *
941  * FUNCTION:    CvPlaceComment
942  *
943  * PARAMETERS:  Int           - Type
944  *              char*         - CommentString
945  *
946  * RETURN:      None
947  *
948  * DESCRIPTION: Given type and CommentString, this function places the
949  *              CommentString in the approperiate global comment list or char*
950  *
951  ******************************************************************************/
952 
953 void
954 CvPlaceComment(
955     UINT8                   Type,
956     char                    *CommentString)
957 {
958     ACPI_PARSE_OBJECT       *LatestParseNode;
959     ACPI_PARSE_OBJECT       *ParenBraceNode;
960 
961 
962     LatestParseNode = Gbl_CommentState.Latest_Parse_Node;
963     ParenBraceNode  = Gbl_CommentState.ParsingParenBraceNode;
964     CvDbgPrint ("Placing comment %s for type %d\n", CommentString, Type);
965 
966     switch (Type)
967     {
968     case ASL_COMMENT_STANDARD:
969 
970         CvAddToCommentList (CommentString);
971         break;
972 
973     case ASLCOMMENT_INLINE:
974 
975         LatestParseNode->Asl.InlineComment =
976             CvAppendInlineComment (LatestParseNode->Asl.InlineComment,
977             CommentString);
978         break;
979 
980     case ASL_COMMENT_OPEN_PAREN:
981 
982         Gbl_Inline_Comment_Buffer =
983             CvAppendInlineComment(Gbl_Inline_Comment_Buffer,
984             CommentString);
985         break;
986 
987     case ASL_COMMENT_CLOSE_PAREN:
988 
989         if (ParenBraceNode)
990         {
991             ParenBraceNode->Asl.EndNodeComment =
992                 CvAppendInlineComment (ParenBraceNode->Asl.EndNodeComment,
993                 CommentString);
994         }
995         else
996         {
997             LatestParseNode->Asl.EndNodeComment =
998                 CvAppendInlineComment (LatestParseNode->Asl.EndNodeComment,
999                 CommentString);
1000         }
1001         break;
1002 
1003     case ASL_COMMENT_CLOSE_BRACE:
1004 
1005         LatestParseNode->Asl.CloseBraceComment = CommentString;
1006         break;
1007 
1008     default:
1009 
1010         break;
1011 
1012     }
1013 }
1014