Home | History | Annotate | Line # | Download | only in ddb
db_expr.c revision 1.14
      1  1.14   thorpej /*	$NetBSD: db_expr.c,v 1.14 2007/02/21 22:59:56 thorpej 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.14   thorpej __KERNEL_RCSID(0, "$NetBSD: db_expr.c,v 1.14 2007/02/21 22:59:56 thorpej 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.1       cgd #include <machine/db_machdep.h>
     39   1.3   mycroft 
     40   1.1       cgd #include <ddb/db_access.h>
     41   1.1       cgd #include <ddb/db_command.h>
     42  1.13    simonb #include <ddb/db_extern.h>
     43  1.13    simonb #include <ddb/db_lex.h>
     44  1.13    simonb #include <ddb/db_output.h>
     45   1.5  christos #include <ddb/db_sym.h>
     46   1.5  christos #include <ddb/db_variables.h>
     47   1.1       cgd 
     48  1.14   thorpej static bool db_term(db_expr_t *);
     49  1.14   thorpej static bool db_unary(db_expr_t *);
     50  1.14   thorpej static bool db_mult_expr(db_expr_t *);
     51  1.14   thorpej static bool db_add_expr(db_expr_t *);
     52  1.14   thorpej static bool db_shift_expr(db_expr_t *);
     53  1.13    simonb 
     54  1.14   thorpej static bool
     55  1.13    simonb db_term(db_expr_t *valuep)
     56   1.1       cgd {
     57   1.1       cgd 	int	t;
     58   1.1       cgd 
     59   1.1       cgd 	t = db_read_token();
     60   1.1       cgd 	if (t == tIDENT) {
     61  1.13    simonb 		if (!db_value_of_name(db_tok_string, valuep)) {
     62  1.13    simonb 			db_expr_t v = 0;
     63  1.13    simonb 			int	i, c, byte;
     64  1.13    simonb 
     65  1.13    simonb 			/* See if we can make a number out of all of it */
     66  1.13    simonb 			for (i = 0; (c = db_tok_string[i]) != '\0'; i++) {
     67  1.13    simonb 				byte = 0;
     68  1.13    simonb 				if (c >= '0' && c <= '9')
     69  1.13    simonb 					byte = c - '0';
     70  1.13    simonb 				else if (db_radix == 16 && c >= 'a' && c <= 'f')
     71  1.13    simonb 					byte = c - 'a' + 10;
     72  1.13    simonb 				else if (db_radix == 16 && c >= 'A' && c <= 'F')
     73  1.13    simonb 					byte = c - 'A' + 10;
     74  1.13    simonb 				else
     75  1.13    simonb 					db_error("Symbol not found\n");
     76  1.13    simonb 					/*NOTREACHED*/
     77  1.13    simonb 				v = v * db_radix + byte;
     78  1.13    simonb 			}
     79  1.13    simonb 			*valuep = (db_expr_t)v;
     80  1.13    simonb 		}
     81  1.13    simonb 		return (TRUE);
     82   1.1       cgd 	}
     83   1.1       cgd 	if (t == tNUMBER) {
     84  1.13    simonb 		*valuep = (db_expr_t)db_tok_number;
     85  1.13    simonb 		return (TRUE);
     86   1.1       cgd 	}
     87   1.1       cgd 	if (t == tDOT) {
     88  1.13    simonb 		*valuep = (db_expr_t)db_dot;
     89  1.13    simonb 		return (TRUE);
     90   1.1       cgd 	}
     91   1.1       cgd 	if (t == tDOTDOT) {
     92  1.13    simonb 		*valuep = (db_expr_t)db_prev;
     93  1.13    simonb 		return (TRUE);
     94   1.1       cgd 	}
     95   1.1       cgd 	if (t == tPLUS) {
     96  1.13    simonb 		*valuep = (db_expr_t) db_next;
     97  1.13    simonb 		return (TRUE);
     98   1.1       cgd 	}
     99   1.1       cgd 	if (t == tDITTO) {
    100  1.13    simonb 		*valuep = (db_expr_t)db_last_addr;
    101  1.13    simonb 		return (TRUE);
    102   1.1       cgd 	}
    103   1.1       cgd 	if (t == tDOLLAR) {
    104  1.13    simonb 		if (!db_get_variable(valuep))
    105  1.13    simonb 		    return (FALSE);
    106  1.13    simonb 		return (TRUE);
    107   1.1       cgd 	}
    108   1.1       cgd 	if (t == tLPAREN) {
    109  1.13    simonb 		if (!db_expression(valuep)) {
    110  1.13    simonb 			db_error("Syntax error\n");
    111  1.13    simonb 			/*NOTREACHED*/
    112  1.13    simonb 		}
    113  1.13    simonb 		t = db_read_token();
    114  1.13    simonb 		if (t != tRPAREN) {
    115  1.13    simonb 			db_error("Syntax error\n");
    116  1.13    simonb 			/*NOTREACHED*/
    117  1.13    simonb 		}
    118  1.13    simonb 		return (TRUE);
    119   1.1       cgd 	}
    120   1.1       cgd 	db_unread_token(t);
    121   1.1       cgd 	return (FALSE);
    122   1.1       cgd }
    123   1.1       cgd 
    124  1.14   thorpej static bool
    125  1.13    simonb db_unary(db_expr_t *valuep)
    126   1.1       cgd {
    127   1.1       cgd 	int	t;
    128   1.1       cgd 
    129   1.1       cgd 	t = db_read_token();
    130   1.1       cgd 	if (t == tMINUS) {
    131  1.13    simonb 		if (!db_unary(valuep)) {
    132  1.13    simonb 			db_error("Syntax error\n");
    133  1.13    simonb 			/*NOTREACHED*/
    134  1.13    simonb 		}
    135  1.13    simonb 		*valuep = -*valuep;
    136  1.13    simonb 		return (TRUE);
    137   1.1       cgd 	}
    138   1.1       cgd 	if (t == tSTAR) {
    139  1.13    simonb 		/* indirection */
    140  1.13    simonb 		if (!db_unary(valuep)) {
    141  1.13    simonb 			db_error("Syntax error\n");
    142  1.13    simonb 			/*NOTREACHED*/
    143  1.13    simonb 		}
    144  1.13    simonb 		*valuep = db_get_value((db_addr_t)*valuep, sizeof(db_expr_t),
    145  1.13    simonb 		    FALSE);
    146  1.13    simonb 		return (TRUE);
    147   1.1       cgd 	}
    148   1.1       cgd 	db_unread_token(t);
    149   1.1       cgd 	return (db_term(valuep));
    150   1.1       cgd }
    151   1.1       cgd 
    152  1.14   thorpej static bool
    153  1.13    simonb db_mult_expr(db_expr_t *valuep)
    154   1.1       cgd {
    155   1.1       cgd 	db_expr_t	lhs, rhs;
    156   1.1       cgd 	int		t;
    157   1.1       cgd 
    158   1.1       cgd 	if (!db_unary(&lhs))
    159  1.13    simonb 		return (FALSE);
    160   1.1       cgd 
    161   1.1       cgd 	t = db_read_token();
    162   1.1       cgd 	while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH) {
    163  1.13    simonb 		if (!db_term(&rhs)) {
    164  1.13    simonb 			db_error("Syntax error\n");
    165  1.13    simonb 			/*NOTREACHED*/
    166  1.13    simonb 		}
    167  1.13    simonb 		if (t == tSTAR)
    168  1.13    simonb 			lhs *= rhs;
    169  1.13    simonb 		else {
    170  1.13    simonb 			if (rhs == 0) {
    171  1.13    simonb 				db_error("Divide by 0\n");
    172  1.13    simonb 				/*NOTREACHED*/
    173  1.13    simonb 			}
    174  1.13    simonb 			if (t == tSLASH)
    175  1.13    simonb 				lhs /= rhs;
    176  1.13    simonb 			else if (t == tPCT)
    177  1.13    simonb 				lhs %= rhs;
    178  1.13    simonb 			else
    179  1.13    simonb 				lhs = ((lhs+rhs-1)/rhs)*rhs;
    180  1.13    simonb 		}
    181  1.13    simonb 		t = db_read_token();
    182   1.1       cgd 	}
    183   1.1       cgd 	db_unread_token(t);
    184   1.1       cgd 	*valuep = lhs;
    185   1.1       cgd 	return (TRUE);
    186   1.1       cgd }
    187   1.1       cgd 
    188  1.14   thorpej static bool
    189  1.13    simonb db_add_expr(db_expr_t *valuep)
    190   1.1       cgd {
    191   1.1       cgd 	db_expr_t	lhs, rhs;
    192   1.1       cgd 	int		t;
    193   1.1       cgd 
    194   1.1       cgd 	if (!db_mult_expr(&lhs))
    195  1.13    simonb 		return (FALSE);
    196   1.1       cgd 
    197   1.1       cgd 	t = db_read_token();
    198   1.1       cgd 	while (t == tPLUS || t == tMINUS) {
    199  1.13    simonb 		if (!db_mult_expr(&rhs)) {
    200  1.13    simonb 			db_error("Syntax error\n");
    201  1.13    simonb 			/*NOTREACHED*/
    202  1.13    simonb 		}
    203  1.13    simonb 		if (t == tPLUS)
    204  1.13    simonb 			lhs += rhs;
    205  1.13    simonb 		else
    206  1.13    simonb 			lhs -= rhs;
    207  1.13    simonb 		t = db_read_token();
    208   1.1       cgd 	}
    209   1.1       cgd 	db_unread_token(t);
    210   1.1       cgd 	*valuep = lhs;
    211   1.1       cgd 	return (TRUE);
    212   1.1       cgd }
    213   1.1       cgd 
    214  1.14   thorpej static bool
    215  1.13    simonb db_shift_expr(db_expr_t *valuep)
    216   1.1       cgd {
    217   1.1       cgd 	db_expr_t	lhs, rhs;
    218   1.1       cgd 	int		t;
    219   1.1       cgd 
    220   1.1       cgd 	if (!db_add_expr(&lhs))
    221  1.13    simonb 		return (FALSE);
    222   1.1       cgd 
    223   1.1       cgd 	t = db_read_token();
    224   1.1       cgd 	while (t == tSHIFT_L || t == tSHIFT_R) {
    225  1.13    simonb 		if (!db_add_expr(&rhs)) {
    226  1.13    simonb 			db_error("Syntax error\n");
    227  1.13    simonb 			/*NOTREACHED*/
    228  1.13    simonb 		}
    229  1.13    simonb 		if (rhs < 0) {
    230  1.13    simonb 			db_error("Negative shift amount\n");
    231  1.13    simonb 			/*NOTREACHED*/
    232  1.13    simonb 		}
    233  1.13    simonb 		if (t == tSHIFT_L)
    234  1.13    simonb 			lhs <<= rhs;
    235  1.13    simonb 		else {
    236  1.13    simonb 			/* Shift right is unsigned */
    237  1.13    simonb 			lhs = (unsigned long) lhs >> rhs;
    238  1.13    simonb 		}
    239  1.13    simonb 		t = db_read_token();
    240   1.1       cgd 	}
    241   1.1       cgd 	db_unread_token(t);
    242   1.1       cgd 	*valuep = lhs;
    243   1.1       cgd 	return (TRUE);
    244   1.1       cgd }
    245   1.1       cgd 
    246   1.1       cgd int
    247  1.13    simonb db_expression(db_expr_t *valuep)
    248   1.1       cgd {
    249  1.13    simonb 
    250   1.1       cgd 	return (db_shift_expr(valuep));
    251   1.1       cgd }
    252