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>
120 #include <contrib/dev/acpica/include/acapps.h>
122 #define _COMPONENT ACPI_COMPILER
123 ACPI_MODULE_NAME ("aslcompile")
125 /* Local prototypes */
132 FlConsumeAnsiComment (
133 ASL_FILE_INFO *FileInfo,
134 ASL_FILE_STATUS *Status);
137 FlConsumeNewComment (
138 ASL_FILE_INFO *FileInfo,
139 ASL_FILE_STATUS *Status);
142 /*******************************************************************************
144 * FUNCTION: AslCompilerSignon
146 * PARAMETERS: FileId - ID of the output file
150 * DESCRIPTION: Display compiler signon
152 ******************************************************************************/
162 /* Set line prefix depending on the destination file type */
166 case ASL_FILE_ASM_SOURCE_OUTPUT:
167 case ASL_FILE_ASM_INCLUDE_OUTPUT:
172 case ASL_FILE_HEX_OUTPUT:
174 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
178 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
179 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
181 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
186 case ASL_FILE_C_SOURCE_OUTPUT:
187 case ASL_FILE_C_INCLUDE_OUTPUT:
193 /* No other output types supported */
197 /* Running compiler or disassembler? */
201 UtilityName = AML_DISASSEMBLER_NAME;
205 UtilityName = ASL_COMPILER_NAME;
208 /* Compiler signon with copyright */
210 FlPrintFile (FileId, "%s\n", Prefix);
211 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
215 /*******************************************************************************
217 * FUNCTION: AslCompilerFileHeader
219 * PARAMETERS: FileId - ID of the output file
223 * DESCRIPTION: Header used at the beginning of output files
225 ******************************************************************************/
228 AslCompilerFileHeader (
236 /* Set line prefix depending on the destination file type */
240 case ASL_FILE_ASM_SOURCE_OUTPUT:
241 case ASL_FILE_ASM_INCLUDE_OUTPUT:
246 case ASL_FILE_HEX_OUTPUT:
248 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
252 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
253 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
259 case ASL_FILE_C_SOURCE_OUTPUT:
260 case ASL_FILE_C_INCLUDE_OUTPUT:
266 /* No other output types supported */
270 /* Compilation header with timestamp */
272 (void) time (&Aclock);
273 NewTime = localtime (&Aclock);
276 "%sCompilation of \"%s\" - %s%s\n",
277 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
282 case ASL_FILE_C_SOURCE_OUTPUT:
283 case ASL_FILE_C_INCLUDE_OUTPUT:
284 FlPrintFile (FileId, " */\n");
288 /* Nothing to do for other output types */
294 /*******************************************************************************
296 * FUNCTION: CmFlushSourceCode
302 * DESCRIPTION: Read in any remaining source code after the parse tree
303 * has been constructed.
305 ******************************************************************************/
314 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
316 InsertLineBuffer ((int) Buffer);
319 ResetCurrentLineBuffer ();
323 /*******************************************************************************
325 * FUNCTION: FlConsume*
327 * PARAMETERS: FileInfo - Points to an open input file
329 * RETURN: Number of lines consumed
331 * DESCRIPTION: Step over both types of comment during check for ascii chars
333 ******************************************************************************/
336 FlConsumeAnsiComment (
337 ASL_FILE_INFO *FileInfo,
338 ASL_FILE_STATUS *Status)
341 BOOLEAN ClosingComment = FALSE;
344 while (fread (&Byte, 1, 1, FileInfo->Handle))
346 /* Scan until comment close is found */
359 ClosingComment = FALSE;
362 else if (Byte == '*')
364 ClosingComment = TRUE;
367 /* Maintain line count */
380 FlConsumeNewComment (
381 ASL_FILE_INFO *FileInfo,
382 ASL_FILE_STATUS *Status)
387 while (fread (&Byte, 1, 1, FileInfo->Handle))
391 /* Comment ends at newline */
402 /*******************************************************************************
404 * FUNCTION: FlCheckForAscii
406 * PARAMETERS: FileInfo - Points to an open input file
410 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
411 * within comments. Note: does not handle nested comments and does
412 * not handle comment delimiters within string literals. However,
413 * on the rare chance this happens and an invalid character is
414 * missed, the parser will catch the error by failing in some
415 * spectactular manner.
417 ******************************************************************************/
421 ASL_FILE_INFO *FileInfo)
424 ACPI_SIZE BadBytes = 0;
425 BOOLEAN OpeningComment = FALSE;
426 ASL_FILE_STATUS Status;
432 /* Read the entire file */
434 while (fread (&Byte, 1, 1, FileInfo->Handle))
436 /* Ignore comment fields (allow non-ascii within) */
440 /* Check for second comment open delimiter */
444 FlConsumeAnsiComment (FileInfo, &Status);
449 FlConsumeNewComment (FileInfo, &Status);
454 OpeningComment = FALSE;
456 else if (Byte == '/')
458 OpeningComment = TRUE;
461 /* Check for an ASCII character */
463 if (!ACPI_IS_ASCII (Byte))
468 "Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
469 Byte, Status.Line, Status.Offset);
475 /* Update line counter */
477 else if (Byte == 0x0A)
485 /* Seek back to the beginning of the source file */
487 fseek (FileInfo->Handle, 0, SEEK_SET);
489 /* Were there any non-ASCII characters in the file? */
494 "%u non-ASCII characters found in input source text, could be a binary file\n",
496 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, FileInfo->Filename);
497 return (AE_BAD_CHARACTER);
506 /*******************************************************************************
508 * FUNCTION: CmDoCompile
512 * RETURN: Status (0 = OK)
514 * DESCRIPTION: This procedure performs the entire compile
516 ******************************************************************************/
527 FullCompile = UtBeginEvent ("*** Total Compile time ***");
528 Event = UtBeginEvent ("Open input and output files");
531 /* Build the parse tree */
533 Event = UtBeginEvent ("Parse source code and build parse tree");
537 /* Flush out any remaining source after parse tree is complete */
539 Event = UtBeginEvent ("Flush source input");
540 CmFlushSourceCode ();
542 /* Did the parse tree get successfully constructed? */
550 /* Optional parse tree dump, compiler debug output only */
554 OpcGetIntegerWidth (RootNode);
557 /* Pre-process parse tree for any operator transforms */
559 Event = UtBeginEvent ("Parse tree transforms");
560 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
561 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
562 TrAmlTransformWalk, NULL, NULL);
565 /* Generate AML opcodes corresponding to the parse tokens */
567 Event = UtBeginEvent ("Generate AML opcodes");
568 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
569 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
570 OpcAmlOpcodeWalk, NULL);
574 * Now that the input is parsed, we can open the AML output file.
575 * Note: by default, the name of this file comes from the table descriptor
576 * within the input file.
578 Event = UtBeginEvent ("Open AML output file");
579 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
580 if (ACPI_FAILURE (Status))
582 AePrintErrorLog (ASL_FILE_STDERR);
587 /* Interpret and generate all compile-time constants */
589 Event = UtBeginEvent ("Constant folding via AML interpreter");
590 DbgPrint (ASL_DEBUG_OUTPUT,
591 "\nInterpreting compile-time constant expressions\n\n");
592 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
593 OpcAmlConstantWalk, NULL, NULL);
596 /* Update AML opcodes if necessary, after constant folding */
598 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
599 DbgPrint (ASL_DEBUG_OUTPUT,
600 "\nUpdating AML opcodes after constant folding\n\n");
601 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
602 NULL, OpcAmlOpcodeUpdateWalk, NULL);
605 /* Calculate all AML package lengths */
607 Event = UtBeginEvent ("Generate AML package lengths");
608 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
609 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
610 LnPackageLengthWalk, NULL);
613 if (Gbl_ParseOnlyFlag)
615 AePrintErrorLog (ASL_FILE_STDOUT);
616 UtDisplaySummary (ASL_FILE_STDOUT);
619 /* Print error summary to the debug file */
621 AePrintErrorLog (ASL_FILE_STDERR);
622 UtDisplaySummary (ASL_FILE_STDERR);
628 * Create an internal namespace and use it as a symbol table
631 /* Namespace loading */
633 Event = UtBeginEvent ("Create ACPI Namespace");
634 Status = LdLoadNamespace (RootNode);
636 if (ACPI_FAILURE (Status))
641 /* Namespace cross-reference */
643 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
644 Status = LkCrossReferenceNamespace ();
645 if (ACPI_FAILURE (Status))
650 /* Namespace - Check for non-referenced objects */
652 LkFindUnreferencedObjects ();
653 UtEndEvent (AslGbl_NamespaceEvent);
656 * Semantic analysis. This can happen only after the
657 * namespace has been loaded and cross-referenced.
659 * part one - check control methods
661 Event = UtBeginEvent ("Analyze control method return types");
662 AnalysisWalkInfo.MethodStack = NULL;
664 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
665 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
666 AnMethodAnalysisWalkBegin,
667 AnMethodAnalysisWalkEnd, &AnalysisWalkInfo);
670 /* Semantic error checking part two - typing of method returns */
672 Event = UtBeginEvent ("Determine object types returned by methods");
673 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
674 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
675 AnMethodTypingWalkBegin,
676 AnMethodTypingWalkEnd, NULL);
679 /* Semantic error checking part three - operand type checking */
681 Event = UtBeginEvent ("Analyze AML operand types");
682 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
683 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
684 AnOperandTypecheckWalkBegin,
685 AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
688 /* Semantic error checking part four - other miscellaneous checks */
690 Event = UtBeginEvent ("Miscellaneous analysis");
691 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
692 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
693 AnOtherSemanticAnalysisWalkBegin,
694 AnOtherSemanticAnalysisWalkEnd, &AnalysisWalkInfo);
697 /* Calculate all AML package lengths */
699 Event = UtBeginEvent ("Finish AML package length generation");
700 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
701 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
702 LnInitLengthsWalk, NULL);
703 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
704 LnPackageLengthWalk, NULL);
707 /* Code generation - emit the AML */
709 Event = UtBeginEvent ("Generate AML code and write output files");
710 CgGenerateAmlOutput ();
713 Event = UtBeginEvent ("Write optional output files");
717 UtEndEvent (FullCompile);
723 /*******************************************************************************
725 * FUNCTION: CmDoOutputFiles
731 * DESCRIPTION: Create all "listing" type files
733 ******************************************************************************/
740 /* Create listings and hex files */
745 /* Dump the namespace to the .nsp file if requested */
747 (void) LsDisplayNamespace ();
751 /*******************************************************************************
753 * FUNCTION: CmDumpEvent
755 * PARAMETERS: Event - A compiler event struct
759 * DESCRIPTION: Dump a compiler event struct
761 ******************************************************************************/
765 ASL_EVENT_INFO *Event)
776 /* Delta will be in 100-nanosecond units */
778 Delta = (UINT32) (Event->EndTime - Event->StartTime);
781 MSec = Delta / 10000;
783 /* Round milliseconds up */
785 if ((USec - (MSec * 1000)) >= 500)
790 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
791 USec, MSec, Event->EventName);
795 /*******************************************************************************
797 * FUNCTION: CmCleanupAndExit
803 * DESCRIPTION: Close all open files and exit the compiler
805 ******************************************************************************/
814 AePrintErrorLog (ASL_FILE_STDOUT);
817 /* Print error summary to the debug file */
819 AePrintErrorLog (ASL_FILE_STDERR);
822 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
823 for (i = 0; i < AslGbl_NextEvent; i++)
825 CmDumpEvent (&AslGbl_Events[i]);
828 if (Gbl_CompileTimesFlag)
830 printf ("\nElapsed time for major events\n\n");
831 for (i = 0; i < AslGbl_NextEvent; i++)
833 CmDumpEvent (&AslGbl_Events[i]);
836 printf ("\nMiscellaneous compile statistics\n\n");
837 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
838 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
839 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
840 printf ("%11u : %s\n", TotalMethods, "Control methods");
841 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
842 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
843 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
847 if (Gbl_NsLookupCount)
849 DbgPrint (ASL_DEBUG_OUTPUT,
850 "\n\nMiscellaneous compile statistics\n\n");
852 DbgPrint (ASL_DEBUG_OUTPUT,
853 "%32s : %u\n", "Total Namespace searches",
856 DbgPrint (ASL_DEBUG_OUTPUT,
857 "%32s : %u usec\n", "Time per search", ((UINT32)
858 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
859 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
863 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
865 printf ("\nMaximum error count (%u) exceeded\n",
866 ASL_MAX_ERROR_COUNT);
869 UtDisplaySummary (ASL_FILE_STDOUT);
871 /* Close all open files */
873 for (i = 2; i < ASL_MAX_FILE_TYPE; i++)
878 /* Delete AML file if there are errors */
880 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors) &&
881 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
883 if (remove (Gbl_Files[ASL_FILE_AML_OUTPUT].Filename))
886 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename);
887 perror ("Could not delete AML file");
892 * Delete intermediate ("combined") source file (if -ls flag not set)
893 * This file is created during normal ASL/AML compiles. It is not
894 * created by the data table compiler.
896 * If the -ls flag is set, then the .SRC file should not be deleted.
897 * In this case, Gbl_SourceOutputFlag is set to TRUE.
899 * Note: Handles are cleared by FlCloseFile above, so we look at the
900 * filename instead, to determine if the .SRC file was actually
903 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
905 if (!Gbl_SourceOutputFlag && Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename)
907 if (remove (Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename))
910 Gbl_Files[ASL_FILE_SOURCE_OUTPUT].Filename);
911 perror ("Could not delete SRC file");