1 /******************************************************************************
3 * Module Name: aslcompile - top level compile module
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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>
49 #include <contrib/dev/acpica/include/acapps.h>
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("aslcompile")
56 * External is here in case the parser emits the same external in the
57 * generated header. (Newer versions of Bison)
63 /* Local prototypes */
70 FlConsumeAnsiComment (
72 ASL_FILE_STATUS *Status);
77 ASL_FILE_STATUS *Status);
84 /*******************************************************************************
86 * FUNCTION: AslCompilerSignon
88 * PARAMETERS: FileId - ID of the output file
92 * DESCRIPTION: Display compiler signon
94 ******************************************************************************/
104 /* Set line prefix depending on the destination file type */
108 case ASL_FILE_ASM_SOURCE_OUTPUT:
109 case ASL_FILE_ASM_INCLUDE_OUTPUT:
114 case ASL_FILE_HEX_OUTPUT:
116 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
120 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
121 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
123 FlPrintFile (ASL_FILE_HEX_OUTPUT, "/*\n");
128 case ASL_FILE_C_SOURCE_OUTPUT:
129 case ASL_FILE_C_OFFSET_OUTPUT:
130 case ASL_FILE_C_INCLUDE_OUTPUT:
137 /* No other output types supported */
142 /* Running compiler or disassembler? */
146 UtilityName = AML_DISASSEMBLER_NAME;
150 UtilityName = ASL_COMPILER_NAME;
153 /* Compiler signon with copyright */
155 FlPrintFile (FileId, "%s\n", Prefix);
156 FlPrintFile (FileId, ACPI_COMMON_HEADER (UtilityName, Prefix));
160 /*******************************************************************************
162 * FUNCTION: AslCompilerFileHeader
164 * PARAMETERS: FileId - ID of the output file
168 * DESCRIPTION: Header used at the beginning of output files
170 ******************************************************************************/
173 AslCompilerFileHeader (
181 /* Set line prefix depending on the destination file type */
185 case ASL_FILE_ASM_SOURCE_OUTPUT:
186 case ASL_FILE_ASM_INCLUDE_OUTPUT:
191 case ASL_FILE_HEX_OUTPUT:
193 if (Gbl_HexOutputFlag == HEX_OUTPUT_ASM)
197 else if ((Gbl_HexOutputFlag == HEX_OUTPUT_C) ||
198 (Gbl_HexOutputFlag == HEX_OUTPUT_ASL))
204 case ASL_FILE_C_SOURCE_OUTPUT:
205 case ASL_FILE_C_OFFSET_OUTPUT:
206 case ASL_FILE_C_INCLUDE_OUTPUT:
213 /* No other output types supported */
218 /* Compilation header with timestamp */
220 (void) time (&Aclock);
221 NewTime = localtime (&Aclock);
224 "%sCompilation of \"%s\" - %s%s\n",
225 Prefix, Gbl_Files[ASL_FILE_INPUT].Filename, asctime (NewTime),
230 case ASL_FILE_C_SOURCE_OUTPUT:
231 case ASL_FILE_C_OFFSET_OUTPUT:
232 case ASL_FILE_C_INCLUDE_OUTPUT:
234 FlPrintFile (FileId, " */\n");
239 /* Nothing to do for other output types */
246 /*******************************************************************************
248 * FUNCTION: CmFlushSourceCode
254 * DESCRIPTION: Read in any remaining source code after the parse tree
255 * has been constructed.
257 ******************************************************************************/
266 while (FlReadFile (ASL_FILE_INPUT, &Buffer, 1) != AE_ERROR)
268 AslInsertLineBuffer ((int) Buffer);
271 AslResetCurrentLineBuffer ();
275 /*******************************************************************************
277 * FUNCTION: FlConsume*
279 * PARAMETERS: Handle - Open input file
280 * Status - File current status struct
282 * RETURN: Number of lines consumed
284 * DESCRIPTION: Step over both types of comment during check for ascii chars
286 ******************************************************************************/
289 FlConsumeAnsiComment (
291 ASL_FILE_STATUS *Status)
294 BOOLEAN ClosingComment = FALSE;
297 while (fread (&Byte, 1, 1, Handle) == 1)
299 /* Scan until comment close is found */
312 ClosingComment = FALSE;
315 else if (Byte == '*')
317 ClosingComment = TRUE;
320 /* Maintain line count */
333 FlConsumeNewComment (
335 ASL_FILE_STATUS *Status)
340 while (fread (&Byte, 1, 1, Handle) == 1)
344 /* Comment ends at newline */
355 /*******************************************************************************
357 * FUNCTION: FlCheckForAcpiTable
359 * PARAMETERS: Handle - Open input file
363 * DESCRIPTION: Determine if a file seems to be a binary ACPI table, via the
364 * following checks on what would be the table header:
365 * 0) File must be at least as long as an ACPI_TABLE_HEADER
366 * 1) The header length field must match the file size
367 * 2) Signature, OemId, OemTableId, AslCompilerId must be ASCII
369 ******************************************************************************/
372 FlCheckForAcpiTable (
375 ACPI_TABLE_HEADER Table;
381 /* Read a potential table header */
383 Actual = fread (&Table, 1, sizeof (ACPI_TABLE_HEADER), Handle);
384 fseek (Handle, 0, SEEK_SET);
386 if (Actual < sizeof (ACPI_TABLE_HEADER))
391 /* Header length field must match the file size */
393 FileSize = DtGetFileSize (Handle);
394 if (Table.Length != FileSize)
400 * These fields must be ASCII:
401 * Signature, OemId, OemTableId, AslCompilerId.
402 * We allow a NULL terminator in OemId and OemTableId.
404 for (i = 0; i < ACPI_NAME_SIZE; i++)
406 if (!ACPI_IS_ASCII ((UINT8) Table.Signature[i]))
411 if (!ACPI_IS_ASCII ((UINT8) Table.AslCompilerId[i]))
417 for (i = 0; (i < ACPI_OEM_ID_SIZE) && (Table.OemId[i]); i++)
419 if (!ACPI_IS_ASCII ((UINT8) Table.OemId[i]))
425 for (i = 0; (i < ACPI_OEM_TABLE_ID_SIZE) && (Table.OemTableId[i]); i++)
427 if (!ACPI_IS_ASCII ((UINT8) Table.OemTableId[i]))
433 printf ("Binary file appears to be a valid ACPI table, disassembling\n");
438 /*******************************************************************************
440 * FUNCTION: FlCheckForAscii
442 * PARAMETERS: Handle - Open input file
443 * Filename - Input filename
444 * DisplayErrors - TRUE if error messages desired
448 * DESCRIPTION: Verify that the input file is entirely ASCII. Ignores characters
449 * within comments. Note: does not handle nested comments and does
450 * not handle comment delimiters within string literals. However,
451 * on the rare chance this happens and an invalid character is
452 * missed, the parser will catch the error by failing in some
453 * spectactular manner.
455 ******************************************************************************/
461 BOOLEAN DisplayErrors)
464 ACPI_SIZE BadBytes = 0;
465 BOOLEAN OpeningComment = FALSE;
466 ASL_FILE_STATUS Status;
472 /* Read the entire file */
474 while (fread (&Byte, 1, 1, Handle) == 1)
476 /* Ignore comment fields (allow non-ascii within) */
480 /* Check for second comment open delimiter */
484 FlConsumeAnsiComment (Handle, &Status);
489 FlConsumeNewComment (Handle, &Status);
494 OpeningComment = FALSE;
496 else if (Byte == '/')
498 OpeningComment = TRUE;
501 /* Check for an ASCII character */
503 if (!ACPI_IS_ASCII (Byte))
505 if ((BadBytes < 10) && (DisplayErrors))
508 "Non-ASCII character [0x%2.2X] found in line %u, file offset 0x%.2X\n",
509 Byte, Status.Line, Status.Offset);
515 /* Update line counter */
517 else if (Byte == 0x0A)
525 /* Seek back to the beginning of the source file */
527 fseek (Handle, 0, SEEK_SET);
529 /* Were there any non-ASCII characters in the file? */
536 "%u non-ASCII characters found in input source text, could be a binary file\n",
538 AslError (ASL_ERROR, ASL_MSG_NON_ASCII, NULL, Filename);
541 return (AE_BAD_CHARACTER);
544 /* File is OK (100% ASCII) */
550 /*******************************************************************************
552 * FUNCTION: CmDoCompile
556 * RETURN: Status (0 = OK)
558 * DESCRIPTION: This procedure performs the entire compile
560 ******************************************************************************/
571 FullCompile = UtBeginEvent ("*** Total Compile time ***");
572 Event = UtBeginEvent ("Open input and output files");
575 Event = UtBeginEvent ("Preprocess input file");
576 if (Gbl_PreprocessFlag)
581 if (Gbl_PreprocessOnly)
590 /* Build the parse tree */
592 Event = UtBeginEvent ("Parse source code and build parse tree");
596 /* Check for parse errors */
598 Status = AslCheckForErrorExit ();
599 if (ACPI_FAILURE (Status))
601 fprintf (stderr, "Compiler aborting due to parser-detected syntax error(s)\n");
606 /* Did the parse tree get successfully constructed? */
611 * If there are no errors, then we have some sort of
614 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
615 NULL, "- Could not resolve parse tree root node");
621 /* Flush out any remaining source after parse tree is complete */
623 Event = UtBeginEvent ("Flush source input");
624 CmFlushSourceCode ();
626 /* Optional parse tree dump, compiler debug output only */
630 OpcGetIntegerWidth (RootNode);
633 /* Pre-process parse tree for any operator transforms */
635 Event = UtBeginEvent ("Parse tree transforms");
636 DbgPrint (ASL_DEBUG_OUTPUT, "\nParse tree transforms\n\n");
637 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
638 TrAmlTransformWalk, NULL, NULL);
641 /* Generate AML opcodes corresponding to the parse tokens */
643 Event = UtBeginEvent ("Generate AML opcodes");
644 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating AML opcodes\n\n");
645 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
646 OpcAmlOpcodeWalk, NULL);
650 * Now that the input is parsed, we can open the AML output file.
651 * Note: by default, the name of this file comes from the table descriptor
652 * within the input file.
654 Event = UtBeginEvent ("Open AML output file");
655 Status = FlOpenAmlOutputFile (Gbl_OutputFilenamePrefix);
657 if (ACPI_FAILURE (Status))
659 AePrintErrorLog (ASL_FILE_STDERR);
663 /* Interpret and generate all compile-time constants */
665 Event = UtBeginEvent ("Constant folding via AML interpreter");
666 DbgPrint (ASL_DEBUG_OUTPUT,
667 "\nInterpreting compile-time constant expressions\n\n");
668 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
669 OpcAmlConstantWalk, NULL, NULL);
672 /* Update AML opcodes if necessary, after constant folding */
674 Event = UtBeginEvent ("Updating AML opcodes after constant folding");
675 DbgPrint (ASL_DEBUG_OUTPUT,
676 "\nUpdating AML opcodes after constant folding\n\n");
677 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
678 NULL, OpcAmlOpcodeUpdateWalk, NULL);
681 /* Calculate all AML package lengths */
683 Event = UtBeginEvent ("Generate AML package lengths");
684 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
685 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
686 LnPackageLengthWalk, NULL);
689 if (Gbl_ParseOnlyFlag)
691 AePrintErrorLog (ASL_FILE_STDERR);
692 UtDisplaySummary (ASL_FILE_STDERR);
695 /* Print error summary to the stdout also */
697 AePrintErrorLog (ASL_FILE_STDOUT);
698 UtDisplaySummary (ASL_FILE_STDOUT);
700 UtEndEvent (FullCompile);
705 * Create an internal namespace and use it as a symbol table
708 /* Namespace loading */
710 Event = UtBeginEvent ("Create ACPI Namespace");
711 Status = LdLoadNamespace (RootNode);
713 if (ACPI_FAILURE (Status))
718 /* Namespace cross-reference */
720 AslGbl_NamespaceEvent = UtBeginEvent ("Cross reference parse tree and Namespace");
721 Status = XfCrossReferenceNamespace ();
722 if (ACPI_FAILURE (Status))
727 /* Namespace - Check for non-referenced objects */
729 LkFindUnreferencedObjects ();
730 UtEndEvent (AslGbl_NamespaceEvent);
733 * Semantic analysis. This can happen only after the
734 * namespace has been loaded and cross-referenced.
736 * part one - check control methods
738 Event = UtBeginEvent ("Analyze control method return types");
739 AnalysisWalkInfo.MethodStack = NULL;
741 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method analysis\n\n");
742 TrWalkParseTree (RootNode, ASL_WALK_VISIT_TWICE,
743 MtMethodAnalysisWalkBegin,
744 MtMethodAnalysisWalkEnd, &AnalysisWalkInfo);
747 /* Semantic error checking part two - typing of method returns */
749 Event = UtBeginEvent ("Determine object types returned by methods");
750 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Method typing\n\n");
751 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
752 NULL, AnMethodTypingWalkEnd, NULL);
755 /* Semantic error checking part three - operand type checking */
757 Event = UtBeginEvent ("Analyze AML operand types");
758 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - Operand type checking\n\n");
759 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD,
760 NULL, AnOperandTypecheckWalkEnd, &AnalysisWalkInfo);
763 /* Semantic error checking part four - other miscellaneous checks */
765 Event = UtBeginEvent ("Miscellaneous analysis");
766 DbgPrint (ASL_DEBUG_OUTPUT, "\nSemantic analysis - miscellaneous\n\n");
767 TrWalkParseTree (RootNode, ASL_WALK_VISIT_DOWNWARD,
768 AnOtherSemanticAnalysisWalkBegin,
769 NULL, &AnalysisWalkInfo);
772 /* Calculate all AML package lengths */
774 Event = UtBeginEvent ("Finish AML package length generation");
775 DbgPrint (ASL_DEBUG_OUTPUT, "\nGenerating Package lengths\n\n");
776 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
777 LnInitLengthsWalk, NULL);
778 TrWalkParseTree (RootNode, ASL_WALK_VISIT_UPWARD, NULL,
779 LnPackageLengthWalk, NULL);
782 /* Code generation - emit the AML */
784 Event = UtBeginEvent ("Generate AML code and write output files");
785 CgGenerateAmlOutput ();
788 Event = UtBeginEvent ("Write optional output files");
792 UtEndEvent (FullCompile);
797 UtEndEvent (FullCompile);
803 /*******************************************************************************
805 * FUNCTION: CmDoOutputFiles
811 * DESCRIPTION: Create all "listing" type files
813 ******************************************************************************/
820 /* Create listings and hex files */
825 /* Dump the namespace to the .nsp file if requested */
827 (void) NsDisplayNamespace ();
831 /*******************************************************************************
833 * FUNCTION: CmDumpAllEvents
839 * DESCRIPTION: Dump all compiler events
841 ******************************************************************************/
847 ASL_EVENT_INFO *Event;
854 Event = AslGbl_Events;
856 DbgPrint (ASL_DEBUG_OUTPUT, "\n\nElapsed time for major events\n\n");
857 if (Gbl_CompileTimesFlag)
859 printf ("\nElapsed time for major events\n\n");
862 for (i = 0; i < AslGbl_NextEvent; i++)
866 /* Delta will be in 100-nanosecond units */
868 Delta = (UINT32) (Event->EndTime - Event->StartTime);
870 USec = Delta / ACPI_100NSEC_PER_USEC;
871 MSec = Delta / ACPI_100NSEC_PER_MSEC;
873 /* Round milliseconds up */
875 if ((USec - (MSec * ACPI_USEC_PER_MSEC)) >= 500)
880 DbgPrint (ASL_DEBUG_OUTPUT, "%8u usec %8u msec - %s\n",
881 USec, MSec, Event->EventName);
883 if (Gbl_CompileTimesFlag)
885 printf ("%8u usec %8u msec - %s\n",
886 USec, MSec, Event->EventName);
895 /*******************************************************************************
897 * FUNCTION: CmCleanupAndExit
903 * DESCRIPTION: Close all open files and exit the compiler
905 ******************************************************************************/
912 BOOLEAN DeleteAmlFile = FALSE;
915 AePrintErrorLog (ASL_FILE_STDERR);
918 /* Print error summary to stdout also */
920 AePrintErrorLog (ASL_FILE_STDOUT);
923 /* Emit compile times if enabled */
927 if (Gbl_CompileTimesFlag)
929 printf ("\nMiscellaneous compile statistics\n\n");
930 printf ("%11u : %s\n", TotalParseNodes, "Parse nodes");
931 printf ("%11u : %s\n", Gbl_NsLookupCount, "Namespace searches");
932 printf ("%11u : %s\n", TotalNamedObjects, "Named objects");
933 printf ("%11u : %s\n", TotalMethods, "Control methods");
934 printf ("%11u : %s\n", TotalAllocations, "Memory Allocations");
935 printf ("%11u : %s\n", TotalAllocated, "Total allocated memory");
936 printf ("%11u : %s\n", TotalFolds, "Constant subtrees folded");
940 if (Gbl_NsLookupCount)
942 DbgPrint (ASL_DEBUG_OUTPUT,
943 "\n\nMiscellaneous compile statistics\n\n");
945 DbgPrint (ASL_DEBUG_OUTPUT,
946 "%32s : %u\n", "Total Namespace searches",
949 DbgPrint (ASL_DEBUG_OUTPUT,
950 "%32s : %u usec\n", "Time per search", ((UINT32)
951 (AslGbl_Events[AslGbl_NamespaceEvent].EndTime -
952 AslGbl_Events[AslGbl_NamespaceEvent].StartTime) / 10) /
956 if (Gbl_ExceptionCount[ASL_ERROR] > ASL_MAX_ERROR_COUNT)
958 printf ("\nMaximum error count (%u) exceeded\n",
959 ASL_MAX_ERROR_COUNT);
962 UtDisplaySummary (ASL_FILE_STDOUT);
965 * We will delete the AML file if there are errors and the
966 * force AML output option has not been used.
968 if ((Gbl_ExceptionCount[ASL_ERROR] > 0) && (!Gbl_IgnoreErrors) &&
969 Gbl_Files[ASL_FILE_AML_OUTPUT].Handle)
971 DeleteAmlFile = TRUE;
974 /* Close all open files */
977 * Take care with the preprocessor file (.i), it might be the same
978 * as the "input" file, depending on where the compiler has terminated
979 * or aborted. Prevent attempt to close the same file twice in
982 if (Gbl_Files[ASL_FILE_PREPROCESSOR].Handle ==
983 Gbl_Files[ASL_FILE_INPUT].Handle)
985 Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
988 /* Close the standard I/O files */
990 for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
995 /* Delete AML file if there are errors */
999 FlDeleteFile (ASL_FILE_AML_OUTPUT);
1002 /* Delete the preprocessor output file (.i) unless -li flag is set */
1004 if (!Gbl_PreprocessorOutputFlag &&
1007 FlDeleteFile (ASL_FILE_PREPROCESSOR);
1011 * Delete intermediate ("combined") source file (if -ls flag not set)
1012 * This file is created during normal ASL/AML compiles. It is not
1013 * created by the data table compiler.
1015 * If the -ls flag is set, then the .SRC file should not be deleted.
1016 * In this case, Gbl_SourceOutputFlag is set to TRUE.
1018 * Note: Handles are cleared by FlCloseFile above, so we look at the
1019 * filename instead, to determine if the .SRC file was actually
1022 * TBD: SourceOutput should be .TMP, then rename if we want to keep it?
1024 if (!Gbl_SourceOutputFlag)
1026 FlDeleteFile (ASL_FILE_SOURCE_OUTPUT);