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>
124 #define _COMPONENT ACPI_COMPILER
125 ACPI_MODULE_NAME ("aslutils")
127 #ifdef _USE_BERKELEY_YACC
128 extern const char * const AslCompilername[];
129 static const char * const *yytname = &AslCompilername[254];
131 extern const char * const yytname[];
136 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
140 /* Local prototypes */
149 UtPadNameWithUnderscores (
151 char *PaddedNameSeg);
155 ACPI_PARSE_OBJECT *Op,
159 /*******************************************************************************
161 * FUNCTION: UtDisplaySupportedTables
167 * DESCRIPTION: Print all supported ACPI table names.
169 ******************************************************************************/
172 UtDisplaySupportedTables (
175 ACPI_DMTABLE_DATA *TableData;
179 printf ("\nACPI tables supported by iASL subsystems in "
181 " ASL and Data Table compilers\n"
182 " AML and Data Table disassemblers\n"
183 " ACPI table template generator\n\n", ACPI_CA_VERSION);
187 printf ("%8u) %s %s\n", 1, ACPI_SIG_DSDT, "Differentiated System Description Table");
188 printf ("%8u) %s %s\n", 2, ACPI_SIG_SSDT, "Secondary System Description Table");
189 printf ("%8u) %s %s\n", 3, ACPI_SIG_FADT, "Fixed ACPI Description Table (FADT)");
190 printf ("%8u) %s %s\n", 4, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
191 printf ("%8u) %s %s\n", 5, ACPI_RSDP_NAME, "Root System Description Pointer");
193 /* All data tables with common table header */
195 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
197 printf ("%8u) %s %s\n", i, TableData->Signature, TableData->Name);
203 /*******************************************************************************
205 * FUNCTION: AcpiPsDisplayConstantOpcodes
211 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
213 ******************************************************************************/
216 UtDisplayConstantOpcodes (
222 printf ("Constant expression opcode information\n\n");
224 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
226 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
228 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
234 /*******************************************************************************
236 * FUNCTION: UtLocalCalloc
238 * PARAMETERS: Size - Bytes to be allocated
240 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
242 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
243 * allocation failure, on the assumption that nothing more can be
246 ******************************************************************************/
255 Allocated = ACPI_ALLOCATE_ZEROED (Size);
258 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
259 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
260 Gbl_InputByteCount, Gbl_CurrentColumn,
261 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
268 TotalAllocated += Size;
273 /*******************************************************************************
275 * FUNCTION: UtBeginEvent
277 * PARAMETERS: Name - Ascii name of this event
279 * RETURN: Event - Event number (integer index)
281 * DESCRIPTION: Saves the current time with this event
283 ******************************************************************************/
290 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
292 AcpiOsPrintf ("Ran out of compiler event structs!\n");
293 return (AslGbl_NextEvent);
296 /* Init event with current (start) time */
298 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
299 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
300 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
302 return (AslGbl_NextEvent++);
306 /*******************************************************************************
308 * FUNCTION: UtEndEvent
310 * PARAMETERS: Event - Event number (integer index)
314 * DESCRIPTION: Saves the current time (end time) with this event
316 ******************************************************************************/
323 if (Event >= ASL_NUM_EVENTS)
328 /* Insert end time for event */
330 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
334 /*******************************************************************************
336 * FUNCTION: UtHexCharToValue
338 * PARAMETERS: HexChar - Hex character in Ascii
340 * RETURN: The binary value of the hex character
342 * DESCRIPTION: Perform ascii-to-hex translation
344 ******************************************************************************/
353 return ((UINT8) (HexChar - 0x30));
358 return ((UINT8) (HexChar - 0x37));
361 return ((UINT8) (HexChar - 0x57));
365 /*******************************************************************************
367 * FUNCTION: UtConvertByteToHex
369 * PARAMETERS: RawByte - Binary data
370 * Buffer - Pointer to where the hex bytes will be stored
372 * RETURN: Ascii hex byte is stored in Buffer.
374 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
377 ******************************************************************************/
388 Buffer[2] = (UINT8) HexLookup[(RawByte >> 4) & 0xF];
389 Buffer[3] = (UINT8) HexLookup[RawByte & 0xF];
393 /*******************************************************************************
395 * FUNCTION: UtConvertByteToAsmHex
397 * PARAMETERS: RawByte - Binary data
398 * Buffer - Pointer to where the hex bytes will be stored
400 * RETURN: Ascii hex byte is stored in Buffer.
402 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
405 ******************************************************************************/
408 UtConvertByteToAsmHex (
414 Buffer[1] = (UINT8) HexLookup[(RawByte >> 4) & 0xF];
415 Buffer[2] = (UINT8) HexLookup[RawByte & 0xF];
420 /*******************************************************************************
424 * PARAMETERS: Type - Type of output
425 * Fmt - Printf format string
426 * ... - variable printf list
430 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
433 ******************************************************************************/
444 va_start (Args, Fmt);
451 if ((Type == ASL_PARSE_OUTPUT) &&
452 (!(AslCompilerdebug)))
457 (void) vfprintf (stderr, Fmt, Args);
463 /*******************************************************************************
465 * FUNCTION: UtPrintFormattedName
467 * PARAMETERS: ParseOpcode - Parser keyword ID
468 * Level - Indentation level
472 * DESCRIPTION: Print the ascii name of the parse opcode.
474 ******************************************************************************/
476 #define TEXT_OFFSET 10
479 UtPrintFormattedName (
486 DbgPrint (ASL_TREE_OUTPUT,
487 "%*s", (3 * Level), " ");
489 DbgPrint (ASL_TREE_OUTPUT,
490 " %-20.20s", UtGetOpName (ParseOpcode));
492 if (Level < TEXT_OFFSET)
494 DbgPrint (ASL_TREE_OUTPUT,
495 "%*s", (TEXT_OFFSET - Level) * 3, " ");
500 /*******************************************************************************
502 * FUNCTION: UtSetParseOpName
508 * DESCRIPTION: Insert the ascii name of the parse opcode
510 ******************************************************************************/
514 ACPI_PARSE_OBJECT *Op)
517 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
518 ACPI_MAX_PARSEOP_NAME);
522 /*******************************************************************************
524 * FUNCTION: UtGetOpName
526 * PARAMETERS: ParseOpcode - Parser keyword ID
528 * RETURN: Pointer to the opcode name
530 * DESCRIPTION: Get the ascii name of the parse opcode
532 ******************************************************************************/
540 * First entries (ASL_YYTNAME_START) in yytname are special reserved names.
541 * Ignore first 8 characters of the name
543 return ((char *) yytname
544 [(ParseOpcode - ASL_FIRST_PARSE_OPCODE) + ASL_YYTNAME_START] + 8);
548 /*******************************************************************************
550 * FUNCTION: UtDisplaySummary
552 * PARAMETERS: FileID - ID of outpout file
556 * DESCRIPTION: Display compilation statistics
558 ******************************************************************************/
565 if (FileId != ASL_FILE_STDOUT)
567 /* Compiler name and version number */
569 FlPrintFile (FileId, "%s version %X\n",
570 CompilerId, (UINT32) ACPI_CA_VERSION);
573 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
576 "Table Input: %s - %u lines, %u bytes, %u fields\n",
577 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
578 Gbl_InputByteCount, Gbl_InputFieldCount);
580 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
583 "Binary Output: %s - %u bytes\n\n",
584 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
589 /* Input/Output summary */
592 "ASL Input: %s - %u lines, %u bytes, %u keywords\n",
593 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
594 Gbl_InputByteCount, TotalKeywords);
598 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
601 "AML Output: %s - %u bytes, %u named objects, %u executable opcodes\n\n",
602 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
603 TotalNamedObjects, TotalExecutableOpcodes);
610 "Compilation complete. %u Errors, %u Warnings, %u Remarks",
611 Gbl_ExceptionCount[ASL_ERROR],
612 Gbl_ExceptionCount[ASL_WARNING] +
613 Gbl_ExceptionCount[ASL_WARNING2] +
614 Gbl_ExceptionCount[ASL_WARNING3],
615 Gbl_ExceptionCount[ASL_REMARK]);
617 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
620 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
623 FlPrintFile (FileId, "\n");
627 /*******************************************************************************
629 * FUNCTION: UtDisplaySummary
631 * PARAMETERS: Op - Integer parse node
632 * LowValue - Smallest allowed value
633 * HighValue - Largest allowed value
635 * RETURN: Op if OK, otherwise NULL
637 * DESCRIPTION: Check integer for an allowable range
639 ******************************************************************************/
642 UtCheckIntegerRange (
643 ACPI_PARSE_OBJECT *Op,
647 char *ParseError = NULL;
656 if (Op->Asl.Value.Integer < LowValue)
658 ParseError = "Value below valid range";
659 Op->Asl.Value.Integer = LowValue;
662 if (Op->Asl.Value.Integer > HighValue)
664 ParseError = "Value above valid range";
665 Op->Asl.Value.Integer = HighValue;
670 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
671 AslCompilererror (Buffer);
680 /*******************************************************************************
682 * FUNCTION: UtGetStringBuffer
684 * PARAMETERS: Length - Size of buffer requested
686 * RETURN: Pointer to the buffer. Aborts on allocation failure
688 * DESCRIPTION: Allocate a string buffer. Bypass the local
689 * dynamic memory manager for performance reasons (This has a
690 * major impact on the speed of the compiler.)
692 ******************************************************************************/
701 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
703 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
704 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
708 Buffer = Gbl_StringCacheNext;
709 Gbl_StringCacheNext += Length;
715 /*******************************************************************************
717 * FUNCTION: UtInternalizeName
719 * PARAMETERS: ExternalName - Name to convert
720 * ConvertedName - Where the converted name is returned
724 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
726 ******************************************************************************/
731 char **ConvertedName)
733 ACPI_NAMESTRING_INFO Info;
742 /* Get the length of the new internal name */
744 Info.ExternalName = ExternalName;
745 AcpiNsGetInternalNameLength (&Info);
747 /* We need a segment to store the internal name */
749 Info.InternalName = UtGetStringBuffer (Info.Length);
750 if (!Info.InternalName)
752 return (AE_NO_MEMORY);
757 Status = AcpiNsBuildInternalName (&Info);
758 if (ACPI_FAILURE (Status))
763 *ConvertedName = Info.InternalName;
768 /*******************************************************************************
770 * FUNCTION: UtPadNameWithUnderscores
772 * PARAMETERS: NameSeg - Input nameseg
773 * PaddedNameSeg - Output padded nameseg
775 * RETURN: Padded nameseg.
777 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
780 ******************************************************************************/
783 UtPadNameWithUnderscores (
790 for (i = 0; (i < ACPI_NAME_SIZE); i++)
794 *PaddedNameSeg = *NameSeg;
799 *PaddedNameSeg = '_';
806 /*******************************************************************************
808 * FUNCTION: UtAttachNameseg
810 * PARAMETERS: Op - Parent parse node
811 * Name - Full ExternalName
813 * RETURN: None; Sets the NameSeg field in parent node
815 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
816 * in the NameSeg field of the Op.
818 ******************************************************************************/
822 ACPI_PARSE_OBJECT *Op,
826 char PaddedNameSeg[4];
834 /* Look for the last dot in the namepath */
836 NameSeg = strrchr (Name, '.');
839 /* Found last dot, we have also found the final nameseg */
842 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
846 /* No dots in the namepath, there is only a single nameseg. */
847 /* Handle prefixes */
849 while ((*Name == '\\') || (*Name == '^'))
854 /* Remaing string should be one single nameseg */
856 UtPadNameWithUnderscores (Name, PaddedNameSeg);
859 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
863 /*******************************************************************************
865 * FUNCTION: UtAttachNamepathToOwner
867 * PARAMETERS: Op - Parent parse node
868 * NameOp - Node that contains the name
870 * RETURN: Sets the ExternalName and Namepath in the parent node
872 * DESCRIPTION: Store the name in two forms in the parent node: The original
873 * (external) name, and the internalized name that is used within
874 * the ACPI namespace manager.
876 ******************************************************************************/
879 UtAttachNamepathToOwner (
880 ACPI_PARSE_OBJECT *Op,
881 ACPI_PARSE_OBJECT *NameOp)
886 /* Full external path */
888 Op->Asl.ExternalName = NameOp->Asl.Value.String;
890 /* Save the NameOp for possible error reporting later */
892 Op->Asl.ParentMethod = (void *) NameOp;
894 /* Last nameseg of the path */
896 UtAttachNameseg (Op, Op->Asl.ExternalName);
898 /* Create internalized path */
900 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
901 if (ACPI_FAILURE (Status))
903 /* TBD: abort on no memory */
908 /*******************************************************************************
910 * FUNCTION: UtDoConstant
912 * PARAMETERS: String - Hex, Octal, or Decimal string
914 * RETURN: Converted Integer
916 * DESCRIPTION: Convert a string to an integer. With error checking.
918 ******************************************************************************/
929 Status = UtStrtoul64 (String, 0, &Converted);
930 if (ACPI_FAILURE (Status))
932 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
933 AcpiFormatException (Status));
934 AslCompilererror (ErrBuf);
941 /* TBD: use version in ACPI CA main code base? */
943 /*******************************************************************************
945 * FUNCTION: UtStrtoul64
947 * PARAMETERS: String - Null terminated string
948 * Terminater - Where a pointer to the terminating byte is
950 * Base - Radix of the string
952 * RETURN: Converted value
954 * DESCRIPTION: Convert a string into an unsigned value.
956 ******************************************************************************/
966 UINT64 ReturnValue = 0;
967 ACPI_STATUS Status = AE_OK;
982 * The specified Base parameter is not in the domain of
985 return (AE_BAD_PARAMETER);
988 /* Skip over any white space in the buffer: */
990 while (isspace ((int) *String) || *String == '\t')
996 * The buffer may contain an optional plus or minus sign.
997 * If it does, then skip over it but remember what is was:
1004 else if (*String == '+')
1015 * If the input parameter Base is zero, then we need to
1016 * determine if it is octal, decimal, or hexadecimal:
1022 if (tolower ((int) *(++String)) == 'x')
1039 * For octal and hexadecimal bases, skip over the leading
1040 * 0 or 0x, if they are present.
1042 if (Base == 8 && *String == '0')
1049 tolower ((int) *(++String)) == 'x')
1054 /* Main loop: convert the string to an unsigned long */
1058 if (isdigit ((int) *String))
1060 Index = ((UINT8) *String) - '0';
1064 Index = (UINT8) toupper ((int) *String);
1065 if (isupper ((int) Index))
1067 Index = Index - 'A' + 10;
1080 /* Check to see if value is out of range: */
1082 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1089 ReturnValue *= Base;
1090 ReturnValue += Index;
1097 /* If a minus sign was present, then "the conversion is negated": */
1099 if (Sign == NEGATIVE)
1101 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1104 *RetInteger = ReturnValue;
1112 Status = AE_BAD_OCTAL_CONSTANT;
1116 Status = AE_BAD_DECIMAL_CONSTANT;
1120 Status = AE_BAD_HEX_CONSTANT;
1124 /* Base validated above */