Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 470 lines (391 sloc) 10.464 kb
5796524 Add ne_expr.cc
steve authored
1 /*
2 * Copyright (c) 2002 Stephen Williams (steve@icarus.com)
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 */
52bf4e6 conditional ident string using autoconfig.
steve authored
19 #ifdef HAVE_CVS_IDENT
6d94f2e Better organize the NetESFunc return type guesses.
steve authored
20 #ident "$Id: net_expr.cc,v 1.15 2003/03/15 04:46:29 steve Exp $"
5796524 Add ne_expr.cc
steve authored
21 #endif
22
928df5c include config.h to eliminate warnings.
steve authored
23 # include "config.h"
5796524 Add ne_expr.cc
steve authored
24 # include "netlist.h"
4c67de5 Add the lex_strings string handler, and put
steve authored
25 # include "compiler.h"
28e0616 Use standard name for iostream.
steve authored
26 # include <iostream>
5796524 Add ne_expr.cc
steve authored
27
46253ed Rework expression parsing and elaboration to
steve authored
28 NetExpr::TYPE NetExpr::expr_type() const
29 {
30 return ET_VECTOR;
31 }
32
de94d09 No need to keep excess width from an
steve authored
33 /*
34 * Create an add/sub node from the two operands. Make a best guess of
35 * the
36 */
37 NetEBAdd::NetEBAdd(char op, NetExpr*l, NetExpr*r)
38 : NetEBinary(op, l, r)
39 {
40 NetEConst* tmp;
41
42 /* Catch the special case that one of the operands is an
43 unsized constant number. If so, then we should set the
44 width of that number to the size of the other operand, plus
45 one. This expands the expression to account for the largest
46 possible result.
47
48 The set_width applied to a constant value will only
49 truncate the constant so far as it can still hold its
50 logical value, so this is safe to do. */
51 if ( (tmp = dynamic_cast<NetEConst*>(r))
52 && (! tmp->has_width())
53 && (tmp->expr_width() > l->expr_width()) ) {
54
55 unsigned target_width = l->expr_width() + 1;
56 r->set_width(target_width);
57
58 /* Note: This constant value will not gain a defined
59 with from this. Make sure. */
60 assert(! r->has_width() );
61
62 } else if ( (tmp = dynamic_cast<NetEConst*>(l))
63 && (! tmp->has_width())
64 && (tmp->expr_width() > r->expr_width()) ) {
65
66 unsigned target_width = r->expr_width() + 1;
67 l->set_width(target_width);
68
69 /* Note: This constant value will not gain a defined
70 with from this. Make sure. */
71 assert(! l->has_width() );
72
73 }
74
75 /* Now that we have the operand sizes the way we like, or as
76 good as we are going to get them, set the size of myself. */
77 if (r->expr_width() > l->expr_width()) {
78
79 expr_width(r->expr_width());
80
81 } else {
82 expr_width(l->expr_width());
83 }
84
85 cast_signed(l->has_sign() && r->has_sign());
86 }
87
88 NetEBAdd::~NetEBAdd()
89 {
90 }
91
92 NetEBAdd* NetEBAdd::dup_expr() const
93 {
94 NetEBAdd*result = new NetEBAdd(op_, left_->dup_expr(),
95 right_->dup_expr());
96 return result;
97 }
98
46253ed Rework expression parsing and elaboration to
steve authored
99 NetExpr::TYPE NetEBAdd::expr_type() const
100 {
101 if (left_->expr_type() == ET_REAL)
102 return ET_REAL;
103
104 if (right_->expr_type() == ET_REAL)
105 return ET_REAL;
106
107 return ET_VECTOR;
108 }
109
110 NetEBDiv::NetEBDiv(char op, NetExpr*l, NetExpr*r)
111 : NetEBinary(op, l, r)
112 {
113 unsigned w = l->expr_width();
114 if (r->expr_width() > w)
115 w = r->expr_width();
116
117 expr_width(w);
118 cast_signed(l->has_sign() && r->has_sign());
119 }
120
121 NetEBDiv::~NetEBDiv()
122 {
123 }
124
125 NetEBDiv* NetEBDiv::dup_expr() const
126 {
127 NetEBDiv*result = new NetEBDiv(op_, left_->dup_expr(),
128 right_->dup_expr());
129 return result;
130 }
131
132 NetExpr::TYPE NetEBDiv::expr_type() const
133 {
134 if (left_->expr_type() == ET_REAL)
135 return ET_REAL;
136
137 if (right_->expr_type() == ET_REAL)
138 return ET_REAL;
139
140 return ET_VECTOR;
141 }
142
143 NetEBMult::NetEBMult(char op, NetExpr*l, NetExpr*r)
144 : NetEBinary(op, l, r)
145 {
146 expr_width(l->expr_width() + r->expr_width());
147 cast_signed(l->has_sign() && r->has_sign());
148 }
149
150 NetEBMult::~NetEBMult()
151 {
152 }
153
154 NetEBMult* NetEBMult::dup_expr() const
155 {
156 NetEBMult*result = new NetEBMult(op_, left_->dup_expr(),
157 right_->dup_expr());
158 return result;
159 }
160
161 NetExpr::TYPE NetEBMult::expr_type() const
162 {
163 if (left_->expr_type() == ET_REAL)
164 return ET_REAL;
165
166 if (right_->expr_type() == ET_REAL)
167 return ET_REAL;
168
169 return ET_VECTOR;
170 }
171
8667b9a Put off evaluation of concatenation repeat expresions
steve authored
172 NetEConcat::NetEConcat(unsigned cnt, NetExpr* r)
173 : parms_(cnt), repeat_(r)
174 {
175 if (repeat_ == 0) {
176 repeat_calculated_ = true;
177 repeat_value_ = 1;
178 } else {
179 repeat_calculated_ = false;
180 }
181
182 expr_width(0);
183 }
184
185 NetEConcat::~NetEConcat()
186 {
187 for (unsigned idx = 0 ; idx < parms_.count() ; idx += 1)
188 delete parms_[idx];
189 }
190
aa8869a Postpone parameter width check to evaluation.
steve authored
191 bool NetEConcat::has_width() const
192 {
193 return true;
194 }
195
8667b9a Put off evaluation of concatenation repeat expresions
steve authored
196 void NetEConcat::set(unsigned idx, NetExpr*e)
197 {
198 assert(idx < parms_.count());
199 assert(parms_[idx] == 0);
200 parms_[idx] = e;
201 expr_width( expr_width() + e->expr_width() );
202 }
203
204 NetEConcat* NetEConcat::dup_expr() const
205 {
206 NetEConcat*dup = new NetEConcat(parms_.count(), repeat_);
207 for (unsigned idx = 0 ; idx < parms_.count() ; idx += 1)
208 if (parms_[idx]) {
209 NetExpr*tmp = parms_[idx]->dup_expr();
210 assert(tmp);
211 dup->parms_[idx] = tmp;
212 }
213
214
215 dup->expr_width(expr_width());
216 return dup;
217 }
218
219 unsigned NetEConcat::repeat()
220 {
221 if (repeat_calculated_)
222 return repeat_value_;
223
224 assert(repeat_);
225
226 if (! dynamic_cast<NetEConst*>(repeat_)) {
227 NetExpr*tmp = repeat_->eval_tree();
228 if (tmp != 0) {
229 delete repeat_;
230 repeat_ = tmp;
231 }
232 }
233
234 NetEConst*repeat_const = dynamic_cast<NetEConst*>(repeat_);
235
236 /* This should not be possible, as it was checked earlier to
237 assure that this is a constant expression. */
238 if (repeat_const == 0) {
239 cerr << get_line() << ": internal error: repeat expression "
240 << "is not a compile time constant." << endl;
241 cerr << get_line() << ": : Expression is: "
242 << *repeat_ << endl;
243 repeat_calculated_ = true;
244 repeat_value_ = 1;
245 return 1;
246 }
247
248 repeat_calculated_ = true;
249 repeat_value_ = repeat_const->value().as_ulong();
250
251 delete repeat_;
252 repeat_ = 0;
253
254 return repeat_value_;
255 }
256
257 unsigned NetEConcat::repeat() const
258 {
259 assert(repeat_calculated_);
260 return repeat_value_;
261 }
262
46253ed Rework expression parsing and elaboration to
steve authored
263 NetECReal::NetECReal(const verireal&val)
264 : value_(val)
265 {
266 }
267
268 NetECReal::~NetECReal()
269 {
270 }
271
272 const verireal& NetECReal::value() const
273 {
274 return value_;
275 }
276
589422b Real constants have no defined vector width
steve authored
277 bool NetECReal::has_width() const
278 {
279 return false;
280 }
281
46253ed Rework expression parsing and elaboration to
steve authored
282 NetECReal* NetECReal::dup_expr() const
283 {
284 NetECReal*tmp = new NetECReal(value_);
285 tmp->set_line(*this);
286 return tmp;
287 }
288
289 NetExpr::TYPE NetECReal::expr_type() const
290 {
291 return ET_REAL;
292 }
293
7e1e44e Properly cast signedness of parameters with ranges.
steve authored
294 NetEParam::NetEParam()
4350180 Redo the parameter vector support to allow
steve authored
295 : des_(0), scope_(0)
7e1e44e Properly cast signedness of parameters with ranges.
steve authored
296 {
297 }
298
299 NetEParam::NetEParam(Design*d, NetScope*s, const hname_t&n)
300 : des_(d), scope_(s), name_(n)
301 {
302 }
303
304 NetEParam::~NetEParam()
305 {
306 }
307
308 bool NetEParam::has_width() const
309 {
310 return false;
311 }
312
313 NetEParam* NetEParam::dup_expr() const
314 {
315 return new NetEParam(des_, scope_, name_);
316 }
317
5796524 Add ne_expr.cc
steve authored
318 NetESelect::NetESelect(NetExpr*exp, NetExpr*base, unsigned wid)
319 : expr_(exp), base_(base)
320 {
321 expr_width(wid);
322 }
323
324 NetESelect::~NetESelect()
325 {
326 delete expr_;
327 delete base_;
328 }
329
330 const NetExpr*NetESelect::sub_expr() const
331 {
332 return expr_;
333 }
334
335 const NetExpr*NetESelect::select() const
336 {
337 return base_;
338 }
339
340 bool NetESelect::has_width() const
341 {
342 return true;
343 }
344
345 bool NetESelect::set_width(unsigned w)
346 {
347 if (expr_width() == 1)
348 return true;
349 else
350 return false;
351 }
352
6d94f2e Better organize the NetESFunc return type guesses.
steve authored
353 NetESFunc::NetESFunc(const char*n, NetExpr::TYPE t,
354 unsigned width, unsigned np)
355 : name_(0), type_(t)
04ada23 Support in various contexts the $realtime
steve authored
356 {
4c67de5 Add the lex_strings string handler, and put
steve authored
357 name_ = lex_strings.add(n);
04ada23 Support in various contexts the $realtime
steve authored
358 expr_width(width);
359 nparms_ = np;
360 parms_ = new NetExpr*[np];
361 for (unsigned idx = 0 ; idx < nparms_ ; idx += 1)
362 parms_[idx] = 0;
363 }
364
365 NetESFunc::~NetESFunc()
366 {
367 for (unsigned idx = 0 ; idx < nparms_ ; idx += 1)
368 if (parms_[idx]) delete parms_[idx];
369
370 delete[]parms_;
4c67de5 Add the lex_strings string handler, and put
steve authored
371 /* name_ string ls lex_strings allocated. */
04ada23 Support in various contexts the $realtime
steve authored
372 }
373
374 const char* NetESFunc::name() const
375 {
376 return name_;
377 }
378
379 unsigned NetESFunc::nparms() const
380 {
381 return nparms_;
382 }
383
384 void NetESFunc::parm(unsigned idx, NetExpr*v)
385 {
386 assert(idx < nparms_);
387 if (parms_[idx])
388 delete parms_[idx];
389 parms_[idx] = v;
390 }
391
392 const NetExpr* NetESFunc::parm(unsigned idx) const
393 {
394 assert(idx < nparms_);
395 return parms_[idx];
396 }
397
398 NetExpr* NetESFunc::parm(unsigned idx)
399 {
400 assert(idx < nparms_);
401 return parms_[idx];
402 }
403
404 NetExpr::TYPE NetESFunc::expr_type() const
405 {
6d94f2e Better organize the NetESFunc return type guesses.
steve authored
406 return type_;
04ada23 Support in various contexts the $realtime
steve authored
407 }
408
5796524 Add ne_expr.cc
steve authored
409 /*
410 * $Log: net_expr.cc,v $
6d94f2e Better organize the NetESFunc return type guesses.
steve authored
411 * Revision 1.15 2003/03/15 04:46:29 steve
412 * Better organize the NetESFunc return type guesses.
413 *
4c67de5 Add the lex_strings string handler, and put
steve authored
414 * Revision 1.14 2003/03/01 06:25:30 steve
415 * Add the lex_strings string handler, and put
416 * scope names and system task/function names
417 * into this table. Also, permallocate event
418 * names from the beginning.
419 *
589422b Real constants have no defined vector width
steve authored
420 * Revision 1.13 2003/02/06 17:50:23 steve
421 * Real constants have no defined vector width
422 *
04ada23 Support in various contexts the $realtime
steve authored
423 * Revision 1.12 2003/01/27 00:14:37 steve
424 * Support in various contexts the $realtime
425 * system task.
426 *
46253ed Rework expression parsing and elaboration to
steve authored
427 * Revision 1.11 2003/01/26 21:15:58 steve
428 * Rework expression parsing and elaboration to
429 * accommodate real/realtime values and expressions.
430 *
aa8869a Postpone parameter width check to evaluation.
steve authored
431 * Revision 1.10 2002/11/09 01:40:19 steve
432 * Postpone parameter width check to evaluation.
433 *
de94d09 No need to keep excess width from an
steve authored
434 * Revision 1.9 2002/11/06 02:25:13 steve
435 * No need to keep excess width from an
436 * unsigned constant value, if it can
437 * be trimmed safely.
438 *
4350180 Redo the parameter vector support to allow
steve authored
439 * Revision 1.8 2002/10/19 22:59:49 steve
440 * Redo the parameter vector support to allow
441 * parameter names in range expressions.
442 *
7e1e44e Properly cast signedness of parameters with ranges.
steve authored
443 * Revision 1.7 2002/09/01 03:01:48 steve
444 * Properly cast signedness of parameters with ranges.
445 *
52bf4e6 conditional ident string using autoconfig.
steve authored
446 * Revision 1.6 2002/08/12 01:34:59 steve
447 * conditional ident string using autoconfig.
448 *
28e0616 Use standard name for iostream.
steve authored
449 * Revision 1.5 2002/06/06 18:57:18 steve
450 * Use standard name for iostream.
451 *
8941a59 include iostream for gcc 3.1
steve authored
452 * Revision 1.4 2002/05/25 16:51:37 steve
453 * include iostream for gcc 3.1
454 *
8667b9a Put off evaluation of concatenation repeat expresions
steve authored
455 * Revision 1.3 2002/05/05 21:11:50 steve
456 * Put off evaluation of concatenation repeat expresions
457 * until after parameters are defined. This allows parms
458 * to be used in repeat expresions.
459 *
460 * Add the builtin $signed system function.
461 *
928df5c include config.h to eliminate warnings.
steve authored
462 * Revision 1.2 2002/01/29 22:36:31 steve
463 * include config.h to eliminate warnings.
464 *
5796524 Add ne_expr.cc
steve authored
465 * Revision 1.1 2002/01/28 01:39:45 steve
466 * Add ne_expr.cc
467 *
468 */
469
Something went wrong with that request. Please try again.