2 /******************************************************************************
4 * Module Name: aslcompile - top level compile module
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
119 #include <contrib/dev/acpica/compiler/aslcompiler.h>
121 #define _COMPONENT ACPI_COMPILER
122 ACPI_MODULE_NAME ("aslcompile")
124 /* Local prototypes */
132 ASL_FILE_INFO *FileInfo);
135 FlConsumeAnsiComment (
136 ASL_FILE_INFO *FileInfo,
137 ASL_FILE_STATUS *Status);
140 FlConsumeNewComment (
141 ASL_FILE_INFO *FileInfo,
142 ASL_FILE_STATUS *Status);
145 /*******************************************************************************
147 * FUNCTION: AslCompilerSignon
149 * PARAMETERS: FileId - ID of the output file
153 * DESCRIPTION: Display compiler signon
155 ******************************************************************************/
164 /* Set line prefix depending on the destination file type */
168 case ASL_FILE_ASM_SOURCE_OUTPUT:
169 case ASL_FILE_ASM_INCLUDE_OUTPUT:
174 case ASL_FILE_HEX_OUTPUT:
176 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
180 else if (Gbl_HexOutputFlag == HEX_OUTPUT_C)
182 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
187 case ASL_FILE_C_SOURCE_OUTPUT:
188 case ASL_FILE_C_INCLUDE_OUTPUT:
194 /* No other output types supported */
199 * Compiler signon with copyright
207 /* Running compiler or disassembler? */
212 "%s", DisassemblerId);
220 /* Version, copyright, compliance */
223 " version %X\n%s%s\n%s%s\n%s\n",
224 (UINT32) ACPI_CA_VERSION,
225 Prefix, CompilerCopyright,
226 Prefix, CompilerCompliance,
231 /*******************************************************************************
233 * FUNCTION: AslCompilerFileHeader
235 * PARAMETERS: FileId - ID of the output file
239 * DESCRIPTION: Header used at the beginning of output files
241 ******************************************************************************/
244 AslCompilerFileHeader (
252 /* Set line prefix depending on the destination file type */
256 case ASL_FILE_ASM_SOURCE_OUTPUT:
257 case ASL_FILE_ASM_INCLUDE_OUTPUT:
262 case ASL_FILE_HEX_OUTPUT:
264 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
268 else if (Gbl_HexOutputFlag == HEX_OUTPUT_C)
274 case ASL_FILE_C_SOURCE_OUTPUT:
275 case ASL_FILE_C_INCLUDE_OUTPUT:
281 /* No other output types supported */
285 /* Compilation header with timestamp */
287 (void) time (&Aclock);
288 NewTime = localtime (&Aclock);
291 "%sCompilation of \"%s\" - %s%s\n",
292 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
297 case ASL_FILE_C_SOURCE_OUTPUT:
298 case ASL_FILE_C_INCLUDE_OUTPUT:
299 FlPrintFile (FileId, " */\n");
303 /* Nothing to do for other output types */
309 /*******************************************************************************
311 * FUNCTION: CmFlushSourceCode
317 * DESCRIPTION: Read in any remaining source code after the parse tree
318 * has been constructed.
320 ******************************************************************************/
329 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
331 InsertLineBuffer ((int) Buffer);
334 ResetCurrentLineBuffer ();
338 /*******************************************************************************
340 * FUNCTION: FlConsume*
342 * PARAMETERS: FileInfo - Points to an open input file
344 * RETURN: Number of lines consumed
346 * DESCRIPTION: Step over both types of comment during check for ascii chars
348 ******************************************************************************/
351 FlConsumeAnsiComment (
352 ASL_FILE_INFO *FileInfo,
353 ASL_FILE_STATUS *Status)
356 BOOLEAN ClosingComment = FALSE;
359 while (fread (&Byte, 1, 1, FileInfo->Handle))
361 /* Scan until comment close is found */
374 ClosingComment = FALSE;
377 else if (Byte == '*')
379 ClosingComment = TRUE;
382 /* Maintain line count */
395 FlConsumeNewComment (
396 ASL_FILE_INFO *FileInfo,
397 ASL_FILE_STATUS *Status)
402 while (fread (&Byte, 1, 1, FileInfo->Handle))
406 /* Comment ends at newline */
417 /*******************************************************************************
419 * FUNCTION: FlCheckForAscii
421 * PARAMETERS: FileInfo - Points to an open input file
425 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
426 * within comments. Note: does not handle nested comments and does
427 * not handle comment delimiters within string literals. However,
428 * on the rare chance this happens and an invalid character is
429 * missed, the parser will catch the error by failing in some
430 * spectactular manner.
432 ******************************************************************************/
436 ASL_FILE_INFO *FileInfo)
439 ACPI_SIZE BadBytes = 0;
440 BOOLEAN OpeningComment = FALSE;
441 ASL_FILE_STATUS Status;
447 /* Read the entire file */
449 while (fread (&Byte, 1, 1, FileInfo->Handle))
451 /* Ignore comment fields (allow non-ascii within) */
455 /* Check for second comment open delimiter */
459 FlConsumeAnsiComment (FileInfo, &Status);
464 FlConsumeNewComment (FileInfo, &Status);
469 OpeningComment = FALSE;
471 else if (Byte == '/')
473 OpeningComment = TRUE;
476 /* Check for an ASCII character */
478 if (!ACPI_IS_ASCII (Byte))
483 "Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
484 Byte, Status.Line, Status.Offset);
490 /* Update line counter */
492 else if (Byte == 0x0A)
500 /* Seek back to the beginning of the source file */
502 fseek (FileInfo->Handle, 0, SEEK_SET);
504 /* Were there any non-ASCII characters in the file? */
509 "%u non-ASCII characters found in input source text, could be a binary file\n",
511 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, FileInfo->Filename);
512 return (AE_BAD_CHARACTER);
521 /*******************************************************************************
523 * FUNCTION: CmDoCompile
527 * RETURN: Status (0 = OK)
529 * DESCRIPTION: This procedure performs the entire compile
531 ******************************************************************************/
542 FullCompile = UtBeginEvent ("*** Total Compile time ***");
543 Event = UtBeginEvent ("Open input and output files");
545 /* Open the required input and output files */
547 Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
548 if (ACPI_FAILURE (Status))
550 AePrintErrorLog (ASL_FILE_STDERR);
554 /* Check for 100% ASCII source file (comments are ignored) */
556 Status = FlCheckForAscii (&Gbl_Files[ASL_FILE_INPUT]);
557 if (ACPI_FAILURE (Status))
559 AePrintErrorLog (ASL_FILE_STDERR);
563 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
564 if (ACPI_FAILURE (Status))
566 AePrintErrorLog (ASL_FILE_STDERR);
571 /* Build the parse tree */
573 Event = UtBeginEvent ("Parse source code and build parse tree");
577 /* Flush out any remaining source after parse tree is complete */
579 Event = UtBeginEvent ("Flush source input");
580 CmFlushSourceCode ();
582 /* Did the parse tree get successfully constructed? */
590 /* Optional parse tree dump, compiler debug output only */
594 OpcGetIntegerWidth (RootNode);
597 /* Pre-process parse tree for any operator transforms */
599 Event = UtBeginEvent ("Parse tree transforms");
600 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
601 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
602 TrAmlTransformWalk, NULL, NULL);
605 /* Generate AML opcodes corresponding to the parse tokens */
607 Event = UtBeginEvent ("Generate AML opcodes");
608 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
609 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
610 OpcAmlOpcodeWalk, NULL);
614 * Now that the input is parsed, we can open the AML output file.
615 * Note: by default, the name of this file comes from the table descriptor
616 * within the input file.
618 Event = UtBeginEvent ("Open AML output file");
619 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
620 if (ACPI_FAILURE (Status))
622 AePrintErrorLog (ASL_FILE_STDERR);
627 /* Interpret and generate all compile-time constants */
629 Event = UtBeginEvent ("Constant folding via AML interpreter");
630 DbgPrint (ASL_DEBUG_OUTPUT,
631 "\nInterpreting compile-time constant expressions\n\n");
632 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
633 OpcAmlConstantWalk, NULL, NULL);
636 /* Update AML opcodes if necessary, after constant folding */
638 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
639 DbgPrint (ASL_DEBUG_OUTPUT,
640 "\nUpdating AML opcodes after constant folding\n\n");
641 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
642 NULL, OpcAmlOpcodeUpdateWalk, NULL);
645 /* Calculate all AML package lengths */
647 Event = UtBeginEvent ("Generate AML package lengths");
648 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
649 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
650 LnPackageLengthWalk, NULL);
653 if (Gbl_ParseOnlyFlag)
655 AePrintErrorLog (ASL_FILE_STDOUT);
656 UtDisplaySummary (ASL_FILE_STDOUT);
659 /* Print error summary to the debug file */
661 AePrintErrorLog (ASL_FILE_STDERR);
662 UtDisplaySummary (ASL_FILE_STDERR);
668 * Create an internal namespace and use it as a symbol table
671 /* Namespace loading */
673 Event = UtBeginEvent ("Create ACPI Namespace");
674 Status = LdLoadNamespace (RootNode);
676 if (ACPI_FAILURE (Status))
681 /* Namespace cross-reference */
683 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
684 Status = LkCrossReferenceNamespace ();
685 if (ACPI_FAILURE (Status))
690 /* Namespace - Check for non-referenced objects */
692 LkFindUnreferencedObjects ();
693 UtEndEvent (AslGbl_NamespaceEvent);
696 * Semantic analysis. This can happen only after the
697 * namespace has been loaded and cross-referenced.
699 * part one - check control methods
701 Event = UtBeginEvent ("Analyze control method return types");
702 AnalysisWalkInfo.MethodStack = NULL;
704 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
705 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
706 AnMethodAnalysisWalkBegin,
707 AnMethodAnalysisWalkEnd, &AnalysisWalkInfo);
710 /* Semantic error checking part two - typing of method returns */
712 Event = UtBeginEvent ("Determine object types returned by methods");
713 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
714 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
715 AnMethodTypingWalkBegin,
716 AnMethodTypingWalkEnd, NULL);
719 /* Semantic error checking part three - operand type checking */
721 Event = UtBeginEvent ("Analyze AML operand types");
722 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
723 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
724 AnOperandTypecheckWalkBegin,
725 AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
728 /* Semantic error checking part four - other miscellaneous checks */
730 Event = UtBeginEvent ("Miscellaneous analysis");
731 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
732 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
733 AnOtherSemanticAnalysisWalkBegin,
734 AnOtherSemanticAnalysisWalkEnd, &AnalysisWalkInfo);
737 /* Calculate all AML package lengths */
739 Event = UtBeginEvent ("Finish AML package length generation");
740 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
741 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
742 LnInitLengthsWalk, NULL);
743 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
744 LnPackageLengthWalk, NULL);
747 /* Code generation - emit the AML */
749 Event = UtBeginEvent ("Generate AML code and write output files");
750 CgGenerateAmlOutput ();
753 Event = UtBeginEvent ("Write optional output files");
757 UtEndEvent (FullCompile);
763 /*******************************************************************************
765 * FUNCTION: CmDoOutputFiles
771 * DESCRIPTION: Create all "listing" type files
773 ******************************************************************************/
780 /* Create listings and hex files */
785 /* Dump the namespace to the .nsp file if requested */
787 (void) LsDisplayNamespace ();
791 /*******************************************************************************
793 * FUNCTION: CmDumpEvent
795 * PARAMETERS: Event - A compiler event struct
799 * DESCRIPTION: Dump a compiler event struct
801 ******************************************************************************/
805 ASL_EVENT_INFO *Event)
816 /* Delta will be in 100-nanosecond units */
818 Delta = (UINT32) (Event->EndTime - Event->StartTime);
821 MSec = Delta / 10000;
823 /* Round milliseconds up */
825 if ((USec - (MSec * 1000)) >= 500)
830 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
831 USec, MSec, Event->EventName);
835 /*******************************************************************************
837 * FUNCTION: CmCleanupAndExit
843 * DESCRIPTION: Close all open files and exit the compiler
845 ******************************************************************************/
854 AePrintErrorLog (ASL_FILE_STDOUT);
857 /* Print error summary to the debug file */
859 AePrintErrorLog (ASL_FILE_STDERR);
862 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
863 for (i = 0; i < AslGbl_NextEvent; i++)
865 CmDumpEvent (&AslGbl_Events[i]);
868 if (Gbl_CompileTimesFlag)
870 printf ("\nElapsed time for major events\n\n");
871 for (i = 0; i < AslGbl_NextEvent; i++)
873 CmDumpEvent (&AslGbl_Events[i]);
876 printf ("\nMiscellaneous compile statistics\n\n");
877 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
878 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
879 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
880 printf ("%11u : %s\n", TotalMethods, "Control methods");
881 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
882 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
883 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
887 if (Gbl_NsLookupCount)
889 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nMiscellaneous compile statistics\n\n");
890 DbgPrint (ASL_DEBUG_OUTPUT, "%32s : %d\n", "Total Namespace searches",
892 DbgPrint (ASL_DEBUG_OUTPUT, "%32s : %d usec\n", "Time per search",
893 ((UINT32) (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
894 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) /
895 10) / Gbl_NsLookupCount);
899 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
901 printf ("\nMaximum error count (%d) exceeded\n", ASL_MAX_ERROR_COUNT);
904 UtDisplaySummary (ASL_FILE_STDOUT);
906 /* Close all open files */
908 for (i = 2; i < ASL_MAX_FILE_TYPE; i++)
913 /* Delete AML file if there are errors */
915 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors))
917 remove (Gbl_Files[ASL_FILE_AML_OUTPUT].Filename);
921 * Delete intermediate ("combined") source file (if -ls flag not set)
923 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
925 if (!Gbl_SourceOutputFlag)
927 if (remove (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename))
929 printf ("Could not remove SRC file, %s\n",
930 Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename);