1 /******************************************************************************
2  *
3  * Module Name: psscope - Parser scope stack management routines
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2004, R. Byron Moore
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 
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 
48 #define _COMPONENT          ACPI_PARSER
49 	 ACPI_MODULE_NAME    ("psscope")
50 
51 
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_ps_get_parent_scope
55  *
56  * PARAMETERS:  parser_state        - Current parser state object
57  *
58  * RETURN:      Pointer to an Op object
59  *
60  * DESCRIPTION: Get parent of current op being parsed
61  *
62  ******************************************************************************/
63 
64 union acpi_parse_object *
acpi_ps_get_parent_scope(struct acpi_parse_state * parser_state)65 acpi_ps_get_parent_scope (
66 	struct acpi_parse_state         *parser_state)
67 {
68 	return (parser_state->scope->parse_scope.op);
69 }
70 
71 
72 /*******************************************************************************
73  *
74  * FUNCTION:    acpi_ps_has_completed_scope
75  *
76  * PARAMETERS:  parser_state        - Current parser state object
77  *
78  * RETURN:      Boolean, TRUE = scope completed.
79  *
80  * DESCRIPTION: Is parsing of current argument complete?  Determined by
81  *              1) AML pointer is at or beyond the end of the scope
82  *              2) The scope argument count has reached zero.
83  *
84  ******************************************************************************/
85 
86 u8
acpi_ps_has_completed_scope(struct acpi_parse_state * parser_state)87 acpi_ps_has_completed_scope (
88 	struct acpi_parse_state         *parser_state)
89 {
90 	return ((u8) ((parser_state->aml >= parser_state->scope->parse_scope.arg_end ||
91 			   !parser_state->scope->parse_scope.arg_count)));
92 }
93 
94 
95 /*******************************************************************************
96  *
97  * FUNCTION:    acpi_ps_init_scope
98  *
99  * PARAMETERS:  parser_state        - Current parser state object
100  *              Root                - the Root Node of this new scope
101  *
102  * RETURN:      Status
103  *
104  * DESCRIPTION: Allocate and init a new scope object
105  *
106  ******************************************************************************/
107 
108 acpi_status
acpi_ps_init_scope(struct acpi_parse_state * parser_state,union acpi_parse_object * root_op)109 acpi_ps_init_scope (
110 	struct acpi_parse_state         *parser_state,
111 	union acpi_parse_object         *root_op)
112 {
113 	union acpi_generic_state        *scope;
114 
115 
116 	ACPI_FUNCTION_TRACE_PTR ("ps_init_scope", root_op);
117 
118 
119 	scope = acpi_ut_create_generic_state ();
120 	if (!scope) {
121 		return_ACPI_STATUS (AE_NO_MEMORY);
122 	}
123 
124 	scope->common.data_type     = ACPI_DESC_TYPE_STATE_RPSCOPE;
125 	scope->parse_scope.op       = root_op;
126 	scope->parse_scope.arg_count = ACPI_VAR_ARGS;
127 	scope->parse_scope.arg_end  = parser_state->aml_end;
128 	scope->parse_scope.pkg_end  = parser_state->aml_end;
129 
130 	parser_state->scope         = scope;
131 	parser_state->start_op      = root_op;
132 
133 	return_ACPI_STATUS (AE_OK);
134 }
135 
136 
137 /*******************************************************************************
138  *
139  * FUNCTION:    acpi_ps_push_scope
140  *
141  * PARAMETERS:  parser_state        - Current parser state object
142  *              Op                  - Current op to be pushed
143  *              remaining_args      - List of args remaining
144  *              arg_count           - Fixed or variable number of args
145  *
146  * RETURN:      Status
147  *
148  * DESCRIPTION: Push current op to begin parsing its argument
149  *
150  ******************************************************************************/
151 
152 acpi_status
acpi_ps_push_scope(struct acpi_parse_state * parser_state,union acpi_parse_object * op,u32 remaining_args,u32 arg_count)153 acpi_ps_push_scope (
154 	struct acpi_parse_state         *parser_state,
155 	union acpi_parse_object         *op,
156 	u32                             remaining_args,
157 	u32                             arg_count)
158 {
159 	union acpi_generic_state        *scope;
160 
161 
162 	ACPI_FUNCTION_TRACE_PTR ("ps_push_scope", op);
163 
164 
165 	scope = acpi_ut_create_generic_state ();
166 	if (!scope) {
167 		return_ACPI_STATUS (AE_NO_MEMORY);
168 	}
169 
170 	scope->common.data_type        = ACPI_DESC_TYPE_STATE_PSCOPE;
171 	scope->parse_scope.op          = op;
172 	scope->parse_scope.arg_list    = remaining_args;
173 	scope->parse_scope.arg_count   = arg_count;
174 	scope->parse_scope.pkg_end     = parser_state->pkg_end;
175 
176 	/* Push onto scope stack */
177 
178 	acpi_ut_push_generic_state (&parser_state->scope, scope);
179 
180 	if (arg_count == ACPI_VAR_ARGS) {
181 		/* multiple arguments */
182 
183 		scope->parse_scope.arg_end = parser_state->pkg_end;
184 	}
185 	else {
186 		/* single argument */
187 
188 		scope->parse_scope.arg_end = ACPI_TO_POINTER (ACPI_MAX_PTR);
189 	}
190 
191 	return_ACPI_STATUS (AE_OK);
192 }
193 
194 
195 /*******************************************************************************
196  *
197  * FUNCTION:    acpi_ps_pop_scope
198  *
199  * PARAMETERS:  parser_state        - Current parser state object
200  *              Op                  - Where the popped op is returned
201  *              arg_list            - Where the popped "next argument" is
202  *                                    returned
203  *              arg_count           - Count of objects in arg_list
204  *
205  * RETURN:      Status
206  *
207  * DESCRIPTION: Return to parsing a previous op
208  *
209  ******************************************************************************/
210 
211 void
acpi_ps_pop_scope(struct acpi_parse_state * parser_state,union acpi_parse_object ** op,u32 * arg_list,u32 * arg_count)212 acpi_ps_pop_scope (
213 	struct acpi_parse_state         *parser_state,
214 	union acpi_parse_object         **op,
215 	u32                             *arg_list,
216 	u32                             *arg_count)
217 {
218 	union acpi_generic_state        *scope = parser_state->scope;
219 
220 
221 	ACPI_FUNCTION_TRACE ("ps_pop_scope");
222 
223 
224 	/*
225 	 * Only pop the scope if there is in fact a next scope
226 	 */
227 	if (scope->common.next) {
228 		scope = acpi_ut_pop_generic_state (&parser_state->scope);
229 
230 		/* return to parsing previous op */
231 
232 		*op                     = scope->parse_scope.op;
233 		*arg_list               = scope->parse_scope.arg_list;
234 		*arg_count              = scope->parse_scope.arg_count;
235 		parser_state->pkg_end   = scope->parse_scope.pkg_end;
236 
237 		/* All done with this scope state structure */
238 
239 		acpi_ut_delete_generic_state (scope);
240 	}
241 	else {
242 		/* empty parse stack, prepare to fetch next opcode */
243 
244 		*op                     = NULL;
245 		*arg_list               = 0;
246 		*arg_count              = 0;
247 	}
248 
249 	ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "Popped Op %p Args %X\n", *op, *arg_count));
250 	return_VOID;
251 }
252 
253 
254 /*******************************************************************************
255  *
256  * FUNCTION:    acpi_ps_cleanup_scope
257  *
258  * PARAMETERS:  parser_state        - Current parser state object
259  *
260  * RETURN:      Status
261  *
262  * DESCRIPTION: Destroy available list, remaining stack levels, and return
263  *              root scope
264  *
265  ******************************************************************************/
266 
267 void
acpi_ps_cleanup_scope(struct acpi_parse_state * parser_state)268 acpi_ps_cleanup_scope (
269 	struct acpi_parse_state         *parser_state)
270 {
271 	union acpi_generic_state        *scope;
272 
273 
274 	ACPI_FUNCTION_TRACE_PTR ("ps_cleanup_scope", parser_state);
275 
276 
277 	if (!parser_state) {
278 		return_VOID;
279 	}
280 
281 	/* Delete anything on the scope stack */
282 
283 	while (parser_state->scope) {
284 		scope = acpi_ut_pop_generic_state (&parser_state->scope);
285 		acpi_ut_delete_generic_state (scope);
286 	}
287 
288 	return_VOID;
289 }
290 
291