2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
46 #include <contrib/dev/acpica/compiler/aslcompiler.h>
47 #include "aslcompiler.y.h"
48 #include <contrib/dev/acpica/include/acdisasm.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
51 #include <contrib/dev/acpica/include/acapps.h>
53 #define _COMPONENT ACPI_COMPILER
54 ACPI_MODULE_NAME ("aslutils")
59 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
62 /* Table below must match ASL_FILE_TYPES in asltypes.h */
64 static const char *AslFileTypeNames [ASL_NUM_FILES] =
83 /* Local prototypes */
86 UtPadNameWithUnderscores (
92 ACPI_PARSE_OBJECT *Op,
96 /*******************************************************************************
98 * FUNCTION: UtDisplaySupportedTables
104 * DESCRIPTION: Print all supported ACPI table names.
106 ******************************************************************************/
108 #define ACPI_TABLE_HELP_FORMAT "%8u) %s %s\n"
111 UtDisplaySupportedTables (
114 ACPI_DMTABLE_DATA *TableData;
118 printf ("\nACPI tables supported by iASL version %8.8X:\n"
119 " (Compiler, Disassembler, Template Generator)\n\n",
124 printf (" Special tables and AML tables:\n");
125 printf (ACPI_TABLE_HELP_FORMAT, 1, ACPI_RSDP_NAME, "Root System Description Pointer");
126 printf (ACPI_TABLE_HELP_FORMAT, 2, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
127 printf (ACPI_TABLE_HELP_FORMAT, 3, ACPI_SIG_DSDT, "Differentiated System Description Table");
128 printf (ACPI_TABLE_HELP_FORMAT, 4, ACPI_SIG_SSDT, "Secondary System Description Table");
130 /* All data tables with common table header */
132 printf ("\n Standard ACPI data tables:\n");
133 for (TableData = AcpiDmTableData, i = 5; TableData->Signature; TableData++, i++)
135 printf (ACPI_TABLE_HELP_FORMAT, i, TableData->Signature, TableData->Name);
140 /*******************************************************************************
142 * FUNCTION: UtDisplayConstantOpcodes
148 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
150 ******************************************************************************/
153 UtDisplayConstantOpcodes (
159 printf ("Constant expression opcode information\n\n");
161 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
163 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
165 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
171 /*******************************************************************************
173 * FUNCTION: UtLocalCalloc
175 * PARAMETERS: Size - Bytes to be allocated
177 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
179 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
180 * allocation failure, on the assumption that nothing more can be
183 ******************************************************************************/
192 Allocated = ACPI_ALLOCATE_ZEROED (Size);
195 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
196 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
197 Gbl_InputByteCount, Gbl_CurrentColumn,
198 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
205 TotalAllocated += Size;
210 /*******************************************************************************
212 * FUNCTION: UtBeginEvent
214 * PARAMETERS: Name - Ascii name of this event
216 * RETURN: Event number (integer index)
218 * DESCRIPTION: Saves the current time with this event
220 ******************************************************************************/
227 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
229 AcpiOsPrintf ("Ran out of compiler event structs!\n");
230 return (AslGbl_NextEvent);
233 /* Init event with current (start) time */
235 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
236 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
237 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
239 return (AslGbl_NextEvent++);
243 /*******************************************************************************
245 * FUNCTION: UtEndEvent
247 * PARAMETERS: Event - Event number (integer index)
251 * DESCRIPTION: Saves the current time (end time) with this event
253 ******************************************************************************/
260 if (Event >= ASL_NUM_EVENTS)
265 /* Insert end time for event */
267 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
271 /*******************************************************************************
273 * FUNCTION: UtHexCharToValue
275 * PARAMETERS: HexChar - Hex character in Ascii
277 * RETURN: The binary value of the hex character
279 * DESCRIPTION: Perform ascii-to-hex translation
281 ******************************************************************************/
290 return ((UINT8) (HexChar - 0x30));
295 return ((UINT8) (HexChar - 0x37));
298 return ((UINT8) (HexChar - 0x57));
302 /*******************************************************************************
304 * FUNCTION: UtConvertByteToHex
306 * PARAMETERS: RawByte - Binary data
307 * Buffer - Pointer to where the hex bytes will be
310 * RETURN: Ascii hex byte is stored in Buffer.
312 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
315 ******************************************************************************/
326 Buffer[2] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
327 Buffer[3] = (UINT8) AslHexLookup[RawByte & 0xF];
331 /*******************************************************************************
333 * FUNCTION: UtConvertByteToAsmHex
335 * PARAMETERS: RawByte - Binary data
336 * Buffer - Pointer to where the hex bytes will be
339 * RETURN: Ascii hex byte is stored in Buffer.
341 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
344 ******************************************************************************/
347 UtConvertByteToAsmHex (
353 Buffer[1] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
354 Buffer[2] = (UINT8) AslHexLookup[RawByte & 0xF];
359 /*******************************************************************************
363 * PARAMETERS: Type - Type of output
364 * Fmt - Printf format string
365 * ... - variable printf list
369 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
372 ******************************************************************************/
383 va_start (Args, Fmt);
390 if ((Type == ASL_PARSE_OUTPUT) &&
391 (!(AslCompilerdebug)))
396 (void) vfprintf (stderr, Fmt, Args);
402 /*******************************************************************************
404 * FUNCTION: UtPrintFormattedName
406 * PARAMETERS: ParseOpcode - Parser keyword ID
407 * Level - Indentation level
411 * DESCRIPTION: Print the ascii name of the parse opcode.
413 ******************************************************************************/
415 #define TEXT_OFFSET 10
418 UtPrintFormattedName (
425 DbgPrint (ASL_TREE_OUTPUT,
426 "%*s", (3 * Level), " ");
428 DbgPrint (ASL_TREE_OUTPUT,
429 " %-20.20s", UtGetOpName (ParseOpcode));
431 if (Level < TEXT_OFFSET)
433 DbgPrint (ASL_TREE_OUTPUT,
434 "%*s", (TEXT_OFFSET - Level) * 3, " ");
439 /*******************************************************************************
441 * FUNCTION: UtSetParseOpName
443 * PARAMETERS: Op - Parse op to be named.
447 * DESCRIPTION: Insert the ascii name of the parse opcode
449 ******************************************************************************/
453 ACPI_PARSE_OBJECT *Op)
456 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
457 ACPI_MAX_PARSEOP_NAME);
461 /*******************************************************************************
463 * FUNCTION: UtDisplaySummary
465 * PARAMETERS: FileID - ID of outpout file
469 * DESCRIPTION: Display compilation statistics
471 ******************************************************************************/
480 if (FileId != ASL_FILE_STDOUT)
482 /* Compiler name and version number */
484 FlPrintFile (FileId, "%s version %X%s\n\n",
485 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH);
488 /* Summary of main input and output files */
490 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
493 "%-14s %s - %u lines, %u bytes, %u fields\n",
495 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
496 Gbl_InputByteCount, Gbl_InputFieldCount);
498 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
501 "%-14s %s - %u bytes\n",
503 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
509 "%-14s %s - %u lines, %u bytes, %u keywords\n",
511 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
512 Gbl_InputByteCount, TotalKeywords);
516 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
519 "%-14s %s - %u bytes, %u named objects, %u executable opcodes\n",
521 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
522 TotalNamedObjects, TotalExecutableOpcodes);
526 /* Display summary of any optional files */
528 for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
530 if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
535 /* .SRC is a temp file unless specifically requested */
537 if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
542 /* .I is a temp file unless specifically requested */
544 if ((i == ASL_FILE_PREPROCESSOR) && (!Gbl_PreprocessorOutputFlag))
549 FlPrintFile (FileId, "%14s %s - %u bytes\n",
550 AslFileTypeNames [i],
551 Gbl_Files[i].Filename, FlGetFileSize (i));
557 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
558 Gbl_ExceptionCount[ASL_ERROR],
559 Gbl_ExceptionCount[ASL_WARNING] +
560 Gbl_ExceptionCount[ASL_WARNING2] +
561 Gbl_ExceptionCount[ASL_WARNING3],
562 Gbl_ExceptionCount[ASL_REMARK]);
564 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
567 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
570 FlPrintFile (FileId, "\n");
574 /*******************************************************************************
576 * FUNCTION: UtCheckIntegerRange
578 * PARAMETERS: Op - Integer parse node
579 * LowValue - Smallest allowed value
580 * HighValue - Largest allowed value
582 * RETURN: Op if OK, otherwise NULL
584 * DESCRIPTION: Check integer for an allowable range
586 ******************************************************************************/
589 UtCheckIntegerRange (
590 ACPI_PARSE_OBJECT *Op,
600 if ((Op->Asl.Value.Integer < LowValue) ||
601 (Op->Asl.Value.Integer > HighValue))
603 sprintf (MsgBuffer, "0x%X, allowable: 0x%X-0x%X",
604 (UINT32) Op->Asl.Value.Integer, LowValue, HighValue);
606 AslError (ASL_ERROR, ASL_MSG_RANGE, Op, MsgBuffer);
614 /*******************************************************************************
616 * FUNCTION: UtGetStringBuffer
618 * PARAMETERS: Length - Size of buffer requested
620 * RETURN: Pointer to the buffer. Aborts on allocation failure
622 * DESCRIPTION: Allocate a string buffer. Bypass the local
623 * dynamic memory manager for performance reasons (This has a
624 * major impact on the speed of the compiler.)
626 ******************************************************************************/
635 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
637 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
638 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
642 Buffer = Gbl_StringCacheNext;
643 Gbl_StringCacheNext += Length;
649 /*******************************************************************************
651 * FUNCTION: UtInternalizeName
653 * PARAMETERS: ExternalName - Name to convert
654 * ConvertedName - Where the converted name is returned
658 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
660 ******************************************************************************/
665 char **ConvertedName)
667 ACPI_NAMESTRING_INFO Info;
676 /* Get the length of the new internal name */
678 Info.ExternalName = ExternalName;
679 AcpiNsGetInternalNameLength (&Info);
681 /* We need a segment to store the internal name */
683 Info.InternalName = UtGetStringBuffer (Info.Length);
684 if (!Info.InternalName)
686 return (AE_NO_MEMORY);
691 Status = AcpiNsBuildInternalName (&Info);
692 if (ACPI_FAILURE (Status))
697 *ConvertedName = Info.InternalName;
702 /*******************************************************************************
704 * FUNCTION: UtPadNameWithUnderscores
706 * PARAMETERS: NameSeg - Input nameseg
707 * PaddedNameSeg - Output padded nameseg
709 * RETURN: Padded nameseg.
711 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
714 ******************************************************************************/
717 UtPadNameWithUnderscores (
724 for (i = 0; (i < ACPI_NAME_SIZE); i++)
728 *PaddedNameSeg = *NameSeg;
733 *PaddedNameSeg = '_';
740 /*******************************************************************************
742 * FUNCTION: UtAttachNameseg
744 * PARAMETERS: Op - Parent parse node
745 * Name - Full ExternalName
747 * RETURN: None; Sets the NameSeg field in parent node
749 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
750 * in the NameSeg field of the Op.
752 ******************************************************************************/
756 ACPI_PARSE_OBJECT *Op,
760 char PaddedNameSeg[4];
768 /* Look for the last dot in the namepath */
770 NameSeg = strrchr (Name, '.');
773 /* Found last dot, we have also found the final nameseg */
776 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
780 /* No dots in the namepath, there is only a single nameseg. */
781 /* Handle prefixes */
783 while ((*Name == '\\') || (*Name == '^'))
788 /* Remaing string should be one single nameseg */
790 UtPadNameWithUnderscores (Name, PaddedNameSeg);
793 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
797 /*******************************************************************************
799 * FUNCTION: UtAttachNamepathToOwner
801 * PARAMETERS: Op - Parent parse node
802 * NameOp - Node that contains the name
804 * RETURN: Sets the ExternalName and Namepath in the parent node
806 * DESCRIPTION: Store the name in two forms in the parent node: The original
807 * (external) name, and the internalized name that is used within
808 * the ACPI namespace manager.
810 ******************************************************************************/
813 UtAttachNamepathToOwner (
814 ACPI_PARSE_OBJECT *Op,
815 ACPI_PARSE_OBJECT *NameOp)
820 /* Full external path */
822 Op->Asl.ExternalName = NameOp->Asl.Value.String;
824 /* Save the NameOp for possible error reporting later */
826 Op->Asl.ParentMethod = (void *) NameOp;
828 /* Last nameseg of the path */
830 UtAttachNameseg (Op, Op->Asl.ExternalName);
832 /* Create internalized path */
834 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
835 if (ACPI_FAILURE (Status))
837 /* TBD: abort on no memory */
842 /*******************************************************************************
844 * FUNCTION: UtDoConstant
846 * PARAMETERS: String - Hex, Octal, or Decimal string
848 * RETURN: Converted Integer
850 * DESCRIPTION: Convert a string to an integer, with error checking.
852 ******************************************************************************/
863 Status = UtStrtoul64 (String, 0, &Converted);
864 if (ACPI_FAILURE (Status))
866 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
867 AcpiFormatException (Status));
868 AslCompilererror (ErrBuf);
875 /* TBD: use version in ACPI CA main code base? */
877 /*******************************************************************************
879 * FUNCTION: UtStrtoul64
881 * PARAMETERS: String - Null terminated string
882 * Terminater - Where a pointer to the terminating byte
884 * Base - Radix of the string
886 * RETURN: Converted value
888 * DESCRIPTION: Convert a string into an unsigned value.
890 ******************************************************************************/
900 UINT64 ReturnValue = 0;
901 ACPI_STATUS Status = AE_OK;
916 * The specified Base parameter is not in the domain of
919 return (AE_BAD_PARAMETER);
922 /* Skip over any white space in the buffer: */
924 while (isspace ((int) *String) || *String == '\t')
930 * The buffer may contain an optional plus or minus sign.
931 * If it does, then skip over it but remember what is was:
938 else if (*String == '+')
949 * If the input parameter Base is zero, then we need to
950 * determine if it is octal, decimal, or hexadecimal:
956 if (tolower ((int) *(++String)) == 'x')
973 * For octal and hexadecimal bases, skip over the leading
974 * 0 or 0x, if they are present.
976 if (Base == 8 && *String == '0')
983 tolower ((int) *(++String)) == 'x')
988 /* Main loop: convert the string to an unsigned long */
992 if (isdigit ((int) *String))
994 Index = ((UINT8) *String) - '0';
998 Index = (UINT8) toupper ((int) *String);
999 if (isupper ((int) Index))
1001 Index = Index - 'A' + 10;
1014 /* Check to see if value is out of range: */
1016 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1023 ReturnValue *= Base;
1024 ReturnValue += Index;
1031 /* If a minus sign was present, then "the conversion is negated": */
1033 if (Sign == NEGATIVE)
1035 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1038 *RetInteger = ReturnValue;
1046 Status = AE_BAD_OCTAL_CONSTANT;
1050 Status = AE_BAD_DECIMAL_CONSTANT;
1054 Status = AE_BAD_HEX_CONSTANT;
1058 /* Base validated above */