From c8cbac58f5820cc28cb7bf5666bdeb6f69b585aa Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Sun, 3 Aug 2008 10:50:31 +0100 Subject: [PATCH] Add forward declarations for functions This patch adds a forward declaration for every user funciton. This fixes VHDL compile problems if a function calls another before it has been declared. --- tgt-vhdl/scope.cc | 5 +++++ tgt-vhdl/vhdl_syntax.cc | 15 +++++++++++++++ tgt-vhdl/vhdl_syntax.hh | 12 ++++++++++++ 3 files changed, 32 insertions(+) diff --git a/tgt-vhdl/scope.cc b/tgt-vhdl/scope.cc index cfd74f611..d3c3d9ea7 100644 --- a/tgt-vhdl/scope.cc +++ b/tgt-vhdl/scope.cc @@ -528,6 +528,11 @@ static int draw_function(ivl_scope_t scope, ivl_scope_t parent) draw_stmt(func, func->get_container(), ivl_scope_def(scope)); } set_active_entity(NULL); + + // Add a forward declaration too in case it is called by + // another function that has already been added + ent->get_arch()->get_scope()->add_forward_decl + (new vhdl_forward_fdecl(func)); ent->get_arch()->get_scope()->add_decl(func); return 0; diff --git a/tgt-vhdl/vhdl_syntax.cc b/tgt-vhdl/vhdl_syntax.cc index 45595deba..dd9a365bb 100644 --- a/tgt-vhdl/vhdl_syntax.cc +++ b/tgt-vhdl/vhdl_syntax.cc @@ -48,6 +48,11 @@ void vhdl_scope::add_decl(vhdl_decl *decl) decls_.push_back(decl); } +void vhdl_scope::add_forward_decl(vhdl_decl *decl) +{ + decls_.push_front(decl); +} + vhdl_decl *vhdl_scope::get_decl(const std::string &name) const { decl_list_t::const_iterator it; @@ -831,6 +836,16 @@ void vhdl_function::emit(std::ostream &of, int level) const of << "end function;"; } +void vhdl_forward_fdecl::emit(std::ostream &of, int level) const +{ + of << "function " << f_->get_name() << " ("; + emit_children(of, f_->scope_.get_decls(), level, ";"); + of << ") "; + newline(of, level); + of << "return " << f_->type_->get_string() << ";"; + newline(of, level); +} + void vhdl_param_decl::emit(std::ostream &of, int level) const { of << name_ << " : "; diff --git a/tgt-vhdl/vhdl_syntax.hh b/tgt-vhdl/vhdl_syntax.hh index 9a3effaa6..c8cbf8c07 100644 --- a/tgt-vhdl/vhdl_syntax.hh +++ b/tgt-vhdl/vhdl_syntax.hh @@ -608,6 +608,7 @@ public: ~vhdl_scope(); void add_decl(vhdl_decl *decl); + void add_forward_decl(vhdl_decl *decl); vhdl_decl *get_decl(const std::string &name) const; bool have_declared(const std::string &name) const; bool contained_within(const vhdl_scope *other) const; @@ -647,6 +648,7 @@ protected: class vhdl_function : public vhdl_decl, public vhdl_procedural { + friend class vhdl_forward_fdecl; public: vhdl_function(const char *name, vhdl_type *ret_type); @@ -657,6 +659,16 @@ private: vhdl_scope variables_; }; +class vhdl_forward_fdecl : public vhdl_decl { +public: + vhdl_forward_fdecl(const vhdl_function *f) + : vhdl_decl((f->get_name() + "_Forward").c_str()), f_(f) {} + + void emit(std::ostream &of, int level) const; +private: + const vhdl_function *f_; +}; + class vhdl_process : public vhdl_conc_stmt, public vhdl_procedural { public: