1 /******************************************************************************
3 * Module Name: aslcompile - top level compile module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2017, 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)
103 /* Enter compiler name as a #define */
105 PrAddDefine (ASL_DEFINE, "", FALSE);
110 Gbl_CurrentLineNumber = 1;
111 Gbl_LogicalLineNumber = 1;
113 if (Gbl_PreprocessOnly)
123 /* Build the parse tree */
125 Event = UtBeginEvent ("Parse source code and build parse tree");
129 /* Check for parser-detected syntax errors */
134 "Compiler aborting due to parser-detected syntax error(s)\n");
139 /* Did the parse tree get successfully constructed? */
141 if (!Gbl_ParseTreeRoot)
144 * If there are no errors, then we have some sort of
147 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
148 NULL, "- Could not resolve parse tree root node");
153 /* Flush out any remaining source after parse tree is complete */
155 Event = UtBeginEvent ("Flush source input");
156 CmFlushSourceCode ();
158 /* Prune the parse tree if requested (debug purposes only) */
160 if (Gbl_PruneParseTree)
162 AslPruneParseTree (Gbl_PruneDepth, Gbl_PruneType);
165 /* Optional parse tree dump, compiler debug output only */
169 OpcGetIntegerWidth (Gbl_ParseTreeRoot->Asl.Child);
172 /* Pre-process parse tree for any operator transforms */
174 Event = UtBeginEvent ("Parse tree transforms");
175 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
176 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_TWICE,
177 TrAmlTransformWalkBegin, TrAmlTransformWalkEnd, NULL);
180 /* Generate AML opcodes corresponding to the parse tokens */
182 Event = UtBeginEvent ("Generate AML opcodes");
183 DbgPrint (ASL_DEBUG_OUTPUT, "Generating AML opcodes\n\n");
184 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
185 OpcAmlOpcodeWalk, NULL);
189 * Now that the input is parsed, we can open the AML output file.
190 * Note: by default, the name of this file comes from the table
191 * descriptor within the input file.
193 Event = UtBeginEvent ("Open AML output file");
194 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
196 if (ACPI_FAILURE (Status))
198 AePrintErrorLog (ASL_FILE_STDERR);
202 /* Interpret and generate all compile-time constants */
204 Event = UtBeginEvent ("Constant folding via AML interpreter");
205 DbgPrint (ASL_DEBUG_OUTPUT,
206 "Interpreting compile-time constant expressions\n\n");
208 if (Gbl_FoldConstants)
210 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
211 NULL, OpcAmlConstantWalk, NULL);
215 DbgPrint (ASL_PARSE_OUTPUT, " Optional folding disabled\n");
219 /* Update AML opcodes if necessary, after constant folding */
221 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
222 DbgPrint (ASL_DEBUG_OUTPUT,
223 "Updating AML opcodes after constant folding\n\n");
224 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
225 NULL, OpcAmlOpcodeUpdateWalk, NULL);
228 /* Calculate all AML package lengths */
230 Event = UtBeginEvent ("Generate AML package lengths");
231 DbgPrint (ASL_DEBUG_OUTPUT, "Generating Package lengths\n\n");
232 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
233 LnPackageLengthWalk, NULL);
236 if (Gbl_ParseOnlyFlag)
238 AePrintErrorLog (ASL_FILE_STDERR);
239 UtDisplaySummary (ASL_FILE_STDERR);
242 /* Print error summary to the stdout also */
244 AePrintErrorLog (ASL_FILE_STDOUT);
245 UtDisplaySummary (ASL_FILE_STDOUT);
247 UtEndEvent (FullCompile);
252 * Create an internal namespace and use it as a symbol table
255 /* Namespace loading */
257 Event = UtBeginEvent ("Create ACPI Namespace");
258 DbgPrint (ASL_DEBUG_OUTPUT, "Creating ACPI Namespace\n\n");
259 Status = LdLoadNamespace (Gbl_ParseTreeRoot);
261 if (ACPI_FAILURE (Status))
266 /* Namespace cross-reference */
268 AslGbl_NamespaceEvent = UtBeginEvent (
269 "Cross reference parse tree and Namespace");
270 DbgPrint (ASL_DEBUG_OUTPUT, "Cross referencing namespace\n\n");
271 Status = XfCrossReferenceNamespace ();
272 if (ACPI_FAILURE (Status))
277 /* Namespace - Check for non-referenced objects */
279 LkFindUnreferencedObjects ();
280 UtEndEvent (AslGbl_NamespaceEvent);
282 /* Resolve External Declarations */
286 Event = UtBeginEvent ("Resolve all Externals");
287 DbgPrint (ASL_DEBUG_OUTPUT, "\nResolve Externals\n\n");
288 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_TWICE,
289 ExAmlExternalWalkBegin, ExAmlExternalWalkEnd, NULL);
294 * Semantic analysis. This can happen only after the
295 * namespace has been loaded and cross-referenced.
297 * part one - check control methods
299 Event = UtBeginEvent ("Analyze control method return types");
300 AnalysisWalkInfo.MethodStack = NULL;
302 DbgPrint (ASL_DEBUG_OUTPUT, "Semantic analysis - Method analysis\n\n");
304 if (Gbl_CrossReferenceOutput)
306 OtPrintHeaders ("Part 1: Object Reference Map "
307 "(Object references from within each control method)");
310 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_TWICE,
311 MtMethodAnalysisWalkBegin,
312 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
315 /* Generate the object cross-reference file if requested */
317 Event = UtBeginEvent ("Generate cross-reference file");
321 /* Semantic error checking part two - typing of method returns */
323 Event = UtBeginEvent ("Determine object types returned by methods");
324 DbgPrint (ASL_DEBUG_OUTPUT, "Semantic analysis - Method typing\n\n");
325 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
326 NULL, AnMethodTypingWalkEnd, NULL);
329 /* Semantic error checking part three - operand type checking */
331 Event = UtBeginEvent ("Analyze AML operand types");
332 DbgPrint (ASL_DEBUG_OUTPUT,
333 "Semantic analysis - Operand type checking\n\n");
334 if (Gbl_DoTypechecking)
336 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD,
337 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
341 /* Semantic error checking part four - other miscellaneous checks */
343 Event = UtBeginEvent ("Miscellaneous analysis");
344 DbgPrint (ASL_DEBUG_OUTPUT, "Semantic analysis - miscellaneous\n\n");
345 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_DOWNWARD,
346 AnOtherSemanticAnalysisWalkBegin,
347 NULL, &AnalysisWalkInfo);
350 /* Calculate all AML package lengths */
352 Event = UtBeginEvent ("Finish AML package length generation");
353 DbgPrint (ASL_DEBUG_OUTPUT, "Generating Package lengths\n\n");
354 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
355 LnInitLengthsWalk, NULL);
356 TrWalkParseTree (Gbl_ParseTreeRoot, ASL_WALK_VISIT_UPWARD, NULL,
357 LnPackageLengthWalk, NULL);
360 /* Code generation - emit the AML */
362 Event = UtBeginEvent ("Generate AML code and write output files");
363 DbgPrint (ASL_DEBUG_OUTPUT, "Writing AML byte code\n\n");
364 CgGenerateAmlOutput ();
367 Event = UtBeginEvent ("Write optional output files");
371 UtEndEvent (FullCompile);
376 UtEndEvent (FullCompile);
382 /*******************************************************************************
384 * FUNCTION: AslCompilerSignon
386 * PARAMETERS: FileId - ID of the output file
390 * DESCRIPTION: Display compiler signon
392 ******************************************************************************/
402 /* Set line prefix depending on the destination file type */
406 case ASL_FILE_ASM_SOURCE_OUTPUT:
407 case ASL_FILE_ASM_INCLUDE_OUTPUT:
412 case ASL_FILE_HEX_OUTPUT:
414 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
418 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
419 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
421 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
426 case ASL_FILE_C_SOURCE_OUTPUT:
427 case ASL_FILE_C_OFFSET_OUTPUT:
428 case ASL_FILE_C_INCLUDE_OUTPUT:
435 /* No other output types supported */
440 /* Running compiler or disassembler? */
444 UtilityName = AML_DISASSEMBLER_NAME;
448 UtilityName = ASL_COMPILER_NAME;
451 /* Compiler signon with copyright */
453 FlPrintFile (FileId, "%s\n", Prefix);
454 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
458 /*******************************************************************************
460 * FUNCTION: AslCompilerFileHeader
462 * PARAMETERS: FileId - ID of the output file
466 * DESCRIPTION: Header used at the beginning of output files
468 ******************************************************************************/
471 AslCompilerFileHeader (
479 /* Set line prefix depending on the destination file type */
483 case ASL_FILE_ASM_SOURCE_OUTPUT:
484 case ASL_FILE_ASM_INCLUDE_OUTPUT:
489 case ASL_FILE_HEX_OUTPUT:
491 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
495 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
496 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
502 case ASL_FILE_C_SOURCE_OUTPUT:
503 case ASL_FILE_C_OFFSET_OUTPUT:
504 case ASL_FILE_C_INCLUDE_OUTPUT:
511 /* No other output types supported */
516 /* Compilation header with timestamp */
518 (void) time (&Aclock);
519 NewTime = localtime (&Aclock);
522 "%sCompilation of \"%s\" - %s%s\n",
523 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
528 case ASL_FILE_C_SOURCE_OUTPUT:
529 case ASL_FILE_C_OFFSET_OUTPUT:
530 case ASL_FILE_C_INCLUDE_OUTPUT:
532 FlPrintFile (FileId, " */\n");
537 /* Nothing to do for other output types */
544 /*******************************************************************************
546 * FUNCTION: CmFlushSourceCode
552 * DESCRIPTION: Read in any remaining source code after the parse tree
553 * has been constructed.
555 ******************************************************************************/
564 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
566 AslInsertLineBuffer ((int) Buffer);
569 AslResetCurrentLineBuffer ();
573 /*******************************************************************************
575 * FUNCTION: CmDoOutputFiles
581 * DESCRIPTION: Create all "listing" type files
583 ******************************************************************************/
590 /* Create listings and hex files */
595 /* Dump the namespace to the .nsp file if requested */
597 (void) NsDisplayNamespace ();
599 /* Dump the device mapping file */
601 MpEmitMappingInfo ();
605 /*******************************************************************************
607 * FUNCTION: CmDumpAllEvents
613 * DESCRIPTION: Dump all compiler events
615 ******************************************************************************/
621 ASL_EVENT_INFO *Event;
628 Event = AslGbl_Events;
630 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
631 if (Gbl_CompileTimesFlag)
633 printf ("\nElapsed time for major events\n\n");
636 for (i = 0; i < AslGbl_NextEvent; i++)
640 /* Delta will be in 100-nanosecond units */
642 Delta = (UINT32) (Event->EndTime - Event->StartTime);
644 MicroSeconds = Delta / ACPI_100NSEC_PER_USEC;
645 MilliSeconds = Delta / ACPI_100NSEC_PER_MSEC;
647 /* Round milliseconds up */
649 if ((MicroSeconds - (MilliSeconds * ACPI_USEC_PER_MSEC)) >= 500)
654 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
655 MicroSeconds, MilliSeconds, Event->EventName);
657 if (Gbl_CompileTimesFlag)
659 printf ("%8u usec %8u msec - %s\n",
660 MicroSeconds, MilliSeconds, Event->EventName);
669 /*******************************************************************************
671 * FUNCTION: CmCleanupAndExit
677 * DESCRIPTION: Close all open files and exit the compiler
679 ******************************************************************************/
686 BOOLEAN DeleteAmlFile = FALSE;
689 AePrintErrorLog (ASL_FILE_STDERR);
692 /* Print error summary to stdout also */
694 AePrintErrorLog (ASL_FILE_STDOUT);
697 /* Emit compile times if enabled */
701 if (Gbl_CompileTimesFlag)
703 printf ("\nMiscellaneous compile statistics\n\n");
704 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
705 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
706 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
707 printf ("%11u : %s\n", TotalMethods, "Control methods");
708 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
709 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
710 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
714 if (Gbl_NsLookupCount)
716 DbgPrint (ASL_DEBUG_OUTPUT,
717 "\n\nMiscellaneous compile statistics\n\n");
719 DbgPrint (ASL_DEBUG_OUTPUT,
720 "%32s : %u\n", "Total Namespace searches",
723 DbgPrint (ASL_DEBUG_OUTPUT,
724 "%32s : %u usec\n", "Time per search", ((UINT32)
725 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
726 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
730 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
732 printf ("\nMaximum error count (%u) exceeded\n",
733 ASL_MAX_ERROR_COUNT);
736 UtDisplaySummary (ASL_FILE_STDOUT);
739 * We will delete the AML file if there are errors and the
740 * force AML output option has not been used.
742 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
743 (!Gbl_IgnoreErrors) &&
744 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
746 DeleteAmlFile = TRUE;
749 /* Close all open files */
752 * Take care with the preprocessor file (.pre), it might be the same
753 * as the "input" file, depending on where the compiler has terminated
754 * or aborted. Prevent attempt to close the same file twice in
757 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
758 Gbl_Files[ASL_FILE_INPUT].Handle)
760 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
763 /* Close the standard I/O files */
765 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
770 /* Delete AML file if there are errors */
774 FlDeleteFile (ASL_FILE_AML_OUTPUT);
777 /* Delete the preprocessor temp file unless full debug was specified */
779 if (Gbl_PreprocessFlag && !Gbl_KeepPreprocessorTempFile)
781 FlDeleteFile (ASL_FILE_PREPROCESSOR);
785 * Delete intermediate ("combined") source file (if -ls flag not set)
786 * This file is created during normal ASL/AML compiles. It is not
787 * created by the data table compiler.
789 * If the -ls flag is set, then the .SRC file should not be deleted.
790 * In this case, Gbl_SourceOutputFlag is set to TRUE.
792 * Note: Handles are cleared by FlCloseFile above, so we look at the
793 * filename instead, to determine if the .SRC file was actually
796 if (!Gbl_SourceOutputFlag)
798 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
801 /* Final cleanup after compiling one file */
807 /*******************************************************************************
809 * FUNCTION: CmDeleteCaches
815 * DESCRIPTION: Delete all local cache buffer blocks
817 ******************************************************************************/
824 ASL_CACHE_INFO *Next;
830 while (Gbl_ParseOpCacheList)
832 Next = Gbl_ParseOpCacheList->Next;
833 ACPI_FREE (Gbl_ParseOpCacheList);
834 Gbl_ParseOpCacheList = Next;
838 DbgPrint (ASL_DEBUG_OUTPUT,
839 "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
840 Gbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
841 (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
843 Gbl_ParseOpCount = 0;
844 Gbl_ParseOpCacheNext = NULL;
845 Gbl_ParseOpCacheLast = NULL;
846 Gbl_ParseTreeRoot = NULL;
848 /* Generic string cache */
851 while (Gbl_StringCacheList)
853 Next = Gbl_StringCacheList->Next;
854 ACPI_FREE (Gbl_StringCacheList);
855 Gbl_StringCacheList = Next;
859 DbgPrint (ASL_DEBUG_OUTPUT,
860 "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
861 Gbl_StringCount, Gbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
865 Gbl_StringCacheNext = NULL;
866 Gbl_StringCacheLast = NULL;