Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 273 lines (239 sloc) 6.681 kb
3fb7a05 Introduce verilog to CVS.
steve authored
1 /*
b734ecf Macintosh compilers do not support ident.
steve authored
2 * Copyright (c) 1998-1999 Stephen Williams (steve@icarus.com)
3fb7a05 Introduce verilog to CVS.
steve authored
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 */
19
b825f8d Create a config.h.in file to hold all the config
steve authored
20 # include "config.h"
21
8ea3b6b @ldoolitt header includes for gcc-4.3 compatibility
ldoolitt authored
22 # include <cstring>
b825f8d Create a config.h.in file to hold all the config
steve authored
23 # include <iostream>
24
3fb7a05 Introduce verilog to CVS.
steve authored
25 # include "PExpr.h"
5de9b7c Parse and elaborate the concatenate operator
steve authored
26 # include "netlist.h"
908955c Handle bit select of parameter with ranges.
steve authored
27 # include "netmisc.h"
219df16 Generalize the evaluation of floating point delays, and
steve authored
28 # include "compiler.h"
3fb7a05 Introduce verilog to CVS.
steve authored
29
8fd42fb Build errors in picky GCC compilers.
steve authored
30 verinum* PExpr::eval_const(Design*, NetScope*) const
3fb7a05 Introduce verilog to CVS.
steve authored
31 {
32 return 0;
33 }
34
8fd42fb Build errors in picky GCC compilers.
steve authored
35 verinum* PEBinary::eval_const(Design*des, NetScope*scope) const
5de9b7c Parse and elaborate the concatenate operator
steve authored
36 {
bf72f39 eval_const uses scope instead of a string path.
steve authored
37 verinum*l = left_->eval_const(des, scope);
5de9b7c Parse and elaborate the concatenate operator
steve authored
38 if (l == 0) return 0;
bf72f39 eval_const uses scope instead of a string path.
steve authored
39 verinum*r = right_->eval_const(des, scope);
5de9b7c Parse and elaborate the concatenate operator
steve authored
40 if (r == 0) {
41 delete l;
42 return 0;
43 }
44 verinum*res;
45
46 switch (op_) {
29abc5a Support + in constant expressions.
steve authored
47 case '+': {
91b01cd constant eval of arithmetic with x and z.
steve authored
48 if (l->is_defined() && r->is_defined()) {
bab0924 Make some effort to preserve bits while
steve authored
49 res = new verinum(*l + *r);
91b01cd constant eval of arithmetic with x and z.
steve authored
50 } else {
51 res = new verinum(verinum::Vx, l->len());
52 }
29abc5a Support + in constant expressions.
steve authored
53 break;
54 }
5de9b7c Parse and elaborate the concatenate operator
steve authored
55 case '-': {
91b01cd constant eval of arithmetic with x and z.
steve authored
56 if (l->is_defined() && r->is_defined()) {
bab0924 Make some effort to preserve bits while
steve authored
57 res = new verinum(*l - *r);
91b01cd constant eval of arithmetic with x and z.
steve authored
58 } else {
59 res = new verinum(verinum::Vx, l->len());
60 }
5de9b7c Parse and elaborate the concatenate operator
steve authored
61 break;
62 }
85ab6d1 Handle multiply in constant expressions.
steve authored
63 case '*': {
91b01cd constant eval of arithmetic with x and z.
steve authored
64 if (l->is_defined() && r->is_defined()) {
bab0924 Make some effort to preserve bits while
steve authored
65 res = new verinum(*l * *r);
91b01cd constant eval of arithmetic with x and z.
steve authored
66 } else {
67 res = new verinum(verinum::Vx, l->len());
68 }
85ab6d1 Handle multiply in constant expressions.
steve authored
69 break;
70 }
71 case '/': {
91b01cd constant eval of arithmetic with x and z.
steve authored
72 if (l->is_defined() && r->is_defined()) {
73 long lv = l->as_long();
74 long rv = r->as_long();
75 res = new verinum(lv / rv, l->len());
76 } else {
77 res = new verinum(verinum::Vx, l->len());
78 }
85ab6d1 Handle multiply in constant expressions.
steve authored
79 break;
80 }
81 case '%': {
91b01cd constant eval of arithmetic with x and z.
steve authored
82 if (l->is_defined() && r->is_defined()) {
83 long lv = l->as_long();
84 long rv = r->as_long();
85 res = new verinum(lv % rv, l->len());
86 } else {
87 res = new verinum(verinum::Vx, l->len());
88 }
85ab6d1 Handle multiply in constant expressions.
steve authored
89 break;
90 }
71bd810 Evaluate > and < in constant expressions.
steve authored
91 case '>': {
92 if (l->is_defined() && r->is_defined()) {
93 long lv = l->as_long();
94 long rv = r->as_long();
95 res = new verinum(lv > rv, l->len());
96 } else {
97 res = new verinum(verinum::Vx, l->len());
98 }
99 break;
100 }
101 case '<': {
102 if (l->is_defined() && r->is_defined()) {
103 long lv = l->as_long();
104 long rv = r->as_long();
105 res = new verinum(lv < rv, l->len());
106 } else {
107 res = new verinum(verinum::Vx, l->len());
108 }
109 break;
110 }
40ef704 Evaluate constant right shifts.
steve authored
111 case 'l': { // left shift (<<)
8befc6f Add support for << is signal indices.
steve authored
112 assert(r->is_defined());
113 unsigned long rv = r->as_ulong();
114 res = new verinum(verinum::V0, l->len());
115 if (rv < res->len()) {
116 unsigned cnt = res->len() - rv;
117 for (unsigned idx = 0 ; idx < cnt ; idx += 1)
118 res->set(idx+rv, l->get(idx));
119 }
120 break;
121 }
40ef704 Evaluate constant right shifts.
steve authored
122 case 'r': { // right shift (>>)
123 assert(r->is_defined());
124 unsigned long rv = r->as_ulong();
125 res = new verinum(verinum::V0, l->len());
126 if (rv < res->len()) {
127 unsigned cnt = res->len() - rv;
128 for (unsigned idx = 0 ; idx < cnt ; idx += 1)
129 res->set(idx, l->get(idx+rv));
130 }
131 break;
132 }
8befc6f Add support for << is signal indices.
steve authored
133
5de9b7c Parse and elaborate the concatenate operator
steve authored
134 default:
135 delete l;
136 delete r;
137 return 0;
138 }
139
8befc6f Add support for << is signal indices.
steve authored
140 delete l;
141 delete r;
5de9b7c Parse and elaborate the concatenate operator
steve authored
142 return res;
143 }
8fd42fb Build errors in picky GCC compilers.
steve authored
144 verinum* PEConcat::eval_const(Design*des, NetScope*scope) const
101b373 Allow constant concat expressions.
steve authored
145 {
146 verinum*accum = parms_[0]->eval_const(des, scope);
147 if (accum == 0)
148 return 0;
149
cced1e7 @steveicarus Remove some uses of the svector template.
steveicarus authored
150 for (unsigned idx = 1 ; idx < parms_.size() ; idx += 1) {
4c885f6 Error message if concat expression cannot evaluate.
steve authored
151
101b373 Allow constant concat expressions.
steve authored
152 verinum*tmp = parms_[idx]->eval_const(des, scope);
4c885f6 Error message if concat expression cannot evaluate.
steve authored
153 if (tmp == 0) {
154 delete accum;
155 return 0;
156 }
101b373 Allow constant concat expressions.
steve authored
157 assert(tmp);
158
159 *accum = concat(*accum, *tmp);
160 delete tmp;
161 }
162
163 return accum;
164 }
5de9b7c Parse and elaborate the concatenate operator
steve authored
165
219df16 Generalize the evaluation of floating point delays, and
steve authored
166
251eb4f Remove spurious message.
steve authored
167 /*
168 * Evaluate an identifier as a constant expression. This is only
169 * possible if the identifier is that of a parameter.
170 */
8fd42fb Build errors in picky GCC compilers.
steve authored
171 verinum* PEIdent::eval_const(Design*des, NetScope*scope) const
5de9b7c Parse and elaborate the concatenate operator
steve authored
172 {
e7efc27 Redesign the implementation of scopes and parameters.
steve authored
173 assert(scope);
908955c Handle bit select of parameter with ranges.
steve authored
174 NetNet*net;
175 NetEvent*eve;
176 const NetExpr*expr;
f001d00 Add support for generate loops w/ wires and gates.
steve authored
177
ddd36ec Rework the heirarchical identifier parse syntax and pform
steve authored
178 const name_component_t&name_tail = path_.back();
179
f001d00 Add support for generate loops w/ wires and gates.
steve authored
180 // Handle the special case that this ident is a genvar
181 // variable name. In that case, the genvar meaning preempts
182 // everything and we just return that value immediately.
183 if (scope->genvar_tmp
ddd36ec Rework the heirarchical identifier parse syntax and pform
steve authored
184 && strcmp(name_tail.name,scope->genvar_tmp) == 0) {
f001d00 Add support for generate loops w/ wires and gates.
steve authored
185 return new verinum(scope->genvar_tmp_val);
186 }
187
18edf2f Rework of automatic task/function support.
Martin Whitaker authored
188 symbol_search(this, des, scope, path_, net, expr, eve);
251eb4f Remove spurious message.
steve authored
189
190 if (expr == 0)
4594ac1 elaborate concatenation repeats.
steve authored
191 return 0;
251eb4f Remove spurious message.
steve authored
192
4fed9d6 Simply give up on constants with indices.
steve authored
193 const NetEConst*eval = dynamic_cast<const NetEConst*>(expr);
1eb714e More verbose eval_const assert message.
steve authored
194 if (eval == 0) {
7975e14 @steveicarus LineInfo uses perm_string for path.
steveicarus authored
195 cerr << get_fileline() << ": internal error: Unable to evaluate "
4350180 Redo the parameter vector support to allow
steve authored
196 << "constant expression (parameter=" << path_
197 << "): " << *expr << endl;
1eb714e More verbose eval_const assert message.
steve authored
198 return 0;
199 }
200
4fed9d6 Simply give up on constants with indices.
steve authored
201 assert(eval);
e96e8c6 NetESignal object no longer need to be NetNode
steve authored
202
ddd36ec Rework the heirarchical identifier parse syntax and pform
steve authored
203 if (!name_tail.index.empty())
3a5e55b Elaborate parameters in phases.
steve authored
204 return 0;
205
4fed9d6 Simply give up on constants with indices.
steve authored
206
5de9b7c Parse and elaborate the concatenate operator
steve authored
207 return new verinum(eval->value());
208 }
209
8fd42fb Build errors in picky GCC compilers.
steve authored
210 verinum* PEFNumber::eval_const(Design*, NetScope*) const
084a464 Support decimal constants in behavioral delays.
steve authored
211 {
212 long val = value_->as_long();
213 return new verinum(val);
214 }
215
8fd42fb Build errors in picky GCC compilers.
steve authored
216 verinum* PENumber::eval_const(Design*, NetScope*) const
3fb7a05 Introduce verilog to CVS.
steve authored
217 {
218 return new verinum(value());
219 }
220
8fd42fb Build errors in picky GCC compilers.
steve authored
221 verinum* PEString::eval_const(Design*, NetScope*) const
e6c0629 Add language support for Verilog-2001 attribute
steve authored
222 {
223 return new verinum(string(text_));
224 }
225
8fd42fb Build errors in picky GCC compilers.
steve authored
226 verinum* PETernary::eval_const(Design*des, NetScope*scope) const
a5921ce netlist support for ternary operator.
steve authored
227 {
bf72f39 eval_const uses scope instead of a string path.
steve authored
228 verinum*test = expr_->eval_const(des, scope);
5ffac8a Handle evaluation of ternary during elaboration.
steve authored
229 if (test == 0)
230 return 0;
231
232 verinum::V bit = test->get(0);
233 delete test;
234 switch (bit) {
235 case verinum::V0:
bf72f39 eval_const uses scope instead of a string path.
steve authored
236 return fal_->eval_const(des, scope);
5ffac8a Handle evaluation of ternary during elaboration.
steve authored
237 case verinum::V1:
bf72f39 eval_const uses scope instead of a string path.
steve authored
238 return tru_->eval_const(des, scope);
5ffac8a Handle evaluation of ternary during elaboration.
steve authored
239 default:
240 return 0;
241 // XXXX It is possible to handle this case if both fal_
242 // and tru_ are constant. Someday...
243 }
a5921ce netlist support for ternary operator.
steve authored
244 }
245
8fd42fb Build errors in picky GCC compilers.
steve authored
246 verinum* PEUnary::eval_const(Design*des, NetScope*scope) const
d86b37d Support unary + and - in constants.
steve authored
247 {
bf72f39 eval_const uses scope instead of a string path.
steve authored
248 verinum*val = expr_->eval_const(des, scope);
d86b37d Support unary + and - in constants.
steve authored
249 if (val == 0)
250 return 0;
251
252 switch (op_) {
253 case '+':
254 return val;
255
6c4b5cf Fix sign extension of evaluated constants. (PR#91)
steve authored
256 case '-': {
257 /* We need to expand the value a bit if we are
258 taking the 2's complement so that we are
259 guaranteed to not overflow. */
fc0695b Handle 64bit delay constants.
steve authored
260 verinum tmp ((uint64_t)0, val->len()+1);
6c4b5cf Fix sign extension of evaluated constants. (PR#91)
steve authored
261 for (unsigned idx = 0 ; idx < val->len() ; idx += 1)
262 tmp.set(idx, val->get(idx));
263
264 *val = v_not(tmp) + verinum(verinum::V1, 1);
265 val->has_sign(true);
266 return val;
267 }
d86b37d Support unary + and - in constants.
steve authored
268
269 }
270 delete val;
271 return 0;
272 }
Something went wrong with that request. Please try again.