"I am a person who works hard and plays hard."

Yuan Wei
Second Year Graduate Student Department of Computer Science
University of Virginia Charlottesville, VA 22903
Email: yw3f@cs.virginia.edu


Source Code Analysis

Main Page   Compound List   File List   Compound Members   File Members  

eval.h

Go to the documentation of this file.
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 2000/05/26 15:21:53 taustin Exp $
00053  *
00054  * $Log: eval.h,v $
00055  * Revision 1.1.1.1  2000/05/26 15:21:53  taustin
00056  * SimpleScalar Tool Set
00057  *
00058  *
00059  * Revision 1.3  1999/12/31 18:36:51  taustin
00060  * cross-endian execution support added
00061  *
00062  * Revision 1.2  1998/08/27 08:27:01  taustin
00063  * implemented host interface description in host.h
00064  * added target interface support
00065  * added support for qword's
00066  *
00067  * Revision 1.1  1997/03/11  01:31:15  taustin
00068  * Initial revision
00069  *
00070  *
00071  */
00072 
00073 #ifndef EVAL_H
00074 #define EVAL_H
00075 
00076 #include <stdio.h>
00077 #include "host.h"
00078 #include "misc.h"
00079 #include "machine.h"
00080 
00081 /* forward declarations */
00082 struct eval_state_t;
00083 struct eval_value_t;
00084 
00085 /* an identifier evaluator, when an evaluator is instantiated, the user
00086    must supply a function of this type that returns the value of identifiers
00087    encountered in expressions */
00088 typedef struct eval_value_t               /* value of the identifier */
00089 (*eval_ident_t)(struct eval_state_t *es); /* ident string in es->tok_buf */
00090 
00091 /* expression tokens */
00092 enum eval_token_t {
00093   tok_ident,            /* user-valued identifiers */
00094   tok_const,            /* numeric literals */
00095   tok_plus,             /* `+' */
00096   tok_minus,            /* `-' */
00097   tok_mult,             /* `*' */
00098   tok_div,              /* `/' */
00099   tok_oparen,           /* `(' */
00100   tok_cparen,           /* `)' */
00101   tok_eof,              /* end of file */
00102   tok_whitespace,       /* ` ', `\t', `\n' */
00103   tok_invalid           /* unrecognized token */
00104 };
00105 
00106 /* an evaluator state record */
00107 struct eval_state_t {
00108   char *p;                      /* ptr to next char to consume from expr */
00109   char *lastp;                  /* save space for token peeks */
00110   eval_ident_t f_eval_ident;    /* identifier evaluator */
00111   void *user_ptr;               /* user-supplied argument pointer */
00112   char tok_buf[512];            /* text of last token returned */
00113   enum eval_token_t peek_tok;   /* peek buffer, for one token look-ahead */
00114 };
00115 
00116 /* evaluation errors */
00117 enum eval_err_t {
00118   ERR_NOERR,                    /* no error */
00119   ERR_UPAREN,                   /* unmatched parenthesis */
00120   ERR_NOTERM,                   /* expression term is missing */
00121   ERR_DIV0,                     /* divide by zero */
00122   ERR_BADCONST,                 /* badly formed constant */
00123   ERR_BADEXPR,                  /* badly formed constant */
00124   ERR_UNDEFVAR,                 /* variable is undefined */
00125   ERR_EXTRA,                    /* extra characters at end of expression */
00126   ERR_NUM
00127 };
00128 
00129 /* expression evaluation error, this must be a global */
00130 extern enum eval_err_t eval_error /* = ERR_NOERR */;
00131 
00132 /* enum eval_err_t -> error description string map */
00133 extern char *eval_err_str[ERR_NUM];
00134 
00135 /* expression value types */
00136 enum eval_type_t {
00137   et_int,                       /* signed integer result */
00138   et_uint,                      /* unsigned integer result */
00139   et_addr,                      /* address value */
00140 #ifdef HOST_HAS_QWORD
00141   et_qword,                     /* unsigned qword length integer result */
00142   et_sqword,                    /* signed qword length integer result */
00143 #endif /* HOST_HAS_QWORD */
00144   et_float,                     /* single-precision floating point value */
00145   et_double,                    /* double-precision floating point value */
00146   et_symbol,                    /* non-numeric result (!allowed in exprs)*/
00147   et_NUM
00148 };
00149 
00150 /* non-zero if type is an integral type */
00151 #ifdef HOST_HAS_QWORD
00152 #define EVAL_INTEGRAL(TYPE)                                             \
00153   ((TYPE) == et_int || (TYPE) == et_uint || (TYPE) == et_addr           \
00154    || (TYPE) == et_qword || (TYPE) == et_sqword)
00155 #else /* !HOST_HAS_QWORD */
00156 #define EVAL_INTEGRAL(TYPE)                                             \
00157   ((TYPE) == et_int || (TYPE) == et_uint || (TYPE) == et_addr)
00158 #endif /* HOST_HAS_QWORD */
00159 
00160 /* enum eval_type_t -> expression type description string map */
00161 extern char *eval_type_str[et_NUM];
00162 
00163 /* an expression value */
00164 struct eval_value_t {
00165   enum eval_type_t type;                /* type of expression value */
00166   union {
00167     int as_int;                         /* value for type == et_int */
00168     unsigned int as_uint;               /* value for type == et_uint */
00169     md_addr_t as_addr;                  /* value for type == et_addr */
00170 #ifdef HOST_HAS_QWORD
00171     qword_t as_qword;                   /* value for type == ec_qword */
00172     sqword_t as_sqword;                 /* value for type == ec_sqword */
00173 #endif /* HOST_HAS_QWORD */
00174     float as_float;                     /* value for type == et_float */
00175     double as_double;                   /* value for type == et_double */
00176     char *as_symbol;                    /* value for type == et_symbol */
00177   } value;
00178 };
00179 
00180 /*
00181  * expression value arithmetic conversions
00182  */
00183 
00184 /* eval_value_t (any numeric type) -> double */
00185 double eval_as_double(struct eval_value_t val);
00186 
00187 /* eval_value_t (any numeric type) -> float */
00188 float eval_as_float(struct eval_value_t val);
00189 
00190 #ifdef HOST_HAS_QWORD
00191 /* eval_value_t (any numeric type) -> qword_t */
00192 qword_t eval_as_qword(struct eval_value_t val);
00193 
00194 /* eval_value_t (any numeric type) -> sqword_t */
00195 sqword_t eval_as_sqword(struct eval_value_t val);
00196 #endif /* HOST_HAS_QWORD */
00197 
00198 /* eval_value_t (any numeric type) -> md_addr_t */
00199 md_addr_t eval_as_addr(struct eval_value_t val);
00200 
00201 /* eval_value_t (any numeric type) -> unsigned int */
00202 unsigned int eval_as_uint(struct eval_value_t val);
00203 
00204 /* eval_value_t (any numeric type) -> int */
00205 int eval_as_int(struct eval_value_t val);
00206 
00207 /* create an evaluator */
00208 struct eval_state_t *                   /* expression evaluator */
00209 eval_new(eval_ident_t f_eval_ident,     /* user ident evaluator */
00210          void *user_ptr);               /* user ptr passed to ident fn */
00211 
00212 /* delete an evaluator */
00213 void
00214 eval_delete(struct eval_state_t *es);   /* evaluator to delete */
00215 
00216 /* evaluate an expression, if an error occurs during evaluation, the
00217    global variable eval_error will be set to a value other than ERR_NOERR */
00218 struct eval_value_t                     /* value of the expression */
00219 eval_expr(struct eval_state_t *es,      /* expression evaluator */
00220           char *p,                      /* ptr to expression string */
00221           char **endp);                 /* returns ptr to 1st unused char */
00222 
00223 /* print an expression value */
00224 void
00225 eval_print(FILE *stream,                /* output stream */
00226            struct eval_value_t val);    /* expression value to print */
00227 
00228 #endif /* EVAL_H */


UVa CS Department of Computer Science
School of Engineering, University of Virginia
151 Engineer's Way, P.O. Box 400740
Charlottesville, Virginia 22904-4740

(434) 982-2200  Fax: (434) 982-2214