Support if statements and function calls in constant functions.

This commit is contained in:
Martin Whitaker 2012-06-05 09:13:06 +01:00 committed by Stephen Williams
parent cec68cfc61
commit 3354d83391
3 changed files with 47 additions and 4 deletions

View File

@ -2001,7 +2001,11 @@ NetExpr* NetEUFunc::eval_tree()
NetFuncDef*def = func_->func_def(); NetFuncDef*def = func_->func_def();
ivl_assert(*this, def); ivl_assert(*this, def);
NetExpr*res = def->evaluate_function(*this, parms_); vector<NetExpr*>args(parms_.size());
for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1)
args[idx] = parms_[idx]->dup_expr();
NetExpr*res = def->evaluate_function(*this, args);
return res; return res;
} }

View File

@ -42,13 +42,12 @@ NetExpr* NetFuncDef::evaluate_function(const LineInfo&loc, const std::vector<Net
// Load the input ports into the map... // Load the input ports into the map...
ivl_assert(loc, ports_.size() == args.size()); ivl_assert(loc, ports_.size() == args.size());
for (size_t idx = 0 ; idx < ports_.size() ; idx += 1) { for (size_t idx = 0 ; idx < ports_.size() ; idx += 1) {
NetExpr*tmp = args[idx]->dup_expr();
perm_string aname = ports_[idx]->name(); perm_string aname = ports_[idx]->name();
context_map[aname] = tmp; context_map[aname] = args[idx];
if (debug_eval_tree) { if (debug_eval_tree) {
cerr << loc.get_fileline() << ": debug: " cerr << loc.get_fileline() << ": debug: "
<< " input " << aname << " = " << *tmp << endl; << " input " << aname << " = " << *args[idx] << endl;
} }
} }
@ -203,6 +202,27 @@ bool NetBlock::evaluate_function(const LineInfo&loc,
return flag; return flag;
} }
bool NetCondit::evaluate_function(const LineInfo&loc,
map<perm_string,NetExpr*>&context_map) const
{
NetExpr*cond = expr_->evaluate_function(loc, context_map);
if (cond == 0)
return false;
NetEConst*cond_const = dynamic_cast<NetEConst*> (cond);
ivl_assert(loc, cond_const);
long val = cond_const->value().as_long();
delete cond;
if (val)
// The condition is true, so evaluate the if clause
return (if_ == 0) || if_->evaluate_function(loc, context_map);
else
// The condition is false, so evaluate the else clause
return (else_ == 0) || else_->evaluate_function(loc, context_map);
}
bool NetWhile::evaluate_function(const LineInfo&loc, bool NetWhile::evaluate_function(const LineInfo&loc,
map<perm_string,NetExpr*>&context_map) const map<perm_string,NetExpr*>&context_map) const
{ {
@ -405,3 +425,17 @@ NetExpr* NetETernary::evaluate_function(const LineInfo&loc,
delete fval; delete fval;
return res; return res;
} }
NetExpr* NetEUFunc::evaluate_function(const LineInfo&loc,
map<perm_string,NetExpr*>&context_map) const
{
NetFuncDef*def = func_->func_def();
ivl_assert(*this, def);
vector<NetExpr*>args(parms_.size());
for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1)
args[idx] = parms_[idx]->evaluate_function(loc, context_map);
NetExpr*res = def->evaluate_function(*this, args);
return res;
}

View File

@ -2671,6 +2671,8 @@ class NetCondit : public NetProc {
virtual int match_proc(struct proc_match_t*); virtual int match_proc(struct proc_match_t*);
virtual void dump(ostream&, unsigned ind) const; virtual void dump(ostream&, unsigned ind) const;
virtual DelayType delay_type() const; virtual DelayType delay_type() const;
virtual bool evaluate_function(const LineInfo&loc,
map<perm_string,NetExpr*>&ctx) const;
private: private:
NetExpr* expr_; NetExpr* expr_;
@ -3225,6 +3227,9 @@ class NetEUFunc : public NetExpr {
virtual NetEUFunc*dup_expr() const; virtual NetEUFunc*dup_expr() const;
virtual NexusSet* nex_input(bool rem_out = true); virtual NexusSet* nex_input(bool rem_out = true);
virtual NetExpr* eval_tree(); virtual NetExpr* eval_tree();
virtual NetExpr*evaluate_function(const LineInfo&loc,
std::map<perm_string,NetExpr*>&ctx) const;
virtual NetNet* synthesize(Design*des, NetScope*scope, NetExpr*root); virtual NetNet* synthesize(Design*des, NetScope*scope, NetExpr*root);
private: private: