Emit VHDL for function declarations

This commit is contained in:
Nick Gasson 2008-06-25 18:00:48 +01:00
parent a3df37b851
commit 43c671cb5c
3 changed files with 53 additions and 7 deletions

View File

@ -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)
{

View File

@ -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<vhdl_decl>(of, scope_.get_decls(), level, ",");
of << ") return ";
type_->emit(of, level);
of << " is";
emit_children<vhdl_decl>(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);
}

View File

@ -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_;
};