1 /******************************************************************************
3 * Module Name: aslstartup - Compiler startup routines, called from main
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 "aslcompiler.h"
48 #include "acconvert.h"
50 #define _COMPONENT ACPI_COMPILER
51 ACPI_MODULE_NAME ("aslstartup")
54 /* Local prototypes */
57 AslDetectSourceFileType (
67 static BOOLEAN AslToFile = TRUE;
70 /*******************************************************************************
72 * FUNCTION: AslInitializeGlobals
78 * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
79 * allows multiple files to be disassembled and/or compiled.
81 ******************************************************************************/
84 AslInitializeGlobals (
90 /* Init compiler globals */
93 Gbl_CurrentColumn = 0;
94 Gbl_CurrentLineNumber = 1;
95 Gbl_LogicalLineNumber = 1;
96 Gbl_CurrentLineOffset = 0;
97 Gbl_InputFieldCount = 0;
98 Gbl_InputByteCount = 0;
99 Gbl_NsLookupCount = 0;
100 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
103 Gbl_NextError = NULL;
104 Gbl_Signature = NULL;
107 TotalExecutableOpcodes = 0;
108 TotalNamedObjects = 0;
112 TotalAllocations = 0;
116 AslGbl_NextEvent = 0;
117 for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
119 Gbl_ExceptionCount[i] = 0;
122 for (i = ASL_FILE_INPUT; i <= ASL_MAX_FILE_TYPE; i++)
124 Gbl_Files[i].Handle = NULL;
125 Gbl_Files[i].Filename = NULL;
128 if (Gbl_CaptureComments)
130 Gbl_CommentState.SpacesBefore = 0;
131 Gbl_CommentState.CommentType = 1;
132 Gbl_CommentState.Latest_Parse_Node = NULL;
133 Gbl_CommentState.ParsingParenBraceNode = NULL;
134 Gbl_CommentState.CaptureComments = TRUE;
139 /*******************************************************************************
141 * FUNCTION: AslDetectSourceFileType
143 * PARAMETERS: Info - Name/Handle for the file (must be open)
147 * DESCRIPTION: Determine the type of the input file. Either binary (contains
148 * non-ASCII characters), ASL file, or an ACPI Data Table file.
150 ******************************************************************************/
153 AslDetectSourceFileType (
157 UINT8 Type = ASL_INPUT_TYPE_ASCII_DATA; /* default */
161 /* Check for 100% ASCII source file (comments are ignored) */
163 Status = FlIsFileAsciiSource (Info->Filename, FALSE);
164 if (ACPI_SUCCESS (Status))
167 * File contains ASCII source code. Determine if this is an ASL
168 * file or an ACPI data table file.
170 while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, Info->Handle))
172 /* Uppercase the buffer for caseless compare */
174 FileChar = Gbl_CurrentLineBuffer;
177 *FileChar = (char) toupper ((int) *FileChar);
181 /* Presence of "DefinitionBlock" indicates actual ASL code */
183 if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
185 /* Appears to be an ASL file */
187 Type = ASL_INPUT_TYPE_ASCII_ASL;
192 /* Appears to be an ASCII data table source file */
194 Type = ASL_INPUT_TYPE_ASCII_DATA;
198 /* We have some sort of binary table, check for valid ACPI table */
200 fseek (Info->Handle, 0, SEEK_SET);
202 Status = AcValidateTableHeader (Info->Handle, 0);
203 if (ACPI_SUCCESS (Status))
206 "Binary file appears to be a valid ACPI table, disassembling\n");
208 Type = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
212 Type = ASL_INPUT_TYPE_BINARY;
217 /* Must seek back to the start of the file */
219 fseek (Info->Handle, 0, SEEK_SET);
224 /*******************************************************************************
226 * FUNCTION: AslDoDisassembly
232 * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build
235 ******************************************************************************/
244 /* ACPICA subsystem initialization */
246 Status = AdInitialize ();
247 if (ACPI_FAILURE (Status))
252 Status = AcpiAllocateRootTable (4);
253 if (ACPI_FAILURE (Status))
255 AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
256 AcpiFormatException (Status));
260 /* Handle additional output files for disassembler */
262 Gbl_FileType = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
263 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
265 /* This is where the disassembly happens */
267 AcpiGbl_DmOpt_Disasm = TRUE;
268 Status = AdAmlDisassemble (AslToFile,
269 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix,
270 &Gbl_Files[ASL_FILE_INPUT].Filename);
271 if (ACPI_FAILURE (Status))
276 /* Check if any control methods were unresolved */
278 AcpiDmUnresolvedWarning (0);
280 /* Shutdown compiler and ACPICA subsystem */
283 (void) AcpiTerminate ();
286 * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
287 * .DSL disassembly file, which can now be compiled if requested
291 AcpiOsPrintf ("\nCompiling \"%s\"\n",
292 Gbl_Files[ASL_FILE_INPUT].Filename);
293 return (AE_CTRL_CONTINUE);
296 /* No need to free the filename string */
298 Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
305 /*******************************************************************************
307 * FUNCTION: AslDoOneFile
309 * PARAMETERS: Filename - Name of the file
313 * DESCRIPTION: Process a single file - either disassemble, compile, or both
315 ******************************************************************************/
324 /* Re-initialize "some" compiler/preprocessor globals */
326 AslInitializeGlobals ();
327 PrInitializeGlobals ();
330 * Extract the directory path. This path is used for possible include
331 * files and the optional AML filename embedded in the input file
332 * DefinitionBlock declaration.
334 Status = FlSplitInputPathname (Filename, &Gbl_DirectoryPath, NULL);
335 if (ACPI_FAILURE (Status))
340 /* Take a copy of the input filename, convert any backslashes */
342 Gbl_Files[ASL_FILE_INPUT].Filename =
343 UtStringCacheCalloc (strlen (Filename) + 1);
345 strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
346 UtConvertBackslashes (Gbl_Files[ASL_FILE_INPUT].Filename);
349 * AML Disassembly (Optional)
353 Status = AslDoDisassembly ();
354 if (Status != AE_CTRL_CONTINUE)
361 * Open the input file. Here, this should be an ASCII source file,
362 * either an ASL file or a Data Table file
364 Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
365 if (ACPI_FAILURE (Status))
367 AePrintErrorLog (ASL_FILE_STDERR);
371 Gbl_OriginalInputFileSize = FlGetFileSize (ASL_FILE_INPUT);
373 /* Determine input file type */
375 Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
376 if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
382 * If -p not specified, we will use the input filename as the
383 * output filename prefix
385 if (Gbl_UseDefaultAmlFilename)
387 Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
390 /* Open the optional output files (listings, etc.) */
392 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
393 if (ACPI_FAILURE (Status))
395 AePrintErrorLog (ASL_FILE_STDERR);
400 * Compilation of ASL source versus DataTable source uses different
401 * compiler subsystems
403 switch (Gbl_FileType)
406 * Data Table Compilation
408 case ASL_INPUT_TYPE_ASCII_DATA:
410 Status = DtDoCompile ();
411 if (ACPI_FAILURE (Status))
418 Gbl_Signature = NULL;
421 /* Check if any errors occurred during compile */
423 Status = AslCheckForErrorExit ();
424 if (ACPI_FAILURE (Status))
429 /* Cleanup (for next source file) and exit */
432 PrTerminatePreprocessor ();
438 case ASL_INPUT_TYPE_ASCII_ASL:
440 /* ACPICA subsystem initialization */
442 Status = AdInitialize ();
443 if (ACPI_FAILURE (Status))
448 (void) CmDoCompile ();
449 (void) AcpiTerminate ();
451 /* Check if any errors occurred during compile */
453 Status = AslCheckForErrorExit ();
454 if (ACPI_FAILURE (Status))
459 /* Cleanup (for next source file) and exit */
462 PrTerminatePreprocessor ();
464 /* ASL-to-ASL+ conversion - Perform immediate disassembly */
466 if (Gbl_DoAslConversion)
469 * New input file is the output AML file from above.
470 * New output is from the input ASL file from above.
472 Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
473 CvDbgPrint ("OUTPUTFILENAME: %s\n", Gbl_OutputFilenamePrefix);
474 Gbl_Files[ASL_FILE_INPUT].Filename =
475 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename;
477 fprintf (stderr, "\n");
480 /* delete the AML file. This AML file should never be utilized by AML interpreters. */
482 FlDeleteFile (ASL_FILE_AML_OUTPUT);
488 * Binary ACPI table was auto-detected, disassemble it
490 case ASL_INPUT_TYPE_BINARY_ACPI_TABLE:
492 /* We have what appears to be an ACPI table, disassemble it */
494 FlCloseFile (ASL_FILE_INPUT);
495 Gbl_DoCompile = FALSE;
496 Gbl_DisasmFlag = TRUE;
497 Status = AslDoDisassembly ();
500 /* Unknown binary table */
502 case ASL_INPUT_TYPE_BINARY:
504 AePrintErrorLog (ASL_FILE_STDERR);
509 printf ("Unknown file type %X\n", Gbl_FileType);
515 /*******************************************************************************
517 * FUNCTION: AslCheckForErrorExit
519 * PARAMETERS: None. Examines global exception count array
523 * DESCRIPTION: Determine if compiler should abort with error status
525 ******************************************************************************/
528 AslCheckForErrorExit (
533 * Return non-zero exit code if there have been errors, unless the
534 * global ignore error flag has been set
536 if (!Gbl_IgnoreErrors)
538 if (Gbl_ExceptionCount[ASL_ERROR] > 0)
543 /* Optionally treat warnings as errors */
545 if (Gbl_WarningsAsErrors)
547 if ((Gbl_ExceptionCount[ASL_WARNING] > 0) ||
548 (Gbl_ExceptionCount[ASL_WARNING2] > 0) ||
549 (Gbl_ExceptionCount[ASL_WARNING3] > 0))