Fixed compatibility of output.
[AROS.git] / arch / all-pc / acpica / source / compiler / asllistsup.c
blobd3bf39fca3584dd32f4f253faa01759e0ebdb7c7
1 /******************************************************************************
3 * Module Name: asllistsup - Listing file support utilities
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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
60 * RETURN: None
62 * DESCRIPTION: Convert hex bytes to ascii
64 ******************************************************************************/
66 void
67 LsDumpAscii (
68 UINT32 FileId,
69 UINT32 Count,
70 UINT8 *Buffer)
72 UINT8 BufChar;
73 UINT32 i;
76 FlPrintFile (FileId, " \"");
77 for (i = 0; i < Count; i++)
79 BufChar = Buffer[i];
80 if (isprint (BufChar))
82 FlPrintFile (FileId, "%c", BufChar);
84 else
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
103 * RETURN: None
105 * DESCRIPTION: Convert hex bytes to ascii
107 ******************************************************************************/
109 void
110 LsDumpAsciiInComment (
111 UINT32 FileId,
112 UINT32 Count,
113 UINT8 *Buffer)
115 UINT8 BufChar = 0;
116 UINT8 LastChar;
117 UINT32 i;
120 FlPrintFile (FileId, " \"");
121 for (i = 0; i < Count; i++)
123 LastChar = BufChar;
124 BufChar = Buffer[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);
140 else
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
159 * RETURN: None
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 ******************************************************************************/
168 void
169 LsCheckException (
170 UINT32 LineNumber,
171 UINT32 FileId)
174 if ((!Gbl_NextError) ||
175 (LineNumber < Gbl_NextError->LogicalLineNumber ))
177 return;
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.
205 * RETURN: None
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
209 * AML bytes.
211 ******************************************************************************/
213 void
214 LsWriteListingHexBytes (
215 UINT8 *Buffer,
216 UINT32 Length,
217 UINT32 FileId)
219 UINT32 i;
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, " ");
235 switch (FileId)
237 case ASL_FILE_LISTING_OUTPUT:
239 FlPrintFile (FileId, "%8.8X%s", Gbl_CurrentAmlOffset,
240 ASL_LISTING_LINE_PREFIX);
241 break;
243 case ASL_FILE_ASM_SOURCE_OUTPUT:
245 FlPrintFile (FileId, " db ");
246 break;
248 case ASL_FILE_C_SOURCE_OUTPUT:
250 FlPrintFile (FileId, " ");
251 break;
253 default:
255 /* No other types supported */
257 return;
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
286 * RETURN: None
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 ******************************************************************************/
296 void
297 LsWriteSourceLines (
298 UINT32 ToLineNumber,
299 UINT32 ToLogicalLineNumber,
300 UINT32 FileId)
303 /* Nothing to do for these file types */
305 if ((FileId == ASL_FILE_ASM_INCLUDE_OUTPUT) ||
306 (FileId == ASL_FILE_C_INCLUDE_OUTPUT))
308 return;
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))
340 { ; }
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 ******************************************************************************/
366 UINT32
367 LsWriteOneSourceLine (
368 UINT32 FileId)
370 UINT8 FileByte;
371 UINT32 Column = 0;
372 UINT32 Index = 16;
373 BOOLEAN StartOfLine = FALSE;
374 BOOLEAN ProcessLongLine = FALSE;
377 Gbl_SourceLine++;
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)
384 return (0);
386 if (FileByte == '\n')
388 return (1);
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);
417 else
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)
431 if (FileByte == '/')
433 FileByte = '*';
437 /* Split long input lines for readability in the listing */
439 Column++;
440 if (Column >= 128)
442 if (!ProcessLongLine)
444 if ((FileByte != '}') &&
445 (FileByte != '{'))
447 goto WriteByte;
450 ProcessLongLine = TRUE;
453 if (FileByte == '{')
455 FlPrintFile (FileId, "\n%*s{\n", Index, " ");
456 StartOfLine = TRUE;
457 Index += 4;
458 continue;
461 else if (FileByte == '}')
463 if (!StartOfLine)
465 FlPrintFile (FileId, "\n");
468 StartOfLine = TRUE;
469 Index -= 4;
470 FlPrintFile (FileId, "%*s}\n", Index, " ");
471 continue;
474 /* Ignore spaces/tabs at the start of line */
476 else if ((FileByte == ' ') && StartOfLine)
478 continue;
481 else if (StartOfLine)
483 StartOfLine = FALSE;
484 FlPrintFile (FileId, "%*s", Index, " ");
487 WriteByte:
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);
497 return (1);
500 else
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);
511 return (1);
515 } while (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) == AE_OK);
517 /* EOF on the input file was reached */
519 return (0);
523 /*******************************************************************************
525 * FUNCTION: LsFlushListingBuffer
527 * PARAMETERS: FileId - ID of the listing file
529 * RETURN: None
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 ******************************************************************************/
537 void
538 LsFlushListingBuffer (
539 UINT32 FileId)
541 UINT32 i;
544 if (Gbl_CurrentHexColumn == 0)
546 return;
549 /* Write the hex bytes */
551 switch (FileId)
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);
568 break;
571 case ASL_FILE_ASM_SOURCE_OUTPUT:
573 for (i = 0; i < Gbl_CurrentHexColumn; i++)
575 if (i > 0)
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);
593 break;
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, " */");
619 break;
621 default:
623 /* No other types supported */
625 return;
628 FlPrintFile (FileId, "\n");
630 Gbl_CurrentHexColumn = 0;
631 Gbl_HexBytesWereWritten = TRUE;
635 /*******************************************************************************
637 * FUNCTION: LsPushNode
639 * PARAMETERS: Filename - Pointer to the include filename
641 * RETURN: None
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 ******************************************************************************/
650 void
651 LsPushNode (
652 char *Filename)
654 ASL_LISTING_NODE *Lnode;
657 /* Create a new node */
659 Lnode = UtLocalCalloc (sizeof (ASL_LISTING_NODE));
661 /* Initialize */
663 Lnode->Filename = Filename;
664 Lnode->LineNumber = 0;
666 /* Link (push) */
668 Lnode->Next = Gbl_ListingNode;
669 Gbl_ListingNode = Lnode;
673 /*******************************************************************************
675 * FUNCTION: LsPopNode
677 * PARAMETERS: None
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 ******************************************************************************/
686 ASL_LISTING_NODE *
687 LsPopNode (
688 void)
690 ASL_LISTING_NODE *Lnode;
693 /* Just grab the node at the head of the list */
695 Lnode = Gbl_ListingNode;
696 if ((!Lnode) ||
697 (!Lnode->Next))
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;
705 ACPI_FREE (Lnode);
707 /* New "Current" node is the new head */
709 return (Gbl_ListingNode);