1 /******************************************************************************
3 * Module Name: aslcompile - top level compile module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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 Gbl_CurrentLineNumber = 1;
107 Gbl_LogicalLineNumber = 1;
109 if (Gbl_PreprocessOnly)
119 /* Build the parse tree */
121 Event = UtBeginEvent ("Parse source code and build parse tree");
125 /* Check for parser-detected syntax errors */
129 fprintf (stderr, "Compiler aborting due to parser-detected syntax error(s)\n");
134 /* Did the parse tree get successfully constructed? */
139 * If there are no errors, then we have some sort of
142 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
143 NULL, "- Could not resolve parse tree root node");
148 /* Flush out any remaining source after parse tree is complete */
150 Event = UtBeginEvent ("Flush source input");
151 CmFlushSourceCode ();
153 /* Prune the parse tree if requested (debug purposes only) */
155 if (Gbl_PruneParseTree)
157 AslPruneParseTree (Gbl_PruneDepth, Gbl_PruneType);
160 /* Optional parse tree dump, compiler debug output only */
164 OpcGetIntegerWidth (RootNode);
167 /* Pre-process parse tree for any operator transforms */
169 Event = UtBeginEvent ("Parse tree transforms");
170 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
171 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
172 TrAmlTransformWalk, NULL, NULL);
175 /* Generate AML opcodes corresponding to the parse tokens */
177 Event = UtBeginEvent ("Generate AML opcodes");
178 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
179 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
180 OpcAmlOpcodeWalk, NULL);
184 * Now that the input is parsed, we can open the AML output file.
185 * Note: by default, the name of this file comes from the table descriptor
186 * within the input file.
188 Event = UtBeginEvent ("Open AML output file");
189 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
191 if (ACPI_FAILURE (Status))
193 AePrintErrorLog (ASL_FILE_STDERR);
197 /* Interpret and generate all compile-time constants */
199 Event = UtBeginEvent ("Constant folding via AML interpreter");
200 DbgPrint (ASL_DEBUG_OUTPUT,
201 "\nInterpreting compile-time constant expressions\n\n");
203 if (Gbl_FoldConstants)
205 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
206 OpcAmlConstantWalk, NULL, NULL);
210 DbgPrint (ASL_PARSE_OUTPUT, " Optional folding disabled\n");
214 /* Update AML opcodes if necessary, after constant folding */
216 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
217 DbgPrint (ASL_DEBUG_OUTPUT,
218 "\nUpdating AML opcodes after constant folding\n\n");
219 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
220 NULL, OpcAmlOpcodeUpdateWalk, NULL);
223 /* Calculate all AML package lengths */
225 Event = UtBeginEvent ("Generate AML package lengths");
226 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
227 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
228 LnPackageLengthWalk, NULL);
231 if (Gbl_ParseOnlyFlag)
233 AePrintErrorLog (ASL_FILE_STDERR);
234 UtDisplaySummary (ASL_FILE_STDERR);
237 /* Print error summary to the stdout also */
239 AePrintErrorLog (ASL_FILE_STDOUT);
240 UtDisplaySummary (ASL_FILE_STDOUT);
242 UtEndEvent (FullCompile);
247 * Create an internal namespace and use it as a symbol table
250 /* Namespace loading */
252 Event = UtBeginEvent ("Create ACPI Namespace");
253 Status = LdLoadNamespace (RootNode);
255 if (ACPI_FAILURE (Status))
260 /* Namespace cross-reference */
262 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
263 Status = XfCrossReferenceNamespace ();
264 if (ACPI_FAILURE (Status))
269 /* Namespace - Check for non-referenced objects */
271 LkFindUnreferencedObjects ();
272 UtEndEvent (AslGbl_NamespaceEvent);
275 * Semantic analysis. This can happen only after the
276 * namespace has been loaded and cross-referenced.
278 * part one - check control methods
280 Event = UtBeginEvent ("Analyze control method return types");
281 AnalysisWalkInfo.MethodStack = NULL;
283 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
284 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
285 MtMethodAnalysisWalkBegin,
286 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
289 /* Semantic error checking part two - typing of method returns */
291 Event = UtBeginEvent ("Determine object types returned by methods");
292 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
293 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
294 NULL, AnMethodTypingWalkEnd, NULL);
297 /* Semantic error checking part three - operand type checking */
299 Event = UtBeginEvent ("Analyze AML operand types");
300 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
301 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
302 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
305 /* Semantic error checking part four - other miscellaneous checks */
307 Event = UtBeginEvent ("Miscellaneous analysis");
308 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
309 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
310 AnOtherSemanticAnalysisWalkBegin,
311 NULL, &AnalysisWalkInfo);
314 /* Calculate all AML package lengths */
316 Event = UtBeginEvent ("Finish AML package length generation");
317 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
318 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
319 LnInitLengthsWalk, NULL);
320 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
321 LnPackageLengthWalk, NULL);
324 /* Code generation - emit the AML */
326 Event = UtBeginEvent ("Generate AML code and write output files");
327 CgGenerateAmlOutput ();
330 Event = UtBeginEvent ("Write optional output files");
334 UtEndEvent (FullCompile);
339 UtEndEvent (FullCompile);
345 /*******************************************************************************
347 * FUNCTION: AslCompilerSignon
349 * PARAMETERS: FileId - ID of the output file
353 * DESCRIPTION: Display compiler signon
355 ******************************************************************************/
365 /* Set line prefix depending on the destination file type */
369 case ASL_FILE_ASM_SOURCE_OUTPUT:
370 case ASL_FILE_ASM_INCLUDE_OUTPUT:
375 case ASL_FILE_HEX_OUTPUT:
377 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
381 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
382 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
384 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
389 case ASL_FILE_C_SOURCE_OUTPUT:
390 case ASL_FILE_C_OFFSET_OUTPUT:
391 case ASL_FILE_C_INCLUDE_OUTPUT:
398 /* No other output types supported */
403 /* Running compiler or disassembler? */
407 UtilityName = AML_DISASSEMBLER_NAME;
411 UtilityName = ASL_COMPILER_NAME;
414 /* Compiler signon with copyright */
416 FlPrintFile (FileId, "%s\n", Prefix);
417 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
421 /*******************************************************************************
423 * FUNCTION: AslCompilerFileHeader
425 * PARAMETERS: FileId - ID of the output file
429 * DESCRIPTION: Header used at the beginning of output files
431 ******************************************************************************/
434 AslCompilerFileHeader (
442 /* Set line prefix depending on the destination file type */
446 case ASL_FILE_ASM_SOURCE_OUTPUT:
447 case ASL_FILE_ASM_INCLUDE_OUTPUT:
452 case ASL_FILE_HEX_OUTPUT:
454 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
458 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
459 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
465 case ASL_FILE_C_SOURCE_OUTPUT:
466 case ASL_FILE_C_OFFSET_OUTPUT:
467 case ASL_FILE_C_INCLUDE_OUTPUT:
474 /* No other output types supported */
479 /* Compilation header with timestamp */
481 (void) time (&Aclock);
482 NewTime = localtime (&Aclock);
485 "%sCompilation of \"%s\" - %s%s\n",
486 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
491 case ASL_FILE_C_SOURCE_OUTPUT:
492 case ASL_FILE_C_OFFSET_OUTPUT:
493 case ASL_FILE_C_INCLUDE_OUTPUT:
495 FlPrintFile (FileId, " */\n");
500 /* Nothing to do for other output types */
507 /*******************************************************************************
509 * FUNCTION: CmFlushSourceCode
515 * DESCRIPTION: Read in any remaining source code after the parse tree
516 * has been constructed.
518 ******************************************************************************/
527 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
529 AslInsertLineBuffer ((int) Buffer);
532 AslResetCurrentLineBuffer ();
536 /*******************************************************************************
538 * FUNCTION: CmDoOutputFiles
544 * DESCRIPTION: Create all "listing" type files
546 ******************************************************************************/
553 /* Create listings and hex files */
558 /* Dump the namespace to the .nsp file if requested */
560 (void) NsDisplayNamespace ();
562 /* Dump the device mapping file */
564 MpEmitMappingInfo ();
568 /*******************************************************************************
570 * FUNCTION: CmDumpAllEvents
576 * DESCRIPTION: Dump all compiler events
578 ******************************************************************************/
584 ASL_EVENT_INFO *Event;
591 Event = AslGbl_Events;
593 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
594 if (Gbl_CompileTimesFlag)
596 printf ("\nElapsed time for major events\n\n");
599 for (i = 0; i < AslGbl_NextEvent; i++)
603 /* Delta will be in 100-nanosecond units */
605 Delta = (UINT32) (Event->EndTime - Event->StartTime);
607 USec = Delta / ACPI_100NSEC_PER_USEC;
608 MSec = Delta / ACPI_100NSEC_PER_MSEC;
610 /* Round milliseconds up */
612 if ((USec - (MSec * ACPI_USEC_PER_MSEC)) >= 500)
617 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
618 USec, MSec, Event->EventName);
620 if (Gbl_CompileTimesFlag)
622 printf ("%8u usec %8u msec - %s\n",
623 USec, MSec, Event->EventName);
632 /*******************************************************************************
634 * FUNCTION: CmCleanupAndExit
640 * DESCRIPTION: Close all open files and exit the compiler
642 ******************************************************************************/
649 BOOLEAN DeleteAmlFile = FALSE;
652 AePrintErrorLog (ASL_FILE_STDERR);
655 /* Print error summary to stdout also */
657 AePrintErrorLog (ASL_FILE_STDOUT);
660 /* Emit compile times if enabled */
664 if (Gbl_CompileTimesFlag)
666 printf ("\nMiscellaneous compile statistics\n\n");
667 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
668 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
669 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
670 printf ("%11u : %s\n", TotalMethods, "Control methods");
671 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
672 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
673 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
677 if (Gbl_NsLookupCount)
679 DbgPrint (ASL_DEBUG_OUTPUT,
680 "\n\nMiscellaneous compile statistics\n\n");
682 DbgPrint (ASL_DEBUG_OUTPUT,
683 "%32s : %u\n", "Total Namespace searches",
686 DbgPrint (ASL_DEBUG_OUTPUT,
687 "%32s : %u usec\n", "Time per search", ((UINT32)
688 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
689 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
693 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
695 printf ("\nMaximum error count (%u) exceeded\n",
696 ASL_MAX_ERROR_COUNT);
699 UtDisplaySummary (ASL_FILE_STDOUT);
702 * We will delete the AML file if there are errors and the
703 * force AML output option has not been used.
705 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) &&
706 (!Gbl_IgnoreErrors) &&
707 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
709 DeleteAmlFile = TRUE;
712 /* Close all open files */
715 * Take care with the preprocessor file (.pre), it might be the same
716 * as the "input" file, depending on where the compiler has terminated
717 * or aborted. Prevent attempt to close the same file twice in
720 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
721 Gbl_Files[ASL_FILE_INPUT].Handle)
723 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
726 /* Close the standard I/O files */
728 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
733 /* Delete AML file if there are errors */
737 FlDeleteFile (ASL_FILE_AML_OUTPUT);
740 /* Delete the preprocessor temp file unless full debug was specified */
742 if (Gbl_PreprocessFlag && !Gbl_KeepPreprocessorTempFile)
744 FlDeleteFile (ASL_FILE_PREPROCESSOR);
748 * Delete intermediate ("combined") source file (if -ls flag not set)
749 * This file is created during normal ASL/AML compiles. It is not
750 * created by the data table compiler.
752 * If the -ls flag is set, then the .SRC file should not be deleted.
753 * In this case, Gbl_SourceOutputFlag is set to TRUE.
755 * Note: Handles are cleared by FlCloseFile above, so we look at the
756 * filename instead, to determine if the .SRC file was actually
759 if (!Gbl_SourceOutputFlag)
761 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);
764 /* Final cleanup after compiling one file */
770 /*******************************************************************************
772 * FUNCTION: CmDeleteCaches
778 * DESCRIPTION: Delete all local cache buffer blocks
780 ******************************************************************************/
787 ASL_CACHE_INFO *Next;
793 while (Gbl_ParseOpCacheList)
795 Next = Gbl_ParseOpCacheList->Next;
796 ACPI_FREE (Gbl_ParseOpCacheList);
797 Gbl_ParseOpCacheList = Next;
801 DbgPrint (ASL_DEBUG_OUTPUT,
802 "%u ParseOps, Buffer size: %u ops (%u bytes), %u Buffers\n",
803 Gbl_ParseOpCount, ASL_PARSEOP_CACHE_SIZE,
804 (sizeof (ACPI_PARSE_OBJECT) * ASL_PARSEOP_CACHE_SIZE), BufferCount);
806 Gbl_ParseOpCount = 0;
807 Gbl_ParseOpCacheNext = NULL;
808 Gbl_ParseOpCacheLast = NULL;
811 /* Generic string cache */
814 while (Gbl_StringCacheList)
816 Next = Gbl_StringCacheList->Next;
817 ACPI_FREE (Gbl_StringCacheList);
818 Gbl_StringCacheList = Next;
822 DbgPrint (ASL_DEBUG_OUTPUT,
823 "%u Strings (%u bytes), Buffer size: %u bytes, %u Buffers\n",
824 Gbl_StringCount, Gbl_StringSize, ASL_STRING_CACHE_SIZE, BufferCount);
828 Gbl_StringCacheNext = NULL;
829 Gbl_StringCacheLast = NULL;