Skip to content

Commit

Permalink
Support banks for full name
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardalee committed Dec 28, 2024
1 parent 7c45b31 commit 1f3844d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,39 @@ private static String deferredAllocationForEffectsOnInputs(ReactorInstance react
*/
private static String deferredSetParentAndName(ReactorInstance reactor) {
var code = new CodeBuilder();
code.pr(CUtil.reactorRef(reactor) + "->base.name = \"" + reactor.getName() + "\";");
if (reactor.isBank()) {
// First, generate code to determine the size of the memory needed for the name.
code.pr("char* format = \"%s[%d]\";");
code.pr(
"int length = snprintf(NULL, 0, format, \""
+ reactor.getName()
+ "\", "
+ CUtil.bankIndexName(reactor)
+ ");\n");
code.pr(
CUtil.reactorRef(reactor)
+ "->base.name = (char*)lf_allocate(length + 1, sizeof(char),"
+ " (allocation_record_t**)&((self_base_t*)"
+ CUtil.reactorRef(reactor)
+ ")->allocations);");
code.pr(
"if("
+ CUtil.reactorRef(reactor)
+ "->base.name != NULL) {"); // Will be NULL if lf_allocate fails.
code.indent();
code.pr(
"snprintf("
+ CUtil.reactorRef(reactor)
+ "->base.name, length + 1, format, \""
+ reactor.getName()
+ "\", "
+ CUtil.bankIndexName(reactor)
+ ");");
code.unindent();
code.pr("}");
} else {
code.pr(CUtil.reactorRef(reactor) + "->base.name = \"" + reactor.getName() + "\";");
}
ReactorInstance parent = reactor.getParent();
if (parent == null) {
code.pr(CUtil.reactorRef(reactor) + "->base.parent = (self_base_t*)NULL;");
Expand Down
16 changes: 10 additions & 6 deletions test/C/src/ReactorName.lf
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ preamble {=
#include <string.h>
=}

reactor A {
reactor A(parent_bank_index: size_t = 0) {
reaction(startup) {=
const char* name = lf_reactor_full_name(self);
lf_print("name: %s", name);
if (strcmp("ReactorName.b.a", name) != 0) {
char buffer[20];
snprintf(buffer, 20, "ReactorName.b[%zu].a", self->parent_bank_index);
if (strcmp(buffer, name) != 0) {
lf_print_error_and_exit("name does not match");
}
=}
}

reactor B {
a = new A()
reactor B(bank_index: size_t = 0) {
a = new A(parent_bank_index=bank_index)

reaction(startup) {=
const char* name = lf_reactor_full_name(self);
lf_print("name: %s", name);
if (strcmp("ReactorName.b", name) != 0) {
char buffer[20];
snprintf(buffer, 20, "ReactorName.b[%zu]", self->bank_index);
if (strcmp(buffer, name) != 0) {
lf_print_error_and_exit("name does not match");
}
=}
}

main reactor {
b = new B()
b = new[3] B()

reaction(startup) {=
const char* name = lf_reactor_full_name(self);
Expand Down

0 comments on commit 1f3844d

Please sign in to comment.