1 /* $OpenBSD: db_expr.c,v 1.10 2014/07/08 13:02:57 deraadt Exp $ */ 2 /* $NetBSD: db_expr.c,v 1.5 1996/02/05 01:56:58 christos Exp $ */ 3 4 /* 5 * Mach Operating System 6 * Copyright (c) 1993,1992,1991,1990 Carnegie Mellon University 7 * All Rights Reserved. 8 * 9 * Permission to use, copy, modify and distribute this software and its 10 * documentation is hereby granted, provided that both the copyright 11 * notice and this permission notice appear in all copies of the 12 * software, derivative works or modified versions, and any portions 13 * thereof, and that both notices appear in supporting documentation. 14 * 15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 17 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 18 * 19 * Carnegie Mellon requests users of this software to return to 20 * 21 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 22 * School of Computer Science 23 * Carnegie Mellon University 24 * Pittsburgh PA 15213-3890 25 * 26 * any improvements or extensions that they make and grant Carnegie Mellon 27 * the rights to redistribute these changes. 28 * 29 * Author: David B. Golub, Carnegie Mellon University 30 * Date: 7/90 31 */ 32 33 #include <sys/param.h> 34 #include <sys/proc.h> 35 36 #include <machine/db_machdep.h> 37 38 #include <ddb/db_lex.h> 39 #include <ddb/db_access.h> 40 #include <ddb/db_command.h> 41 #include <ddb/db_sym.h> 42 #include <ddb/db_extern.h> 43 #include <ddb/db_variables.h> 44 45 boolean_t 46 db_term(db_expr_t *valuep) 47 { 48 int t; 49 50 t = db_read_token(); 51 if (t == tIDENT) { 52 if (!db_value_of_name(db_tok_string, valuep)) { 53 db_error("Symbol not found\n"); 54 /*NOTREACHED*/ 55 } 56 return (TRUE); 57 } 58 if (t == tNUMBER) { 59 *valuep = db_tok_number; 60 return (TRUE); 61 } 62 if (t == tDOT) { 63 *valuep = (db_expr_t)db_dot; 64 return (TRUE); 65 } 66 if (t == tDOTDOT) { 67 *valuep = (db_expr_t)db_prev; 68 return (TRUE); 69 } 70 if (t == tPLUS) { 71 *valuep = (db_expr_t) db_next; 72 return (TRUE); 73 } 74 if (t == tDITTO) { 75 *valuep = (db_expr_t)db_last_addr; 76 return (TRUE); 77 } 78 if (t == tDOLLAR) { 79 if (!db_get_variable(valuep)) 80 return (FALSE); 81 return (TRUE); 82 } 83 if (t == tLPAREN) { 84 if (!db_expression(valuep)) { 85 db_error("Syntax error\n"); 86 /*NOTREACHED*/ 87 } 88 t = db_read_token(); 89 if (t != tRPAREN) { 90 db_error("Syntax error\n"); 91 /*NOTREACHED*/ 92 } 93 return (TRUE); 94 } 95 db_unread_token(t); 96 return (FALSE); 97 } 98 99 boolean_t 100 db_unary(db_expr_t *valuep) 101 { 102 int t; 103 104 t = db_read_token(); 105 if (t == tMINUS) { 106 if (!db_unary(valuep)) { 107 db_error("Syntax error\n"); 108 /*NOTREACHED*/ 109 } 110 *valuep = -*valuep; 111 return (TRUE); 112 } 113 if (t == tSTAR) { 114 /* indirection */ 115 if (!db_unary(valuep)) { 116 db_error("Syntax error\n"); 117 /*NOTREACHED*/ 118 } 119 *valuep = db_get_value((db_addr_t)*valuep, sizeof(db_addr_t), FALSE); 120 return (TRUE); 121 } 122 db_unread_token(t); 123 return (db_term(valuep)); 124 } 125 126 boolean_t 127 db_mult_expr(db_expr_t *valuep) 128 { 129 db_expr_t lhs, rhs; 130 int t; 131 132 if (!db_unary(&lhs)) 133 return (FALSE); 134 135 t = db_read_token(); 136 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) { 137 if (!db_term(&rhs)) { 138 db_error("Syntax error\n"); 139 /*NOTREACHED*/ 140 } 141 if (t == tSTAR) 142 lhs *= rhs; 143 else { 144 if (rhs == 0) { 145 db_error("Divide by 0\n"); 146 /*NOTREACHED*/ 147 } 148 if (t == tSLASH) 149 lhs /= rhs; 150 else if (t == tPCT) 151 lhs %= rhs; 152 else 153 lhs = ((lhs+rhs-1)/rhs)*rhs; 154 } 155 t = db_read_token(); 156 } 157 db_unread_token(t); 158 *valuep = lhs; 159 return (TRUE); 160 } 161 162 boolean_t 163 db_add_expr(db_expr_t *valuep) 164 { 165 db_expr_t lhs, rhs; 166 int t; 167 168 if (!db_mult_expr(&lhs)) 169 return (FALSE); 170 171 t = db_read_token(); 172 while (t == tPLUS || t == tMINUS) { 173 if (!db_mult_expr(&rhs)) { 174 db_error("Syntax error\n"); 175 /*NOTREACHED*/ 176 } 177 if (t == tPLUS) 178 lhs += rhs; 179 else 180 lhs -= rhs; 181 t = db_read_token(); 182 } 183 db_unread_token(t); 184 *valuep = lhs; 185 return (TRUE); 186 } 187 188 boolean_t 189 db_shift_expr(db_expr_t *valuep) 190 { 191 db_expr_t lhs, rhs; 192 int t; 193 194 if (!db_add_expr(&lhs)) 195 return (FALSE); 196 197 t = db_read_token(); 198 while (t == tSHIFT_L || t == tSHIFT_R) { 199 if (!db_add_expr(&rhs)) { 200 db_error("Syntax error\n"); 201 /*NOTREACHED*/ 202 } 203 if (rhs < 0) { 204 db_error("Negative shift amount\n"); 205 /*NOTREACHED*/ 206 } 207 if (t == tSHIFT_L) 208 lhs <<= rhs; 209 else { 210 /* Shift right is unsigned */ 211 lhs = (unsigned) lhs >> rhs; 212 } 213 t = db_read_token(); 214 } 215 db_unread_token(t); 216 *valuep = lhs; 217 return (TRUE); 218 } 219 220 int 221 db_expression(db_expr_t *valuep) 222 { 223 return (db_shift_expr(valuep)); 224 } 225