Skip to content

Commit

Permalink
Optimise code generation for protected procedure calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nickg committed Jul 26, 2023
1 parent 30852c5 commit b9bc550
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 18 deletions.
12 changes: 11 additions & 1 deletion src/jit/jit-exits.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,17 @@ void x_unreachable(tree_t where)

void x_func_wait(void)
{
jit_msg(NULL, DIAG_FATAL, "cannot wait inside function call");
jit_stack_trace_t *trace = jit_stack_trace();
tree_t inner = trace->frames[0].decl;
free(trace);

if (tree_kind(inner) == T_PROC_BODY) {
// Must be procedure body in protected object
jit_msg(NULL, DIAG_FATAL, "cannot wait inside call to protected "
"type method");
}
else
jit_msg(NULL, DIAG_FATAL, "cannot wait inside function call");
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
3 changes: 2 additions & 1 deletion src/lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -9858,7 +9858,8 @@ static void lower_predef(lower_unit_t *parent, tree_t decl)

static void lower_proc_body(lower_unit_t *parent, tree_t body)
{
const bool never_waits = !!(tree_flags(body) & TREE_F_NEVER_WAITS);
const bool never_waits = !!(tree_flags(body) & TREE_F_NEVER_WAITS)
|| tree_kind(parent->container) == T_PROT_BODY;

vcode_select_unit(parent->vunit);

Expand Down
7 changes: 4 additions & 3 deletions src/sem.c
Original file line number Diff line number Diff line change
Expand Up @@ -2894,7 +2894,8 @@ static bool sem_check_pcall(tree_t t, nametab_t *tab)
if (!tree_has_ref(t))
return false;

if (tree_kind(t) == T_PROT_PCALL && tree_has_name(t)
const bool is_protected = (tree_kind(t) == T_PROT_PCALL);
if (is_protected && tree_has_name(t)
&& !sem_check(tree_name(t), tab))
return false;

Expand All @@ -2914,8 +2915,8 @@ static bool sem_check_pcall(tree_t t, nametab_t *tab)

const tree_flags_t flags = tree_flags(decl);

const bool never_waits = !!(flags & TREE_F_NEVER_WAITS);
const bool has_wait = !!(flags & TREE_F_HAS_WAIT);
const bool never_waits = is_protected || !!(flags & TREE_F_NEVER_WAITS);
const bool has_wait = !is_protected && !!(flags & TREE_F_HAS_WAIT);

assert(!never_waits || !has_wait);

Expand Down
1 change: 1 addition & 0 deletions test/regress/gold/protected11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0ms+0: cannot wait inside call to protected type method
56 changes: 56 additions & 0 deletions test/regress/protected11.vhd
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
entity protected11 is
end entity;

architecture test of protected11 is

procedure do_something;
procedure do_wait;

type pt is protected
procedure proc;
end protected;

type pt is protected body
variable count : natural;

procedure proc(arg : integer);

procedure proc is
begin
count := count + 1;
assert count = 1;
proc(5);
assert count = 2;
proc(-1);
end procedure;

procedure proc(arg : integer) is
begin
count := count + 1;
do_something;
if arg < 0 then
do_wait; -- Error
end if;
end procedure;

end protected body;

procedure do_something is
begin
end procedure;

procedure do_wait is
begin
wait for 1 ns;
end procedure;

shared variable v : pt;
begin

tb: process is
begin
v.proc;
wait;
end process;

end architecture;
1 change: 1 addition & 0 deletions test/regress/testlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -827,3 +827,4 @@ psl4 fail,psl,gold,2008
cover17 cover,export=cobertura
toplevel3 normal,guut.i=2,guut.s=2
predef4 normal,2008
protected11 fail,gold,2002
17 changes: 4 additions & 13 deletions test/test_lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -5206,15 +5206,7 @@ START_TEST(test_protpcall)
{ VCODE_OP_ASSERT },
{ VCODE_OP_CONTEXT_UPREF, .hops = 1 },
{ VCODE_OP_CONST, .value = 5 },
{ VCODE_OP_PCALL, .func = "WORK.PROTECTED11.PT.PROC(I)", .target = 1 },
};

CHECK_BB(0);

EXPECT_BB(1) = {
{ VCODE_OP_RESUME, .func = "WORK.PROTECTED11.PT.PROC(I)" },
{ VCODE_OP_CONST, .value = 2 },
{ VCODE_OP_VAR_UPREF, .name = "COUNT", .hops = 1 },
{ VCODE_OP_FCALL, .func = "WORK.PROTECTED11.PT.PROC(I)" },
{ VCODE_OP_LOAD_INDIRECT },
{ VCODE_OP_CONST, .value = 2 },
{ VCODE_OP_CMP, .cmp = VCODE_CMP_EQ },
Expand All @@ -5223,24 +5215,23 @@ START_TEST(test_protpcall)
{ VCODE_OP_RETURN },
};

CHECK_BB(1);
CHECK_BB(0);
}

{
vcode_unit_t vu = find_unit("WORK.PROTECTED11.PT.PROC(I)");
vcode_select_unit(vu);

EXPECT_BB(0) = {
{ VCODE_OP_STORE, .name = "ARG" },
{ VCODE_OP_VAR_UPREF, .name = "COUNT", .hops = 1 },
{ VCODE_OP_LOAD_INDIRECT },
{ VCODE_OP_CONST, .value = 1 },
{ VCODE_OP_DEBUG_LOCUS },
{ VCODE_OP_TRAP_ADD },
{ VCODE_OP_STORE_INDIRECT },
{ VCODE_OP_CONTEXT_UPREF, .hops = 2 },
{ VCODE_OP_PCALL, .func = "WORK.PROTECTED11.DO_SOMETHING",
.target = 1 },
{ VCODE_OP_FCALL, .func = "WORK.PROTECTED11.DO_SOMETHING" },
{ VCODE_OP_RETURN },
};

CHECK_BB(0);
Expand Down

0 comments on commit b9bc550

Please sign in to comment.