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