xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/aslmain.c (revision 2c7d7e3ca2e4f0b675c6c58e614f6aede66c678e)
1 /******************************************************************************
2  *
3  * Module Name: aslmain - compiler main and utilities
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2023, 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 MERCHANTABILITY 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:    main
80  *
81  * PARAMETERS:  Standard argc/argv
82  *
83  * RETURN:      Program termination code
84  *
85  * DESCRIPTION: C main routine for the iASL Compiler/Disassembler. Process
86  *  command line options and begin the compile/disassembly for each file on
87  *  the command line (wildcards supported).
88  *
89  ******************************************************************************/
90 
91 int ACPI_SYSTEM_XFACE
main(int argc,char ** argv)92 main (
93     int                     argc,
94     char                    **argv)
95 {
96     ACPI_STATUS             Status;
97     int                     Index1;
98     int                     Index2;
99     int                     ReturnStatus = 0;
100 
101 
102     signal (SIGINT, AslSignalHandler);
103 
104     /*
105      * Big-endian machines are not currently supported. ACPI tables must
106      * be little-endian, and support for big-endian machines needs to
107      * be implemented.
108      */
109     if (UtIsBigEndianMachine ())
110     {
111         fprintf (stderr,
112             "iASL is not currently supported on big-endian machines.\n");
113         return (-1);
114     }
115 
116     AcpiOsInitialize ();
117     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
118 
119     /* Initialize preprocessor and compiler before command line processing */
120 
121     AcpiGbl_ExternalFileList = NULL;
122     AcpiDbgLevel = 0;
123     PrInitializePreprocessor ();
124     AslInitialize ();
125 
126     Index1 = Index2 =
127         AslCommandLine (argc, argv);
128 
129     /* Allocate the line buffer(s), must be after command line */
130 
131     AslGbl_LineBufferSize /= 2;
132     UtExpandLineBuffers ();
133 
134     /* Perform global actions first/only */
135 
136     if (AslGbl_DisassembleAll)
137     {
138         while (argv[Index1])
139         {
140             Status = AcpiDmAddToExternalFileList (argv[Index1]);
141             if (ACPI_FAILURE (Status))
142             {
143                 return (-1);
144             }
145 
146             Index1++;
147         }
148     }
149 
150     /* ACPICA subsystem initialization */
151 
152     Status = AdInitialize ();
153     if (ACPI_FAILURE (Status))
154     {
155         return (Status);
156     }
157 
158 
159     /* Process each pathname/filename in the list, with possible wildcards */
160 
161     while (argv[Index2])
162     {
163         /*
164          * If -p not specified, we will use the input filename as the
165          * output filename prefix
166          */
167         if (AslGbl_UseDefaultAmlFilename)
168         {
169             AslGbl_OutputFilenamePrefix = argv[Index2];
170             UtConvertBackslashes (AslGbl_OutputFilenamePrefix);
171         }
172 
173         Status = AslDoOneFile (argv[Index2]);
174         if (ACPI_FAILURE (Status))
175         {
176             ReturnStatus = -1;
177         }
178 
179         Index2++;
180     }
181 
182     /*
183      * At this point, compilation of a data table or disassembly is complete.
184      * However, if there is a parse tree, perform compiler analysis and
185      * generate AML.
186      */
187     if (AslGbl_PreprocessOnly || AcpiGbl_DisasmFlag || !AslGbl_ParseTreeRoot)
188     {
189         goto CleanupAndExit;
190     }
191 
192     CmDoAslMiddleAndBackEnd ();
193 
194     /*
195      * At this point, all semantic analysis has been completed. Check
196      * expected error messages before cleanup or conversion.
197      */
198     AslCheckExpectedExceptions ();
199 
200     /* ASL-to-ASL+ conversion - Perform immediate disassembly */
201 
202     if (AslGbl_DoAslConversion)
203     {
204         /* re-initialize ACPICA subsystem for disassembler */
205 
206         Status = AdInitialize ();
207         if (ACPI_FAILURE (Status))
208         {
209             return (Status);
210         }
211 
212         /*
213          * New input file is the output AML file from above.
214          * New output is from the input ASL file from above.
215          */
216         AslGbl_OutputFilenamePrefix = AslGbl_Files[ASL_FILE_INPUT].Filename;
217         AslGbl_Files[ASL_FILE_INPUT].Filename =
218             AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename;
219 
220         CvDbgPrint ("Output filename: %s\n", AslGbl_OutputFilenamePrefix);
221         fprintf (stderr, "\n");
222 
223         AcpiGbl_DisasmFlag = TRUE;
224         AslDoDisassembly ();
225         AcpiGbl_DisasmFlag = FALSE;
226 
227         /* delete the AML file. This AML file should never be utilized by AML interpreters. */
228 
229         FlDeleteFile (ASL_FILE_AML_OUTPUT);
230     }
231 
232 
233 CleanupAndExit:
234 
235     UtFreeLineBuffers ();
236     AslParserCleanup ();
237     AcpiDmClearExternalFileList();
238     (void) AcpiTerminate ();
239 
240     /* CmCleanupAndExit is intended for the compiler only */
241 
242     if (!AcpiGbl_DisasmFlag)
243     {
244         ReturnStatus = CmCleanupAndExit ();
245     }
246 
247 
248     return (ReturnStatus);
249 }
250 
251 
252 /******************************************************************************
253  *
254  * FUNCTION:    AslSignalHandler
255  *
256  * PARAMETERS:  Sig                 - Signal that invoked this handler
257  *
258  * RETURN:      None
259  *
260  * DESCRIPTION: Signal interrupt handler. Delete any intermediate files and
261  *              any output files that may be left in an indeterminate state.
262  *              Currently handles SIGINT (control-c).
263  *
264  *****************************************************************************/
265 
266 static void ACPI_SYSTEM_XFACE
AslSignalHandler(int Sig)267 AslSignalHandler (
268     int                     Sig)
269 {
270     UINT32                  i;
271 
272 
273     signal (Sig, SIG_IGN);
274     fflush (stdout);
275     fflush (stderr);
276 
277     switch (Sig)
278     {
279     case SIGINT:
280 
281         printf ("\n" ASL_PREFIX "<Control-C>\n");
282         break;
283 
284     default:
285 
286         printf (ASL_PREFIX "Unknown interrupt signal (%d)\n", Sig);
287         break;
288     }
289 
290     /*
291      * Close all open files
292      * Note: the .pre file is the same as the input source file
293      */
294     if (AslGbl_Files)
295     {
296         AslGbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
297 
298         for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
299         {
300             FlCloseFile (i);
301         }
302 
303         /* Delete any output files */
304 
305         for (i = ASL_FILE_AML_OUTPUT; i < ASL_MAX_FILE_TYPE; i++)
306         {
307             FlDeleteFile (i);
308         }
309     }
310 
311     printf (ASL_PREFIX "Terminating\n");
312     exit (0);
313 }
314 
315 
316 /*******************************************************************************
317  *
318  * FUNCTION:    AslInitialize
319  *
320  * PARAMETERS:  None
321  *
322  * RETURN:      None
323  *
324  * DESCRIPTION: Initialize compiler globals
325  *
326  ******************************************************************************/
327 
328 static void
AslInitialize(void)329 AslInitialize (
330     void)
331 {
332     AcpiGbl_DmOpt_Verbose = FALSE;
333 
334     /* Default integer width is 32 bits */
335 
336     AcpiGbl_IntegerBitWidth = 32;
337     AcpiGbl_IntegerNybbleWidth = 8;
338     AcpiGbl_IntegerByteWidth = 4;
339 }
340