Skip to content

Commit

Permalink
Merge pull request #2420 from lf-lang/mutable
Browse files Browse the repository at this point in the history
Fixed parameterized mutable inputs
  • Loading branch information
edwardalee authored Oct 3, 2024
2 parents b69b696 + eaac986 commit ae1740e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/lflang/generator/c/CUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ private static List<String> multiportWidthTerms(Variable variable) {
if (!((Port) variable).getWidthSpec().isOfVariableLength()) {
for (WidthTerm term : ((Port) variable).getWidthSpec().getTerms()) {
if (term.getParameter() != null) {
result.add(getTargetReference(term.getParameter()));
result.add("self->" + getTargetReference(term.getParameter()));
} else {
result.add(String.valueOf(term.getWidth()));
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/lib/c/reactor-c
43 changes: 43 additions & 0 deletions test/C/src/Mutable.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
target C

reactor S(width: int = 4) {
output[width] out: int

reaction(startup) -> out {=
for(int i = 0; i < self->width; i++) {
lf_set(out[i], 0);
}
=}
}

reactor R(width: int = 4) {
mutable input[width] in: int
output[width] out: int

reaction(in) -> out {=
for(int i = 0; i < self->width; i++) {
in[i]->value = (i + 1) * 2;
lf_set(out[i], in[i]->value);
}
=}
}

main reactor {
s = new S(width=2)
r = new R(width=2)
s.out -> r.in

reaction(r.out) {=
lf_print("Received from R %d, %d", r.out[0]->value, r.out[1]->value);
if (r.out[0]->value != 2 || r.out[1]->value != 4) {
lf_print_error_and_exit("Expected 2 and 4.");
}
=}

reaction(s.out) {=
lf_print("Received from S %d, %d", s.out[0]->value, s.out[1]->value);
if (s.out[0]->value != 0 || s.out[1]->value != 0) {
lf_print_error_and_exit("Expected zeros.");
}
=}
}
2 changes: 1 addition & 1 deletion test/C/src/enclave/EnclaveRequestStop.lf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** Test that enclaves all stop at the time requested by the first enclave to request a stop. */
target C {
timeout: 1 sec
timeout: 10 sec
}

reactor Stop(stop_time: time = 5 s) {
Expand Down
50 changes: 50 additions & 0 deletions test/C/src/token/MutableToken.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
target C {
timeout: 0 s,
files: ["include/array.h"]
}

import TokenSource from "lib/Token.lf"

preamble {=
#include "array.h"
=}

reactor R(width: int = 4) {
mutable input[width] in: int_array_t*
output[width] out: int_array_t*

reaction(in) -> out {=
for(int i = 0; i < self->width; i++) {
for (int j = 0; j < in[i]->value->length; j++) {
in[i]->value->data[j] = in[i]->value->data[j] * 2;
}
lf_set_token(out[i], in[i]->token);
}
=}
}

main reactor {
s = new[2] TokenSource()
r = new R(width=2)
s.out -> r.in

reaction(r.out) {=
lf_print("Received from R [%d, %d, %d], [%d, %d, %d]",
r.out[0]->value->data[0], r.out[0]->value->data[1], r.out[0]->value->data[2],
r.out[1]->value->data[0], r.out[1]->value->data[1], r.out[1]->value->data[2]);
if (r.out[0]->value->data[0] != 0 || r.out[0]->value->data[1] != 2 || r.out[0]->value->data[2] != 4
|| r.out[1]->value->data[0] != 0 || r.out[1]->value->data[1] != 2 || r.out[1]->value->data[2] != 4) {
lf_print_error_and_exit("Expected [0, 2, 4].");
}
=}

reaction(s.out) {=
lf_print("Received from S [%d, %d, %d], [%d, %d, %d]",
s[0].out->value->data[0], s[0].out->value->data[1], s[0].out->value->data[2],
s[1].out->value->data[0], s[1].out->value->data[1], s[1].out->value->data[2]);
if (s[0].out->value->data[0] != 0 || s[0].out->value->data[1] != 1 || s[0].out->value->data[2] != 2
|| s[1].out->value->data[0] != 0 || s[1].out->value->data[1] != 1 || s[1].out->value->data[2] != 2) {
lf_print_error_and_exit("Expected [0, 2, 4].");
}
=}
}

0 comments on commit ae1740e

Please sign in to comment.