diff --git a/grammars/vhdlParser.g4 b/grammars/vhdlParser.g4 index 766890ec..ad56ff72 100644 --- a/grammars/vhdlParser.g4 +++ b/grammars/vhdlParser.g4 @@ -400,8 +400,7 @@ record_element_resolution: identifier resolution_indication; type_mark: name; constraint: range_constraint - | array_constraint - | record_constraint + | element_constraint ; element_constraint: array_constraint diff --git a/include/hdlConvertor/vhdlConvertor/typeDeclarationParser.h b/include/hdlConvertor/vhdlConvertor/typeDeclarationParser.h index 50628308..751d6059 100644 --- a/include/hdlConvertor/vhdlConvertor/typeDeclarationParser.h +++ b/include/hdlConvertor/vhdlConvertor/typeDeclarationParser.h @@ -16,15 +16,20 @@ class VhdlTypeDeclarationParser { static std::unique_ptr visitConstraint( std::unique_ptr selectedName, vhdlParser::ConstraintContext *ctx); - static std::pair, - std::vector> > visitArray_constraint( + static std::unique_ptr visitArray_constraint( + std::unique_ptr selectedName, vhdlParser::Array_constraintContext *ctx); - static std::vector> visitIndex_constraint( + static std::unique_ptr visitIndex_constraint( + std::unique_ptr selectedName, vhdlParser::Index_constraintContext *ctx); static std::unique_ptr visitRecord_constraint( vhdlParser::Record_constraintContext *ctx); static std::unique_ptr visitArray_element_constraint( + std::unique_ptr selectedName, vhdlParser::Array_element_constraintContext *ctx); + static std::unique_ptr visitElement_constraint( + std::unique_ptr selectedName, + vhdlParser::Element_constraintContext *ctx); static std::unique_ptr visitType_declaration( vhdlParser::Type_declarationContext *ctx); diff --git a/src/vhdlConvertor/typeDeclarationParser.cpp b/src/vhdlConvertor/typeDeclarationParser.cpp index aa6b2d33..5052cc39 100644 --- a/src/vhdlConvertor/typeDeclarationParser.cpp +++ b/src/vhdlConvertor/typeDeclarationParser.cpp @@ -81,8 +81,8 @@ unique_ptr VhdlTypeDeclarationParser::visitType_definition( } } } - return create_object(ctx, move(name), - HdlValueSymbol::type(), move(t)); + return create_object(ctx, move(name), HdlValueSymbol::type(), + move(t)); } unique_ptr VhdlTypeDeclarationParser::visitScalar_type_definition( @@ -167,56 +167,55 @@ unique_ptr VhdlTypeDeclarationParser::visitEnumeration_type_definiti } unique_ptr VhdlTypeDeclarationParser::visitRange_constraint( - vhdlParser::Range_constraintContext *ctx) { - // range_constraint - // : RANGE range - // ; - auto op = VhdlExprParser::visitRange(ctx->range()); - return create_object(ctx, HdlOpType::RANGE, move(op)); + vhdlParser::Range_constraintContext *ctx) { + // range_constraint + // : RANGE range + // ; + return VhdlExprParser::visitRange(ctx->range()); } unique_ptr VhdlTypeDeclarationParser::visitInteger_type_definition( vhdlParser::Integer_type_definitionContext *ctx) { - // integer_type_definition: range_constraint; - auto r = ctx->range_constraint(); - return visitRange_constraint(r); + // integer_type_definition: range_constraint; + auto r = ctx->range_constraint(); + auto rc = visitRange_constraint(r); + return create_object(ctx, HdlOpType::RANGE, move(rc)); } unique_ptr VhdlTypeDeclarationParser::visitFloating_type_definition( vhdlParser::Floating_type_definitionContext *ctx) { - // floating_type_definition: range_constraint; - auto r = ctx->range_constraint(); - return visitRange_constraint(r); + // floating_type_definition: range_constraint; + auto r = ctx->range_constraint(); + auto rc = visitRange_constraint(r); + return create_object(ctx, HdlOpType::RANGE, move(rc)); } unique_ptr VhdlTypeDeclarationParser::visitPhysical_type_definition( vhdlParser::Physical_type_definitionContext *ctx) { - // physical_type_definition: - // range_constraint - // KW_UNITS - // primary_unit_declaration - // ( secondary_unit_declaration )* - // KW_END KW_UNITS ( identifier )? - // ; - auto r = ctx->range_constraint(); - // range_constraint - // : RANGE range - // ; - auto rop = VhdlExprParser::visitRange(r->range()); - - auto pdecl = make_unique(); - pdecl->range = create_object(ctx, HdlOpType::RANGE, move(rop)); - // primary_unit_declaration: identifier SEMI; - auto _pu = ctx->primary_unit_declaration()->identifier(); - auto pu = VhdlLiteralParser::getIdentifierStr(_pu); - pdecl->members.push_back( { pu, nullptr }); - // secondary_unit_declaration: identifier EQ physical_literal SEMI; + // physical_type_definition: + // range_constraint + // KW_UNITS + // primary_unit_declaration + // ( secondary_unit_declaration )* + // KW_END KW_UNITS ( identifier )? + // ; + auto r = ctx->range_constraint(); + auto rop = visitRange_constraint(r); + + auto pdecl = make_unique(); + pdecl->range = create_object(ctx, HdlOpType::RANGE, move(rop)); + // primary_unit_declaration: identifier SEMI; + auto _pu = ctx->primary_unit_declaration()->identifier(); + auto pu = VhdlLiteralParser::getIdentifierStr(_pu); + pdecl->members.push_back( { pu, nullptr }); + // secondary_unit_declaration: identifier EQ physical_literal SEMI; for (auto uit : ctx->secondary_unit_declaration()) { - auto _su = uit->identifier(); - auto su = VhdlLiteralParser::getIdentifierStr(_su); - auto val = VhdlLiteralParser::visitPhysical_literal(uit->physical_literal()); - pdecl->members.push_back( { su, move(val) }); - } + auto _su = uit->identifier(); + auto su = VhdlLiteralParser::getIdentifierStr(_su); + auto val = VhdlLiteralParser::visitPhysical_literal( + uit->physical_literal()); + pdecl->members.push_back( { su, move(val) }); + } return pdecl; } @@ -263,12 +262,7 @@ unique_ptr VhdlTypeDeclarationParser::visitConstrained_array_defin // KW_ARRAY index_constraint KW_OF subtype_indication //; auto element_t = visitSubtype_indication(ctx->subtype_indication()); - auto tdef = make_unique(HdlOpType::INDEX, move(element_t)); - auto indexes = visitIndex_constraint(ctx->index_constraint()); - for (auto &i : indexes) { - tdef->operands.push_back(move(i)); - } - return tdef; + return visitIndex_constraint(move(element_t), ctx->index_constraint()); } unique_ptr VhdlTypeDeclarationParser::visitRecord_type_definition( @@ -332,8 +326,8 @@ unique_ptr VhdlTypeDeclarationParser::visitSubtype_indication( e = visitConstraint(move(e), c); } if (ri) { - return create_object(ctx, move(ri), - HdlOpType::DEFINE_RESOLVER, move(e)); + return create_object(ctx, move(ri), HdlOpType::DEFINE_RESOLVER, + move(e)); } else { return e; } @@ -342,45 +336,66 @@ unique_ptr VhdlTypeDeclarationParser::visitSubtype_indication( /* * @return element_type, dimension indexes * */ -pair, vector>> VhdlTypeDeclarationParser::visitArray_constraint( +unique_ptr VhdlTypeDeclarationParser::visitArray_constraint( + unique_ptr selectedName, vhdlParser::Array_constraintContext *ctx) { // array_constraint: // index_constraint ( array_element_constraint )? // | LPAREN OPEN RPAREN ( array_element_constraint )? // ; auto ic = ctx->index_constraint(); + unique_ptr res; + if (ic) { + res = visitIndex_constraint(move(selectedName), ic); + } else { + res = create_object(ctx, move(selectedName), HdlOpType::INDEX, + HdlValueSymbol::all()); + } auto aec = ctx->array_element_constraint(); - unique_ptr elem_t = nullptr; if (aec) { - elem_t = visitArray_element_constraint(aec); - } - vector> e; - if (ic) { - e = visitIndex_constraint(ic); + res = visitArray_element_constraint(move(res), aec); } - return {move(elem_t), move(e)}; + + return res; } -vector> VhdlTypeDeclarationParser::visitIndex_constraint( + +unique_ptr VhdlTypeDeclarationParser::visitIndex_constraint( + unique_ptr selectedName, vhdlParser::Index_constraintContext *ctx) { // index_constraint // : LPAREN discrete_range ( COMMA discrete_range )* RPAREN // ; - vector> res; + unique_ptr res = move(selectedName); + vector> indexes; for (auto dr_ctx : ctx->discrete_range()) { - res.push_back(VhdlExprParser::visitDiscrete_range(dr_ctx)); + indexes.push_back(VhdlExprParser::visitDiscrete_range(dr_ctx)); } + res = HdlOp::index(ctx, move(res), indexes); return res; } + unique_ptr VhdlTypeDeclarationParser::visitArray_element_constraint( + unique_ptr selectedName, vhdlParser::Array_element_constraintContext *ctx) { // array_element_constraint: element_constraint; + return visitElement_constraint(move(selectedName), + ctx->element_constraint()); +} + +unique_ptr VhdlTypeDeclarationParser::visitElement_constraint( + unique_ptr selectedName, + vhdlParser::Element_constraintContext *ctx) { // element_constraint: // array_constraint // | record_constraint // ; - NotImplementedLogger::print( - "VhdlTypeDeclarationParser.visitArray_element_constraint", ctx); - return create_object(ctx); + auto i = ctx->array_constraint(); + if (i) { + return visitArray_constraint(move(selectedName), i); + } else { + auto rc = ctx->record_constraint(); + return visitRecord_constraint(rc); + } } unique_ptr VhdlTypeDeclarationParser::visitConstraint( @@ -388,39 +403,16 @@ unique_ptr VhdlTypeDeclarationParser::visitConstraint( vhdlParser::ConstraintContext *ctx) { // constraint: // range_constraint - // | array_constraint - // | record_constraint + // | element_constraint // ; auto r = ctx->range_constraint(); if (r) { - // range_constraint - // : RANGE range - // ; - auto op1 = VhdlExprParser::visitRange(r->range()); - return create_object(ctx, move(selectedName), - HdlOpType::RANGE, move(op1)); + auto rc = visitRange_constraint(r); + return create_object(r, move(selectedName), HdlOpType::RANGE, move(rc)); } else { - auto i = ctx->array_constraint(); - if (i) { - auto ac = visitArray_constraint(i); - if (ac.first == nullptr) { - auto res = create_object(ctx, HdlOpType::INDEX, - move(selectedName)); - for (auto &i : ac.second) { - res->operands.push_back(move(i)); - } - return res; - } else { - NotImplementedLogger::print( - "VhdlTypeDeclarationParser.visitConstraint visitArray_constraint", - ctx); - return create_object(ctx); - } - } else { - auto rc = ctx->record_constraint(); - return visitRecord_constraint(rc); - } + auto ec = ctx->element_constraint(); + return visitElement_constraint(move(selectedName), ec); } } diff --git a/tests/test_vhdl_conversion.py b/tests/test_vhdl_conversion.py index f6ab1e57..398ef36a 100644 --- a/tests/test_vhdl_conversion.py +++ b/tests/test_vhdl_conversion.py @@ -24,7 +24,7 @@ def test_package_array_const(self): self.assertEqual(pkg.name, 'array_const_pkg') def test_package_constants(self): - _, res = parseFile("package_constants.vhd") + _, res = self.parseWithRef("package_constants.vhd", Language.VHDL) str(res) pkg = res.objs[4] # first 4 objects are libraries and 'use' clauses self.assertIsInstance(pkg, HdlValueIdspace) @@ -112,7 +112,7 @@ def test_package_typedefs(self): if __name__ == "__main__": suite = unittest.TestSuite() - # suite.addTest(VhdlConversionTC('test_entity_declarative_item')) + #suite.addTest(VhdlConversionTC('test_package_constants')) suite.addTest(unittest.makeSuite(VhdlConversionTC)) runner = unittest.TextTestRunner(verbosity=3) diff --git a/tests/vhdl/expected/package_constants.vhd b/tests/vhdl/expected/package_constants.vhd new file mode 100644 index 00000000..71d26123 --- /dev/null +++ b/tests/vhdl/expected/package_constants.vhd @@ -0,0 +1,27 @@ +LIBRARY ieee; +USE ieee.std_logic_1164.ALL; +USE ieee.std_logic_arith.ALL; +USE ieee.std_logic_unsigned.ALL; +PACKAGE constants_pkg IS + FUNCTION fn1 (SIGNAL bits : integer) RETURN integer; + FUNCTION fn0 (SIGNAL n : integer; + SIGNAL diff : integer) RETURN integer; + FUNCTION eval_log2 (SIGNAL n : integer) RETURN integer; + CONSTANT MTR_MBIST_ARCHITECTURE : std_logic := '1'; + CONSTANT CONST_NATURAL : natural := 24; + CONSTANT NATURAL_WITH_RANGE : natural RANGE 0 TO CONST_NATURAL := 256; + CONSTANT MAX_NCUTSEL : natural := NATURAL_WITH_RANGE + (2 * eval_log2(NATURAL_WITH_RANGE / 2) - 1); + CONSTANT NREGS : natural := 103 + CONST_NATURAL * CONST_NATURAL + 5 + CONST_NATURAL + 6 + 6; + TYPE lb_res_reg IS ARRAY (fn1(CONST_NATURAL) DOWNTO 0) OF std_logic_vector(31 DOWNTO 0); + TYPE prpg_size IS ARRAY (fn1(CONST_NATURAL) DOWNTO 0) OF natural; + CONSTANT CONST_32b_0 : std_logic_vector(31 DOWNTO 0) := "00000000000000000000000000000000"; + CONSTANT CONST_32b_0_1 : std_logic_vector(31 DOWNTO 0) := X"00000000"; + CONSTANT CONST_128b_0 : std_logic_vector(127 DOWNTO 0) := X"00000000000000000000000000000000"; + CONSTANT CONST_512b_0 : std_logic_vector(511 DOWNTO 0) := X"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + CONSTANT CONST_expr_b_0 : std_logic_vector(fn1(CONST_NATURAL) DOWNTO 0) := (OTHERS => '1'); + CONSTANT CONST_expr_b_1 : std_logic_vector(fn1(CONST_NATURAL) DOWNTO 0) := "1000000000"; + CONSTANT CONST_32b_expr_0 : std_logic_vector(31 DOWNTO 0) := CONST_32b_0 + X"00000008"; + CONSTANT CONST_32b_expr_0 : std_logic_vector(31 DOWNTO 0) := CONST_32b_0_1 + X"00000070"; + CONSTANT TestConst4 : TestArray2DUU(TestInt'range, 100 DOWNTO -100)(10 DOWNTO 0); +END PACKAGE; + diff --git a/tests/vhdl/package_constants.vhd b/tests/vhdl/package_constants.vhd index 4cb664e2..4b3731d5 100644 --- a/tests/vhdl/package_constants.vhd +++ b/tests/vhdl/package_constants.vhd @@ -25,5 +25,7 @@ package constants_pkg is CONSTANT CONST_32b_expr_0 : std_logic_vector(31 downto 0) := CONST_32b_0 + X"0000_0008"; CONSTANT CONST_32b_expr_0 : std_logic_vector(31 downto 0) := CONST_32b_0_1 + X"0000_0070" ; + CONSTANT TestConst4 : TestArray2DUU(TestInt'range, 100 downto -100)(10 downto 0); + end constants_pkg;