From 2d9a7c28c12484953fcf7608bf0e36f82f1479f6 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Thu, 3 Oct 2024 04:48:52 -0700 Subject: [PATCH 1/6] Fixed #2117 and added tests --- .../generator/c/CReactionGenerator.java | 12 ++--- core/src/main/resources/lib/c/reactor-c | 2 +- test/C/src/Mutable.lf | 36 +++++++++++++++ test/C/src/token/MutableToken.lf | 46 +++++++++++++++++++ 4 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 test/C/src/Mutable.lf create mode 100644 test/C/src/token/MutableToken.lf diff --git a/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java b/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java index 151d2a47fb..41aeba5895 100644 --- a/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java @@ -694,11 +694,11 @@ private static String generateInputVariablesInReaction( structType + " _lf_tmp_" + inputName - + "[" + + "[self->" + CUtil.multiportWidthExpression(input) + "];", - structType + "* " + inputName + "[" + CUtil.multiportWidthExpression(input) + "];", - "for (int i = 0; i < " + CUtil.multiportWidthExpression(input) + "; i++) {", + structType + "* " + inputName + "[self->" + CUtil.multiportWidthExpression(input) + "];", + "for (int i = 0; i < self->" + CUtil.multiportWidthExpression(input) + "; i++) {", " " + inputName + "[i] = &_lf_tmp_" + inputName + "[i];", " _lf_tmp_" + inputName + "[i] = *(self->_lf_" + inputName + "[i]);", " // If necessary, copy the tokens.", @@ -732,11 +732,11 @@ private static String generateInputVariablesInReaction( structType + " _lf_tmp_" + inputName - + "[" + + "[self->" + CUtil.multiportWidthExpression(input) + "];", - structType + "* " + inputName + "[" + CUtil.multiportWidthExpression(input) + "];", - "for (int i = 0; i < " + CUtil.multiportWidthExpression(input) + "; i++) {", + structType + "* " + inputName + "[self->" + CUtil.multiportWidthExpression(input) + "];", + "for (int i = 0; i < self->" + CUtil.multiportWidthExpression(input) + "; i++) {", " " + inputName + "[i] = &_lf_tmp_" + inputName + "[i];", " // Copy the struct, which includes the value.", " _lf_tmp_" + inputName + "[i] = *(self->_lf_" + inputName + "[i]);", diff --git a/core/src/main/resources/lib/c/reactor-c b/core/src/main/resources/lib/c/reactor-c index 815696c5d5..bea933ff67 160000 --- a/core/src/main/resources/lib/c/reactor-c +++ b/core/src/main/resources/lib/c/reactor-c @@ -1 +1 @@ -Subproject commit 815696c5d53775755e55dc8da4c6fb99f218efe1 +Subproject commit bea933ff67fa976013fcbd73bfaa1e88f6d846a9 diff --git a/test/C/src/Mutable.lf b/test/C/src/Mutable.lf new file mode 100644 index 0000000000..2f03613cea --- /dev/null +++ b/test/C/src/Mutable.lf @@ -0,0 +1,36 @@ +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."); + } + =} +} diff --git a/test/C/src/token/MutableToken.lf b/test/C/src/token/MutableToken.lf new file mode 100644 index 0000000000..86b64bc0a2 --- /dev/null +++ b/test/C/src/token/MutableToken.lf @@ -0,0 +1,46 @@ +target C { + timeout: 0s, + 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]."); + } + =} +} From cae55a438c283c4afc2b25703f033ebd19411161 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Thu, 3 Oct 2024 04:52:12 -0700 Subject: [PATCH 2/6] Format --- test/C/src/Mutable.lf | 19 +++++++++++++------ test/C/src/token/MutableToken.lf | 10 +++++++--- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/test/C/src/Mutable.lf b/test/C/src/Mutable.lf index 2f03613cea..575473986f 100644 --- a/test/C/src/Mutable.lf +++ b/test/C/src/Mutable.lf @@ -1,15 +1,19 @@ -target C; -reactor S(width:int = 4) { - output[width] out:int +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) { + +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; @@ -17,16 +21,19 @@ reactor R(width:int=4) { } =} } + main reactor { - s = new S(width = 2) - r = new R(width = 2) + 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) { diff --git a/test/C/src/token/MutableToken.lf b/test/C/src/token/MutableToken.lf index 86b64bc0a2..724b8a6579 100644 --- a/test/C/src/token/MutableToken.lf +++ b/test/C/src/token/MutableToken.lf @@ -1,5 +1,5 @@ target C { - timeout: 0s, + timeout: 0 s, files: ["include/array.h"] } @@ -9,9 +9,10 @@ preamble {= #include "array.h" =} -reactor R(width:int=4) { +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++) { @@ -21,10 +22,12 @@ reactor R(width:int=4) { } =} } + main reactor { s = new[2] TokenSource() - r = new R(width = 2) + 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], @@ -34,6 +37,7 @@ main reactor { 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], From 58d965f40714939c2a0726f6fda0f53710fa9ffd Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Thu, 3 Oct 2024 06:11:39 -0700 Subject: [PATCH 3/6] Use self-> modifier only for variables --- .../org/lflang/generator/c/CReactionGenerator.java | 12 ++++++------ core/src/main/java/org/lflang/generator/c/CUtil.java | 2 +- core/src/main/resources/lib/c/reactor-c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java b/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java index 41aeba5895..151d2a47fb 100644 --- a/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java +++ b/core/src/main/java/org/lflang/generator/c/CReactionGenerator.java @@ -694,11 +694,11 @@ private static String generateInputVariablesInReaction( structType + " _lf_tmp_" + inputName - + "[self->" + + "[" + CUtil.multiportWidthExpression(input) + "];", - structType + "* " + inputName + "[self->" + CUtil.multiportWidthExpression(input) + "];", - "for (int i = 0; i < self->" + CUtil.multiportWidthExpression(input) + "; i++) {", + structType + "* " + inputName + "[" + CUtil.multiportWidthExpression(input) + "];", + "for (int i = 0; i < " + CUtil.multiportWidthExpression(input) + "; i++) {", " " + inputName + "[i] = &_lf_tmp_" + inputName + "[i];", " _lf_tmp_" + inputName + "[i] = *(self->_lf_" + inputName + "[i]);", " // If necessary, copy the tokens.", @@ -732,11 +732,11 @@ private static String generateInputVariablesInReaction( structType + " _lf_tmp_" + inputName - + "[self->" + + "[" + CUtil.multiportWidthExpression(input) + "];", - structType + "* " + inputName + "[self->" + CUtil.multiportWidthExpression(input) + "];", - "for (int i = 0; i < self->" + CUtil.multiportWidthExpression(input) + "; i++) {", + structType + "* " + inputName + "[" + CUtil.multiportWidthExpression(input) + "];", + "for (int i = 0; i < " + CUtil.multiportWidthExpression(input) + "; i++) {", " " + inputName + "[i] = &_lf_tmp_" + inputName + "[i];", " // Copy the struct, which includes the value.", " _lf_tmp_" + inputName + "[i] = *(self->_lf_" + inputName + "[i]);", diff --git a/core/src/main/java/org/lflang/generator/c/CUtil.java b/core/src/main/java/org/lflang/generator/c/CUtil.java index 64e63bfe95..dae1425342 100644 --- a/core/src/main/java/org/lflang/generator/c/CUtil.java +++ b/core/src/main/java/org/lflang/generator/c/CUtil.java @@ -755,7 +755,7 @@ private static List 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())); } diff --git a/core/src/main/resources/lib/c/reactor-c b/core/src/main/resources/lib/c/reactor-c index bea933ff67..7208c9f02e 160000 --- a/core/src/main/resources/lib/c/reactor-c +++ b/core/src/main/resources/lib/c/reactor-c @@ -1 +1 @@ -Subproject commit bea933ff67fa976013fcbd73bfaa1e88f6d846a9 +Subproject commit 7208c9f02e991f85dd85cef0c4cb576dcf1deebb From b47c1790bda37e4c42d298fafabeeb6951c0ce05 Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Thu, 3 Oct 2024 06:13:57 -0700 Subject: [PATCH 4/6] Align reactor-c --- core/src/main/resources/lib/c/reactor-c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/resources/lib/c/reactor-c b/core/src/main/resources/lib/c/reactor-c index 7208c9f02e..693c5f5bdb 160000 --- a/core/src/main/resources/lib/c/reactor-c +++ b/core/src/main/resources/lib/c/reactor-c @@ -1 +1 @@ -Subproject commit 7208c9f02e991f85dd85cef0c4cb576dcf1deebb +Subproject commit 693c5f5bdbc6918cebc619c3ae46a22af719b741 From d51f98f252aeed04918aab9060a8578d7d23d028 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Thu, 3 Oct 2024 10:04:07 -0700 Subject: [PATCH 5/6] Remove EnclaveRequestShutdown --- test/C/src/enclave/EnclaveRequestStop.lf | 29 ------------------------ 1 file changed, 29 deletions(-) delete mode 100644 test/C/src/enclave/EnclaveRequestStop.lf diff --git a/test/C/src/enclave/EnclaveRequestStop.lf b/test/C/src/enclave/EnclaveRequestStop.lf deleted file mode 100644 index aea05a874d..0000000000 --- a/test/C/src/enclave/EnclaveRequestStop.lf +++ /dev/null @@ -1,29 +0,0 @@ -/** Test that enclaves all stop at the time requested by the first enclave to request a stop. */ -target C { - timeout: 1 sec -} - -reactor Stop(stop_time: time = 5 s) { - preamble {= - #include "platform.h" // Defines PRINTF_TIME - =} - timer t(stop_time) - - reaction(t) {= - lf_request_stop(); - =} - - reaction(shutdown) {= - lf_print("Stopped at tag (" PRINTF_TIME ", %d)", lf_time_logical_elapsed(), lf_tag().microstep); - if (lf_time_logical_elapsed() != 50000000LL || lf_tag().microstep != 1) { - lf_print_error_and_exit("Expected stop tag to be (50ms, 1)."); - } - =} -} - -main reactor { - @enclave - s1 = new Stop() - @enclave - s2 = new Stop(stop_time = 50 ms) -} From eaac986f98251355fea600efb68457192ea22fda Mon Sep 17 00:00:00 2001 From: "Edward A. Lee" Date: Thu, 3 Oct 2024 11:50:38 -0700 Subject: [PATCH 6/6] Fixed test so that stop time is not earlier than request_stop time --- test/C/src/enclave/EnclaveRequestStop.lf | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/C/src/enclave/EnclaveRequestStop.lf diff --git a/test/C/src/enclave/EnclaveRequestStop.lf b/test/C/src/enclave/EnclaveRequestStop.lf new file mode 100644 index 0000000000..91df3b1a60 --- /dev/null +++ b/test/C/src/enclave/EnclaveRequestStop.lf @@ -0,0 +1,29 @@ +/** Test that enclaves all stop at the time requested by the first enclave to request a stop. */ +target C { + timeout: 10 sec +} + +reactor Stop(stop_time: time = 5 s) { + preamble {= + #include "platform.h" // Defines PRINTF_TIME + =} + timer t(stop_time) + + reaction(t) {= + lf_request_stop(); + =} + + reaction(shutdown) {= + lf_print("Stopped at tag (" PRINTF_TIME ", %d)", lf_time_logical_elapsed(), lf_tag().microstep); + if (lf_time_logical_elapsed() != 50000000LL || lf_tag().microstep != 1) { + lf_print_error_and_exit("Expected stop tag to be (50ms, 1)."); + } + =} +} + +main reactor { + @enclave + s1 = new Stop() + @enclave + s2 = new Stop(stop_time = 50 ms) +}