Update ACPI build wrappers to use new ACPICA(20050309) code.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050211 / interpreter / executer / exmisc.c
1
2 /******************************************************************************
3  *
4  * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5  *              $Revision: 128 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2005, 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
89  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
90  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91  * PARTICULAR PURPOSE.
92  *
93  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
94  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
95  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
96  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
97  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
98  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
99  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100  * LIMITED REMEDY.
101  *
102  * 4.3. Licensee shall not export, either directly or indirectly, any of this
103  * software or system incorporating such software without first obtaining any
104  * required license or other approval from the U. S. Department of Commerce or
105  * any other agency or department of the United States Government.  In the
106  * event Licensee exports any such software from the United States or
107  * re-exports any such software from a foreign destination, Licensee shall
108  * ensure that the distribution and export/re-export of the software is in
109  * compliance with all laws, regulations, orders, or other restrictions of the
110  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
111  * any of its subsidiaries will export/re-export any technical data, process,
112  * software, or service, directly or indirectly, to any country for which the
113  * United States government or any agency thereof requires an export license,
114  * other governmental approval, or letter of assurance, without first obtaining
115  * such license, approval or letter.
116  *
117  *****************************************************************************/
118
119 #define __EXMISC_C__
120
121 #include "acpi.h"
122 #include "acinterp.h"
123 #include "amlcode.h"
124
125
126 #define _COMPONENT          ACPI_EXECUTER
127         ACPI_MODULE_NAME    ("exmisc")
128
129
130 /*******************************************************************************
131  *
132  * FUNCTION:    AcpiExGetObjectReference
133  *
134  * PARAMETERS:  ObjDesc             - Create a reference to this object
135  *              ReturnDesc          - Where to store the reference
136  *              WalkState           - Current state
137  *
138  * RETURN:      Status
139  *
140  * DESCRIPTION: Obtain and return a "reference" to the target object
141  *              Common code for the RefOfOp and the CondRefOfOp.
142  *
143  ******************************************************************************/
144
145 ACPI_STATUS
146 AcpiExGetObjectReference (
147     ACPI_OPERAND_OBJECT     *ObjDesc,
148     ACPI_OPERAND_OBJECT     **ReturnDesc,
149     ACPI_WALK_STATE         *WalkState)
150 {
151     ACPI_OPERAND_OBJECT     *ReferenceObj;
152     ACPI_OPERAND_OBJECT     *ReferencedObj;
153
154
155     ACPI_FUNCTION_TRACE_PTR ("ExGetObjectReference", ObjDesc);
156
157
158     *ReturnDesc = NULL;
159
160     switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
161     {
162     case ACPI_DESC_TYPE_OPERAND:
163
164         if (ACPI_GET_OBJECT_TYPE (ObjDesc) != ACPI_TYPE_LOCAL_REFERENCE)
165         {
166             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
167         }
168
169         /*
170          * Must be a reference to a Local or Arg
171          */
172         switch (ObjDesc->Reference.Opcode)
173         {
174         case AML_LOCAL_OP:
175         case AML_ARG_OP:
176
177             /* The referenced object is the pseudo-node for the local/arg */
178
179             ReferencedObj = ObjDesc->Reference.Object;
180             break;
181
182         default:
183
184             ACPI_REPORT_ERROR (("Unknown Reference subtype in get ref %X\n",
185                 ObjDesc->Reference.Opcode));
186             return_ACPI_STATUS (AE_AML_INTERNAL);
187         }
188         break;
189
190
191     case ACPI_DESC_TYPE_NAMED:
192
193         /*
194          * A named reference that has already been resolved to a Node
195          */
196         ReferencedObj = ObjDesc;
197         break;
198
199
200     default:
201
202         ACPI_REPORT_ERROR (("Invalid descriptor type in get ref: %X\n",
203                 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc)));
204         return_ACPI_STATUS (AE_TYPE);
205     }
206
207
208     /* Create a new reference object */
209
210     ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
211     if (!ReferenceObj)
212     {
213         return_ACPI_STATUS (AE_NO_MEMORY);
214     }
215
216     ReferenceObj->Reference.Opcode = AML_REF_OF_OP;
217     ReferenceObj->Reference.Object = ReferencedObj;
218     *ReturnDesc = ReferenceObj;
219
220     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
221             ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
222
223     return_ACPI_STATUS (AE_OK);
224 }
225
226
227 /*******************************************************************************
228  *
229  * FUNCTION:    AcpiExConcatTemplate
230  *
231  * PARAMETERS:  Operand0            - First source object
232  *              Operand1            - Second source object
233  *              ActualReturnDesc    - Where to place the return object
234  *              WalkState           - Current walk state
235  *
236  * RETURN:      Status
237  *
238  * DESCRIPTION: Concatenate two resource templates
239  *
240  ******************************************************************************/
241
242 ACPI_STATUS
243 AcpiExConcatTemplate (
244     ACPI_OPERAND_OBJECT     *Operand0,
245     ACPI_OPERAND_OBJECT     *Operand1,
246     ACPI_OPERAND_OBJECT     **ActualReturnDesc,
247     ACPI_WALK_STATE         *WalkState)
248 {
249     ACPI_OPERAND_OBJECT     *ReturnDesc;
250     UINT8                   *NewBuf;
251     UINT8                   *EndTag1;
252     UINT8                   *EndTag2;
253     ACPI_SIZE               Length1;
254     ACPI_SIZE               Length2;
255
256
257     ACPI_FUNCTION_TRACE ("ExConcatTemplate");
258
259
260     /* Find the EndTags in each resource template */
261
262     EndTag1 = AcpiUtGetResourceEndTag (Operand0);
263     EndTag2 = AcpiUtGetResourceEndTag (Operand1);
264     if (!EndTag1 || !EndTag2)
265     {
266         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
267     }
268
269     /* Compute the length of each part */
270
271     Length1 = ACPI_PTR_DIFF (EndTag1, Operand0->Buffer.Pointer);
272     Length2 = ACPI_PTR_DIFF (EndTag2, Operand1->Buffer.Pointer) +
273                              2; /* Size of END_TAG */
274
275     /* Create a new buffer object for the result */
276
277     ReturnDesc = AcpiUtCreateBufferObject (Length1 + Length2);
278     if (!ReturnDesc)
279     {
280         return_ACPI_STATUS (AE_NO_MEMORY);
281     }
282
283     /* Copy the templates to the new descriptor */
284
285     NewBuf = ReturnDesc->Buffer.Pointer;
286     ACPI_MEMCPY (NewBuf, Operand0->Buffer.Pointer, Length1);
287     ACPI_MEMCPY (NewBuf + Length1, Operand1->Buffer.Pointer, Length2);
288
289     /* Compute the new checksum */
290
291     NewBuf[ReturnDesc->Buffer.Length - 1] =
292             AcpiUtGenerateChecksum (ReturnDesc->Buffer.Pointer,
293                                    (ReturnDesc->Buffer.Length - 1));
294
295     /* Return the completed template descriptor */
296
297     *ActualReturnDesc = ReturnDesc;
298     return_ACPI_STATUS (AE_OK);
299 }
300
301
302 /*******************************************************************************
303  *
304  * FUNCTION:    AcpiExDoConcatenate
305  *
306  * PARAMETERS:  Operand0            - First source object
307  *              Operand1            - Second source object
308  *              ActualReturnDesc    - Where to place the return object
309  *              WalkState           - Current walk state
310  *
311  * RETURN:      Status
312  *
313  * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
314  *
315  ******************************************************************************/
316
317 ACPI_STATUS
318 AcpiExDoConcatenate (
319     ACPI_OPERAND_OBJECT     *Operand0,
320     ACPI_OPERAND_OBJECT     *Operand1,
321     ACPI_OPERAND_OBJECT     **ActualReturnDesc,
322     ACPI_WALK_STATE         *WalkState)
323 {
324     ACPI_OPERAND_OBJECT     *LocalOperand1 = Operand1;
325     ACPI_OPERAND_OBJECT     *ReturnDesc;
326     char                    *NewBuf;
327     ACPI_STATUS             Status;
328     ACPI_SIZE               NewLength;
329
330
331     ACPI_FUNCTION_TRACE ("ExDoConcatenate");
332
333
334     /*
335      * Convert the second operand if necessary.  The first operand
336      * determines the type of the second operand, (See the Data Types
337      * section of the ACPI specification.)  Both object types are
338      * guaranteed to be either Integer/String/Buffer by the operand
339      * resolution mechanism.
340      */
341     switch (ACPI_GET_OBJECT_TYPE (Operand0))
342     {
343     case ACPI_TYPE_INTEGER:
344         Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
345         break;
346
347     case ACPI_TYPE_STRING:
348         Status = AcpiExConvertToString (Operand1, &LocalOperand1,
349                     ACPI_IMPLICIT_CONVERT_HEX);
350         break;
351
352     case ACPI_TYPE_BUFFER:
353         Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
354         break;
355
356     default:
357         ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
358                 ACPI_GET_OBJECT_TYPE (Operand0)));
359         Status = AE_AML_INTERNAL;
360     }
361
362     if (ACPI_FAILURE (Status))
363     {
364         goto Cleanup;
365     }
366
367     /*
368      * Both operands are now known to be the same object type
369      * (Both are Integer, String, or Buffer), and we can now perform the
370      * concatenation.
371      */
372
373     /*
374      * There are three cases to handle:
375      *
376      * 1) Two Integers concatenated to produce a new Buffer
377      * 2) Two Strings concatenated to produce a new String
378      * 3) Two Buffers concatenated to produce a new Buffer
379      */
380     switch (ACPI_GET_OBJECT_TYPE (Operand0))
381     {
382     case ACPI_TYPE_INTEGER:
383
384         /* Result of two Integers is a Buffer */
385         /* Need enough buffer space for two integers */
386
387         ReturnDesc = AcpiUtCreateBufferObject (
388                             ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
389         if (!ReturnDesc)
390         {
391             Status = AE_NO_MEMORY;
392             goto Cleanup;
393         }
394
395         NewBuf = (char *) ReturnDesc->Buffer.Pointer;
396
397         /* Copy the first integer, LSB first */
398
399         ACPI_MEMCPY (NewBuf,
400                         &Operand0->Integer.Value,
401                         AcpiGbl_IntegerByteWidth);
402
403         /* Copy the second integer (LSB first) after the first */
404
405         ACPI_MEMCPY (NewBuf + AcpiGbl_IntegerByteWidth,
406                         &LocalOperand1->Integer.Value,
407                         AcpiGbl_IntegerByteWidth);
408         break;
409
410     case ACPI_TYPE_STRING:
411
412         /* Result of two Strings is a String */
413
414         NewLength = (ACPI_SIZE) Operand0->String.Length +
415                     (ACPI_SIZE) LocalOperand1->String.Length;
416         if (NewLength > ACPI_MAX_STRING_CONVERSION)
417         {
418             Status = AE_AML_STRING_LIMIT;
419             goto Cleanup;
420         }
421
422         ReturnDesc = AcpiUtCreateStringObject (NewLength);
423         if (!ReturnDesc)
424         {
425             Status = AE_NO_MEMORY;
426             goto Cleanup;
427         }
428
429         NewBuf = ReturnDesc->String.Pointer;
430
431         /* Concatenate the strings */
432
433         ACPI_STRCPY (NewBuf,
434                         Operand0->String.Pointer);
435         ACPI_STRCPY (NewBuf + Operand0->String.Length,
436                         LocalOperand1->String.Pointer);
437         break;
438
439     case ACPI_TYPE_BUFFER:
440
441         /* Result of two Buffers is a Buffer */
442
443         ReturnDesc = AcpiUtCreateBufferObject (
444                             (ACPI_SIZE) Operand0->Buffer.Length +
445                             (ACPI_SIZE) LocalOperand1->Buffer.Length);
446         if (!ReturnDesc)
447         {
448             Status = AE_NO_MEMORY;
449             goto Cleanup;
450         }
451
452         NewBuf = (char *) ReturnDesc->Buffer.Pointer;
453
454         /* Concatenate the buffers */
455
456         ACPI_MEMCPY (NewBuf,
457                         Operand0->Buffer.Pointer,
458                         Operand0->Buffer.Length);
459         ACPI_MEMCPY (NewBuf + Operand0->Buffer.Length,
460                         LocalOperand1->Buffer.Pointer,
461                         LocalOperand1->Buffer.Length);
462         break;
463
464     default:
465
466         /* Invalid object type, should not happen here */
467
468         ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
469                 ACPI_GET_OBJECT_TYPE (Operand0)));
470         Status =AE_AML_INTERNAL;
471         goto Cleanup;
472     }
473
474     *ActualReturnDesc = ReturnDesc;
475
476 Cleanup:
477     if (LocalOperand1 != Operand1)
478     {
479         AcpiUtRemoveReference (LocalOperand1);
480     }
481     return_ACPI_STATUS (Status);
482 }
483
484
485 /*******************************************************************************
486  *
487  * FUNCTION:    AcpiExDoMathOp
488  *
489  * PARAMETERS:  Opcode              - AML opcode
490  *              Integer0            - Integer operand #0
491  *              Integer1            - Integer operand #1
492  *
493  * RETURN:      Integer result of the operation
494  *
495  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
496  *              math functions here is to prevent a lot of pointer dereferencing
497  *              to obtain the operands.
498  *
499  ******************************************************************************/
500
501 ACPI_INTEGER
502 AcpiExDoMathOp (
503     UINT16                  Opcode,
504     ACPI_INTEGER            Integer0,
505     ACPI_INTEGER            Integer1)
506 {
507
508     ACPI_FUNCTION_ENTRY ();
509
510
511     switch (Opcode)
512     {
513     case AML_ADD_OP:                /* Add (Integer0, Integer1, Result) */
514
515         return (Integer0 + Integer1);
516
517
518     case AML_BIT_AND_OP:            /* And (Integer0, Integer1, Result) */
519
520         return (Integer0 & Integer1);
521
522
523     case AML_BIT_NAND_OP:           /* NAnd (Integer0, Integer1, Result) */
524
525         return (~(Integer0 & Integer1));
526
527
528     case AML_BIT_OR_OP:             /* Or (Integer0, Integer1, Result) */
529
530         return (Integer0 | Integer1);
531
532
533     case AML_BIT_NOR_OP:            /* NOr (Integer0, Integer1, Result) */
534
535         return (~(Integer0 | Integer1));
536
537
538     case AML_BIT_XOR_OP:            /* XOr (Integer0, Integer1, Result) */
539
540         return (Integer0 ^ Integer1);
541
542
543     case AML_MULTIPLY_OP:           /* Multiply (Integer0, Integer1, Result) */
544
545         return (Integer0 * Integer1);
546
547
548     case AML_SHIFT_LEFT_OP:         /* ShiftLeft (Operand, ShiftCount, Result) */
549
550         return (Integer0 << Integer1);
551
552
553     case AML_SHIFT_RIGHT_OP:        /* ShiftRight (Operand, ShiftCount, Result) */
554
555         return (Integer0 >> Integer1);
556
557
558     case AML_SUBTRACT_OP:           /* Subtract (Integer0, Integer1, Result) */
559
560         return (Integer0 - Integer1);
561
562     default:
563
564         return (0);
565     }
566 }
567
568
569 /*******************************************************************************
570  *
571  * FUNCTION:    AcpiExDoLogicalNumericOp
572  *
573  * PARAMETERS:  Opcode              - AML opcode
574  *              Integer0            - Integer operand #0
575  *              Integer1            - Integer operand #1
576  *              LogicalResult       - TRUE/FALSE result of the operation
577  *
578  * RETURN:      Status
579  *
580  * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
581  *              operators (LAnd and LOr), both operands must be integers.
582  *
583  *              Note: cleanest machine code seems to be produced by the code
584  *              below, rather than using statements of the form:
585  *                  Result = (Integer0 && Integer1);
586  *
587  ******************************************************************************/
588
589 ACPI_STATUS
590 AcpiExDoLogicalNumericOp (
591     UINT16                  Opcode,
592     ACPI_INTEGER            Integer0,
593     ACPI_INTEGER            Integer1,
594     BOOLEAN                 *LogicalResult)
595 {
596     ACPI_STATUS             Status = AE_OK;
597     BOOLEAN                 LocalResult = FALSE;
598
599
600     ACPI_FUNCTION_TRACE ("ExDoLogicalNumericOp");
601
602
603     switch (Opcode)
604     {
605     case AML_LAND_OP:               /* LAnd (Integer0, Integer1) */
606
607         if (Integer0 && Integer1)
608         {
609             LocalResult = TRUE;
610         }
611         break;
612
613     case AML_LOR_OP:                /* LOr (Integer0, Integer1) */
614
615         if (Integer0 || Integer1)
616         {
617             LocalResult = TRUE;
618         }
619         break;
620
621     default:
622         Status = AE_AML_INTERNAL;
623         break;
624     }
625
626     /* Return the logical result and status */
627
628     *LogicalResult = LocalResult;
629     return_ACPI_STATUS (Status);
630 }
631
632
633 /*******************************************************************************
634  *
635  * FUNCTION:    AcpiExDoLogicalOp
636  *
637  * PARAMETERS:  Opcode              - AML opcode
638  *              Operand0            - operand #0
639  *              Operand1            - operand #1
640  *              LogicalResult       - TRUE/FALSE result of the operation
641  *
642  * RETURN:      Status
643  *
644  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
645  *              functions here is to prevent a lot of pointer dereferencing
646  *              to obtain the operands and to simplify the generation of the
647  *              logical value. For the Numeric operators (LAnd and LOr), both
648  *              operands must be integers. For the other logical operators,
649  *              operands can be any combination of Integer/String/Buffer. The
650  *              first operand determines the type to which the second operand
651  *              will be converted.
652  *
653  *              Note: cleanest machine code seems to be produced by the code
654  *              below, rather than using statements of the form:
655  *                  Result = (Operand0 == Operand1);
656  *
657  ******************************************************************************/
658
659 ACPI_STATUS
660 AcpiExDoLogicalOp (
661     UINT16                  Opcode,
662     ACPI_OPERAND_OBJECT     *Operand0,
663     ACPI_OPERAND_OBJECT     *Operand1,
664     BOOLEAN                 *LogicalResult)
665 {
666     ACPI_OPERAND_OBJECT     *LocalOperand1 = Operand1;
667     ACPI_INTEGER            Integer0;
668     ACPI_INTEGER            Integer1;
669     UINT32                  Length0;
670     UINT32                  Length1;
671     ACPI_STATUS             Status = AE_OK;
672     BOOLEAN                 LocalResult = FALSE;
673     int                     Compare;
674
675
676     ACPI_FUNCTION_TRACE ("ExDoLogicalOp");
677
678
679     /*
680      * Convert the second operand if necessary.  The first operand
681      * determines the type of the second operand, (See the Data Types
682      * section of the ACPI 3.0+ specification.)  Both object types are
683      * guaranteed to be either Integer/String/Buffer by the operand
684      * resolution mechanism.
685      */
686     switch (ACPI_GET_OBJECT_TYPE (Operand0))
687     {
688     case ACPI_TYPE_INTEGER:
689         Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
690         break;
691
692     case ACPI_TYPE_STRING:
693         Status = AcpiExConvertToString (Operand1, &LocalOperand1,
694                     ACPI_IMPLICIT_CONVERT_HEX);
695         break;
696
697     case ACPI_TYPE_BUFFER:
698         Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
699         break;
700
701     default:
702         Status = AE_AML_INTERNAL;
703         break;
704     }
705
706     if (ACPI_FAILURE (Status))
707     {
708         goto Cleanup;
709     }
710
711     /*
712      * Two cases: 1) Both Integers, 2) Both Strings or Buffers
713      */
714     if (ACPI_GET_OBJECT_TYPE (Operand0) == ACPI_TYPE_INTEGER)
715     {
716         /*
717          * 1) Both operands are of type integer
718          *    Note: LocalOperand1 may have changed above
719          */
720         Integer0 = Operand0->Integer.Value;
721         Integer1 = LocalOperand1->Integer.Value;
722
723         switch (Opcode)
724         {
725         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
726
727             if (Integer0 == Integer1)
728             {
729                 LocalResult = TRUE;
730             }
731             break;
732
733         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
734
735             if (Integer0 > Integer1)
736             {
737                 LocalResult = TRUE;
738             }
739             break;
740
741         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
742
743             if (Integer0 < Integer1)
744             {
745                 LocalResult = TRUE;
746             }
747             break;
748
749         default:
750             Status = AE_AML_INTERNAL;
751             break;
752         }
753     }
754     else
755     {
756         /*
757          * 2) Both operands are Strings or both are Buffers
758          *    Note: Code below takes advantage of common Buffer/String
759          *          object fields. LocalOperand1 may have changed above. Use
760          *          memcmp to handle nulls in buffers.
761          */
762         Length0 = Operand0->Buffer.Length;
763         Length1 = LocalOperand1->Buffer.Length;
764
765         /* Lexicographic compare: compare the data bytes */
766
767         Compare = ACPI_MEMCMP ((const char * ) Operand0->Buffer.Pointer,
768                     (const char * ) LocalOperand1->Buffer.Pointer,
769                     (Length0 > Length1) ? Length1 : Length0);
770
771         switch (Opcode)
772         {
773         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
774
775             /* Length and all bytes must be equal */
776
777             if ((Length0 == Length1) &&
778                 (Compare == 0))
779             {
780                 /* Length and all bytes match ==> TRUE */
781
782                 LocalResult = TRUE;
783             }
784             break;
785
786         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
787
788             if (Compare > 0)
789             {
790                 LocalResult = TRUE;
791                 goto Cleanup;   /* TRUE */
792             }
793             if (Compare < 0)
794             {
795                 goto Cleanup;   /* FALSE */
796             }
797
798             /* Bytes match (to shortest length), compare lengths */
799
800             if (Length0 > Length1)
801             {
802                 LocalResult = TRUE;
803             }
804             break;
805
806         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
807
808             if (Compare > 0)
809             {
810                 goto Cleanup;   /* FALSE */
811             }
812             if (Compare < 0)
813             {
814                 LocalResult = TRUE;
815                 goto Cleanup;   /* TRUE */
816             }
817
818             /* Bytes match (to shortest length), compare lengths */
819
820             if (Length0 < Length1)
821             {
822                 LocalResult = TRUE;
823             }
824             break;
825
826         default:
827             Status = AE_AML_INTERNAL;
828             break;
829         }
830     }
831
832 Cleanup:
833
834     /* New object was created if implicit conversion performed - delete */
835
836     if (LocalOperand1 != Operand1)
837     {
838         AcpiUtRemoveReference (LocalOperand1);
839     }
840
841     /* Return the logical result and status */
842
843     *LogicalResult = LocalResult;
844     return_ACPI_STATUS (Status);
845 }
846
847