Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jul 25, 2023
1 parent 9ca6ee7 commit 6a84191
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ bool eval_possible(tree_t t, unit_registry_t *ur)
if (tree_has_value(decl))
return eval_possible(tree_value(decl), ur);
else
return true;
return false;

default:
return eval_not_possible(t, "reference");
Expand Down
32 changes: 29 additions & 3 deletions src/lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -2698,11 +2698,37 @@ static vcode_reg_t lower_link_var(lower_unit_t *lu, tree_t decl)
istr(tree_ident(decl)));
}
}
else if (mode == LOWER_THUNK && lu->parent == NULL
&& !arena_frozen(tree_arena(container))) {
else if (mode == LOWER_THUNK && lu->parent == NULL) {
// Handle a special case of simplifying locally static expressions
// that reference constant declarations
return lower_rvalue(lu, tree_value(decl));
assert(tree_kind(decl) == T_CONST_DECL);

type_t type = tree_type(decl);
tree_t value = tree_value(decl);
vcode_reg_t value_reg = lower_rvalue(lu, value), ptr_reg;

switch (vcode_reg_kind(value_reg)) {
case VCODE_TYPE_POINTER:
case VCODE_TYPE_UARRAY:
ptr_reg = value_reg;
break;
default:
{
vcode_type_t vtype = lower_type(type);
vcode_type_t vbounds = lower_bounds(type);
vcode_var_t var = emit_var(vtype, vbounds, tree_ident(decl), 0);
lower_put_vcode_obj(decl, var, lu);
emit_store(value_reg, var);

ptr_reg = emit_index(var, VCODE_INVALID_REG);
}
break;
}

if (type_is_array(type))
return lower_coerce_arrays(lu, tree_type(value), type, ptr_reg);
else
return ptr_reg;
}

assert(!is_uninstantiated_package(container));
Expand Down
24 changes: 24 additions & 0 deletions test/simp/issue742.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PACKAGE tb_test_package IS

CONSTANT C_VEC : bit_vector(2 DOWNTO 0) := "111";

PROCEDURE test (
o: OUT bit
);

END PACKAGE tb_test_package;

PACKAGE BODY tb_test_package IS

PROCEDURE test (
o: OUT bit
) IS
BEGIN
IF C_VEC /= "000" THEN
o := '1';
ELSE
o := '0';
END IF;
END test;

END PACKAGE BODY tb_test_package;
28 changes: 3 additions & 25 deletions test/test_lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -2702,8 +2702,9 @@ START_TEST(test_thunk)
vcode_select_unit(t0);

EXPECT_BB(0) = {
{ VCODE_OP_PACKAGE_INIT, .name = "WORK.PACK" },
{ VCODE_OP_LINK_VAR, .name = "C" },
{ VCODE_OP_CONST, .value = 1 },
{ VCODE_OP_CONST_ARRAY, .length = 4 },
{ VCODE_OP_ADDRESS_OF },
{ VCODE_OP_CONST, .value = 2 },
{ VCODE_OP_ARRAY_REF },
{ VCODE_OP_LOAD_INDIRECT },
Expand All @@ -2715,29 +2716,6 @@ START_TEST(test_thunk)

vcode_unit_unref(t0);
}

{
vcode_unit_t t1 = lower_thunk(NULL, tree_value(tree_decl(arch, 1)));
fail_if(t1 == NULL);
vcode_select_unit(t1);

EXPECT_BB(0) = {
{ VCODE_OP_PACKAGE_INIT, .name = "WORK.PACK" },
{ VCODE_OP_LINK_VAR, .name = "D" },
{ VCODE_OP_CONST, .value = 0 },
{ VCODE_OP_CONST, .value = 1 },
{ VCODE_OP_ARRAY_REF },
{ VCODE_OP_RECORD_REF, .field = 0 },
{ VCODE_OP_LOAD_INDIRECT },
{ VCODE_OP_DEBUG_LOCUS },
{ VCODE_OP_TRAP_ADD },
{ VCODE_OP_RETURN },
};

CHECK_BB(0);

vcode_unit_unref(t1);
}
}
END_TEST

Expand Down
22 changes: 21 additions & 1 deletion test/test_simp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,11 @@ START_TEST(test_genpack1)

tree_t b0 = tree_stmt(top, 0);
fail_unless(tree_kind(b0) == T_BLOCK);
fail_unless(tree_stmts(b0) == 0);
fail_unless(tree_stmts(b0) == 1);

tree_t p1 = tree_stmt(b0, 0);
fail_unless(tree_kind(p1) == T_PROCESS);
fail_unless(tree_stmts(p1) == 2); // Should have been eliminated

fail_if_errors();
}
Expand Down Expand Up @@ -1520,6 +1524,21 @@ START_TEST(test_ieee1)
}
END_TEST

START_TEST(test_issue742)
{
input_from_file(TESTDIR "/simp/issue742.vhd");

tree_t b = parse_check_and_simplify(T_PACKAGE, T_PACK_BODY);

tree_t test = tree_decl(b, 0);
fail_unless(tree_kind(test) == T_PROC_BODY);
fail_unless(tree_stmts(test) == 1);
fail_unless(tree_kind(tree_stmt(test, 0)) == T_VAR_ASSIGN);

fail_if_errors();
}
END_TEST

Suite *get_simp_tests(void)
{
Suite *s = suite_create("simplify");
Expand Down Expand Up @@ -1578,6 +1597,7 @@ Suite *get_simp_tests(void)
tcase_add_test(tc_core, test_casegen);
tcase_add_test(tc_core, test_condexpr);
tcase_add_test(tc_core, test_ieee1);
tcase_add_test(tc_core, test_issue742);
suite_add_tcase(s, tc_core);

return s;
Expand Down

0 comments on commit 6a84191

Please sign in to comment.