00001 /* 00002 * expr.h - expression evaluator interfaces 00003 * 00004 * This file is a part of the SimpleScalar tool suite written by 00005 * Todd M. Austin as a part of the Multiscalar Research Project. 00006 * 00007 * The tool suite is currently maintained by Doug Burger and Todd M. Austin. 00008 * 00009 * Copyright (C) 1994, 1995, 1996, 1997, 1998 by Todd M. Austin 00010 * 00011 * This source file is distributed "as is" in the hope that it will be 00012 * useful. The tool set comes with no warranty, and no author or 00013 * distributor accepts any responsibility for the consequences of its 00014 * use. 00015 * 00016 * Everyone is granted permission to copy, modify and redistribute 00017 * this tool set under the following conditions: 00018 * 00019 * This source code is distributed for non-commercial use only. 00020 * Please contact the maintainer for restrictions applying to 00021 * commercial use. 00022 * 00023 * Permission is granted to anyone to make or distribute copies 00024 * of this source code, either as received or modified, in any 00025 * medium, provided that all copyright notices, permission and 00026 * nonwarranty notices are preserved, and that the distributor 00027 * grants the recipient permission for further redistribution as 00028 * permitted by this document. 00029 * 00030 * Permission is granted to distribute this file in compiled 00031 * or executable form under the same conditions that apply for 00032 * source code, provided that either: 00033 * 00034 * A. it is accompanied by the corresponding machine-readable 00035 * source code, 00036 * B. it is accompanied by a written offer, with no time limit, 00037 * to give anyone a machine-readable copy of the corresponding 00038 * source code in return for reimbursement of the cost of 00039 * distribution. This written offer must permit verbatim 00040 * duplication by anyone, or 00041 * C. it is distributed by someone who received only the 00042 * executable form, and is accompanied by a copy of the 00043 * written offer of source code that they received concurrently. 00044 * 00045 * In other words, you are welcome to use, share and improve this 00046 * source file. You are forbidden to forbid anyone else to use, share 00047 * and improve what you give them. 00048 * 00049 * INTERNET: dburger@cs.wisc.edu 00050 * US Mail: 1210 W. Dayton Street, Madison, WI 53706 00051 * 00052 * $Id: eval.h,v 1.1.1.1 2003/09/18 00:57:54 panalyzer Exp $ 00053 * 00054 * $Log: eval.h,v $ 00055 * Revision 1.1.1.1 2003/09/18 00:57:54 panalyzer 00056 * 00057 * 00058 * Revision 1.1.1.1 2003/09/18 00:18:43 panalyzer 00059 * 00060 * 00061 * Revision 1.1.1.1 2003/09/16 18:48:14 gdm 00062 * 00063 * 00064 * Revision 1.1.1.1 2000/11/29 14:53:54 cu-cs 00065 * Grand unification of arm sources. 00066 * 00067 * 00068 * Revision 1.1.1.1 2000/05/26 15:21:53 taustin 00069 * SimpleScalar Tool Set 00070 * 00071 * 00072 * Revision 1.3 1999/12/31 18:36:51 taustin 00073 * cross-endian execution support added 00074 * 00075 * Revision 1.2 1998/08/27 08:27:01 taustin 00076 * implemented host interface description in host.h 00077 * added target interface support 00078 * added support for qword's 00079 * 00080 * Revision 1.1 1997/03/11 01:31:15 taustin 00081 * Initial revision 00082 * 00083 * 00084 */ 00085 00086 #ifndef EVAL_H 00087 #define EVAL_H 00088 00089 #include <stdio.h> 00090 #include "host.h" 00091 #include "misc.h" 00092 #include "machine.h" 00093 00094 /* forward declarations */ 00095 struct eval_state_t; 00096 struct eval_value_t; 00097 00098 /* an identifier evaluator, when an evaluator is instantiated, the user 00099 must supply a function of this type that returns the value of identifiers 00100 encountered in expressions */ 00101 typedef struct eval_value_t /* value of the identifier */ 00102 (*eval_ident_t)(struct eval_state_t *es); /* ident string in es->tok_buf */ 00103 00104 /* expression tokens */ 00105 enum eval_token_t { 00106 tok_ident, /* user-valued identifiers */ 00107 tok_const, /* numeric literals */ 00108 tok_plus, /* `+' */ 00109 tok_minus, /* `-' */ 00110 tok_mult, /* `*' */ 00111 tok_div, /* `/' */ 00112 tok_oparen, /* `(' */ 00113 tok_cparen, /* `)' */ 00114 tok_eof, /* end of file */ 00115 tok_whitespace, /* ` ', `\t', `\n' */ 00116 tok_invalid /* unrecognized token */ 00117 }; 00118 00119 /* an evaluator state record */ 00120 struct eval_state_t { 00121 char *p; /* ptr to next char to consume from expr */ 00122 char *lastp; /* save space for token peeks */ 00123 eval_ident_t f_eval_ident; /* identifier evaluator */ 00124 void *user_ptr; /* user-supplied argument pointer */ 00125 char tok_buf[512]; /* text of last token returned */ 00126 enum eval_token_t peek_tok; /* peek buffer, for one token look-ahead */ 00127 }; 00128 00129 /* evaluation errors */ 00130 enum eval_err_t { 00131 ERR_NOERR, /* no error */ 00132 ERR_UPAREN, /* unmatched parenthesis */ 00133 ERR_NOTERM, /* expression term is missing */ 00134 ERR_DIV0, /* divide by zero */ 00135 ERR_BADCONST, /* badly formed constant */ 00136 ERR_BADEXPR, /* badly formed constant */ 00137 ERR_UNDEFVAR, /* variable is undefined */ 00138 ERR_EXTRA, /* extra characters at end of expression */ 00139 ERR_NUM 00140 }; 00141 00142 /* expression evaluation error, this must be a global */ 00143 extern enum eval_err_t eval_error /* = ERR_NOERR */; 00144 00145 /* enum eval_err_t -> error description string map */ 00146 extern char *eval_err_str[ERR_NUM]; 00147 00148 /* expression value types */ 00149 enum eval_type_t { 00150 et_int, /* signed integer result */ 00151 et_uint, /* unsigned integer result */ 00152 et_addr, /* address value */ 00153 #ifdef HOST_HAS_QWORD 00154 et_qword, /* unsigned qword length integer result */ 00155 et_sqword, /* signed qword length integer result */ 00156 #endif /* HOST_HAS_QWORD */ 00157 et_float, /* single-precision floating point value */ 00158 et_double, /* double-precision floating point value */ 00159 et_symbol, /* non-numeric result (!allowed in exprs)*/ 00160 et_NUM 00161 }; 00162 00163 /* non-zero if type is an integral type */ 00164 #ifdef HOST_HAS_QWORD 00165 #define EVAL_INTEGRAL(TYPE) \ 00166 ((TYPE) == et_int || (TYPE) == et_uint || (TYPE) == et_addr \ 00167 || (TYPE) == et_qword || (TYPE) == et_sqword) 00168 #else /* !HOST_HAS_QWORD */ 00169 #define EVAL_INTEGRAL(TYPE) \ 00170 ((TYPE) == et_int || (TYPE) == et_uint || (TYPE) == et_addr) 00171 #endif /* HOST_HAS_QWORD */ 00172 00173 /* enum eval_type_t -> expression type description string map */ 00174 extern char *eval_type_str[et_NUM]; 00175 00176 /* an expression value */ 00177 struct eval_value_t { 00178 enum eval_type_t type; /* type of expression value */ 00179 union { 00180 int as_int; /* value for type == et_int */ 00181 unsigned int as_uint; /* value for type == et_uint */ 00182 md_addr_t as_addr; /* value for type == et_addr */ 00183 #ifdef HOST_HAS_QWORD 00184 qword_t as_qword; /* value for type == ec_qword */ 00185 sqword_t as_sqword; /* value for type == ec_sqword */ 00186 #endif /* HOST_HAS_QWORD */ 00187 float as_float; /* value for type == et_float */ 00188 double as_double; /* value for type == et_double */ 00189 char *as_symbol; /* value for type == et_symbol */ 00190 } value; 00191 }; 00192 00193 /* 00194 * expression value arithmetic conversions 00195 */ 00196 00197 /* eval_value_t (any numeric type) -> double */ 00198 double eval_as_double(struct eval_value_t val); 00199 00200 /* eval_value_t (any numeric type) -> float */ 00201 float eval_as_float(struct eval_value_t val); 00202 00203 #ifdef HOST_HAS_QWORD 00204 /* eval_value_t (any numeric type) -> qword_t */ 00205 qword_t eval_as_qword(struct eval_value_t val); 00206 00207 /* eval_value_t (any numeric type) -> sqword_t */ 00208 sqword_t eval_as_sqword(struct eval_value_t val); 00209 #endif /* HOST_HAS_QWORD */ 00210 00211 /* eval_value_t (any numeric type) -> md_addr_t */ 00212 md_addr_t eval_as_addr(struct eval_value_t val); 00213 00214 /* eval_value_t (any numeric type) -> unsigned int */ 00215 unsigned int eval_as_uint(struct eval_value_t val); 00216 00217 /* eval_value_t (any numeric type) -> int */ 00218 int eval_as_int(struct eval_value_t val); 00219 00220 /* create an evaluator */ 00221 struct eval_state_t * /* expression evaluator */ 00222 eval_new(eval_ident_t f_eval_ident, /* user ident evaluator */ 00223 void *user_ptr); /* user ptr passed to ident fn */ 00224 00225 /* delete an evaluator */ 00226 void 00227 eval_delete(struct eval_state_t *es); /* evaluator to delete */ 00228 00229 /* evaluate an expression, if an error occurs during evaluation, the 00230 global variable eval_error will be set to a value other than ERR_NOERR */ 00231 struct eval_value_t /* value of the expression */ 00232 eval_expr(struct eval_state_t *es, /* expression evaluator */ 00233 char *p, /* ptr to expression string */ 00234 char **endp); /* returns ptr to 1st unused char */ 00235 00236 /* print an expression value */ 00237 void 00238 eval_print(FILE *stream, /* output stream */ 00239 struct eval_value_t val); /* expression value to print */ 00240 00241 #endif /* EVAL_H */