2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2010, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #include <contrib/dev/acpica/compiler/aslcompiler.h>
119 #include "aslcompiler.y.h"
120 #include <contrib/dev/acpica/include/acdisasm.h>
121 #include <contrib/dev/acpica/include/acnamesp.h>
122 #include <contrib/dev/acpica/include/amlcode.h>
123 #include <contrib/dev/acpica/include/acapps.h>
125 #define _COMPONENT ACPI_COMPILER
126 ACPI_MODULE_NAME ("aslutils")
128 #ifdef _USE_BERKELEY_YACC
129 extern const char * const AslCompilername[];
130 static const char * const *yytname = &AslCompilername[254];
132 extern const char * const yytname[];
137 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
141 /* Local prototypes */
150 UtPadNameWithUnderscores (
152 char *PaddedNameSeg);
156 ACPI_PARSE_OBJECT *Op,
160 /*******************************************************************************
162 * FUNCTION: UtDisplaySupportedTables
168 * DESCRIPTION: Print all supported ACPI table names.
170 ******************************************************************************/
173 UtDisplaySupportedTables (
176 ACPI_DMTABLE_DATA *TableData;
180 printf ("\nACPI tables supported by iASL subsystems in "
182 " ASL and Data Table compilers\n"
183 " AML and Data Table disassemblers\n"
184 " ACPI table template generator\n\n", ACPI_CA_VERSION);
188 printf ("%8u) %s %s\n", 1, ACPI_SIG_DSDT, "Differentiated System Description Table");
189 printf ("%8u) %s %s\n", 2, ACPI_SIG_SSDT, "Secondary System Description Table");
190 printf ("%8u) %s %s\n", 3, ACPI_SIG_FADT, "Fixed ACPI Description Table (FADT)");
191 printf ("%8u) %s %s\n", 4, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
192 printf ("%8u) %s %s\n", 5, ACPI_RSDP_NAME, "Root System Description Pointer");
194 /* All data tables with common table header */
196 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
198 printf ("%8u) %s %s\n", i, TableData->Signature, TableData->Name);
204 /*******************************************************************************
206 * FUNCTION: AcpiPsDisplayConstantOpcodes
212 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
214 ******************************************************************************/
217 UtDisplayConstantOpcodes (
223 printf ("Constant expression opcode information\n\n");
225 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
227 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
229 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
235 /*******************************************************************************
237 * FUNCTION: UtLocalCalloc
239 * PARAMETERS: Size - Bytes to be allocated
241 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
243 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
244 * allocation failure, on the assumption that nothing more can be
247 ******************************************************************************/
256 Allocated = ACPI_ALLOCATE_ZEROED (Size);
259 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
260 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
261 Gbl_InputByteCount, Gbl_CurrentColumn,
262 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
269 TotalAllocated += Size;
274 /*******************************************************************************
276 * FUNCTION: UtBeginEvent
278 * PARAMETERS: Name - Ascii name of this event
280 * RETURN: Event - Event number (integer index)
282 * DESCRIPTION: Saves the current time with this event
284 ******************************************************************************/
291 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
293 AcpiOsPrintf ("Ran out of compiler event structs!\n");
294 return (AslGbl_NextEvent);
297 /* Init event with current (start) time */
299 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
300 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
301 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
303 return (AslGbl_NextEvent++);
307 /*******************************************************************************
309 * FUNCTION: UtEndEvent
311 * PARAMETERS: Event - Event number (integer index)
315 * DESCRIPTION: Saves the current time (end time) with this event
317 ******************************************************************************/
324 if (Event >= ASL_NUM_EVENTS)
329 /* Insert end time for event */
331 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
335 /*******************************************************************************
337 * FUNCTION: UtHexCharToValue
339 * PARAMETERS: HexChar - Hex character in Ascii
341 * RETURN: The binary value of the hex character
343 * DESCRIPTION: Perform ascii-to-hex translation
345 ******************************************************************************/
354 return ((UINT8) (HexChar - 0x30));
359 return ((UINT8) (HexChar - 0x37));
362 return ((UINT8) (HexChar - 0x57));
366 /*******************************************************************************
368 * FUNCTION: UtConvertByteToHex
370 * PARAMETERS: RawByte - Binary data
371 * Buffer - Pointer to where the hex bytes will be stored
373 * RETURN: Ascii hex byte is stored in Buffer.
375 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
378 ******************************************************************************/
389 Buffer[2] = (UINT8) HexLookup[(RawByte >> 4) & 0xF];
390 Buffer[3] = (UINT8) HexLookup[RawByte & 0xF];
394 /*******************************************************************************
396 * FUNCTION: UtConvertByteToAsmHex
398 * PARAMETERS: RawByte - Binary data
399 * Buffer - Pointer to where the hex bytes will be stored
401 * RETURN: Ascii hex byte is stored in Buffer.
403 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
406 ******************************************************************************/
409 UtConvertByteToAsmHex (
415 Buffer[1] = (UINT8) HexLookup[(RawByte >> 4) & 0xF];
416 Buffer[2] = (UINT8) HexLookup[RawByte & 0xF];
421 /*******************************************************************************
425 * PARAMETERS: Type - Type of output
426 * Fmt - Printf format string
427 * ... - variable printf list
431 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
434 ******************************************************************************/
445 va_start (Args, Fmt);
452 if ((Type == ASL_PARSE_OUTPUT) &&
453 (!(AslCompilerdebug)))
458 (void) vfprintf (stderr, Fmt, Args);
464 /*******************************************************************************
466 * FUNCTION: UtPrintFormattedName
468 * PARAMETERS: ParseOpcode - Parser keyword ID
469 * Level - Indentation level
473 * DESCRIPTION: Print the ascii name of the parse opcode.
475 ******************************************************************************/
477 #define TEXT_OFFSET 10
480 UtPrintFormattedName (
487 DbgPrint (ASL_TREE_OUTPUT,
488 "%*s", (3 * Level), " ");
490 DbgPrint (ASL_TREE_OUTPUT,
491 " %-20.20s", UtGetOpName (ParseOpcode));
493 if (Level < TEXT_OFFSET)
495 DbgPrint (ASL_TREE_OUTPUT,
496 "%*s", (TEXT_OFFSET - Level) * 3, " ");
501 /*******************************************************************************
503 * FUNCTION: UtSetParseOpName
509 * DESCRIPTION: Insert the ascii name of the parse opcode
511 ******************************************************************************/
515 ACPI_PARSE_OBJECT *Op)
518 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
519 ACPI_MAX_PARSEOP_NAME);
523 /*******************************************************************************
525 * FUNCTION: UtGetOpName
527 * PARAMETERS: ParseOpcode - Parser keyword ID
529 * RETURN: Pointer to the opcode name
531 * DESCRIPTION: Get the ascii name of the parse opcode
533 ******************************************************************************/
541 * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
542 * Ignore first 8 characters of the name
544 return ((char *) yytname
545 [(ParseOpcode - ASL_FIRST_PARSE_OPCODE) + ASL_YYTNAME_START] + 8);
549 /*******************************************************************************
551 * FUNCTION: UtDisplaySummary
553 * PARAMETERS: FileID - ID of outpout file
557 * DESCRIPTION: Display compilation statistics
559 ******************************************************************************/
566 if (FileId != ASL_FILE_STDOUT)
568 /* Compiler name and version number */
570 FlPrintFile (FileId, "%s version %X%s\n",
571 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH);
574 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
577 "Table Input: %s - %u lines, %u bytes, %u fields\n",
578 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
579 Gbl_InputByteCount, Gbl_InputFieldCount);
581 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
584 "Binary Output: %s - %u bytes\n\n",
585 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
590 /* Input/Output summary */
593 "ASL Input: %s - %u lines, %u bytes, %u keywords\n",
594 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
595 Gbl_InputByteCount, TotalKeywords);
599 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
602 "AML Output: %s - %u bytes, %u named objects, %u executable opcodes\n\n",
603 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
604 TotalNamedObjects, TotalExecutableOpcodes);
611 "Compilation complete. %u Errors, %u Warnings, %u Remarks",
612 Gbl_ExceptionCount[ASL_ERROR],
613 Gbl_ExceptionCount[ASL_WARNING] +
614 Gbl_ExceptionCount[ASL_WARNING2] +
615 Gbl_ExceptionCount[ASL_WARNING3],
616 Gbl_ExceptionCount[ASL_REMARK]);
618 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
621 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
624 FlPrintFile (FileId, "\n");
628 /*******************************************************************************
630 * FUNCTION: UtDisplaySummary
632 * PARAMETERS: Op - Integer parse node
633 * LowValue - Smallest allowed value
634 * HighValue - Largest allowed value
636 * RETURN: Op if OK, otherwise NULL
638 * DESCRIPTION: Check integer for an allowable range
640 ******************************************************************************/
643 UtCheckIntegerRange (
644 ACPI_PARSE_OBJECT *Op,
648 char *ParseError = NULL;
657 if (Op->Asl.Value.Integer < LowValue)
659 ParseError = "Value below valid range";
660 Op->Asl.Value.Integer = LowValue;
663 if (Op->Asl.Value.Integer > HighValue)
665 ParseError = "Value above valid range";
666 Op->Asl.Value.Integer = HighValue;
671 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
672 AslCompilererror (Buffer);
681 /*******************************************************************************
683 * FUNCTION: UtGetStringBuffer
685 * PARAMETERS: Length - Size of buffer requested
687 * RETURN: Pointer to the buffer. Aborts on allocation failure
689 * DESCRIPTION: Allocate a string buffer. Bypass the local
690 * dynamic memory manager for performance reasons (This has a
691 * major impact on the speed of the compiler.)
693 ******************************************************************************/
702 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
704 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
705 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
709 Buffer = Gbl_StringCacheNext;
710 Gbl_StringCacheNext += Length;
716 /*******************************************************************************
718 * FUNCTION: UtInternalizeName
720 * PARAMETERS: ExternalName - Name to convert
721 * ConvertedName - Where the converted name is returned
725 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
727 ******************************************************************************/
732 char **ConvertedName)
734 ACPI_NAMESTRING_INFO Info;
743 /* Get the length of the new internal name */
745 Info.ExternalName = ExternalName;
746 AcpiNsGetInternalNameLength (&Info);
748 /* We need a segment to store the internal name */
750 Info.InternalName = UtGetStringBuffer (Info.Length);
751 if (!Info.InternalName)
753 return (AE_NO_MEMORY);
758 Status = AcpiNsBuildInternalName (&Info);
759 if (ACPI_FAILURE (Status))
764 *ConvertedName = Info.InternalName;
769 /*******************************************************************************
771 * FUNCTION: UtPadNameWithUnderscores
773 * PARAMETERS: NameSeg - Input nameseg
774 * PaddedNameSeg - Output padded nameseg
776 * RETURN: Padded nameseg.
778 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
781 ******************************************************************************/
784 UtPadNameWithUnderscores (
791 for (i = 0; (i < ACPI_NAME_SIZE); i++)
795 *PaddedNameSeg = *NameSeg;
800 *PaddedNameSeg = '_';
807 /*******************************************************************************
809 * FUNCTION: UtAttachNameseg
811 * PARAMETERS: Op - Parent parse node
812 * Name - Full ExternalName
814 * RETURN: None; Sets the NameSeg field in parent node
816 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
817 * in the NameSeg field of the Op.
819 ******************************************************************************/
823 ACPI_PARSE_OBJECT *Op,
827 char PaddedNameSeg[4];
835 /* Look for the last dot in the namepath */
837 NameSeg = strrchr (Name, '.');
840 /* Found last dot, we have also found the final nameseg */
843 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
847 /* No dots in the namepath, there is only a single nameseg. */
848 /* Handle prefixes */
850 while ((*Name == '\\') || (*Name == '^'))
855 /* Remaing string should be one single nameseg */
857 UtPadNameWithUnderscores (Name, PaddedNameSeg);
860 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
864 /*******************************************************************************
866 * FUNCTION: UtAttachNamepathToOwner
868 * PARAMETERS: Op - Parent parse node
869 * NameOp - Node that contains the name
871 * RETURN: Sets the ExternalName and Namepath in the parent node
873 * DESCRIPTION: Store the name in two forms in the parent node: The original
874 * (external) name, and the internalized name that is used within
875 * the ACPI namespace manager.
877 ******************************************************************************/
880 UtAttachNamepathToOwner (
881 ACPI_PARSE_OBJECT *Op,
882 ACPI_PARSE_OBJECT *NameOp)
887 /* Full external path */
889 Op->Asl.ExternalName = NameOp->Asl.Value.String;
891 /* Save the NameOp for possible error reporting later */
893 Op->Asl.ParentMethod = (void *) NameOp;
895 /* Last nameseg of the path */
897 UtAttachNameseg (Op, Op->Asl.ExternalName);
899 /* Create internalized path */
901 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
902 if (ACPI_FAILURE (Status))
904 /* TBD: abort on no memory */
909 /*******************************************************************************
911 * FUNCTION: UtDoConstant
913 * PARAMETERS: String - Hex, Octal, or Decimal string
915 * RETURN: Converted Integer
917 * DESCRIPTION: Convert a string to an integer. With error checking.
919 ******************************************************************************/
930 Status = UtStrtoul64 (String, 0, &Converted);
931 if (ACPI_FAILURE (Status))
933 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
934 AcpiFormatException (Status));
935 AslCompilererror (ErrBuf);
942 /* TBD: use version in ACPI CA main code base? */
944 /*******************************************************************************
946 * FUNCTION: UtStrtoul64
948 * PARAMETERS: String - Null terminated string
949 * Terminater - Where a pointer to the terminating byte is
951 * Base - Radix of the string
953 * RETURN: Converted value
955 * DESCRIPTION: Convert a string into an unsigned value.
957 ******************************************************************************/
967 UINT64 ReturnValue = 0;
968 ACPI_STATUS Status = AE_OK;
983 * The specified Base parameter is not in the domain of
986 return (AE_BAD_PARAMETER);
989 /* Skip over any white space in the buffer: */
991 while (isspace ((int) *String) || *String == '\t')
997 * The buffer may contain an optional plus or minus sign.
998 * If it does, then skip over it but remember what is was:
1005 else if (*String == '+')
1016 * If the input parameter Base is zero, then we need to
1017 * determine if it is octal, decimal, or hexadecimal:
1023 if (tolower ((int) *(++String)) == 'x')
1040 * For octal and hexadecimal bases, skip over the leading
1041 * 0 or 0x, if they are present.
1043 if (Base == 8 && *String == '0')
1050 tolower ((int) *(++String)) == 'x')
1055 /* Main loop: convert the string to an unsigned long */
1059 if (isdigit ((int) *String))
1061 Index = ((UINT8) *String) - '0';
1065 Index = (UINT8) toupper ((int) *String);
1066 if (isupper ((int) Index))
1068 Index = Index - 'A' + 10;
1081 /* Check to see if value is out of range: */
1083 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1090 ReturnValue *= Base;
1091 ReturnValue += Index;
1098 /* If a minus sign was present, then "the conversion is negated": */
1100 if (Sign == NEGATIVE)
1102 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1105 *RetInteger = ReturnValue;
1113 Status = AE_BAD_OCTAL_CONSTANT;
1117 Status = AE_BAD_DECIMAL_CONSTANT;
1121 Status = AE_BAD_HEX_CONSTANT;
1125 /* Base validated above */