1118611Snjl/******************************************************************************
2118611Snjl *
3118611Snjl * Module Name: aslmain - compiler main and utilities
4118611Snjl *
5118611Snjl *****************************************************************************/
6118611Snjl
7217365Sjkim/*
8306536Sjkim * Copyright (C) 2000 - 2016, 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
54281075Sdim/*
55281075Sdim * Main routine for the iASL compiler.
56281075Sdim *
57281075Sdim * Portability note: The compiler depends upon the host for command-line
58281075Sdim * wildcard support - it is not implemented locally. For example:
59281075Sdim *
60281075Sdim * Linux/Unix systems: Shell expands wildcards automatically.
61281075Sdim *
62281075Sdim * Windows: The setargv.obj module must be linked in to automatically
63281075Sdim * expand wildcards.
64281075Sdim */
65250838Sjkim
66151937Sjkim/* Local prototypes */
67118611Snjl
68240716Sjkimstatic void ACPI_SYSTEM_XFACE
69240716SjkimAslSignalHandler (
70240716Sjkim    int                     Sig);
71240716Sjkim
72151937Sjkimstatic void
73151937SjkimAslInitialize (
74151937Sjkim    void);
75151937Sjkim
76151937Sjkim
77118611Snjl/*******************************************************************************
78118611Snjl *
79250838Sjkim * FUNCTION:    Usage
80118611Snjl *
81118611Snjl * PARAMETERS:  None
82118611Snjl *
83118611Snjl * RETURN:      None
84118611Snjl *
85246849Sjkim * DESCRIPTION: Display option help message.
86246849Sjkim *              Optional items in square brackets.
87118611Snjl *
88118611Snjl ******************************************************************************/
89118611Snjl
90250838Sjkimvoid
91250838SjkimUsage (
92118611Snjl    void)
93118611Snjl{
94250838Sjkim    printf ("%s\n\n", ASL_COMPLIANCE);
95250838Sjkim    ACPI_USAGE_HEADER ("iasl [Options] [Files]");
96118611Snjl
97281075Sdim    printf ("\nGeneral:\n");
98228110Sjkim    ACPI_OPTION ("-@ <file>",       "Specify command file");
99228110Sjkim    ACPI_OPTION ("-I <dir>",        "Specify additional include directory");
100306536Sjkim    ACPI_OPTION ("-T <sig list>|ALL",   "Create ACPI table template/example files");
101306536Sjkim    ACPI_OPTION ("-T <count>",      "Emit DSDT and <count> SSDTs to same file");
102281075Sdim    ACPI_OPTION ("-p <prefix>",     "Specify path/filename prefix for all output files");
103238381Sjkim    ACPI_OPTION ("-v",              "Display compiler version");
104281075Sdim    ACPI_OPTION ("-vo",             "Enable optimization comments");
105281075Sdim    ACPI_OPTION ("-vs",             "Disable signon");
106197104Sjkim
107281075Sdim    printf ("\nHelp:\n");
108281075Sdim    ACPI_OPTION ("-h",              "This message");
109281075Sdim    ACPI_OPTION ("-hc",             "Display operators allowed in constant expressions");
110281075Sdim    ACPI_OPTION ("-hf",             "Display help for output filename generation");
111281075Sdim    ACPI_OPTION ("-hr",             "Display ACPI reserved method names");
112281075Sdim    ACPI_OPTION ("-ht",             "Display currently supported ACPI table names");
113281075Sdim
114233250Sjkim    printf ("\nPreprocessor:\n");
115233250Sjkim    ACPI_OPTION ("-D <symbol>",     "Define symbol for preprocessor use");
116233250Sjkim    ACPI_OPTION ("-li",             "Create preprocessed output file (*.i)");
117233250Sjkim    ACPI_OPTION ("-P",              "Preprocess only and create preprocessor output file (*.i)");
118234623Sjkim    ACPI_OPTION ("-Pn",             "Disable preprocessor");
119233250Sjkim
120281075Sdim    printf ("\nErrors, Warnings, and Remarks:\n");
121281075Sdim    ACPI_OPTION ("-va",             "Disable all errors/warnings/remarks");
122281075Sdim    ACPI_OPTION ("-ve",             "Report only errors (ignore warnings and remarks)");
123228110Sjkim    ACPI_OPTION ("-vi",             "Less verbose errors and warnings for use with IDEs");
124228110Sjkim    ACPI_OPTION ("-vr",             "Disable remarks");
125250838Sjkim    ACPI_OPTION ("-vw <messageid>", "Disable specific warning or remark");
126233250Sjkim    ACPI_OPTION ("-w1 -w2 -w3",     "Set warning reporting level");
127234623Sjkim    ACPI_OPTION ("-we",             "Report warnings as errors");
128118611Snjl
129246849Sjkim    printf ("\nAML Code Generation (*.aml):\n");
130228110Sjkim    ACPI_OPTION ("-oa",             "Disable all optimizations (compatibility mode)");
131228110Sjkim    ACPI_OPTION ("-of",             "Disable constant folding");
132228110Sjkim    ACPI_OPTION ("-oi",             "Disable integer optimization to Zero/One/Ones");
133228110Sjkim    ACPI_OPTION ("-on",             "Disable named reference string optimization");
134306536Sjkim    ACPI_OPTION ("-ot",             "Disable typechecking");
135228110Sjkim    ACPI_OPTION ("-cr",             "Disable Resource Descriptor error checking");
136245582Sjkim    ACPI_OPTION ("-in",             "Ignore NoOp operators");
137228110Sjkim    ACPI_OPTION ("-r <revision>",   "Override table header Revision (1-255)");
138118611Snjl
139246849Sjkim    printf ("\nOptional Source Code Output Files:\n");
140246849Sjkim    ACPI_OPTION ("-sc -sa",         "Create source file in C or assembler (*.c or *.asm)");
141246849Sjkim    ACPI_OPTION ("-ic -ia",         "Create include file in C or assembler (*.h or *.inc)");
142246849Sjkim    ACPI_OPTION ("-tc -ta -ts",     "Create hex AML table in C, assembler, or ASL (*.hex)");
143249112Sjkim    ACPI_OPTION ("-so",             "Create offset table in C (*.offset.h)");
144246849Sjkim
145246849Sjkim    printf ("\nOptional Listing Files:\n");
146228110Sjkim    ACPI_OPTION ("-l",              "Create mixed listing file (ASL source and AML) (*.lst)");
147281075Sdim    ACPI_OPTION ("-lm",             "Create hardware summary map file (*.map)");
148228110Sjkim    ACPI_OPTION ("-ln",             "Create namespace file (*.nsp)");
149228110Sjkim    ACPI_OPTION ("-ls",             "Create combined source file (expanded includes) (*.src)");
150306536Sjkim    ACPI_OPTION ("-lx",             "Create cross-reference file (*.xrf)");
151118611Snjl
152246849Sjkim    printf ("\nData Table Compiler:\n");
153246849Sjkim    ACPI_OPTION ("-G",              "Compile custom table that contains generic operators");
154246849Sjkim    ACPI_OPTION ("-vt",             "Create verbose template files (full disassembly)");
155209746Sjkim
156118611Snjl    printf ("\nAML Disassembler:\n");
157281075Sdim    ACPI_OPTION ("-d  <f1 f2 ...>", "Disassemble or decode binary ACPI tables to file (*.dsl)");
158246849Sjkim    ACPI_OPTION ("",                "  (Optional, file type is automatically detected)");
159281075Sdim    ACPI_OPTION ("-da <f1 f2 ...>", "Disassemble multiple tables from single namespace");
160240716Sjkim    ACPI_OPTION ("-db",             "Do not translate Buffers to Resource Templates");
161281075Sdim    ACPI_OPTION ("-dc <f1 f2 ...>", "Disassemble AML and immediately compile it");
162246849Sjkim    ACPI_OPTION ("",                "  (Obtain DSDT from current system if no input file)");
163281687Sjkim    ACPI_OPTION ("-df",             "Force disassembler to assume table contains valid AML");
164281075Sdim    ACPI_OPTION ("-dl",             "Emit legacy ASL code only (no C-style operators)");
165281075Sdim    ACPI_OPTION ("-e  <f1 f2 ...>", "Include ACPI table(s) for external symbol resolution");
166254745Sjkim    ACPI_OPTION ("-fe <file>",      "Specify external symbol declaration file");
167245582Sjkim    ACPI_OPTION ("-in",             "Ignore NoOp opcodes");
168306536Sjkim    ACPI_OPTION ("-l",              "Disassemble to mixed ASL and AML code");
169241973Sjkim    ACPI_OPTION ("-vt",             "Dump binary table data in hex format within output file");
170118611Snjl
171237412Sjkim    printf ("\nDebug Options:\n");
172281075Sdim    ACPI_OPTION ("-bf",             "Create debug file (full output) (*.txt)");
173281075Sdim    ACPI_OPTION ("-bs",             "Create debug file (parse tree only) (*.txt)");
174281075Sdim    ACPI_OPTION ("-bp <depth>",     "Prune ASL parse tree");
175281075Sdim    ACPI_OPTION ("-bt <type>",      "Object type to be pruned from the parse tree");
176237412Sjkim    ACPI_OPTION ("-f",              "Ignore errors, force creation of AML output file(s)");
177246849Sjkim    ACPI_OPTION ("-m <size>",       "Set internal line buffer size (in Kbytes)");
178237412Sjkim    ACPI_OPTION ("-n",              "Parse only, no output generation");
179306536Sjkim    ACPI_OPTION ("-oc",             "Display compile times and statistics");
180237412Sjkim    ACPI_OPTION ("-x <level>",      "Set debug level for trace output");
181237412Sjkim    ACPI_OPTION ("-z",              "Do not insert new compiler ID for DataTables");
182118611Snjl}
183118611Snjl
184118611Snjl
185118611Snjl/*******************************************************************************
186118611Snjl *
187237412Sjkim * FUNCTION:    FilenameHelp
188118611Snjl *
189118611Snjl * PARAMETERS:  None
190118611Snjl *
191118611Snjl * RETURN:      None
192118611Snjl *
193237412Sjkim * DESCRIPTION: Display help message for output filename generation
194118611Snjl *
195118611Snjl ******************************************************************************/
196118611Snjl
197250838Sjkimvoid
198250838SjkimAslFilenameHelp (
199118611Snjl    void)
200118611Snjl{
201118611Snjl
202228110Sjkim    printf ("\nAML output filename generation:\n");
203118611Snjl    printf ("  Output filenames are generated by appending an extension to a common\n");
204241973Sjkim    printf ("  filename prefix. The filename prefix is obtained via one of the\n");
205118611Snjl    printf ("  following methods (in priority order):\n");
206118611Snjl    printf ("    1) The -p option specifies the prefix\n");
207118611Snjl    printf ("    2) The prefix of the AMLFileName in the ASL Definition Block\n");
208118611Snjl    printf ("    3) The prefix of the input filename\n");
209118611Snjl    printf ("\n");
210118611Snjl}
211118611Snjl
212118611Snjl
213240716Sjkim/******************************************************************************
214240716Sjkim *
215240716Sjkim * FUNCTION:    AslSignalHandler
216240716Sjkim *
217240716Sjkim * PARAMETERS:  Sig                 - Signal that invoked this handler
218240716Sjkim *
219240716Sjkim * RETURN:      None
220240716Sjkim *
221240716Sjkim * DESCRIPTION: Control-C handler. Delete any intermediate files and any
222240716Sjkim *              output files that may be left in an indeterminate state.
223240716Sjkim *
224240716Sjkim *****************************************************************************/
225240716Sjkim
226240716Sjkimstatic void ACPI_SYSTEM_XFACE
227240716SjkimAslSignalHandler (
228240716Sjkim    int                     Sig)
229240716Sjkim{
230240716Sjkim    UINT32                  i;
231240716Sjkim
232240716Sjkim
233240716Sjkim    signal (Sig, SIG_IGN);
234240716Sjkim    printf ("Aborting\n\n");
235240716Sjkim
236240716Sjkim    /* Close all open files */
237240716Sjkim
238306536Sjkim    Gbl_Files[ASL_FILE_PREPROCESSOR].Handle = NULL; /* the .pre file is same as source file */
239240716Sjkim
240240716Sjkim    for (i = ASL_FILE_INPUT; i < ASL_MAX_FILE_TYPE; i++)
241240716Sjkim    {
242240716Sjkim        FlCloseFile (i);
243240716Sjkim    }
244240716Sjkim
245240716Sjkim    /* Delete any output files */
246240716Sjkim
247240716Sjkim    for (i = ASL_FILE_AML_OUTPUT; i < ASL_MAX_FILE_TYPE; i++)
248240716Sjkim    {
249240716Sjkim        FlDeleteFile (i);
250240716Sjkim    }
251240716Sjkim
252240716Sjkim    exit (0);
253240716Sjkim}
254240716Sjkim
255240716Sjkim
256118611Snjl/*******************************************************************************
257118611Snjl *
258118611Snjl * FUNCTION:    AslInitialize
259118611Snjl *
260118611Snjl * PARAMETERS:  None
261118611Snjl *
262118611Snjl * RETURN:      None
263118611Snjl *
264118611Snjl * DESCRIPTION: Initialize compiler globals
265118611Snjl *
266118611Snjl ******************************************************************************/
267118611Snjl
268151937Sjkimstatic void
269151937SjkimAslInitialize (
270151937Sjkim    void)
271118611Snjl{
272118611Snjl    UINT32                  i;
273118611Snjl
274118611Snjl
275306536Sjkim    AcpiGbl_DmOpt_Verbose = FALSE;
276306536Sjkim
277306536Sjkim    /* Default integer width is 64 bits */
278306536Sjkim
279306536Sjkim    AcpiGbl_IntegerBitWidth = 64;
280306536Sjkim    AcpiGbl_IntegerNybbleWidth = 16;
281306536Sjkim    AcpiGbl_IntegerByteWidth = 8;
282306536Sjkim
283118611Snjl    for (i = 0; i < ASL_NUM_FILES; i++)
284118611Snjl    {
285118611Snjl        Gbl_Files[i].Handle = NULL;
286118611Snjl        Gbl_Files[i].Filename = NULL;
287118611Snjl    }
288118611Snjl
289118611Snjl    Gbl_Files[ASL_FILE_STDOUT].Handle   = stdout;
290118611Snjl    Gbl_Files[ASL_FILE_STDOUT].Filename = "STDOUT";
291118611Snjl
292118611Snjl    Gbl_Files[ASL_FILE_STDERR].Handle   = stderr;
293118611Snjl    Gbl_Files[ASL_FILE_STDERR].Filename = "STDERR";
294118611Snjl}
295118611Snjl
296118611Snjl
297118611Snjl/*******************************************************************************
298118611Snjl *
299118611Snjl * FUNCTION:    main
300118611Snjl *
301118611Snjl * PARAMETERS:  Standard argc/argv
302118611Snjl *
303118611Snjl * RETURN:      Program termination code
304118611Snjl *
305193529Sjkim * DESCRIPTION: C main routine for the Asl Compiler. Handle command line
306193529Sjkim *              options and begin the compile for each file on the command line
307118611Snjl *
308118611Snjl ******************************************************************************/
309118611Snjl
310118611Snjlint ACPI_SYSTEM_XFACE
311118611Snjlmain (
312118611Snjl    int                     argc,
313118611Snjl    char                    **argv)
314118611Snjl{
315118611Snjl    ACPI_STATUS             Status;
316210976Sjkim    int                     Index1;
317210976Sjkim    int                     Index2;
318281075Sdim    int                     ReturnStatus = 0;
319118611Snjl
320118611Snjl
321281075Sdim    /*
322281075Sdim     * Big-endian machines are not currently supported. ACPI tables must
323281075Sdim     * be little-endian, and support for big-endian machines needs to
324281075Sdim     * be implemented.
325281075Sdim     */
326306536Sjkim    if (UtIsBigEndianMachine ())
327281075Sdim    {
328281075Sdim        fprintf (stderr,
329281075Sdim            "iASL is not currently supported on big-endian machines.\n");
330281075Sdim        return (-1);
331281075Sdim    }
332281075Sdim
333281075Sdim    AcpiOsInitialize ();
334250838Sjkim    ACPI_DEBUG_INITIALIZE (); /* For debug version only */
335250838Sjkim
336250838Sjkim    /* Initialize preprocessor and compiler before command line processing */
337250838Sjkim
338240716Sjkim    signal (SIGINT, AslSignalHandler);
339210976Sjkim    AcpiGbl_ExternalFileList = NULL;
340240716Sjkim    AcpiDbgLevel = 0;
341250838Sjkim    PrInitializePreprocessor ();
342250838Sjkim    AslInitialize ();
343210976Sjkim
344250838Sjkim    Index1 = Index2 = AslCommandLine (argc, argv);
345151937Sjkim
346250838Sjkim    /* Allocate the line buffer(s), must be after command line */
347118611Snjl
348250838Sjkim    Gbl_LineBufferSize /= 2;
349250838Sjkim    UtExpandLineBuffers ();
350240716Sjkim
351250838Sjkim    /* Perform global actions first/only */
352118611Snjl
353210976Sjkim    if (Gbl_DisassembleAll)
354210976Sjkim    {
355210976Sjkim        while (argv[Index1])
356210976Sjkim        {
357281075Sdim            Status = AcpiDmAddToExternalFileList (argv[Index1]);
358210976Sjkim            if (ACPI_FAILURE (Status))
359210976Sjkim            {
360210976Sjkim                return (-1);
361210976Sjkim            }
362210976Sjkim
363210976Sjkim            Index1++;
364210976Sjkim        }
365210976Sjkim    }
366210976Sjkim
367193529Sjkim    /* Process each pathname/filename in the list, with possible wildcards */
368118611Snjl
369210976Sjkim    while (argv[Index2])
370118611Snjl    {
371281075Sdim        /*
372281075Sdim         * If -p not specified, we will use the input filename as the
373281075Sdim         * output filename prefix
374281075Sdim         */
375281075Sdim        if (Gbl_UseDefaultAmlFilename)
376281075Sdim        {
377281075Sdim            Gbl_OutputFilenamePrefix = argv[Index2];
378281075Sdim            UtConvertBackslashes (Gbl_OutputFilenamePrefix);
379281075Sdim        }
380281075Sdim
381281075Sdim        Status = AslDoOneFile (argv[Index2]);
382151937Sjkim        if (ACPI_FAILURE (Status))
383151937Sjkim        {
384281075Sdim            ReturnStatus = -1;
385281075Sdim            goto CleanupAndExit;
386151937Sjkim        }
387118611Snjl
388210976Sjkim        Index2++;
389118611Snjl    }
390118611Snjl
391281075Sdim
392281075SdimCleanupAndExit:
393281075Sdim
394281075Sdim    UtFreeLineBuffers ();
395281075Sdim    AslParserCleanup ();
396281075Sdim
397210976Sjkim    if (AcpiGbl_ExternalFileList)
398210976Sjkim    {
399210976Sjkim        AcpiDmClearExternalFileList();
400210976Sjkim    }
401210976Sjkim
402281075Sdim    return (ReturnStatus);
403118611Snjl}
404