xref: /dflybsd-src/sys/contrib/dev/acpica/source/compiler/aslmain.c (revision fc7e83fa167ace550636f25b513c873f2f3135ff)
1 /******************************************************************************
2  *
3  * Module Name: aslmain - compiler main and utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2014, Intel Corp.
9  * All rights reserved.
10  *
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.
25  *
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.
29  *
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.
42  */
43 
44 #define _DECLARE_GLOBALS
45 
46 #include "aslcompiler.h"
47 #include "acapps.h"
48 #include "acdisasm.h"
49 #include <signal.h>
50 
51 #define _COMPONENT          ACPI_COMPILER
52         ACPI_MODULE_NAME    ("aslmain")
53 
54 /*
55  * Main routine for the iASL compiler.
56  *
57  * Portability note: The compiler depends upon the host for command-line
58  * wildcard support - it is not implemented locally. For example:
59  *
60  * Linux/Unix systems: Shell expands wildcards automatically.
61  *
62  * Windows: The setargv.obj module must be linked in to automatically
63  * expand wildcards.
64  */
65 
66 /* Local prototypes */
67 
68 static void ACPI_SYSTEM_XFACE
69 AslSignalHandler (
70     int                     Sig);
71 
72 static void
73 AslInitialize (
74     void);
75 
76 
77 /*******************************************************************************
78  *
79  * FUNCTION:    Usage
80  *
81  * PARAMETERS:  None
82  *
83  * RETURN:      None
84  *
85  * DESCRIPTION: Display option help message.
86  *              Optional items in square brackets.
87  *
88  ******************************************************************************/
89 
90 void
91 Usage (
92     void)
93 {
94     printf ("%s\n\n", ASL_COMPLIANCE);
95     ACPI_USAGE_HEADER ("iasl [Options] [Files]");
96 
97     printf ("\nGeneral:\n");
98     ACPI_OPTION ("-@ <file>",       "Specify command file");
99     ACPI_OPTION ("-I <dir>",        "Specify additional include directory");
100     ACPI_OPTION ("-T <sig>|ALL|*",  "Create table template file for ACPI <Sig>");
101     ACPI_OPTION ("-p <prefix>",     "Specify path/filename prefix for all output files");
102     ACPI_OPTION ("-v",              "Display compiler version");
103     ACPI_OPTION ("-vo",             "Enable optimization comments");
104     ACPI_OPTION ("-vs",             "Disable signon");
105 
106     printf ("\nHelp:\n");
107     ACPI_OPTION ("-h",              "This message");
108     ACPI_OPTION ("-hc",             "Display operators allowed in constant expressions");
109     ACPI_OPTION ("-hf",             "Display help for output filename generation");
110     ACPI_OPTION ("-hr",             "Display ACPI reserved method names");
111     ACPI_OPTION ("-ht",             "Display currently supported ACPI table names");
112 
113     printf ("\nPreprocessor:\n");
114     ACPI_OPTION ("-D <symbol>",     "Define symbol for preprocessor use");
115     ACPI_OPTION ("-li",             "Create preprocessed output file (*.i)");
116     ACPI_OPTION ("-P",              "Preprocess only and create preprocessor output file (*.i)");
117     ACPI_OPTION ("-Pn",             "Disable preprocessor");
118 
119     printf ("\nErrors, Warnings, and Remarks:\n");
120     ACPI_OPTION ("-va",             "Disable all errors/warnings/remarks");
121     ACPI_OPTION ("-ve",             "Report only errors (ignore warnings and remarks)");
122     ACPI_OPTION ("-vi",             "Less verbose errors and warnings for use with IDEs");
123     ACPI_OPTION ("-vr",             "Disable remarks");
124     ACPI_OPTION ("-vw <messageid>", "Disable specific warning or remark");
125     ACPI_OPTION ("-w1 -w2 -w3",     "Set warning reporting level");
126     ACPI_OPTION ("-we",             "Report warnings as errors");
127 
128     printf ("\nAML Code Generation (*.aml):\n");
129     ACPI_OPTION ("-oa",             "Disable all optimizations (compatibility mode)");
130     ACPI_OPTION ("-of",             "Disable constant folding");
131     ACPI_OPTION ("-oi",             "Disable integer optimization to Zero/One/Ones");
132     ACPI_OPTION ("-on",             "Disable named reference string optimization");
133     ACPI_OPTION ("-cr",             "Disable Resource Descriptor error checking");
134     ACPI_OPTION ("-in",             "Ignore NoOp operators");
135     ACPI_OPTION ("-r <revision>",   "Override table header Revision (1-255)");
136 
137     printf ("\nOptional Source Code Output Files:\n");
138     ACPI_OPTION ("-sc -sa",         "Create source file in C or assembler (*.c or *.asm)");
139     ACPI_OPTION ("-ic -ia",         "Create include file in C or assembler (*.h or *.inc)");
140     ACPI_OPTION ("-tc -ta -ts",     "Create hex AML table in C, assembler, or ASL (*.hex)");
141     ACPI_OPTION ("-so",             "Create offset table in C (*.offset.h)");
142 
143     printf ("\nOptional Listing Files:\n");
144     ACPI_OPTION ("-l",              "Create mixed listing file (ASL source and AML) (*.lst)");
145     ACPI_OPTION ("-ln",             "Create namespace file (*.nsp)");
146     ACPI_OPTION ("-ls",             "Create combined source file (expanded includes) (*.src)");
147 
148     printf ("\nData Table Compiler:\n");
149     ACPI_OPTION ("-G",              "Compile custom table that contains generic operators");
150     ACPI_OPTION ("-vt",             "Create verbose template files (full disassembly)");
151 
152     printf ("\nAML Disassembler:\n");
153     ACPI_OPTION ("-d  <f1 f2 ...>", "Disassemble or decode binary ACPI tables to file (*.dsl)");
154     ACPI_OPTION ("",                "  (Optional, file type is automatically detected)");
155     ACPI_OPTION ("-da <f1 f2 ...>", "Disassemble multiple tables from single namespace");
156     ACPI_OPTION ("-db",             "Do not translate Buffers to Resource Templates");
157     ACPI_OPTION ("-dc <f1 f2 ...>", "Disassemble AML and immediately compile it");
158     ACPI_OPTION ("",                "  (Obtain DSDT from current system if no input file)");
159     ACPI_OPTION ("-e  <f1 f2 ...>", "Include ACPI table(s) for external symbol resolution");
160     ACPI_OPTION ("-fe <file>",      "Specify external symbol declaration file");
161     ACPI_OPTION ("-in",             "Ignore NoOp opcodes");
162     ACPI_OPTION ("-vt",             "Dump binary table data in hex format within output file");
163 
164     printf ("\nDebug Options:\n");
165     ACPI_OPTION ("-bf -bt",         "Create debug file (full or parse tree only) (*.txt)");
166     ACPI_OPTION ("-f",              "Ignore errors, force creation of AML output file(s)");
167     ACPI_OPTION ("-m <size>",       "Set internal line buffer size (in Kbytes)");
168     ACPI_OPTION ("-n",              "Parse only, no output generation");
169     ACPI_OPTION ("-ot",             "Display compile times and statistics");
170     ACPI_OPTION ("-x <level>",      "Set debug level for trace output");
171     ACPI_OPTION ("-z",              "Do not insert new compiler ID for DataTables");
172 }
173 
174 
175 /*******************************************************************************
176  *
177  * FUNCTION:    FilenameHelp
178  *
179  * PARAMETERS:  None
180  *
181  * RETURN:      None
182  *
183  * DESCRIPTION: Display help message for output filename generation
184  *
185  ******************************************************************************/
186 
187 void
188 AslFilenameHelp (
189     void)
190 {
191 
192     printf ("\nAML output filename generation:\n");
193     printf ("  Output filenames are generated by appending an extension to a common\n");
194     printf ("  filename prefix. The filename prefix is obtained via one of the\n");
195     printf ("  following methods (in priority order):\n");
196     printf ("    1) The -p option specifies the prefix\n");
197     printf ("    2) The prefix of the AMLFileName in the ASL Definition Block\n");
198     printf ("    3) The prefix of the input filename\n");
199     printf ("\n");
200 }
201 
202 
203 /******************************************************************************
204  *
205  * FUNCTION:    AslSignalHandler
206  *
207  * PARAMETERS:  Sig                 - Signal that invoked this handler
208  *
209  * RETURN:      None
210  *
211  * DESCRIPTION: Control-C handler. Delete any intermediate files and any
212  *              output files that may be left in an indeterminate state.
213  *
214  *****************************************************************************/
215 
216 static void ACPI_SYSTEM_XFACE
217 AslSignalHandler (
218     int                     Sig)
219 {
220     UINT32                  i;
221 
222 
223     signal (Sig, SIG_IGN);
224     printf ("Aborting\n\n");
225 
226     /* Close all open files */
227 
228     Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL; /* the .i file is same as source file */
229 
230     for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
231     {
232         FlCloseFile (i);
233     }
234 
235     /* Delete any output files */
236 
237     for (i = ASL_FILE_AML_OUTPUT; i < ASL_MAX_FILE_TYPE; i++)
238     {
239         FlDeleteFile (i);
240     }
241 
242     exit (0);
243 }
244 
245 
246 /*******************************************************************************
247  *
248  * FUNCTION:    AslInitialize
249  *
250  * PARAMETERS:  None
251  *
252  * RETURN:      None
253  *
254  * DESCRIPTION: Initialize compiler globals
255  *
256  ******************************************************************************/
257 
258 static void
259 AslInitialize (
260     void)
261 {
262     UINT32                  i;
263 
264 
265     for (i = 0; i < ASL_NUM_FILES; i++)
266     {
267         Gbl_Files[i].Handle = NULL;
268         Gbl_Files[i].Filename = NULL;
269     }
270 
271     Gbl_Files[ASL_FILE_STDOUT].Handle   = stdout;
272     Gbl_Files[ASL_FILE_STDOUT].Filename = "STDOUT";
273 
274     Gbl_Files[ASL_FILE_STDERR].Handle   = stderr;
275     Gbl_Files[ASL_FILE_STDERR].Filename = "STDERR";
276 }
277 
278 
279 /*******************************************************************************
280  *
281  * FUNCTION:    main
282  *
283  * PARAMETERS:  Standard argc/argv
284  *
285  * RETURN:      Program termination code
286  *
287  * DESCRIPTION: C main routine for the Asl Compiler. Handle command line
288  *              options and begin the compile for each file on the command line
289  *
290  ******************************************************************************/
291 
292 int ACPI_SYSTEM_XFACE
293 main (
294     int                     argc,
295     char                    **argv)
296 {
297     ACPI_STATUS             Status;
298     int                     Index1;
299     int                     Index2;
300 
301 
302     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
303 
304     /* Initialize preprocessor and compiler before command line processing */
305 
306     signal (SIGINT, AslSignalHandler);
307     AcpiGbl_ExternalFileList = NULL;
308     AcpiDbgLevel = 0;
309     PrInitializePreprocessor ();
310     AslInitialize ();
311 
312     Index1 = Index2 = AslCommandLine (argc, argv);
313 
314     /* Allocate the line buffer(s), must be after command line */
315 
316     Gbl_LineBufferSize /= 2;
317     UtExpandLineBuffers ();
318 
319     /* Perform global actions first/only */
320 
321     if (Gbl_DisassembleAll)
322     {
323         while (argv[Index1])
324         {
325             Status = AcpiDmAddToExternalFileList (argv[Index1]);
326             if (ACPI_FAILURE (Status))
327             {
328                 return (-1);
329             }
330 
331             Index1++;
332         }
333     }
334 
335     /* Process each pathname/filename in the list, with possible wildcards */
336 
337     while (argv[Index2])
338     {
339         /*
340          * If -p not specified, we will use the input filename as the
341          * output filename prefix
342          */
343         if (Gbl_UseDefaultAmlFilename)
344         {
345             Gbl_OutputFilenamePrefix = argv[Index2];
346             UtConvertBackslashes (Gbl_OutputFilenamePrefix);
347         }
348 
349         Status = AslDoOneFile (argv[Index2]);
350         if (ACPI_FAILURE (Status))
351         {
352             return (-1);
353         }
354 
355         Index2++;
356     }
357 
358     if (AcpiGbl_ExternalFileList)
359     {
360         AcpiDmClearExternalFileList();
361     }
362 
363     return (0);
364 }
365