1 /* $OpenBSD: db_expr.c,v 1.11 2014/09/14 14:17:24 jsg 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 35 #include <machine/db_machdep.h> 36 37 #include <ddb/db_lex.h> 38 #include <ddb/db_access.h> 39 #include <ddb/db_command.h> 40 #include <ddb/db_sym.h> 41 #include <ddb/db_extern.h> 42 #include <ddb/db_variables.h> 43 44 boolean_t 45 db_term(db_expr_t *valuep) 46 { 47 int t; 48 49 t = db_read_token(); 50 if (t == tIDENT) { 51 if (!db_value_of_name(db_tok_string, valuep)) { 52 db_error("Symbol not found\n"); 53 /*NOTREACHED*/ 54 } 55 return (TRUE); 56 } 57 if (t == tNUMBER) { 58 *valuep = db_tok_number; 59 return (TRUE); 60 } 61 if (t == tDOT) { 62 *valuep = (db_expr_t)db_dot; 63 return (TRUE); 64 } 65 if (t == tDOTDOT) { 66 *valuep = (db_expr_t)db_prev; 67 return (TRUE); 68 } 69 if (t == tPLUS) { 70 *valuep = (db_expr_t) db_next; 71 return (TRUE); 72 } 73 if (t == tDITTO) { 74 *valuep = (db_expr_t)db_last_addr; 75 return (TRUE); 76 } 77 if (t == tDOLLAR) { 78 if (!db_get_variable(valuep)) 79 return (FALSE); 80 return (TRUE); 81 } 82 if (t == tLPAREN) { 83 if (!db_expression(valuep)) { 84 db_error("Syntax error\n"); 85 /*NOTREACHED*/ 86 } 87 t = db_read_token(); 88 if (t != tRPAREN) { 89 db_error("Syntax error\n"); 90 /*NOTREACHED*/ 91 } 92 return (TRUE); 93 } 94 db_unread_token(t); 95 return (FALSE); 96 } 97 98 boolean_t 99 db_unary(db_expr_t *valuep) 100 { 101 int t; 102 103 t = db_read_token(); 104 if (t == tMINUS) { 105 if (!db_unary(valuep)) { 106 db_error("Syntax error\n"); 107 /*NOTREACHED*/ 108 } 109 *valuep = -*valuep; 110 return (TRUE); 111 } 112 if (t == tSTAR) { 113 /* indirection */ 114 if (!db_unary(valuep)) { 115 db_error("Syntax error\n"); 116 /*NOTREACHED*/ 117 } 118 *valuep = db_get_value((db_addr_t)*valuep, sizeof(db_addr_t), FALSE); 119 return (TRUE); 120 } 121 db_unread_token(t); 122 return (db_term(valuep)); 123 } 124 125 boolean_t 126 db_mult_expr(db_expr_t *valuep) 127 { 128 db_expr_t lhs, rhs; 129 int t; 130 131 if (!db_unary(&lhs)) 132 return (FALSE); 133 134 t = db_read_token(); 135 while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) { 136 if (!db_term(&rhs)) { 137 db_error("Syntax error\n"); 138 /*NOTREACHED*/ 139 } 140 if (t == tSTAR) 141 lhs *= rhs; 142 else { 143 if (rhs == 0) { 144 db_error("Divide by 0\n"); 145 /*NOTREACHED*/ 146 } 147 if (t == tSLASH) 148 lhs /= rhs; 149 else if (t == tPCT) 150 lhs %= rhs; 151 else 152 lhs = ((lhs+rhs-1)/rhs)*rhs; 153 } 154 t = db_read_token(); 155 } 156 db_unread_token(t); 157 *valuep = lhs; 158 return (TRUE); 159 } 160 161 boolean_t 162 db_add_expr(db_expr_t *valuep) 163 { 164 db_expr_t lhs, rhs; 165 int t; 166 167 if (!db_mult_expr(&lhs)) 168 return (FALSE); 169 170 t = db_read_token(); 171 while (t == tPLUS || t == tMINUS) { 172 if (!db_mult_expr(&rhs)) { 173 db_error("Syntax error\n"); 174 /*NOTREACHED*/ 175 } 176 if (t == tPLUS) 177 lhs += rhs; 178 else 179 lhs -= rhs; 180 t = db_read_token(); 181 } 182 db_unread_token(t); 183 *valuep = lhs; 184 return (TRUE); 185 } 186 187 boolean_t 188 db_shift_expr(db_expr_t *valuep) 189 { 190 db_expr_t lhs, rhs; 191 int t; 192 193 if (!db_add_expr(&lhs)) 194 return (FALSE); 195 196 t = db_read_token(); 197 while (t == tSHIFT_L || t == tSHIFT_R) { 198 if (!db_add_expr(&rhs)) { 199 db_error("Syntax error\n"); 200 /*NOTREACHED*/ 201 } 202 if (rhs < 0) { 203 db_error("Negative shift amount\n"); 204 /*NOTREACHED*/ 205 } 206 if (t == tSHIFT_L) 207 lhs <<= rhs; 208 else { 209 /* Shift right is unsigned */ 210 lhs = (unsigned) lhs >> rhs; 211 } 212 t = db_read_token(); 213 } 214 db_unread_token(t); 215 *valuep = lhs; 216 return (TRUE); 217 } 218 219 int 220 db_expression(db_expr_t *valuep) 221 { 222 return (db_shift_expr(valuep)); 223 } 224