Sync ACPICA with Intel's version 20160831.
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / aslstartup.c
blobdf90ae5fe7f0f916083c287253b082e8a8e24789
1 /******************************************************************************
3 * Module Name: aslstartup - Compiler startup routines, called from main
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2016, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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"
45 #include "actables.h"
46 #include "acdisasm.h"
47 #include "acapps.h"
49 #define _COMPONENT ACPI_COMPILER
50 ACPI_MODULE_NAME ("aslstartup")
53 /* Local prototypes */
55 static UINT8
56 AslDetectSourceFileType (
57 ASL_FILE_INFO *Info);
59 static ACPI_STATUS
60 AslDoDisassembly (
61 void);
64 /* Globals */
66 static BOOLEAN AslToFile = TRUE;
69 /*******************************************************************************
71 * FUNCTION: AslInitializeGlobals
73 * PARAMETERS: None
75 * RETURN: None
77 * DESCRIPTION: Re-initialize globals needed to restart the compiler. This
78 * allows multiple files to be disassembled and/or compiled.
80 ******************************************************************************/
82 void
83 AslInitializeGlobals (
84 void)
86 UINT32 i;
89 /* Init compiler globals */
91 Gbl_SyntaxError = 0;
92 Gbl_CurrentColumn = 0;
93 Gbl_CurrentLineNumber = 1;
94 Gbl_LogicalLineNumber = 1;
95 Gbl_CurrentLineOffset = 0;
96 Gbl_InputFieldCount = 0;
97 Gbl_InputByteCount = 0;
98 Gbl_NsLookupCount = 0;
99 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
101 Gbl_ErrorLog = NULL;
102 Gbl_NextError = NULL;
103 Gbl_Signature = NULL;
104 Gbl_FileType = 0;
106 TotalExecutableOpcodes = 0;
107 TotalNamedObjects = 0;
108 TotalKeywords = 0;
109 TotalParseNodes = 0;
110 TotalMethods = 0;
111 TotalAllocations = 0;
112 TotalAllocated = 0;
113 TotalFolds = 0;
115 AslGbl_NextEvent = 0;
116 for (i = 0; i < ASL_NUM_REPORT_LEVELS; i++)
118 Gbl_ExceptionCount[i] = 0;
121 for (i = ASL_FILE_INPUT; i <= ASL_MAX_FILE_TYPE; i++)
123 Gbl_Files[i].Handle = NULL;
124 Gbl_Files[i].Filename = NULL;
129 /*******************************************************************************
131 * FUNCTION: AslDetectSourceFileType
133 * PARAMETERS: Info - Name/Handle for the file (must be open)
135 * RETURN: File Type
137 * DESCRIPTION: Determine the type of the input file. Either binary (contains
138 * non-ASCII characters), ASL file, or an ACPI Data Table file.
140 ******************************************************************************/
142 static UINT8
143 AslDetectSourceFileType (
144 ASL_FILE_INFO *Info)
146 char *FileChar;
147 UINT8 Type = ASL_INPUT_TYPE_ASCII_DATA; /* default */
148 ACPI_STATUS Status;
151 /* Check for 100% ASCII source file (comments are ignored) */
153 Status = FlIsFileAsciiSource (Info->Filename, FALSE);
154 if (ACPI_SUCCESS (Status))
157 * File contains ASCII source code. Determine if this is an ASL
158 * file or an ACPI data table file.
160 while (fgets (Gbl_CurrentLineBuffer, Gbl_LineBufferSize, Info->Handle))
162 /* Uppercase the buffer for caseless compare */
164 FileChar = Gbl_CurrentLineBuffer;
165 while (*FileChar)
167 *FileChar = (char) toupper ((int) *FileChar);
168 FileChar++;
171 /* Presence of "DefinitionBlock" indicates actual ASL code */
173 if (strstr (Gbl_CurrentLineBuffer, "DEFINITIONBLOCK"))
175 /* Appears to be an ASL file */
177 Type = ASL_INPUT_TYPE_ASCII_ASL;
178 goto Cleanup;
182 /* Appears to be an ASCII data table source file */
184 Type = ASL_INPUT_TYPE_ASCII_DATA;
185 goto Cleanup;
188 /* We have some sort of binary table, check for valid ACPI table */
190 fseek (Info->Handle, 0, SEEK_SET);
192 Status = AcValidateTableHeader (Info->Handle, 0);
193 if (ACPI_SUCCESS (Status))
195 fprintf (stderr,
196 "Binary file appears to be a valid ACPI table, disassembling\n");
198 Type = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
199 goto Cleanup;
202 Type = ASL_INPUT_TYPE_BINARY;
205 Cleanup:
207 /* Must seek back to the start of the file */
209 fseek (Info->Handle, 0, SEEK_SET);
210 return (Type);
214 /*******************************************************************************
216 * FUNCTION: AslDoDisassembly
218 * PARAMETERS: None
220 * RETURN: Status
222 * DESCRIPTION: Initiate AML file disassembly. Uses ACPICA subsystem to build
223 * namespace.
225 ******************************************************************************/
227 static ACPI_STATUS
228 AslDoDisassembly (
229 void)
231 ACPI_STATUS Status;
234 /* ACPICA subsystem initialization */
236 Status = AdInitialize ();
237 if (ACPI_FAILURE (Status))
239 return (Status);
242 Status = AcpiAllocateRootTable (4);
243 if (ACPI_FAILURE (Status))
245 AcpiOsPrintf ("Could not initialize ACPI Table Manager, %s\n",
246 AcpiFormatException (Status));
247 return (Status);
250 /* Handle additional output files for disassembler */
252 Gbl_FileType = ASL_INPUT_TYPE_BINARY_ACPI_TABLE;
253 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
255 /* This is where the disassembly happens */
257 AcpiGbl_DmOpt_Disasm = TRUE;
258 Status = AdAmlDisassemble (AslToFile,
259 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_OutputFilenamePrefix,
260 &Gbl_Files[ASL_FILE_INPUT].Filename);
261 if (ACPI_FAILURE (Status))
263 return (Status);
266 /* Check if any control methods were unresolved */
268 AcpiDmUnresolvedWarning (0);
270 /* Shutdown compiler and ACPICA subsystem */
272 AeClearErrorLog ();
273 (void) AcpiTerminate ();
276 * Gbl_Files[ASL_FILE_INPUT].Filename was replaced with the
277 * .DSL disassembly file, which can now be compiled if requested
279 if (Gbl_DoCompile)
281 AcpiOsPrintf ("\nCompiling \"%s\"\n",
282 Gbl_Files[ASL_FILE_INPUT].Filename);
283 return (AE_CTRL_CONTINUE);
286 /* No need to free the filename string */
288 Gbl_Files[ASL_FILE_INPUT].Filename = NULL;
290 CmDeleteCaches ();
291 return (AE_OK);
295 /*******************************************************************************
297 * FUNCTION: AslDoOneFile
299 * PARAMETERS: Filename - Name of the file
301 * RETURN: Status
303 * DESCRIPTION: Process a single file - either disassemble, compile, or both
305 ******************************************************************************/
307 ACPI_STATUS
308 AslDoOneFile (
309 char *Filename)
311 ACPI_STATUS Status;
314 /* Re-initialize "some" compiler/preprocessor globals */
316 AslInitializeGlobals ();
317 PrInitializeGlobals ();
320 * Extract the directory path. This path is used for possible include
321 * files and the optional AML filename embedded in the input file
322 * DefinitionBlock declaration.
324 Status = FlSplitInputPathname (Filename, &Gbl_DirectoryPath, NULL);
325 if (ACPI_FAILURE (Status))
327 return (Status);
330 /* Take a copy of the input filename, convert any backslashes */
332 Gbl_Files[ASL_FILE_INPUT].Filename =
333 UtStringCacheCalloc (strlen (Filename) + 1);
335 strcpy (Gbl_Files[ASL_FILE_INPUT].Filename, Filename);
336 UtConvertBackslashes (Gbl_Files[ASL_FILE_INPUT].Filename);
339 * AML Disassembly (Optional)
341 if (Gbl_DisasmFlag)
343 Status = AslDoDisassembly ();
344 if (Status != AE_CTRL_CONTINUE)
346 return (Status);
351 * Open the input file. Here, this should be an ASCII source file,
352 * either an ASL file or a Data Table file
354 Status = FlOpenInputFile (Gbl_Files[ASL_FILE_INPUT].Filename);
355 if (ACPI_FAILURE (Status))
357 AePrintErrorLog (ASL_FILE_STDERR);
358 return (AE_ERROR);
361 Gbl_OriginalInputFileSize = FlGetFileSize (ASL_FILE_INPUT);
363 /* Determine input file type */
365 Gbl_FileType = AslDetectSourceFileType (&Gbl_Files[ASL_FILE_INPUT]);
366 if (Gbl_FileType == ASL_INPUT_TYPE_BINARY)
368 return (AE_ERROR);
372 * If -p not specified, we will use the input filename as the
373 * output filename prefix
375 if (Gbl_UseDefaultAmlFilename)
377 Gbl_OutputFilenamePrefix = Gbl_Files[ASL_FILE_INPUT].Filename;
380 /* Open the optional output files (listings, etc.) */
382 Status = FlOpenMiscOutputFiles (Gbl_OutputFilenamePrefix);
383 if (ACPI_FAILURE (Status))
385 AePrintErrorLog (ASL_FILE_STDERR);
386 return (AE_ERROR);
390 * Compilation of ASL source versus DataTable source uses different
391 * compiler subsystems
393 switch (Gbl_FileType)
396 * Data Table Compilation
398 case ASL_INPUT_TYPE_ASCII_DATA:
400 Status = DtDoCompile ();
401 if (ACPI_FAILURE (Status))
403 return (Status);
406 if (Gbl_Signature)
408 Gbl_Signature = NULL;
411 /* Check if any errors occurred during compile */
413 Status = AslCheckForErrorExit ();
414 if (ACPI_FAILURE (Status))
416 return (Status);
419 /* Cleanup (for next source file) and exit */
421 AeClearErrorLog ();
422 PrTerminatePreprocessor ();
423 return (Status);
426 * ASL Compilation
428 case ASL_INPUT_TYPE_ASCII_ASL:
430 /* ACPICA subsystem initialization */
432 Status = AdInitialize ();
433 if (ACPI_FAILURE (Status))
435 return (Status);
438 (void) CmDoCompile ();
439 (void) AcpiTerminate ();
441 /* Check if any errors occurred during compile */
443 Status = AslCheckForErrorExit ();
444 if (ACPI_FAILURE (Status))
446 return (Status);
449 /* Cleanup (for next source file) and exit */
451 AeClearErrorLog ();
452 PrTerminatePreprocessor ();
453 return (AE_OK);
456 * Binary ACPI table was auto-detected, disassemble it
458 case ASL_INPUT_TYPE_BINARY_ACPI_TABLE:
460 /* We have what appears to be an ACPI table, disassemble it */
462 FlCloseFile (ASL_FILE_INPUT);
463 Gbl_DoCompile = FALSE;
464 Gbl_DisasmFlag = TRUE;
465 Status = AslDoDisassembly ();
466 return (Status);
468 /* Unknown binary table */
470 case ASL_INPUT_TYPE_BINARY:
472 AePrintErrorLog (ASL_FILE_STDERR);
473 return (AE_ERROR);
475 default:
477 printf ("Unknown file type %X\n", Gbl_FileType);
478 return (AE_ERROR);
483 /*******************************************************************************
485 * FUNCTION: AslCheckForErrorExit
487 * PARAMETERS: None. Examines global exception count array
489 * RETURN: Status
491 * DESCRIPTION: Determine if compiler should abort with error status
493 ******************************************************************************/
495 ACPI_STATUS
496 AslCheckForErrorExit (
497 void)
501 * Return non-zero exit code if there have been errors, unless the
502 * global ignore error flag has been set
504 if (!Gbl_IgnoreErrors)
506 if (Gbl_ExceptionCount[ASL_ERROR] > 0)
508 return (AE_ERROR);
511 /* Optionally treat warnings as errors */
513 if (Gbl_WarningsAsErrors)
515 if ((Gbl_ExceptionCount[ASL_WARNING] > 0) ||
516 (Gbl_ExceptionCount[ASL_WARNING2] > 0) ||
517 (Gbl_ExceptionCount[ASL_WARNING3] > 0))
519 return (AE_ERROR);
524 return (AE_OK);