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 2006-01-31 16:35:46 afrodri Exp $ 00053 * 00054 * $Log: not supported by cvs2svn $ 00055 * Revision 1.2 2004/07/14 18:23:28 arodrig6 00056 * documentation chnges 00057 * 00058 * Revision 1.1 2004/04/22 16:37:36 arodrig6 00059 * ppc backend 00060 * 00061 * Revision 1.2 2000/04/10 23:46:29 karu 00062 * converted all quad to qword. NO OTHER change made. 00063 * the tag specfp95-before-quad-qword is a snapshow before this change was made 00064 * 00065 * Revision 1.1.1.1 2000/03/07 05:15:16 karu 00066 * this is the repository created for my own maintanence. 00067 * created when spec95 (lisp and compress worked). 00068 * compress still had the scanf("%i") problem 00069 * DIFF from the repository I am using alongwith ramdass on /projects 00070 * need to merge the two sometime :-) 00071 * 00072 * Revision 1.1.1.1 2000/02/25 21:02:50 karu 00073 * creating cvs repository for ss3-ppc 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 quadword'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 "ppcMachine.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 //!IGNORE: 00121 struct eval_state_t { 00122 char *p; /* ptr to next char to consume from expr */ 00123 char *lastp; /* save space for token peeks */ 00124 eval_ident_t f_eval_ident; /* identifier evaluator */ 00125 void *user_ptr; /* user-supplied argument pointer */ 00126 char tok_buf[512]; /* text of last token returned */ 00127 enum eval_token_t peek_tok; /* peek buffer, for one token look-ahead */ 00128 }; 00129 00130 /* evaluation errors */ 00131 enum eval_err_t { 00132 ERR_NOERR, /* no error */ 00133 ERR_UPAREN, /* unmatched parenthesis */ 00134 ERR_NOTERM, /* expression term is missing */ 00135 ERR_DIV0, /* divide by zero */ 00136 ERR_BADCONST, /* badly formed constant */ 00137 ERR_BADEXPR, /* badly formed constant */ 00138 ERR_UNDEFVAR, /* variable is undefined */ 00139 ERR_EXTRA, /* extra characters at end of expression */ 00140 ERR_NUM 00141 }; 00142 00143 /* expression evaluation error, this must be a global */ 00144 extern enum eval_err_t eval_error /* = ERR_NOERR */; 00145 00146 /* enum eval_err_t -> error description string map */ 00147 extern char *eval_err_str[ERR_NUM]; 00148 00149 /* expression value types */ 00150 enum eval_type_t { 00151 et_int, /* signed integer result */ 00152 et_uint, /* unsigned integer result */ 00153 et_addr, /* address value */ 00154 #ifdef HOST_HAS_QWORD 00155 et_quad, /* unsigned quadword length integer result */ 00156 et_squad, /* signed quadword length integer result */ 00157 #endif /* HOST_HAS_QWORD */ 00158 et_float, /* single-precision floating point value */ 00159 et_double, /* double-precision floating point value */ 00160 et_symbol, /* non-numeric result (!allowed in exprs)*/ 00161 et_NUM 00162 }; 00163 00164 /* non-zero if type is an integral type */ 00165 #ifdef HOST_HAS_QWORD 00166 #define EVAL_INTEGRAL(TYPE) \ 00167 ((TYPE) == et_int || (TYPE) == et_uint || (TYPE) == et_addr \ 00168 || (TYPE) == et_quad || (TYPE) == et_squad) 00169 #else /* !HOST_HAS_QWORD */ 00170 #define EVAL_INTEGRAL(TYPE) \ 00171 ((TYPE) == et_int || (TYPE) == et_uint || (TYPE) == et_addr) 00172 #endif /* HOST_HAS_QWORD */ 00173 00174 /* enum eval_type_t -> expression type description string map */ 00175 extern char *eval_type_str[et_NUM]; 00176 00177 //: an expression value 00178 //!IGNORE: 00179 struct eval_value_t { 00180 enum eval_type_t type; /* type of expression value */ 00181 union { 00182 int as_int; /* value for type == et_int */ 00183 unsigned int as_uint; /* value for type == et_uint */ 00184 md_addr_t as_addr; /* value for type == et_addr */ 00185 #ifdef HOST_HAS_QWORD 00186 qword_t as_quad; /* value for type == ec_quad */ 00187 sqword_t as_squad; /* value for type == ec_squad */ 00188 #endif /* HOST_HAS_QWORD */ 00189 float as_float; /* value for type == et_float */ 00190 double as_double; /* value for type == et_double */ 00191 char *as_symbol; /* value for type == et_symbol */ 00192 } value; 00193 }; 00194 00195 /* 00196 * expression value arithmetic conversions 00197 */ 00198 00199 /* eval_value_t (any numeric type) -> double */ 00200 double eval_as_double(struct eval_value_t val); 00201 00202 /* eval_value_t (any numeric type) -> float */ 00203 float eval_as_float(struct eval_value_t val); 00204 00205 #ifdef HOST_HAS_QWORD 00206 /* eval_value_t (any numeric type) -> qword_t */ 00207 qword_t eval_as_quad(struct eval_value_t val); 00208 00209 /* eval_value_t (any numeric type) -> sqword_t */ 00210 sqword_t eval_as_squad(struct eval_value_t val); 00211 #endif /* HOST_HAS_QWORD */ 00212 00213 /* eval_value_t (any numeric type) -> md_addr_t */ 00214 md_addr_t eval_as_addr(struct eval_value_t val); 00215 00216 /* eval_value_t (any numeric type) -> unsigned int */ 00217 unsigned int eval_as_uint(struct eval_value_t val); 00218 00219 /* eval_value_t (any numeric type) -> int */ 00220 int eval_as_int(struct eval_value_t val); 00221 00222 /* create an evaluator */ 00223 struct eval_state_t * /* expression evaluator */ 00224 eval_new(eval_ident_t f_eval_ident, /* user ident evaluator */ 00225 void *user_ptr); /* user ptr passed to ident fn */ 00226 00227 /* delete an evaluator */ 00228 void 00229 eval_delete(struct eval_state_t *es); /* evaluator to delete */ 00230 00231 /* evaluate an expression, if an error occurs during evaluation, the 00232 global variable eval_error will be set to a value other than ERR_NOERR */ 00233 struct eval_value_t /* value of the expression */ 00234 eval_expr(struct eval_state_t *es, /* expression evaluator */ 00235 char *p, /* ptr to expression string */ 00236 char **endp); /* returns ptr to 1st unused char */ 00237 00238 /* print an expression value */ 00239 void 00240 eval_print(FILE *stream, /* output stream */ 00241 struct eval_value_t val); /* expression value to print */ 00242 00243 #endif /* EVAL_H */