1118611Snjl/******************************************************************************
2118611Snjl *
3118611Snjl * Module Name: aslmain - compiler main and utilities
4118611Snjl *
5118611Snjl *****************************************************************************/
6118611Snjl
7217365Sjkim/*
8245582Sjkim * Copyright (C) 2000 - 2013, Intel Corp.
9118611Snjl * All rights reserved.
10118611Snjl *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
25118611Snjl *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
29118611Snjl *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
43118611Snjl
44118611Snjl#define _DECLARE_GLOBALS
45118611Snjl
46151937Sjkim#include <contrib/dev/acpica/compiler/aslcompiler.h>
47193529Sjkim#include <contrib/dev/acpica/include/acapps.h>
48210976Sjkim#include <contrib/dev/acpica/include/acdisasm.h>
49240716Sjkim#include <signal.h>
50118611Snjl
51118611Snjl#define _COMPONENT          ACPI_COMPILER
52118611Snjl        ACPI_MODULE_NAME    ("aslmain")
53118611Snjl
54250838Sjkim
55151937Sjkim/* Local prototypes */
56118611Snjl
57240716Sjkimstatic void ACPI_SYSTEM_XFACE
58240716SjkimAslSignalHandler (
59240716Sjkim    int                     Sig);
60240716Sjkim
61151937Sjkimstatic void
62151937SjkimAslInitialize (
63151937Sjkim    void);
64151937Sjkim
65151937Sjkim
66118611Snjl/*******************************************************************************
67118611Snjl *
68250838Sjkim * FUNCTION:    Usage
69118611Snjl *
70118611Snjl * PARAMETERS:  None
71118611Snjl *
72118611Snjl * RETURN:      None
73118611Snjl *
74246849Sjkim * DESCRIPTION: Display option help message.
75246849Sjkim *              Optional items in square brackets.
76118611Snjl *
77118611Snjl ******************************************************************************/
78118611Snjl
79250838Sjkimvoid
80250838SjkimUsage (
81118611Snjl    void)
82118611Snjl{
83250838Sjkim    printf ("%s\n\n", ASL_COMPLIANCE);
84250838Sjkim    ACPI_USAGE_HEADER ("iasl [Options] [Files]");
85118611Snjl
86228110Sjkim    printf ("\nGlobal:\n");
87228110Sjkim    ACPI_OPTION ("-@ <file>",       "Specify command file");
88228110Sjkim    ACPI_OPTION ("-I <dir>",        "Specify additional include directory");
89241973Sjkim    ACPI_OPTION ("-T <sig>|ALL|*",  "Create table template file for ACPI <Sig>");
90238381Sjkim    ACPI_OPTION ("-v",              "Display compiler version");
91197104Sjkim
92233250Sjkim    printf ("\nPreprocessor:\n");
93233250Sjkim    ACPI_OPTION ("-D <symbol>",     "Define symbol for preprocessor use");
94233250Sjkim    ACPI_OPTION ("-li",             "Create preprocessed output file (*.i)");
95233250Sjkim    ACPI_OPTION ("-P",              "Preprocess only and create preprocessor output file (*.i)");
96234623Sjkim    ACPI_OPTION ("-Pn",             "Disable preprocessor");
97233250Sjkim
98246849Sjkim    printf ("\nGeneral Processing:\n");
99228110Sjkim    ACPI_OPTION ("-p <prefix>",     "Specify path/filename prefix for all output files");
100228110Sjkim    ACPI_OPTION ("-va",             "Disable all errors and warnings (summary only)");
101228110Sjkim    ACPI_OPTION ("-vi",             "Less verbose errors and warnings for use with IDEs");
102228110Sjkim    ACPI_OPTION ("-vo",             "Enable optimization comments");
103228110Sjkim    ACPI_OPTION ("-vr",             "Disable remarks");
104228110Sjkim    ACPI_OPTION ("-vs",             "Disable signon");
105250838Sjkim    ACPI_OPTION ("-vw <messageid>", "Disable specific warning or remark");
106233250Sjkim    ACPI_OPTION ("-w1 -w2 -w3",     "Set warning reporting level");
107234623Sjkim    ACPI_OPTION ("-we",             "Report warnings as errors");
108118611Snjl
109246849Sjkim    printf ("\nAML Code Generation (*.aml):\n");
110228110Sjkim    ACPI_OPTION ("-oa",             "Disable all optimizations (compatibility mode)");
111228110Sjkim    ACPI_OPTION ("-of",             "Disable constant folding");
112228110Sjkim    ACPI_OPTION ("-oi",             "Disable integer optimization to Zero/One/Ones");
113228110Sjkim    ACPI_OPTION ("-on",             "Disable named reference string optimization");
114228110Sjkim    ACPI_OPTION ("-cr",             "Disable Resource Descriptor error checking");
115245582Sjkim    ACPI_OPTION ("-in",             "Ignore NoOp operators");
116228110Sjkim    ACPI_OPTION ("-r <revision>",   "Override table header Revision (1-255)");
117118611Snjl
118246849Sjkim    printf ("\nOptional Source Code Output Files:\n");
119246849Sjkim    ACPI_OPTION ("-sc -sa",         "Create source file in C or assembler (*.c or *.asm)");
120246849Sjkim    ACPI_OPTION ("-ic -ia",         "Create include file in C or assembler (*.h or *.inc)");
121246849Sjkim    ACPI_OPTION ("-tc -ta -ts",     "Create hex AML table in C, assembler, or ASL (*.hex)");
122249112Sjkim    ACPI_OPTION ("-so",             "Create offset table in C (*.offset.h)");
123246849Sjkim
124246849Sjkim    printf ("\nOptional Listing Files:\n");
125228110Sjkim    ACPI_OPTION ("-l",              "Create mixed listing file (ASL source and AML) (*.lst)");
126228110Sjkim    ACPI_OPTION ("-ln",             "Create namespace file (*.nsp)");
127228110Sjkim    ACPI_OPTION ("-ls",             "Create combined source file (expanded includes) (*.src)");
128118611Snjl
129246849Sjkim    printf ("\nData Table Compiler:\n");
130246849Sjkim    ACPI_OPTION ("-G",              "Compile custom table that contains generic operators");
131246849Sjkim    ACPI_OPTION ("-vt",             "Create verbose template files (full disassembly)");
132209746Sjkim
133118611Snjl    printf ("\nAML Disassembler:\n");
134246849Sjkim    ACPI_OPTION ("-d  <f1,f2>",     "Disassemble or decode binary ACPI tables to file (*.dsl)");
135246849Sjkim    ACPI_OPTION ("",                "  (Optional, file type is automatically detected)");
136246849Sjkim    ACPI_OPTION ("-da <f1,f2>",     "Disassemble multiple tables from single namespace");
137240716Sjkim    ACPI_OPTION ("-db",             "Do not translate Buffers to Resource Templates");
138246849Sjkim    ACPI_OPTION ("-dc <f1,f2>",     "Disassemble AML and immediately compile it");
139246849Sjkim    ACPI_OPTION ("",                "  (Obtain DSDT from current system if no input file)");
140246849Sjkim    ACPI_OPTION ("-e  <f1,f2>",     "Include ACPI table(s) for external symbol resolution");
141254745Sjkim    ACPI_OPTION ("-fe <file>",      "Specify external symbol declaration file");
142228110Sjkim    ACPI_OPTION ("-g",              "Get ACPI tables and write to files (*.dat)");
143245582Sjkim    ACPI_OPTION ("-in",             "Ignore NoOp opcodes");
144241973Sjkim    ACPI_OPTION ("-vt",             "Dump binary table data in hex format within output file");
145118611Snjl
146118611Snjl    printf ("\nHelp:\n");
147237412Sjkim    ACPI_OPTION ("-h",              "This message");
148228110Sjkim    ACPI_OPTION ("-hc",             "Display operators allowed in constant expressions");
149237412Sjkim    ACPI_OPTION ("-hf",             "Display help for output filename generation");
150228110Sjkim    ACPI_OPTION ("-hr",             "Display ACPI reserved method names");
151228110Sjkim    ACPI_OPTION ("-ht",             "Display currently supported ACPI table names");
152237412Sjkim
153237412Sjkim    printf ("\nDebug Options:\n");
154237412Sjkim    ACPI_OPTION ("-bf -bt",         "Create debug file (full or parse tree only) (*.txt)");
155237412Sjkim    ACPI_OPTION ("-f",              "Ignore errors, force creation of AML output file(s)");
156246849Sjkim    ACPI_OPTION ("-m <size>",       "Set internal line buffer size (in Kbytes)");
157237412Sjkim    ACPI_OPTION ("-n",              "Parse only, no output generation");
158237412Sjkim    ACPI_OPTION ("-ot",             "Display compile times and statistics");
159237412Sjkim    ACPI_OPTION ("-x <level>",      "Set debug level for trace output");
160237412Sjkim    ACPI_OPTION ("-z",              "Do not insert new compiler ID for DataTables");
161118611Snjl}
162118611Snjl
163118611Snjl
164118611Snjl/*******************************************************************************
165118611Snjl *
166237412Sjkim * FUNCTION:    FilenameHelp
167118611Snjl *
168118611Snjl * PARAMETERS:  None
169118611Snjl *
170118611Snjl * RETURN:      None
171118611Snjl *
172237412Sjkim * DESCRIPTION: Display help message for output filename generation
173118611Snjl *
174118611Snjl ******************************************************************************/
175118611Snjl
176250838Sjkimvoid
177250838SjkimAslFilenameHelp (
178118611Snjl    void)
179118611Snjl{
180118611Snjl
181228110Sjkim    printf ("\nAML output filename generation:\n");
182118611Snjl    printf ("  Output filenames are generated by appending an extension to a common\n");
183241973Sjkim    printf ("  filename prefix. The filename prefix is obtained via one of the\n");
184118611Snjl    printf ("  following methods (in priority order):\n");
185118611Snjl    printf ("    1) The -p option specifies the prefix\n");
186118611Snjl    printf ("    2) The prefix of the AMLFileName in the ASL Definition Block\n");
187118611Snjl    printf ("    3) The prefix of the input filename\n");
188118611Snjl    printf ("\n");
189118611Snjl}
190118611Snjl
191118611Snjl
192240716Sjkim/******************************************************************************
193240716Sjkim *
194240716Sjkim * FUNCTION:    AslSignalHandler
195240716Sjkim *
196240716Sjkim * PARAMETERS:  Sig                 - Signal that invoked this handler
197240716Sjkim *
198240716Sjkim * RETURN:      None
199240716Sjkim *
200240716Sjkim * DESCRIPTION: Control-C handler. Delete any intermediate files and any
201240716Sjkim *              output files that may be left in an indeterminate state.
202240716Sjkim *
203240716Sjkim *****************************************************************************/
204240716Sjkim
205240716Sjkimstatic void ACPI_SYSTEM_XFACE
206240716SjkimAslSignalHandler (
207240716Sjkim    int                     Sig)
208240716Sjkim{
209240716Sjkim    UINT32                  i;
210240716Sjkim
211240716Sjkim
212240716Sjkim    signal (Sig, SIG_IGN);
213240716Sjkim    printf ("Aborting\n\n");
214240716Sjkim
215240716Sjkim    /* Close all open files */
216240716Sjkim
217240716Sjkim    Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL; /* the .i file is same as source file */
218240716Sjkim
219240716Sjkim    for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
220240716Sjkim    {
221240716Sjkim        FlCloseFile (i);
222240716Sjkim    }
223240716Sjkim
224240716Sjkim    /* Delete any output files */
225240716Sjkim
226240716Sjkim    for (i = ASL_FILE_AML_OUTPUT; i < ASL_MAX_FILE_TYPE; i++)
227240716Sjkim    {
228240716Sjkim        FlDeleteFile (i);
229240716Sjkim    }
230240716Sjkim
231240716Sjkim    exit (0);
232240716Sjkim}
233240716Sjkim
234240716Sjkim
235118611Snjl/*******************************************************************************
236118611Snjl *
237118611Snjl * FUNCTION:    AslInitialize
238118611Snjl *
239118611Snjl * PARAMETERS:  None
240118611Snjl *
241118611Snjl * RETURN:      None
242118611Snjl *
243118611Snjl * DESCRIPTION: Initialize compiler globals
244118611Snjl *
245118611Snjl ******************************************************************************/
246118611Snjl
247151937Sjkimstatic void
248151937SjkimAslInitialize (
249151937Sjkim    void)
250118611Snjl{
251118611Snjl    UINT32                  i;
252118611Snjl
253118611Snjl
254118611Snjl    for (i = 0; i < ASL_NUM_FILES; i++)
255118611Snjl    {
256118611Snjl        Gbl_Files[i].Handle = NULL;
257118611Snjl        Gbl_Files[i].Filename = NULL;
258118611Snjl    }
259118611Snjl
260118611Snjl    Gbl_Files[ASL_FILE_STDOUT].Handle   = stdout;
261118611Snjl    Gbl_Files[ASL_FILE_STDOUT].Filename = "STDOUT";
262118611Snjl
263118611Snjl    Gbl_Files[ASL_FILE_STDERR].Handle   = stderr;
264118611Snjl    Gbl_Files[ASL_FILE_STDERR].Filename = "STDERR";
265118611Snjl}
266118611Snjl
267118611Snjl
268118611Snjl/*******************************************************************************
269118611Snjl *
270118611Snjl * FUNCTION:    main
271118611Snjl *
272118611Snjl * PARAMETERS:  Standard argc/argv
273118611Snjl *
274118611Snjl * RETURN:      Program termination code
275118611Snjl *
276193529Sjkim * DESCRIPTION: C main routine for the Asl Compiler. Handle command line
277193529Sjkim *              options and begin the compile for each file on the command line
278118611Snjl *
279118611Snjl ******************************************************************************/
280118611Snjl
281118611Snjlint ACPI_SYSTEM_XFACE
282118611Snjlmain (
283118611Snjl    int                     argc,
284118611Snjl    char                    **argv)
285118611Snjl{
286118611Snjl    ACPI_STATUS             Status;
287210976Sjkim    int                     Index1;
288210976Sjkim    int                     Index2;
289118611Snjl
290118611Snjl
291250838Sjkim    ACPI_DEBUG_INITIALIZE (); /* For debug version only */
292250838Sjkim
293250838Sjkim    /* Initialize preprocessor and compiler before command line processing */
294250838Sjkim
295240716Sjkim    signal (SIGINT, AslSignalHandler);
296210976Sjkim    AcpiGbl_ExternalFileList = NULL;
297240716Sjkim    AcpiDbgLevel = 0;
298250838Sjkim    PrInitializePreprocessor ();
299250838Sjkim    AslInitialize ();
300210976Sjkim
301250838Sjkim    Index1 = Index2 = AslCommandLine (argc, argv);
302151937Sjkim
303250838Sjkim    /* Allocate the line buffer(s), must be after command line */
304118611Snjl
305250838Sjkim    Gbl_LineBufferSize /= 2;
306250838Sjkim    UtExpandLineBuffers ();
307240716Sjkim
308250838Sjkim    /* Perform global actions first/only */
309118611Snjl
310193529Sjkim    if (Gbl_GetAllTables)
311118611Snjl    {
312193529Sjkim        Status = AslDoOneFile (NULL);
313118611Snjl        if (ACPI_FAILURE (Status))
314118611Snjl        {
315193529Sjkim            return (-1);
316118611Snjl        }
317193529Sjkim        return (0);
318193529Sjkim    }
319118611Snjl
320210976Sjkim    if (Gbl_DisassembleAll)
321210976Sjkim    {
322210976Sjkim        while (argv[Index1])
323210976Sjkim        {
324210976Sjkim            Status = AslDoOnePathname (argv[Index1], AcpiDmAddToExternalFileList);
325210976Sjkim            if (ACPI_FAILURE (Status))
326210976Sjkim            {
327210976Sjkim                return (-1);
328210976Sjkim            }
329210976Sjkim
330210976Sjkim            Index1++;
331210976Sjkim        }
332210976Sjkim    }
333210976Sjkim
334193529Sjkim    /* Process each pathname/filename in the list, with possible wildcards */
335118611Snjl
336210976Sjkim    while (argv[Index2])
337118611Snjl    {
338210976Sjkim        Status = AslDoOnePathname (argv[Index2], AslDoOneFile);
339151937Sjkim        if (ACPI_FAILURE (Status))
340151937Sjkim        {
341193529Sjkim            return (-1);
342151937Sjkim        }
343118611Snjl
344210976Sjkim        Index2++;
345118611Snjl    }
346118611Snjl
347210976Sjkim    if (AcpiGbl_ExternalFileList)
348210976Sjkim    {
349210976Sjkim        AcpiDmClearExternalFileList();
350210976Sjkim    }
351210976Sjkim
352118611Snjl    return (0);
353118611Snjl}
354