1 /******************************************************************************
3 * Module Name: aslutils -- compiler utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 #include "aslcompiler.h"
46 #include "aslcompiler.y.h"
52 #define _COMPONENT ACPI_COMPILER
53 ACPI_MODULE_NAME ("aslutils")
56 /* Local prototypes */
59 UtPadNameWithUnderscores (
65 ACPI_PARSE_OBJECT *Op,
69 /*******************************************************************************
71 * FUNCTION: UtDisplaySupportedTables
77 * DESCRIPTION: Print all supported ACPI table names.
79 ******************************************************************************/
81 #define ACPI_TABLE_HELP_FORMAT "%8u) %s %s\n"
84 UtDisplaySupportedTables (
87 ACPI_DMTABLE_DATA *TableData;
91 printf ("\nACPI tables supported by iASL version %8.8X:\n"
92 " (Compiler, Disassembler, Template Generator)\n\n",
97 printf (" Special tables and AML tables:\n");
98 printf (ACPI_TABLE_HELP_FORMAT, 1, ACPI_RSDP_NAME, "Root System Description Pointer");
99 printf (ACPI_TABLE_HELP_FORMAT, 2, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
100 printf (ACPI_TABLE_HELP_FORMAT, 3, ACPI_SIG_DSDT, "Differentiated System Description Table");
101 printf (ACPI_TABLE_HELP_FORMAT, 4, ACPI_SIG_SSDT, "Secondary System Description Table");
103 /* All data tables with common table header */
105 printf ("\n Standard ACPI data tables:\n");
106 for (TableData = AcpiDmTableData, i = 5; TableData->Signature; TableData++, i++)
108 printf (ACPI_TABLE_HELP_FORMAT, i, TableData->Signature, TableData->Name);
113 /*******************************************************************************
115 * FUNCTION: UtDisplayConstantOpcodes
121 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
123 ******************************************************************************/
126 UtDisplayConstantOpcodes (
132 printf ("Constant expression opcode information\n\n");
134 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
136 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
138 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
144 /*******************************************************************************
146 * FUNCTION: UtLocalCalloc
148 * PARAMETERS: Size - Bytes to be allocated
150 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
152 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
153 * allocation failure, on the assumption that nothing more can be
156 ******************************************************************************/
165 Allocated = ACPI_ALLOCATE_ZEROED (Size);
168 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
169 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
170 Gbl_InputByteCount, Gbl_CurrentColumn,
171 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
178 TotalAllocated += Size;
183 /*******************************************************************************
185 * FUNCTION: UtBeginEvent
187 * PARAMETERS: Name - Ascii name of this event
189 * RETURN: Event number (integer index)
191 * DESCRIPTION: Saves the current time with this event
193 ******************************************************************************/
200 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
202 AcpiOsPrintf ("Ran out of compiler event structs!\n");
203 return (AslGbl_NextEvent);
206 /* Init event with current (start) time */
208 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
209 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
210 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
212 return (AslGbl_NextEvent++);
216 /*******************************************************************************
218 * FUNCTION: UtEndEvent
220 * PARAMETERS: Event - Event number (integer index)
224 * DESCRIPTION: Saves the current time (end time) with this event
226 ******************************************************************************/
233 if (Event >= ASL_NUM_EVENTS)
238 /* Insert end time for event */
240 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
244 /*******************************************************************************
246 * FUNCTION: UtConvertByteToHex
248 * PARAMETERS: RawByte - Binary data
249 * Buffer - Pointer to where the hex bytes will be
252 * RETURN: Ascii hex byte is stored in Buffer.
254 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
257 ******************************************************************************/
268 Buffer[2] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 4);
269 Buffer[3] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 0);
273 /*******************************************************************************
275 * FUNCTION: UtConvertByteToAsmHex
277 * PARAMETERS: RawByte - Binary data
278 * Buffer - Pointer to where the hex bytes will be
281 * RETURN: Ascii hex byte is stored in Buffer.
283 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
284 * with '0', and a trailing 'h' is added.
286 ******************************************************************************/
289 UtConvertByteToAsmHex (
295 Buffer[1] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 4);
296 Buffer[2] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 0);
301 /*******************************************************************************
305 * PARAMETERS: Type - Type of output
306 * Fmt - Printf format string
307 * ... - variable printf list
311 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
314 ******************************************************************************/
330 if ((Type == ASL_PARSE_OUTPUT) &&
331 (!(AslCompilerdebug)))
336 va_start (Args, Fmt);
337 (void) vfprintf (stderr, Fmt, Args);
343 /*******************************************************************************
345 * FUNCTION: UtPrintFormattedName
347 * PARAMETERS: ParseOpcode - Parser keyword ID
348 * Level - Indentation level
352 * DESCRIPTION: Print the ascii name of the parse opcode.
354 ******************************************************************************/
356 #define TEXT_OFFSET 10
359 UtPrintFormattedName (
366 DbgPrint (ASL_TREE_OUTPUT,
367 "%*s", (3 * Level), " ");
369 DbgPrint (ASL_TREE_OUTPUT,
370 " %-20.20s", UtGetOpName (ParseOpcode));
372 if (Level < TEXT_OFFSET)
374 DbgPrint (ASL_TREE_OUTPUT,
375 "%*s", (TEXT_OFFSET - Level) * 3, " ");
380 /*******************************************************************************
382 * FUNCTION: UtSetParseOpName
384 * PARAMETERS: Op - Parse op to be named.
388 * DESCRIPTION: Insert the ascii name of the parse opcode
390 ******************************************************************************/
394 ACPI_PARSE_OBJECT *Op)
397 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
398 ACPI_MAX_PARSEOP_NAME);
402 /*******************************************************************************
404 * FUNCTION: UtDisplaySummary
406 * PARAMETERS: FileID - ID of outpout file
410 * DESCRIPTION: Display compilation statistics
412 ******************************************************************************/
421 if (FileId != ASL_FILE_STDOUT)
423 /* Compiler name and version number */
425 FlPrintFile (FileId, "%s version %X%s [%s]\n\n",
426 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH, __DATE__);
429 /* Summary of main input and output files */
431 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
434 "%-14s %s - %u lines, %u bytes, %u fields\n",
436 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
437 Gbl_InputByteCount, Gbl_InputFieldCount);
439 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
442 "%-14s %s - %u bytes\n",
444 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
450 "%-14s %s - %u lines, %u bytes, %u keywords\n",
452 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
453 Gbl_InputByteCount, TotalKeywords);
457 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
460 "%-14s %s - %u bytes, %u named objects, %u executable opcodes\n",
462 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
463 TotalNamedObjects, TotalExecutableOpcodes);
467 /* Display summary of any optional files */
469 for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
471 if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
476 /* .SRC is a temp file unless specifically requested */
478 if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
483 /* .I is a temp file unless specifically requested */
485 if ((i == ASL_FILE_PREPROCESSOR) && (!Gbl_PreprocessorOutputFlag))
490 FlPrintFile (FileId, "%14s %s - %u bytes\n",
491 Gbl_Files[i].ShortDescription,
492 Gbl_Files[i].Filename, FlGetFileSize (i));
498 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
499 Gbl_ExceptionCount[ASL_ERROR],
500 Gbl_ExceptionCount[ASL_WARNING] +
501 Gbl_ExceptionCount[ASL_WARNING2] +
502 Gbl_ExceptionCount[ASL_WARNING3],
503 Gbl_ExceptionCount[ASL_REMARK]);
505 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
508 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
511 FlPrintFile (FileId, "\n");
515 /*******************************************************************************
517 * FUNCTION: UtCheckIntegerRange
519 * PARAMETERS: Op - Integer parse node
520 * LowValue - Smallest allowed value
521 * HighValue - Largest allowed value
523 * RETURN: Op if OK, otherwise NULL
525 * DESCRIPTION: Check integer for an allowable range
527 ******************************************************************************/
530 UtCheckIntegerRange (
531 ACPI_PARSE_OBJECT *Op,
541 if ((Op->Asl.Value.Integer < LowValue) ||
542 (Op->Asl.Value.Integer > HighValue))
544 sprintf (MsgBuffer, "0x%X, allowable: 0x%X-0x%X",
545 (UINT32) Op->Asl.Value.Integer, LowValue, HighValue);
547 AslError (ASL_ERROR, ASL_MSG_RANGE, Op, MsgBuffer);
555 /*******************************************************************************
557 * FUNCTION: UtGetStringBuffer
559 * PARAMETERS: Length - Size of buffer requested
561 * RETURN: Pointer to the buffer. Aborts on allocation failure
563 * DESCRIPTION: Allocate a string buffer. Bypass the local
564 * dynamic memory manager for performance reasons (This has a
565 * major impact on the speed of the compiler.)
567 ******************************************************************************/
576 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
578 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
579 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
583 Buffer = Gbl_StringCacheNext;
584 Gbl_StringCacheNext += Length;
590 /******************************************************************************
592 * FUNCTION: UtExpandLineBuffers
594 * PARAMETERS: None. Updates global line buffer pointers.
596 * RETURN: None. Reallocates the global line buffers
598 * DESCRIPTION: Called if the current line buffer becomes filled. Reallocates
599 * all global line buffers and updates Gbl_LineBufferSize. NOTE:
600 * Also used for the initial allocation of the buffers, when
601 * all of the buffer pointers are NULL. Initial allocations are
602 * of size ASL_DEFAULT_LINE_BUFFER_SIZE
604 *****************************************************************************/
607 UtExpandLineBuffers (
613 /* Attempt to double the size of all line buffers */
615 NewSize = Gbl_LineBufferSize * 2;
616 if (Gbl_CurrentLineBuffer)
618 DbgPrint (ASL_DEBUG_OUTPUT,"Increasing line buffer size from %u to %u\n",
619 Gbl_LineBufferSize, NewSize);
622 Gbl_CurrentLineBuffer = realloc (Gbl_CurrentLineBuffer, NewSize);
623 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
624 if (!Gbl_CurrentLineBuffer)
629 Gbl_MainTokenBuffer = realloc (Gbl_MainTokenBuffer, NewSize);
630 if (!Gbl_MainTokenBuffer)
635 Gbl_MacroTokenBuffer = realloc (Gbl_MacroTokenBuffer, NewSize);
636 if (!Gbl_MacroTokenBuffer)
641 Gbl_ExpressionTokenBuffer = realloc (Gbl_ExpressionTokenBuffer, NewSize);
642 if (!Gbl_ExpressionTokenBuffer)
647 Gbl_LineBufferSize = NewSize;
651 /* On error above, simply issue error messages and abort, cannot continue */
654 printf ("Could not increase line buffer size from %u to %u\n",
655 Gbl_LineBufferSize, Gbl_LineBufferSize * 2);
657 AslError (ASL_ERROR, ASL_MSG_BUFFER_ALLOCATION,
663 /*******************************************************************************
665 * FUNCTION: UtInternalizeName
667 * PARAMETERS: ExternalName - Name to convert
668 * ConvertedName - Where the converted name is returned
672 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
674 ******************************************************************************/
679 char **ConvertedName)
681 ACPI_NAMESTRING_INFO Info;
690 /* Get the length of the new internal name */
692 Info.ExternalName = ExternalName;
693 AcpiNsGetInternalNameLength (&Info);
695 /* We need a segment to store the internal name */
697 Info.InternalName = UtGetStringBuffer (Info.Length);
698 if (!Info.InternalName)
700 return (AE_NO_MEMORY);
705 Status = AcpiNsBuildInternalName (&Info);
706 if (ACPI_FAILURE (Status))
711 *ConvertedName = Info.InternalName;
716 /*******************************************************************************
718 * FUNCTION: UtPadNameWithUnderscores
720 * PARAMETERS: NameSeg - Input nameseg
721 * PaddedNameSeg - Output padded nameseg
723 * RETURN: Padded nameseg.
725 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
728 ******************************************************************************/
731 UtPadNameWithUnderscores (
738 for (i = 0; (i < ACPI_NAME_SIZE); i++)
742 *PaddedNameSeg = *NameSeg;
747 *PaddedNameSeg = '_';
754 /*******************************************************************************
756 * FUNCTION: UtAttachNameseg
758 * PARAMETERS: Op - Parent parse node
759 * Name - Full ExternalName
761 * RETURN: None; Sets the NameSeg field in parent node
763 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
764 * in the NameSeg field of the Op.
766 ******************************************************************************/
770 ACPI_PARSE_OBJECT *Op,
774 char PaddedNameSeg[4];
782 /* Look for the last dot in the namepath */
784 NameSeg = strrchr (Name, '.');
787 /* Found last dot, we have also found the final nameseg */
790 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
794 /* No dots in the namepath, there is only a single nameseg. */
795 /* Handle prefixes */
797 while (ACPI_IS_ROOT_PREFIX (*Name) ||
798 ACPI_IS_PARENT_PREFIX (*Name))
803 /* Remaining string should be one single nameseg */
805 UtPadNameWithUnderscores (Name, PaddedNameSeg);
808 ACPI_MOVE_NAME (Op->Asl.NameSeg, PaddedNameSeg);
812 /*******************************************************************************
814 * FUNCTION: UtAttachNamepathToOwner
816 * PARAMETERS: Op - Parent parse node
817 * NameOp - Node that contains the name
819 * RETURN: Sets the ExternalName and Namepath in the parent node
821 * DESCRIPTION: Store the name in two forms in the parent node: The original
822 * (external) name, and the internalized name that is used within
823 * the ACPI namespace manager.
825 ******************************************************************************/
828 UtAttachNamepathToOwner (
829 ACPI_PARSE_OBJECT *Op,
830 ACPI_PARSE_OBJECT *NameOp)
835 /* Full external path */
837 Op->Asl.ExternalName = NameOp->Asl.Value.String;
839 /* Save the NameOp for possible error reporting later */
841 Op->Asl.ParentMethod = (void *) NameOp;
843 /* Last nameseg of the path */
845 UtAttachNameseg (Op, Op->Asl.ExternalName);
847 /* Create internalized path */
849 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
850 if (ACPI_FAILURE (Status))
852 /* TBD: abort on no memory */
857 /*******************************************************************************
859 * FUNCTION: UtDoConstant
861 * PARAMETERS: String - Hex, Octal, or Decimal string
863 * RETURN: Converted Integer
865 * DESCRIPTION: Convert a string to an integer, with error checking.
867 ******************************************************************************/
878 Status = UtStrtoul64 (String, 0, &Converted);
879 if (ACPI_FAILURE (Status))
881 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
882 AcpiFormatException (Status));
883 AslCompilererror (ErrBuf);
890 /* TBD: use version in ACPICA main code base? */
892 /*******************************************************************************
894 * FUNCTION: UtStrtoul64
896 * PARAMETERS: String - Null terminated string
897 * Terminater - Where a pointer to the terminating byte
899 * Base - Radix of the string
901 * RETURN: Converted value
903 * DESCRIPTION: Convert a string into an unsigned value.
905 ******************************************************************************/
915 UINT64 ReturnValue = 0;
916 ACPI_STATUS Status = AE_OK;
932 * The specified Base parameter is not in the domain of
935 return (AE_BAD_PARAMETER);
938 /* Skip over any white space in the buffer: */
940 while (isspace ((int) *String) || *String == '\t')
946 * The buffer may contain an optional plus or minus sign.
947 * If it does, then skip over it but remember what is was:
954 else if (*String == '+')
965 * If the input parameter Base is zero, then we need to
966 * determine if it is octal, decimal, or hexadecimal:
972 if (tolower ((int) *(++String)) == 'x')
989 * For octal and hexadecimal bases, skip over the leading
990 * 0 or 0x, if they are present.
992 if (Base == 8 && *String == '0')
999 tolower ((int) *(++String)) == 'x')
1004 /* Main loop: convert the string to an unsigned long */
1008 if (isdigit ((int) *String))
1010 Index = ((UINT8) *String) - '0';
1014 Index = (UINT8) toupper ((int) *String);
1015 if (isupper ((int) Index))
1017 Index = Index - 'A' + 10;
1030 /* Check to see if value is out of range: */
1032 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1039 ReturnValue *= Base;
1040 ReturnValue += Index;
1047 /* If a minus sign was present, then "the conversion is negated": */
1049 if (Sign == NEGATIVE)
1051 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1054 *RetInteger = ReturnValue;
1063 Status = AE_BAD_OCTAL_CONSTANT;
1068 Status = AE_BAD_DECIMAL_CONSTANT;
1073 Status = AE_BAD_HEX_CONSTANT;
1078 /* Base validated above */