Skip to content

Commit

Permalink
Support if statements and function calls in constant functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Whitaker authored and steveicarus committed Jun 7, 2012
1 parent cec68cf commit 3354d83
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
6 changes: 5 additions & 1 deletion eval_tree.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,11 @@ NetExpr* NetEUFunc::eval_tree()
NetFuncDef*def = func_->func_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;
}

Expand Down
40 changes: 37 additions & 3 deletions net_func_eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ NetExpr* NetFuncDef::evaluate_function(const LineInfo&loc, const std::vector<Net
// Load the input ports into the map...
ivl_assert(loc, ports_.size() == args.size());
for (size_t idx = 0 ; idx < ports_.size() ; idx += 1) {
NetExpr*tmp = args[idx]->dup_expr();
perm_string aname = ports_[idx]->name();
context_map[aname] = tmp;
context_map[aname] = args[idx];

if (debug_eval_tree) {
cerr << loc.get_fileline() << ": debug: "
<< " input " << aname << " = " << *tmp << endl;
<< " input " << aname << " = " << *args[idx] << endl;
}
}

Expand Down Expand Up @@ -203,6 +202,27 @@ bool NetBlock::evaluate_function(const LineInfo&loc,
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,
map<perm_string,NetExpr*>&context_map) const
{
Expand Down Expand Up @@ -405,3 +425,17 @@ NetExpr* NetETernary::evaluate_function(const LineInfo&loc,
delete fval;
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;
}
5 changes: 5 additions & 0 deletions netlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -2671,6 +2671,8 @@ class NetCondit : public NetProc {
virtual int match_proc(struct proc_match_t*);
virtual void dump(ostream&, unsigned ind) const;
virtual DelayType delay_type() const;
virtual bool evaluate_function(const LineInfo&loc,
map<perm_string,NetExpr*>&ctx) const;

private:
NetExpr* expr_;
Expand Down Expand Up @@ -3225,6 +3227,9 @@ class NetEUFunc : public NetExpr {
virtual NetEUFunc*dup_expr() const;
virtual NexusSet* nex_input(bool rem_out = true);
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);

private:
Expand Down

0 comments on commit 3354d83

Please sign in to comment.