1 /******************************************************************************
3 * Module Name: aslcompile - top level compile module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
44 #include <contrib/dev/acpica/compiler/aslcompiler.h>
45 #include <contrib/dev/acpica/compiler/dtcompiler.h>
46 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/acapps.h>
52 #define _COMPONENT ACPI_COMPILER
53 ACPI_MODULE_NAME ("aslcompile")
57 * External is here in case the parser emits the same external in the
58 * generated header. (Newer versions of Bison)
64 /* Local prototypes */
75 /*******************************************************************************
77 * FUNCTION: CmDoCompile
81 * RETURN: Status (0 = OK)
83 * DESCRIPTION: This procedure performs the entire compile
85 ******************************************************************************/
96 FullCompile = UtBeginEvent ("*** Total Compile time ***");
97 Event = UtBeginEvent ("Open input and output files");
100 Event = UtBeginEvent ("Preprocess input file");
101 if (Gbl_PreprocessFlag)
106 if (Gbl_PreprocessOnly)
115 /* Build the parse tree */
117 Event = UtBeginEvent ("Parse source code and build parse tree");
121 /* Check for parse errors */
123 Status = AslCheckForErrorExit ();
124 if (ACPI_FAILURE (Status))
126 fprintf (stderr, "Compiler aborting due to parser-detected syntax error(s)\n");
131 /* Did the parse tree get successfully constructed? */
136 * If there are no errors, then we have some sort of
139 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
140 NULL, "- Could not resolve parse tree root node");
145 /* Flush out any remaining source after parse tree is complete */
147 Event = UtBeginEvent ("Flush source input");
148 CmFlushSourceCode ();
150 /* Optional parse tree dump, compiler debug output only */
154 OpcGetIntegerWidth (RootNode);
157 /* Pre-process parse tree for any operator transforms */
159 Event = UtBeginEvent ("Parse tree transforms");
160 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
161 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
162 TrAmlTransformWalk, NULL, NULL);
165 /* Generate AML opcodes corresponding to the parse tokens */
167 Event = UtBeginEvent ("Generate AML opcodes");
168 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
169 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
170 OpcAmlOpcodeWalk, NULL);
174 * Now that the input is parsed, we can open the AML output file.
175 * Note: by default, the name of this file comes from the table descriptor
176 * within the input file.
178 Event = UtBeginEvent ("Open AML output file");
179 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
181 if (ACPI_FAILURE (Status))
183 AePrintErrorLog (ASL_FILE_STDERR);
187 /* Interpret and generate all compile-time constants */
189 Event = UtBeginEvent ("Constant folding via AML interpreter");
190 DbgPrint (ASL_DEBUG_OUTPUT,
191 "\nInterpreting compile-time constant expressions\n\n");
192 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
193 OpcAmlConstantWalk, NULL, NULL);
196 /* Update AML opcodes if necessary, after constant folding */
198 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
199 DbgPrint (ASL_DEBUG_OUTPUT,
200 "\nUpdating AML opcodes after constant folding\n\n");
201 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
202 NULL, OpcAmlOpcodeUpdateWalk, NULL);
205 /* Calculate all AML package lengths */
207 Event = UtBeginEvent ("Generate AML package lengths");
208 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
209 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
210 LnPackageLengthWalk, NULL);
213 if (Gbl_ParseOnlyFlag)
215 AePrintErrorLog (ASL_FILE_STDERR);
216 UtDisplaySummary (ASL_FILE_STDERR);
219 /* Print error summary to the stdout also */
221 AePrintErrorLog (ASL_FILE_STDOUT);
222 UtDisplaySummary (ASL_FILE_STDOUT);
224 UtEndEvent (FullCompile);
229 * Create an internal namespace and use it as a symbol table
232 /* Namespace loading */
234 Event = UtBeginEvent ("Create ACPI Namespace");
235 Status = LdLoadNamespace (RootNode);
237 if (ACPI_FAILURE (Status))
242 /* Namespace cross-reference */
244 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
245 Status = XfCrossReferenceNamespace ();
246 if (ACPI_FAILURE (Status))
251 /* Namespace - Check for non-referenced objects */
253 LkFindUnreferencedObjects ();
254 UtEndEvent (AslGbl_NamespaceEvent);
257 * Semantic analysis. This can happen only after the
258 * namespace has been loaded and cross-referenced.
260 * part one - check control methods
262 Event = UtBeginEvent ("Analyze control method return types");
263 AnalysisWalkInfo.MethodStack = NULL;
265 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
266 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
267 MtMethodAnalysisWalkBegin,
268 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
271 /* Semantic error checking part two - typing of method returns */
273 Event = UtBeginEvent ("Determine object types returned by methods");
274 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
275 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
276 NULL, AnMethodTypingWalkEnd, NULL);
279 /* Semantic error checking part three - operand type checking */
281 Event = UtBeginEvent ("Analyze AML operand types");
282 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
283 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
284 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
287 /* Semantic error checking part four - other miscellaneous checks */
289 Event = UtBeginEvent ("Miscellaneous analysis");
290 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
291 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
292 AnOtherSemanticAnalysisWalkBegin,
293 NULL, &AnalysisWalkInfo);
296 /* Calculate all AML package lengths */
298 Event = UtBeginEvent ("Finish AML package length generation");
299 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
300 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
301 LnInitLengthsWalk, NULL);
302 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
303 LnPackageLengthWalk, NULL);
306 /* Code generation - emit the AML */
308 Event = UtBeginEvent ("Generate AML code and write output files");
309 CgGenerateAmlOutput ();
312 Event = UtBeginEvent ("Write optional output files");
316 UtEndEvent (FullCompile);
321 UtEndEvent (FullCompile);
327 /*******************************************************************************
329 * FUNCTION: AslCompilerSignon
331 * PARAMETERS: FileId - ID of the output file
335 * DESCRIPTION: Display compiler signon
337 ******************************************************************************/
347 /* Set line prefix depending on the destination file type */
351 case ASL_FILE_ASM_SOURCE_OUTPUT:
352 case ASL_FILE_ASM_INCLUDE_OUTPUT:
357 case ASL_FILE_HEX_OUTPUT:
359 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
363 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
364 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
366 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
371 case ASL_FILE_C_SOURCE_OUTPUT:
372 case ASL_FILE_C_OFFSET_OUTPUT:
373 case ASL_FILE_C_INCLUDE_OUTPUT:
380 /* No other output types supported */
385 /* Running compiler or disassembler? */
389 UtilityName = AML_DISASSEMBLER_NAME;
393 UtilityName = ASL_COMPILER_NAME;
396 /* Compiler signon with copyright */
398 FlPrintFile (FileId, "%s\n", Prefix);
399 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
403 /*******************************************************************************
405 * FUNCTION: AslCompilerFileHeader
407 * PARAMETERS: FileId - ID of the output file
411 * DESCRIPTION: Header used at the beginning of output files
413 ******************************************************************************/
416 AslCompilerFileHeader (
424 /* Set line prefix depending on the destination file type */
428 case ASL_FILE_ASM_SOURCE_OUTPUT:
429 case ASL_FILE_ASM_INCLUDE_OUTPUT:
434 case ASL_FILE_HEX_OUTPUT:
436 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
440 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
441 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
447 case ASL_FILE_C_SOURCE_OUTPUT:
448 case ASL_FILE_C_OFFSET_OUTPUT:
449 case ASL_FILE_C_INCLUDE_OUTPUT:
456 /* No other output types supported */
461 /* Compilation header with timestamp */
463 (void) time (&Aclock);
464 NewTime = localtime (&Aclock);
467 "%sCompilation of \"%s\" - %s%s\n",
468 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
473 case ASL_FILE_C_SOURCE_OUTPUT:
474 case ASL_FILE_C_OFFSET_OUTPUT:
475 case ASL_FILE_C_INCLUDE_OUTPUT:
477 FlPrintFile (FileId, " */\n");
482 /* Nothing to do for other output types */
489 /*******************************************************************************
491 * FUNCTION: CmFlushSourceCode
497 * DESCRIPTION: Read in any remaining source code after the parse tree
498 * has been constructed.
500 ******************************************************************************/
509 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
511 AslInsertLineBuffer ((int) Buffer);
514 AslResetCurrentLineBuffer ();
518 /*******************************************************************************
520 * FUNCTION: CmDoOutputFiles
526 * DESCRIPTION: Create all "listing" type files
528 ******************************************************************************/
535 /* Create listings and hex files */
540 /* Dump the namespace to the .nsp file if requested */
542 (void) NsDisplayNamespace ();
544 /* Dump the device mapping file */
546 MpEmitMappingInfo ();
550 /*******************************************************************************
552 * FUNCTION: CmDumpAllEvents
558 * DESCRIPTION: Dump all compiler events
560 ******************************************************************************/
566 ASL_EVENT_INFO *Event;
573 Event = AslGbl_Events;
575 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
576 if (Gbl_CompileTimesFlag)
578 printf ("\nElapsed time for major events\n\n");
581 for (i = 0; i < AslGbl_NextEvent; i++)
585 /* Delta will be in 100-nanosecond units */
587 Delta = (UINT32) (Event->EndTime - Event->StartTime);
589 USec = Delta / ACPI_100NSEC_PER_USEC;
590 MSec = Delta / ACPI_100NSEC_PER_MSEC;
592 /* Round milliseconds up */
594 if ((USec - (MSec * ACPI_USEC_PER_MSEC)) >= 500)
599 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
600 USec, MSec, Event->EventName);
602 if (Gbl_CompileTimesFlag)
604 printf ("%8u usec %8u msec - %s\n",
605 USec, MSec, Event->EventName);
614 /*******************************************************************************
616 * FUNCTION: CmCleanupAndExit
622 * DESCRIPTION: Close all open files and exit the compiler
624 ******************************************************************************/
631 BOOLEAN DeleteAmlFile = FALSE;
634 AePrintErrorLog (ASL_FILE_STDERR);
637 /* Print error summary to stdout also */
639 AePrintErrorLog (ASL_FILE_STDOUT);
642 /* Emit compile times if enabled */
646 if (Gbl_CompileTimesFlag)
648 printf ("\nMiscellaneous compile statistics\n\n");
649 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
650 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
651 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
652 printf ("%11u : %s\n", TotalMethods, "Control methods");
653 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
654 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
655 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
659 if (Gbl_NsLookupCount)
661 DbgPrint (ASL_DEBUG_OUTPUT,
662 "\n\nMiscellaneous compile statistics\n\n");
664 DbgPrint (ASL_DEBUG_OUTPUT,
665 "%32s : %u\n", "Total Namespace searches",
668 DbgPrint (ASL_DEBUG_OUTPUT,
669 "%32s : %u usec\n", "Time per search", ((UINT32)
670 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
671 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
675 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
677 printf ("\nMaximum error count (%u) exceeded\n",
678 ASL_MAX_ERROR_COUNT);
681 UtDisplaySummary (ASL_FILE_STDOUT);
684 * We will delete the AML file if there are errors and the
685 * force AML output option has not been used.
687 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
688 (!Gbl_IgnoreErrors) &&
689 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
691 DeleteAmlFile = TRUE;
694 /* Close all open files */
697 * Take care with the preprocessor file (.i), it might be the same
698 * as the "input" file, depending on where the compiler has terminated
699 * or aborted. Prevent attempt to close the same file twice in
702 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
703 Gbl_Files[ASL_FILE_INPUT].Handle)
705 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
708 /* Close the standard I/O files */
710 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
715 /* Delete AML file if there are errors */
719 FlDeleteFile (ASL_FILE_AML_OUTPUT);
722 /* Delete the preprocessor output file (.i) unless -li flag is set */
724 if (!Gbl_PreprocessorOutputFlag &&
727 FlDeleteFile (ASL_FILE_PREPROCESSOR);
731 * Delete intermediate ("combined") source file (if -ls flag not set)
732 * This file is created during normal ASL/AML compiles. It is not
733 * created by the data table compiler.
735 * If the -ls flag is set, then the .SRC file should not be deleted.
736 * In this case, Gbl_SourceOutputFlag is set to TRUE.
738 * Note: Handles are cleared by FlCloseFile above, so we look at the
739 * filename instead, to determine if the .SRC file was actually
742 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
744 if (!Gbl_SourceOutputFlag)
746 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
749 /* Final cleanup after compiling one file */
755 /*******************************************************************************
757 * FUNCTION: CmDeleteCaches
763 * DESCRIPTION: Delete all local cache buffer blocks
765 ******************************************************************************/
772 ASL_CACHE_INFO *Next;
778 while (Gbl_ParseOpCacheList)
780 Next = Gbl_ParseOpCacheList->Next;
781 ACPI_FREE (Gbl_ParseOpCacheList);
782 Gbl_ParseOpCacheList = Next;
786 DbgPrint (ASL_DEBUG_OUTPUT,
787 "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
788 Gbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
789 (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
791 Gbl_ParseOpCount = 0;
792 Gbl_ParseOpCacheNext = NULL;
793 Gbl_ParseOpCacheLast = NULL;
796 /* Generic string cache */
799 while (Gbl_StringCacheList)
801 Next = Gbl_StringCacheList->Next;
802 ACPI_FREE (Gbl_StringCacheList);
803 Gbl_StringCacheList = Next;
807 DbgPrint (ASL_DEBUG_OUTPUT,
808 "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
809 Gbl_StringCount, Gbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
813 Gbl_StringCacheNext = NULL;
814 Gbl_StringCacheLast = NULL;