1*cd6b1c8fSad /* $NetBSD: db_expr.c,v 1.16 2009/03/07 22:02:17 ad Exp $ */
2cf92afd6Scgd
361f28255Scgd /*
461f28255Scgd * Mach Operating System
561f28255Scgd * Copyright (c) 1991,1990 Carnegie Mellon University
661f28255Scgd * All Rights Reserved.
761f28255Scgd *
861f28255Scgd * Permission to use, copy, modify and distribute this software and its
961f28255Scgd * documentation is hereby granted, provided that both the copyright
1061f28255Scgd * notice and this permission notice appear in all copies of the
1161f28255Scgd * software, derivative works or modified versions, and any portions
1261f28255Scgd * thereof, and that both notices appear in supporting documentation.
1361f28255Scgd *
14b13e5d14Spk * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
1561f28255Scgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
1661f28255Scgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1761f28255Scgd *
1861f28255Scgd * Carnegie Mellon requests users of this software to return to
1961f28255Scgd *
2061f28255Scgd * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
2161f28255Scgd * School of Computer Science
2261f28255Scgd * Carnegie Mellon University
2361f28255Scgd * Pittsburgh PA 15213-3890
2461f28255Scgd *
2561f28255Scgd * any improvements or extensions that they make and grant Carnegie the
2661f28255Scgd * rights to redistribute these changes.
2737cabe30Scgd *
2861f28255Scgd * Author: David B. Golub, Carnegie Mellon University
2961f28255Scgd * Date: 7/90
3061f28255Scgd */
31f1a5c330Smycroft
321ac69d9cSlukem #include <sys/cdefs.h>
33*cd6b1c8fSad __KERNEL_RCSID(0, "$NetBSD: db_expr.c,v 1.16 2009/03/07 22:02:17 ad Exp $");
341ac69d9cSlukem
35f1a5c330Smycroft #include <sys/param.h>
36f1a5c330Smycroft #include <sys/proc.h>
37f1a5c330Smycroft
38*cd6b1c8fSad #include <ddb/ddb.h>
3961f28255Scgd
40712239e3Sthorpej static bool db_term(db_expr_t *);
41712239e3Sthorpej static bool db_unary(db_expr_t *);
42712239e3Sthorpej static bool db_mult_expr(db_expr_t *);
43712239e3Sthorpej static bool db_add_expr(db_expr_t *);
44712239e3Sthorpej static bool db_shift_expr(db_expr_t *);
454eaa4d66Ssimonb
46712239e3Sthorpej static bool
db_term(db_expr_t * valuep)474eaa4d66Ssimonb db_term(db_expr_t *valuep)
4861f28255Scgd {
4961f28255Scgd int t;
5061f28255Scgd
5161f28255Scgd t = db_read_token();
5261f28255Scgd if (t == tIDENT) {
5361f28255Scgd if (!db_value_of_name(db_tok_string, valuep)) {
5474e5e4f0Seeh db_expr_t v = 0;
5574e5e4f0Seeh int i, c, byte;
5674e5e4f0Seeh
5774e5e4f0Seeh /* See if we can make a number out of all of it */
5839ff4fafSmycroft for (i = 0; (c = db_tok_string[i]) != '\0'; i++) {
5974e5e4f0Seeh byte = 0;
6074e5e4f0Seeh if (c >= '0' && c <= '9')
6174e5e4f0Seeh byte = c - '0';
6274e5e4f0Seeh else if (db_radix == 16 && c >= 'a' && c <= 'f')
6374e5e4f0Seeh byte = c - 'a' + 10;
6474e5e4f0Seeh else if (db_radix == 16 && c >= 'A' && c <= 'F')
6574e5e4f0Seeh byte = c - 'A' + 10;
6674e5e4f0Seeh else
6761f28255Scgd db_error("Symbol not found\n");
6861f28255Scgd /*NOTREACHED*/
6974e5e4f0Seeh v = v * db_radix + byte;
7074e5e4f0Seeh }
7174e5e4f0Seeh *valuep = (db_expr_t)v;
7261f28255Scgd }
734f3d5a9cSthorpej return (true);
7461f28255Scgd }
7561f28255Scgd if (t == tNUMBER) {
7661f28255Scgd *valuep = (db_expr_t)db_tok_number;
774f3d5a9cSthorpej return (true);
7861f28255Scgd }
7961f28255Scgd if (t == tDOT) {
8061f28255Scgd *valuep = (db_expr_t)db_dot;
814f3d5a9cSthorpej return (true);
8261f28255Scgd }
8361f28255Scgd if (t == tDOTDOT) {
8461f28255Scgd *valuep = (db_expr_t)db_prev;
854f3d5a9cSthorpej return (true);
8661f28255Scgd }
8761f28255Scgd if (t == tPLUS) {
8861f28255Scgd *valuep = (db_expr_t) db_next;
894f3d5a9cSthorpej return (true);
9061f28255Scgd }
9161f28255Scgd if (t == tDITTO) {
9261f28255Scgd *valuep = (db_expr_t)db_last_addr;
934f3d5a9cSthorpej return (true);
9461f28255Scgd }
9561f28255Scgd if (t == tDOLLAR) {
9661f28255Scgd if (!db_get_variable(valuep))
974f3d5a9cSthorpej return (false);
984f3d5a9cSthorpej return (true);
9961f28255Scgd }
10061f28255Scgd if (t == tLPAREN) {
10161f28255Scgd if (!db_expression(valuep)) {
10261f28255Scgd db_error("Syntax error\n");
10361f28255Scgd /*NOTREACHED*/
10461f28255Scgd }
10561f28255Scgd t = db_read_token();
10661f28255Scgd if (t != tRPAREN) {
10761f28255Scgd db_error("Syntax error\n");
10861f28255Scgd /*NOTREACHED*/
10961f28255Scgd }
1104f3d5a9cSthorpej return (true);
11161f28255Scgd }
11261f28255Scgd db_unread_token(t);
1134f3d5a9cSthorpej return (false);
11461f28255Scgd }
11561f28255Scgd
116712239e3Sthorpej static bool
db_unary(db_expr_t * valuep)1174eaa4d66Ssimonb db_unary(db_expr_t *valuep)
11861f28255Scgd {
11961f28255Scgd int t;
12061f28255Scgd
12161f28255Scgd t = db_read_token();
12261f28255Scgd if (t == tMINUS) {
12361f28255Scgd if (!db_unary(valuep)) {
12461f28255Scgd db_error("Syntax error\n");
12561f28255Scgd /*NOTREACHED*/
12661f28255Scgd }
12761f28255Scgd *valuep = -*valuep;
1284f3d5a9cSthorpej return (true);
12961f28255Scgd }
13061f28255Scgd if (t == tSTAR) {
13161f28255Scgd /* indirection */
13261f28255Scgd if (!db_unary(valuep)) {
13361f28255Scgd db_error("Syntax error\n");
13461f28255Scgd /*NOTREACHED*/
13561f28255Scgd }
13684c0797eSthorpej *valuep = db_get_value((db_addr_t)*valuep, sizeof(db_expr_t),
1374f3d5a9cSthorpej false);
1384f3d5a9cSthorpej return (true);
13961f28255Scgd }
14061f28255Scgd db_unread_token(t);
14161f28255Scgd return (db_term(valuep));
14261f28255Scgd }
14361f28255Scgd
144712239e3Sthorpej static bool
db_mult_expr(db_expr_t * valuep)1454eaa4d66Ssimonb db_mult_expr(db_expr_t *valuep)
14661f28255Scgd {
14761f28255Scgd db_expr_t lhs, rhs;
14861f28255Scgd int t;
14961f28255Scgd
15061f28255Scgd if (!db_unary(&lhs))
1514f3d5a9cSthorpej return (false);
15261f28255Scgd
15361f28255Scgd t = db_read_token();
15461f28255Scgd while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) {
15561f28255Scgd if (!db_term(&rhs)) {
15661f28255Scgd db_error("Syntax error\n");
15761f28255Scgd /*NOTREACHED*/
15861f28255Scgd }
15961f28255Scgd if (t == tSTAR)
16061f28255Scgd lhs *= rhs;
16161f28255Scgd else {
16261f28255Scgd if (rhs == 0) {
16361f28255Scgd db_error("Divide by 0\n");
16461f28255Scgd /*NOTREACHED*/
16561f28255Scgd }
16661f28255Scgd if (t == tSLASH)
16761f28255Scgd lhs /= rhs;
16861f28255Scgd else if (t == tPCT)
16961f28255Scgd lhs %= rhs;
17061f28255Scgd else
17161f28255Scgd lhs = ((lhs+rhs-1)/rhs)*rhs;
17261f28255Scgd }
17361f28255Scgd t = db_read_token();
17461f28255Scgd }
17561f28255Scgd db_unread_token(t);
17661f28255Scgd *valuep = lhs;
1774f3d5a9cSthorpej return (true);
17861f28255Scgd }
17961f28255Scgd
180712239e3Sthorpej static bool
db_add_expr(db_expr_t * valuep)1814eaa4d66Ssimonb db_add_expr(db_expr_t *valuep)
18261f28255Scgd {
18361f28255Scgd db_expr_t lhs, rhs;
18461f28255Scgd int t;
18561f28255Scgd
18661f28255Scgd if (!db_mult_expr(&lhs))
1874f3d5a9cSthorpej return (false);
18861f28255Scgd
18961f28255Scgd t = db_read_token();
19061f28255Scgd while (t == tPLUS || t == tMINUS) {
19161f28255Scgd if (!db_mult_expr(&rhs)) {
19261f28255Scgd db_error("Syntax error\n");
19361f28255Scgd /*NOTREACHED*/
19461f28255Scgd }
19561f28255Scgd if (t == tPLUS)
19661f28255Scgd lhs += rhs;
19761f28255Scgd else
19861f28255Scgd lhs -= rhs;
19961f28255Scgd t = db_read_token();
20061f28255Scgd }
20161f28255Scgd db_unread_token(t);
20261f28255Scgd *valuep = lhs;
2034f3d5a9cSthorpej return (true);
20461f28255Scgd }
20561f28255Scgd
206712239e3Sthorpej static bool
db_shift_expr(db_expr_t * valuep)2074eaa4d66Ssimonb db_shift_expr(db_expr_t *valuep)
20861f28255Scgd {
20961f28255Scgd db_expr_t lhs, rhs;
21061f28255Scgd int t;
21161f28255Scgd
21261f28255Scgd if (!db_add_expr(&lhs))
2134f3d5a9cSthorpej return (false);
21461f28255Scgd
21561f28255Scgd t = db_read_token();
21661f28255Scgd while (t == tSHIFT_L || t == tSHIFT_R) {
21761f28255Scgd if (!db_add_expr(&rhs)) {
21861f28255Scgd db_error("Syntax error\n");
21961f28255Scgd /*NOTREACHED*/
22061f28255Scgd }
22161f28255Scgd if (rhs < 0) {
22261f28255Scgd db_error("Negative shift amount\n");
22361f28255Scgd /*NOTREACHED*/
22461f28255Scgd }
22561f28255Scgd if (t == tSHIFT_L)
22661f28255Scgd lhs <<= rhs;
22761f28255Scgd else {
22861f28255Scgd /* Shift right is unsigned */
22965a65d45Scgd lhs = (unsigned long) lhs >> rhs;
23061f28255Scgd }
23161f28255Scgd t = db_read_token();
23261f28255Scgd }
23361f28255Scgd db_unread_token(t);
23461f28255Scgd *valuep = lhs;
2354f3d5a9cSthorpej return (true);
23661f28255Scgd }
23761f28255Scgd
23861f28255Scgd int
db_expression(db_expr_t * valuep)2394eaa4d66Ssimonb db_expression(db_expr_t *valuep)
24061f28255Scgd {
2414eaa4d66Ssimonb
24261f28255Scgd return (db_shift_expr(valuep));
24361f28255Scgd }
244