diff --git a/vhdlpp/architec_elaborate.cc b/vhdlpp/architec_elaborate.cc index 749e2a0d6..62a368813 100644 --- a/vhdlpp/architec_elaborate.cc +++ b/vhdlpp/architec_elaborate.cc @@ -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(*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; diff --git a/vhdlpp/architec_emit.cc b/vhdlpp/architec_emit.cc index ef17e370a..c344564e0 100644 --- a/vhdlpp/architec_emit.cc +++ b/vhdlpp/architec_emit.cc @@ -103,7 +103,9 @@ int Architecture::emit(ostream&out, Entity*entity) for (map::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::iterator cur = statements_.begin() diff --git a/vhdlpp/expression_elaborate.cc b/vhdlpp/expression_elaborate.cc index 256970085..d16f63db4 100644 --- a/vhdlpp/expression_elaborate.cc +++ b/vhdlpp/expression_elaborate.cc @@ -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(); } diff --git a/vhdlpp/library.cc b/vhdlpp/library.cc index 3ed27b0e2..1eb5fc54f 100644 --- a/vhdlpp/library.cc +++ b/vhdlpp/library.cc @@ -430,13 +430,10 @@ void library_set_work_path(const char*path) library_work_path = path; } +list 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&packages) @@ -447,6 +444,13 @@ static int emit_packages(perm_string, const map&packages) errors += cur->second->emit_package(cout); } + for (list::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; } diff --git a/vhdlpp/package.cc b/vhdlpp/package.cc index 9c0025f47..240eb37a1 100644 --- a/vhdlpp/package.cc +++ b/vhdlpp/package.cc @@ -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::const_iterator cur = cur_subprograms_.begin() + ; cur != cur_subprograms_.end() ; ++cur) { + cur->second->write_to_stream_body(fd); + } + fd << "end " << name_ << ";" << endl; } diff --git a/vhdlpp/package_emit.cc b/vhdlpp/package_emit.cc index f8e631c2f..42ce821a9 100644 --- a/vhdlpp/package_emit.cc +++ b/vhdlpp/package_emit.cc @@ -69,7 +69,9 @@ int Package::emit_package(ostream&fd) const for (map::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; diff --git a/vhdlpp/sequential_elaborate.cc b/vhdlpp/sequential_elaborate.cc index 2244e0f16..29b4e4914 100644 --- a/vhdlpp/sequential_elaborate.cc +++ b/vhdlpp/sequential_elaborate.cc @@ -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(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; } diff --git a/vhdlpp/subprogram.cc b/vhdlpp/subprogram.cc index c5b6d9ee0..8d2f4f814 100644 --- a/vhdlpp/subprogram.cc +++ b/vhdlpp/subprogram.cc @@ -50,82 +50,17 @@ void Subprogram::set_program_body(list*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(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::iterator s = statements_->begin() - ; s != statements_->end(); ++s) { - cast_return_type r(return_type_); - (*s)->visit(r); - } - } -} - -void Subprogram::fix_variables() { - for(std::map::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(type); - - // Currently we handle only one dimensional variables - assert(arr->dimensions() == 1); - - // Now substitute the variable type - VTypeArray*new_array = static_cast(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 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(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::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 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; } } diff --git a/vhdlpp/subprogram.h b/vhdlpp/subprogram.h index 5af77244d..22c49879e 100644 --- a/vhdlpp/subprogram.h +++ b/vhdlpp/subprogram.h @@ -70,34 +70,14 @@ class Subprogram : public LineInfo, public ScopeBase { // for limited length logic vector. Subprogram*make_instance(std::vector 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_;