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