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: 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,
594 char *ParseError = NULL;
603 if (Op->Asl.Value.Integer < LowValue)
605 ParseError = "Value below valid range";
606 Op->Asl.Value.Integer = LowValue;
609 if (Op->Asl.Value.Integer > HighValue)
611 ParseError = "Value above valid range";
612 Op->Asl.Value.Integer = HighValue;
617 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
618 AslCompilererror (Buffer);
627 /*******************************************************************************
629 * FUNCTION: UtGetStringBuffer
631 * PARAMETERS: Length - Size of buffer requested
633 * RETURN: Pointer to the buffer. Aborts on allocation failure
635 * DESCRIPTION: Allocate a string buffer. Bypass the local
636 * dynamic memory manager for performance reasons (This has a
637 * major impact on the speed of the compiler.)
639 ******************************************************************************/
648 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
650 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
651 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
655 Buffer = Gbl_StringCacheNext;
656 Gbl_StringCacheNext += Length;
662 /*******************************************************************************
664 * FUNCTION: UtInternalizeName
666 * PARAMETERS: ExternalName - Name to convert
667 * ConvertedName - Where the converted name is returned
671 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
673 ******************************************************************************/
678 char **ConvertedName)
680 ACPI_NAMESTRING_INFO Info;
689 /* Get the length of the new internal name */
691 Info.ExternalName = ExternalName;
692 AcpiNsGetInternalNameLength (&Info);
694 /* We need a segment to store the internal name */
696 Info.InternalName = UtGetStringBuffer (Info.Length);
697 if (!Info.InternalName)
699 return (AE_NO_MEMORY);
704 Status = AcpiNsBuildInternalName (&Info);
705 if (ACPI_FAILURE (Status))
710 *ConvertedName = Info.InternalName;
715 /*******************************************************************************
717 * FUNCTION: UtPadNameWithUnderscores
719 * PARAMETERS: NameSeg - Input nameseg
720 * PaddedNameSeg - Output padded nameseg
722 * RETURN: Padded nameseg.
724 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
727 ******************************************************************************/
730 UtPadNameWithUnderscores (
737 for (i = 0; (i < ACPI_NAME_SIZE); i++)
741 *PaddedNameSeg = *NameSeg;
746 *PaddedNameSeg = '_';
753 /*******************************************************************************
755 * FUNCTION: UtAttachNameseg
757 * PARAMETERS: Op - Parent parse node
758 * Name - Full ExternalName
760 * RETURN: None; Sets the NameSeg field in parent node
762 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
763 * in the NameSeg field of the Op.
765 ******************************************************************************/
769 ACPI_PARSE_OBJECT *Op,
773 char PaddedNameSeg[4];
781 /* Look for the last dot in the namepath */
783 NameSeg = strrchr (Name, '.');
786 /* Found last dot, we have also found the final nameseg */
789 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
793 /* No dots in the namepath, there is only a single nameseg. */
794 /* Handle prefixes */
796 while ((*Name == '\\') || (*Name == '^'))
801 /* Remaing string should be one single nameseg */
803 UtPadNameWithUnderscores (Name, PaddedNameSeg);
806 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
810 /*******************************************************************************
812 * FUNCTION: UtAttachNamepathToOwner
814 * PARAMETERS: Op - Parent parse node
815 * NameOp - Node that contains the name
817 * RETURN: Sets the ExternalName and Namepath in the parent node
819 * DESCRIPTION: Store the name in two forms in the parent node: The original
820 * (external) name, and the internalized name that is used within
821 * the ACPI namespace manager.
823 ******************************************************************************/
826 UtAttachNamepathToOwner (
827 ACPI_PARSE_OBJECT *Op,
828 ACPI_PARSE_OBJECT *NameOp)
833 /* Full external path */
835 Op->Asl.ExternalName = NameOp->Asl.Value.String;
837 /* Save the NameOp for possible error reporting later */
839 Op->Asl.ParentMethod = (void *) NameOp;
841 /* Last nameseg of the path */
843 UtAttachNameseg (Op, Op->Asl.ExternalName);
845 /* Create internalized path */
847 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
848 if (ACPI_FAILURE (Status))
850 /* TBD: abort on no memory */
855 /*******************************************************************************
857 * FUNCTION: UtDoConstant
859 * PARAMETERS: String - Hex, Octal, or Decimal string
861 * RETURN: Converted Integer
863 * DESCRIPTION: Convert a string to an integer, with error checking.
865 ******************************************************************************/
876 Status = UtStrtoul64 (String, 0, &Converted);
877 if (ACPI_FAILURE (Status))
879 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
880 AcpiFormatException (Status));
881 AslCompilererror (ErrBuf);
888 /* TBD: use version in ACPI CA main code base? */
890 /*******************************************************************************
892 * FUNCTION: UtStrtoul64
894 * PARAMETERS: String - Null terminated string
895 * Terminater - Where a pointer to the terminating byte
897 * Base - Radix of the string
899 * RETURN: Converted value
901 * DESCRIPTION: Convert a string into an unsigned value.
903 ******************************************************************************/
913 UINT64 ReturnValue = 0;
914 ACPI_STATUS Status = AE_OK;
929 * The specified Base parameter is not in the domain of
932 return (AE_BAD_PARAMETER);
935 /* Skip over any white space in the buffer: */
937 while (isspace ((int) *String) || *String == '\t')
943 * The buffer may contain an optional plus or minus sign.
944 * If it does, then skip over it but remember what is was:
951 else if (*String == '+')
962 * If the input parameter Base is zero, then we need to
963 * determine if it is octal, decimal, or hexadecimal:
969 if (tolower ((int) *(++String)) == 'x')
986 * For octal and hexadecimal bases, skip over the leading
987 * 0 or 0x, if they are present.
989 if (Base == 8 && *String == '0')
996 tolower ((int) *(++String)) == 'x')
1001 /* Main loop: convert the string to an unsigned long */
1005 if (isdigit ((int) *String))
1007 Index = ((UINT8) *String) - '0';
1011 Index = (UINT8) toupper ((int) *String);
1012 if (isupper ((int) Index))
1014 Index = Index - 'A' + 10;
1027 /* Check to see if value is out of range: */
1029 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1036 ReturnValue *= Base;
1037 ReturnValue += Index;
1044 /* If a minus sign was present, then "the conversion is negated": */
1046 if (Sign == NEGATIVE)
1048 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1051 *RetInteger = ReturnValue;
1059 Status = AE_BAD_OCTAL_CONSTANT;
1063 Status = AE_BAD_DECIMAL_CONSTANT;
1067 Status = AE_BAD_HEX_CONSTANT;
1071 /* Base validated above */