]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/ddb/db_expr.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
28 * Revision 1.1.1.1 1998/09/22 21:05:48 wsanchez
29 * Import of Mac OS X kernel (~semeria)
31 * Revision 1.1.1.1 1998/03/07 02:26:09 wsanchez
32 * Import of OSF Mach kernel (~mburg)
34 * Revision 1.2.19.1 1997/03/27 18:46:35 barbou
35 * ri-osc CR1561: make operators "logical and", "logical or"
37 * [1995/09/20 15:26:38 bolinger]
40 * Revision 1.2.10.2 1995/01/06 19:10:13 devrcs
41 * mk6 CR668 - 1.3b26 merge
42 * * Revision 1.2.3.5 1994/05/06 18:39:16 tmt
43 * Merged osc1.3dec/shared with osc1.3b19
44 * Merge Alpha changes into osc1.312b source code.
47 * [1994/11/04 08:49:27 dwm]
49 * Revision 1.2.10.1 1994/09/23 01:19:06 ezf
50 * change marker to not FREE
51 * [1994/09/22 21:09:53 ezf]
53 * Revision 1.2.3.3 1993/07/27 18:27:15 elliston
54 * Add ANSI prototypes. CR #9523.
55 * [1993/07/27 18:11:36 elliston]
57 * Revision 1.2.3.2 1993/06/09 02:20:06 gm
58 * Added to OSF/1 R1.3 from NMK15.0.
59 * [1993/06/02 20:56:16 jeffc]
61 * Revision 1.2 1993/04/19 16:02:09 devrcs
62 * Allow unprefixed (0x) hexadecimal constants starting by a letter:
63 * unknown symbols are tentatively interpreted as hexadecimal constants,
64 * and ambiguities are reported.
68 * Removed unused variable from db_unary().
72 * Added string format arguments [barbou@gr.osf.org]
75 * Revision 1.1 1992/09/30 02:01:04 robert
82 * Revision 2.5 91/10/09 15:59:46 af
83 * Revision 2.4.3.1 91/10/05 13:06:04 jeffreyh
84 * Added relational expression etc. to support condition expression.
85 * Supported modifier after indirect expression to specify size,
86 * sign extention and non current task space indirection.
87 * Changed error messages to print more information.
90 * Revision 2.4.3.1 91/10/05 13:06:04 jeffreyh
91 * Added relational expression etc. to support condition expression.
92 * Supported modifier after indirect expression to specify size,
93 * sign extention and non current task space indirection.
94 * Changed error messages to print more information.
97 * Revision 2.4 91/05/14 15:33:45 mrt
98 * Correcting copyright
100 * Revision 2.3 91/02/05 17:06:25 mrt
101 * Changed to new Mach copyright
102 * [91/01/31 16:17:46 mrt]
104 * Revision 2.2 90/08/27 21:50:57 dbg
105 * Use '..' instead of '$$' for db_prev.
106 * Use '+' for db_next.
109 * Allow repeated unary operators.
112 * Reflected back rename of db_symbol_value->db_value_of_name
122 * Mach Operating System
123 * Copyright (c) 1991,1990 Carnegie Mellon University
124 * All Rights Reserved.
126 * Permission to use, copy, modify and distribute this software and its
127 * documentation is hereby granted, provided that both the copyright
128 * notice and this permission notice appear in all copies of the
129 * software, derivative works or modified versions, and any portions
130 * thereof, and that both notices appear in supporting documentation.
132 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
133 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
134 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
136 * Carnegie Mellon requests users of this software to return to
138 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
139 * School of Computer Science
140 * Carnegie Mellon University
141 * Pittsburgh PA 15213-3890
143 * any improvements or extensions that they make and grant Carnegie Mellon
144 * the rights to redistribute these changes.
149 * Author: David B. Golub, Carnegie Mellon University
153 #include <mach/boolean.h>
154 #include <machine/db_machdep.h>
155 #include <ddb/db_access.h>
156 #include <ddb/db_command.h>
157 #include <ddb/db_expr.h>
158 #include <ddb/db_lex.h>
159 #include <ddb/db_output.h> /* For db_printf() */
160 #include <ddb/db_sym.h>
161 #include <ddb/db_variables.h>
162 #include <kern/task.h>
166 /* Prototypes for functions local to this file. XXX -- should be static!
168 boolean_t
db_term(db_expr_t
*valuep
);
169 boolean_t
db_unary(db_expr_t
*valuep
);
170 boolean_t
db_mult_expr(db_expr_t
*valuep
);
171 boolean_t
db_add_expr(db_expr_t
*valuep
);
172 boolean_t
db_shift_expr(db_expr_t
*valuep
);
173 boolean_t
db_logical_relation_expr(db_expr_t
*valuep
);
174 boolean_t
db_logical_and_expr(db_expr_t
*valuep
);
175 boolean_t
db_logical_or_expr(db_expr_t
*valuep
);
178 /* try to interpret unknown symbols as hexadecimal constants */
179 int db_allow_unprefixed_hexa
= 1;
182 db_term(db_expr_t
*valuep
)
185 boolean_t valid_symbol
= FALSE
;
186 boolean_t valid_hexa
= FALSE
;
188 switch(t
= db_read_token()) {
190 if (db_value_of_name(db_tok_string
, valuep
)) {
193 if (db_allow_unprefixed_hexa
&& db_radix
== 16 &&
200 for (cp
= db_tok_string
; *cp
; cp
++) {
201 if (*cp
>= 'a' && *cp
<= 'f') {
202 value
= value
* 16 + 10 + (*cp
- 'a');
203 } else if (*cp
>= 'A' && *cp
<= 'F') {
204 value
= value
* 16 + 10 + (*cp
- 'A');
205 } else if (*cp
>= '0' && *cp
<= '9') {
206 value
= value
* 16 + (*cp
- '0');
214 db_printf("Ambiguous constant %x used as a symbol\n",
217 *valuep
= (db_expr_t
)value
;
221 if (!valid_symbol
&& !valid_hexa
) {
222 db_printf("Symbol \"%s\" not found\n", db_tok_string
);
228 *valuep
= /*(db_expr_t)*/db_tok_number
;
231 *valuep
= (db_expr_t
)db_dot
;
234 *valuep
= (db_expr_t
)db_prev
;
237 *valuep
= (db_expr_t
) db_next
;
240 *valuep
= (db_expr_t
)db_last_addr
;
243 if (!db_get_variable(valuep
))
247 if (!db_expression(valuep
)) {
248 db_error("Unmached ()s\n");
253 db_printf("')' expected at \"%s...\"\n", db_tok_string
);
260 static db_tok_offset
= 0;
263 sp
= (char *)db_tok_string
+ db_tok_offset
;
264 *valuep
= *(int *)sp
;
266 *cp
&& cp
< sp
+ sizeof (int);
268 if (cp
== sp
+ sizeof (int) && *cp
) {
269 db_tok_offset
+= sizeof (int);
289 int size
= sizeof(int);
293 for (p
= modif
; *p
; p
++) {
299 size
= sizeof(short);
316 db_unary(db_expr_t
*valuep
)
320 boolean_t u_opt
, t_opt
;
322 extern task_t db_default_task
;
326 if (!db_unary(valuep
)) {
327 db_error("Expression syntax error after '-'\n");
335 if (!db_unary(valuep
)) {
336 db_error("Expression syntax error after '*'\n");
340 size
= sizeof(db_addr_t
);
343 if (t
== tIDENT
&& db_tok_string
[0] == ':') {
344 size
= db_size_option(&db_tok_string
[1], &u_opt
, &t_opt
);
346 task
= db_default_task
;
349 *valuep
= db_get_task_value((db_addr_t
)*valuep
, size
, !u_opt
, task
);
353 if (!db_unary(valuep
)) {
354 db_error("Expression syntax error after '!'\n");
357 *valuep
= (!(*valuep
));
361 return (db_term(valuep
));
365 db_mult_expr(db_expr_t
*valuep
)
375 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
377 c
= db_tok_string
[0];
378 if (!db_term(&rhs
)) {
379 db_printf("Expression syntax error after '%c'\n", c
);
392 db_error("Divide by 0\n");
400 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
410 db_add_expr(db_expr_t
*valuep
)
416 if (!db_mult_expr(&lhs
))
420 while (t
== tPLUS
|| t
== tMINUS
|| t
== tBIT_OR
) {
421 c
= db_tok_string
[0];
422 if (!db_mult_expr(&rhs
)) {
423 db_printf("Expression syntax error after '%c'\n", c
);
429 else if (t
== tMINUS
)
441 db_shift_expr(db_expr_t
*valuep
)
446 if (!db_add_expr(&lhs
))
450 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
451 if (!db_add_expr(&rhs
)) {
452 db_printf("Expression syntax error after \"%s\"\n",
453 (t
== tSHIFT_L
)? "<<": ">>");
458 db_error("Negative shift amount\n");
464 /* Shift right is unsigned */
465 lhs
= (natural_t
) lhs
>> rhs
;
475 db_logical_relation_expr(db_expr_t
*valuep
)
481 if (!db_shift_expr(&lhs
))
485 while (t
== tLOG_EQ
|| t
== tLOG_NOT_EQ
486 || t
== tGREATER
|| t
== tGREATER_EQ
487 || t
== tLESS
|| t
== tLESS_EQ
) {
488 op
[0] = db_tok_string
[0];
489 op
[1] = db_tok_string
[1];
491 if (!db_shift_expr(&rhs
)) {
492 db_printf("Expression syntax error after \"%s\"\n", op
);
524 db_logical_and_expr(db_expr_t
*valuep
)
529 if (!db_logical_relation_expr(&lhs
))
533 while (t
== tLOG_AND
) {
534 if (!db_logical_relation_expr(&rhs
)) {
535 db_error("Expression syntax error after \"&&\"\n");
547 db_logical_or_expr(db_expr_t
*valuep
)
552 if (!db_logical_and_expr(&lhs
))
556 while (t
== tLOG_OR
) {
557 if (!db_logical_and_expr(&rhs
)) {
558 db_error("Expression syntax error after \"||\"\n");
570 db_expression(db_expr_t
*valuep
)
572 return (db_logical_or_expr(valuep
));