Debian Codegraph @

source/compiler/aslexternal.c

raw symbols SCIP
1 /******************************************************************************
2 *
3 * Module Name: aslexternal - ASL External opcode compiler support
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2026, 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 MERCHANTABILITY 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 "acparser.h"
47 #include "amlcode.h"
48 #include "acnamesp.h"
49
50
51 #define _COMPONENT ACPI_COMPILER
52 ACPI_MODULE_NAME ("aslexternal")
53
54
55 /* Local prototypes */
56
57 static void
58 ExInsertArgCount (
59 ACPI_PARSE_OBJECT *Op);
60
61 static void
62 ExMoveExternals (
63 ACPI_PARSE_OBJECT *DefinitionBlockOp);
64
65 static void
66 ExRemoveOpFromTree (
67 ACPI_PARSE_OBJECT *Op);
68
69
70 /*******************************************************************************
71 *
72 * FUNCTION: ExDoExternal
73 *
74 * PARAMETERS: Op - Current Parse node
75 *
76 * RETURN: None
77 *
78 * DESCRIPTION: Add an External() definition to the global list. This list
79 * is used to generate External opcodes.
80 *
81 ******************************************************************************/
82
83 void
84 ExDoExternal (
85 ACPI_PARSE_OBJECT *Op)
86 {
87 ACPI_PARSE_OBJECT *ListOp;
88 ACPI_PARSE_OBJECT *Prev;
89 ACPI_PARSE_OBJECT *Next;
90 ACPI_PARSE_OBJECT *ArgCountOp;
91 ACPI_PARSE_OBJECT *TypeOp;
92 ACPI_PARSE_OBJECT *ExternTypeOp = Op->Asl.Child->Asl.Next;
93 ACPI_PARSE_OBJECT *Existing;
94 ACPI_PARSE_OBJECT *NameOp = Op->Asl.Child;
95 ACPI_PARSE_OBJECT *ExistingNameOp;
96 UINT32 ExternType;
97 UINT8 ParamCount = ASL_EXTERNAL_METHOD_UNKNOWN_PARAMS;
98 UINT32 ParamTypes[ACPI_METHOD_NUM_ARGS];
99 char *NewPath = NULL;
100 char *ExistingPath;
101
102
103 ExternType = AnMapObjTypeToBtype (ExternTypeOp);
104 if (ExternType != ACPI_BTYPE_METHOD)
105 {
106 /*
107 * If this is not a method, it has zero parameters this local variable
108 * is used only for methods
109 */
110 ParamCount = 0;
111 }
112
113 /*
114 * The parser allows optional parameter return types regardless of the
115 * type. Check object type keyword emit error if optional parameter/return
116 * types exist.
117 *
118 * Check the parameter return type
119 */
120 TypeOp = ExternTypeOp->Asl.Next;
121 if (TypeOp->Asl.Child)
122 {
123 /* Ignore the return type for now. */
124
125 (void) MtProcessTypeOp (TypeOp->Asl.Child);
126 if (ExternType != ACPI_BTYPE_METHOD)
127 {
128 sprintf (AslGbl_MsgBuffer, "Found type [%s]", AcpiUtGetTypeName(ExternType));
129 AslError (ASL_ERROR, ASL_MSG_EXTERN_INVALID_RET_TYPE, TypeOp,
130 AslGbl_MsgBuffer);
131 }
132 }
133
134 /* Check the parameter types */
135
136 TypeOp = TypeOp->Asl.Next;
137 if (TypeOp->Asl.Child)
138 {
139 ParamCount = MtProcessParameterTypeList (TypeOp->Asl.Child, ParamTypes);
140 if (ExternType != ACPI_BTYPE_METHOD)
141 {
142 sprintf (AslGbl_MsgBuffer, "Found type [%s]", AcpiUtGetTypeName(ExternType));
143 AslError (ASL_ERROR, ASL_MSG_EXTERN_INVALID_PARAM_TYPE, TypeOp,
144 AslGbl_MsgBuffer);
145 }
146 }
147
148 ArgCountOp = Op->Asl.Child->Asl.Next->Asl.Next;
149 ArgCountOp->Asl.AmlOpcode = AML_RAW_DATA_BYTE;
150 ArgCountOp->Asl.ParseOpcode = PARSEOP_BYTECONST;
151 ArgCountOp->Asl.Value.Integer = ParamCount;
152 UtSetParseOpName (ArgCountOp);
153
154 /*
155 * Deduplicate identical External() declarations. The disassembler
156 * already collapses duplicates; mirror that behavior in the compiler
157 * so duplicate externals do not bloat emitted AML.
158 */
159
160 NewPath = NameOp->Asl.Namepath;
161 if (!NewPath && NameOp->Asl.Value.String)
162 {
163 (void) UtInternalizeName (NameOp->Asl.Value.String, &NewPath);
164 NameOp->Asl.Namepath = NewPath;
165 }
166
167 Existing = AslGbl_ExternalsListHead;
168 while (Existing && NewPath)
169 {
170 ExistingNameOp = Existing->Asl.Child->Asl.Child;
171 ExistingPath = ExistingNameOp->Asl.Namepath;
172 if (!ExistingPath && ExistingNameOp->Asl.Value.String)
173 {
174 (void) UtInternalizeName (ExistingNameOp->Asl.Value.String,
175 &ExistingPath);
176 ExistingNameOp->Asl.Namepath = ExistingPath;
177 }
178
179 if (ExistingPath && !strcmp (NewPath, ExistingPath))
180 {
181 ACPI_PARSE_OBJECT *ExistingArgCount;
182
183 /* Update arg count if this instance is more specific */
184
185 ExistingArgCount = Existing->Asl.Child->Asl.Child->Asl.Next->Asl.Next;
186 if ((ExternType == ACPI_BTYPE_METHOD) &&
187 (ParamCount < ExistingArgCount->Asl.Value.Integer))
188 {
189 ExistingArgCount->Asl.Value.Integer = ParamCount;
190 }
191
192 /* Remove duplicate External node from the parse tree */
193
194 ExRemoveOpFromTree (Op);
195 return;
196 }
197
198 Existing = Existing->Asl.Next;
199 }
200
201 /* Create new list node of arbitrary type */
202
203 ListOp = TrAllocateOp (PARSEOP_DEFAULT_ARG);
204
205 /* Store External node as child */
206
207 ListOp->Asl.Child = Op;
208 ListOp->Asl.Next = NULL;
209
210 if (AslGbl_ExternalsListHead)
211 {
212 /* Link new External to end of list */
213
214 Prev = AslGbl_ExternalsListHead;
215 Next = Prev;
216 while (Next)
217 {
218 Prev = Next;
219 Next = Next->Asl.Next;
220 }
221
222 Prev->Asl.Next = ListOp;
223 }
224 else
225 {
226 AslGbl_ExternalsListHead = ListOp;
227 }
228 }
229
230
231 /*******************************************************************************
232 *
233 * FUNCTION: ExRemoveOpFromTree
234 *
235 * PARAMETERS: Op - Parse op to remove from the tree
236 *
237 * RETURN: None
238 *
239 * DESCRIPTION: Unlink a parse op from its parent sibling list. Used to drop
240 * duplicate External() declarations before AML emission.
241 *
242 ******************************************************************************/
243
244 static void
245 ExRemoveOpFromTree (
246 ACPI_PARSE_OBJECT *Op)
247 {
248 ACPI_PARSE_OBJECT *Parent;
249 ACPI_PARSE_OBJECT *Child;
250
251
252 if (!Op || !Op->Asl.Parent)
253 {
254 return;
255 }
256
257 Parent = Op->Asl.Parent;
258 Child = Parent->Asl.Child;
259
260 if (Child == Op)
261 {
262 Parent->Asl.Child = Op->Asl.Next;
263 return;
264 }
265
266 while (Child && Child->Asl.Next != Op)
267 {
268 Child = Child->Asl.Next;
269 }
270
271 if (Child)
272 {
273 Child->Asl.Next = Op->Asl.Next;
274 }
275 }
276
277
278 /*******************************************************************************
279 *
280 * FUNCTION: ExInsertArgCount
281 *
282 * PARAMETERS: Op - Op for a method invocation
283 *
284 * RETURN: None
285 *
286 * DESCRIPTION: Obtain the number of arguments for a control method -- from
287 * the actual invocation.
288 *
289 ******************************************************************************/
290
291 static void
292 ExInsertArgCount (
293 ACPI_PARSE_OBJECT *Op)
294 {
295 ACPI_PARSE_OBJECT *Next;
296 ACPI_PARSE_OBJECT *NameOp;
297 ACPI_PARSE_OBJECT *Child;
298 ACPI_PARSE_OBJECT *ArgCountOp;
299 char * ExternalName;
300 char * CallName;
301 UINT16 ArgCount = 0;
302 ACPI_STATUS Status;
303
304
305 CallName = AcpiNsGetNormalizedPathname (Op->Asl.Node, TRUE);
306
307 Next = AslGbl_ExternalsListHead;
308 while (Next)
309 {
310 ArgCount = 0;
311
312 /* Skip if External node already handled */
313
314 if (Next->Asl.Child->Asl.CompileFlags & OP_VISITED)
315 {
316 Next = Next->Asl.Next;
317 continue;
318 }
319
320 NameOp = Next->Asl.Child->Asl.Child;
321 ExternalName = AcpiNsGetNormalizedPathname (NameOp->Asl.Node, TRUE);
322
323 if (strcmp (CallName, ExternalName))
324 {
325 ACPI_FREE (ExternalName);
326 Next = Next->Asl.Next;
327 continue;
328 }
329
330 Next->Asl.Child->Asl.CompileFlags |= OP_VISITED;
331
332 /*
333 * Since we will reposition Externals to the Root, set Namepath
334 * to the fully qualified name and recalculate the aml length
335 */
336 Status = UtInternalizeName (ExternalName,
337 &NameOp->Asl.Value.String);
338
339 ACPI_FREE (ExternalName);
340 if (ACPI_FAILURE (Status))
341 {
342 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
343 NULL, "- Could not Internalize External");
344 break;
345 }
346
347 NameOp->Asl.AmlLength = strlen (NameOp->Asl.Value.String);
348
349 /* Get argument count */
350
351 Child = Op->Asl.Child;
352 while (Child)
353 {
354 ArgCount++;
355 Child = Child->Asl.Next;
356 }
357
358 /* Setup ArgCount operand */
359
360 ArgCountOp = Next->Asl.Child->Asl.Child->Asl.Next->Asl.Next;
361 ArgCountOp->Asl.Value.Integer = ArgCount;
362 break;
363 }
364
365 ACPI_FREE (CallName);
366 }
367
368
369 /*******************************************************************************
370 *
371 * FUNCTION: ExAmlExternalWalkBegin
372 *
373 * PARAMETERS: ASL_WALK_CALLBACK
374 *
375 * RETURN: None
376 *
377 * DESCRIPTION: Parse tree walk to create external opcode list for methods.
378 *
379 ******************************************************************************/
380
381 ACPI_STATUS
382 ExAmlExternalWalkBegin (
383 ACPI_PARSE_OBJECT *Op,
384 UINT32 Level,
385 void *Context)
386 {
387
388 /* External list head saved in the definition block op */
389
390 if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
391 {
392 AslGbl_ExternalsListHead = Op->Asl.Value.Arg;
393 }
394
395 if (!AslGbl_ExternalsListHead)
396 {
397 return (AE_OK);
398 }
399
400 if (Op->Asl.ParseOpcode != PARSEOP_METHODCALL)
401 {
402 return (AE_OK);
403 }
404
405 /*
406 * The NameOp child under an ExternalOp gets turned into PARSE_METHODCALL
407 * by XfNamespaceLocateBegin(). Ignore these.
408 */
409 if (Op->Asl.Parent &&
410 Op->Asl.Parent->Asl.ParseOpcode == PARSEOP_EXTERNAL)
411 {
412 return (AE_OK);
413 }
414
415 ExInsertArgCount (Op);
416 return (AE_OK);
417 }
418
419
420 /*******************************************************************************
421 *
422 * FUNCTION: ExAmlExternalWalkEnd
423 *
424 * PARAMETERS: ASL_WALK_CALLBACK
425 *
426 * RETURN: None
427 *
428 * DESCRIPTION: Parse tree walk to create external opcode list for methods.
429 * Here, we just want to catch the case where a definition block
430 * has been completed. Then we move all of the externals into
431 * a single block in the parse tree and thus the AML code.
432 *
433 ******************************************************************************/
434
435 ACPI_STATUS
436 ExAmlExternalWalkEnd (
437 ACPI_PARSE_OBJECT *Op,
438 UINT32 Level,
439 void *Context)
440 {
441
442 if (Op->Asl.ParseOpcode == PARSEOP_DEFINITION_BLOCK)
443 {
444 /*
445 * Process any existing external list. (Support for
446 * multiple definition blocks in a single file/compile)
447 */
448 ExMoveExternals (Op);
449 AslGbl_ExternalsListHead = NULL;
450 }
451
452 return (AE_OK);
453 }
454
455
456 /*******************************************************************************
457 *
458 * FUNCTION: ExMoveExternals
459 *
460 * PARAMETERS: DefinitionBlockOp - Op for current definition block
461 *
462 * RETURN: None
463 *
464 * DESCRIPTION: Move all externals present in the source file into a single
465 * block of AML code, surrounded by an "If (0)" to prevent
466 * AML interpreters from attempting to execute the External
467 * opcodes.
468 *
469 ******************************************************************************/
470
471 static void
472 ExMoveExternals (
473 ACPI_PARSE_OBJECT *DefinitionBlockOp)
474 {
475 ACPI_PARSE_OBJECT *ParentOp;
476 ACPI_PARSE_OBJECT *ExternalOp;
477 ACPI_PARSE_OBJECT *PredicateOp;
478 ACPI_PARSE_OBJECT *NextOp;
479 ACPI_PARSE_OBJECT *Prev;
480 ACPI_PARSE_OBJECT *Next;
481 char *ExternalName;
482 ACPI_OBJECT_TYPE ObjType;
483 ACPI_STATUS Status;
484 UINT32 i;
485
486
487 if (!AslGbl_ExternalsListHead)
488 {
489 return;
490 }
491
492 /* Remove the External nodes from the tree */
493
494 NextOp = AslGbl_ExternalsListHead;
495 while (NextOp)
496 {
497 /*
498 * The External is stored in child pointer of each node in the
499 * list
500 */
501 ExternalOp = NextOp->Asl.Child;
502
503 /* Get/set the fully qualified name */
504
505 ExternalName = AcpiNsGetNormalizedPathname (ExternalOp->Asl.Node, TRUE);
506 ExternalOp->Asl.ExternalName = ExternalName;
507 ExternalOp->Asl.Namepath = ExternalName;
508
509 /* Set line numbers (for listings, etc.) */
510
511 ExternalOp->Asl.LineNumber = 0;
512 ExternalOp->Asl.LogicalLineNumber = 0;
513
514 Next = ExternalOp->Asl.Child;
515 Next->Asl.LineNumber = 0;
516 Next->Asl.LogicalLineNumber = 0;
517
518 if (Next->Asl.ParseOpcode == PARSEOP_NAMESEG)
519 {
520 Next->Asl.ParseOpcode = PARSEOP_NAMESTRING;
521 }
522
523 Next->Asl.ExternalName = ExternalName;
524 Status = UtInternalizeName (ExternalName, &Next->Asl.Value.String);
525 if (ACPI_FAILURE (Status))
526 {
527 AslError (ASL_ERROR, ASL_MSG_COMPILER_INTERNAL,
528 Next, "Could not internalize namestring");
529 return;
530 }
531
532 Next->Asl.AmlLength = strlen (Next->Asl.Value.String);
533
534 Next = Next->Asl.Next;
535 Next->Asl.LineNumber = 0;
536 Next->Asl.LogicalLineNumber = 0;
537
538 Next = Next->Asl.Next;
539 Next->Asl.LineNumber = 0;
540 Next->Asl.LogicalLineNumber = 0;
541
542 Next = Next->Asl.Next;
543 Next->Asl.LineNumber = 0;
544 Next->Asl.LogicalLineNumber = 0;
545
546 ParentOp = ExternalOp->Asl.Parent;
547 Prev = Next = ParentOp->Asl.Child;
548
549 /* Now find the External node's position in parse tree */
550
551 while (Next != ExternalOp)
552 {
553 Prev = Next;
554 Next = Next->Asl.Next;
555 }
556
557 /* Remove the External from the parse tree */
558
559 if (Prev == ExternalOp)
560 {
561 /* External was the first child node */
562
563 ParentOp->Asl.Child = ExternalOp->Asl.Next;
564 }
565
566 Prev->Asl.Next = ExternalOp->Asl.Next;
567 ExternalOp->Asl.Next = NULL;
568 ExternalOp->Asl.Parent = AslGbl_ExternalsListHead;
569
570 /* Point the External to the next in the list */
571
572 if (NextOp->Asl.Next)
573 {
574 ExternalOp->Asl.Next = NextOp->Asl.Next->Asl.Child;
575 }
576
577 NextOp = NextOp->Asl.Next;
578 }
579
580 /*
581 * Loop again to remove MethodObj Externals for which
582 * a MethodCall was not found (dead external reference)
583 */
584 Prev = AslGbl_ExternalsListHead->Asl.Child;
585 Next = Prev;
586 while (Next)
587 {
588 ObjType = (ACPI_OBJECT_TYPE)
589 Next->Asl.Child->Asl.Next->Asl.Value.Integer;
590
591 if (ObjType == ACPI_TYPE_METHOD &&
592 !(Next->Asl.CompileFlags & OP_VISITED))
593 {
594 if (Next == Prev)
595 {
596 AslGbl_ExternalsListHead->Asl.Child = Next->Asl.Next;
597 Next->Asl.Next = NULL;
598 Prev = AslGbl_ExternalsListHead->Asl.Child;
599 Next = Prev;
600 continue;
601 }
602 else
603 {
604 Prev->Asl.Next = Next->Asl.Next;
605 Next->Asl.Next = NULL;
606 Next = Prev->Asl.Next;
607 continue;
608 }
609 }
610
611 Prev = Next;
612 Next = Next->Asl.Next;
613 }
614
615 /* If list is now empty, don't bother to make If (0) block */
616
617 if (!AslGbl_ExternalsListHead->Asl.Child)
618 {
619 return;
620 }
621
622 /* Convert Gbl_ExternalsListHead parent to If(). */
623
624 AslGbl_ExternalsListHead->Asl.ParseOpcode = PARSEOP_IF;
625 AslGbl_ExternalsListHead->Asl.AmlOpcode = AML_IF_OP;
626 AslGbl_ExternalsListHead->Asl.CompileFlags = OP_AML_PACKAGE;
627 UtSetParseOpName (AslGbl_ExternalsListHead);
628
629 /* Create a Zero op for the If predicate */
630
631 PredicateOp = TrAllocateOp (PARSEOP_ZERO);
632 PredicateOp->Asl.AmlOpcode = AML_ZERO_OP;
633
634 PredicateOp->Asl.Parent = AslGbl_ExternalsListHead;
635 PredicateOp->Asl.Child = NULL;
636 PredicateOp->Asl.Next = AslGbl_ExternalsListHead->Asl.Child;
637 AslGbl_ExternalsListHead->Asl.Child = PredicateOp;
638
639 /* Set line numbers (for listings, etc.) */
640
641 AslGbl_ExternalsListHead->Asl.LineNumber = 0;
642 AslGbl_ExternalsListHead->Asl.LogicalLineNumber = 0;
643
644 PredicateOp->Asl.LineNumber = 0;
645 PredicateOp->Asl.LogicalLineNumber = 0;
646
647 /* Insert block back in the list */
648
649 Prev = DefinitionBlockOp->Asl.Child;
650 Next = Prev;
651
652 /* Find last default arg */
653
654 for (i = 0; i < 6; i++)
655 {
656 Prev = Next;
657 Next = Prev->Asl.Next;
658 }
659
660 if (Next)
661 {
662 /* Definition Block is not empty */
663
664 AslGbl_ExternalsListHead->Asl.Next = Next;
665 }
666 else
667 {
668 /* Definition Block is empty. */
669
670 AslGbl_ExternalsListHead->Asl.Next = NULL;
671 }
672
673 Prev->Asl.Next = AslGbl_ExternalsListHead;
674 AslGbl_ExternalsListHead->Asl.Parent = Prev->Asl.Parent;
675 }