1 /******************************************************************************
2  *
3  * Module Name: aslbtypes - Support for bitfield types
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2016, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
25  *
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.
29  *
30  * NO WARRANTY
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.
42  */
43 
44 #include "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 #include "amlcode.h"
47 
48 
49 #define _COMPONENT          ACPI_COMPILER
50         ACPI_MODULE_NAME    ("aslbtypes")
51 
52 /* Local prototypes */
53 
54 static UINT32
55 AnMapEtypeToBtype (
56     UINT32                  Etype);
57 
58 
59 /*******************************************************************************
60  *
61  * FUNCTION:    AnMapArgTypeToBtype
62  *
63  * PARAMETERS:  ArgType             - The ARGI required type(s) for this
64  *                                    argument, from the opcode info table
65  *
66  * RETURN:      The corresponding Bit-encoded types
67  *
68  * DESCRIPTION: Convert an encoded ARGI required argument type code into a
69  *              bitfield type code. Implements the implicit source conversion
70  *              rules.
71  *
72  ******************************************************************************/
73 
74 UINT32
75 AnMapArgTypeToBtype (
76     UINT32                  ArgType)
77 {
78 
79     switch (ArgType)
80     {
81     /* Simple types */
82 
83     case ARGI_ANYTYPE:
84 
85         return (ACPI_BTYPE_OBJECTS_AND_REFS);
86 
87     case ARGI_PACKAGE:
88 
89         return (ACPI_BTYPE_PACKAGE);
90 
91     case ARGI_EVENT:
92 
93         return (ACPI_BTYPE_EVENT);
94 
95     case ARGI_MUTEX:
96 
97         return (ACPI_BTYPE_MUTEX);
98 
99     case ARGI_DDBHANDLE:
100         /*
101          * DDBHandleObject := SuperName
102          * ACPI_BTYPE_REFERENCE_OBJECT:
103          *      Index reference as parameter of Load/Unload
104          */
105         return (ACPI_BTYPE_DDB_HANDLE | ACPI_BTYPE_REFERENCE_OBJECT);
106 
107     /* Interchangeable types */
108     /*
109      * Source conversion rules:
110      * Integer, String, and Buffer are all interchangeable
111      */
112     case ARGI_INTEGER:
113     case ARGI_STRING:
114     case ARGI_BUFFER:
115     case ARGI_BUFFER_OR_STRING:
116     case ARGI_COMPUTEDATA:
117 
118         return (ACPI_BTYPE_COMPUTE_DATA);
119 
120     /* References */
121 
122     case ARGI_INTEGER_REF:
123 
124         return (ACPI_BTYPE_INTEGER);
125 
126     case ARGI_OBJECT_REF:
127 
128         return (ACPI_BTYPE_ALL_OBJECTS);
129 
130     case ARGI_DEVICE_REF:
131 
132         return (ACPI_BTYPE_DEVICE_OBJECTS);
133 
134     case ARGI_REFERENCE:
135 
136         return (ACPI_BTYPE_NAMED_REFERENCE); /* Name or Namestring */
137 
138     case ARGI_TARGETREF:
139 
140         /*
141          * Target operand for most math and logic operators.
142          * Package objects not allowed as target.
143          */
144         return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_DEBUG_OBJECT |
145             ACPI_BTYPE_REFERENCE_OBJECT);
146 
147     case ARGI_STORE_TARGET:
148 
149         /* Special target for Store(), includes packages */
150 
151         return (ACPI_BTYPE_DATA | ACPI_BTYPE_DEBUG_OBJECT |
152             ACPI_BTYPE_REFERENCE_OBJECT);
153 
154     case ARGI_SIMPLE_TARGET:
155 
156         return (ACPI_BTYPE_OBJECTS_AND_REFS);
157 
158     /* Complex types */
159 
160     case ARGI_DATAOBJECT:
161         /*
162          * Buffer, string, package or reference to a Op -
163          * Used only by SizeOf operator
164          */
165         return (ACPI_BTYPE_STRING | ACPI_BTYPE_BUFFER |
166             ACPI_BTYPE_PACKAGE | ACPI_BTYPE_REFERENCE_OBJECT);
167 
168     case ARGI_COMPLEXOBJ:
169 
170         /* Buffer, String, or package */
171 
172         return (ACPI_BTYPE_STRING | ACPI_BTYPE_BUFFER |
173             ACPI_BTYPE_PACKAGE);
174 
175     case ARGI_REF_OR_STRING:
176 
177         /* Used by DeRefOf operator only */
178 
179         return (ACPI_BTYPE_STRING | ACPI_BTYPE_REFERENCE_OBJECT);
180 
181     case ARGI_REGION_OR_BUFFER:
182 
183         /* Used by Load() only. Allow buffers in addition to regions/fields */
184 
185         return (ACPI_BTYPE_REGION | ACPI_BTYPE_BUFFER |
186             ACPI_BTYPE_FIELD_UNIT);
187 
188     case ARGI_DATAREFOBJ:
189 
190         /* Used by Store() only, as the source operand */
191 
192         return (ACPI_BTYPE_DATA_REFERENCE | ACPI_BTYPE_REFERENCE_OBJECT);
193 
194     default:
195 
196         break;
197     }
198 
199     return (ACPI_BTYPE_OBJECTS_AND_REFS);
200 }
201 
202 
203 /*******************************************************************************
204  *
205  * FUNCTION:    AnMapEtypeToBtype
206  *
207  * PARAMETERS:  Etype               - Encoded ACPI Type
208  *
209  * RETURN:      Btype corresponding to the Etype
210  *
211  * DESCRIPTION: Convert an encoded ACPI type to a bitfield type applying the
212  *              operand conversion rules. In other words, returns the type(s)
213  *              this Etype is implicitly converted to during interpretation.
214  *
215  ******************************************************************************/
216 
217 static UINT32
218 AnMapEtypeToBtype (
219     UINT32                  Etype)
220 {
221 
222     if (Etype == ACPI_TYPE_ANY)
223     {
224         return (ACPI_BTYPE_OBJECTS_AND_REFS);
225     }
226 
227     /* Try the standard ACPI data types */
228 
229     if (Etype <= ACPI_TYPE_EXTERNAL_MAX)
230     {
231         /*
232          * This switch statement implements the allowed operand conversion
233          * rules as per the "ASL Data Types" section of the ACPI
234          * specification.
235          */
236         switch (Etype)
237         {
238         case ACPI_TYPE_INTEGER:
239 
240             return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_DDB_HANDLE);
241 
242         case ACPI_TYPE_STRING:
243         case ACPI_TYPE_BUFFER:
244 
245             return (ACPI_BTYPE_COMPUTE_DATA);
246 
247         case ACPI_TYPE_PACKAGE:
248 
249             return (ACPI_BTYPE_PACKAGE);
250 
251         case ACPI_TYPE_FIELD_UNIT:
252 
253             return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_FIELD_UNIT);
254 
255         case ACPI_TYPE_BUFFER_FIELD:
256 
257             return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_BUFFER_FIELD);
258 
259         case ACPI_TYPE_DDB_HANDLE:
260 
261             return (ACPI_BTYPE_INTEGER | ACPI_BTYPE_DDB_HANDLE);
262 
263         case ACPI_TYPE_DEBUG_OBJECT:
264 
265             /* Cannot be used as a source operand */
266 
267             return (0);
268 
269         default:
270 
271             return (1 << (Etype - 1));
272         }
273     }
274 
275     /* Try the internal data types */
276 
277     switch (Etype)
278     {
279     case ACPI_TYPE_LOCAL_REGION_FIELD:
280     case ACPI_TYPE_LOCAL_BANK_FIELD:
281     case ACPI_TYPE_LOCAL_INDEX_FIELD:
282 
283         /* Named fields can be either Integer/Buffer/String */
284 
285         return (ACPI_BTYPE_COMPUTE_DATA | ACPI_BTYPE_FIELD_UNIT);
286 
287     case ACPI_TYPE_LOCAL_ALIAS:
288 
289         return (ACPI_BTYPE_INTEGER);
290 
291 
292     case ACPI_TYPE_LOCAL_RESOURCE:
293     case ACPI_TYPE_LOCAL_RESOURCE_FIELD:
294 
295         return (ACPI_BTYPE_REFERENCE_OBJECT);
296 
297     default:
298 
299         printf ("Unhandled encoded type: %X\n", Etype);
300         return (0);
301     }
302 }
303 
304 
305 /*******************************************************************************
306  *
307  * FUNCTION:    AnFormatBtype
308  *
309  * PARAMETERS:  Btype               - Bitfield of ACPI types
310  *              Buffer              - Where to put the ascii string
311  *
312  * RETURN:      None.
313  *
314  * DESCRIPTION: Convert a Btype to a string of ACPI types
315  *
316  ******************************************************************************/
317 
318 void
319 AnFormatBtype (
320     char                    *Buffer,
321     UINT32                  Btype)
322 {
323     UINT32                  Type;
324     BOOLEAN                 First = TRUE;
325 
326 
327     *Buffer = 0;
328     if (Btype == 0)
329     {
330         strcat (Buffer, "NoReturnValue");
331         return;
332     }
333 
334     for (Type = 1; Type <= ACPI_TYPE_EXTERNAL_MAX; Type++)
335     {
336         if (Btype & 0x00000001)
337         {
338             if (!First)
339             {
340                 strcat (Buffer, "|");
341             }
342 
343             First = FALSE;
344             strcat (Buffer, AcpiUtGetTypeName (Type));
345         }
346         Btype >>= 1;
347     }
348 
349     if (Btype & 0x00000001)
350     {
351         if (!First)
352         {
353             strcat (Buffer, "|");
354         }
355 
356         First = FALSE;
357         strcat (Buffer, "Reference");
358     }
359 
360     Btype >>= 1;
361     if (Btype & 0x00000001)
362     {
363         if (!First)
364         {
365             strcat (Buffer, "|");
366         }
367 
368         First = FALSE;
369         strcat (Buffer, "Resource");
370     }
371 }
372 
373 
374 /*******************************************************************************
375  *
376  * FUNCTION:    AnGetBtype
377  *
378  * PARAMETERS:  Op                  - Parse node whose type will be returned.
379  *
380  * RETURN:      The Btype associated with the Op.
381  *
382  * DESCRIPTION: Get the (bitfield) ACPI type associated with the parse node.
383  *              Handles the case where the node is a name or method call and
384  *              the actual type must be obtained from the namespace node.
385  *
386  ******************************************************************************/
387 
388 UINT32
389 AnGetBtype (
390     ACPI_PARSE_OBJECT       *Op)
391 {
392     ACPI_NAMESPACE_NODE     *Node;
393     ACPI_PARSE_OBJECT       *ReferencedNode;
394     UINT32                  ThisNodeBtype = 0;
395 
396 
397     if (!Op)
398     {
399         AcpiOsPrintf ("Null Op in AnGetBtype\n");
400         return (ACPI_UINT32_MAX);
401     }
402 
403     if ((Op->Asl.ParseOpcode == PARSEOP_NAMESEG)     ||
404         (Op->Asl.ParseOpcode == PARSEOP_NAMESTRING)  ||
405         (Op->Asl.ParseOpcode == PARSEOP_METHODCALL))
406     {
407         Node = Op->Asl.Node;
408         if (!Node)
409         {
410             /* These are not expected to have a node at this time */
411 
412             if ((Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEWORDFIELD) ||
413                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEDWORDFIELD) ||
414                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEQWORDFIELD) ||
415                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEBYTEFIELD) ||
416                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEBITFIELD) ||
417                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CREATEFIELD)    ||
418                 (Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_CONDREFOF))
419             {
420                 return (ACPI_UINT32_MAX - 1);
421             }
422 
423             DbgPrint (ASL_DEBUG_OUTPUT,
424                 "No attached Nsnode: [%s] at line %u name [%s], "
425                 "ignoring typecheck. Parent [%s]\n",
426                 Op->Asl.ParseOpName, Op->Asl.LineNumber,
427                 Op->Asl.ExternalName, Op->Asl.Parent->Asl.ParseOpName);
428             return (ACPI_UINT32_MAX - 1);
429         }
430 
431         ThisNodeBtype = AnMapEtypeToBtype (Node->Type);
432         if (!ThisNodeBtype)
433         {
434             AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
435                 "could not map type");
436         }
437 
438         if (Op->Asl.ParseOpcode == PARSEOP_METHODCALL)
439         {
440             ReferencedNode = Node->Op;
441             if (!ReferencedNode)
442             {
443                 /* Check for an internal method */
444 
445                 if (AnIsInternalMethod (Op))
446                 {
447                     return (AnGetInternalMethodReturnType (Op));
448                 }
449 
450                 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL, Op,
451                     "null Op pointer");
452                 return (ACPI_UINT32_MAX);
453             }
454 
455             if (ReferencedNode->Asl.CompileFlags & NODE_METHOD_TYPED)
456             {
457                 ThisNodeBtype = ReferencedNode->Asl.AcpiBtype;
458             }
459             else
460             {
461                 return (ACPI_UINT32_MAX -1);
462             }
463         }
464     }
465     else
466     {
467         ThisNodeBtype = Op->Asl.AcpiBtype;
468     }
469 
470     return (ThisNodeBtype);
471 }
472 
473 
474 /*******************************************************************************
475  *
476  * FUNCTION:    AnMapObjTypeToBtype
477  *
478  * PARAMETERS:  Op                  - A parse node
479  *
480  * RETURN:      A Btype
481  *
482  * DESCRIPTION: Map object to the associated "Btype"
483  *
484  ******************************************************************************/
485 
486 UINT32
487 AnMapObjTypeToBtype (
488     ACPI_PARSE_OBJECT       *Op)
489 {
490 
491     switch (Op->Asl.ParseOpcode)
492     {
493     case PARSEOP_OBJECTTYPE_BFF:        /* "BuffFieldObj" */
494 
495         return (ACPI_BTYPE_BUFFER_FIELD);
496 
497     case PARSEOP_OBJECTTYPE_BUF:        /* "BuffObj" */
498 
499         return (ACPI_BTYPE_BUFFER);
500 
501     case PARSEOP_OBJECTTYPE_DDB:        /* "DDBHandleObj" */
502 
503         return (ACPI_BTYPE_DDB_HANDLE);
504 
505     case PARSEOP_OBJECTTYPE_DEV:        /* "DeviceObj" */
506 
507         return (ACPI_BTYPE_DEVICE);
508 
509     case PARSEOP_OBJECTTYPE_EVT:        /* "EventObj" */
510 
511         return (ACPI_BTYPE_EVENT);
512 
513     case PARSEOP_OBJECTTYPE_FLD:        /* "FieldUnitObj" */
514 
515         return (ACPI_BTYPE_FIELD_UNIT);
516 
517     case PARSEOP_OBJECTTYPE_INT:        /* "IntObj" */
518 
519         return (ACPI_BTYPE_INTEGER);
520 
521     case PARSEOP_OBJECTTYPE_MTH:        /* "MethodObj" */
522 
523         return (ACPI_BTYPE_METHOD);
524 
525     case PARSEOP_OBJECTTYPE_MTX:        /* "MutexObj" */
526 
527         return (ACPI_BTYPE_MUTEX);
528 
529     case PARSEOP_OBJECTTYPE_OPR:        /* "OpRegionObj" */
530 
531         return (ACPI_BTYPE_REGION);
532 
533     case PARSEOP_OBJECTTYPE_PKG:        /* "PkgObj" */
534 
535         return (ACPI_BTYPE_PACKAGE);
536 
537     case PARSEOP_OBJECTTYPE_POW:        /* "PowerResObj" */
538 
539         return (ACPI_BTYPE_POWER);
540 
541     case PARSEOP_OBJECTTYPE_STR:        /* "StrObj" */
542 
543         return (ACPI_BTYPE_STRING);
544 
545     case PARSEOP_OBJECTTYPE_THZ:        /* "ThermalZoneObj" */
546 
547         return (ACPI_BTYPE_THERMAL);
548 
549     case PARSEOP_OBJECTTYPE_UNK:        /* "UnknownObj" */
550 
551         return (ACPI_BTYPE_OBJECTS_AND_REFS);
552 
553     default:
554 
555         return (0);
556     }
557 }
558 
559 
560 #ifdef ACPI_OBSOLETE_FUNCTIONS
561 /*******************************************************************************
562  *
563  * FUNCTION:    AnMapBtypeToEtype
564  *
565  * PARAMETERS:  Btype               - Bitfield of ACPI types
566  *
567  * RETURN:      The Etype corresponding the the Btype
568  *
569  * DESCRIPTION: Convert a bitfield type to an encoded type
570  *
571  ******************************************************************************/
572 
573 UINT32
574 AnMapBtypeToEtype (
575     UINT32              Btype)
576 {
577     UINT32              i;
578     UINT32              Etype;
579 
580 
581     if (Btype == 0)
582     {
583         return (0);
584     }
585 
586     Etype = 1;
587     for (i = 1; i < Btype; i *= 2)
588     {
589         Etype++;
590     }
591 
592     return (Etype);
593 }
594 #endif
595