2 /******************************************************************************
4 * Module Name: aslutils -- compiler utilities
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, 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")
58 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
62 /* Local prototypes */
65 UtPadNameWithUnderscores (
71 ACPI_PARSE_OBJECT *Op,
75 /*******************************************************************************
77 * FUNCTION: UtDisplaySupportedTables
83 * DESCRIPTION: Print all supported ACPI table names.
85 ******************************************************************************/
88 UtDisplaySupportedTables (
91 ACPI_DMTABLE_DATA *TableData;
95 printf ("\nACPI tables supported by iASL subsystems in "
97 " ASL and Data Table compilers\n"
98 " AML and Data Table disassemblers\n"
99 " ACPI table template generator\n\n", ACPI_CA_VERSION);
103 printf ("%8u) %s %s\n", 1, ACPI_SIG_DSDT, "Differentiated System Description Table");
104 printf ("%8u) %s %s\n", 2, ACPI_SIG_SSDT, "Secondary System Description Table");
105 printf ("%8u) %s %s\n", 3, ACPI_SIG_FADT, "Fixed ACPI Description Table (FADT)");
106 printf ("%8u) %s %s\n", 4, ACPI_SIG_FACS, "Firmware ACPI Control Structure");
107 printf ("%8u) %s %s\n", 5, ACPI_RSDP_NAME, "Root System Description Pointer");
109 /* All data tables with common table header */
111 for (TableData = AcpiDmTableData; TableData->Signature; TableData++)
113 printf ("%8u) %s %s\n", i, TableData->Signature, TableData->Name);
119 /*******************************************************************************
121 * FUNCTION: AcpiPsDisplayConstantOpcodes
127 * DESCRIPTION: Print AML opcodes that can be used in constant expressions.
129 ******************************************************************************/
132 UtDisplayConstantOpcodes (
138 printf ("Constant expression opcode information\n\n");
140 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++)
142 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT)
144 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name);
150 /*******************************************************************************
152 * FUNCTION: UtLocalCalloc
154 * PARAMETERS: Size - Bytes to be allocated
156 * RETURN: Pointer to the allocated memory. Guaranteed to be valid.
158 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an
159 * allocation failure, on the assumption that nothing more can be
162 ******************************************************************************/
171 Allocated = ACPI_ALLOCATE_ZEROED (Size);
174 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION,
175 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber,
176 Gbl_InputByteCount, Gbl_CurrentColumn,
177 Gbl_Files[ASL_FILE_INPUT].Filename, NULL);
184 TotalAllocated += Size;
189 /*******************************************************************************
191 * FUNCTION: UtBeginEvent
193 * PARAMETERS: Name - Ascii name of this event
195 * RETURN: Event - Event number (integer index)
197 * DESCRIPTION: Saves the current time with this event
199 ******************************************************************************/
206 if (AslGbl_NextEvent >= ASL_NUM_EVENTS)
208 AcpiOsPrintf ("Ran out of compiler event structs!\n");
209 return (AslGbl_NextEvent);
212 /* Init event with current (start) time */
214 AslGbl_Events[AslGbl_NextEvent].StartTime = AcpiOsGetTimer ();
215 AslGbl_Events[AslGbl_NextEvent].EventName = Name;
216 AslGbl_Events[AslGbl_NextEvent].Valid = TRUE;
218 return (AslGbl_NextEvent++);
222 /*******************************************************************************
224 * FUNCTION: UtEndEvent
226 * PARAMETERS: Event - Event number (integer index)
230 * DESCRIPTION: Saves the current time (end time) with this event
232 ******************************************************************************/
239 if (Event >= ASL_NUM_EVENTS)
244 /* Insert end time for event */
246 AslGbl_Events[Event].EndTime = AcpiOsGetTimer ();
250 /*******************************************************************************
252 * FUNCTION: UtHexCharToValue
254 * PARAMETERS: HexChar - Hex character in Ascii
256 * RETURN: The binary value of the hex character
258 * DESCRIPTION: Perform ascii-to-hex translation
260 ******************************************************************************/
269 return ((UINT8) (HexChar - 0x30));
274 return ((UINT8) (HexChar - 0x37));
277 return ((UINT8) (HexChar - 0x57));
281 /*******************************************************************************
283 * FUNCTION: UtConvertByteToHex
285 * PARAMETERS: RawByte - Binary data
286 * Buffer - Pointer to where the hex bytes will be stored
288 * RETURN: Ascii hex byte is stored in Buffer.
290 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
293 ******************************************************************************/
304 Buffer[2] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
305 Buffer[3] = (UINT8) AslHexLookup[RawByte & 0xF];
309 /*******************************************************************************
311 * FUNCTION: UtConvertByteToAsmHex
313 * PARAMETERS: RawByte - Binary data
314 * Buffer - Pointer to where the hex bytes will be stored
316 * RETURN: Ascii hex byte is stored in Buffer.
318 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
321 ******************************************************************************/
324 UtConvertByteToAsmHex (
330 Buffer[1] = (UINT8) AslHexLookup[(RawByte >> 4) & 0xF];
331 Buffer[2] = (UINT8) AslHexLookup[RawByte & 0xF];
336 /*******************************************************************************
340 * PARAMETERS: Type - Type of output
341 * Fmt - Printf format string
342 * ... - variable printf list
346 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
349 ******************************************************************************/
360 va_start (Args, Fmt);
367 if ((Type == ASL_PARSE_OUTPUT) &&
368 (!(AslCompilerdebug)))
373 (void) vfprintf (stderr, Fmt, Args);
379 /*******************************************************************************
381 * FUNCTION: UtPrintFormattedName
383 * PARAMETERS: ParseOpcode - Parser keyword ID
384 * Level - Indentation level
388 * DESCRIPTION: Print the ascii name of the parse opcode.
390 ******************************************************************************/
392 #define TEXT_OFFSET 10
395 UtPrintFormattedName (
402 DbgPrint (ASL_TREE_OUTPUT,
403 "%*s", (3 * Level), " ");
405 DbgPrint (ASL_TREE_OUTPUT,
406 " %-20.20s", UtGetOpName (ParseOpcode));
408 if (Level < TEXT_OFFSET)
410 DbgPrint (ASL_TREE_OUTPUT,
411 "%*s", (TEXT_OFFSET - Level) * 3, " ");
416 /*******************************************************************************
418 * FUNCTION: UtSetParseOpName
424 * DESCRIPTION: Insert the ascii name of the parse opcode
426 ******************************************************************************/
430 ACPI_PARSE_OBJECT *Op)
433 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode),
434 ACPI_MAX_PARSEOP_NAME);
438 /*******************************************************************************
440 * FUNCTION: UtDisplaySummary
442 * PARAMETERS: FileID - ID of outpout file
446 * DESCRIPTION: Display compilation statistics
448 ******************************************************************************/
455 if (FileId != ASL_FILE_STDOUT)
457 /* Compiler name and version number */
459 FlPrintFile (FileId, "%s version %X%s\n",
460 ASL_COMPILER_NAME, (UINT32) ACPI_CA_VERSION, ACPI_WIDTH);
463 if (Gbl_FileType == ASL_INPUT_TYPE_ASCII_DATA)
466 "Table Input: %s - %u lines, %u bytes, %u fields\n",
467 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
468 Gbl_InputByteCount, Gbl_InputFieldCount);
470 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
473 "Binary Output: %s - %u bytes\n\n",
474 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength);
479 /* Input/Output summary */
482 "ASL Input: %s - %u lines, %u bytes, %u keywords\n",
483 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber,
484 Gbl_InputByteCount, TotalKeywords);
488 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors))
491 "AML Output: %s - %u bytes, %u named objects, %u executable opcodes\n\n",
492 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength,
493 TotalNamedObjects, TotalExecutableOpcodes);
500 "Compilation complete. %u Errors, %u Warnings, %u Remarks",
501 Gbl_ExceptionCount[ASL_ERROR],
502 Gbl_ExceptionCount[ASL_WARNING] +
503 Gbl_ExceptionCount[ASL_WARNING2] +
504 Gbl_ExceptionCount[ASL_WARNING3],
505 Gbl_ExceptionCount[ASL_REMARK]);
507 if (Gbl_FileType != ASL_INPUT_TYPE_ASCII_DATA)
510 ", %u Optimizations", Gbl_ExceptionCount[ASL_OPTIMIZATION]);
513 FlPrintFile (FileId, "\n");
517 /*******************************************************************************
519 * FUNCTION: UtDisplaySummary
521 * PARAMETERS: Op - Integer parse node
522 * LowValue - Smallest allowed value
523 * HighValue - Largest allowed value
525 * RETURN: Op if OK, otherwise NULL
527 * DESCRIPTION: Check integer for an allowable range
529 ******************************************************************************/
532 UtCheckIntegerRange (
533 ACPI_PARSE_OBJECT *Op,
537 char *ParseError = NULL;
546 if (Op->Asl.Value.Integer < LowValue)
548 ParseError = "Value below valid range";
549 Op->Asl.Value.Integer = LowValue;
552 if (Op->Asl.Value.Integer > HighValue)
554 ParseError = "Value above valid range";
555 Op->Asl.Value.Integer = HighValue;
560 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue);
561 AslCompilererror (Buffer);
570 /*******************************************************************************
572 * FUNCTION: UtGetStringBuffer
574 * PARAMETERS: Length - Size of buffer requested
576 * RETURN: Pointer to the buffer. Aborts on allocation failure
578 * DESCRIPTION: Allocate a string buffer. Bypass the local
579 * dynamic memory manager for performance reasons (This has a
580 * major impact on the speed of the compiler.)
582 ******************************************************************************/
591 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast)
593 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length);
594 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE +
598 Buffer = Gbl_StringCacheNext;
599 Gbl_StringCacheNext += Length;
605 /*******************************************************************************
607 * FUNCTION: UtInternalizeName
609 * PARAMETERS: ExternalName - Name to convert
610 * ConvertedName - Where the converted name is returned
614 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
616 ******************************************************************************/
621 char **ConvertedName)
623 ACPI_NAMESTRING_INFO Info;
632 /* Get the length of the new internal name */
634 Info.ExternalName = ExternalName;
635 AcpiNsGetInternalNameLength (&Info);
637 /* We need a segment to store the internal name */
639 Info.InternalName = UtGetStringBuffer (Info.Length);
640 if (!Info.InternalName)
642 return (AE_NO_MEMORY);
647 Status = AcpiNsBuildInternalName (&Info);
648 if (ACPI_FAILURE (Status))
653 *ConvertedName = Info.InternalName;
658 /*******************************************************************************
660 * FUNCTION: UtPadNameWithUnderscores
662 * PARAMETERS: NameSeg - Input nameseg
663 * PaddedNameSeg - Output padded nameseg
665 * RETURN: Padded nameseg.
667 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
670 ******************************************************************************/
673 UtPadNameWithUnderscores (
680 for (i = 0; (i < ACPI_NAME_SIZE); i++)
684 *PaddedNameSeg = *NameSeg;
689 *PaddedNameSeg = '_';
696 /*******************************************************************************
698 * FUNCTION: UtAttachNameseg
700 * PARAMETERS: Op - Parent parse node
701 * Name - Full ExternalName
703 * RETURN: None; Sets the NameSeg field in parent node
705 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
706 * in the NameSeg field of the Op.
708 ******************************************************************************/
712 ACPI_PARSE_OBJECT *Op,
716 char PaddedNameSeg[4];
724 /* Look for the last dot in the namepath */
726 NameSeg = strrchr (Name, '.');
729 /* Found last dot, we have also found the final nameseg */
732 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg);
736 /* No dots in the namepath, there is only a single nameseg. */
737 /* Handle prefixes */
739 while ((*Name == '\\') || (*Name == '^'))
744 /* Remaing string should be one single nameseg */
746 UtPadNameWithUnderscores (Name, PaddedNameSeg);
749 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4);
753 /*******************************************************************************
755 * FUNCTION: UtAttachNamepathToOwner
757 * PARAMETERS: Op - Parent parse node
758 * NameOp - Node that contains the name
760 * RETURN: Sets the ExternalName and Namepath in the parent node
762 * DESCRIPTION: Store the name in two forms in the parent node: The original
763 * (external) name, and the internalized name that is used within
764 * the ACPI namespace manager.
766 ******************************************************************************/
769 UtAttachNamepathToOwner (
770 ACPI_PARSE_OBJECT *Op,
771 ACPI_PARSE_OBJECT *NameOp)
776 /* Full external path */
778 Op->Asl.ExternalName = NameOp->Asl.Value.String;
780 /* Save the NameOp for possible error reporting later */
782 Op->Asl.ParentMethod = (void *) NameOp;
784 /* Last nameseg of the path */
786 UtAttachNameseg (Op, Op->Asl.ExternalName);
788 /* Create internalized path */
790 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath);
791 if (ACPI_FAILURE (Status))
793 /* TBD: abort on no memory */
798 /*******************************************************************************
800 * FUNCTION: UtDoConstant
802 * PARAMETERS: String - Hex, Octal, or Decimal string
804 * RETURN: Converted Integer
806 * DESCRIPTION: Convert a string to an integer. With error checking.
808 ******************************************************************************/
819 Status = UtStrtoul64 (String, 0, &Converted);
820 if (ACPI_FAILURE (Status))
822 sprintf (ErrBuf, "%s %s\n", "Conversion error:",
823 AcpiFormatException (Status));
824 AslCompilererror (ErrBuf);
831 /* TBD: use version in ACPI CA main code base? */
833 /*******************************************************************************
835 * FUNCTION: UtStrtoul64
837 * PARAMETERS: String - Null terminated string
838 * Terminater - Where a pointer to the terminating byte is
840 * Base - Radix of the string
842 * RETURN: Converted value
844 * DESCRIPTION: Convert a string into an unsigned value.
846 ******************************************************************************/
856 UINT64 ReturnValue = 0;
857 ACPI_STATUS Status = AE_OK;
872 * The specified Base parameter is not in the domain of
875 return (AE_BAD_PARAMETER);
878 /* Skip over any white space in the buffer: */
880 while (isspace ((int) *String) || *String == '\t')
886 * The buffer may contain an optional plus or minus sign.
887 * If it does, then skip over it but remember what is was:
894 else if (*String == '+')
905 * If the input parameter Base is zero, then we need to
906 * determine if it is octal, decimal, or hexadecimal:
912 if (tolower ((int) *(++String)) == 'x')
929 * For octal and hexadecimal bases, skip over the leading
930 * 0 or 0x, if they are present.
932 if (Base == 8 && *String == '0')
939 tolower ((int) *(++String)) == 'x')
944 /* Main loop: convert the string to an unsigned long */
948 if (isdigit ((int) *String))
950 Index = ((UINT8) *String) - '0';
954 Index = (UINT8) toupper ((int) *String);
955 if (isupper ((int) Index))
957 Index = Index - 'A' + 10;
970 /* Check to see if value is out of range: */
972 if (ReturnValue > ((ACPI_UINT64_MAX - (UINT64) Index) /
980 ReturnValue += Index;
987 /* If a minus sign was present, then "the conversion is negated": */
989 if (Sign == NEGATIVE)
991 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1;
994 *RetInteger = ReturnValue;
1002 Status = AE_BAD_OCTAL_CONSTANT;
1006 Status = AE_BAD_DECIMAL_CONSTANT;
1010 Status = AE_BAD_HEX_CONSTANT;
1014 /* Base validated above */