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>
39 #include <ddb/db_lex.h>
40 #include <ddb/db_access.h>
41 #include <ddb/db_command.h>
43 static bool db_add_expr(db_expr_t *valuep);
44 static bool db_mult_expr(db_expr_t *valuep);
45 static bool db_shift_expr(db_expr_t *valuep);
46 static bool db_term(db_expr_t *valuep);
47 static bool db_unary(db_expr_t *valuep);
48 static bool db_logical_or_expr(db_expr_t *valuep);
49 static bool db_logical_and_expr(db_expr_t *valuep);
50 static bool db_logical_relation_expr(db_expr_t *valuep);
53 db_term(db_expr_t *valuep)
59 if (!db_value_of_name(db_tok_string, valuep) &&
60 !db_value_of_name_pcpu(db_tok_string, valuep) &&
61 !db_value_of_name_vnet(db_tok_string, valuep)) {
62 db_printf("Symbol '%s' not found\n", db_tok_string);
69 *valuep = (db_expr_t)db_tok_number;
73 *valuep = (db_expr_t)db_dot;
77 *valuep = (db_expr_t)db_prev;
81 *valuep = (db_expr_t) db_next;
85 *valuep = (db_expr_t)db_last_addr;
89 if (!db_get_variable(valuep))
94 if (!db_expression(valuep)) {
95 db_printf("Expression syntax error after '%c'\n", '(');
101 db_printf("Expression syntax error -- expected '%c'\n", ')');
112 db_unary(db_expr_t *valuep)
118 if (!db_unary(valuep)) {
119 db_printf("Expression syntax error after '%c'\n", '-');
127 if(!db_unary(valuep)) {
128 db_printf("Expression syntax error after '%c'\n", '!');
132 *valuep = (!(*valuep));
136 if(!db_unary(valuep)) {
137 db_printf("Expression syntax error after '%c'\n", '~');
141 *valuep = (~(*valuep));
146 if (!db_unary(valuep)) {
147 db_printf("Expression syntax error after '%c'\n", '*');
151 *valuep = db_get_value((db_addr_t)*valuep, sizeof(void *),
156 return (db_term(valuep));
160 db_mult_expr(db_expr_t *valuep)
169 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH ||
171 if (!db_term(&rhs)) {
172 db_printf("Expression syntax error after '%c'\n",
173 t == tSTAR ? '*' : t == tSLASH ? '/' : t == tPCT ? '%' :
174 t == tHASH ? '#' : '&');
187 db_error("Division by 0\n");
195 lhs = roundup(lhs, rhs);
205 db_add_expr(db_expr_t *valuep)
210 if (!db_mult_expr(&lhs))
214 while (t == tPLUS || t == tMINUS || t == tBIT_OR) {
215 if (!db_mult_expr(&rhs)) {
216 db_printf("Expression syntax error after '%c'\n",
217 t == tPLUS ? '+' : t == tMINUS ? '-' : '|');
242 db_shift_expr(db_expr_t *valuep)
247 if (!db_add_expr(&lhs))
250 while (t == tSHIFT_L || t == tSHIFT_R) {
251 if (!db_add_expr(&rhs)) {
252 db_printf("Expression syntax error after '%s'\n",
253 t == tSHIFT_L ? "<<" : ">>");
258 db_printf("Negative shift amount %jd\n", (intmax_t)rhs);
265 /* Shift right is unsigned */
266 lhs = (db_addr_t)lhs >> rhs;
276 db_logical_relation_expr(
282 if (!db_shift_expr(&lhs))
286 while (t == tLOG_EQ || t == tLOG_NOT_EQ || t == tGREATER ||
287 t == tGREATER_EQ || t == tLESS || t == tLESS_EQ) {
288 if (!db_shift_expr(&rhs)) {
289 db_printf("Expression syntax error after '%s'\n",
290 t == tLOG_EQ ? "==" : t == tLOG_NOT_EQ ? "!=" :
291 t == tGREATER ? ">" : t == tGREATER_EQ ? ">=" :
292 t == tLESS ? "<" : "<=");
332 if (!db_logical_relation_expr(&lhs))
336 while (t == tLOG_AND) {
337 if (!db_logical_relation_expr(&rhs)) {
338 db_printf("Expression syntax error after '%s'\n", "&&");
357 if (!db_logical_and_expr(&lhs))
361 while (t == tLOG_OR) {
362 if (!db_logical_and_expr(&rhs)) {
363 db_printf("Expression syntax error after '%s'\n", "||");
376 db_expression(db_expr_t *valuep)
378 return (db_logical_or_expr(valuep));