Merge branch 'master' into verilog-ams
[sverilog.git] / PExpr.cc
blob96428204fe0369a20df86094abbf7c1adc9ff645
1 /*
2 * Copyright (c) 1998-2007 Stephen Williams <steve@icarus.com>
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.
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.
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
20 # include "config.h"
22 # include <iostream>
24 # include "PExpr.h"
25 # include "Module.h"
26 # include <typeinfo>
28 PExpr::PExpr()
32 PExpr::~PExpr()
36 bool PExpr::is_the_same(const PExpr*that) const
38 return typeid(this) == typeid(that);
41 bool PExpr::is_constant(Module*) const
43 return false;
46 NetNet* PExpr::elaborate_lnet(Design*des, NetScope*) const
48 cerr << get_fileline() << ": error: expression not valid in assign l-value: "
49 << *this << endl;
50 return 0;
53 NetNet* PExpr::elaborate_bi_net(Design*des, NetScope*) const
55 cerr << get_fileline() << ": error: "
56 << "expression not valid as argument to inout port: "
57 << *this << endl;
58 return 0;
61 PEBinary::PEBinary(char op, PExpr*l, PExpr*r)
62 : op_(op), left_(l), right_(r)
66 PEBinary::~PEBinary()
70 bool PEBinary::is_constant(Module*mod) const
72 return left_->is_constant(mod) && right_->is_constant(mod);
75 PEBComp::PEBComp(char op, PExpr*l, PExpr*r)
76 : PEBinary(op, l, r)
80 PEBComp::~PEBComp()
84 PEBShift::PEBShift(char op, PExpr*l, PExpr*r)
85 : PEBinary(op, l, r)
89 PEBShift::~PEBShift()
93 PECallFunction::PECallFunction(const pform_name_t&n, const svector<PExpr *> &parms)
94 : path_(n), parms_(parms)
98 static pform_name_t pn_from_ps(perm_string n)
100 name_component_t tmp_name (n);
101 pform_name_t tmp;
102 tmp.push_back(tmp_name);
103 return tmp;
106 PECallFunction::PECallFunction(perm_string n, const svector<PExpr*>&parms)
107 : path_(pn_from_ps(n)), parms_(parms)
111 PECallFunction::PECallFunction(perm_string n)
112 : path_(pn_from_ps(n))
116 PECallFunction::~PECallFunction()
120 PEConcat::PEConcat(const svector<PExpr*>&p, PExpr*r)
121 : parms_(p), repeat_(r)
125 bool PEConcat::is_constant(Module *mod) const
127 bool constant = repeat_? repeat_->is_constant(mod) : true;
128 for (unsigned i = 0; constant && i < parms_.count(); ++i) {
129 constant = constant && parms_[i]->is_constant(mod);
131 return constant;
134 PEConcat::~PEConcat()
136 delete repeat_;
139 PEEvent::PEEvent(PEEvent::edge_t t, PExpr*e)
140 : type_(t), expr_(e)
144 PEEvent::~PEEvent()
148 PEEvent::edge_t PEEvent::type() const
150 return type_;
153 PExpr* PEEvent::expr() const
155 return expr_;
158 PEFNumber::PEFNumber(verireal*v)
159 : value_(v)
163 PEFNumber::~PEFNumber()
165 delete value_;
168 const verireal& PEFNumber::value() const
170 return *value_;
173 bool PEFNumber::is_constant(Module*) const
175 return true;
178 PEIdent::PEIdent(const pform_name_t&that)
179 : path_(that)
183 PEIdent::PEIdent(perm_string s)
185 path_.push_back(name_component_t(s));
188 PEIdent::~PEIdent()
193 * An identifier can be in a constant expression if (and only if) it is
194 * a parameter or genvar.
196 * NOTE: This test does not work if the name is hierarchical!
198 bool PEIdent::is_constant(Module*mod) const
200 if (mod == 0) return false;
202 /* */
203 perm_string tmp = path_.back().name;
205 { map<perm_string,Module::param_expr_t>::const_iterator cur;
206 cur = mod->parameters.find(tmp);
207 if (cur != mod->parameters.end()) return true;
210 { map<perm_string,Module::param_expr_t>::const_iterator cur;
211 cur = mod->localparams.find(tmp);
212 if (cur != mod->localparams.end()) return true;
215 { map<perm_string,LineInfo*>::const_iterator cur;
216 cur = mod->genvars.find(tmp);
217 if (cur != mod->genvars.end()) return true;
220 return false;
223 PENumber::PENumber(verinum*vp)
224 : value_(vp)
226 assert(vp);
229 PENumber::~PENumber()
231 delete value_;
234 const verinum& PENumber::value() const
236 return *value_;
239 bool PENumber::is_the_same(const PExpr*that) const
241 const PENumber*obj = dynamic_cast<const PENumber*>(that);
242 if (obj == 0)
243 return false;
245 return *value_ == *obj->value_;
248 bool PENumber::is_constant(Module*) const
250 return true;
253 PEString::PEString(char*s)
254 : text_(s)
258 PEString::~PEString()
260 delete[]text_;
263 string PEString::value() const
265 return text_;
268 bool PEString::is_constant(Module*) const
270 return true;
273 PETernary::PETernary(PExpr*e, PExpr*t, PExpr*f)
274 : expr_(e), tru_(t), fal_(f)
278 PETernary::~PETernary()
282 bool PETernary::is_constant(Module*m) const
284 return expr_->is_constant(m)
285 && tru_->is_constant(m)
286 && fal_->is_constant(m);
289 PEUnary::PEUnary(char op, PExpr*ex)
290 : op_(op), expr_(ex)
294 PEUnary::~PEUnary()
298 bool PEUnary::is_constant(Module*m) const
300 return expr_->is_constant(m);