1 /* $NetBSD: db_expr.c,v 1.15 2007/02/22 06:41:01 thorpej Exp $ */
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
28 * Author: David B. Golub, Carnegie Mellon University
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: db_expr.c,v 1.15 2007/02/22 06:41:01 thorpej Exp $");
35 #include <sys/param.h>
40 static bool db_term(db_expr_t
*);
41 static bool db_unary(db_expr_t
*);
42 static bool db_mult_expr(db_expr_t
*);
43 static bool db_add_expr(db_expr_t
*);
44 static bool db_shift_expr(db_expr_t
*);
47 db_term(db_expr_t
*valuep
)
53 if (!db_value_of_name(db_tok_string
, valuep
)) {
57 /* See if we can make a number out of all of it */
58 for (i
= 0; (c
= db_tok_string
[i
]) != '\0'; i
++) {
60 if (c
>= '0' && c
<= '9')
62 else if (db_radix
== 16 && c
>= 'a' && c
<= 'f')
64 else if (db_radix
== 16 && c
>= 'A' && c
<= 'F')
67 db_error("Symbol not found\n");
69 v
= v
* db_radix
+ byte
;
71 *valuep
= (db_expr_t
)v
;
76 *valuep
= (db_expr_t
)db_tok_number
;
80 *valuep
= (db_expr_t
)db_dot
;
84 *valuep
= (db_expr_t
)db_prev
;
88 *valuep
= (db_expr_t
) db_next
;
92 *valuep
= (db_expr_t
)db_last_addr
;
96 if (!db_get_variable(valuep
))
101 if (!db_expression(valuep
)) {
102 db_error("Syntax error\n");
107 db_error("Syntax error\n");
117 db_unary(db_expr_t
*valuep
)
123 if (!db_unary(valuep
)) {
124 db_error("Syntax error\n");
132 if (!db_unary(valuep
)) {
133 db_error("Syntax error\n");
136 *valuep
= db_get_value((db_addr_t
)*valuep
, sizeof(db_expr_t
),
141 return (db_term(valuep
));
145 db_mult_expr(db_expr_t
*valuep
)
154 while (t
== tSTAR
|| t
== tSLASH
|| t
== tPCT
|| t
== tHASH
) {
155 if (!db_term(&rhs
)) {
156 db_error("Syntax error\n");
163 db_error("Divide by 0\n");
171 lhs
= ((lhs
+rhs
-1)/rhs
)*rhs
;
181 db_add_expr(db_expr_t
*valuep
)
186 if (!db_mult_expr(&lhs
))
190 while (t
== tPLUS
|| t
== tMINUS
) {
191 if (!db_mult_expr(&rhs
)) {
192 db_error("Syntax error\n");
207 db_shift_expr(db_expr_t
*valuep
)
212 if (!db_add_expr(&lhs
))
216 while (t
== tSHIFT_L
|| t
== tSHIFT_R
) {
217 if (!db_add_expr(&rhs
)) {
218 db_error("Syntax error\n");
222 db_error("Negative shift amount\n");
228 /* Shift right is unsigned */
229 lhs
= (unsigned long) lhs
>> rhs
;
239 db_expression(db_expr_t
*valuep
)
242 return (db_shift_expr(valuep
));