1 /******************************************************************************
3 * Module Name: aslutils -- compiler utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2013, 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: UtHexCharToValue
248 * PARAMETERS: HexChar - Hex character in Ascii
250 * RETURN: The binary value of the hex character
252 * DESCRIPTION: Perform ascii-to-hex translation
254 ******************************************************************************/
263 return ((UINT8
) (HexChar
- 0x30));
268 return ((UINT8
) (HexChar
- 0x37));
271 return ((UINT8
) (HexChar
- 0x57));
275 /*******************************************************************************
277 * FUNCTION: UtConvertByteToHex
279 * PARAMETERS: RawByte - Binary data
280 * Buffer - Pointer to where the hex bytes will be
283 * RETURN: Ascii hex byte is stored in Buffer.
285 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
288 ******************************************************************************/
299 Buffer
[2] = (UINT8
) AslHexLookup
[(RawByte
>> 4) & 0xF];
300 Buffer
[3] = (UINT8
) AslHexLookup
[RawByte
& 0xF];
304 /*******************************************************************************
306 * FUNCTION: UtConvertByteToAsmHex
308 * PARAMETERS: RawByte - Binary data
309 * Buffer - Pointer to where the hex bytes will be
312 * RETURN: Ascii hex byte is stored in Buffer.
314 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed
317 ******************************************************************************/
320 UtConvertByteToAsmHex (
326 Buffer
[1] = (UINT8
) AslHexLookup
[(RawByte
>> 4) & 0xF];
327 Buffer
[2] = (UINT8
) AslHexLookup
[RawByte
& 0xF];
332 /*******************************************************************************
336 * PARAMETERS: Type - Type of output
337 * Fmt - Printf format string
338 * ... - variable printf list
342 * DESCRIPTION: Conditional print statement. Prints to stderr only if the
345 ******************************************************************************/
361 if ((Type
== ASL_PARSE_OUTPUT
) &&
362 (!(AslCompilerdebug
)))
367 va_start (Args
, Fmt
);
368 (void) vfprintf (stderr
, Fmt
, Args
);
374 /*******************************************************************************
376 * FUNCTION: UtPrintFormattedName
378 * PARAMETERS: ParseOpcode - Parser keyword ID
379 * Level - Indentation level
383 * DESCRIPTION: Print the ascii name of the parse opcode.
385 ******************************************************************************/
387 #define TEXT_OFFSET 10
390 UtPrintFormattedName (
397 DbgPrint (ASL_TREE_OUTPUT
,
398 "%*s", (3 * Level
), " ");
400 DbgPrint (ASL_TREE_OUTPUT
,
401 " %-20.20s", UtGetOpName (ParseOpcode
));
403 if (Level
< TEXT_OFFSET
)
405 DbgPrint (ASL_TREE_OUTPUT
,
406 "%*s", (TEXT_OFFSET
- Level
) * 3, " ");
411 /*******************************************************************************
413 * FUNCTION: UtSetParseOpName
415 * PARAMETERS: Op - Parse op to be named.
419 * DESCRIPTION: Insert the ascii name of the parse opcode
421 ******************************************************************************/
425 ACPI_PARSE_OBJECT
*Op
)
428 strncpy (Op
->Asl
.ParseOpName
, UtGetOpName (Op
->Asl
.ParseOpcode
),
429 ACPI_MAX_PARSEOP_NAME
);
433 /*******************************************************************************
435 * FUNCTION: UtDisplaySummary
437 * PARAMETERS: FileID - ID of outpout file
441 * DESCRIPTION: Display compilation statistics
443 ******************************************************************************/
452 if (FileId
!= ASL_FILE_STDOUT
)
454 /* Compiler name and version number */
456 FlPrintFile (FileId
, "%s version %X%s [%s]\n\n",
457 ASL_COMPILER_NAME
, (UINT32
) ACPI_CA_VERSION
, ACPI_WIDTH
, __DATE__
);
460 /* Summary of main input and output files */
462 if (Gbl_FileType
== ASL_INPUT_TYPE_ASCII_DATA
)
465 "%-14s %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 "%-14s %s - %u bytes\n",
475 Gbl_Files
[ASL_FILE_AML_OUTPUT
].Filename
, Gbl_TableLength
);
481 "%-14s %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 "%-14s %s - %u bytes, %u named objects, %u executable opcodes\n",
493 Gbl_Files
[ASL_FILE_AML_OUTPUT
].Filename
, Gbl_TableLength
,
494 TotalNamedObjects
, TotalExecutableOpcodes
);
498 /* Display summary of any optional files */
500 for (i
= ASL_FILE_SOURCE_OUTPUT
; i
<= ASL_MAX_FILE_TYPE
; i
++)
502 if (!Gbl_Files
[i
].Filename
|| !Gbl_Files
[i
].Handle
)
507 /* .SRC is a temp file unless specifically requested */
509 if ((i
== ASL_FILE_SOURCE_OUTPUT
) && (!Gbl_SourceOutputFlag
))
514 /* .I is a temp file unless specifically requested */
516 if ((i
== ASL_FILE_PREPROCESSOR
) && (!Gbl_PreprocessorOutputFlag
))
521 FlPrintFile (FileId
, "%14s %s - %u bytes\n",
522 Gbl_Files
[i
].ShortDescription
,
523 Gbl_Files
[i
].Filename
, FlGetFileSize (i
));
529 "\nCompilation complete. %u Errors, %u Warnings, %u Remarks",
530 Gbl_ExceptionCount
[ASL_ERROR
],
531 Gbl_ExceptionCount
[ASL_WARNING
] +
532 Gbl_ExceptionCount
[ASL_WARNING2
] +
533 Gbl_ExceptionCount
[ASL_WARNING3
],
534 Gbl_ExceptionCount
[ASL_REMARK
]);
536 if (Gbl_FileType
!= ASL_INPUT_TYPE_ASCII_DATA
)
539 ", %u Optimizations", Gbl_ExceptionCount
[ASL_OPTIMIZATION
]);
542 FlPrintFile (FileId
, "\n");
546 /*******************************************************************************
548 * FUNCTION: UtCheckIntegerRange
550 * PARAMETERS: Op - Integer parse node
551 * LowValue - Smallest allowed value
552 * HighValue - Largest allowed value
554 * RETURN: Op if OK, otherwise NULL
556 * DESCRIPTION: Check integer for an allowable range
558 ******************************************************************************/
561 UtCheckIntegerRange (
562 ACPI_PARSE_OBJECT
*Op
,
572 if ((Op
->Asl
.Value
.Integer
< LowValue
) ||
573 (Op
->Asl
.Value
.Integer
> HighValue
))
575 sprintf (MsgBuffer
, "0x%X, allowable: 0x%X-0x%X",
576 (UINT32
) Op
->Asl
.Value
.Integer
, LowValue
, HighValue
);
578 AslError (ASL_ERROR
, ASL_MSG_RANGE
, Op
, MsgBuffer
);
586 /*******************************************************************************
588 * FUNCTION: UtGetStringBuffer
590 * PARAMETERS: Length - Size of buffer requested
592 * RETURN: Pointer to the buffer. Aborts on allocation failure
594 * DESCRIPTION: Allocate a string buffer. Bypass the local
595 * dynamic memory manager for performance reasons (This has a
596 * major impact on the speed of the compiler.)
598 ******************************************************************************/
607 if ((Gbl_StringCacheNext
+ Length
) >= Gbl_StringCacheLast
)
609 Gbl_StringCacheNext
= UtLocalCalloc (ASL_STRING_CACHE_SIZE
+ Length
);
610 Gbl_StringCacheLast
= Gbl_StringCacheNext
+ ASL_STRING_CACHE_SIZE
+
614 Buffer
= Gbl_StringCacheNext
;
615 Gbl_StringCacheNext
+= Length
;
621 /******************************************************************************
623 * FUNCTION: UtExpandLineBuffers
625 * PARAMETERS: None. Updates global line buffer pointers.
627 * RETURN: None. Reallocates the global line buffers
629 * DESCRIPTION: Called if the current line buffer becomes filled. Reallocates
630 * all global line buffers and updates Gbl_LineBufferSize. NOTE:
631 * Also used for the initial allocation of the buffers, when
632 * all of the buffer pointers are NULL. Initial allocations are
633 * of size ASL_DEFAULT_LINE_BUFFER_SIZE
635 *****************************************************************************/
638 UtExpandLineBuffers (
644 /* Attempt to double the size of all line buffers */
646 NewSize
= Gbl_LineBufferSize
* 2;
647 if (Gbl_CurrentLineBuffer
)
649 DbgPrint (ASL_DEBUG_OUTPUT
,"Increasing line buffer size from %u to %u\n",
650 Gbl_LineBufferSize
, NewSize
);
653 Gbl_CurrentLineBuffer
= realloc (Gbl_CurrentLineBuffer
, NewSize
);
654 Gbl_LineBufPtr
= Gbl_CurrentLineBuffer
;
655 if (!Gbl_CurrentLineBuffer
)
660 Gbl_MainTokenBuffer
= realloc (Gbl_MainTokenBuffer
, NewSize
);
661 if (!Gbl_MainTokenBuffer
)
666 Gbl_MacroTokenBuffer
= realloc (Gbl_MacroTokenBuffer
, NewSize
);
667 if (!Gbl_MacroTokenBuffer
)
672 Gbl_ExpressionTokenBuffer
= realloc (Gbl_ExpressionTokenBuffer
, NewSize
);
673 if (!Gbl_ExpressionTokenBuffer
)
678 Gbl_LineBufferSize
= NewSize
;
682 /* On error above, simply issue error messages and abort, cannot continue */
685 printf ("Could not increase line buffer size from %u to %u\n",
686 Gbl_LineBufferSize
, Gbl_LineBufferSize
* 2);
688 AslError (ASL_ERROR
, ASL_MSG_BUFFER_ALLOCATION
,
694 /*******************************************************************************
696 * FUNCTION: UtInternalizeName
698 * PARAMETERS: ExternalName - Name to convert
699 * ConvertedName - Where the converted name is returned
703 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name
705 ******************************************************************************/
710 char **ConvertedName
)
712 ACPI_NAMESTRING_INFO Info
;
721 /* Get the length of the new internal name */
723 Info
.ExternalName
= ExternalName
;
724 AcpiNsGetInternalNameLength (&Info
);
726 /* We need a segment to store the internal name */
728 Info
.InternalName
= UtGetStringBuffer (Info
.Length
);
729 if (!Info
.InternalName
)
731 return (AE_NO_MEMORY
);
736 Status
= AcpiNsBuildInternalName (&Info
);
737 if (ACPI_FAILURE (Status
))
742 *ConvertedName
= Info
.InternalName
;
747 /*******************************************************************************
749 * FUNCTION: UtPadNameWithUnderscores
751 * PARAMETERS: NameSeg - Input nameseg
752 * PaddedNameSeg - Output padded nameseg
754 * RETURN: Padded nameseg.
756 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full
759 ******************************************************************************/
762 UtPadNameWithUnderscores (
769 for (i
= 0; (i
< ACPI_NAME_SIZE
); i
++)
773 *PaddedNameSeg
= *NameSeg
;
778 *PaddedNameSeg
= '_';
785 /*******************************************************************************
787 * FUNCTION: UtAttachNameseg
789 * PARAMETERS: Op - Parent parse node
790 * Name - Full ExternalName
792 * RETURN: None; Sets the NameSeg field in parent node
794 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it
795 * in the NameSeg field of the Op.
797 ******************************************************************************/
801 ACPI_PARSE_OBJECT
*Op
,
805 char PaddedNameSeg
[4];
813 /* Look for the last dot in the namepath */
815 NameSeg
= strrchr (Name
, '.');
818 /* Found last dot, we have also found the final nameseg */
821 UtPadNameWithUnderscores (NameSeg
, PaddedNameSeg
);
825 /* No dots in the namepath, there is only a single nameseg. */
826 /* Handle prefixes */
828 while (ACPI_IS_ROOT_PREFIX (*Name
) ||
829 ACPI_IS_PARENT_PREFIX (*Name
))
834 /* Remaining string should be one single nameseg */
836 UtPadNameWithUnderscores (Name
, PaddedNameSeg
);
839 ACPI_MOVE_NAME (Op
->Asl
.NameSeg
, PaddedNameSeg
);
843 /*******************************************************************************
845 * FUNCTION: UtAttachNamepathToOwner
847 * PARAMETERS: Op - Parent parse node
848 * NameOp - Node that contains the name
850 * RETURN: Sets the ExternalName and Namepath in the parent node
852 * DESCRIPTION: Store the name in two forms in the parent node: The original
853 * (external) name, and the internalized name that is used within
854 * the ACPI namespace manager.
856 ******************************************************************************/
859 UtAttachNamepathToOwner (
860 ACPI_PARSE_OBJECT
*Op
,
861 ACPI_PARSE_OBJECT
*NameOp
)
866 /* Full external path */
868 Op
->Asl
.ExternalName
= NameOp
->Asl
.Value
.String
;
870 /* Save the NameOp for possible error reporting later */
872 Op
->Asl
.ParentMethod
= (void *) NameOp
;
874 /* Last nameseg of the path */
876 UtAttachNameseg (Op
, Op
->Asl
.ExternalName
);
878 /* Create internalized path */
880 Status
= UtInternalizeName (NameOp
->Asl
.Value
.String
, &Op
->Asl
.Namepath
);
881 if (ACPI_FAILURE (Status
))
883 /* TBD: abort on no memory */
888 /*******************************************************************************
890 * FUNCTION: UtDoConstant
892 * PARAMETERS: String - Hex, Octal, or Decimal string
894 * RETURN: Converted Integer
896 * DESCRIPTION: Convert a string to an integer, with error checking.
898 ******************************************************************************/
909 Status
= UtStrtoul64 (String
, 0, &Converted
);
910 if (ACPI_FAILURE (Status
))
912 sprintf (ErrBuf
, "%s %s\n", "Conversion error:",
913 AcpiFormatException (Status
));
914 AslCompilererror (ErrBuf
);
921 /* TBD: use version in ACPI CA main code base? */
923 /*******************************************************************************
925 * FUNCTION: UtStrtoul64
927 * PARAMETERS: String - Null terminated string
928 * Terminater - Where a pointer to the terminating byte
930 * Base - Radix of the string
932 * RETURN: Converted value
934 * DESCRIPTION: Convert a string into an unsigned value.
936 ******************************************************************************/
946 UINT64 ReturnValue
= 0;
947 ACPI_STATUS Status
= AE_OK
;
963 * The specified Base parameter is not in the domain of
966 return (AE_BAD_PARAMETER
);
969 /* Skip over any white space in the buffer: */
971 while (isspace ((int) *String
) || *String
== '\t')
977 * The buffer may contain an optional plus or minus sign.
978 * If it does, then skip over it but remember what is was:
985 else if (*String
== '+')
996 * If the input parameter Base is zero, then we need to
997 * determine if it is octal, decimal, or hexadecimal:
1003 if (tolower ((int) *(++String
)) == 'x')
1020 * For octal and hexadecimal bases, skip over the leading
1021 * 0 or 0x, if they are present.
1023 if (Base
== 8 && *String
== '0')
1030 tolower ((int) *(++String
)) == 'x')
1035 /* Main loop: convert the string to an unsigned long */
1039 if (isdigit ((int) *String
))
1041 Index
= ((UINT8
) *String
) - '0';
1045 Index
= (UINT8
) toupper ((int) *String
);
1046 if (isupper ((int) Index
))
1048 Index
= Index
- 'A' + 10;
1061 /* Check to see if value is out of range: */
1063 if (ReturnValue
> ((ACPI_UINT64_MAX
- (UINT64
) Index
) /
1070 ReturnValue
*= Base
;
1071 ReturnValue
+= Index
;
1078 /* If a minus sign was present, then "the conversion is negated": */
1080 if (Sign
== NEGATIVE
)
1082 ReturnValue
= (ACPI_UINT32_MAX
- ReturnValue
) + 1;
1085 *RetInteger
= ReturnValue
;
1094 Status
= AE_BAD_OCTAL_CONSTANT
;
1099 Status
= AE_BAD_DECIMAL_CONSTANT
;
1104 Status
= AE_BAD_HEX_CONSTANT
;
1109 /* Base validated above */