From 43c671cb5c8757145c4daf63b3af33e145950ebc Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Wed, 25 Jun 2008 18:00:48 +0100 Subject: [PATCH] Emit VHDL for function declarations --- tgt-vhdl/scope.cc | 9 +++++++-- tgt-vhdl/vhdl_syntax.cc | 29 +++++++++++++++++++++++++++++ tgt-vhdl/vhdl_syntax.hh | 22 +++++++++++++++++----- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/tgt-vhdl/scope.cc b/tgt-vhdl/scope.cc index 237b77d64..049ab1141 100644 --- a/tgt-vhdl/scope.cc +++ b/tgt-vhdl/scope.cc @@ -377,9 +377,14 @@ int draw_function(ivl_scope_t scope, ivl_scope_t parent) vhdl_entity *ent = find_entity(ivl_scope_tname(parent)); assert(ent); - return 1; -} + const char *funcname = ivl_scope_tname(scope); + vhdl_function *func = new vhdl_function(funcname, vhdl_type::std_logic()); + + ent->get_arch()->get_scope()->add_decl(func); + + return 0; +} int draw_scope(ivl_scope_t scope, void *_parent) { diff --git a/tgt-vhdl/vhdl_syntax.cc b/tgt-vhdl/vhdl_syntax.cc index 921f15964..d8bdb95ed 100644 --- a/tgt-vhdl/vhdl_syntax.cc +++ b/tgt-vhdl/vhdl_syntax.cc @@ -751,3 +751,32 @@ void vhdl_while_stmt::emit(std::ofstream &of, int level) const stmts_.emit(of, level); of << "end loop;"; } + +vhdl_function::vhdl_function(const char *name, vhdl_type *ret_type) + : vhdl_decl(name, ret_type) +{ + // A function contains two scopes: + // scope_ = The paramters + // variables_ = Local variables + // A call to get_scope returns variables_ whose parent is scope_ + variables_.set_parent(&scope_); +} + +void vhdl_function::emit(std::ofstream &of, int level) const +{ + of << "function " << name_ << " ("; + emit_children(of, scope_.get_decls(), level, ","); + of << ") return "; + type_->emit(of, level); + of << " is"; + emit_children(of, variables_.get_decls(), level); + of << "begin"; + stmts_.emit(of, level); + of << "end function;"; +} + +void vhdl_param_decl::emit(std::ofstream &of, int level) const +{ + of << name_ << " : "; + type_->emit(of, level); +} diff --git a/tgt-vhdl/vhdl_syntax.hh b/tgt-vhdl/vhdl_syntax.hh index 742a38f5d..463a637b4 100644 --- a/tgt-vhdl/vhdl_syntax.hh +++ b/tgt-vhdl/vhdl_syntax.hh @@ -467,6 +467,16 @@ public: }; +/* + * A parameter to a function. + */ +class vhdl_param_decl : public vhdl_decl { +public: + vhdl_param_decl(const char *name, vhdl_type *type) + : vhdl_decl(name, type) {} + void emit(std::ofstream &of, int level) const; +}; + enum vhdl_port_mode_t { VHDL_PORT_IN, VHDL_PORT_OUT, @@ -553,8 +563,8 @@ class vhdl_procedural { public: virtual ~vhdl_procedural() {} - stmt_container *get_container() { return &stmts_; } - vhdl_scope *get_scope() { return &scope_; } + virtual stmt_container *get_container() { return &stmts_; } + virtual vhdl_scope *get_scope() { return &scope_; } protected: stmt_container stmts_; vhdl_scope scope_; @@ -563,11 +573,13 @@ protected: class vhdl_function : public vhdl_decl, public vhdl_procedural { public: - vhdl_function(const char *name, vhdl_type *ret_type) - : vhdl_decl(name, ret_type) {} - + vhdl_function(const char *name, vhdl_type *ret_type); + void emit(std::ofstream &of, int level) const; + vhdl_scope *get_scope() { return &variables_; } + void add_param(vhdl_param_decl *p) { scope_.add_decl(p); } private: + vhdl_scope variables_; };