Import acpica-unix-20061109 from Intel.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / compiler / asllisting.c
blobb8e64922204a10381728fa90e7e0dacb2b3d3b40
2 /******************************************************************************
4 * Module Name: asllisting - Listing file generation
5 * $Revision: 1.62 $
7 *****************************************************************************/
9 /******************************************************************************
11 * 1. Copyright Notice
13 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
14 * All rights reserved.
16 * 2. License
18 * 2.1. This is your license from Intel Corp. under its intellectual property
19 * rights. You may have additional license terms from the party that provided
20 * you this software, covering your right to use that party's intellectual
21 * property rights.
23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24 * copy of the source code appearing in this file ("Covered Code") an
25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26 * base code distributed originally by Intel ("Original Intel Code") to copy,
27 * make derivatives, distribute, use and display any portion of the Covered
28 * Code in any form, with the right to sublicense such rights; and
30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31 * license (with the right to sublicense), under only those claims of Intel
32 * patents that are infringed by the Original Intel Code, to make, use, sell,
33 * offer to sell, and import the Covered Code and derivative works thereof
34 * solely to the minimum extent necessary to exercise the above copyright
35 * license, and in no event shall the patent license extend to any additions
36 * to or modifications of the Original Intel Code. No other license or right
37 * is granted directly or by implication, estoppel or otherwise;
39 * The above copyright and patent license is granted only if the following
40 * conditions are met:
42 * 3. Conditions
44 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45 * Redistribution of source code of any substantial portion of the Covered
46 * Code or modification with rights to further distribute source must include
47 * the above Copyright Notice, the above License, this list of Conditions,
48 * and the following Disclaimer and Export Compliance provision. In addition,
49 * Licensee must cause all Covered Code to which Licensee contributes to
50 * contain a file documenting the changes Licensee made to create that Covered
51 * Code and the date of any change. Licensee must include in that file the
52 * documentation of any changes made by any predecessor Licensee. Licensee
53 * must include a prominent statement that the modification is derived,
54 * directly or indirectly, from Original Intel Code.
56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57 * Redistribution of source code of any substantial portion of the Covered
58 * Code or modification without rights to further distribute source must
59 * include the following Disclaimer and Export Compliance provision in the
60 * documentation and/or other materials provided with distribution. In
61 * addition, Licensee may not authorize further sublicense of source of any
62 * portion of the Covered Code, and must include terms to the effect that the
63 * license from Licensee to its licensee is limited to the intellectual
64 * property embodied in the software Licensee provides to its licensee, and
65 * not to intellectual property embodied in modifications its licensee may
66 * make.
68 * 3.3. Redistribution of Executable. Redistribution in executable form of any
69 * substantial portion of the Covered Code or modification must reproduce the
70 * above Copyright Notice, and the following Disclaimer and Export Compliance
71 * provision in the documentation and/or other materials provided with the
72 * distribution.
74 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * Intel Code.
77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78 * Intel shall be used in advertising or otherwise to promote the sale, use or
79 * other dealings in products derived from or relating to the Covered Code
80 * without prior written authorization from Intel.
82 * 4. Disclaimer and Export Compliance
84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * PARTICULAR PURPOSE.
92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * LIMITED REMEDY.
101 * 4.3. Licensee shall not export, either directly or indirectly, any of this
102 * software or system incorporating such software without first obtaining any
103 * required license or other approval from the U. S. Department of Commerce or
104 * any other agency or department of the United States Government. In the
105 * event Licensee exports any such software from the United States or
106 * re-exports any such software from a foreign destination, Licensee shall
107 * ensure that the distribution and export/re-export of the software is in
108 * compliance with all laws, regulations, orders, or other restrictions of the
109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110 * any of its subsidiaries will export/re-export any technical data, process,
111 * software, or service, directly or indirectly, to any country for which the
112 * United States government or any agency thereof requires an export license,
113 * other governmental approval, or letter of assurance, without first obtaining
114 * such license, approval or letter.
116 *****************************************************************************/
119 #include "aslcompiler.h"
120 #include "aslcompiler.y.h"
121 #include "amlcode.h"
122 #include "acparser.h"
123 #include "acnamesp.h"
125 #define _COMPONENT ACPI_COMPILER
126 ACPI_MODULE_NAME ("aslisting")
128 /* Local prototypes */
130 static void
131 LsDumpAscii (
132 UINT32 FileId,
133 UINT32 Count,
134 UINT8 *Buffer);
136 static void
137 LsDumpAsciiInComment (
138 UINT32 FileId,
139 UINT32 Count,
140 UINT8 *Buffer);
142 static ACPI_STATUS
143 LsAmlListingWalk (
144 ACPI_PARSE_OBJECT *Op,
145 UINT32 Level,
146 void *Context);
148 static void
149 LsGenerateListing (
150 UINT32 FileId);
152 static void
153 LsPushNode (
154 char *Filename);
156 static ASL_LISTING_NODE *
157 LsPopNode (
158 void);
160 static void
161 LsCheckException (
162 UINT32 LineNumber,
163 UINT32 FileId);
165 static void
166 LsFlushListingBuffer (
167 UINT32 FileId);
169 static void
170 LsWriteListingHexBytes (
171 UINT8 *Buffer,
172 UINT32 Length,
173 UINT32 FileId);
175 static UINT32
176 LsWriteOneSourceLine (
177 UINT32 FileId);
179 static void
180 LsFinishSourceListing (
181 UINT32 FileId);
183 static void
184 LsWriteSourceLines (
185 UINT32 ToLineNumber,
186 UINT32 ToLogicalLineNumber,
187 UINT32 FileId);
189 static void
190 LsWriteNodeToListing (
191 ACPI_PARSE_OBJECT *Op,
192 UINT32 FileId);
194 static void
195 LsDoHexOutputC (
196 void);
198 static void
199 LsDoHexOutputAsm (
200 void);
203 /*******************************************************************************
205 * FUNCTION: LsTreeWriteWalk
207 * PARAMETERS: ASL_WALK_CALLBACK
210 * RETURN: None.
212 * DESCRIPTION: Dump entire parse tree, for compiler debug only
214 ******************************************************************************/
216 ACPI_STATUS
217 LsTreeWriteWalk (
218 ACPI_PARSE_OBJECT *Op,
219 UINT32 Level,
220 void *Context)
223 /* Debug output */
225 DbgPrint (ASL_TREE_OUTPUT,
226 "%5.5d [%2d]", Op->Asl.LogicalLineNumber, Level);
227 UtPrintFormattedName (Op->Asl.ParseOpcode, Level);
230 DbgPrint (ASL_TREE_OUTPUT, "\n");
231 return (AE_OK);
235 void
236 LsDumpParseTree (
237 void)
240 if (!Gbl_DebugFlag)
242 return;
245 DbgPrint (ASL_TREE_OUTPUT, "\nOriginal parse tree from parser:\n\n");
246 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
247 LsTreeWriteWalk, NULL, NULL);
251 /*******************************************************************************
253 * FUNCTION: LsDumpAscii
255 * PARAMETERS: FileId - ID of current listing file
256 * Count - Number of bytes to convert
257 * Buffer - Buffer of bytes to convert
259 * RETURN: None.
261 * DESCRIPTION: Convert hex bytes to ascii
263 ******************************************************************************/
265 static void
266 LsDumpAscii (
267 UINT32 FileId,
268 UINT32 Count,
269 UINT8 *Buffer)
271 UINT8 BufChar;
272 UINT32 i;
275 FlPrintFile (FileId, " \"");
276 for (i = 0; i < Count; i++)
278 BufChar = Buffer[i];
279 if (isprint (BufChar))
281 FlPrintFile (FileId, "%c", BufChar);
283 else
285 /* Not a printable character, just put out a dot */
287 FlPrintFile (FileId, ".");
290 FlPrintFile (FileId, "\"");
294 /*******************************************************************************
296 * FUNCTION: LsDumpAsciiInComment
298 * PARAMETERS: FileId - ID of current listing file
299 * Count - Number of bytes to convert
300 * Buffer - Buffer of bytes to convert
302 * RETURN: None.
304 * DESCRIPTION: Convert hex bytes to ascii
306 ******************************************************************************/
308 static void
309 LsDumpAsciiInComment (
310 UINT32 FileId,
311 UINT32 Count,
312 UINT8 *Buffer)
314 UINT8 BufChar = 0;
315 UINT8 LastChar;
316 UINT32 i;
319 FlPrintFile (FileId, " \"");
320 for (i = 0; i < Count; i++)
322 LastChar = BufChar;
323 BufChar = Buffer[i];
325 if (isprint (BufChar))
327 /* Handle embedded C comment sequences */
329 if (((LastChar == '*') && (BufChar == '/')) ||
330 ((LastChar == '/') && (BufChar == '*')))
332 /* Insert a space to break the sequence */
334 FlPrintFile (FileId, ".", BufChar);
337 FlPrintFile (FileId, "%c", BufChar);
339 else
341 /* Not a printable character, just put out a dot */
343 FlPrintFile (FileId, ".");
346 FlPrintFile (FileId, "\"");
350 /*******************************************************************************
352 * FUNCTION: LsAmlListingWalk
354 * PARAMETERS: ASL_WALK_CALLBACK
356 * RETURN: Status
358 * DESCRIPTION: Process one node during a listing file generation.
360 ******************************************************************************/
362 static ACPI_STATUS
363 LsAmlListingWalk (
364 ACPI_PARSE_OBJECT *Op,
365 UINT32 Level,
366 void *Context)
368 UINT8 FileByte;
369 UINT32 i;
370 UINT32 FileId = (UINT32) ACPI_TO_INTEGER (Context);
373 LsWriteNodeToListing (Op, FileId);
375 if (Op->Asl.CompileFlags & NODE_IS_RESOURCE_DATA)
377 /* Buffer is a resource template, don't dump the data all at once */
379 return (AE_OK);
382 /* Write the hex bytes to the listing file(s) (if requested) */
384 for (i = 0; i < Op->Asl.FinalAmlLength; i++)
386 if (ACPI_FAILURE (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte, 1)))
388 FlFileError (ASL_FILE_AML_OUTPUT, ASL_MSG_READ);
389 AslAbort ();
391 LsWriteListingHexBytes (&FileByte, 1, FileId);
394 return (AE_OK);
398 /*******************************************************************************
400 * FUNCTION: LsGenerateListing
402 * PARAMETERS: FileId - ID of listing file
404 * RETURN: None
406 * DESCRIPTION: Generate a listing file. This can be one of the several types
407 * of "listings" supported.
409 ******************************************************************************/
411 static void
412 LsGenerateListing (
413 UINT32 FileId)
416 /* Start at the beginning of both the source and AML files */
418 FlSeekFile (ASL_FILE_SOURCE_OUTPUT, 0);
419 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
420 Gbl_SourceLine = 0;
421 Gbl_CurrentHexColumn = 0;
422 LsPushNode (Gbl_Files[ASL_FILE_INPUT].Filename);
424 /* Process all parse nodes */
426 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD, LsAmlListingWalk,
427 NULL, (void *) ACPI_TO_POINTER (FileId));
429 /* Final processing */
431 LsFinishSourceListing (FileId);
435 /*******************************************************************************
437 * FUNCTION: LsDoListings
439 * PARAMETERS: None.
441 * RETURN: None
443 * DESCRIPTION: Generate all requested listing files.
445 ******************************************************************************/
447 void
448 LsDoListings (
449 void)
452 if (Gbl_C_OutputFlag)
454 LsGenerateListing (ASL_FILE_C_SOURCE_OUTPUT);
457 if (Gbl_ListingFlag)
459 LsGenerateListing (ASL_FILE_LISTING_OUTPUT);
462 if (Gbl_AsmOutputFlag)
464 LsGenerateListing (ASL_FILE_ASM_SOURCE_OUTPUT);
467 if (Gbl_C_IncludeOutputFlag)
469 LsGenerateListing (ASL_FILE_C_INCLUDE_OUTPUT);
472 if (Gbl_AsmIncludeOutputFlag)
474 LsGenerateListing (ASL_FILE_ASM_INCLUDE_OUTPUT);
479 /*******************************************************************************
481 * FUNCTION: LsPushNode
483 * PARAMETERS: Filename - Pointer to the include filename
485 * RETURN: None
487 * DESCRIPTION: Push a listing node on the listing/include file stack. This
488 * stack enables tracking of include files (infinitely nested)
489 * and resumption of the listing of the parent file when the
490 * include file is finished.
492 ******************************************************************************/
494 static void
495 LsPushNode (
496 char *Filename)
498 ASL_LISTING_NODE *Lnode;
501 /* Create a new node */
503 Lnode = UtLocalCalloc (sizeof (ASL_LISTING_NODE));
505 /* Initialize */
507 Lnode->Filename = Filename;
508 Lnode->LineNumber = 0;
510 /* Link (push) */
512 Lnode->Next = Gbl_ListingNode;
513 Gbl_ListingNode = Lnode;
517 /*******************************************************************************
519 * FUNCTION: LsPopNode
521 * PARAMETERS: None
523 * RETURN: List head after current head is popped off
525 * DESCRIPTION: Pop the current head of the list, free it, and return the
526 * next node on the stack (the new current node).
528 ******************************************************************************/
530 static ASL_LISTING_NODE *
531 LsPopNode (
532 void)
534 ASL_LISTING_NODE *Lnode;
537 /* Just grab the node at the head of the list */
539 Lnode = Gbl_ListingNode;
540 if ((!Lnode) ||
541 (!Lnode->Next))
543 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, NULL,
544 "Could not pop empty listing stack");
545 return Gbl_ListingNode;
548 Gbl_ListingNode = Lnode->Next;
549 ACPI_FREE (Lnode);
551 /* New "Current" node is the new head */
553 return (Gbl_ListingNode);
557 /*******************************************************************************
559 * FUNCTION: LsCheckException
561 * PARAMETERS: LineNumber - Current logical (cumulative) line #
562 * FileId - ID of output listing file
564 * RETURN: None
566 * DESCRIPTION: Check if there is an exception for this line, and if there is,
567 * put it in the listing immediately. Handles multiple errors
568 * per line. Gbl_NextError points to the next error in the
569 * sorted (by line #) list of compile errors/warnings.
571 ******************************************************************************/
573 static void
574 LsCheckException (
575 UINT32 LineNumber,
576 UINT32 FileId)
579 if ((!Gbl_NextError) ||
580 (LineNumber < Gbl_NextError->LogicalLineNumber ))
582 return;
585 /* Handle multiple errors per line */
587 if (FileId == ASL_FILE_LISTING_OUTPUT)
589 while (Gbl_NextError &&
590 (LineNumber >= Gbl_NextError->LogicalLineNumber))
592 AePrintException (FileId, Gbl_NextError, "\n[****iasl****]\n");
594 Gbl_NextError = Gbl_NextError->Next;
597 FlPrintFile (FileId, "\n");
602 /*******************************************************************************
604 * FUNCTION: LsFlushListingBuffer
606 * PARAMETERS: FileId - ID of the listing file
608 * RETURN: None
610 * DESCRIPTION: Flush out the current contents of the 16-byte hex AML code
611 * buffer. Usually called at the termination of a single line
612 * of source code or when the buffer is full.
614 ******************************************************************************/
616 static void
617 LsFlushListingBuffer (
618 UINT32 FileId)
620 UINT32 i;
623 if (Gbl_CurrentHexColumn == 0)
625 return;
628 /* Write the hex bytes */
630 switch (FileId)
632 case ASL_FILE_LISTING_OUTPUT:
634 for (i = 0; i < Gbl_CurrentHexColumn; i++)
636 FlPrintFile (FileId, "%2.2X ", Gbl_AmlBuffer[i]);
639 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 3); i++)
641 FlWriteFile (FileId, ".", 1);
644 /* Write the ASCII character associated with each of the bytes */
646 LsDumpAscii (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
647 break;
650 case ASL_FILE_ASM_SOURCE_OUTPUT:
652 for (i = 0; i < Gbl_CurrentHexColumn; i++)
654 if (i > 0)
656 FlPrintFile (FileId, ",");
658 FlPrintFile (FileId, "0%2.2Xh", Gbl_AmlBuffer[i]);
661 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 5); i++)
663 FlWriteFile (FileId, " ", 1);
666 FlPrintFile (FileId, " ;%8.8X",
667 Gbl_CurrentAmlOffset - HEX_LISTING_LINE_SIZE);
669 /* Write the ASCII character associated with each of the bytes */
671 LsDumpAscii (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
672 break;
675 case ASL_FILE_C_SOURCE_OUTPUT:
677 for (i = 0; i < Gbl_CurrentHexColumn; i++)
679 FlPrintFile (FileId, "0x%2.2X,", Gbl_AmlBuffer[i]);
682 for (i = 0; i < ((HEX_LISTING_LINE_SIZE - Gbl_CurrentHexColumn) * 5); i++)
684 FlWriteFile (FileId, " ", 1);
687 FlPrintFile (FileId, " /* %8.8X",
688 Gbl_CurrentAmlOffset - HEX_LISTING_LINE_SIZE);
690 /* Write the ASCII character associated with each of the bytes */
692 LsDumpAsciiInComment (FileId, Gbl_CurrentHexColumn, Gbl_AmlBuffer);
693 FlPrintFile (FileId, " */");
694 break;
696 default:
697 /* No other types supported */
698 return;
701 FlPrintFile (FileId, "\n");
703 Gbl_CurrentHexColumn = 0;
704 Gbl_HexBytesWereWritten = TRUE;
708 /*******************************************************************************
710 * FUNCTION: LsWriteListingHexBytes
712 * PARAMETERS: Buffer - AML code buffer
713 * Length - Number of AML bytes to write
714 * FileId - ID of current listing file.
716 * RETURN: None
718 * DESCRIPTION: Write the contents of the AML buffer to the listing file via
719 * the listing buffer. The listing buffer is flushed every 16
720 * AML bytes.
722 ******************************************************************************/
724 static void
725 LsWriteListingHexBytes (
726 UINT8 *Buffer,
727 UINT32 Length,
728 UINT32 FileId)
730 UINT32 i;
733 /* Transfer all requested bytes */
735 for (i = 0; i < Length; i++)
737 /* Print line header when buffer is empty */
739 if (Gbl_CurrentHexColumn == 0)
741 if (Gbl_HasIncludeFiles)
743 FlPrintFile (FileId, "%*s", 10, " ");
746 switch (FileId)
748 case ASL_FILE_LISTING_OUTPUT:
750 FlPrintFile (FileId, "%8.8X....", Gbl_CurrentAmlOffset);
751 break;
753 case ASL_FILE_ASM_SOURCE_OUTPUT:
755 FlPrintFile (FileId, " db ");
756 break;
758 case ASL_FILE_C_SOURCE_OUTPUT:
760 FlPrintFile (FileId, " ");
761 break;
763 default:
764 /* No other types supported */
765 return;
769 /* Transfer AML byte and update counts */
771 Gbl_AmlBuffer[Gbl_CurrentHexColumn] = Buffer[i];
773 Gbl_CurrentHexColumn++;
774 Gbl_CurrentAmlOffset++;
776 /* Flush buffer when it is full */
778 if (Gbl_CurrentHexColumn >= HEX_LISTING_LINE_SIZE)
780 LsFlushListingBuffer (FileId);
786 /*******************************************************************************
788 * FUNCTION: LsWriteOneSourceLine
790 * PARAMETERS: FileID - ID of current listing file
792 * RETURN: FALSE on EOF (input source file), TRUE otherwise
794 * DESCRIPTION: Read one line from the input source file and echo it to the
795 * listing file, prefixed with the line number, and if the source
796 * file contains include files, prefixed with the current filename
798 ******************************************************************************/
800 static UINT32
801 LsWriteOneSourceLine (
802 UINT32 FileId)
804 UINT8 FileByte;
807 Gbl_SourceLine++;
808 Gbl_ListingNode->LineNumber++;
810 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
812 FlPrintFile (FileId, " *");
814 if (FileId == ASL_FILE_ASM_SOURCE_OUTPUT)
816 FlPrintFile (FileId, "; ");
819 if (Gbl_HasIncludeFiles)
822 * This file contains "include" statements, print the current
823 * filename and line number within the current file
825 FlPrintFile (FileId, "%12s %5d....",
826 Gbl_ListingNode->Filename, Gbl_ListingNode->LineNumber);
828 else
830 /* No include files, just print the line number */
832 FlPrintFile (FileId, "%8d....", Gbl_SourceLine);
835 /* Read one line (up to a newline or EOF) */
837 while (FlReadFile (ASL_FILE_SOURCE_OUTPUT, &FileByte, 1) == AE_OK)
839 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
841 if (FileByte == '/')
843 FileByte = '*';
847 FlWriteFile (FileId, &FileByte, 1);
848 if (FileByte == '\n')
851 * Check if an error occurred on this source line during the compile.
852 * If so, we print the error message after the source line.
854 LsCheckException (Gbl_SourceLine, FileId);
855 return (1);
859 /* EOF on the input file was reached */
861 return (0);
865 /*******************************************************************************
867 * FUNCTION: LsFinishSourceListing
869 * PARAMETERS: FileId - ID of current listing file.
871 * RETURN: None
873 * DESCRIPTION: Cleanup routine for the listing file. Flush the hex AML
874 * listing buffer, and flush out any remaining lines in the
875 * source input file.
877 ******************************************************************************/
879 static void
880 LsFinishSourceListing (
881 UINT32 FileId)
884 if ((FileId == ASL_FILE_ASM_INCLUDE_OUTPUT) ||
885 (FileId == ASL_FILE_C_INCLUDE_OUTPUT))
887 return;
890 LsFlushListingBuffer (FileId);
891 Gbl_CurrentAmlOffset = 0;
893 /* Flush any remaining text in the source file */
895 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
897 FlPrintFile (FileId, " /*\n");
900 while (LsWriteOneSourceLine (FileId))
901 { ; }
903 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
905 FlPrintFile (FileId, "\n */\n };\n");
908 FlPrintFile (FileId, "\n");
910 if (FileId == ASL_FILE_LISTING_OUTPUT)
912 /* Print a summary of the compile exceptions */
914 FlPrintFile (FileId, "\n\nSummary of errors and warnings\n\n");
915 AePrintErrorLog (FileId);
916 FlPrintFile (FileId, "\n\n");
917 UtDisplaySummary (FileId);
918 FlPrintFile (FileId, "\n\n");
923 /*******************************************************************************
925 * FUNCTION: LsWriteSourceLines
927 * PARAMETERS: ToLineNumber -
928 * ToLogicalLineNumber - Write up to this source line number
929 * FileId - ID of current listing file
931 * RETURN: None
933 * DESCRIPTION: Read then write source lines to the listing file until we have
934 * reached the specified logical (cumulative) line number. This
935 * automatically echos out comment blocks and other non-AML
936 * generating text until we get to the actual AML-generating line
937 * of ASL code specified by the logical line number.
939 ******************************************************************************/
941 static void
942 LsWriteSourceLines (
943 UINT32 ToLineNumber,
944 UINT32 ToLogicalLineNumber,
945 UINT32 FileId)
948 if ((FileId == ASL_FILE_ASM_INCLUDE_OUTPUT) ||
949 (FileId == ASL_FILE_C_INCLUDE_OUTPUT))
951 return;
954 Gbl_CurrentLine = ToLogicalLineNumber;
956 /* Flush any hex bytes remaining from the last opcode */
958 LsFlushListingBuffer (FileId);
960 /* Read lines and write them as long as we are not caught up */
962 if (Gbl_SourceLine < Gbl_CurrentLine)
965 * If we just completed writing some AML hex bytes, output a linefeed
966 * to add some whitespace for readability.
968 if (Gbl_HexBytesWereWritten)
970 FlPrintFile (FileId, "\n");
971 Gbl_HexBytesWereWritten = FALSE;
974 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
976 FlPrintFile (FileId, " /*\n");
979 /* Write one line at a time until we have reached the target line # */
981 while ((Gbl_SourceLine < Gbl_CurrentLine) &&
982 LsWriteOneSourceLine (FileId))
983 { ; }
985 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
987 FlPrintFile (FileId, " */");
989 FlPrintFile (FileId, "\n");
994 /*******************************************************************************
996 * FUNCTION: LsWriteNodeToListing
998 * PARAMETERS: Op - Parse node to write to the listing file.
999 * FileId - ID of current listing file
1001 * RETURN: None.
1003 * DESCRIPTION: Write "a node" to the listing file. This means to
1004 * 1) Write out all of the source text associated with the node
1005 * 2) Write out all of the AML bytes associated with the node
1006 * 3) Write any compiler exceptions associated with the node
1008 ******************************************************************************/
1010 static void
1011 LsWriteNodeToListing (
1012 ACPI_PARSE_OBJECT *Op,
1013 UINT32 FileId)
1015 const ACPI_OPCODE_INFO *OpInfo;
1016 UINT32 OpClass;
1017 char *Pathname;
1018 UINT32 Length;
1019 UINT32 i;
1022 OpInfo = AcpiPsGetOpcodeInfo (Op->Asl.AmlOpcode);
1023 OpClass = OpInfo->Class;
1025 /* TBD: clean this up with a single flag that says:
1026 * I start a named output block
1028 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
1030 switch (Op->Asl.ParseOpcode)
1032 case PARSEOP_DEFINITIONBLOCK:
1033 case PARSEOP_METHODCALL:
1034 case PARSEOP_INCLUDE:
1035 case PARSEOP_INCLUDE_END:
1036 case PARSEOP_DEFAULT_ARG:
1038 break;
1040 default:
1041 switch (OpClass)
1043 case AML_CLASS_NAMED_OBJECT:
1044 switch (Op->Asl.AmlOpcode)
1046 case AML_SCOPE_OP:
1047 case AML_ALIAS_OP:
1048 break;
1050 default:
1051 if (Op->Asl.ExternalName)
1053 LsFlushListingBuffer (FileId);
1054 FlPrintFile (FileId, " };\n");
1056 break;
1058 break;
1060 default:
1061 /* Don't care about other objects */
1062 break;
1064 break;
1068 /* These cases do not have a corresponding AML opcode */
1070 switch (Op->Asl.ParseOpcode)
1072 case PARSEOP_DEFINITIONBLOCK:
1074 LsWriteSourceLines (Op->Asl.EndLine, Op->Asl.EndLogicalLine, FileId);
1076 /* Use the table Signature and TableId to build a unique name */
1078 if (FileId == ASL_FILE_ASM_SOURCE_OUTPUT)
1080 FlPrintFile (FileId,
1081 "%s_%s_Header \\\n",
1082 Gbl_TableSignature, Gbl_TableId);
1084 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
1086 FlPrintFile (FileId,
1087 " unsigned char %s_%s_Header [] =\n {\n",
1088 Gbl_TableSignature, Gbl_TableId);
1090 if (FileId == ASL_FILE_ASM_INCLUDE_OUTPUT)
1092 FlPrintFile (FileId,
1093 "extrn %s_%s_Header : byte\n",
1094 Gbl_TableSignature, Gbl_TableId);
1096 if (FileId == ASL_FILE_C_INCLUDE_OUTPUT)
1098 FlPrintFile (FileId,
1099 "extern unsigned char %s_%s_Header [];\n",
1100 Gbl_TableSignature, Gbl_TableId);
1102 return;
1105 case PARSEOP_METHODCALL:
1107 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.LogicalLineNumber,
1108 FileId);
1109 return;
1112 case PARSEOP_INCLUDE:
1114 /* Flush everything up to and including the include source line */
1116 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.LogicalLineNumber,
1117 FileId);
1119 /* Create a new listing node and push it */
1121 LsPushNode (Op->Asl.Child->Asl.Value.String);
1122 return;
1125 case PARSEOP_INCLUDE_END:
1127 /* Flush out the rest of the include file */
1129 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.LogicalLineNumber,
1130 FileId);
1132 /* Pop off this listing node and go back to the parent file */
1134 (void) LsPopNode ();
1135 return;
1138 case PARSEOP_DEFAULT_ARG:
1140 if (Op->Asl.CompileFlags & NODE_IS_RESOURCE_DESC)
1142 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.EndLogicalLine,
1143 FileId);
1145 return;
1148 default:
1149 /* All other opcodes have an AML opcode */
1150 break;
1154 * Otherwise, we look at the AML opcode because we can
1155 * switch on the opcode type, getting an entire class
1156 * at once
1158 switch (OpClass)
1160 case AML_CLASS_ARGUMENT: /* argument type only */
1161 case AML_CLASS_INTERNAL:
1163 break;
1166 case AML_CLASS_NAMED_OBJECT:
1168 switch (Op->Asl.AmlOpcode)
1170 case AML_FIELD_OP:
1171 case AML_INDEX_FIELD_OP:
1172 case AML_BANK_FIELD_OP:
1175 * For fields, we want to dump all the AML after the
1176 * entire definition
1178 LsWriteSourceLines (Op->Asl.EndLine, Op->Asl.EndLogicalLine,
1179 FileId);
1180 break;
1182 case AML_NAME_OP:
1184 if (Op->Asl.CompileFlags & NODE_IS_RESOURCE_DESC)
1186 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.LogicalLineNumber,
1187 FileId);
1189 else
1192 * For fields, we want to dump all the AML after the
1193 * entire definition
1195 LsWriteSourceLines (Op->Asl.EndLine, Op->Asl.EndLogicalLine,
1196 FileId);
1198 break;
1200 default:
1201 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.LogicalLineNumber,
1202 FileId);
1203 break;
1206 switch (Op->Asl.AmlOpcode)
1208 case AML_SCOPE_OP:
1209 case AML_ALIAS_OP:
1211 /* These opcodes do not declare a new object, ignore them */
1213 break;
1215 default:
1217 /* All other named object opcodes come here */
1219 switch (FileId)
1221 case ASL_FILE_ASM_SOURCE_OUTPUT:
1222 case ASL_FILE_C_SOURCE_OUTPUT:
1223 case ASL_FILE_ASM_INCLUDE_OUTPUT:
1224 case ASL_FILE_C_INCLUDE_OUTPUT:
1227 * For named objects, we will create a valid symbol so that the
1228 * AML code can be referenced from C or ASM
1230 if (Op->Asl.ExternalName)
1232 /* Get the full pathname associated with this node */
1234 Pathname = AcpiNsGetExternalPathname (Op->Asl.Node);
1235 Length = strlen (Pathname);
1236 if (Length >= 4)
1238 /* Convert all dots in the path to underscores */
1240 for (i = 0; i < Length; i++)
1242 if (Pathname[i] == '.')
1244 Pathname[i] = '_';
1248 /* Create the appropriate symbol in the output file */
1250 if (FileId == ASL_FILE_ASM_SOURCE_OUTPUT)
1252 FlPrintFile (FileId,
1253 "%s_%s_%s \\\n",
1254 Gbl_TableSignature, Gbl_TableId, &Pathname[1]);
1256 if (FileId == ASL_FILE_C_SOURCE_OUTPUT)
1258 FlPrintFile (FileId,
1259 " unsigned char %s_%s_%s [] =\n {\n",
1260 Gbl_TableSignature, Gbl_TableId, &Pathname[1]);
1262 if (FileId == ASL_FILE_ASM_INCLUDE_OUTPUT)
1264 FlPrintFile (FileId,
1265 "extrn %s_%s_%s : byte\n",
1266 Gbl_TableSignature, Gbl_TableId, &Pathname[1]);
1268 if (FileId == ASL_FILE_C_INCLUDE_OUTPUT)
1270 FlPrintFile (FileId,
1271 "extern unsigned char %s_%s_%s [];\n",
1272 Gbl_TableSignature, Gbl_TableId, &Pathname[1]);
1275 ACPI_FREE (Pathname);
1277 break;
1279 default:
1280 /* Nothing to do for listing file */
1281 break;
1284 break;
1286 case AML_CLASS_EXECUTE:
1287 case AML_CLASS_CREATE:
1288 default:
1290 if ((Op->Asl.ParseOpcode == PARSEOP_BUFFER) &&
1291 (Op->Asl.CompileFlags & NODE_IS_RESOURCE_DESC))
1293 return;
1296 LsWriteSourceLines (Op->Asl.LineNumber, Op->Asl.LogicalLineNumber,
1297 FileId);
1298 break;
1300 case AML_CLASS_UNKNOWN:
1301 break;
1306 /*******************************************************************************
1308 * FUNCTION: LsDoHexOutput
1310 * PARAMETERS: None
1312 * RETURN: None.
1314 * DESCRIPTION: Create the hex output file.
1316 ******************************************************************************/
1318 void
1319 LsDoHexOutput (
1320 void)
1323 switch (Gbl_HexOutputFlag)
1325 case HEX_OUTPUT_C:
1327 LsDoHexOutputC ();
1328 break;
1330 case HEX_OUTPUT_ASM:
1332 LsDoHexOutputAsm ();
1333 break;
1335 default:
1336 /* No other output types supported */
1337 break;
1342 /*******************************************************************************
1344 * FUNCTION: LsDoHexOutputC
1346 * PARAMETERS: None
1348 * RETURN: None.
1350 * DESCRIPTION: Create the hex output file. This is the same data as the AML
1351 * output file, but formatted into hex/ascii bytes suitable for
1352 * inclusion into a C source file.
1354 ******************************************************************************/
1356 static void
1357 LsDoHexOutputC (
1358 void)
1360 UINT32 j;
1361 UINT8 FileByte[HEX_TABLE_LINE_SIZE];
1362 UINT8 Buffer[4];
1363 UINT32 Offset = 0;
1366 FlPrintFile (ASL_FILE_HEX_OUTPUT, " * C source code output\n *\n */\n");
1367 FlPrintFile (ASL_FILE_HEX_OUTPUT, "unsigned char AmlCode[] =\n{\n");
1369 /* Start at the beginning of the AML file */
1371 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1373 /* Process all AML bytes in the AML file */
1375 j = 0;
1376 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte[j], 1) == AE_OK)
1378 if (j == 0)
1380 FlPrintFile (ASL_FILE_HEX_OUTPUT, " ");
1383 /* Convert each AML byte to hex */
1385 UtConvertByteToHex (FileByte[j], Buffer);
1386 FlWriteFile (ASL_FILE_HEX_OUTPUT, Buffer, 4);
1387 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
1389 /* An occasional linefeed improves readability */
1391 Offset++;
1392 j++;
1394 if (j >= HEX_TABLE_LINE_SIZE)
1396 /* End of line, emit the ascii dump of the entire line */
1398 FlPrintFile (ASL_FILE_HEX_OUTPUT,
1399 " /* %8.8X", Offset - HEX_TABLE_LINE_SIZE);
1401 /* Write the ASCII character associated with each of the bytes */
1403 LsDumpAsciiInComment (ASL_FILE_HEX_OUTPUT,
1404 HEX_TABLE_LINE_SIZE, FileByte);
1405 FlPrintFile (ASL_FILE_HEX_OUTPUT, " */\n");
1407 /* Start new line */
1409 j = 0;
1413 FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n};\n");
1414 FlCloseFile (ASL_FILE_HEX_OUTPUT);
1418 /*******************************************************************************
1420 * FUNCTION: LsDoHexOutputAsm
1422 * PARAMETERS: None
1424 * RETURN: None.
1426 * DESCRIPTION: Create the hex output file. This is the same data as the AML
1427 * output file, but formatted into hex/ascii bytes suitable for
1428 * inclusion into a ASM source file.
1430 ******************************************************************************/
1432 static void
1433 LsDoHexOutputAsm (
1434 void)
1436 UINT32 j;
1437 UINT8 FileByte[HEX_TABLE_LINE_SIZE];
1438 UINT8 Buffer[4];
1439 UINT32 Offset = 0;
1440 BOOLEAN DoComma = FALSE;
1443 FlPrintFile (ASL_FILE_HEX_OUTPUT, "; Assembly code source output\n;\n");
1445 /* Start at the beginning of the AML file */
1447 FlSeekFile (ASL_FILE_AML_OUTPUT, 0);
1449 /* Process all AML bytes in the AML file */
1451 j = 0;
1452 while (FlReadFile (ASL_FILE_AML_OUTPUT, &FileByte[j], 1) == AE_OK)
1454 if (j == 0)
1456 FlPrintFile (ASL_FILE_HEX_OUTPUT, " db ");
1458 else if (DoComma)
1460 FlPrintFile (ASL_FILE_HEX_OUTPUT, ",");
1461 DoComma = FALSE;
1464 /* Convert each AML byte to hex */
1466 UtConvertByteToAsmHex (FileByte[j], Buffer);
1467 FlWriteFile (ASL_FILE_HEX_OUTPUT, Buffer, 4);
1469 /* An occasional linefeed improves readability */
1471 Offset++;
1472 j++;
1473 if (j >= HEX_TABLE_LINE_SIZE)
1475 FlPrintFile (ASL_FILE_HEX_OUTPUT,
1476 " ;%8.8X", Offset - HEX_TABLE_LINE_SIZE);
1478 /* Write the ASCII character associated with each of the bytes */
1480 LsDumpAscii (ASL_FILE_HEX_OUTPUT, HEX_TABLE_LINE_SIZE, FileByte);
1481 FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
1482 j = 0;
1484 else
1486 DoComma = TRUE;
1490 FlPrintFile (ASL_FILE_HEX_OUTPUT, "\n");
1491 FlCloseFile (ASL_FILE_HEX_OUTPUT);