vhdlpp: Alternative way of dealing with unbounded vectors in fuctions (instances).

This commit is contained in:
Maciej Suminski 2015-02-04 14:29:41 +01:00
parent 5349ca9a55
commit 19ff6a434b
9 changed files with 38 additions and 129 deletions

View File

@ -373,11 +373,6 @@ int SignalAssignment::elaborate(Entity*ent, Architecture*arc)
(*cur)->elaborate_expr(ent, arc, lval_type);
// Handle functions that return unbounded arrays
if(ExpFunc*call = dynamic_cast<ExpFunc*>(*cur)) {
const VType*ret_type = call->func_ret_type();
if(ret_type && ret_type->is_unbounded())
*cur = new ExpCast(*cur, get_global_typedef(lval_type));
}
}
return errors;

View File

@ -103,7 +103,9 @@ int Architecture::emit(ostream&out, Entity*entity)
for (map<perm_string,Subprogram*>::const_iterator cur = cur_subprograms_.begin()
; cur != cur_subprograms_.end() ; ++ cur) {
errors += cur->second->emit_package(out);
// Do not emit unbounded functions, we will just need fixed instances later
if(!cur->second->unbounded())
errors += cur->second->emit_package(out);
}
for (list<Architecture::Statement*>::iterator cur = statements_.begin()

View File

@ -757,8 +757,6 @@ int ExpFunc::elaborate_expr(Entity*ent, ScopeBase*scope, const VType*)
ivl_assert(*this, def_==0);
def_ = prog;
bool new_instance = false;
// Elaborate arguments
for (size_t idx = 0 ; idx < argv_.size() ; idx += 1) {
const VType*tmp = argv_[idx]->probe_type(ent, scope);
@ -768,14 +766,9 @@ int ExpFunc::elaborate_expr(Entity*ent, ScopeBase*scope, const VType*)
tmp = param_type;
errors += argv_[idx]->elaborate_expr(ent, scope, tmp);
// Type casting for unbounded arrays
if(param_type && param_type->is_unbounded() /*&& !param_type->type_match(tmp)*/) {
new_instance = true; // we need a new instance
}
}
if(new_instance) {
if(def_ && def_->unbounded()) {
def_ = prog->make_instance(argv_, scope);
name_ = def_->name();
}

View File

@ -430,13 +430,10 @@ void library_set_work_path(const char*path)
library_work_path = path;
}
list<const Package*> work_packages;
static void store_package_in_work(const Package*pack)
{
string path = make_work_package_path(pack->name());
ofstream file (path.c_str(), ios_base::out);
pack->write_to_stream(file);
work_packages.push_back(pack);
}
static int emit_packages(perm_string, const map<perm_string,Package*>&packages)
@ -447,6 +444,13 @@ static int emit_packages(perm_string, const map<perm_string,Package*>&packages)
errors += cur->second->emit_package(cout);
}
for (list<const Package*>::const_iterator cur = work_packages.begin()
; cur != work_packages.end(); ++cur) {
string path = make_work_package_path((*cur)->name());
ofstream file (path.c_str(), ios_base::out);
(*cur)->write_to_stream(file);
}
return errors;
}

View File

@ -130,4 +130,11 @@ void Package::write_to_stream(ostream&fd) const
}
fd << "end package " << name_ << ";" << endl;
fd << "package body " << name_ << " is" << endl;
for (map<perm_string,Subprogram*>::const_iterator cur = cur_subprograms_.begin()
; cur != cur_subprograms_.end() ; ++cur) {
cur->second->write_to_stream_body(fd);
}
fd << "end " << name_ << ";" << endl;
}

View File

@ -69,7 +69,9 @@ int Package::emit_package(ostream&fd) const
for (map<perm_string,Subprogram*>::const_iterator cur = cur_subprograms_.begin()
; cur != cur_subprograms_.end() ; ++ cur) {
errors += cur->second->emit_package(fd);
// Do not emit unbounded functions, we will just need fixed instances later
if(!cur->second->unbounded())
errors += cur->second->emit_package(fd);
}
fd << "endpackage" << endl;

View File

@ -179,13 +179,6 @@ int VariableSeqAssignment::elaborate(Entity*ent, ScopeBase*scope)
// Elaborate the r-value expression.
errors += rval_->elaborate_expr(ent, scope, lval_type);
// Handle functions that return unbounded arrays
if(ExpFunc*call = dynamic_cast<ExpFunc*>(rval_)) {
const VType*ret_type = call->func_ret_type();
if(ret_type && ret_type->is_unbounded())
rval_ = new ExpCast(rval_, get_global_typedef(lval_type));
}
return errors;
}

View File

@ -50,82 +50,17 @@ void Subprogram::set_program_body(list<SequentialStmt*>*stmt)
{
ivl_assert(*this, statements_==0);
statements_ = stmt;
fix_port_types();
}
// Functor used to add type casting to each return statement.
struct cast_return_type : public SeqStmtVisitor {
cast_return_type(const VType*ret_type) : ret_(ret_type) {}
bool Subprogram::unbounded() const {
if(return_type_->is_unbounded())
return true;
void operator() (SequentialStmt*s)
{
ReturnStmt*ret;
if((ret = dynamic_cast<ReturnStmt*>(s))) {
ret->cast_to(ret_);
}
}
private:
const VType*ret_;
};
void Subprogram::fix_port_types()
{
// Check function parameters for unbounded vectors and possibly fix it.
// Try to settle at a fixed width return type.
if(fixed_return_type())
return;
// Check if the returned type is an unbounded vector.
if(check_unb_vector(return_type_)) {
if(!statements_)
return;
// Go through the statement list and add type casting to return
// statements to comply with the modified return type.
for (std::list<SequentialStmt*>::iterator s = statements_->begin()
; s != statements_->end(); ++s) {
cast_return_type r(return_type_);
(*s)->visit(r);
}
}
}
void Subprogram::fix_variables() {
for(std::map<perm_string, Variable*>::iterator it = new_variables_.begin(); it != new_variables_.end(); ++it) {
Variable*var = it->second;
const VType*type = var->peek_type();
if(type->is_variable_length(this)) {
const VTypeArray*arr = dynamic_cast<const VTypeArray*>(type);
// Currently we handle only one dimensional variables
assert(arr->dimensions() == 1);
// Now substitute the variable type
VTypeArray*new_array = static_cast<VTypeArray*>(arr->clone());
new_array->evaluate_ranges(this);
it->second = new Variable(var->peek_name(), new_array);
delete var;
}
}
}
VTypeArray*Subprogram::fix_logic_darray(const VTypeArray*type)
{
Expression*zero = new ExpInteger(0);
std::vector<VTypeArray::range_t> sub_range;
sub_range.push_back(VTypeArray::range_t(zero, zero));
return new VTypeArray(type, sub_range);
}
bool Subprogram::check_unb_vector(const VType*&type)
{
if(const VTypeArray*arr = dynamic_cast<const VTypeArray*>(type)) {
if(arr->dimensions() == 1 && arr->dimension(0).is_box() ) {
type = get_global_typedef(fix_logic_darray(arr));
return true;
if(ports_) {
for(std::list<InterfacePort*>::const_iterator it = ports_->begin();
it != ports_->end(); ++it) {
if((*it)->type->is_unbounded())
return true;
}
}
@ -218,6 +153,7 @@ Subprogram*Subprogram::make_instance(std::vector<Expression*> arguments, ScopeBa
instance->set_parent(scope);
instance->set_program_body(statements_);
instance->fix_return_type();
scope->bind_subprogram(new_name, instance);
return instance;
@ -263,10 +199,10 @@ private:
const VType*ret_type_;
};
bool Subprogram::fixed_return_type(void)
void Subprogram::fix_return_type()
{
if(!statements_)
return false;
return;
check_return_type r(this);
@ -284,9 +220,6 @@ bool Subprogram::fixed_return_type(void)
arr->evaluate_ranges(this);
}
return_type_ = return_type;
return true;
} else {
return false;
}
}

View File

@ -70,34 +70,14 @@ class Subprogram : public LineInfo, public ScopeBase {
// for limited length logic vector.
Subprogram*make_instance(std::vector<Expression*> arguments, ScopeBase*scope);
// Checks if either return type or parameters are unbounded vectors.
bool unbounded() const;
private:
// Tries to set the return type to a fixed type. VHDL functions that
// return std_logic_vectors do not specify its length, as SystemVerilog
// demands.
// The function goes through the function body looking for return
// statments and probes the returned type. If it is the same for every
// statemnt then we can assume that the function returns vector of a
// fixed size.
bool fixed_return_type();
// Iterates through the list of function ports to fix all quirks related
// to translation between VHDL and SystemVerilog.
void fix_port_types();
// SystemVerilog does not allow to have signals/variables which size is
// evaluated at runtime. This function finds such variables and modifies
// their type to dynamic array and adds appropriate 'new' statement in
// the program body.
void fix_variables();
// For the time being, dynamic arrays work exclusively with vectors.
// To emulate dynamic array of 'logic'/'bit' type, we need to create a vector
// of width == 1, to be used as the array element type.
// Effectively 'logic name []' becomes 'logic [0:0] name []'.
VTypeArray*fix_logic_darray(const VTypeArray*type);
// Creates a typedef for an unbounded vector and updates the given type.
bool check_unb_vector(const VType*&type);
void fix_return_type();
perm_string name_;
const ScopeBase*parent_;