1 2 /****************************************************************************** 3 * 4 * Module Name: aslutils -- compiler utilities 5 * $Revision: 58 $ 6 * 7 *****************************************************************************/ 8 9 /****************************************************************************** 10 * 11 * 1. Copyright Notice 12 * 13 * Some or all of this work - Copyright (c) 1999 - 2004, Intel Corp. 14 * All rights reserved. 15 * 16 * 2. License 17 * 18 * 2.1. This is your license from Intel Corp. under its intellectual property 19 * rights. You may have additional license terms from the party that provided 20 * you this software, covering your right to use that party's intellectual 21 * property rights. 22 * 23 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 24 * copy of the source code appearing in this file ("Covered Code") an 25 * irrevocable, perpetual, worldwide license under Intel's copyrights in the 26 * base code distributed originally by Intel ("Original Intel Code") to copy, 27 * make derivatives, distribute, use and display any portion of the Covered 28 * Code in any form, with the right to sublicense such rights; and 29 * 30 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 31 * license (with the right to sublicense), under only those claims of Intel 32 * patents that are infringed by the Original Intel Code, to make, use, sell, 33 * offer to sell, and import the Covered Code and derivative works thereof 34 * solely to the minimum extent necessary to exercise the above copyright 35 * license, and in no event shall the patent license extend to any additions 36 * to or modifications of the Original Intel Code. No other license or right 37 * is granted directly or by implication, estoppel or otherwise; 38 * 39 * The above copyright and patent license is granted only if the following 40 * conditions are met: 41 * 42 * 3. Conditions 43 * 44 * 3.1. Redistribution of Source with Rights to Further Distribute Source. 45 * Redistribution of source code of any substantial portion of the Covered 46 * Code or modification with rights to further distribute source must include 47 * the above Copyright Notice, the above License, this list of Conditions, 48 * and the following Disclaimer and Export Compliance provision. In addition, 49 * Licensee must cause all Covered Code to which Licensee contributes to 50 * contain a file documenting the changes Licensee made to create that Covered 51 * Code and the date of any change. Licensee must include in that file the 52 * documentation of any changes made by any predecessor Licensee. Licensee 53 * must include a prominent statement that the modification is derived, 54 * directly or indirectly, from Original Intel Code. 55 * 56 * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 57 * Redistribution of source code of any substantial portion of the Covered 58 * Code or modification without rights to further distribute source must 59 * include the following Disclaimer and Export Compliance provision in the 60 * documentation and/or other materials provided with distribution. In 61 * addition, Licensee may not authorize further sublicense of source of any 62 * portion of the Covered Code, and must include terms to the effect that the 63 * license from Licensee to its licensee is limited to the intellectual 64 * property embodied in the software Licensee provides to its licensee, and 65 * not to intellectual property embodied in modifications its licensee may 66 * make. 67 * 68 * 3.3. Redistribution of Executable. Redistribution in executable form of any 69 * substantial portion of the Covered Code or modification must reproduce the 70 * above Copyright Notice, and the following Disclaimer and Export Compliance 71 * provision in the documentation and/or other materials provided with the 72 * distribution. 73 * 74 * 3.4. Intel retains all right, title, and interest in and to the Original 75 * Intel Code. 76 * 77 * 3.5. Neither the name Intel nor any other trademark owned or controlled by 78 * Intel shall be used in advertising or otherwise to promote the sale, use or 79 * other dealings in products derived from or relating to the Covered Code 80 * without prior written authorization from Intel. 81 * 82 * 4. Disclaimer and Export Compliance 83 * 84 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 85 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 86 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 87 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 88 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 89 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 90 * PARTICULAR PURPOSE. 91 * 92 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 93 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 94 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 95 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 96 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 97 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 98 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 99 * LIMITED REMEDY. 100 * 101 * 4.3. Licensee shall not export, either directly or indirectly, any of this 102 * software or system incorporating such software without first obtaining any 103 * required license or other approval from the U. S. Department of Commerce or 104 * any other agency or department of the United States Government. In the 105 * event Licensee exports any such software from the United States or 106 * re-exports any such software from a foreign destination, Licensee shall 107 * ensure that the distribution and export/re-export of the software is in 108 * compliance with all laws, regulations, orders, or other restrictions of the 109 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 110 * any of its subsidiaries will export/re-export any technical data, process, 111 * software, or service, directly or indirectly, to any country for which the 112 * United States government or any agency thereof requires an export license, 113 * other governmental approval, or letter of assurance, without first obtaining 114 * such license, approval or letter. 115 * 116 *****************************************************************************/ 117 118 119 #include "aslcompiler.h" 120 #include "aslcompiler.y.h" 121 #include "acnamesp.h" 122 #include "amlcode.h" 123 124 #define _COMPONENT ACPI_COMPILER 125 ACPI_MODULE_NAME ("aslutils") 126 127 #ifdef _USE_BERKELEY_YACC 128 extern const char * const AslCompilername[]; 129 static const char * const *yytname = &AslCompilername[254]; 130 #else 131 extern const char * const yytname[]; 132 #endif 133 134 135 void 136 AslOptimizeNamepath ( 137 char *Buffer) 138 { 139 printf ("NamePath: %s\n", Buffer); 140 } 141 142 143 /******************************************************************************* 144 * 145 * FUNCTION: AcpiPsDisplayConstantOpcodes 146 * 147 * PARAMETERS: None 148 * 149 * RETURN: None 150 * 151 * DESCRIPTION: Print AML opcodes that can be used in constant expressions. 152 * 153 ******************************************************************************/ 154 155 void 156 UtDisplayConstantOpcodes ( 157 void) 158 { 159 UINT32 i; 160 161 printf ("Constant expression opcode information\n\n"); 162 163 for (i = 0; i < sizeof (AcpiGbl_AmlOpInfo) / sizeof (ACPI_OPCODE_INFO); i++) 164 { 165 if (AcpiGbl_AmlOpInfo[i].Flags & AML_CONSTANT) 166 { 167 printf ("%s\n", AcpiGbl_AmlOpInfo[i].Name); 168 } 169 } 170 } 171 172 173 /******************************************************************************* 174 * 175 * FUNCTION: UtLocalCalloc 176 * 177 * PARAMETERS: Size - Bytes to be allocated 178 * 179 * RETURN: Pointer to the allocated memory. Guaranteed to be valid. 180 * 181 * DESCRIPTION: Allocate zero-initialized memory. Aborts the compile on an 182 * allocation failure, on the assumption that nothing more can be 183 * accomplished. 184 * 185 ******************************************************************************/ 186 187 void * 188 UtLocalCalloc ( 189 UINT32 Size) 190 { 191 void *Allocated; 192 193 194 Allocated = ACPI_MEM_CALLOCATE (Size); 195 if (!Allocated) 196 { 197 AslCommonError (ASL_ERROR, ASL_MSG_MEMORY_ALLOCATION, 198 Gbl_CurrentLineNumber, Gbl_LogicalLineNumber, 199 Gbl_InputByteCount, Gbl_CurrentColumn, 200 Gbl_Files[ASL_FILE_INPUT].Filename, NULL); 201 exit (1); 202 } 203 204 TotalAllocations++; 205 TotalAllocated += Size; 206 return Allocated; 207 } 208 209 210 /******************************************************************************* 211 * 212 * FUNCTION: UtBeginEvent 213 * 214 * PARAMETERS: Event - Event number (integer index) 215 * Name - Ascii name of this event 216 * 217 * RETURN: None 218 * 219 * DESCRIPTION: Saves the current time with this event 220 * 221 ******************************************************************************/ 222 223 void 224 UtBeginEvent ( 225 UINT32 Event, 226 char *Name) 227 { 228 229 AslGbl_Events[Event].StartTime = (time_t) AcpiOsGetTimer(); 230 AslGbl_Events[Event].EventName = Name; 231 AslGbl_Events[Event].Valid = TRUE; 232 } 233 234 235 /******************************************************************************* 236 * 237 * FUNCTION: UtEndEvent 238 * 239 * PARAMETERS: Event - Event number (integer index) 240 * 241 * RETURN: None 242 * 243 * DESCRIPTION: Saves the current time (end time) with this event 244 * 245 ******************************************************************************/ 246 247 void 248 UtEndEvent ( 249 UINT32 Event) 250 { 251 252 AslGbl_Events[Event].EndTime = (time_t) AcpiOsGetTimer(); 253 } 254 255 256 /******************************************************************************* 257 * 258 * FUNCTION: UtHexCharToValue 259 * 260 * PARAMETERS: hc - Hex character in Ascii 261 * 262 * RETURN: The binary value of the hex character 263 * 264 * DESCRIPTION: Perform ascii-to-hex translation 265 * 266 ******************************************************************************/ 267 268 UINT8 269 UtHexCharToValue ( 270 int hc) 271 { 272 273 if (hc <= 0x39) 274 { 275 return ((UINT8) (hc - 0x30)); 276 } 277 278 if (hc <= 0x46) 279 { 280 return ((UINT8) (hc - 0x37)); 281 } 282 283 return ((UINT8) (hc - 0x57)); 284 } 285 286 287 /******************************************************************************* 288 * 289 * FUNCTION: UtConvertByteToHex 290 * 291 * PARAMETERS: RawByte - Binary data 292 * *Buffer - Pointer to where the hex bytes will be stored 293 * 294 * RETURN: Ascii hex byte is stored in Buffer. 295 * 296 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed 297 * with "0x" 298 * 299 ******************************************************************************/ 300 301 void 302 UtConvertByteToHex ( 303 UINT8 RawByte, 304 UINT8 *Buffer) 305 { 306 307 Buffer[0] = '0'; 308 Buffer[1] = 'x'; 309 310 Buffer[2] = (UINT8) hex[(RawByte >> 4) & 0xF]; 311 Buffer[3] = (UINT8) hex[RawByte & 0xF]; 312 } 313 314 315 /******************************************************************************* 316 * 317 * FUNCTION: UtConvertByteToAsmHex 318 * 319 * PARAMETERS: RawByte - Binary data 320 * *Buffer - Pointer to where the hex bytes will be stored 321 * 322 * RETURN: Ascii hex byte is stored in Buffer. 323 * 324 * DESCRIPTION: Perform hex-to-ascii translation. The return data is prefixed 325 * with "0x" 326 * 327 ******************************************************************************/ 328 329 void 330 UtConvertByteToAsmHex ( 331 UINT8 RawByte, 332 UINT8 *Buffer) 333 { 334 335 Buffer[0] = '0'; 336 337 Buffer[1] = (UINT8) hex[(RawByte >> 4) & 0xF]; 338 Buffer[2] = (UINT8) hex[RawByte & 0xF]; 339 Buffer[3] = 'h'; 340 } 341 342 343 /******************************************************************************* 344 * 345 * FUNCTION: DbgPrint 346 * 347 * PARAMETERS: Fmt - Printf format string 348 * ... - variable printf list 349 * 350 * RETURN: None 351 * 352 * DESCRIPTION: Conditional print statement. Prints to stderr only if the 353 * debug flag is set. 354 * 355 ******************************************************************************/ 356 357 void 358 DbgPrint ( 359 UINT32 Type, 360 char *Fmt, 361 ...) 362 { 363 va_list Args; 364 365 366 va_start (Args, Fmt); 367 368 if (!Gbl_DebugFlag) 369 { 370 return; 371 } 372 373 if ((Type == ASL_PARSE_OUTPUT) && 374 (!(AslCompilerdebug))) 375 { 376 return; 377 } 378 379 (void) vfprintf (stderr, Fmt, Args); 380 va_end (Args); 381 return; 382 } 383 384 385 /******************************************************************************* 386 * 387 * FUNCTION: UtPrintFormattedName 388 * 389 * PARAMETERS: ParseOpcode - Parser keyword ID 390 * Level - Indentation level 391 * 392 * RETURN: None 393 * 394 * DESCRIPTION: Print the ascii name of the parse opcode. 395 * 396 ******************************************************************************/ 397 398 #define TEXT_OFFSET 10 399 400 void 401 UtPrintFormattedName ( 402 UINT16 ParseOpcode, 403 UINT32 Level) 404 { 405 406 DbgPrint (ASL_TREE_OUTPUT, 407 "%*s %-16.16s", (3 * Level), " ", 408 UtGetOpName (ParseOpcode)); 409 410 if (Level < TEXT_OFFSET) 411 { 412 DbgPrint (ASL_TREE_OUTPUT, 413 "%*s", (TEXT_OFFSET - Level) * 3, " "); 414 } 415 } 416 417 418 /******************************************************************************* 419 * 420 * FUNCTION: UtSetParseOpName 421 * 422 * PARAMETERS: Op 423 * 424 * RETURN: None 425 * 426 * DESCRIPTION: Insert the ascii name of the parse opcode 427 * 428 ******************************************************************************/ 429 430 void 431 UtSetParseOpName ( 432 ACPI_PARSE_OBJECT *Op) 433 { 434 strncpy (Op->Asl.ParseOpName, UtGetOpName (Op->Asl.ParseOpcode), 12); 435 } 436 437 438 /******************************************************************************* 439 * 440 * FUNCTION: UtGetOpName 441 * 442 * PARAMETERS: ParseOpcode - Parser keyword ID 443 * 444 * RETURN: Pointer to the opcode name 445 * 446 * DESCRIPTION: Get the ascii name of the parse opcode 447 * 448 ******************************************************************************/ 449 450 char * 451 UtGetOpName ( 452 UINT32 ParseOpcode) 453 { 454 455 /* 456 * First entries (ASL_YYTNAME_START) in yytname are special reserved names. 457 * Ignore first 8 characters of the name 458 */ 459 return ((char *) yytname [(ParseOpcode - ASL_FIRST_PARSE_OPCODE) + ASL_YYTNAME_START] + 8); 460 } 461 462 463 /******************************************************************************* 464 * 465 * FUNCTION: UtDisplaySummary 466 * 467 * PARAMETERS: None 468 * 469 * RETURN: None 470 * 471 * DESCRIPTION: Display compilation statistics 472 * 473 ******************************************************************************/ 474 475 void 476 UtDisplaySummary ( 477 UINT32 FileId) 478 { 479 480 if (FileId != ASL_FILE_STDOUT) 481 { 482 /* Compiler name and version number */ 483 484 FlPrintFile (FileId, "%s version %X [%s]\n", 485 CompilerId, (UINT32) ACPI_CA_VERSION, __DATE__); 486 } 487 488 /* Input/Output summary */ 489 490 FlPrintFile (FileId, 491 "ASL Input: %s - %d lines, %d bytes, %d keywords\n", 492 Gbl_Files[ASL_FILE_INPUT].Filename, Gbl_CurrentLineNumber, 493 Gbl_InputByteCount, TotalKeywords); 494 495 /* AML summary */ 496 497 if ((Gbl_ExceptionCount[ASL_ERROR] == 0) || (Gbl_IgnoreErrors)) 498 { 499 FlPrintFile (FileId, 500 "AML Output: %s - %d bytes %d named objects %d executable opcodes\n\n", 501 Gbl_Files[ASL_FILE_AML_OUTPUT].Filename, Gbl_TableLength, 502 TotalNamedObjects, TotalExecutableOpcodes); 503 } 504 505 /* Error summary */ 506 507 FlPrintFile (FileId, 508 "Compilation complete. %d Errors, %d Warnings, %d Remarks, %d Optimizations\n", 509 Gbl_ExceptionCount[ASL_ERROR], 510 Gbl_ExceptionCount[ASL_WARNING], 511 Gbl_ExceptionCount[ASL_REMARK], 512 Gbl_ExceptionCount[ASL_OPTIMIZATION]); 513 } 514 515 516 /******************************************************************************* 517 * 518 * FUNCTION: UtDisplaySummary 519 * 520 * PARAMETERS: Op - Integer parse node 521 * LowValue - Smallest allowed value 522 * HighValue - Largest allowed value 523 * 524 * RETURN: Op if OK, otherwise NULL 525 * 526 * DESCRIPTION: Check integer for an allowable range 527 * 528 ******************************************************************************/ 529 530 ACPI_PARSE_OBJECT * 531 UtCheckIntegerRange ( 532 ACPI_PARSE_OBJECT *Op, 533 UINT32 LowValue, 534 UINT32 HighValue) 535 { 536 char *ParseError = NULL; 537 char Buffer[64]; 538 539 540 if (!Op) 541 { 542 return NULL; 543 } 544 545 if (Op->Asl.Value.Integer < LowValue) 546 { 547 ParseError = "Value below valid range"; 548 Op->Asl.Value.Integer = LowValue; 549 } 550 551 if (Op->Asl.Value.Integer > HighValue) 552 { 553 ParseError = "Value above valid range"; 554 Op->Asl.Value.Integer = HighValue; 555 } 556 557 if (ParseError) 558 { 559 sprintf (Buffer, "%s 0x%X-0x%X", ParseError, LowValue, HighValue); 560 AslCompilererror (Buffer); 561 562 return NULL; 563 } 564 565 return Op; 566 } 567 568 569 /******************************************************************************* 570 * 571 * FUNCTION: UtGetStringBuffer 572 * 573 * PARAMETERS: Length - Size of buffer requested 574 * 575 * RETURN: Pointer to the buffer. Aborts on allocation failure 576 * 577 * DESCRIPTION: Allocate a string buffer. Bypass the local 578 * dynamic memory manager for performance reasons (This has a 579 * major impact on the speed of the compiler.) 580 * 581 ******************************************************************************/ 582 583 char * 584 UtGetStringBuffer ( 585 UINT32 Length) 586 { 587 char *Buffer; 588 589 590 if ((Gbl_StringCacheNext + Length) >= Gbl_StringCacheLast) 591 { 592 Gbl_StringCacheNext = UtLocalCalloc (ASL_STRING_CACHE_SIZE + Length); 593 Gbl_StringCacheLast = Gbl_StringCacheNext + ASL_STRING_CACHE_SIZE + Length; 594 } 595 596 Buffer = Gbl_StringCacheNext; 597 Gbl_StringCacheNext += Length; 598 599 return (Buffer); 600 } 601 602 603 /******************************************************************************* 604 * 605 * FUNCTION: UtInternalizeName 606 * 607 * PARAMETERS: ExternalName - Name to convert 608 * ConvertedName - Where the converted name is returned 609 * 610 * RETURN: Status 611 * 612 * DESCRIPTION: Convert an external (ASL) name to an internal (AML) name 613 * 614 ******************************************************************************/ 615 616 ACPI_STATUS 617 UtInternalizeName ( 618 char *ExternalName, 619 char **ConvertedName) 620 { 621 ACPI_NAMESTRING_INFO Info; 622 ACPI_STATUS Status; 623 624 625 if (!ExternalName) 626 { 627 return (AE_OK); 628 } 629 630 /* Get the length of the new internal name */ 631 632 Info.ExternalName = ExternalName; 633 AcpiNsGetInternalNameLength (&Info); 634 635 /* We need a segment to store the internal name */ 636 637 Info.InternalName = UtGetStringBuffer (Info.Length); 638 if (!Info.InternalName) 639 { 640 return (AE_NO_MEMORY); 641 } 642 643 /* Build the name */ 644 645 Status = AcpiNsBuildInternalName (&Info); 646 if (ACPI_FAILURE (Status)) 647 { 648 return (Status); 649 } 650 651 *ConvertedName = Info.InternalName; 652 return (AE_OK); 653 } 654 655 656 /******************************************************************************* 657 * 658 * FUNCTION: UtPadNameWithUnderscores 659 * 660 * PARAMETERS: NameSeg - Input nameseg 661 * PaddedNameSeg - Output padded nameseg 662 * 663 * RETURN: Padded nameseg. 664 * 665 * DESCRIPTION: Pads a NameSeg with underscores if necessary to form a full 666 * ACPI_NAME. 667 * 668 ******************************************************************************/ 669 670 void 671 UtPadNameWithUnderscores ( 672 char *NameSeg, 673 char *PaddedNameSeg) 674 { 675 UINT32 i; 676 677 678 for (i = 0; (i < ACPI_NAME_SIZE); i++) 679 { 680 if (*NameSeg) 681 { 682 *PaddedNameSeg = *NameSeg; 683 NameSeg++; 684 } 685 else 686 { 687 *PaddedNameSeg = '_'; 688 } 689 PaddedNameSeg++; 690 } 691 } 692 693 694 /******************************************************************************* 695 * 696 * FUNCTION: UtAttachNameseg 697 * 698 * PARAMETERS: Op - Parent parse node 699 * Name - Full ExternalName 700 * 701 * RETURN: Sets the NameSeg field in parent node 702 * 703 * DESCRIPTION: Extract the last nameseg of the ExternalName and store it 704 * in the NameSeg field of the Op. 705 * 706 ******************************************************************************/ 707 708 void 709 UtAttachNameseg ( 710 ACPI_PARSE_OBJECT *Op, 711 char *Name) 712 { 713 char *NameSeg; 714 char PaddedNameSeg[4]; 715 716 717 if (!Name) 718 { 719 return; 720 } 721 722 /* Look for the last dot in the namepath */ 723 724 NameSeg = strrchr (Name, '.'); 725 if (NameSeg) 726 { 727 /* Found last dot, we have also found the final nameseg */ 728 729 NameSeg++; 730 UtPadNameWithUnderscores (NameSeg, PaddedNameSeg); 731 } 732 else 733 { 734 /* No dots in the namepath, there is only a single nameseg. */ 735 /* Handle prefixes */ 736 737 while ((*Name == '\\') || (*Name == '^')) 738 { 739 Name++; 740 } 741 742 /* Remaing string should be one single nameseg */ 743 744 UtPadNameWithUnderscores (Name, PaddedNameSeg); 745 } 746 747 strncpy (Op->Asl.NameSeg, PaddedNameSeg, 4); 748 } 749 750 751 /******************************************************************************* 752 * 753 * FUNCTION: UtAttachNamepathToOwner 754 * 755 * PARAMETERS: Op - Parent parse node 756 * NameOp - Node that contains the name 757 * 758 * RETURN: Sets the ExternalName and Namepath in the parent node 759 * 760 * DESCRIPTION: Store the name in two forms in the parent node: The original 761 * (external) name, and the internalized name that is used within 762 * the ACPI namespace manager. 763 * 764 ******************************************************************************/ 765 766 void 767 UtAttachNamepathToOwner ( 768 ACPI_PARSE_OBJECT *Op, 769 ACPI_PARSE_OBJECT *NameOp) 770 { 771 ACPI_STATUS Status; 772 773 774 /* Full external path */ 775 776 Op->Asl.ExternalName = NameOp->Asl.Value.String; 777 778 /* Last nameseg of the path */ 779 780 UtAttachNameseg (Op, Op->Asl.ExternalName); 781 782 /* Create internalized path */ 783 784 Status = UtInternalizeName (NameOp->Asl.Value.String, &Op->Asl.Namepath); 785 if (ACPI_FAILURE (Status)) 786 { 787 /* TBD: abort on no memory */ 788 } 789 } 790 791 792 /******************************************************************************* 793 * 794 * FUNCTION: UtDoConstant 795 * 796 * PARAMETERS: String - Hex, Octal, or Decimal string 797 * 798 * RETURN: Converted Integer 799 * 800 * DESCRIPTION: Convert a string to an integer. With error checking. 801 * 802 ******************************************************************************/ 803 804 ACPI_INTEGER 805 UtDoConstant ( 806 char *String) 807 { 808 ACPI_STATUS Status; 809 ACPI_INTEGER Converted; 810 char ErrBuf[64]; 811 812 813 Status = UtStrtoul64 (String, 0, &Converted); 814 if (ACPI_FAILURE (Status)) 815 { 816 sprintf (ErrBuf, "%s %s\n", "Conversion error:", AcpiFormatException (Status)); 817 AslCompilererror (ErrBuf); 818 } 819 820 return (Converted); 821 } 822 823 824 /******************************************************************************* 825 * 826 * FUNCTION: UtStrtoul64 827 * 828 * PARAMETERS: String - Null terminated string 829 * Terminater - Where a pointer to the terminating byte is returned 830 * Base - Radix of the string 831 * 832 * RETURN: Converted value 833 * 834 * DESCRIPTION: Convert a string into an unsigned value. 835 * 836 ******************************************************************************/ 837 #define NEGATIVE 1 838 #define POSITIVE 0 839 840 ACPI_STATUS 841 UtStrtoul64 ( 842 char *String, 843 UINT32 Base, 844 ACPI_INTEGER *RetInteger) 845 { 846 UINT32 Index; 847 UINT32 Sign; 848 ACPI_INTEGER ReturnValue = 0; 849 ACPI_STATUS Status = AE_OK; 850 851 852 *RetInteger = 0; 853 854 switch (Base) 855 { 856 case 0: 857 case 8: 858 case 10: 859 case 16: 860 break; 861 862 default: 863 /* 864 * The specified Base parameter is not in the domain of 865 * this function: 866 */ 867 return (AE_BAD_PARAMETER); 868 } 869 870 /* 871 * skip over any white space in the buffer: 872 */ 873 while (isspace (*String) || *String == '\t') 874 { 875 ++String; 876 } 877 878 /* 879 * The buffer may contain an optional plus or minus sign. 880 * If it does, then skip over it but remember what is was: 881 */ 882 if (*String == '-') 883 { 884 Sign = NEGATIVE; 885 ++String; 886 } 887 else if (*String == '+') 888 { 889 ++String; 890 Sign = POSITIVE; 891 } 892 else 893 { 894 Sign = POSITIVE; 895 } 896 897 /* 898 * If the input parameter Base is zero, then we need to 899 * determine if it is octal, decimal, or hexadecimal: 900 */ 901 if (Base == 0) 902 { 903 if (*String == '0') 904 { 905 if (tolower (*(++String)) == 'x') 906 { 907 Base = 16; 908 ++String; 909 } 910 else 911 { 912 Base = 8; 913 } 914 } 915 else 916 { 917 Base = 10; 918 } 919 } 920 921 /* 922 * For octal and hexadecimal bases, skip over the leading 923 * 0 or 0x, if they are present. 924 */ 925 if (Base == 8 && *String == '0') 926 { 927 String++; 928 } 929 930 if (Base == 16 && 931 *String == '0' && 932 tolower (*(++String)) == 'x') 933 { 934 String++; 935 } 936 937 /* Main loop: convert the string to an unsigned long */ 938 939 while (*String) 940 { 941 if (isdigit (*String)) 942 { 943 Index = ((UINT8) *String) - '0'; 944 } 945 else 946 { 947 Index = (UINT8) toupper (*String); 948 if (isupper ((char) Index)) 949 { 950 Index = Index - 'A' + 10; 951 } 952 else 953 { 954 goto ErrorExit; 955 } 956 } 957 958 if (Index >= Base) 959 { 960 goto ErrorExit; 961 } 962 963 /* Check to see if value is out of range: */ 964 965 if (ReturnValue > ((ACPI_INTEGER_MAX - (ACPI_INTEGER) Index) / 966 (ACPI_INTEGER) Base)) 967 { 968 goto ErrorExit; 969 } 970 else 971 { 972 ReturnValue *= Base; 973 ReturnValue += Index; 974 } 975 976 ++String; 977 } 978 979 980 /* 981 * If a minus sign was present, then "the conversion is negated": 982 */ 983 if (Sign == NEGATIVE) 984 { 985 ReturnValue = (ACPI_UINT32_MAX - ReturnValue) + 1; 986 } 987 988 *RetInteger = ReturnValue; 989 return (Status); 990 991 992 ErrorExit: 993 switch (Base) 994 { 995 case 8: 996 Status = AE_BAD_OCTAL_CONSTANT; 997 break; 998 999 case 10: 1000 Status = AE_BAD_DECIMAL_CONSTANT; 1001 break; 1002 1003 case 16: 1004 Status = AE_BAD_HEX_CONSTANT; 1005 break; 1006 1007 default: 1008 /* Base validated above */ 1009 break; 1010 } 1011 1012 return (Status); 1013 } 1014 1015 1016