Merge from vendor branch GCC:
[dragonfly.git] / sys / contrib / dev / acpica-unix-20050309 / interpreter / executer / exmisc.c
1
2 /******************************************************************************
3  *
4  * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5  *              $Revision: 129 $
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         case AML_DEBUG_OP:
177
178             /* The referenced object is the pseudo-node for the local/arg */
179
180             ReferencedObj = ObjDesc->Reference.Object;
181             break;
182
183         default:
184
185             ACPI_REPORT_ERROR (("Unknown Reference opcode in GetReference %X\n",
186                 ObjDesc->Reference.Opcode));
187             return_ACPI_STATUS (AE_AML_INTERNAL);
188         }
189         break;
190
191
192     case ACPI_DESC_TYPE_NAMED:
193
194         /*
195          * A named reference that has already been resolved to a Node
196          */
197         ReferencedObj = ObjDesc;
198         break;
199
200
201     default:
202
203         ACPI_REPORT_ERROR (("Invalid descriptor type in GetReference: %X\n",
204                 ACPI_GET_DESCRIPTOR_TYPE (ObjDesc)));
205         return_ACPI_STATUS (AE_TYPE);
206     }
207
208
209     /* Create a new reference object */
210
211     ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
212     if (!ReferenceObj)
213     {
214         return_ACPI_STATUS (AE_NO_MEMORY);
215     }
216
217     ReferenceObj->Reference.Opcode = AML_REF_OF_OP;
218     ReferenceObj->Reference.Object = ReferencedObj;
219     *ReturnDesc = ReferenceObj;
220
221     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
222             ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
223
224     return_ACPI_STATUS (AE_OK);
225 }
226
227
228 /*******************************************************************************
229  *
230  * FUNCTION:    AcpiExConcatTemplate
231  *
232  * PARAMETERS:  Operand0            - First source object
233  *              Operand1            - Second source object
234  *              ActualReturnDesc    - Where to place the return object
235  *              WalkState           - Current walk state
236  *
237  * RETURN:      Status
238  *
239  * DESCRIPTION: Concatenate two resource templates
240  *
241  ******************************************************************************/
242
243 ACPI_STATUS
244 AcpiExConcatTemplate (
245     ACPI_OPERAND_OBJECT     *Operand0,
246     ACPI_OPERAND_OBJECT     *Operand1,
247     ACPI_OPERAND_OBJECT     **ActualReturnDesc,
248     ACPI_WALK_STATE         *WalkState)
249 {
250     ACPI_OPERAND_OBJECT     *ReturnDesc;
251     UINT8                   *NewBuf;
252     UINT8                   *EndTag1;
253     UINT8                   *EndTag2;
254     ACPI_SIZE               Length1;
255     ACPI_SIZE               Length2;
256
257
258     ACPI_FUNCTION_TRACE ("ExConcatTemplate");
259
260
261     /* Find the EndTags in each resource template */
262
263     EndTag1 = AcpiUtGetResourceEndTag (Operand0);
264     EndTag2 = AcpiUtGetResourceEndTag (Operand1);
265     if (!EndTag1 || !EndTag2)
266     {
267         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
268     }
269
270     /* Compute the length of each part */
271
272     Length1 = ACPI_PTR_DIFF (EndTag1, Operand0->Buffer.Pointer);
273     Length2 = ACPI_PTR_DIFF (EndTag2, Operand1->Buffer.Pointer) +
274                              2; /* Size of END_TAG */
275
276     /* Create a new buffer object for the result */
277
278     ReturnDesc = AcpiUtCreateBufferObject (Length1 + Length2);
279     if (!ReturnDesc)
280     {
281         return_ACPI_STATUS (AE_NO_MEMORY);
282     }
283
284     /* Copy the templates to the new descriptor */
285
286     NewBuf = ReturnDesc->Buffer.Pointer;
287     ACPI_MEMCPY (NewBuf, Operand0->Buffer.Pointer, Length1);
288     ACPI_MEMCPY (NewBuf + Length1, Operand1->Buffer.Pointer, Length2);
289
290     /* Compute the new checksum */
291
292     NewBuf[ReturnDesc->Buffer.Length - 1] =
293             AcpiUtGenerateChecksum (ReturnDesc->Buffer.Pointer,
294                                    (ReturnDesc->Buffer.Length - 1));
295
296     /* Return the completed template descriptor */
297
298     *ActualReturnDesc = ReturnDesc;
299     return_ACPI_STATUS (AE_OK);
300 }
301
302
303 /*******************************************************************************
304  *
305  * FUNCTION:    AcpiExDoConcatenate
306  *
307  * PARAMETERS:  Operand0            - First source object
308  *              Operand1            - Second source object
309  *              ActualReturnDesc    - Where to place the return object
310  *              WalkState           - Current walk state
311  *
312  * RETURN:      Status
313  *
314  * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
315  *
316  ******************************************************************************/
317
318 ACPI_STATUS
319 AcpiExDoConcatenate (
320     ACPI_OPERAND_OBJECT     *Operand0,
321     ACPI_OPERAND_OBJECT     *Operand1,
322     ACPI_OPERAND_OBJECT     **ActualReturnDesc,
323     ACPI_WALK_STATE         *WalkState)
324 {
325     ACPI_OPERAND_OBJECT     *LocalOperand1 = Operand1;
326     ACPI_OPERAND_OBJECT     *ReturnDesc;
327     char                    *NewBuf;
328     ACPI_STATUS             Status;
329     ACPI_SIZE               NewLength;
330
331
332     ACPI_FUNCTION_TRACE ("ExDoConcatenate");
333
334
335     /*
336      * Convert the second operand if necessary.  The first operand
337      * determines the type of the second operand, (See the Data Types
338      * section of the ACPI specification.)  Both object types are
339      * guaranteed to be either Integer/String/Buffer by the operand
340      * resolution mechanism.
341      */
342     switch (ACPI_GET_OBJECT_TYPE (Operand0))
343     {
344     case ACPI_TYPE_INTEGER:
345         Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
346         break;
347
348     case ACPI_TYPE_STRING:
349         Status = AcpiExConvertToString (Operand1, &LocalOperand1,
350                     ACPI_IMPLICIT_CONVERT_HEX);
351         break;
352
353     case ACPI_TYPE_BUFFER:
354         Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
355         break;
356
357     default:
358         ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
359                 ACPI_GET_OBJECT_TYPE (Operand0)));
360         Status = AE_AML_INTERNAL;
361     }
362
363     if (ACPI_FAILURE (Status))
364     {
365         goto Cleanup;
366     }
367
368     /*
369      * Both operands are now known to be the same object type
370      * (Both are Integer, String, or Buffer), and we can now perform the
371      * concatenation.
372      */
373
374     /*
375      * There are three cases to handle:
376      *
377      * 1) Two Integers concatenated to produce a new Buffer
378      * 2) Two Strings concatenated to produce a new String
379      * 3) Two Buffers concatenated to produce a new Buffer
380      */
381     switch (ACPI_GET_OBJECT_TYPE (Operand0))
382     {
383     case ACPI_TYPE_INTEGER:
384
385         /* Result of two Integers is a Buffer */
386         /* Need enough buffer space for two integers */
387
388         ReturnDesc = AcpiUtCreateBufferObject (
389                             ACPI_MUL_2 (AcpiGbl_IntegerByteWidth));
390         if (!ReturnDesc)
391         {
392             Status = AE_NO_MEMORY;
393             goto Cleanup;
394         }
395
396         NewBuf = (char *) ReturnDesc->Buffer.Pointer;
397
398         /* Copy the first integer, LSB first */
399
400         ACPI_MEMCPY (NewBuf,
401                         &Operand0->Integer.Value,
402                         AcpiGbl_IntegerByteWidth);
403
404         /* Copy the second integer (LSB first) after the first */
405
406         ACPI_MEMCPY (NewBuf + AcpiGbl_IntegerByteWidth,
407                         &LocalOperand1->Integer.Value,
408                         AcpiGbl_IntegerByteWidth);
409         break;
410
411     case ACPI_TYPE_STRING:
412
413         /* Result of two Strings is a String */
414
415         NewLength = (ACPI_SIZE) Operand0->String.Length +
416                     (ACPI_SIZE) LocalOperand1->String.Length;
417         if (NewLength > ACPI_MAX_STRING_CONVERSION)
418         {
419             Status = AE_AML_STRING_LIMIT;
420             goto Cleanup;
421         }
422
423         ReturnDesc = AcpiUtCreateStringObject (NewLength);
424         if (!ReturnDesc)
425         {
426             Status = AE_NO_MEMORY;
427             goto Cleanup;
428         }
429
430         NewBuf = ReturnDesc->String.Pointer;
431
432         /* Concatenate the strings */
433
434         ACPI_STRCPY (NewBuf,
435                         Operand0->String.Pointer);
436         ACPI_STRCPY (NewBuf + Operand0->String.Length,
437                         LocalOperand1->String.Pointer);
438         break;
439
440     case ACPI_TYPE_BUFFER:
441
442         /* Result of two Buffers is a Buffer */
443
444         ReturnDesc = AcpiUtCreateBufferObject (
445                             (ACPI_SIZE) Operand0->Buffer.Length +
446                             (ACPI_SIZE) LocalOperand1->Buffer.Length);
447         if (!ReturnDesc)
448         {
449             Status = AE_NO_MEMORY;
450             goto Cleanup;
451         }
452
453         NewBuf = (char *) ReturnDesc->Buffer.Pointer;
454
455         /* Concatenate the buffers */
456
457         ACPI_MEMCPY (NewBuf,
458                         Operand0->Buffer.Pointer,
459                         Operand0->Buffer.Length);
460         ACPI_MEMCPY (NewBuf + Operand0->Buffer.Length,
461                         LocalOperand1->Buffer.Pointer,
462                         LocalOperand1->Buffer.Length);
463         break;
464
465     default:
466
467         /* Invalid object type, should not happen here */
468
469         ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
470                 ACPI_GET_OBJECT_TYPE (Operand0)));
471         Status =AE_AML_INTERNAL;
472         goto Cleanup;
473     }
474
475     *ActualReturnDesc = ReturnDesc;
476
477 Cleanup:
478     if (LocalOperand1 != Operand1)
479     {
480         AcpiUtRemoveReference (LocalOperand1);
481     }
482     return_ACPI_STATUS (Status);
483 }
484
485
486 /*******************************************************************************
487  *
488  * FUNCTION:    AcpiExDoMathOp
489  *
490  * PARAMETERS:  Opcode              - AML opcode
491  *              Integer0            - Integer operand #0
492  *              Integer1            - Integer operand #1
493  *
494  * RETURN:      Integer result of the operation
495  *
496  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
497  *              math functions here is to prevent a lot of pointer dereferencing
498  *              to obtain the operands.
499  *
500  ******************************************************************************/
501
502 ACPI_INTEGER
503 AcpiExDoMathOp (
504     UINT16                  Opcode,
505     ACPI_INTEGER            Integer0,
506     ACPI_INTEGER            Integer1)
507 {
508
509     ACPI_FUNCTION_ENTRY ();
510
511
512     switch (Opcode)
513     {
514     case AML_ADD_OP:                /* Add (Integer0, Integer1, Result) */
515
516         return (Integer0 + Integer1);
517
518
519     case AML_BIT_AND_OP:            /* And (Integer0, Integer1, Result) */
520
521         return (Integer0 & Integer1);
522
523
524     case AML_BIT_NAND_OP:           /* NAnd (Integer0, Integer1, Result) */
525
526         return (~(Integer0 & Integer1));
527
528
529     case AML_BIT_OR_OP:             /* Or (Integer0, Integer1, Result) */
530
531         return (Integer0 | Integer1);
532
533
534     case AML_BIT_NOR_OP:            /* NOr (Integer0, Integer1, Result) */
535
536         return (~(Integer0 | Integer1));
537
538
539     case AML_BIT_XOR_OP:            /* XOr (Integer0, Integer1, Result) */
540
541         return (Integer0 ^ Integer1);
542
543
544     case AML_MULTIPLY_OP:           /* Multiply (Integer0, Integer1, Result) */
545
546         return (Integer0 * Integer1);
547
548
549     case AML_SHIFT_LEFT_OP:         /* ShiftLeft (Operand, ShiftCount, Result) */
550
551         return (Integer0 << Integer1);
552
553
554     case AML_SHIFT_RIGHT_OP:        /* ShiftRight (Operand, ShiftCount, Result) */
555
556         return (Integer0 >> Integer1);
557
558
559     case AML_SUBTRACT_OP:           /* Subtract (Integer0, Integer1, Result) */
560
561         return (Integer0 - Integer1);
562
563     default:
564
565         return (0);
566     }
567 }
568
569
570 /*******************************************************************************
571  *
572  * FUNCTION:    AcpiExDoLogicalNumericOp
573  *
574  * PARAMETERS:  Opcode              - AML opcode
575  *              Integer0            - Integer operand #0
576  *              Integer1            - Integer operand #1
577  *              LogicalResult       - TRUE/FALSE result of the operation
578  *
579  * RETURN:      Status
580  *
581  * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
582  *              operators (LAnd and LOr), both operands must be integers.
583  *
584  *              Note: cleanest machine code seems to be produced by the code
585  *              below, rather than using statements of the form:
586  *                  Result = (Integer0 && Integer1);
587  *
588  ******************************************************************************/
589
590 ACPI_STATUS
591 AcpiExDoLogicalNumericOp (
592     UINT16                  Opcode,
593     ACPI_INTEGER            Integer0,
594     ACPI_INTEGER            Integer1,
595     BOOLEAN                 *LogicalResult)
596 {
597     ACPI_STATUS             Status = AE_OK;
598     BOOLEAN                 LocalResult = FALSE;
599
600
601     ACPI_FUNCTION_TRACE ("ExDoLogicalNumericOp");
602
603
604     switch (Opcode)
605     {
606     case AML_LAND_OP:               /* LAnd (Integer0, Integer1) */
607
608         if (Integer0 && Integer1)
609         {
610             LocalResult = TRUE;
611         }
612         break;
613
614     case AML_LOR_OP:                /* LOr (Integer0, Integer1) */
615
616         if (Integer0 || Integer1)
617         {
618             LocalResult = TRUE;
619         }
620         break;
621
622     default:
623         Status = AE_AML_INTERNAL;
624         break;
625     }
626
627     /* Return the logical result and status */
628
629     *LogicalResult = LocalResult;
630     return_ACPI_STATUS (Status);
631 }
632
633
634 /*******************************************************************************
635  *
636  * FUNCTION:    AcpiExDoLogicalOp
637  *
638  * PARAMETERS:  Opcode              - AML opcode
639  *              Operand0            - operand #0
640  *              Operand1            - operand #1
641  *              LogicalResult       - TRUE/FALSE result of the operation
642  *
643  * RETURN:      Status
644  *
645  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
646  *              functions here is to prevent a lot of pointer dereferencing
647  *              to obtain the operands and to simplify the generation of the
648  *              logical value. For the Numeric operators (LAnd and LOr), both
649  *              operands must be integers. For the other logical operators,
650  *              operands can be any combination of Integer/String/Buffer. The
651  *              first operand determines the type to which the second operand
652  *              will be converted.
653  *
654  *              Note: cleanest machine code seems to be produced by the code
655  *              below, rather than using statements of the form:
656  *                  Result = (Operand0 == Operand1);
657  *
658  ******************************************************************************/
659
660 ACPI_STATUS
661 AcpiExDoLogicalOp (
662     UINT16                  Opcode,
663     ACPI_OPERAND_OBJECT     *Operand0,
664     ACPI_OPERAND_OBJECT     *Operand1,
665     BOOLEAN                 *LogicalResult)
666 {
667     ACPI_OPERAND_OBJECT     *LocalOperand1 = Operand1;
668     ACPI_INTEGER            Integer0;
669     ACPI_INTEGER            Integer1;
670     UINT32                  Length0;
671     UINT32                  Length1;
672     ACPI_STATUS             Status = AE_OK;
673     BOOLEAN                 LocalResult = FALSE;
674     int                     Compare;
675
676
677     ACPI_FUNCTION_TRACE ("ExDoLogicalOp");
678
679
680     /*
681      * Convert the second operand if necessary.  The first operand
682      * determines the type of the second operand, (See the Data Types
683      * section of the ACPI 3.0+ specification.)  Both object types are
684      * guaranteed to be either Integer/String/Buffer by the operand
685      * resolution mechanism.
686      */
687     switch (ACPI_GET_OBJECT_TYPE (Operand0))
688     {
689     case ACPI_TYPE_INTEGER:
690         Status = AcpiExConvertToInteger (Operand1, &LocalOperand1, 16);
691         break;
692
693     case ACPI_TYPE_STRING:
694         Status = AcpiExConvertToString (Operand1, &LocalOperand1,
695                     ACPI_IMPLICIT_CONVERT_HEX);
696         break;
697
698     case ACPI_TYPE_BUFFER:
699         Status = AcpiExConvertToBuffer (Operand1, &LocalOperand1);
700         break;
701
702     default:
703         Status = AE_AML_INTERNAL;
704         break;
705     }
706
707     if (ACPI_FAILURE (Status))
708     {
709         goto Cleanup;
710     }
711
712     /*
713      * Two cases: 1) Both Integers, 2) Both Strings or Buffers
714      */
715     if (ACPI_GET_OBJECT_TYPE (Operand0) == ACPI_TYPE_INTEGER)
716     {
717         /*
718          * 1) Both operands are of type integer
719          *    Note: LocalOperand1 may have changed above
720          */
721         Integer0 = Operand0->Integer.Value;
722         Integer1 = LocalOperand1->Integer.Value;
723
724         switch (Opcode)
725         {
726         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
727
728             if (Integer0 == Integer1)
729             {
730                 LocalResult = TRUE;
731             }
732             break;
733
734         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
735
736             if (Integer0 > Integer1)
737             {
738                 LocalResult = TRUE;
739             }
740             break;
741
742         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
743
744             if (Integer0 < Integer1)
745             {
746                 LocalResult = TRUE;
747             }
748             break;
749
750         default:
751             Status = AE_AML_INTERNAL;
752             break;
753         }
754     }
755     else
756     {
757         /*
758          * 2) Both operands are Strings or both are Buffers
759          *    Note: Code below takes advantage of common Buffer/String
760          *          object fields. LocalOperand1 may have changed above. Use
761          *          memcmp to handle nulls in buffers.
762          */
763         Length0 = Operand0->Buffer.Length;
764         Length1 = LocalOperand1->Buffer.Length;
765
766         /* Lexicographic compare: compare the data bytes */
767
768         Compare = ACPI_MEMCMP ((const char * ) Operand0->Buffer.Pointer,
769                     (const char * ) LocalOperand1->Buffer.Pointer,
770                     (Length0 > Length1) ? Length1 : Length0);
771
772         switch (Opcode)
773         {
774         case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
775
776             /* Length and all bytes must be equal */
777
778             if ((Length0 == Length1) &&
779                 (Compare == 0))
780             {
781                 /* Length and all bytes match ==> TRUE */
782
783                 LocalResult = TRUE;
784             }
785             break;
786
787         case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
788
789             if (Compare > 0)
790             {
791                 LocalResult = TRUE;
792                 goto Cleanup;   /* TRUE */
793             }
794             if (Compare < 0)
795             {
796                 goto Cleanup;   /* FALSE */
797             }
798
799             /* Bytes match (to shortest length), compare lengths */
800
801             if (Length0 > Length1)
802             {
803                 LocalResult = TRUE;
804             }
805             break;
806
807         case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
808
809             if (Compare > 0)
810             {
811                 goto Cleanup;   /* FALSE */
812             }
813             if (Compare < 0)
814             {
815                 LocalResult = TRUE;
816                 goto Cleanup;   /* TRUE */
817             }
818
819             /* Bytes match (to shortest length), compare lengths */
820
821             if (Length0 < Length1)
822             {
823                 LocalResult = TRUE;
824             }
825             break;
826
827         default:
828             Status = AE_AML_INTERNAL;
829             break;
830         }
831     }
832
833 Cleanup:
834
835     /* New object was created if implicit conversion performed - delete */
836
837     if (LocalOperand1 != Operand1)
838     {
839         AcpiUtRemoveReference (LocalOperand1);
840     }
841
842     /* Return the logical result and status */
843
844     *LogicalResult = LocalResult;
845     return_ACPI_STATUS (Status);
846 }
847
848