1 /******************************************************************************
3 * Module Name: asllistsup - Listing file support utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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 "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
, ".");
91 FlPrintFile (FileId
, "\"");
95 /*******************************************************************************
97 * FUNCTION: LsDumpAsciiInComment
99 * PARAMETERS: FileId - ID of current listing file
100 * Count - Number of bytes to convert
101 * Buffer - Buffer of bytes to convert
105 * DESCRIPTION: Convert hex bytes to ascii
107 ******************************************************************************/
110 LsDumpAsciiInComment (
120 FlPrintFile (FileId
, " \"");
121 for (i
= 0; i
< Count
; i
++)
126 if (isprint (BufChar
))
128 /* Handle embedded C comment sequences */
130 if (((LastChar
== '*') && (BufChar
== '/')) ||
131 ((LastChar
== '/') && (BufChar
== '*')))
133 /* Insert a space to break the sequence */
135 FlPrintFile (FileId
, ".", BufChar
);
138 FlPrintFile (FileId
, "%c", BufChar
);
142 /* Not a printable character, just put out a dot */
144 FlPrintFile (FileId
, ".");
148 FlPrintFile (FileId
, "\"");
152 /*******************************************************************************
154 * FUNCTION: LsCheckException
156 * PARAMETERS: LineNumber - Current logical (cumulative) line #
157 * FileId - ID of output listing file
161 * DESCRIPTION: Check if there is an exception for this line, and if there is,
162 * put it in the listing immediately. Handles multiple errors
163 * per line. Gbl_NextError points to the next error in the
164 * sorted (by line #) list of compile errors/warnings.
166 ******************************************************************************/
174 if ((!Gbl_NextError
) ||
175 (LineNumber
< Gbl_NextError
->LogicalLineNumber
))
180 /* Handle multiple errors per line */
182 if (FileId
== ASL_FILE_LISTING_OUTPUT
)
184 while (Gbl_NextError
&&
185 (LineNumber
>= Gbl_NextError
->LogicalLineNumber
))
187 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
, ",");
579 FlPrintFile (FileId
, "0%2.2Xh", Gbl_AmlBuffer
[i
]);
582 for (i
= 0; i
< ((HEX_LISTING_LINE_SIZE
- Gbl_CurrentHexColumn
) * 5); i
++)
584 FlWriteFile (FileId
, " ", 1);
587 FlPrintFile (FileId
, " ;%8.8X",
588 Gbl_CurrentAmlOffset
- HEX_LISTING_LINE_SIZE
);
590 /* Write the ASCII character associated with each of the bytes */
592 LsDumpAscii (FileId
, Gbl_CurrentHexColumn
, Gbl_AmlBuffer
);
596 case ASL_FILE_C_SOURCE_OUTPUT
:
598 for (i
= 0; i
< Gbl_CurrentHexColumn
; i
++)
600 FlPrintFile (FileId
, "0x%2.2X,", Gbl_AmlBuffer
[i
]);
603 /* Pad hex output with spaces if line is shorter than max line size */
605 for (i
= 0; i
< ((HEX_LISTING_LINE_SIZE
- Gbl_CurrentHexColumn
) * 5); i
++)
607 FlWriteFile (FileId
, " ", 1);
610 /* AML offset for the start of the line */
612 FlPrintFile (FileId
, " /* %8.8X",
613 Gbl_CurrentAmlOffset
- Gbl_CurrentHexColumn
);
615 /* Write the ASCII character associated with each of the bytes */
617 LsDumpAsciiInComment (FileId
, Gbl_CurrentHexColumn
, Gbl_AmlBuffer
);
618 FlPrintFile (FileId
, " */");
623 /* No other types supported */
628 FlPrintFile (FileId
, "\n");
630 Gbl_CurrentHexColumn
= 0;
631 Gbl_HexBytesWereWritten
= TRUE
;
635 /*******************************************************************************
637 * FUNCTION: LsPushNode
639 * PARAMETERS: Filename - Pointer to the include filename
643 * DESCRIPTION: Push a listing node on the listing/include file stack. This
644 * stack enables tracking of include files (infinitely nested)
645 * and resumption of the listing of the parent file when the
646 * include file is finished.
648 ******************************************************************************/
654 ASL_LISTING_NODE
*Lnode
;
657 /* Create a new node */
659 Lnode
= UtLocalCalloc (sizeof (ASL_LISTING_NODE
));
663 Lnode
->Filename
= Filename
;
664 Lnode
->LineNumber
= 0;
668 Lnode
->Next
= Gbl_ListingNode
;
669 Gbl_ListingNode
= Lnode
;
673 /*******************************************************************************
675 * FUNCTION: LsPopNode
679 * RETURN: List head after current head is popped off
681 * DESCRIPTION: Pop the current head of the list, free it, and return the
682 * next node on the stack (the new current node).
684 ******************************************************************************/
690 ASL_LISTING_NODE
*Lnode
;
693 /* Just grab the node at the head of the list */
695 Lnode
= Gbl_ListingNode
;
699 AslError (ASL_ERROR
, ASL_MSG_COMPILER_INTERNAL
, NULL
,
700 "Could not pop empty listing stack");
701 return (Gbl_ListingNode
);
704 Gbl_ListingNode
= Lnode
->Next
;
707 /* New "Current" node is the new head */
709 return (Gbl_ListingNode
);