1 /**************************************************************************** 2 Copyright (C) 1987-2015 by Jeffery P. Hansen 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation; either version 2 of the License, or 7 (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License along 15 with this program; if not, write to the Free Software Foundation, Inc., 16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 17 18 Last edit by hansen on Sat Sep 9 19:28:06 2006 19 ****************************************************************************/ 20 #ifndef __expr_h 21 #define __expr_h 22 23 /***************************************************************************** 24 * 25 * Table of expression codes. 26 * 27 *****************************************************************************/ 28 typedef enum { 29 E_NOT = 0, 30 E_UINV = 1, 31 E_UNEG = 2, 32 E_POSEDGE = 3, 33 E_NEGEDGE = 4, 34 E_MUL = 5, 35 E_DIV = 6, 36 E_MOD = 7, 37 E_ADD = 8, 38 E_SUB = 9, 39 E_RSHIFT = 10, 40 E_LSHIFT = 11, 41 E_ARSHIFT = 12, 42 E_ALSHIFT = 13, 43 E_GT = 14, 44 E_LT = 15, 45 E_GE = 16, 46 E_LE = 17, 47 E_EQ = 18, 48 E_NE = 19, 49 E_BAND = 20, 50 E_BNAND = 21, 51 E_BXOR = 22, 52 E_BNXOR = 23, 53 E_BOR = 24, 54 E_BNOR = 25, 55 E_AND = 26, 56 E_OR = 27, 57 E_QUEST = 28, 58 E_AT = 29, 59 E_DELAY = 30, 60 E_CONCAT = 31, 61 E_REPCAT = 32, 62 E_EQZ = 33, 63 E_NEZ = 34, 64 E_VECTORP = 35, 65 E_VECTORN = 36, 66 E_RANGE = 37, 67 E_UAND = 38, 68 E_UOR = 39, 69 E_UXOR = 40, 70 E_UNAND = 41, 71 E_UNOR = 42, 72 E_UNXOR = 43, 73 E_COND = 44, 74 75 E_LITERAL = 1000, 76 E_HEX = 1001, 77 E_NUMBER = 1002, 78 E_EVENTOR = 1005, 79 E_TASK = 1006, 80 E_REAL = 1007, 81 } exprcode_t; 82 83 /***************************************************************************** 84 * 85 * Operator output bit size. 86 * 87 *****************************************************************************/ 88 typedef enum { 89 OS_NONE, /* Output size is not applicable */ 90 OS_ONE, /* Output is always 1-bit */ 91 OS_MAX, /* Output is size of largest operand */ 92 OS_SUM, /* Output size is sum of operands */ 93 OS_SPECIAL, /* Special handling for output size */ 94 } outsize_t; 95 96 /***************************************************************************** 97 * 98 * Range styles 99 * 100 *****************************************************************************/ 101 typedef enum { 102 RS_SINGLE, /* This is the [expr] form */ 103 RS_MAXMIN, /* This is the [max:min] form */ 104 RS_BASEUP, /* This is the [base+:width] form */ 105 RS_BASEDN, /* This is the [base-:width] form */ 106 RS_AUTO, /* This is the [*] form */ 107 } rangestyle_t; 108 109 typedef enum { 110 PEF_NONE = 0x0, /* Normal processing */ 111 PEF_SPECPARM = 0x1, /* Lookup specify parameters */ 112 } parmevflags_t; 113 114 /***************************************************************************** 115 * 116 * Expr - Verilog expression object 117 * 118 * This object is used to represent a Verilog expression. 119 * 120 *****************************************************************************/ 121 struct Expr_str { 122 exprcode_t e_type; /* Expression type */ 123 int e_nbits; /* Number of bits in expression (or -1 for unsized) */ 124 union { 125 Expr *opr[3]; /* Operator arguments */ 126 Value *snum; /* Sized number */ 127 struct { 128 char *name; /* Literal name */ 129 int ishlit; /* Is this a hierarchical literal */ 130 } literal; 131 struct { 132 char *name; /* Name of task */ 133 int argc; /* Number of arguments */ 134 Expr **argv; /* Arguments passed */ 135 } task; 136 } e; 137 }; 138 139 /***************************************************************************** 140 * 141 * NameExpr - Name/Expr pair 142 * 143 *****************************************************************************/ 144 typedef struct { 145 char *ne_name; /* Name */ 146 Expr *ne_expr; /* Expression */ 147 } NameExpr; 148 149 /***************************************************************************** 150 * 151 * OpDesc - Operator description 152 * 153 *****************************************************************************/ 154 typedef struct { 155 exprcode_t od_type; /* Type code for operand */ 156 int od_plev; /* Precidence level */ 157 int od_nopr; /* Number of operands */ 158 char *od_text; /* Text for operand */ 159 outsize_t od_outSize; /* Code representing output bit sizing */ 160 valueop_f *od_opfunc; /* Function implementing operator */ 161 valueop_f *od_w_opfunc; /* Function implementing operator when operands <32 bits */ 162 valueop_f *od_f_opfunc; /* Function implementing floating point operands */ 163 } OpDesc; 164 165 166 /***************************************************************************** 167 * 168 * VRange - Range declaration (may contain uninstantiated parameters) 169 * 170 *****************************************************************************/ 171 typedef struct { 172 rangestyle_t vr_style; /* Range style */ 173 Expr *vr_left,*vr_right; /* Left and right portions of range */ 174 } VRange; 175 176 /***************************************************************************** 177 * Expr methods 178 *****************************************************************************/ 179 Expr *new_Expr_lit(const char *name); 180 Expr *new_Expr_op1(exprcode_t op,Expr *e); 181 Expr *new_Expr_op(exprcode_t op,Expr *l,Expr *r); 182 Expr *new_Expr_op3(exprcode_t op,Expr *l,Expr *m,Expr *r); 183 Expr *new_Expr_repcat(Expr *n,Expr *e); 184 Expr *new_Expr_realnum(real_t); 185 Expr *new_Expr_num(int); 186 Expr *new_Expr_hex(const char *spec); 187 Expr *new_Expr_str(const char *s); 188 Expr *new_Expr_task(const char *name,List *); 189 void delete_Expr(Expr*e); 190 void Expr_print(Expr*e,FILE *f); 191 char *Expr_getstr(Expr*e,char *p); 192 Value *Expr_parmEval(Expr *e,Scope *scope,parmevflags_t); 193 int Expr_parmEvalI(Expr *e,Scope *scope,unsigned *n,parmevflags_t); 194 void Expr_getLastError(char*); 195 int Expr_getBitSize(Expr *e,Scope *scope); 196 int Expr_getCollapsedBitSize(Expr *e,Scope *scope); 197 Value *Expr_generate(Expr *e,int nbits,Scope *scope,CodeBlock *cb); 198 Value *Expr_generateS(Expr *e,Scope *scope,CodeBlock *cb); 199 #define Expr_type(ex) (ex)->e_type 200 #define Expr_getLitName(ex) (ex)->e.literal.name 201 #define Expr_isHLiteral(ex) (ex)->e.literal.ishlit 202 203 void Expr_expandConcat(Expr *e,Scope *scope, List *l); 204 void Expr_makeTriggerList(Expr *trigger, List *triggerList); 205 Trigger *Expr_getTrigger(Expr *trigger,Scope *scope, StatDecl *stat); 206 int Expr_generateTrigger(Expr *triggerExpr, Scope *scope, CodeBlock *cb, StatDecl *stat); 207 int Expr_generateBCond(Expr *bcond, Scope *scope, CodeBlock *cb, StatDecl *stat); 208 int Expr_getDelay(Expr *delayExpr,Scope *scope,Timescale *ts,deltatime_t *delay); 209 int Expr_generateDelay(Expr *delay, Scope *scope, CodeBlock *cb); 210 void Expr_getReaders(Expr*e, Scope *scope, PHash *H); 211 void Expr_getStaticReaders(Expr*e, SHash *H); 212 Trigger *Expr_getDefaultTrigger(Expr *e,Scope *scope); 213 Trigger *Expr_getDefaultTriggerFromSet(PHash *P,Circuit *c); 214 int Expr_lhsGenerate(Expr *e,Scope *scope, CodeBlock *cb,Net **n,Value **nLsb,unsigned *size,Value **nAddr); 215 int Expr_decodeVector(Expr *e,Scope *scope,Net **n,VRange **addr,VRange **bits); 216 217 /***************************************************************************** 218 * VRange methods 219 *****************************************************************************/ 220 VRange *new_VRange(rangestyle_t ,Expr *msb,Expr *lsb); 221 void delete_VRange(VRange *r, int recursive); 222 void VRange_print(VRange *r,FILE *f); 223 int VRange_parmEvalMsb(VRange *r,Scope *scope,unsigned *msb); 224 int VRange_parmEvalLsb(VRange *r,Scope *scope,unsigned *lsb); 225 Expr *VRange_getLsb(VRange*); 226 int VRange_getSize(VRange*,Scope *scope,unsigned *width); 227 int VRange_getDirect(VRange *r,char *buf); 228 #define VRange_getStyle(r) (r)->vr_style 229 230 /***************************************************************************** 231 * NameExpr methods 232 *****************************************************************************/ 233 NameExpr *new_NameExpr(const char *name,Expr *e); 234 235 /***************************************************************************** 236 * OpDesc methods 237 *****************************************************************************/ 238 OpDesc *OpDesc_findFunc(valueop_f *opfunc); 239 #endif 240