diff --git a/org.lflang/src/org/lflang/generator/c/CGenerator.java b/org.lflang/src/org/lflang/generator/c/CGenerator.java index 8d537798a0..4ba439fc01 100644 --- a/org.lflang/src/org/lflang/generator/c/CGenerator.java +++ b/org.lflang/src/org/lflang/generator/c/CGenerator.java @@ -2231,6 +2231,8 @@ private void generateModeStructure(ReactorInstance instance) { */ public void generateParameterInitialization(ReactorInstance instance) { var selfRef = CUtil.reactorRef(instance); + // Declare a local bank_index variable so that initializers can use it. + initializeTriggerObjects.pr("int bank_index = "+CUtil.bankIndex(instance)+";"); for (ParameterInstance parameter : instance.parameters) { // NOTE: we now use the resolved literal value. For better efficiency, we could // store constants in a global array and refer to its elements to avoid duplicate diff --git a/org.lflang/src/org/lflang/generator/python/PythonReactorGenerator.java b/org.lflang/src/org/lflang/generator/python/PythonReactorGenerator.java index be06af5254..98d95f98f0 100644 --- a/org.lflang/src/org/lflang/generator/python/PythonReactorGenerator.java +++ b/org.lflang/src/org/lflang/generator/python/PythonReactorGenerator.java @@ -147,6 +147,9 @@ public static String generatePythonClassInstantiations(ReactorInstance instance, "for "+PyUtil.bankIndexName(instance)+" in range("+instance.getWidth()+"):" )); code.indent(); + // Define a bank_index local variable so that it can be used while + // setting parameter values. + code.pr("bank_index = "+PyUtil.bankIndexName(instance)); code.pr(generatePythonClassInstantiation(instance, className)); } diff --git a/test/C/src/multiport/BankIndexInitializer.lf b/test/C/src/multiport/BankIndexInitializer.lf new file mode 100644 index 0000000000..2a66a41432 --- /dev/null +++ b/test/C/src/multiport/BankIndexInitializer.lf @@ -0,0 +1,43 @@ +// Test bank of reactors to multiport input with id parameter in the bank. +target C; + +preamble {= + int table[] = {4, 3, 2, 1}; +=} + +reactor Source( + bank_index:int(0), + value:int(0) +) { + output out:int; + reaction (startup) -> out {= + SET(out, self->value); + =} +} + +reactor Sink(width:int(4)) { + input[width] in:int; + state received:bool(false); + + reaction (in) {= + for (int idx = 0; idx < in_width; idx++) { + if (in[idx]->is_present) { + printf("Received on channel %d: %d\n", idx, in[idx]->value); + self->received = true; + if (in[idx]->value != 4 - idx) { + error_print_and_exit("Expected %d.", 4 - idx); + } + } + } + =} + reaction(shutdown) {= + if (!self->received) { + error_print_and_exit("Sink received no data."); + } + =} +} +main reactor(width:int(4)) { + source = new[width] Source(value = {= table[bank_index] =}); + sink = new Sink(width = width); + source.out -> sink.in; +} \ No newline at end of file diff --git a/test/Python/src/multiport/BankIndexInitializer.lf b/test/Python/src/multiport/BankIndexInitializer.lf new file mode 100644 index 0000000000..54fb4c16ce --- /dev/null +++ b/test/Python/src/multiport/BankIndexInitializer.lf @@ -0,0 +1,41 @@ +// Test bank of reactors to multiport input with id parameter in the bank. +target Python; + +preamble {= + table = [4, 3, 2, 1] +=} + +reactor Source( + bank_index(0), + value(0) +) { + output out; + reaction (startup) -> out {= + out.set(self.value) + =} +} + +reactor Sink(width(4)) { + input[width] _in; + state received(false); + + reaction (_in) {= + for (idx, port) in enumerate(_in): + if port.is_present is True: + print("Received on channel {:d}: {:d}".format(idx, port.value)) + self.received = True + if port.value != 4 - idx: + sys.stderr.write("ERROR: expected {:d}\n".format(4 - idx)) + exit(1) + =} + reaction(shutdown) {= + if self.received is False: + sys.stderr.write("ERROR: Sink received no data\n") + exit(1) + =} +} +main reactor(width(4)) { + source = new[width] Source(value = {= table[bank_index] =}); + sink = new Sink(width = width); + source.out -> sink._in; +} \ No newline at end of file