Skip to content

Commit

Permalink
implement rest of visitArray_element_constraint #141
Browse files Browse the repository at this point in the history
  • Loading branch information
Nic30 committed Nov 1, 2020
1 parent 8a51ac1 commit 125eea2
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 96 deletions.
3 changes: 1 addition & 2 deletions grammars/vhdlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions include/hdlConvertor/vhdlConvertor/typeDeclarationParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@ class VhdlTypeDeclarationParser {
static std::unique_ptr<hdlAst::iHdlExprItem> visitConstraint(
std::unique_ptr<hdlAst::iHdlExprItem> selectedName,
vhdlParser::ConstraintContext *ctx);
static std::pair<std::unique_ptr<hdlAst::iHdlExprItem>,
std::vector<std::unique_ptr<hdlAst::iHdlExprItem>> > visitArray_constraint(
static std::unique_ptr<hdlAst::iHdlExprItem> visitArray_constraint(
std::unique_ptr<hdlAst::iHdlExprItem> selectedName,
vhdlParser::Array_constraintContext *ctx);
static std::vector<std::unique_ptr<hdlAst::iHdlExprItem>> visitIndex_constraint(
static std::unique_ptr<hdlAst::iHdlExprItem> visitIndex_constraint(
std::unique_ptr<hdlAst::iHdlExprItem> selectedName,
vhdlParser::Index_constraintContext *ctx);
static std::unique_ptr<hdlAst::iHdlExprItem> visitRecord_constraint(
vhdlParser::Record_constraintContext *ctx);
static std::unique_ptr<hdlAst::iHdlExprItem> visitArray_element_constraint(
std::unique_ptr<hdlAst::iHdlExprItem> selectedName,
vhdlParser::Array_element_constraintContext *ctx);
static std::unique_ptr<hdlAst::iHdlExprItem> visitElement_constraint(
std::unique_ptr<hdlAst::iHdlExprItem> selectedName,
vhdlParser::Element_constraintContext *ctx);

static std::unique_ptr<hdlAst::HdlIdDef> visitType_declaration(
vhdlParser::Type_declarationContext *ctx);
Expand Down
170 changes: 81 additions & 89 deletions src/vhdlConvertor/typeDeclarationParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ unique_ptr<HdlIdDef> VhdlTypeDeclarationParser::visitType_definition(
}
}
}
return create_object<HdlIdDef>(ctx, move(name),
HdlValueSymbol::type(), move(t));
return create_object<HdlIdDef>(ctx, move(name), HdlValueSymbol::type(),
move(t));
}

unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitScalar_type_definition(
Expand Down Expand Up @@ -167,56 +167,55 @@ unique_ptr<HdlEnumDef> VhdlTypeDeclarationParser::visitEnumeration_type_definiti
}

unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitRange_constraint(
vhdlParser::Range_constraintContext *ctx) {
// range_constraint
// : RANGE range
// ;
auto op = VhdlExprParser::visitRange(ctx->range());
return create_object<HdlOp>(ctx, HdlOpType::RANGE, move(op));
vhdlParser::Range_constraintContext *ctx) {
// range_constraint
// : RANGE range
// ;
return VhdlExprParser::visitRange(ctx->range());
}

unique_ptr<iHdlExprItem> 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<HdlOp>(ctx, HdlOpType::RANGE, move(rc));
}

unique_ptr<iHdlExprItem> 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<HdlOp>(ctx, HdlOpType::RANGE, move(rc));
}

unique_ptr<HdlPhysicalDef> 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<HdlPhysicalDef>();
pdecl->range = create_object<HdlOp>(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<HdlPhysicalDef>();
pdecl->range = create_object<HdlOp>(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;
}

Expand Down Expand Up @@ -263,12 +262,7 @@ unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitConstrained_array_defin
// KW_ARRAY index_constraint KW_OF subtype_indication
//;
auto element_t = visitSubtype_indication(ctx->subtype_indication());
auto tdef = make_unique<HdlOp>(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<HdlClassDef> VhdlTypeDeclarationParser::visitRecord_type_definition(
Expand Down Expand Up @@ -332,8 +326,8 @@ unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitSubtype_indication(
e = visitConstraint(move(e), c);
}
if (ri) {
return create_object<HdlOp>(ctx, move(ri),
HdlOpType::DEFINE_RESOLVER, move(e));
return create_object<HdlOp>(ctx, move(ri), HdlOpType::DEFINE_RESOLVER,
move(e));
} else {
return e;
}
Expand All @@ -342,85 +336,83 @@ unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitSubtype_indication(
/*
* @return element_type, dimension indexes
* */
pair<unique_ptr<iHdlExprItem>, vector<unique_ptr<iHdlExprItem>>> VhdlTypeDeclarationParser::visitArray_constraint(
unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitArray_constraint(
unique_ptr<iHdlExprItem> 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<iHdlExprItem> res;
if (ic) {
res = visitIndex_constraint(move(selectedName), ic);
} else {
res = create_object<HdlOp>(ctx, move(selectedName), HdlOpType::INDEX,
HdlValueSymbol::all());
}
auto aec = ctx->array_element_constraint();
unique_ptr<iHdlExprItem> elem_t = nullptr;
if (aec) {
elem_t = visitArray_element_constraint(aec);
}
vector<unique_ptr<iHdlExprItem>> e;
if (ic) {
e = visitIndex_constraint(ic);
res = visitArray_element_constraint(move(res), aec);
}
return {move(elem_t), move(e)};

return res;
}
vector<unique_ptr<iHdlExprItem>> VhdlTypeDeclarationParser::visitIndex_constraint(

unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitIndex_constraint(
unique_ptr<iHdlExprItem> selectedName,
vhdlParser::Index_constraintContext *ctx) {
// index_constraint
// : LPAREN discrete_range ( COMMA discrete_range )* RPAREN
// ;
vector<unique_ptr<iHdlExprItem>> res;
unique_ptr<iHdlExprItem> res = move(selectedName);
vector<unique_ptr<iHdlExprItem>> 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<iHdlExprItem> VhdlTypeDeclarationParser::visitArray_element_constraint(
unique_ptr<iHdlExprItem> selectedName,
vhdlParser::Array_element_constraintContext *ctx) {
// array_element_constraint: element_constraint;
return visitElement_constraint(move(selectedName),
ctx->element_constraint());
}

unique_ptr<iHdlExprItem> VhdlTypeDeclarationParser::visitElement_constraint(
unique_ptr<iHdlExprItem> selectedName,
vhdlParser::Element_constraintContext *ctx) {
// element_constraint:
// array_constraint
// | record_constraint
// ;
NotImplementedLogger::print(
"VhdlTypeDeclarationParser.visitArray_element_constraint", ctx);
return create_object<HdlExprNotImplemented>(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<iHdlExprItem> VhdlTypeDeclarationParser::visitConstraint(
unique_ptr<iHdlExprItem> selectedName,
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<HdlOp>(ctx, move(selectedName),
HdlOpType::RANGE, move(op1));
auto rc = visitRange_constraint(r);
return create_object<HdlOp>(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<HdlOp>(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<HdlExprNotImplemented>(ctx);
}
} else {
auto rc = ctx->record_constraint();
return visitRecord_constraint(rc);
}
auto ec = ctx->element_constraint();
return visitElement_constraint(move(selectedName), ec);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/test_vhdl_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions tests/vhdl/expected/package_constants.vhd
Original file line number Diff line number Diff line change
@@ -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;

2 changes: 2 additions & 0 deletions tests/vhdl/package_constants.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 125eea2

Please sign in to comment.