1 /******************************************************************************
3 * Module Name: asllistsup - Listing file support utilities
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2017, Intel Corp.
12 * All rights reserved.
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
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
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;
37 * The above copyright and patent license is granted only if the following
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.
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
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
72 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
80 * 4. Disclaimer and Export Compliance
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
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
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
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.
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.
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.
150 *****************************************************************************/
152 #include <contrib/dev/acpica/compiler/aslcompiler.h>
153 #include "aslcompiler.y.h"
156 #define _COMPONENT ACPI_COMPILER
157 ACPI_MODULE_NAME ("aslistsup")
160 /*******************************************************************************
162 * FUNCTION: LsDumpAscii
164 * PARAMETERS: FileId - ID of current listing file
165 * Count - Number of bytes to convert
166 * Buffer - Buffer of bytes to convert
170 * DESCRIPTION: Convert hex bytes to ascii
172 ******************************************************************************/
184 FlPrintFile (FileId, " \"");
185 for (i = 0; i < Count; i++)
188 if (isprint (BufChar))
190 FlPrintFile (FileId, "%c", BufChar);
194 /* Not a printable character, just put out a dot */
196 FlPrintFile (FileId, ".");
200 FlPrintFile (FileId, "\"");
204 /*******************************************************************************
206 * FUNCTION: LsDumpAsciiInComment
208 * PARAMETERS: FileId - ID of current listing file
209 * Count - Number of bytes to convert
210 * Buffer - Buffer of bytes to convert
214 * DESCRIPTION: Convert hex bytes to ascii
216 ******************************************************************************/
219 LsDumpAsciiInComment (
229 FlPrintFile (FileId, " \"");
230 for (i = 0; i < Count; i++)
235 if (isprint (BufChar))
237 /* Handle embedded C comment sequences */
239 if (((LastChar == '*') && (BufChar == '/')) ||
240 ((LastChar == '/') && (BufChar == '*')))
242 /* Insert a space to break the sequence */
244 FlPrintFile (FileId, ".", BufChar);
247 FlPrintFile (FileId, "%c", BufChar);
251 /* Not a printable character, just put out a dot */
253 FlPrintFile (FileId, ".");
257 FlPrintFile (FileId, "\"");
261 /*******************************************************************************
263 * FUNCTION: LsCheckException
265 * PARAMETERS: LineNumber - Current logical (cumulative) line #
266 * FileId - ID of output listing file
270 * DESCRIPTION: Check if there is an exception for this line, and if there is,
271 * put it in the listing immediately. Handles multiple errors
272 * per line. Gbl_NextError points to the next error in the
273 * sorted (by line #) list of compile errors/warnings.
275 ******************************************************************************/
283 if ((!Gbl_NextError) ||
284 (LineNumber < Gbl_NextError->LogicalLineNumber ))
289 /* Handle multiple errors per line */
291 if (FileId == ASL_FILE_LISTING_OUTPUT)
293 while (Gbl_NextError &&
294 (LineNumber >= Gbl_NextError->LogicalLineNumber))
296 AePrintException (FileId, Gbl_NextError, "\n[****iasl****]\n");
297 Gbl_NextError = Gbl_NextError->Next;
300 FlPrintFile (FileId, "\n");
305 /*******************************************************************************
307 * FUNCTION: LsWriteListingHexBytes
309 * PARAMETERS: Buffer - AML code buffer
310 * Length - Number of AML bytes to write
311 * FileId - ID of current listing file.
315 * DESCRIPTION: Write the contents of the AML buffer to the listing file via
316 * the listing buffer. The listing buffer is flushed every 16
319 ******************************************************************************/
322 LsWriteListingHexBytes (
330 /* Transfer all requested bytes */
332 for (i = 0; i < Length; i++)
334 /* Print line header when buffer is empty */
336 if (Gbl_CurrentHexColumn == 0)
338 if (Gbl_HasIncludeFiles)
340 FlPrintFile (FileId, "%*s", 10, " ");
345 case ASL_FILE_LISTING_OUTPUT:
347 FlPrintFile (FileId, "%8.8X%s", Gbl_CurrentAmlOffset,
348 ASL_LISTING_LINE_PREFIX);
351 case ASL_FILE_ASM_SOURCE_OUTPUT:
353 FlPrintFile (FileId, " db ");
356 case ASL_FILE_C_SOURCE_OUTPUT:
358 FlPrintFile (FileId, " ");
363 /* No other types supported */
369 /* Transfer AML byte and update counts */
371 Gbl_AmlBuffer[Gbl_CurrentHexColumn] = Buffer[i];
373 Gbl_CurrentHexColumn++;
374 Gbl_CurrentAmlOffset++;
376 /* Flush buffer when it is full */
378 if (Gbl_CurrentHexColumn >= HEX_LISTING_LINE_SIZE)
380 LsFlushListingBuffer (FileId);
386 /*******************************************************************************
388 * FUNCTION: LsWriteSourceLines
390 * PARAMETERS: ToLineNumber -
391 * ToLogicalLineNumber - Write up to this source line number
392 * FileId - ID of current listing file
396 * DESCRIPTION: Read then write source lines to the listing file until we have
397 * reached the specified logical (cumulative) line number. This
398 * automatically echos out comment blocks and other non-AML
399 * generating text until we get to the actual AML-generating line
400 * of ASL code specified by the logical line number.
402 ******************************************************************************/
407 UINT32 ToLogicalLineNumber,
411 /* Nothing to do for these file types */
413 if ((FileId == ASL_FILE_ASM_INCLUDE_OUTPUT) ||
414 (FileId == ASL_FILE_C_INCLUDE_OUTPUT))
419 Gbl_CurrentLine = ToLogicalLineNumber;
421 /* Flush any hex bytes remaining from the last opcode */
423 LsFlushListingBuffer (FileId);
425 /* Read lines and write them as long as we are not caught up */
427 if (Gbl_SourceLine < Gbl_CurrentLine)
430 * If we just completed writing some AML hex bytes, output a linefeed
431 * to add some whitespace for readability.
433 if (Gbl_HexBytesWereWritten)
435 FlPrintFile (FileId, "\n");
436 Gbl_HexBytesWereWritten = FALSE;
439 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
441 FlPrintFile (FileId, " /*\n");
444 /* Write one line at a time until we have reached the target line # */
446 while ((Gbl_SourceLine < Gbl_CurrentLine) &&
447 LsWriteOneSourceLine (FileId))
450 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
452 FlPrintFile (FileId, " */");
455 FlPrintFile (FileId, "\n");
460 /*******************************************************************************
462 * FUNCTION: LsWriteOneSourceLine
464 * PARAMETERS: FileId - ID of current listing file
466 * RETURN: FALSE on EOF (input source file), TRUE otherwise
468 * DESCRIPTION: Read one line from the input source file and echo it to the
469 * listing file, prefixed with the line number, and if the source
470 * file contains include files, prefixed with the current filename
472 ******************************************************************************/
475 LsWriteOneSourceLine (
481 BOOLEAN StartOfLine = FALSE;
482 BOOLEAN ProcessLongLine = FALSE;
486 Gbl_ListingNode->LineNumber++;
488 /* Ignore lines that are completely blank (but count the line above) */
490 if (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) != AE_OK)
494 if (FileByte == '\n')
500 * This is a non-empty line, we will print the entire line with
501 * the line number and possibly other prefixes and transforms.
504 /* Line prefixes for special files, C and ASM output */
506 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
508 FlPrintFile (FileId, " *");
510 if (FileId == ASL_FILE_ASM_SOURCE_OUTPUT)
512 FlPrintFile (FileId, "; ");
515 if (Gbl_HasIncludeFiles)
518 * This file contains "include" statements, print the current
519 * filename and line number within the current file
521 FlPrintFile (FileId, "%12s %5d%s",
522 Gbl_ListingNode->Filename, Gbl_ListingNode->LineNumber,
523 ASL_LISTING_LINE_PREFIX);
527 /* No include files, just print the line number */
529 FlPrintFile (FileId, "%8u%s", Gbl_SourceLine,
530 ASL_LISTING_LINE_PREFIX);
533 /* Read the rest of this line (up to a newline or EOF) */
537 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
545 /* Split long input lines for readability in the listing */
550 if (!ProcessLongLine)
552 if ((FileByte != '}') &&
558 ProcessLongLine = TRUE;
563 FlPrintFile (FileId, "\n%*s{\n", Index, " ");
569 else if (FileByte == '}')
573 FlPrintFile (FileId, "\n");
578 FlPrintFile (FileId, "%*s}\n", Index, " ");
582 /* Ignore spaces/tabs at the start of line */
584 else if ((FileByte == ' ') && StartOfLine)
589 else if (StartOfLine)
592 FlPrintFile (FileId, "%*s", Index, " ");
596 FlWriteFile (FileId, &FileByte, 1);
597 if (FileByte == '\n')
600 * This line has been completed.
601 * Check if an error occurred on this source line during the compile.
602 * If so, we print the error message after the source line.
604 LsCheckException (Gbl_SourceLine, FileId);
610 FlWriteFile (FileId, &FileByte, 1);
611 if (FileByte == '\n')
614 * This line has been completed.
615 * Check if an error occurred on this source line during the compile.
616 * If so, we print the error message after the source line.
618 LsCheckException (Gbl_SourceLine, FileId);
623 } while (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) == AE_OK);
625 /* EOF on the input file was reached */
631 /*******************************************************************************
633 * FUNCTION: LsFlushListingBuffer
635 * PARAMETERS: FileId - ID of the listing file
639 * DESCRIPTION: Flush out the current contents of the 16-byte hex AML code
640 * buffer. Usually called at the termination of a single line
641 * of source code or when the buffer is full.
643 ******************************************************************************/
646 LsFlushListingBuffer (
652 if (Gbl_CurrentHexColumn == 0)
657 /* Write the hex bytes */
661 case ASL_FILE_LISTING_OUTPUT:
663 for (i = 0; i < Gbl_CurrentHexColumn; i++)
665 FlPrintFile (FileId, "%2.2X ", Gbl_AmlBuffer[i]);
668 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 3); i++)
670 FlWriteFile (FileId, ".", 1);
673 /* Write the ASCII character associated with each of the bytes */
675 LsDumpAscii (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
679 case ASL_FILE_ASM_SOURCE_OUTPUT:
681 for (i = 0; i < Gbl_CurrentHexColumn; i++)
685 FlPrintFile (FileId, ",");
688 FlPrintFile (FileId, "0%2.2Xh", Gbl_AmlBuffer[i]);
691 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 5); i++)
693 FlWriteFile (FileId, " ", 1);
696 FlPrintFile (FileId, " ;%8.8X",
697 Gbl_CurrentAmlOffset - HEX_LISTING_LINE_SIZE);
699 /* Write the ASCII character associated with each of the bytes */
701 LsDumpAscii (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
705 case ASL_FILE_C_SOURCE_OUTPUT:
707 for (i = 0; i < Gbl_CurrentHexColumn; i++)
709 FlPrintFile (FileId, "0x%2.2X,", Gbl_AmlBuffer[i]);
712 /* Pad hex output with spaces if line is shorter than max line size */
714 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 5); i++)
716 FlWriteFile (FileId, " ", 1);
719 /* AML offset for the start of the line */
721 FlPrintFile (FileId, " /* %8.8X",
722 Gbl_CurrentAmlOffset - Gbl_CurrentHexColumn);
724 /* Write the ASCII character associated with each of the bytes */
726 LsDumpAsciiInComment (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
727 FlPrintFile (FileId, " */");
732 /* No other types supported */
737 FlPrintFile (FileId, "\n");
739 Gbl_CurrentHexColumn = 0;
740 Gbl_HexBytesWereWritten = TRUE;
744 /*******************************************************************************
746 * FUNCTION: LsPushNode
748 * PARAMETERS: Filename - Pointer to the include filename
752 * DESCRIPTION: Push a listing node on the listing/include file stack. This
753 * stack enables tracking of include files (infinitely nested)
754 * and resumption of the listing of the parent file when the
755 * include file is finished.
757 ******************************************************************************/
763 ASL_LISTING_NODE *Lnode;
766 /* Create a new node */
768 Lnode = UtLocalCalloc (sizeof (ASL_LISTING_NODE));
772 Lnode->Filename = Filename;
773 Lnode->LineNumber = 0;
777 Lnode->Next = Gbl_ListingNode;
778 Gbl_ListingNode = Lnode;
782 /*******************************************************************************
784 * FUNCTION: LsPopNode
788 * RETURN: List head after current head is popped off
790 * DESCRIPTION: Pop the current head of the list, free it, and return the
791 * next node on the stack (the new current node).
793 ******************************************************************************/
799 ASL_LISTING_NODE *Lnode;
802 /* Just grab the node at the head of the list */
804 Lnode = Gbl_ListingNode;
808 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
809 "Could not pop empty listing stack");
810 return (Gbl_ListingNode);
813 Gbl_ListingNode = Lnode->Next;
816 /* New "Current" node is the new head */
818 return (Gbl_ListingNode);