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