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