2 * SPDX-License-Identifier: MIT-CMU
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
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.
29 * Author: David B. Golub, Carnegie Mellon University
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <ddb/db_lex.h>
41 #include <ddb/db_access.h>
42 #include <ddb/db_command.h>
44 static bool db_add_expr(db_expr_t *valuep);
45 static bool db_mult_expr(db_expr_t *valuep);
46 static bool db_shift_expr(db_expr_t *valuep);
47 static bool db_term(db_expr_t *valuep);
48 static bool db_unary(db_expr_t *valuep);
49 static bool db_logical_or_expr(db_expr_t *valuep);
50 static bool db_logical_and_expr(db_expr_t *valuep);
51 static bool db_logical_relation_expr(db_expr_t *valuep);
54 db_term(db_expr_t *valuep)
60 if (!db_value_of_name(db_tok_string, valuep) &&
61 !db_value_of_name_pcpu(db_tok_string, valuep) &&
62 !db_value_of_name_vnet(db_tok_string, valuep)) {
63 db_printf("Symbol '%s' not found\n", db_tok_string);
70 *valuep = (db_expr_t)db_tok_number;
74 *valuep = (db_expr_t)db_dot;
78 *valuep = (db_expr_t)db_prev;
82 *valuep = (db_expr_t) db_next;
86 *valuep = (db_expr_t)db_last_addr;
90 if (!db_get_variable(valuep))
95 if (!db_expression(valuep)) {
96 db_printf("Expression syntax error after '%c'\n", '(');
102 db_printf("Expression syntax error -- expected '%c'\n", ')');
113 db_unary(db_expr_t *valuep)
119 if (!db_unary(valuep)) {
120 db_printf("Expression syntax error after '%c'\n", '-');
128 if(!db_unary(valuep)) {
129 db_printf("Expression syntax error after '%c'\n", '!');
133 *valuep = (!(*valuep));
137 if(!db_unary(valuep)) {
138 db_printf("Expression syntax error after '%c'\n", '~');
142 *valuep = (~(*valuep));
147 if (!db_unary(valuep)) {
148 db_printf("Expression syntax error after '%c'\n", '*');
152 *valuep = db_get_value((db_addr_t)*valuep, sizeof(void *),
157 return (db_term(valuep));
161 db_mult_expr(db_expr_t *valuep)
170 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH ||
172 if (!db_term(&rhs)) {
173 db_printf("Expression syntax error after '%c'\n",
174 t == tSTAR ? '*' : t == tSLASH ? '/' : t == tPCT ? '%' :
175 t == tHASH ? '#' : '&');
188 db_error("Division by 0\n");
196 lhs = roundup(lhs, rhs);
206 db_add_expr(db_expr_t *valuep)
211 if (!db_mult_expr(&lhs))
215 while (t == tPLUS || t == tMINUS || t == tBIT_OR) {
216 if (!db_mult_expr(&rhs)) {
217 db_printf("Expression syntax error after '%c'\n",
218 t == tPLUS ? '+' : t == tMINUS ? '-' : '|');
233 __assert_unreachable();
243 db_shift_expr(db_expr_t *valuep)
248 if (!db_add_expr(&lhs))
251 while (t == tSHIFT_L || t == tSHIFT_R) {
252 if (!db_add_expr(&rhs)) {
253 db_printf("Expression syntax error after '%s'\n",
254 t == tSHIFT_L ? "<<" : ">>");
259 db_printf("Negative shift amount %jd\n", (intmax_t)rhs);
266 /* Shift right is unsigned */
267 lhs = (db_addr_t)lhs >> rhs;
277 db_logical_relation_expr(
283 if (!db_shift_expr(&lhs))
287 while (t == tLOG_EQ || t == tLOG_NOT_EQ || t == tGREATER ||
288 t == tGREATER_EQ || t == tLESS || t == tLESS_EQ) {
289 if (!db_shift_expr(&rhs)) {
290 db_printf("Expression syntax error after '%s'\n",
291 t == tLOG_EQ ? "==" : t == tLOG_NOT_EQ ? "!=" :
292 t == tGREATER ? ">" : t == tGREATER_EQ ? ">=" :
293 t == tLESS ? "<" : "<=");
317 __assert_unreachable();
333 if (!db_logical_relation_expr(&lhs))
337 while (t == tLOG_AND) {
338 if (!db_logical_relation_expr(&rhs)) {
339 db_printf("Expression syntax error after '%s'\n", "&&");
358 if (!db_logical_and_expr(&lhs))
362 while (t == tLOG_OR) {
363 if (!db_logical_and_expr(&rhs)) {
364 db_printf("Expression syntax error after '%s'\n", "||");
377 db_expression(db_expr_t *valuep)
379 return (db_logical_or_expr(valuep));