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 "aslcompiler.h"
47 #include "aslcompiler.y.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] =
82 /* Local prototypes */
85 UtPadNameWithUnderscores (
91 ACPI_PARSE_OBJECT *Op,
95 /*******************************************************************************
97 * FUNCTION: UtDisplaySupportedTables
103 * DESCRIPTION: Print all supported ACPI table names.
105 ******************************************************************************/
108 UtDisplaySupportedTables (
111 ACPI_DMTABLE_DATA *TableData;
115 printf ("\nACPI tables supported by iASL subsystems in "
117 " ASL and Data Table compilers\n"
118 " AML and Data Table disassemblers\n"
119 " ACPI table template generator\n\n", ACPI_CA_VERSION);
123 printf ("%8u) %s %s\n", 1, ACPI_SIG_DSDT, "Differentiated System Description Table");
124 printf ("%8u) %s %s\n", 2, ACPI_SIG_SSDT, "Secondary System Description Table");
125 printf ("%8u) %s %s\n", 3, ACPI_SIG_FADT, "Fixed ACPI Description Table (FADT)");
126 printf ("%8u) %s %s\n", 4, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
127 printf ("%8u) %s %s\n", 5, ACPI_RSDP_NAME, "Root System Description Pointer");
129 /* All data tables with common table header */
131 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
133 printf ("%8u) %s %s\n", i, TableData->Signature, TableData->Name);
139 /*******************************************************************************
141 * FUNCTION: AcpiPsDisplayConstantOpcodes
147 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
149 ******************************************************************************/
152 UtDisplayConstantOpcodes (
158 printf ("Constant expression opcode information\n\n");
160 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
162 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
164 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
170 /*******************************************************************************
172 * FUNCTION: UtLocalCalloc
174 * PARAMETERS: Size - Bytes to be allocated
176 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
178 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
179 * allocation failure, on the assumption that nothing more can be
182 ******************************************************************************/
191 Allocated = ACPI_ALLOCATE_ZEROED (Size);
194 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
195 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
196 Gbl_InputByteCount, Gbl_CurrentColumn,
197 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
204 TotalAllocated += Size;
209 /*******************************************************************************
211 * FUNCTION: UtBeginEvent
213 * PARAMETERS: Name - Ascii name of this event
215 * RETURN: Event - Event number (integer index)
217 * DESCRIPTION: Saves the current time with this event
219 ******************************************************************************/
226 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
228 AcpiOsPrintf ("Ran out of compiler event structs!\n");
229 return (AslGbl_NextEvent);
232 /* Init event with current (start) time */
234 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
235 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
236 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
238 return (AslGbl_NextEvent++);
242 /*******************************************************************************
244 * FUNCTION: UtEndEvent
246 * PARAMETERS: Event - Event number (integer index)
250 * DESCRIPTION: Saves the current time (end time) with this event
252 ******************************************************************************/
259 if (Event >= ASL_NUM_EVENTS)
264 /* Insert end time for event */
266 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
270 /*******************************************************************************
272 * FUNCTION: UtHexCharToValue
274 * PARAMETERS: HexChar - Hex character in Ascii
276 * RETURN: The binary value of the hex character
278 * DESCRIPTION: Perform ascii-to-hex translation
280 ******************************************************************************/
289 return ((UINT8) (HexChar - 0x30));
294 return ((UINT8) (HexChar - 0x37));
297 return ((UINT8) (HexChar - 0x57));
301 /*******************************************************************************
303 * FUNCTION: UtConvertByteToHex
305 * PARAMETERS: RawByte - Binary data
306 * Buffer - Pointer to where the hex bytes will be stored
308 * RETURN: Ascii hex byte is stored in Buffer.
310 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
313 ******************************************************************************/
324 Buffer[2] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
325 Buffer[3] = (UINT8) AslHexLookup[RawByte & 0xF];
329 /*******************************************************************************
331 * FUNCTION: UtConvertByteToAsmHex
333 * PARAMETERS: RawByte - Binary data
334 * Buffer - Pointer to where the hex bytes will be stored
336 * RETURN: Ascii hex byte is stored in Buffer.
338 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
341 ******************************************************************************/
344 UtConvertByteToAsmHex (
350 Buffer[1] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
351 Buffer[2] = (UINT8) AslHexLookup[RawByte & 0xF];
356 /*******************************************************************************
360 * PARAMETERS: Type - Type of output
361 * Fmt - Printf format string
362 * ... - variable printf list
366 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
369 ******************************************************************************/
380 va_start (Args, Fmt);
387 if ((Type == ASL_PARSE_OUTPUT) &&
388 (!(AslCompilerdebug)))
393 (void) vfprintf (stderr, Fmt, Args);
399 /*******************************************************************************
401 * FUNCTION: UtPrintFormattedName
403 * PARAMETERS: ParseOpcode - Parser keyword ID
404 * Level - Indentation level
408 * DESCRIPTION: Print the ascii name of the parse opcode.
410 ******************************************************************************/
412 #define TEXT_OFFSET 10
415 UtPrintFormattedName (
422 DbgPrint (ASL_TREE_OUTPUT,
423 "%*s", (3 * Level), " ");
425 DbgPrint (ASL_TREE_OUTPUT,
426 " %-20.20s", UtGetOpName (ParseOpcode));
428 if (Level < TEXT_OFFSET)
430 DbgPrint (ASL_TREE_OUTPUT,
431 "%*s", (TEXT_OFFSET - Level) * 3, " ");
436 /*******************************************************************************
438 * FUNCTION: UtSetParseOpName
444 * DESCRIPTION: Insert the ascii name of the parse opcode
446 ******************************************************************************/
450 ACPI_PARSE_OBJECT *Op)
453 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
454 ACPI_MAX_PARSEOP_NAME);
458 /*******************************************************************************
460 * FUNCTION: UtDisplaySummary
462 * PARAMETERS: FileID - ID of outpout file
466 * DESCRIPTION: Display compilation statistics
468 ******************************************************************************/
477 if (FileId != ASL_FILE_STDOUT)
479 /* Compiler name and version number */
481 FlPrintFile (FileId, "%s version %X%s [%s]\n\n",
482 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH, __DATE__);
485 /* Summary of main input and output files */
487 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
490 "%-14s %s - %u lines, %u bytes, %u fields\n",
492 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
493 Gbl_InputByteCount, Gbl_InputFieldCount);
495 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
498 "%-14s %s - %u bytes\n",
500 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
506 "%-14s %s - %u lines, %u bytes, %u keywords\n",
508 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
509 Gbl_InputByteCount, TotalKeywords);
513 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
516 "%-14s %s - %u bytes, %u named objects, %u executable opcodes\n",
518 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
519 TotalNamedObjects, TotalExecutableOpcodes);
523 /* Display summary of any optional files */
525 for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
527 if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
532 /* .SRC is a temp file unless specifically requested */
534 if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
539 FlPrintFile (FileId, "%14s %s - %u bytes\n",
540 AslFileTypeNames [i],
541 Gbl_Files[i].Filename, FlGetFileSize (i));
547 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
548 Gbl_ExceptionCount[ASL_ERROR],
549 Gbl_ExceptionCount[ASL_WARNING] +
550 Gbl_ExceptionCount[ASL_WARNING2] +
551 Gbl_ExceptionCount[ASL_WARNING3],
552 Gbl_ExceptionCount[ASL_REMARK]);
554 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
557 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
560 FlPrintFile (FileId, "\n");
564 /*******************************************************************************
566 * FUNCTION: UtDisplaySummary
568 * PARAMETERS: Op - Integer parse node
569 * LowValue - Smallest allowed value
570 * HighValue - Largest allowed value
572 * RETURN: Op if OK, otherwise NULL
574 * DESCRIPTION: Check integer for an allowable range
576 ******************************************************************************/
579 UtCheckIntegerRange (
580 ACPI_PARSE_OBJECT *Op,
584 char *ParseError = NULL;
593 if (Op->Asl.Value.Integer < LowValue)
595 ParseError = "Value below valid range";
596 Op->Asl.Value.Integer = LowValue;
599 if (Op->Asl.Value.Integer > HighValue)
601 ParseError = "Value above valid range";
602 Op->Asl.Value.Integer = HighValue;
607 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
608 AslCompilererror (Buffer);
617 /*******************************************************************************
619 * FUNCTION: UtGetStringBuffer
621 * PARAMETERS: Length - Size of buffer requested
623 * RETURN: Pointer to the buffer. Aborts on allocation failure
625 * DESCRIPTION: Allocate a string buffer. Bypass the local
626 * dynamic memory manager for performance reasons (This has a
627 * major impact on the speed of the compiler.)
629 ******************************************************************************/
638 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
640 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
641 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
645 Buffer = Gbl_StringCacheNext;
646 Gbl_StringCacheNext += Length;
652 /*******************************************************************************
654 * FUNCTION: UtInternalizeName
656 * PARAMETERS: ExternalName - Name to convert
657 * ConvertedName - Where the converted name is returned
661 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
663 ******************************************************************************/
668 char **ConvertedName)
670 ACPI_NAMESTRING_INFO Info;
679 /* Get the length of the new internal name */
681 Info.ExternalName = ExternalName;
682 AcpiNsGetInternalNameLength (&Info);
684 /* We need a segment to store the internal name */
686 Info.InternalName = UtGetStringBuffer (Info.Length);
687 if (!Info.InternalName)
689 return (AE_NO_MEMORY);
694 Status = AcpiNsBuildInternalName (&Info);
695 if (ACPI_FAILURE (Status))
700 *ConvertedName = Info.InternalName;
705 /*******************************************************************************
707 * FUNCTION: UtPadNameWithUnderscores
709 * PARAMETERS: NameSeg - Input nameseg
710 * PaddedNameSeg - Output padded nameseg
712 * RETURN: Padded nameseg.
714 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
717 ******************************************************************************/
720 UtPadNameWithUnderscores (
727 for (i = 0; (i < ACPI_NAME_SIZE); i++)
731 *PaddedNameSeg = *NameSeg;
736 *PaddedNameSeg = '_';
743 /*******************************************************************************
745 * FUNCTION: UtAttachNameseg
747 * PARAMETERS: Op - Parent parse node
748 * Name - Full ExternalName
750 * RETURN: None; Sets the NameSeg field in parent node
752 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
753 * in the NameSeg field of the Op.
755 ******************************************************************************/
759 ACPI_PARSE_OBJECT *Op,
763 char PaddedNameSeg[4];
771 /* Look for the last dot in the namepath */
773 NameSeg = strrchr (Name, '.');
776 /* Found last dot, we have also found the final nameseg */
779 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
783 /* No dots in the namepath, there is only a single nameseg. */
784 /* Handle prefixes */
786 while ((*Name == '\\') || (*Name == '^'))
791 /* Remaing string should be one single nameseg */
793 UtPadNameWithUnderscores (Name, PaddedNameSeg);
796 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
800 /*******************************************************************************
802 * FUNCTION: UtAttachNamepathToOwner
804 * PARAMETERS: Op - Parent parse node
805 * NameOp - Node that contains the name
807 * RETURN: Sets the ExternalName and Namepath in the parent node
809 * DESCRIPTION: Store the name in two forms in the parent node: The original
810 * (external) name, and the internalized name that is used within
811 * the ACPI namespace manager.
813 ******************************************************************************/
816 UtAttachNamepathToOwner (
817 ACPI_PARSE_OBJECT *Op,
818 ACPI_PARSE_OBJECT *NameOp)
823 /* Full external path */
825 Op->Asl.ExternalName = NameOp->Asl.Value.String;
827 /* Save the NameOp for possible error reporting later */
829 Op->Asl.ParentMethod = (void *) NameOp;
831 /* Last nameseg of the path */
833 UtAttachNameseg (Op, Op->Asl.ExternalName);
835 /* Create internalized path */
837 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
838 if (ACPI_FAILURE (Status))
840 /* TBD: abort on no memory */
845 /*******************************************************************************
847 * FUNCTION: UtDoConstant
849 * PARAMETERS: String - Hex, Octal, or Decimal string
851 * RETURN: Converted Integer
853 * DESCRIPTION: Convert a string to an integer. With error checking.
855 ******************************************************************************/
866 Status = UtStrtoul64 (String, 0, &Converted);
867 if (ACPI_FAILURE (Status))
869 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
870 AcpiFormatException (Status));
871 AslCompilererror (ErrBuf);
878 /* TBD: use version in ACPI CA main code base? */
880 /*******************************************************************************
882 * FUNCTION: UtStrtoul64
884 * PARAMETERS: String - Null terminated string
885 * Terminater - Where a pointer to the terminating byte is
887 * Base - Radix of the string
889 * RETURN: Converted value
891 * DESCRIPTION: Convert a string into an unsigned value.
893 ******************************************************************************/
903 UINT64 ReturnValue = 0;
904 ACPI_STATUS Status = AE_OK;
919 * The specified Base parameter is not in the domain of
922 return (AE_BAD_PARAMETER);
925 /* Skip over any white space in the buffer: */
927 while (isspace ((int) *String) || *String == '\t')
933 * The buffer may contain an optional plus or minus sign.
934 * If it does, then skip over it but remember what is was:
941 else if (*String == '+')
952 * If the input parameter Base is zero, then we need to
953 * determine if it is octal, decimal, or hexadecimal:
959 if (tolower ((int) *(++String)) == 'x')
976 * For octal and hexadecimal bases, skip over the leading
977 * 0 or 0x, if they are present.
979 if (Base == 8 && *String == '0')
986 tolower ((int) *(++String)) == 'x')
991 /* Main loop: convert the string to an unsigned long */
995 if (isdigit ((int) *String))
997 Index = ((UINT8) *String) - '0';
1001 Index = (UINT8) toupper ((int) *String);
1002 if (isupper ((int) Index))
1004 Index = Index - 'A' + 10;
1017 /* Check to see if value is out of range: */
1019 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1026 ReturnValue *= Base;
1027 ReturnValue += Index;
1034 /* If a minus sign was present, then "the conversion is negated": */
1036 if (Sign == NEGATIVE)
1038 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1041 *RetInteger = ReturnValue;
1049 Status = AE_BAD_OCTAL_CONSTANT;
1053 Status = AE_BAD_DECIMAL_CONSTANT;
1057 Status = AE_BAD_HEX_CONSTANT;
1061 /* Base validated above */