xref: /netbsd-src/sys/external/bsd/acpica/dist/compiler/aslmain.c (revision 2c7d7e3ca2e4f0b675c6c58e614f6aede66c678e)
128c506b8Sjruoho /******************************************************************************
228c506b8Sjruoho  *
328c506b8Sjruoho  * Module Name: aslmain - compiler main and utilities
428c506b8Sjruoho  *
528c506b8Sjruoho  *****************************************************************************/
628c506b8Sjruoho 
7124f4c82Sjruoho /*
8*2c7d7e3cSchristos  * Copyright (C) 2000 - 2023, Intel Corp.
928c506b8Sjruoho  * All rights reserved.
1028c506b8Sjruoho  *
11124f4c82Sjruoho  * Redistribution and use in source and binary forms, with or without
12124f4c82Sjruoho  * modification, are permitted provided that the following conditions
13124f4c82Sjruoho  * are met:
14124f4c82Sjruoho  * 1. Redistributions of source code must retain the above copyright
15124f4c82Sjruoho  *    notice, this list of conditions, and the following disclaimer,
16124f4c82Sjruoho  *    without modification.
17124f4c82Sjruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18124f4c82Sjruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
19124f4c82Sjruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
20124f4c82Sjruoho  *    including a substantially similar Disclaimer requirement for further
21124f4c82Sjruoho  *    binary redistribution.
22124f4c82Sjruoho  * 3. Neither the names of the above-listed copyright holders nor the names
23124f4c82Sjruoho  *    of any contributors may be used to endorse or promote products derived
24124f4c82Sjruoho  *    from this software without specific prior written permission.
2528c506b8Sjruoho  *
26124f4c82Sjruoho  * Alternatively, this software may be distributed under the terms of the
27124f4c82Sjruoho  * GNU General Public License ("GPL") version 2 as published by the Free
28124f4c82Sjruoho  * Software Foundation.
2928c506b8Sjruoho  *
30124f4c82Sjruoho  * NO WARRANTY
31124f4c82Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32124f4c82Sjruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3346a330b4Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34124f4c82Sjruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35124f4c82Sjruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36124f4c82Sjruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37124f4c82Sjruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38124f4c82Sjruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39124f4c82Sjruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40124f4c82Sjruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41124f4c82Sjruoho  * POSSIBILITY OF SUCH DAMAGES.
42124f4c82Sjruoho  */
4328c506b8Sjruoho 
4428c506b8Sjruoho #define _DECLARE_GLOBALS
4528c506b8Sjruoho 
4628c506b8Sjruoho #include "aslcompiler.h"
4728c506b8Sjruoho #include "acapps.h"
48124f4c82Sjruoho #include "acdisasm.h"
49ff4a156dSchristos #include <signal.h>
5028c506b8Sjruoho 
5128c506b8Sjruoho #define _COMPONENT          ACPI_COMPILER
5228c506b8Sjruoho         ACPI_MODULE_NAME    ("aslmain")
5328c506b8Sjruoho 
54ff4a156dSchristos /*
55ff4a156dSchristos  * Main routine for the iASL compiler.
56ff4a156dSchristos  *
57ff4a156dSchristos  * Portability note: The compiler depends upon the host for command-line
58ff4a156dSchristos  * wildcard support - it is not implemented locally. For example:
59ff4a156dSchristos  *
60ff4a156dSchristos  * Linux/Unix systems: Shell expands wildcards automatically.
61ff4a156dSchristos  *
62ff4a156dSchristos  * Windows: The setargv.obj module must be linked in to automatically
63ff4a156dSchristos  * expand wildcards.
64ff4a156dSchristos  */
65ff4a156dSchristos 
6628c506b8Sjruoho /* Local prototypes */
6728c506b8Sjruoho 
68ff4a156dSchristos static void ACPI_SYSTEM_XFACE
69ff4a156dSchristos AslSignalHandler (
70ff4a156dSchristos     int                     Sig);
7128c506b8Sjruoho 
7228c506b8Sjruoho static void
7328c506b8Sjruoho AslInitialize (
7428c506b8Sjruoho     void);
7528c506b8Sjruoho 
7628c506b8Sjruoho 
7728c506b8Sjruoho /*******************************************************************************
7828c506b8Sjruoho  *
79d0e1da26Schristos  * FUNCTION:    main
8028c506b8Sjruoho  *
81d0e1da26Schristos  * PARAMETERS:  Standard argc/argv
8228c506b8Sjruoho  *
83d0e1da26Schristos  * RETURN:      Program termination code
8428c506b8Sjruoho  *
85d0e1da26Schristos  * DESCRIPTION: C main routine for the iASL Compiler/Disassembler. Process
86d0e1da26Schristos  *  command line options and begin the compile/disassembly for each file on
87d0e1da26Schristos  *  the command line (wildcards supported).
8828c506b8Sjruoho  *
8928c506b8Sjruoho  ******************************************************************************/
9028c506b8Sjruoho 
91d0e1da26Schristos int ACPI_SYSTEM_XFACE
main(int argc,char ** argv)92d0e1da26Schristos main (
93d0e1da26Schristos     int                     argc,
94d0e1da26Schristos     char                    **argv)
9528c506b8Sjruoho {
96d0e1da26Schristos     ACPI_STATUS             Status;
97d0e1da26Schristos     int                     Index1;
98d0e1da26Schristos     int                     Index2;
99d0e1da26Schristos     int                     ReturnStatus = 0;
10028c506b8Sjruoho 
101ff4a156dSchristos 
10289b8eb6cSchristos     signal (SIGINT, AslSignalHandler);
10389b8eb6cSchristos 
104d0e1da26Schristos     /*
105d0e1da26Schristos      * Big-endian machines are not currently supported. ACPI tables must
106d0e1da26Schristos      * be little-endian, and support for big-endian machines needs to
107d0e1da26Schristos      * be implemented.
108d0e1da26Schristos      */
109d0e1da26Schristos     if (UtIsBigEndianMachine ())
110d0e1da26Schristos     {
111d0e1da26Schristos         fprintf (stderr,
112d0e1da26Schristos             "iASL is not currently supported on big-endian machines.\n");
113d0e1da26Schristos         return (-1);
114d0e1da26Schristos     }
115ff4a156dSchristos 
116d0e1da26Schristos     AcpiOsInitialize ();
117d0e1da26Schristos     ACPI_DEBUG_INITIALIZE (); /* For debug version only */
118ff4a156dSchristos 
119d0e1da26Schristos     /* Initialize preprocessor and compiler before command line processing */
120ff4a156dSchristos 
121d0e1da26Schristos     AcpiGbl_ExternalFileList = NULL;
122d0e1da26Schristos     AcpiDbgLevel = 0;
123d0e1da26Schristos     PrInitializePreprocessor ();
124d0e1da26Schristos     AslInitialize ();
125ff4a156dSchristos 
126d0e1da26Schristos     Index1 = Index2 =
127d0e1da26Schristos         AslCommandLine (argc, argv);
128ff4a156dSchristos 
129d0e1da26Schristos     /* Allocate the line buffer(s), must be after command line */
130ff4a156dSchristos 
1314c4e8184Schristos     AslGbl_LineBufferSize /= 2;
132d0e1da26Schristos     UtExpandLineBuffers ();
133ff4a156dSchristos 
134d0e1da26Schristos     /* Perform global actions first/only */
135ff4a156dSchristos 
1364c4e8184Schristos     if (AslGbl_DisassembleAll)
137d0e1da26Schristos     {
138d0e1da26Schristos         while (argv[Index1])
139d0e1da26Schristos         {
140d0e1da26Schristos             Status = AcpiDmAddToExternalFileList (argv[Index1]);
141d0e1da26Schristos             if (ACPI_FAILURE (Status))
142d0e1da26Schristos             {
143d0e1da26Schristos                 return (-1);
144d0e1da26Schristos             }
145d0e1da26Schristos 
146d0e1da26Schristos             Index1++;
147d0e1da26Schristos         }
148d0e1da26Schristos     }
149d0e1da26Schristos 
15094783addSchristos     /* ACPICA subsystem initialization */
15194783addSchristos 
15294783addSchristos     Status = AdInitialize ();
15394783addSchristos     if (ACPI_FAILURE (Status))
15494783addSchristos     {
15594783addSchristos         return (Status);
15694783addSchristos     }
15794783addSchristos 
15889b8eb6cSchristos 
159d0e1da26Schristos     /* Process each pathname/filename in the list, with possible wildcards */
160d0e1da26Schristos 
161d0e1da26Schristos     while (argv[Index2])
162d0e1da26Schristos     {
163d0e1da26Schristos         /*
164d0e1da26Schristos          * If -p not specified, we will use the input filename as the
165d0e1da26Schristos          * output filename prefix
166d0e1da26Schristos          */
1674c4e8184Schristos         if (AslGbl_UseDefaultAmlFilename)
168d0e1da26Schristos         {
1694c4e8184Schristos             AslGbl_OutputFilenamePrefix = argv[Index2];
1704c4e8184Schristos             UtConvertBackslashes (AslGbl_OutputFilenamePrefix);
171d0e1da26Schristos         }
172d0e1da26Schristos 
173d0e1da26Schristos         Status = AslDoOneFile (argv[Index2]);
174d0e1da26Schristos         if (ACPI_FAILURE (Status))
175d0e1da26Schristos         {
176d0e1da26Schristos             ReturnStatus = -1;
177d0e1da26Schristos         }
178d0e1da26Schristos 
179d0e1da26Schristos         Index2++;
180ff4a156dSchristos     }
181ff4a156dSchristos 
18294783addSchristos     /*
18394783addSchristos      * At this point, compilation of a data table or disassembly is complete.
184783af925Schristos      * However, if there is a parse tree, perform compiler analysis and
185783af925Schristos      * generate AML.
18694783addSchristos      */
187783af925Schristos     if (AslGbl_PreprocessOnly || AcpiGbl_DisasmFlag || !AslGbl_ParseTreeRoot)
18894783addSchristos     {
18994783addSchristos         goto CleanupAndExit;
19094783addSchristos     }
19194783addSchristos 
19294783addSchristos     CmDoAslMiddleAndBackEnd ();
19394783addSchristos 
19494783addSchristos     /*
19594783addSchristos      * At this point, all semantic analysis has been completed. Check
19694783addSchristos      * expected error messages before cleanup or conversion.
19794783addSchristos      */
19894783addSchristos     AslCheckExpectedExceptions ();
19994783addSchristos 
20094783addSchristos     /* ASL-to-ASL+ conversion - Perform immediate disassembly */
20194783addSchristos 
20294783addSchristos     if (AslGbl_DoAslConversion)
20394783addSchristos     {
20494783addSchristos         /* re-initialize ACPICA subsystem for disassembler */
20594783addSchristos 
20694783addSchristos         Status = AdInitialize ();
20794783addSchristos         if (ACPI_FAILURE (Status))
20894783addSchristos         {
20994783addSchristos             return (Status);
21094783addSchristos         }
21194783addSchristos 
21294783addSchristos         /*
21394783addSchristos          * New input file is the output AML file from above.
21494783addSchristos          * New output is from the input ASL file from above.
21594783addSchristos          */
21694783addSchristos         AslGbl_OutputFilenamePrefix = AslGbl_Files[ASL_FILE_INPUT].Filename;
21794783addSchristos         AslGbl_Files[ASL_FILE_INPUT].Filename =
21894783addSchristos             AslGbl_Files[ASL_FILE_AML_OUTPUT].Filename;
21994783addSchristos 
22094783addSchristos         CvDbgPrint ("Output filename: %s\n", AslGbl_OutputFilenamePrefix);
22194783addSchristos         fprintf (stderr, "\n");
22294783addSchristos 
22394783addSchristos         AcpiGbl_DisasmFlag = TRUE;
22494783addSchristos         AslDoDisassembly ();
22594783addSchristos         AcpiGbl_DisasmFlag = FALSE;
22694783addSchristos 
22794783addSchristos         /* delete the AML file. This AML file should never be utilized by AML interpreters. */
22894783addSchristos 
22994783addSchristos         FlDeleteFile (ASL_FILE_AML_OUTPUT);
23094783addSchristos     }
23194783addSchristos 
23294783addSchristos 
233d0e1da26Schristos CleanupAndExit:
234ff4a156dSchristos 
235d0e1da26Schristos     UtFreeLineBuffers ();
236d0e1da26Schristos     AslParserCleanup ();
237d0e1da26Schristos     AcpiDmClearExternalFileList();
23894783addSchristos     (void) AcpiTerminate ();
23994783addSchristos 
24094783addSchristos     /* CmCleanupAndExit is intended for the compiler only */
24194783addSchristos 
24294783addSchristos     if (!AcpiGbl_DisasmFlag)
24394783addSchristos     {
244783af925Schristos         ReturnStatus = CmCleanupAndExit ();
245d0e1da26Schristos     }
246ff4a156dSchristos 
24794783addSchristos 
248d0e1da26Schristos     return (ReturnStatus);
249ff4a156dSchristos }
250ff4a156dSchristos 
251ff4a156dSchristos 
252ff4a156dSchristos /******************************************************************************
253ff4a156dSchristos  *
254ff4a156dSchristos  * FUNCTION:    AslSignalHandler
255ff4a156dSchristos  *
256ff4a156dSchristos  * PARAMETERS:  Sig                 - Signal that invoked this handler
257ff4a156dSchristos  *
258ff4a156dSchristos  * RETURN:      None
259ff4a156dSchristos  *
26089b8eb6cSchristos  * DESCRIPTION: Signal interrupt handler. Delete any intermediate files and
26189b8eb6cSchristos  *              any output files that may be left in an indeterminate state.
26247315524Schristos  *              Currently handles SIGINT (control-c).
263ff4a156dSchristos  *
264ff4a156dSchristos  *****************************************************************************/
265ff4a156dSchristos 
266ff4a156dSchristos static void ACPI_SYSTEM_XFACE
AslSignalHandler(int Sig)267ff4a156dSchristos AslSignalHandler (
268ff4a156dSchristos     int                     Sig)
269ff4a156dSchristos {
270ff4a156dSchristos     UINT32                  i;
271ff4a156dSchristos 
272ff4a156dSchristos 
273ff4a156dSchristos     signal (Sig, SIG_IGN);
27489b8eb6cSchristos     fflush (stdout);
27589b8eb6cSchristos     fflush (stderr);
276ff4a156dSchristos 
27789b8eb6cSchristos     switch (Sig)
27889b8eb6cSchristos     {
27989b8eb6cSchristos     case SIGINT:
280ff4a156dSchristos 
28189b8eb6cSchristos         printf ("\n" ASL_PREFIX "<Control-C>\n");
28289b8eb6cSchristos         break;
28389b8eb6cSchristos 
28489b8eb6cSchristos     default:
28589b8eb6cSchristos 
2864c4e8184Schristos         printf (ASL_PREFIX "Unknown interrupt signal (%d)\n", Sig);
28747315524Schristos         break;
28889b8eb6cSchristos     }
28989b8eb6cSchristos 
29089b8eb6cSchristos     /*
29189b8eb6cSchristos      * Close all open files
29289b8eb6cSchristos      * Note: the .pre file is the same as the input source file
29389b8eb6cSchristos      */
294783af925Schristos     if (AslGbl_Files)
295783af925Schristos     {
2964c4e8184Schristos         AslGbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL;
297ff4a156dSchristos 
298ff4a156dSchristos         for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
299ff4a156dSchristos         {
300ff4a156dSchristos             FlCloseFile (i);
301ff4a156dSchristos         }
302ff4a156dSchristos 
303ff4a156dSchristos         /* Delete any output files */
304ff4a156dSchristos 
305ff4a156dSchristos         for (i = ASL_FILE_AML_OUTPUT; i < ASL_MAX_FILE_TYPE; i++)
306ff4a156dSchristos         {
307ff4a156dSchristos             FlDeleteFile (i);
308ff4a156dSchristos         }
309783af925Schristos     }
310ff4a156dSchristos 
31189b8eb6cSchristos     printf (ASL_PREFIX "Terminating\n");
312bae73670Schristos     exit (0);
31328c506b8Sjruoho }
31428c506b8Sjruoho 
31528c506b8Sjruoho 
31628c506b8Sjruoho /*******************************************************************************
31728c506b8Sjruoho  *
31828c506b8Sjruoho  * FUNCTION:    AslInitialize
31928c506b8Sjruoho  *
32028c506b8Sjruoho  * PARAMETERS:  None
32128c506b8Sjruoho  *
32228c506b8Sjruoho  * RETURN:      None
32328c506b8Sjruoho  *
32428c506b8Sjruoho  * DESCRIPTION: Initialize compiler globals
32528c506b8Sjruoho  *
32628c506b8Sjruoho  ******************************************************************************/
32728c506b8Sjruoho 
32828c506b8Sjruoho static void
AslInitialize(void)32928c506b8Sjruoho AslInitialize (
33028c506b8Sjruoho     void)
33128c506b8Sjruoho {
33271e38f1dSchristos     AcpiGbl_DmOpt_Verbose = FALSE;
33371e38f1dSchristos 
33489b8eb6cSchristos     /* Default integer width is 32 bits */
335cfbb7280Schristos 
33689b8eb6cSchristos     AcpiGbl_IntegerBitWidth = 32;
33789b8eb6cSchristos     AcpiGbl_IntegerNybbleWidth = 8;
33889b8eb6cSchristos     AcpiGbl_IntegerByteWidth = 4;
33928c506b8Sjruoho }
340