1 /* $NetBSD: db_expr.c,v 1.13 2002/02/15 07:33:50 simonb Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 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. 13 * 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. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 * 28 * Author: David B. Golub, Carnegie Mellon University 29 * Date: 7/90 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: db_expr.c,v 1.13 2002/02/15 07:33:50 simonb Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/proc.h> 37 38 #include <machine/db_machdep.h> 39 40 #include <ddb/db_access.h> 41 #include <ddb/db_command.h> 42 #include <ddb/db_extern.h> 43 #include <ddb/db_lex.h> 44 #include <ddb/db_output.h> 45 #include <ddb/db_sym.h> 46 #include <ddb/db_variables.h> 47 48 static boolean_t db_term(db_expr_t *); 49 static boolean_t db_unary(db_expr_t *); 50 static boolean_t db_mult_expr(db_expr_t *); 51 static boolean_t db_add_expr(db_expr_t *); 52 static boolean_t db_shift_expr(db_expr_t *); 53 54 static boolean_t 55 db_term(db_expr_t *valuep) 56 { 57 int t; 58 59 t = db_read_token(); 60 if (t == tIDENT) { 61 if (!db_value_of_name(db_tok_string, valuep)) { 62 db_expr_t v = 0; 63 int i, c, byte; 64 65 /* See if we can make a number out of all of it */ 66 for (i = 0; (c = db_tok_string[i]) != '\0'; i++) { 67 byte = 0; 68 if (c >= '0' && c <= '9') 69 byte = c - '0'; 70 else if (db_radix == 16 && c >= 'a' && c <= 'f') 71 byte = c - 'a' + 10; 72 else if (db_radix == 16 && c >= 'A' && c <= 'F') 73 byte = c - 'A' + 10; 74 else 75 db_error("Symbol not found\n"); 76 /*NOTREACHED*/ 77 v = v * db_radix + byte; 78 } 79 *valuep = (db_expr_t)v; 80 } 81 return (TRUE); 82 } 83 if (t == tNUMBER) { 84 *valuep = (db_expr_t)db_tok_number; 85 return (TRUE); 86 } 87 if (t == tDOT) { 88 *valuep = (db_expr_t)db_dot; 89 return (TRUE); 90 } 91 if (t == tDOTDOT) { 92 *valuep = (db_expr_t)db_prev; 93 return (TRUE); 94 } 95 if (t == tPLUS) { 96 *valuep = (db_expr_t) db_next; 97 return (TRUE); 98 } 99 if (t == tDITTO) { 100 *valuep = (db_expr_t)db_last_addr; 101 return (TRUE); 102 } 103 if (t == tDOLLAR) { 104 if (!db_get_variable(valuep)) 105 return (FALSE); 106 return (TRUE); 107 } 108 if (t == tLPAREN) { 109 if (!db_expression(valuep)) { 110 db_error("Syntax error\n"); 111 /*NOTREACHED*/ 112 } 113 t = db_read_token(); 114 if (t != tRPAREN) { 115 db_error("Syntax error\n"); 116 /*NOTREACHED*/ 117 } 118 return (TRUE); 119 } 120 db_unread_token(t); 121 return (FALSE); 122 } 123 124 static boolean_t 125 db_unary(db_expr_t *valuep) 126 { 127 int t; 128 129 t = db_read_token(); 130 if (t == tMINUS) { 131 if (!db_unary(valuep)) { 132 db_error("Syntax error\n"); 133 /*NOTREACHED*/ 134 } 135 *valuep = -*valuep; 136 return (TRUE); 137 } 138 if (t == tSTAR) { 139 /* indirection */ 140 if (!db_unary(valuep)) { 141 db_error("Syntax error\n"); 142 /*NOTREACHED*/ 143 } 144 *valuep = db_get_value((db_addr_t)*valuep, sizeof(db_expr_t), 145 FALSE); 146 return (TRUE); 147 } 148 db_unread_token(t); 149 return (db_term(valuep)); 150 } 151 152 static boolean_t 153 db_mult_expr(db_expr_t *valuep) 154 { 155 db_expr_t lhs, rhs; 156 int t; 157 158 if (!db_unary(&lhs)) 159 return (FALSE); 160 161 t = db_read_token(); 162 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) { 163 if (!db_term(&rhs)) { 164 db_error("Syntax error\n"); 165 /*NOTREACHED*/ 166 } 167 if (t == tSTAR) 168 lhs *= rhs; 169 else { 170 if (rhs == 0) { 171 db_error("Divide by 0\n"); 172 /*NOTREACHED*/ 173 } 174 if (t == tSLASH) 175 lhs /= rhs; 176 else if (t == tPCT) 177 lhs %= rhs; 178 else 179 lhs = ((lhs+rhs-1)/rhs)*rhs; 180 } 181 t = db_read_token(); 182 } 183 db_unread_token(t); 184 *valuep = lhs; 185 return (TRUE); 186 } 187 188 static boolean_t 189 db_add_expr(db_expr_t *valuep) 190 { 191 db_expr_t lhs, rhs; 192 int t; 193 194 if (!db_mult_expr(&lhs)) 195 return (FALSE); 196 197 t = db_read_token(); 198 while (t == tPLUS || t == tMINUS) { 199 if (!db_mult_expr(&rhs)) { 200 db_error("Syntax error\n"); 201 /*NOTREACHED*/ 202 } 203 if (t == tPLUS) 204 lhs += rhs; 205 else 206 lhs -= rhs; 207 t = db_read_token(); 208 } 209 db_unread_token(t); 210 *valuep = lhs; 211 return (TRUE); 212 } 213 214 static boolean_t 215 db_shift_expr(db_expr_t *valuep) 216 { 217 db_expr_t lhs, rhs; 218 int t; 219 220 if (!db_add_expr(&lhs)) 221 return (FALSE); 222 223 t = db_read_token(); 224 while (t == tSHIFT_L || t == tSHIFT_R) { 225 if (!db_add_expr(&rhs)) { 226 db_error("Syntax error\n"); 227 /*NOTREACHED*/ 228 } 229 if (rhs < 0) { 230 db_error("Negative shift amount\n"); 231 /*NOTREACHED*/ 232 } 233 if (t == tSHIFT_L) 234 lhs <<= rhs; 235 else { 236 /* Shift right is unsigned */ 237 lhs = (unsigned long) lhs >> rhs; 238 } 239 t = db_read_token(); 240 } 241 db_unread_token(t); 242 *valuep = lhs; 243 return (TRUE); 244 } 245 246 int 247 db_expression(db_expr_t *valuep) 248 { 249 250 return (db_shift_expr(valuep)); 251 } 252