:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / drivers / bus / acpi / parser / psscope.c
1 /******************************************************************************
2  *
3  * Module Name: psscope - Parser scope stack management routines
4  *              $Revision$
5  *
6  *****************************************************************************/
7
8 /*
9  *  Copyright (C) 2000, 2001 R. Byron Moore
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26
27 #include "acpi.h"
28 #include "acparser.h"
29
30 #define _COMPONENT          ACPI_PARSER
31          MODULE_NAME         ("psscope")
32
33
34 /*******************************************************************************
35  *
36  * FUNCTION:    Acpi_ps_get_parent_scope
37  *
38  * PARAMETERS:  Parser_state        - Current parser state object
39  *
40  * RETURN:      Pointer to an Op object
41  *
42  * DESCRIPTION: Get parent of current op being parsed
43  *
44  ******************************************************************************/
45
46 ACPI_PARSE_OBJECT *
47 acpi_ps_get_parent_scope (
48         ACPI_PARSE_STATE        *parser_state)
49 {
50         return (parser_state->scope->parse_scope.op);
51 }
52
53
54 /*******************************************************************************
55  *
56  * FUNCTION:    Acpi_ps_has_completed_scope
57  *
58  * PARAMETERS:  Parser_state        - Current parser state object
59  *
60  * RETURN:      Boolean, TRUE = scope completed.
61  *
62  * DESCRIPTION: Is parsing of current argument complete?  Determined by
63  *              1) AML pointer is at or beyond the end of the scope
64  *              2) The scope argument count has reached zero.
65  *
66  ******************************************************************************/
67
68 u8
69 acpi_ps_has_completed_scope (
70         ACPI_PARSE_STATE        *parser_state)
71 {
72         return ((u8) ((parser_state->aml >= parser_state->scope->parse_scope.arg_end ||
73                            !parser_state->scope->parse_scope.arg_count)));
74 }
75
76
77 /*******************************************************************************
78  *
79  * FUNCTION:    Acpi_ps_init_scope
80  *
81  * PARAMETERS:  Parser_state        - Current parser state object
82  *              Root                - the Root Node of this new scope
83  *
84  * RETURN:      Status
85  *
86  * DESCRIPTION: Allocate and init a new scope object
87  *
88  ******************************************************************************/
89
90 ACPI_STATUS
91 acpi_ps_init_scope (
92         ACPI_PARSE_STATE        *parser_state,
93         ACPI_PARSE_OBJECT       *root_op)
94 {
95         ACPI_GENERIC_STATE      *scope;
96
97
98         scope = acpi_cm_create_generic_state ();
99         if (!scope) {
100                 return (AE_NO_MEMORY);
101         }
102
103         scope->parse_scope.op       = root_op;
104         scope->parse_scope.arg_count = ACPI_VAR_ARGS;
105         scope->parse_scope.arg_end  = parser_state->aml_end;
106         scope->parse_scope.pkg_end  = parser_state->aml_end;
107
108         parser_state->scope         = scope;
109         parser_state->start_op      = root_op;
110
111         return (AE_OK);
112 }
113
114
115 /*******************************************************************************
116  *
117  * FUNCTION:    Acpi_ps_push_scope
118  *
119  * PARAMETERS:  Parser_state        - Current parser state object
120  *              Op                  - Current op to be pushed
121  *              Remaining_args      - List of args remaining
122  *              Arg_count           - Fixed or variable number of args
123  *
124  * RETURN:      Status
125  *
126  * DESCRIPTION: Push current op to begin parsing its argument
127  *
128  ******************************************************************************/
129
130 ACPI_STATUS
131 acpi_ps_push_scope (
132         ACPI_PARSE_STATE        *parser_state,
133         ACPI_PARSE_OBJECT       *op,
134         u32                     remaining_args,
135         u32                     arg_count)
136 {
137         ACPI_GENERIC_STATE      *scope;
138
139
140         scope = acpi_cm_create_generic_state ();
141         if (!scope) {
142                 return (AE_NO_MEMORY);
143         }
144
145
146         scope->parse_scope.op          = op;
147         scope->parse_scope.arg_list    = remaining_args;
148         scope->parse_scope.arg_count   = arg_count;
149         scope->parse_scope.pkg_end     = parser_state->pkg_end;
150
151         /* Push onto scope stack */
152
153         acpi_cm_push_generic_state (&parser_state->scope, scope);
154
155
156         if (arg_count == ACPI_VAR_ARGS) {
157                 /* multiple arguments */
158
159                 scope->parse_scope.arg_end = parser_state->pkg_end;
160         }
161
162         else {
163                 /* single argument */
164
165                 scope->parse_scope.arg_end = ACPI_MAX_AML;
166         }
167
168         return (AE_OK);
169 }
170
171
172 /*******************************************************************************
173  *
174  * FUNCTION:    Acpi_ps_pop_scope
175  *
176  * PARAMETERS:  Parser_state        - Current parser state object
177  *              Op                  - Where the popped op is returned
178  *              Arg_list            - Where the popped "next argument" is
179  *                                    returned
180  *              Arg_count           - Count of objects in Arg_list
181  *
182  * RETURN:      Status
183  *
184  * DESCRIPTION: Return to parsing a previous op
185  *
186  ******************************************************************************/
187
188 void
189 acpi_ps_pop_scope (
190         ACPI_PARSE_STATE        *parser_state,
191         ACPI_PARSE_OBJECT       **op,
192         u32                     *arg_list,
193         u32                     *arg_count)
194 {
195         ACPI_GENERIC_STATE      *scope = parser_state->scope;
196
197
198         /*
199          * Only pop the scope if there is in fact a next scope
200          */
201         if (scope->common.next) {
202                 scope = acpi_cm_pop_generic_state (&parser_state->scope);
203
204
205                 /* return to parsing previous op */
206
207                 *op                     = scope->parse_scope.op;
208                 *arg_list               = scope->parse_scope.arg_list;
209                 *arg_count              = scope->parse_scope.arg_count;
210                 parser_state->pkg_end   = scope->parse_scope.pkg_end;
211
212                 /* All done with this scope state structure */
213
214                 acpi_cm_delete_generic_state (scope);
215         }
216
217         else {
218                 /* empty parse stack, prepare to fetch next opcode */
219
220                 *op                     = NULL;
221                 *arg_list               = 0;
222                 *arg_count              = 0;
223         }
224
225
226         return;
227 }
228
229
230 /*******************************************************************************
231  *
232  * FUNCTION:    Acpi_ps_cleanup_scope
233  *
234  * PARAMETERS:  Parser_state        - Current parser state object
235  *
236  * RETURN:      Status
237  *
238  * DESCRIPTION: Destroy available list, remaining stack levels, and return
239  *              root scope
240  *
241  ******************************************************************************/
242
243 void
244 acpi_ps_cleanup_scope (
245         ACPI_PARSE_STATE        *parser_state)
246 {
247         ACPI_GENERIC_STATE      *scope;
248
249
250         if (!parser_state) {
251                 return;
252         }
253
254
255         /* Delete anything on the scope stack */
256
257         while (parser_state->scope) {
258                 scope = acpi_cm_pop_generic_state (&parser_state->scope);
259                 acpi_cm_delete_generic_state (scope);
260         }
261
262         return;
263 }
264