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.
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("aslutils")
55 /* Local prototypes */
58 UtPadNameWithUnderscores (
64 ACPI_PARSE_OBJECT *Op,
68 /*******************************************************************************
70 * FUNCTION: UtDisplaySupportedTables
76 * DESCRIPTION: Print all supported ACPI table names.
78 ******************************************************************************/
80 #define ACPI_TABLE_HELP_FORMAT "%8u) %s %s\n"
83 UtDisplaySupportedTables (
86 ACPI_DMTABLE_DATA *TableData;
90 printf ("\nACPI tables supported by iASL version %8.8X:\n"
91 " (Compiler, Disassembler, Template Generator)\n\n",
96 printf (" Special tables and AML tables:\n");
97 printf (ACPI_TABLE_HELP_FORMAT, 1, ACPI_RSDP_NAME, "Root System Description Pointer");
98 printf (ACPI_TABLE_HELP_FORMAT, 2, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
99 printf (ACPI_TABLE_HELP_FORMAT, 3, ACPI_SIG_DSDT, "Differentiated System Description Table");
100 printf (ACPI_TABLE_HELP_FORMAT, 4, ACPI_SIG_SSDT, "Secondary System Description Table");
102 /* All data tables with common table header */
104 printf ("\n Standard ACPI data tables:\n");
105 for (TableData = AcpiDmTableData, i = 5; TableData->Signature; TableData++, i++)
107 printf (ACPI_TABLE_HELP_FORMAT, i, TableData->Signature, TableData->Name);
112 /*******************************************************************************
114 * FUNCTION: UtDisplayConstantOpcodes
120 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
122 ******************************************************************************/
125 UtDisplayConstantOpcodes (
131 printf ("Constant expression opcode information\n\n");
133 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
135 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
137 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
143 /*******************************************************************************
145 * FUNCTION: UtLocalCalloc
147 * PARAMETERS: Size - Bytes to be allocated
149 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
151 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
152 * allocation failure, on the assumption that nothing more can be
155 ******************************************************************************/
164 Allocated = ACPI_ALLOCATE_ZEROED (Size);
167 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
168 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
169 Gbl_InputByteCount, Gbl_CurrentColumn,
170 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
177 TotalAllocated += Size;
182 /*******************************************************************************
184 * FUNCTION: UtBeginEvent
186 * PARAMETERS: Name - Ascii name of this event
188 * RETURN: Event number (integer index)
190 * DESCRIPTION: Saves the current time with this event
192 ******************************************************************************/
199 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
201 AcpiOsPrintf ("Ran out of compiler event structs!\n");
202 return (AslGbl_NextEvent);
205 /* Init event with current (start) time */
207 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
208 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
209 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
211 return (AslGbl_NextEvent++);
215 /*******************************************************************************
217 * FUNCTION: UtEndEvent
219 * PARAMETERS: Event - Event number (integer index)
223 * DESCRIPTION: Saves the current time (end time) with this event
225 ******************************************************************************/
232 if (Event >= ASL_NUM_EVENTS)
237 /* Insert end time for event */
239 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
243 /*******************************************************************************
245 * FUNCTION: UtConvertByteToHex
247 * PARAMETERS: RawByte - Binary data
248 * Buffer - Pointer to where the hex bytes will be
251 * RETURN: Ascii hex byte is stored in Buffer.
253 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
256 ******************************************************************************/
267 Buffer[2] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 4);
268 Buffer[3] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 0);
272 /*******************************************************************************
274 * FUNCTION: UtConvertByteToAsmHex
276 * PARAMETERS: RawByte - Binary data
277 * Buffer - Pointer to where the hex bytes will be
280 * RETURN: Ascii hex byte is stored in Buffer.
282 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
283 * with '0', and a trailing 'h' is added.
285 ******************************************************************************/
288 UtConvertByteToAsmHex (
294 Buffer[1] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 4);
295 Buffer[2] = (UINT8) AcpiUtHexToAsciiChar (RawByte, 0);
300 /*******************************************************************************
304 * PARAMETERS: Type - Type of output
305 * Fmt - Printf format string
306 * ... - variable printf list
310 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
313 ******************************************************************************/
329 if ((Type == ASL_PARSE_OUTPUT) &&
330 (!(AslCompilerdebug)))
335 va_start (Args, Fmt);
336 (void) vfprintf (stderr, Fmt, Args);
342 /*******************************************************************************
344 * FUNCTION: UtPrintFormattedName
346 * PARAMETERS: ParseOpcode - Parser keyword ID
347 * Level - Indentation level
351 * DESCRIPTION: Print the ascii name of the parse opcode.
353 ******************************************************************************/
355 #define TEXT_OFFSET 10
358 UtPrintFormattedName (
365 DbgPrint (ASL_TREE_OUTPUT,
366 "%*s", (3 * Level), " ");
368 DbgPrint (ASL_TREE_OUTPUT,
369 " %-20.20s", UtGetOpName (ParseOpcode));
371 if (Level < TEXT_OFFSET)
373 DbgPrint (ASL_TREE_OUTPUT,
374 "%*s", (TEXT_OFFSET - Level) * 3, " ");
379 /*******************************************************************************
381 * FUNCTION: UtSetParseOpName
383 * PARAMETERS: Op - Parse op to be named.
387 * DESCRIPTION: Insert the ascii name of the parse opcode
389 ******************************************************************************/
393 ACPI_PARSE_OBJECT *Op)
396 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
397 ACPI_MAX_PARSEOP_NAME);
401 /*******************************************************************************
403 * FUNCTION: UtDisplaySummary
405 * PARAMETERS: FileID - ID of outpout file
409 * DESCRIPTION: Display compilation statistics
411 ******************************************************************************/
420 if (FileId != ASL_FILE_STDOUT)
422 /* Compiler name and version number */
424 FlPrintFile (FileId, "%s version %X%s [%s]\n\n",
425 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH, __DATE__);
428 /* Summary of main input and output files */
430 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
433 "%-14s %s - %u lines, %u bytes, %u fields\n",
435 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
436 Gbl_InputByteCount, Gbl_InputFieldCount);
438 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
441 "%-14s %s - %u bytes\n",
443 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
449 "%-14s %s - %u lines, %u bytes, %u keywords\n",
451 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
452 Gbl_InputByteCount, TotalKeywords);
456 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
459 "%-14s %s - %u bytes, %u named objects, %u executable opcodes\n",
461 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
462 TotalNamedObjects, TotalExecutableOpcodes);
466 /* Display summary of any optional files */
468 for (i = ASL_FILE_SOURCE_OUTPUT; i <= ASL_MAX_FILE_TYPE; i++)
470 if (!Gbl_Files[i].Filename || !Gbl_Files[i].Handle)
475 /* .SRC is a temp file unless specifically requested */
477 if ((i == ASL_FILE_SOURCE_OUTPUT) && (!Gbl_SourceOutputFlag))
482 /* .I is a temp file unless specifically requested */
484 if ((i == ASL_FILE_PREPROCESSOR) && (!Gbl_PreprocessorOutputFlag))
489 FlPrintFile (FileId, "%14s %s - %u bytes\n",
490 Gbl_Files[i].ShortDescription,
491 Gbl_Files[i].Filename, FlGetFileSize (i));
497 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
498 Gbl_ExceptionCount[ASL_ERROR],
499 Gbl_ExceptionCount[ASL_WARNING] +
500 Gbl_ExceptionCount[ASL_WARNING2] +
501 Gbl_ExceptionCount[ASL_WARNING3],
502 Gbl_ExceptionCount[ASL_REMARK]);
504 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
507 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
510 FlPrintFile (FileId, "\n");
514 /*******************************************************************************
516 * FUNCTION: UtCheckIntegerRange
518 * PARAMETERS: Op - Integer parse node
519 * LowValue - Smallest allowed value
520 * HighValue - Largest allowed value
522 * RETURN: Op if OK, otherwise NULL
524 * DESCRIPTION: Check integer for an allowable range
526 ******************************************************************************/
529 UtCheckIntegerRange (
530 ACPI_PARSE_OBJECT *Op,
540 if ((Op->Asl.Value.Integer < LowValue) ||
541 (Op->Asl.Value.Integer > HighValue))
543 sprintf (MsgBuffer, "0x%X, allowable: 0x%X-0x%X",
544 (UINT32) Op->Asl.Value.Integer, LowValue, HighValue);
546 AslError (ASL_ERROR, ASL_MSG_RANGE, Op, MsgBuffer);
554 /*******************************************************************************
556 * FUNCTION: UtStringCacheCalloc
558 * PARAMETERS: Length - Size of buffer requested
560 * RETURN: Pointer to the buffer. Aborts on allocation failure
562 * DESCRIPTION: Allocate a string buffer. Bypass the local
563 * dynamic memory manager for performance reasons (This has a
564 * major impact on the speed of the compiler.)
566 ******************************************************************************/
569 UtStringCacheCalloc (
573 ASL_CACHE_INFO *Cache;
576 if (Length > ASL_STRING_CACHE_SIZE)
578 Buffer = UtLocalCalloc (Length);
582 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
584 /* Allocate a new buffer */
586 Cache = UtLocalCalloc (sizeof (Cache->Next) +
587 ASL_STRING_CACHE_SIZE);
589 /* Link new cache buffer to head of list */
591 Cache->Next = Gbl_StringCacheList;
592 Gbl_StringCacheList = Cache;
594 /* Setup cache management pointers */
596 Gbl_StringCacheNext = Cache->Buffer;
597 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE;
601 Gbl_StringSize += Length;
603 Buffer = Gbl_StringCacheNext;
604 Gbl_StringCacheNext += Length;
609 /******************************************************************************
611 * FUNCTION: UtExpandLineBuffers
613 * PARAMETERS: None. Updates global line buffer pointers.
615 * RETURN: None. Reallocates the global line buffers
617 * DESCRIPTION: Called if the current line buffer becomes filled. Reallocates
618 * all global line buffers and updates Gbl_LineBufferSize. NOTE:
619 * Also used for the initial allocation of the buffers, when
620 * all of the buffer pointers are NULL. Initial allocations are
621 * of size ASL_DEFAULT_LINE_BUFFER_SIZE
623 *****************************************************************************/
626 UtExpandLineBuffers (
632 /* Attempt to double the size of all line buffers */
634 NewSize = Gbl_LineBufferSize * 2;
635 if (Gbl_CurrentLineBuffer)
637 DbgPrint (ASL_DEBUG_OUTPUT,
638 "Increasing line buffer size from %u to %u\n",
639 Gbl_LineBufferSize, NewSize);
642 Gbl_CurrentLineBuffer = realloc (Gbl_CurrentLineBuffer, NewSize);
643 Gbl_LineBufPtr = Gbl_CurrentLineBuffer;
644 if (!Gbl_CurrentLineBuffer)
649 Gbl_MainTokenBuffer = realloc (Gbl_MainTokenBuffer, NewSize);
650 if (!Gbl_MainTokenBuffer)
655 Gbl_MacroTokenBuffer = realloc (Gbl_MacroTokenBuffer, NewSize);
656 if (!Gbl_MacroTokenBuffer)
661 Gbl_ExpressionTokenBuffer = realloc (Gbl_ExpressionTokenBuffer, NewSize);
662 if (!Gbl_ExpressionTokenBuffer)
667 Gbl_LineBufferSize = NewSize;
671 /* On error above, simply issue error messages and abort, cannot continue */
674 printf ("Could not increase line buffer size from %u to %u\n",
675 Gbl_LineBufferSize, Gbl_LineBufferSize * 2);
677 AslError (ASL_ERROR, ASL_MSG_BUFFER_ALLOCATION,
683 /******************************************************************************
685 * FUNCTION: UtFreeLineBuffers
691 * DESCRIPTION: Free all line buffers
693 *****************************************************************************/
700 free (Gbl_CurrentLineBuffer);
701 free (Gbl_MainTokenBuffer);
702 free (Gbl_MacroTokenBuffer);
703 free (Gbl_ExpressionTokenBuffer);
707 /*******************************************************************************
709 * FUNCTION: UtInternalizeName
711 * PARAMETERS: ExternalName - Name to convert
712 * ConvertedName - Where the converted name is returned
716 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
718 ******************************************************************************/
723 char **ConvertedName)
725 ACPI_NAMESTRING_INFO Info;
734 /* Get the length of the new internal name */
736 Info.ExternalName = ExternalName;
737 AcpiNsGetInternalNameLength (&Info);
739 /* We need a segment to store the internal name */
741 Info.InternalName = UtStringCacheCalloc (Info.Length);
742 if (!Info.InternalName)
744 return (AE_NO_MEMORY);
749 Status = AcpiNsBuildInternalName (&Info);
750 if (ACPI_FAILURE (Status))
755 *ConvertedName = Info.InternalName;
760 /*******************************************************************************
762 * FUNCTION: UtPadNameWithUnderscores
764 * PARAMETERS: NameSeg - Input nameseg
765 * PaddedNameSeg - Output padded nameseg
767 * RETURN: Padded nameseg.
769 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
772 ******************************************************************************/
775 UtPadNameWithUnderscores (
782 for (i = 0; (i < ACPI_NAME_SIZE); i++)
786 *PaddedNameSeg = *NameSeg;
791 *PaddedNameSeg = '_';
798 /*******************************************************************************
800 * FUNCTION: UtAttachNameseg
802 * PARAMETERS: Op - Parent parse node
803 * Name - Full ExternalName
805 * RETURN: None; Sets the NameSeg field in parent node
807 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
808 * in the NameSeg field of the Op.
810 ******************************************************************************/
814 ACPI_PARSE_OBJECT *Op,
818 char PaddedNameSeg[4];
826 /* Look for the last dot in the namepath */
828 NameSeg = strrchr (Name, '.');
831 /* Found last dot, we have also found the final nameseg */
834 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
838 /* No dots in the namepath, there is only a single nameseg. */
839 /* Handle prefixes */
841 while (ACPI_IS_ROOT_PREFIX (*Name) ||
842 ACPI_IS_PARENT_PREFIX (*Name))
847 /* Remaining string should be one single nameseg */
849 UtPadNameWithUnderscores (Name, PaddedNameSeg);
852 ACPI_MOVE_NAME (Op->Asl.NameSeg, PaddedNameSeg);
856 /*******************************************************************************
858 * FUNCTION: UtAttachNamepathToOwner
860 * PARAMETERS: Op - Parent parse node
861 * NameOp - Node that contains the name
863 * RETURN: Sets the ExternalName and Namepath in the parent node
865 * DESCRIPTION: Store the name in two forms in the parent node: The original
866 * (external) name, and the internalized name that is used within
867 * the ACPI namespace manager.
869 ******************************************************************************/
872 UtAttachNamepathToOwner (
873 ACPI_PARSE_OBJECT *Op,
874 ACPI_PARSE_OBJECT *NameOp)
879 /* Full external path */
881 Op->Asl.ExternalName = NameOp->Asl.Value.String;
883 /* Save the NameOp for possible error reporting later */
885 Op->Asl.ParentMethod = (void *) NameOp;
887 /* Last nameseg of the path */
889 UtAttachNameseg (Op, Op->Asl.ExternalName);
891 /* Create internalized path */
893 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
894 if (ACPI_FAILURE (Status))
896 /* TBD: abort on no memory */
901 /*******************************************************************************
903 * FUNCTION: UtDoConstant
905 * PARAMETERS: String - Hex, Octal, or Decimal string
907 * RETURN: Converted Integer
909 * DESCRIPTION: Convert a string to an integer, with error checking.
911 ******************************************************************************/
922 Status = UtStrtoul64 (String, 0, &Converted);
923 if (ACPI_FAILURE (Status))
925 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
926 AcpiFormatException (Status));
927 AslCompilererror (ErrBuf);
934 /* TBD: use version in ACPICA main code base? */
936 /*******************************************************************************
938 * FUNCTION: UtStrtoul64
940 * PARAMETERS: String - Null terminated string
941 * Terminater - Where a pointer to the terminating byte
943 * Base - Radix of the string
945 * RETURN: Converted value
947 * DESCRIPTION: Convert a string into an unsigned value.
949 ******************************************************************************/
959 UINT64 ReturnValue = 0;
960 ACPI_STATUS Status = AE_OK;
976 * The specified Base parameter is not in the domain of
979 return (AE_BAD_PARAMETER);
982 /* Skip over any white space in the buffer: */
984 while (isspace ((int) *String) || *String == '\t')
990 * The buffer may contain an optional plus or minus sign.
991 * If it does, then skip over it but remember what is was:
998 else if (*String == '+')
1009 * If the input parameter Base is zero, then we need to
1010 * determine if it is octal, decimal, or hexadecimal:
1016 if (tolower ((int) *(++String)) == 'x')
1033 * For octal and hexadecimal bases, skip over the leading
1034 * 0 or 0x, if they are present.
1036 if (Base == 8 && *String == '0')
1043 tolower ((int) *(++String)) == 'x')
1048 /* Main loop: convert the string to an unsigned long */
1052 if (isdigit ((int) *String))
1054 Index = ((UINT8) *String) - '0';
1058 Index = (UINT8) toupper ((int) *String);
1059 if (isupper ((int) Index))
1061 Index = Index - 'A' + 10;
1074 /* Check to see if value is out of range: */
1076 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
1083 ReturnValue *= Base;
1084 ReturnValue += Index;
1091 /* If a minus sign was present, then "the conversion is negated": */
1093 if (Sign == NEGATIVE)
1095 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
1098 *RetInteger = ReturnValue;
1107 Status = AE_BAD_OCTAL_CONSTANT;
1112 Status = AE_BAD_DECIMAL_CONSTANT;
1117 Status = AE_BAD_HEX_CONSTANT;
1122 /* Base validated above */