1 /******************************************************************************
3 * Module Name: asllistsup - Listing file support utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include "aslcompiler.y.h"
48 #define _COMPONENT ACPI_COMPILER
49 ACPI_MODULE_NAME ("aslistsup")
52 /*******************************************************************************
54 * FUNCTION: LsDumpAscii
56 * PARAMETERS: FileId - ID of current listing file
57 * Count - Number of bytes to convert
58 * Buffer - Buffer of bytes to convert
62 * DESCRIPTION: Convert hex bytes to ascii
64 ******************************************************************************/
76 FlPrintFile (FileId, " \"");
77 for (i = 0; i < Count; i++)
80 if (isprint (BufChar))
82 FlPrintFile (FileId, "%c", BufChar);
86 /* Not a printable character, just put out a dot */
88 FlPrintFile (FileId, ".");
92 FlPrintFile (FileId, "\"");
96 /*******************************************************************************
98 * FUNCTION: LsDumpAsciiInComment
100 * PARAMETERS: FileId - ID of current listing file
101 * Count - Number of bytes to convert
102 * Buffer - Buffer of bytes to convert
106 * DESCRIPTION: Convert hex bytes to ascii
108 ******************************************************************************/
111 LsDumpAsciiInComment (
121 FlPrintFile (FileId, " \"");
122 for (i = 0; i < Count; i++)
127 if (isprint (BufChar))
129 /* Handle embedded C comment sequences */
131 if (((LastChar == '*') && (BufChar == '/')) ||
132 ((LastChar == '/') && (BufChar == '*')))
134 /* Insert a space to break the sequence */
136 FlPrintFile (FileId, ".", BufChar);
139 FlPrintFile (FileId, "%c", BufChar);
143 /* Not a printable character, just put out a dot */
145 FlPrintFile (FileId, ".");
149 FlPrintFile (FileId, "\"");
153 /*******************************************************************************
155 * FUNCTION: LsCheckException
157 * PARAMETERS: LineNumber - Current logical (cumulative) line #
158 * FileId - ID of output listing file
162 * DESCRIPTION: Check if there is an exception for this line, and if there is,
163 * put it in the listing immediately. Handles multiple errors
164 * per line. Gbl_NextError points to the next error in the
165 * sorted (by line #) list of compile errors/warnings.
167 ******************************************************************************/
175 if ((!Gbl_NextError) ||
176 (LineNumber < Gbl_NextError->LogicalLineNumber ))
181 /* Handle multiple errors per line */
183 if (FileId == ASL_FILE_LISTING_OUTPUT)
185 while (Gbl_NextError &&
186 (LineNumber >= Gbl_NextError->LogicalLineNumber))
188 AePrintException (FileId, Gbl_NextError, "\n[****iasl****]\n");
189 Gbl_NextError = Gbl_NextError->Next;
192 FlPrintFile (FileId, "\n");
197 /*******************************************************************************
199 * FUNCTION: LsWriteListingHexBytes
201 * PARAMETERS: Buffer - AML code buffer
202 * Length - Number of AML bytes to write
203 * FileId - ID of current listing file.
207 * DESCRIPTION: Write the contents of the AML buffer to the listing file via
208 * the listing buffer. The listing buffer is flushed every 16
211 ******************************************************************************/
214 LsWriteListingHexBytes (
222 /* Transfer all requested bytes */
224 for (i = 0; i < Length; i++)
226 /* Print line header when buffer is empty */
228 if (Gbl_CurrentHexColumn == 0)
230 if (Gbl_HasIncludeFiles)
232 FlPrintFile (FileId, "%*s", 10, " ");
237 case ASL_FILE_LISTING_OUTPUT:
239 FlPrintFile (FileId, "%8.8X%s", Gbl_CurrentAmlOffset,
240 ASL_LISTING_LINE_PREFIX);
243 case ASL_FILE_ASM_SOURCE_OUTPUT:
245 FlPrintFile (FileId, " db ");
248 case ASL_FILE_C_SOURCE_OUTPUT:
250 FlPrintFile (FileId, " ");
255 /* No other types supported */
261 /* Transfer AML byte and update counts */
263 Gbl_AmlBuffer[Gbl_CurrentHexColumn] = Buffer[i];
265 Gbl_CurrentHexColumn++;
266 Gbl_CurrentAmlOffset++;
268 /* Flush buffer when it is full */
270 if (Gbl_CurrentHexColumn >= HEX_LISTING_LINE_SIZE)
272 LsFlushListingBuffer (FileId);
278 /*******************************************************************************
280 * FUNCTION: LsWriteSourceLines
282 * PARAMETERS: ToLineNumber -
283 * ToLogicalLineNumber - Write up to this source line number
284 * FileId - ID of current listing file
288 * DESCRIPTION: Read then write source lines to the listing file until we have
289 * reached the specified logical (cumulative) line number. This
290 * automatically echos out comment blocks and other non-AML
291 * generating text until we get to the actual AML-generating line
292 * of ASL code specified by the logical line number.
294 ******************************************************************************/
299 UINT32 ToLogicalLineNumber,
303 /* Nothing to do for these file types */
305 if ((FileId == ASL_FILE_ASM_INCLUDE_OUTPUT) ||
306 (FileId == ASL_FILE_C_INCLUDE_OUTPUT))
311 Gbl_CurrentLine = ToLogicalLineNumber;
313 /* Flush any hex bytes remaining from the last opcode */
315 LsFlushListingBuffer (FileId);
317 /* Read lines and write them as long as we are not caught up */
319 if (Gbl_SourceLine < Gbl_CurrentLine)
322 * If we just completed writing some AML hex bytes, output a linefeed
323 * to add some whitespace for readability.
325 if (Gbl_HexBytesWereWritten)
327 FlPrintFile (FileId, "\n");
328 Gbl_HexBytesWereWritten = FALSE;
331 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
333 FlPrintFile (FileId, " /*\n");
336 /* Write one line at a time until we have reached the target line # */
338 while ((Gbl_SourceLine < Gbl_CurrentLine) &&
339 LsWriteOneSourceLine (FileId))
342 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
344 FlPrintFile (FileId, " */");
347 FlPrintFile (FileId, "\n");
352 /*******************************************************************************
354 * FUNCTION: LsWriteOneSourceLine
356 * PARAMETERS: FileId - ID of current listing file
358 * RETURN: FALSE on EOF (input source file), TRUE otherwise
360 * DESCRIPTION: Read one line from the input source file and echo it to the
361 * listing file, prefixed with the line number, and if the source
362 * file contains include files, prefixed with the current filename
364 ******************************************************************************/
367 LsWriteOneSourceLine (
373 BOOLEAN StartOfLine = FALSE;
374 BOOLEAN ProcessLongLine = FALSE;
378 Gbl_ListingNode->LineNumber++;
380 /* Ignore lines that are completely blank (but count the line above) */
382 if (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) != AE_OK)
386 if (FileByte == '\n')
392 * This is a non-empty line, we will print the entire line with
393 * the line number and possibly other prefixes and transforms.
396 /* Line prefixes for special files, C and ASM output */
398 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
400 FlPrintFile (FileId, " *");
402 if (FileId == ASL_FILE_ASM_SOURCE_OUTPUT)
404 FlPrintFile (FileId, "; ");
407 if (Gbl_HasIncludeFiles)
410 * This file contains "include" statements, print the current
411 * filename and line number within the current file
413 FlPrintFile (FileId, "%12s %5d%s",
414 Gbl_ListingNode->Filename, Gbl_ListingNode->LineNumber,
415 ASL_LISTING_LINE_PREFIX);
419 /* No include files, just print the line number */
421 FlPrintFile (FileId, "%8u%s", Gbl_SourceLine,
422 ASL_LISTING_LINE_PREFIX);
425 /* Read the rest of this line (up to a newline or EOF) */
429 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
437 /* Split long input lines for readability in the listing */
442 if (!ProcessLongLine)
444 if ((FileByte != '}') &&
450 ProcessLongLine = TRUE;
455 FlPrintFile (FileId, "\n%*s{\n", Index, " ");
461 else if (FileByte == '}')
465 FlPrintFile (FileId, "\n");
470 FlPrintFile (FileId, "%*s}\n", Index, " ");
474 /* Ignore spaces/tabs at the start of line */
476 else if ((FileByte == ' ') && StartOfLine)
481 else if (StartOfLine)
484 FlPrintFile (FileId, "%*s", Index, " ");
488 FlWriteFile (FileId, &FileByte, 1);
489 if (FileByte == '\n')
492 * This line has been completed.
493 * Check if an error occurred on this source line during the compile.
494 * If so, we print the error message after the source line.
496 LsCheckException (Gbl_SourceLine, FileId);
502 FlWriteFile (FileId, &FileByte, 1);
503 if (FileByte == '\n')
506 * This line has been completed.
507 * Check if an error occurred on this source line during the compile.
508 * If so, we print the error message after the source line.
510 LsCheckException (Gbl_SourceLine, FileId);
515 } while (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) == AE_OK);
517 /* EOF on the input file was reached */
523 /*******************************************************************************
525 * FUNCTION: LsFlushListingBuffer
527 * PARAMETERS: FileId - ID of the listing file
531 * DESCRIPTION: Flush out the current contents of the 16-byte hex AML code
532 * buffer. Usually called at the termination of a single line
533 * of source code or when the buffer is full.
535 ******************************************************************************/
538 LsFlushListingBuffer (
544 if (Gbl_CurrentHexColumn == 0)
549 /* Write the hex bytes */
553 case ASL_FILE_LISTING_OUTPUT:
555 for (i = 0; i < Gbl_CurrentHexColumn; i++)
557 FlPrintFile (FileId, "%2.2X ", Gbl_AmlBuffer[i]);
560 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 3); i++)
562 FlWriteFile (FileId, ".", 1);
565 /* Write the ASCII character associated with each of the bytes */
567 LsDumpAscii (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
571 case ASL_FILE_ASM_SOURCE_OUTPUT:
573 for (i = 0; i < Gbl_CurrentHexColumn; i++)
577 FlPrintFile (FileId, ",");
580 FlPrintFile (FileId, "0%2.2Xh", Gbl_AmlBuffer[i]);
583 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 5); i++)
585 FlWriteFile (FileId, " ", 1);
588 FlPrintFile (FileId, " ;%8.8X",
589 Gbl_CurrentAmlOffset - HEX_LISTING_LINE_SIZE);
591 /* Write the ASCII character associated with each of the bytes */
593 LsDumpAscii (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
597 case ASL_FILE_C_SOURCE_OUTPUT:
599 for (i = 0; i < Gbl_CurrentHexColumn; i++)
601 FlPrintFile (FileId, "0x%2.2X,", Gbl_AmlBuffer[i]);
604 /* Pad hex output with spaces if line is shorter than max line size */
606 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 5); i++)
608 FlWriteFile (FileId, " ", 1);
611 /* AML offset for the start of the line */
613 FlPrintFile (FileId, " /* %8.8X",
614 Gbl_CurrentAmlOffset - Gbl_CurrentHexColumn);
616 /* Write the ASCII character associated with each of the bytes */
618 LsDumpAsciiInComment (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
619 FlPrintFile (FileId, " */");
624 /* No other types supported */
629 FlPrintFile (FileId, "\n");
631 Gbl_CurrentHexColumn = 0;
632 Gbl_HexBytesWereWritten = TRUE;
636 /*******************************************************************************
638 * FUNCTION: LsPushNode
640 * PARAMETERS: Filename - Pointer to the include filename
644 * DESCRIPTION: Push a listing node on the listing/include file stack. This
645 * stack enables tracking of include files (infinitely nested)
646 * and resumption of the listing of the parent file when the
647 * include file is finished.
649 ******************************************************************************/
655 ASL_LISTING_NODE *Lnode;
658 /* Create a new node */
660 Lnode = UtLocalCalloc (sizeof (ASL_LISTING_NODE));
664 Lnode->Filename = Filename;
665 Lnode->LineNumber = 0;
669 Lnode->Next = Gbl_ListingNode;
670 Gbl_ListingNode = Lnode;
674 /*******************************************************************************
676 * FUNCTION: LsPopNode
680 * RETURN: List head after current head is popped off
682 * DESCRIPTION: Pop the current head of the list, free it, and return the
683 * next node on the stack (the new current node).
685 ******************************************************************************/
691 ASL_LISTING_NODE *Lnode;
694 /* Just grab the node at the head of the list */
696 Lnode = Gbl_ListingNode;
700 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
701 "Could not pop empty listing stack");
702 return (Gbl_ListingNode);
705 Gbl_ListingNode = Lnode->Next;
708 /* New "Current" node is the new head */
710 return (Gbl_ListingNode);