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 ******************************************************************************/
109 UtDisplaySupportedTables (
112 ACPI_DMTABLE_DATA *TableData;
116 printf ("\nACPI tables supported by iASL subsystems in "
118 " ASL and Data Table compilers\n"
119 " AML and Data Table disassemblers\n"
120 " ACPI table template generator\n\n", ACPI_CA_VERSION);
124 printf ("%8u) %s %s\n", 1, ACPI_SIG_DSDT, "Differentiated System Description Table");
125 printf ("%8u) %s %s\n", 2, ACPI_SIG_SSDT, "Secondary System Description Table");
126 printf ("%8u) %s %s\n", 3, ACPI_SIG_FADT, "Fixed ACPI Description Table (FADT)");
127 printf ("%8u) %s %s\n", 4, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
128 printf ("%8u) %s %s\n", 5, ACPI_RSDP_NAME, "Root System Description Pointer");
130 /* All data tables with common table header */
132 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
134 printf ("%8u) %s %s\n", i, TableData->Signature, TableData->Name);
140 /*******************************************************************************
142 * FUNCTION: AcpiPsDisplayConstantOpcodes
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 - 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 stored
309 * RETURN: Ascii hex byte is stored in Buffer.
311 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
314 ******************************************************************************/
325 Buffer[2] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
326 Buffer[3] = (UINT8) AslHexLookup[RawByte & 0xF];
330 /*******************************************************************************
332 * FUNCTION: UtConvertByteToAsmHex
334 * PARAMETERS: RawByte - Binary data
335 * Buffer - Pointer to where the hex bytes will be stored
337 * RETURN: Ascii hex byte is stored in Buffer.
339 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
342 ******************************************************************************/
345 UtConvertByteToAsmHex (
351 Buffer[1] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
352 Buffer[2] = (UINT8) AslHexLookup[RawByte & 0xF];
357 /*******************************************************************************
361 * PARAMETERS: Type - Type of output
362 * Fmt - Printf format string
363 * ... - variable printf list
367 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
370 ******************************************************************************/
381 va_start (Args, Fmt);
388 if ((Type == ASL_PARSE_OUTPUT) &&
389 (!(AslCompilerdebug)))
394 (void) vfprintf (stderr, Fmt, Args);
400 /*******************************************************************************
402 * FUNCTION: UtPrintFormattedName
404 * PARAMETERS: ParseOpcode - Parser keyword ID
405 * Level - Indentation level
409 * DESCRIPTION: Print the ascii name of the parse opcode.
411 ******************************************************************************/
413 #define TEXT_OFFSET 10
416 UtPrintFormattedName (
423 DbgPrint (ASL_TREE_OUTPUT,
424 "%*s", (3 * Level), " ");
426 DbgPrint (ASL_TREE_OUTPUT,
427 " %-20.20s", UtGetOpName (ParseOpcode));
429 if (Level < TEXT_OFFSET)
431 DbgPrint (ASL_TREE_OUTPUT,
432 "%*s", (TEXT_OFFSET - Level) * 3, " ");
437 /*******************************************************************************
439 * FUNCTION: UtSetParseOpName
445 * DESCRIPTION: Insert the ascii name of the parse opcode
447 ******************************************************************************/
451 ACPI_PARSE_OBJECT *Op)
454 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
455 ACPI_MAX_PARSEOP_NAME);
459 /*******************************************************************************
461 * FUNCTION: UtDisplaySummary
463 * PARAMETERS: FileID - ID of outpout file
467 * DESCRIPTION: Display compilation statistics
469 ******************************************************************************/
478 if (FileId != ASL_FILE_STDOUT)
480 /* Compiler name and version number */
482 FlPrintFile (FileId, "%s version %X%s\n\n",
483 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH);
486 /* Summary of main input and output files */
488 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
491 "%-14s %s - %u lines, %u bytes, %u fields\n",
493 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
494 Gbl_InputByteCount, Gbl_InputFieldCount);
496 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
499 "%-14s %s - %u bytes\n",
501 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
507 "%-14s %s - %u lines, %u bytes, %u keywords\n",
509 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
510 Gbl_InputByteCount, TotalKeywords);
514 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
517 "%-14s %s - %u bytes, %u named objects, %u executable opcodes\n",
519 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
520 TotalNamedObjects, TotalExecutableOpcodes);
524 /* Display summary of any optional files */
526 for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
528 if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
533 /* .SRC is a temp file unless specifically requested */
535 if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
540 /* .I is a temp file unless specifically requested */
542 if ((i == ASL_FILE_PREPROCESSOR) && (!Gbl_PreprocessorOutputFlag))
547 FlPrintFile (FileId, "%14s %s - %u bytes\n",
548 AslFileTypeNames [i],
549 Gbl_Files[i].Filename, FlGetFileSize (i));
555 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
556 Gbl_ExceptionCount[ASL_ERROR],
557 Gbl_ExceptionCount[ASL_WARNING] +
558 Gbl_ExceptionCount[ASL_WARNING2] +
559 Gbl_ExceptionCount[ASL_WARNING3],
560 Gbl_ExceptionCount[ASL_REMARK]);
562 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
565 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
568 FlPrintFile (FileId, "\n");
572 /*******************************************************************************
574 * FUNCTION: UtDisplaySummary
576 * PARAMETERS: Op - Integer parse node
577 * LowValue - Smallest allowed value
578 * HighValue - Largest allowed value
580 * RETURN: Op if OK, otherwise NULL
582 * DESCRIPTION: Check integer for an allowable range
584 ******************************************************************************/
587 UtCheckIntegerRange (
588 ACPI_PARSE_OBJECT *Op,
592 char *ParseError = NULL;
601 if (Op->Asl.Value.Integer < LowValue)
603 ParseError = "Value below valid range";
604 Op->Asl.Value.Integer = LowValue;
607 if (Op->Asl.Value.Integer > HighValue)
609 ParseError = "Value above valid range";
610 Op->Asl.Value.Integer = HighValue;
615 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
616 AslCompilererror (Buffer);
625 /*******************************************************************************
627 * FUNCTION: UtGetStringBuffer
629 * PARAMETERS: Length - Size of buffer requested
631 * RETURN: Pointer to the buffer. Aborts on allocation failure
633 * DESCRIPTION: Allocate a string buffer. Bypass the local
634 * dynamic memory manager for performance reasons (This has a
635 * major impact on the speed of the compiler.)
637 ******************************************************************************/
646 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
648 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
649 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
653 Buffer = Gbl_StringCacheNext;
654 Gbl_StringCacheNext += Length;
660 /*******************************************************************************
662 * FUNCTION: UtInternalizeName
664 * PARAMETERS: ExternalName - Name to convert
665 * ConvertedName - Where the converted name is returned
669 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
671 ******************************************************************************/
676 char **ConvertedName)
678 ACPI_NAMESTRING_INFO Info;
687 /* Get the length of the new internal name */
689 Info.ExternalName = ExternalName;
690 AcpiNsGetInternalNameLength (&Info);
692 /* We need a segment to store the internal name */
694 Info.InternalName = UtGetStringBuffer (Info.Length);
695 if (!Info.InternalName)
697 return (AE_NO_MEMORY);
702 Status = AcpiNsBuildInternalName (&Info);
703 if (ACPI_FAILURE (Status))
708 *ConvertedName = Info.InternalName;
713 /*******************************************************************************
715 * FUNCTION: UtPadNameWithUnderscores
717 * PARAMETERS: NameSeg - Input nameseg
718 * PaddedNameSeg - Output padded nameseg
720 * RETURN: Padded nameseg.
722 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
725 ******************************************************************************/
728 UtPadNameWithUnderscores (
735 for (i = 0; (i < ACPI_NAME_SIZE); i++)
739 *PaddedNameSeg = *NameSeg;
744 *PaddedNameSeg = '_';
751 /*******************************************************************************
753 * FUNCTION: UtAttachNameseg
755 * PARAMETERS: Op - Parent parse node
756 * Name - Full ExternalName
758 * RETURN: None; Sets the NameSeg field in parent node
760 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
761 * in the NameSeg field of the Op.
763 ******************************************************************************/
767 ACPI_PARSE_OBJECT *Op,
771 char PaddedNameSeg[4];
779 /* Look for the last dot in the namepath */
781 NameSeg = strrchr (Name, '.');
784 /* Found last dot, we have also found the final nameseg */
787 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
791 /* No dots in the namepath, there is only a single nameseg. */
792 /* Handle prefixes */
794 while ((*Name == '\\') || (*Name == '^'))
799 /* Remaing string should be one single nameseg */
801 UtPadNameWithUnderscores (Name, PaddedNameSeg);
804 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
808 /*******************************************************************************
810 * FUNCTION: UtAttachNamepathToOwner
812 * PARAMETERS: Op - Parent parse node
813 * NameOp - Node that contains the name
815 * RETURN: Sets the ExternalName and Namepath in the parent node
817 * DESCRIPTION: Store the name in two forms in the parent node: The original
818 * (external) name, and the internalized name that is used within
819 * the ACPI namespace manager.
821 ******************************************************************************/
824 UtAttachNamepathToOwner (
825 ACPI_PARSE_OBJECT *Op,
826 ACPI_PARSE_OBJECT *NameOp)
831 /* Full external path */
833 Op->Asl.ExternalName = NameOp->Asl.Value.String;
835 /* Save the NameOp for possible error reporting later */
837 Op->Asl.ParentMethod = (void *) NameOp;
839 /* Last nameseg of the path */
841 UtAttachNameseg (Op, Op->Asl.ExternalName);
843 /* Create internalized path */
845 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
846 if (ACPI_FAILURE (Status))
848 /* TBD: abort on no memory */
853 /*******************************************************************************
855 * FUNCTION: UtDoConstant
857 * PARAMETERS: String - Hex, Octal, or Decimal string
859 * RETURN: Converted Integer
861 * DESCRIPTION: Convert a string to an integer. With error checking.
863 ******************************************************************************/
874 Status = UtStrtoul64 (String, 0, &Converted);
875 if (ACPI_FAILURE (Status))
877 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
878 AcpiFormatException (Status));
879 AslCompilererror (ErrBuf);
886 /* TBD: use version in ACPI CA main code base? */
888 /*******************************************************************************
890 * FUNCTION: UtStrtoul64
892 * PARAMETERS: String - Null terminated string
893 * Terminater - Where a pointer to the terminating byte is
895 * Base - Radix of the string
897 * RETURN: Converted value
899 * DESCRIPTION: Convert a string into an unsigned value.
901 ******************************************************************************/
911 UINT64 ReturnValue = 0;
912 ACPI_STATUS Status = AE_OK;
927 * The specified Base parameter is not in the domain of
930 return (AE_BAD_PARAMETER);
933 /* Skip over any white space in the buffer: */
935 while (isspace ((int) *String) || *String == '\t')
941 * The buffer may contain an optional plus or minus sign.
942 * If it does, then skip over it but remember what is was:
949 else if (*String == '+')
960 * If the input parameter Base is zero, then we need to
961 * determine if it is octal, decimal, or hexadecimal:
967 if (tolower ((int) *(++String)) == 'x')
984 * For octal and hexadecimal bases, skip over the leading
985 * 0 or 0x, if they are present.
987 if (Base == 8 && *String == '0')
994 tolower ((int) *(++String)) == 'x')
999 /* Main loop: convert the string to an unsigned long */
1003 if (isdigit ((int) *String))
1005 Index = ((UINT8) *String) - '0';
1009 Index = (UINT8) toupper ((int) *String);
1010 if (isupper ((int) Index))
1012 Index = Index - 'A' + 10;
1025 /* Check to see if value is out of range: */
1027 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1034 ReturnValue *= Base;
1035 ReturnValue += Index;
1042 /* If a minus sign was present, then "the conversion is negated": */
1044 if (Sign == NEGATIVE)
1046 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1049 *RetInteger = ReturnValue;
1057 Status = AE_BAD_OCTAL_CONSTANT;
1061 Status = AE_BAD_DECIMAL_CONSTANT;
1065 Status = AE_BAD_HEX_CONSTANT;
1069 /* Base validated above */