Skip to content
Browse files

Support if statements and function calls in constant functions.

  • Loading branch information...
1 parent cec68cf commit 3354d833910627b29b6da608f882464756e6e9bf Martin Whitaker committed with
Showing with 47 additions and 4 deletions.
  1. +5 −1 eval_tree.cc
  2. +37 −3 net_func_eval.cc
  3. +5 −0 netlist.h
View
6 eval_tree.cc
@@ -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;
}
View
40 net_func_eval.cc
@@ -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;
}
}
@@ -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
{
@@ -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;
+}
View
5 netlist.h
@@ -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_;
@@ -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:

0 comments on commit 3354d83

Please sign in to comment.
Something went wrong with that request. Please try again.