xref: /freebsd/sys/ddb/db_expr.c (revision fc891c1907eb4123f5f2574eb15e4cce22537c79)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  *	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
30753960f7SDavid E. O'Brien 
31753960f7SDavid E. O'Brien #include <sys/cdefs.h>
32753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
33753960f7SDavid E. O'Brien 
34f540b106SGarrett Wollman #include <sys/param.h>
355ccbc3ccSBruce Evans 
36f540b106SGarrett Wollman #include <ddb/ddb.h>
375b81b6b3SRodney W. Grimes #include <ddb/db_lex.h>
385b81b6b3SRodney W. Grimes #include <ddb/db_access.h>
395b81b6b3SRodney W. Grimes #include <ddb/db_command.h>
405b81b6b3SRodney W. Grimes 
41cd508278SPedro F. Giffuni static bool	db_add_expr(db_expr_t *valuep);
42cd508278SPedro F. Giffuni static bool	db_mult_expr(db_expr_t *valuep);
43cd508278SPedro F. Giffuni static bool	db_shift_expr(db_expr_t *valuep);
44cd508278SPedro F. Giffuni static bool	db_term(db_expr_t *valuep);
45cd508278SPedro F. Giffuni static bool	db_unary(db_expr_t *valuep);
46058284fcSBruce Evans 
47cd508278SPedro F. Giffuni static bool
4819b79af1SWarner Losh db_term(db_expr_t *valuep)
495b81b6b3SRodney W. Grimes {
505b81b6b3SRodney W. Grimes 	int	t;
515b81b6b3SRodney W. Grimes 
525b81b6b3SRodney W. Grimes 	t = db_read_token();
535b81b6b3SRodney W. Grimes 	if (t == tIDENT) {
54eddfbb76SRobert Watson 	    if (!db_value_of_name(db_tok_string, valuep) &&
55eddfbb76SRobert Watson 		!db_value_of_name_pcpu(db_tok_string, valuep) &&
56eddfbb76SRobert Watson 		!db_value_of_name_vnet(db_tok_string, valuep)) {
575b81b6b3SRodney W. Grimes 		db_error("Symbol not found\n");
585b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
595b81b6b3SRodney W. Grimes 	    }
602b490bc7SPedro F. Giffuni 	    return (true);
615b81b6b3SRodney W. Grimes 	}
625b81b6b3SRodney W. Grimes 	if (t == tNUMBER) {
635b81b6b3SRodney W. Grimes 	    *valuep = (db_expr_t)db_tok_number;
642b490bc7SPedro F. Giffuni 	    return (true);
655b81b6b3SRodney W. Grimes 	}
665b81b6b3SRodney W. Grimes 	if (t == tDOT) {
675b81b6b3SRodney W. Grimes 	    *valuep = (db_expr_t)db_dot;
682b490bc7SPedro F. Giffuni 	    return (true);
695b81b6b3SRodney W. Grimes 	}
705b81b6b3SRodney W. Grimes 	if (t == tDOTDOT) {
715b81b6b3SRodney W. Grimes 	    *valuep = (db_expr_t)db_prev;
722b490bc7SPedro F. Giffuni 	    return (true);
735b81b6b3SRodney W. Grimes 	}
745b81b6b3SRodney W. Grimes 	if (t == tPLUS) {
755b81b6b3SRodney W. Grimes 	    *valuep = (db_expr_t) db_next;
762b490bc7SPedro F. Giffuni 	    return (true);
775b81b6b3SRodney W. Grimes 	}
785b81b6b3SRodney W. Grimes 	if (t == tDITTO) {
795b81b6b3SRodney W. Grimes 	    *valuep = (db_expr_t)db_last_addr;
802b490bc7SPedro F. Giffuni 	    return (true);
815b81b6b3SRodney W. Grimes 	}
825b81b6b3SRodney W. Grimes 	if (t == tDOLLAR) {
835b81b6b3SRodney W. Grimes 	    if (!db_get_variable(valuep))
842b490bc7SPedro F. Giffuni 		return (false);
852b490bc7SPedro F. Giffuni 	    return (true);
865b81b6b3SRodney W. Grimes 	}
875b81b6b3SRodney W. Grimes 	if (t == tLPAREN) {
885b81b6b3SRodney W. Grimes 	    if (!db_expression(valuep)) {
895b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
905b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
915b81b6b3SRodney W. Grimes 	    }
925b81b6b3SRodney W. Grimes 	    t = db_read_token();
935b81b6b3SRodney W. Grimes 	    if (t != tRPAREN) {
945b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
955b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
965b81b6b3SRodney W. Grimes 	    }
972b490bc7SPedro F. Giffuni 	    return (true);
985b81b6b3SRodney W. Grimes 	}
995b81b6b3SRodney W. Grimes 	db_unread_token(t);
1002b490bc7SPedro F. Giffuni 	return (false);
1015b81b6b3SRodney W. Grimes }
1025b81b6b3SRodney W. Grimes 
103cd508278SPedro F. Giffuni static bool
10419b79af1SWarner Losh db_unary(db_expr_t *valuep)
1055b81b6b3SRodney W. Grimes {
1065b81b6b3SRodney W. Grimes 	int	t;
1075b81b6b3SRodney W. Grimes 
1085b81b6b3SRodney W. Grimes 	t = db_read_token();
1095b81b6b3SRodney W. Grimes 	if (t == tMINUS) {
1105b81b6b3SRodney W. Grimes 	    if (!db_unary(valuep)) {
1115b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
1125b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
1135b81b6b3SRodney W. Grimes 	    }
1145b81b6b3SRodney W. Grimes 	    *valuep = -*valuep;
1152b490bc7SPedro F. Giffuni 	    return (true);
1165b81b6b3SRodney W. Grimes 	}
1175b81b6b3SRodney W. Grimes 	if (t == tSTAR) {
1185b81b6b3SRodney W. Grimes 	    /* indirection */
1195b81b6b3SRodney W. Grimes 	    if (!db_unary(valuep)) {
1205b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
1215b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
1225b81b6b3SRodney W. Grimes 	    }
1232b490bc7SPedro F. Giffuni 	    *valuep = db_get_value((db_addr_t)*valuep, sizeof(void *), false);
1242b490bc7SPedro F. Giffuni 	    return (true);
1255b81b6b3SRodney W. Grimes 	}
1265b81b6b3SRodney W. Grimes 	db_unread_token(t);
1275b81b6b3SRodney W. Grimes 	return (db_term(valuep));
1285b81b6b3SRodney W. Grimes }
1295b81b6b3SRodney W. Grimes 
130cd508278SPedro F. Giffuni static bool
13119b79af1SWarner Losh db_mult_expr(db_expr_t *valuep)
1325b81b6b3SRodney W. Grimes {
1335b81b6b3SRodney W. Grimes 	db_expr_t	lhs, rhs;
1345b81b6b3SRodney W. Grimes 	int		t;
1355b81b6b3SRodney W. Grimes 
1365b81b6b3SRodney W. Grimes 	if (!db_unary(&lhs))
1372b490bc7SPedro F. Giffuni 	    return (false);
1385b81b6b3SRodney W. Grimes 
1395b81b6b3SRodney W. Grimes 	t = db_read_token();
1405b81b6b3SRodney W. Grimes 	while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) {
1415b81b6b3SRodney W. Grimes 	    if (!db_term(&rhs)) {
1425b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
1435b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
1445b81b6b3SRodney W. Grimes 	    }
1455b81b6b3SRodney W. Grimes 	    if (t == tSTAR)
1465b81b6b3SRodney W. Grimes 		lhs *= rhs;
1475b81b6b3SRodney W. Grimes 	    else {
1485b81b6b3SRodney W. Grimes 		if (rhs == 0) {
1495b81b6b3SRodney W. Grimes 		    db_error("Divide by 0\n");
1505b81b6b3SRodney W. Grimes 		    /*NOTREACHED*/
1515b81b6b3SRodney W. Grimes 		}
1525b81b6b3SRodney W. Grimes 		if (t == tSLASH)
1535b81b6b3SRodney W. Grimes 		    lhs /= rhs;
1545b81b6b3SRodney W. Grimes 		else if (t == tPCT)
1555b81b6b3SRodney W. Grimes 		    lhs %= rhs;
1565b81b6b3SRodney W. Grimes 		else
157*fc891c19SPedro F. Giffuni 		    lhs = roundup(lhs, rhs);
1585b81b6b3SRodney W. Grimes 	    }
1595b81b6b3SRodney W. Grimes 	    t = db_read_token();
1605b81b6b3SRodney W. Grimes 	}
1615b81b6b3SRodney W. Grimes 	db_unread_token(t);
1625b81b6b3SRodney W. Grimes 	*valuep = lhs;
1632b490bc7SPedro F. Giffuni 	return (true);
1645b81b6b3SRodney W. Grimes }
1655b81b6b3SRodney W. Grimes 
166cd508278SPedro F. Giffuni static bool
16719b79af1SWarner Losh db_add_expr(db_expr_t *valuep)
1685b81b6b3SRodney W. Grimes {
1695b81b6b3SRodney W. Grimes 	db_expr_t	lhs, rhs;
1705b81b6b3SRodney W. Grimes 	int		t;
1715b81b6b3SRodney W. Grimes 
1725b81b6b3SRodney W. Grimes 	if (!db_mult_expr(&lhs))
1732b490bc7SPedro F. Giffuni 	    return (false);
1745b81b6b3SRodney W. Grimes 
1755b81b6b3SRodney W. Grimes 	t = db_read_token();
1765b81b6b3SRodney W. Grimes 	while (t == tPLUS || t == tMINUS) {
1775b81b6b3SRodney W. Grimes 	    if (!db_mult_expr(&rhs)) {
1785b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
1795b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
1805b81b6b3SRodney W. Grimes 	    }
1815b81b6b3SRodney W. Grimes 	    if (t == tPLUS)
1825b81b6b3SRodney W. Grimes 		lhs += rhs;
1835b81b6b3SRodney W. Grimes 	    else
1845b81b6b3SRodney W. Grimes 		lhs -= rhs;
1855b81b6b3SRodney W. Grimes 	    t = db_read_token();
1865b81b6b3SRodney W. Grimes 	}
1875b81b6b3SRodney W. Grimes 	db_unread_token(t);
1885b81b6b3SRodney W. Grimes 	*valuep = lhs;
1892b490bc7SPedro F. Giffuni 	return (true);
1905b81b6b3SRodney W. Grimes }
1915b81b6b3SRodney W. Grimes 
192cd508278SPedro F. Giffuni static bool
19319b79af1SWarner Losh db_shift_expr(db_expr_t *valuep)
1945b81b6b3SRodney W. Grimes {
1955b81b6b3SRodney W. Grimes 	db_expr_t	lhs, rhs;
1965b81b6b3SRodney W. Grimes 	int		t;
1975b81b6b3SRodney W. Grimes 
1985b81b6b3SRodney W. Grimes 	if (!db_add_expr(&lhs))
1992b490bc7SPedro F. Giffuni 	    return (false);
2005b81b6b3SRodney W. Grimes 
2015b81b6b3SRodney W. Grimes 	t = db_read_token();
2025b81b6b3SRodney W. Grimes 	while (t == tSHIFT_L || t == tSHIFT_R) {
2035b81b6b3SRodney W. Grimes 	    if (!db_add_expr(&rhs)) {
2045b81b6b3SRodney W. Grimes 		db_error("Syntax error\n");
2055b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
2065b81b6b3SRodney W. Grimes 	    }
2075b81b6b3SRodney W. Grimes 	    if (rhs < 0) {
2085b81b6b3SRodney W. Grimes 		db_error("Negative shift amount\n");
2095b81b6b3SRodney W. Grimes 		/*NOTREACHED*/
2105b81b6b3SRodney W. Grimes 	    }
2115b81b6b3SRodney W. Grimes 	    if (t == tSHIFT_L)
2125b81b6b3SRodney W. Grimes 		lhs <<= rhs;
2135b81b6b3SRodney W. Grimes 	    else {
2145b81b6b3SRodney W. Grimes 		/* Shift right is unsigned */
2155b81b6b3SRodney W. Grimes 		lhs = (unsigned) lhs >> rhs;
2165b81b6b3SRodney W. Grimes 	    }
2175b81b6b3SRodney W. Grimes 	    t = db_read_token();
2185b81b6b3SRodney W. Grimes 	}
2195b81b6b3SRodney W. Grimes 	db_unread_token(t);
2205b81b6b3SRodney W. Grimes 	*valuep = lhs;
2212b490bc7SPedro F. Giffuni 	return (true);
2225b81b6b3SRodney W. Grimes }
2235b81b6b3SRodney W. Grimes 
2245b81b6b3SRodney W. Grimes int
22519b79af1SWarner Losh db_expression(db_expr_t *valuep)
2265b81b6b3SRodney W. Grimes {
2275b81b6b3SRodney W. Grimes 	return (db_shift_expr(valuep));
2285b81b6b3SRodney W. Grimes }
229