Skip to content

Commit

Permalink
Merge pull request #1101 from lf-lang/bank-index-in-initializers
Browse files Browse the repository at this point in the history
Allow bank_index in initializers.
  • Loading branch information
edwardalee authored Apr 19, 2022
2 parents bde72b9 + 9e6d859 commit e1b40d7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
2 changes: 2 additions & 0 deletions org.lflang/src/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
43 changes: 43 additions & 0 deletions test/C/src/multiport/BankIndexInitializer.lf
Original file line number Diff line number Diff line change
@@ -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;
}
41 changes: 41 additions & 0 deletions test/Python/src/multiport/BankIndexInitializer.lf
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit e1b40d7

Please sign in to comment.