From 3c437874e208b50bb35f07ca7683817431f1e01e Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 9 Jun 2015 10:59:33 +0200 Subject: [PATCH] vhdlpp: Allow initializers for variables. --- vhdlpp/parse.y | 14 ++++++++++---- vhdlpp/vsignal.cc | 8 +++++++- vhdlpp/vsignal.h | 6 +++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/vhdlpp/parse.y b/vhdlpp/parse.y index 39061ffbb..29512b178 100644 --- a/vhdlpp/parse.y +++ b/vhdlpp/parse.y @@ -331,8 +331,8 @@ static void touchup_interface_for_functions(std::list*ports) %type expression_logical_xnor expression_logical_xor %type name prefix selected_name %type shift_expression signal_declaration_assign_opt -%type simple_expression simple_expression_2 term waveform_element -%type interface_element_expression +%type simple_expression simple_expression_2 term +%type variable_declaration_assign_opt waveform_element interface_element_expression %type waveform waveform_elements %type name_list expression_list @@ -2661,11 +2661,12 @@ variable_assignment ; variable_declaration /* IEEE 1076-2008 P6.4.2.4 */ - : K_shared_opt K_variable identifier_list ':' subtype_indication ';' + : K_shared_opt K_variable identifier_list ':' subtype_indication + variable_declaration_assign_opt ';' { /* Save the signal declaration in the block_signals map. */ for (std::list::iterator cur = $3->begin() ; cur != $3->end() ; ++cur) { - Variable*sig = new Variable(*cur, $5); + Variable*sig = new Variable(*cur, $5, $6); FILE_NAME(sig, @2); active_scope->bind_name(*cur, sig); } @@ -2677,6 +2678,11 @@ variable_declaration /* IEEE 1076-2008 P6.4.2.4 */ } ; +variable_declaration_assign_opt + : VASSIGN expression { $$ = $2; } + | { $$ = 0; } + ; + wait_statement : K_wait K_for expression ';' { WaitForStmt*tmp = new WaitForStmt($3); diff --git a/vhdlpp/vsignal.cc b/vhdlpp/vsignal.cc index 043c2b1a9..38516dbaf 100644 --- a/vhdlpp/vsignal.cc +++ b/vhdlpp/vsignal.cc @@ -66,7 +66,7 @@ int Signal::emit(ostream&out, Entity*ent, ScopeBase*scope) return errors; } -int Variable::emit(ostream&out, Entity*, ScopeBase*) +int Variable::emit(ostream&out, Entity*ent, ScopeBase*scope) { int errors = 0; @@ -75,6 +75,12 @@ int Variable::emit(ostream&out, Entity*, ScopeBase*) if (peek_refcnt_sequ_() > 0 || !peek_type()->can_be_packed()) decl.reg_flag = true; errors += decl.emit(out, peek_name()); + + Expression*init_expr = peek_init_expr(); + if (init_expr) { + out << " = "; + init_expr->emit(out, ent, scope); + } out << ";" << endl; return errors; } diff --git a/vhdlpp/vsignal.h b/vhdlpp/vsignal.h index d2bfdbcd0..6aa225d14 100644 --- a/vhdlpp/vsignal.h +++ b/vhdlpp/vsignal.h @@ -77,7 +77,7 @@ class Signal : public SigVarBase { class Variable : public SigVarBase { public: - Variable(perm_string name, const VType*type); + Variable(perm_string name, const VType*type, Expression*init_expr = NULL); int emit(ostream&out, Entity*ent, ScopeBase*scope); void write_to_stream(std::ostream&fd); @@ -93,8 +93,8 @@ inline Signal::Signal(perm_string name, const VType*type, Expression*init_expr) { } -inline Variable::Variable(perm_string name, const VType*type) -: SigVarBase(name, type, 0) +inline Variable::Variable(perm_string name, const VType*type, Expression*init_expr) +: SigVarBase(name, type, init_expr) { }