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 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <ddb/db_lex.h>
39 #include <ddb/db_access.h>
40 #include <ddb/db_command.h>
42 static bool db_add_expr(db_expr_t *valuep);
43 static bool db_mult_expr(db_expr_t *valuep);
44 static bool db_shift_expr(db_expr_t *valuep);
45 static bool db_term(db_expr_t *valuep);
46 static bool db_unary(db_expr_t *valuep);
47 static bool db_logical_or_expr(db_expr_t *valuep);
48 static bool db_logical_and_expr(db_expr_t *valuep);
49 static bool db_logical_relation_expr(db_expr_t *valuep);
52 db_term(db_expr_t *valuep)
58 if (!db_value_of_name(db_tok_string, valuep) &&
59 !db_value_of_name_pcpu(db_tok_string, valuep) &&
60 !db_value_of_name_vnet(db_tok_string, valuep)) {
61 db_printf("Symbol '%s' not found\n", db_tok_string);
68 *valuep = (db_expr_t)db_tok_number;
72 *valuep = (db_expr_t)db_dot;
76 *valuep = (db_expr_t)db_prev;
80 *valuep = (db_expr_t) db_next;
84 *valuep = (db_expr_t)db_last_addr;
88 if (!db_get_variable(valuep))
93 if (!db_expression(valuep)) {
94 db_printf("Expression syntax error after '%c'\n", '(');
100 db_printf("Expression syntax error -- expected '%c'\n", ')');
111 db_unary(db_expr_t *valuep)
117 if (!db_unary(valuep)) {
118 db_printf("Expression syntax error after '%c'\n", '-');
126 if (!db_unary(valuep)) {
127 db_printf("Expression syntax error after '%c'\n", '!');
131 *valuep = (!(*valuep));
135 if (!db_unary(valuep)) {
136 db_printf("Expression syntax error after '%c'\n", '~');
140 *valuep = (~(*valuep));
145 if (!db_unary(valuep)) {
146 db_printf("Expression syntax error after '%c'\n", '*');
150 *valuep = db_get_value((db_addr_t)*valuep, sizeof(void *),
155 return (db_term(valuep));
159 db_mult_expr(db_expr_t *valuep)
168 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH ||
170 if (!db_term(&rhs)) {
171 db_printf("Expression syntax error after '%c'\n",
172 t == tSTAR ? '*' : t == tSLASH ? '/' : t == tPCT ? '%' :
173 t == tHASH ? '#' : '&');
186 db_error("Division by 0\n");
194 lhs = roundup(lhs, rhs);
204 db_add_expr(db_expr_t *valuep)
209 if (!db_mult_expr(&lhs))
213 while (t == tPLUS || t == tMINUS || t == tBIT_OR) {
214 if (!db_mult_expr(&rhs)) {
215 db_printf("Expression syntax error after '%c'\n",
216 t == tPLUS ? '+' : t == tMINUS ? '-' : '|');
231 __assert_unreachable();
241 db_shift_expr(db_expr_t *valuep)
246 if (!db_add_expr(&lhs))
249 while (t == tSHIFT_L || t == tSHIFT_R) {
250 if (!db_add_expr(&rhs)) {
251 db_printf("Expression syntax error after '%s'\n",
252 t == tSHIFT_L ? "<<" : ">>");
257 db_printf("Negative shift amount %jd\n", (intmax_t)rhs);
264 /* Shift right is unsigned */
265 lhs = (db_addr_t)lhs >> rhs;
275 db_logical_relation_expr(
281 if (!db_shift_expr(&lhs))
285 while (t == tLOG_EQ || t == tLOG_NOT_EQ || t == tGREATER ||
286 t == tGREATER_EQ || t == tLESS || t == tLESS_EQ) {
287 if (!db_shift_expr(&rhs)) {
288 db_printf("Expression syntax error after '%s'\n",
289 t == tLOG_EQ ? "==" : t == tLOG_NOT_EQ ? "!=" :
290 t == tGREATER ? ">" : t == tGREATER_EQ ? ">=" :
291 t == tLESS ? "<" : "<=");
315 __assert_unreachable();
331 if (!db_logical_relation_expr(&lhs))
335 while (t == tLOG_AND) {
336 if (!db_logical_relation_expr(&rhs)) {
337 db_printf("Expression syntax error after '%s'\n", "&&");
356 if (!db_logical_and_expr(&lhs))
360 while (t == tLOG_OR) {
361 if (!db_logical_and_expr(&rhs)) {
362 db_printf("Expression syntax error after '%s'\n", "||");
375 db_expression(db_expr_t *valuep)
377 return (db_logical_or_expr(valuep));