Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SET, schedule and tag APIs in the C target #1103

Merged
merged 14 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion org.lflang/src/lib/py/reactor-c-py
14 changes: 14 additions & 0 deletions org.lflang/src/org/lflang/generator/c/CCoreFilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ public static List<String> getCoreFiles(
return coreFiles;
}

public static List<String> getCTargetHeaders() {
return List.of(
"ctarget_schedule.h",
"ctarget_set.h",
"ctarget_set_undef.h"
);
}

public static List<String> getCTargetSrc() {
return List.of(
"ctarget_schedule.c"
);
}

private static List<String> getBaseCoreFiles() {
return List.of(
"reactor_common.c",
Expand Down
14 changes: 11 additions & 3 deletions org.lflang/src/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1274,8 +1274,16 @@ protected void initializeClockSynchronization() {
* Copy target-specific header file to the src-gen directory.
*/
public void copyTargetHeaderFile() throws IOException{
FileUtil.copyFileFromClassPath("/lib/c/reactor-c/include/ctarget.h", fileConfig.getSrcGenPath().resolve("ctarget.h"));
FileUtil.copyFileFromClassPath("/lib/c/reactor-c/lib/ctarget.c", fileConfig.getSrcGenPath().resolve("ctarget.c"));
FileUtil.copyFilesFromClassPath(
"/lib/c/reactor-c/include",
fileConfig.getSrcGenPath(),
CCoreFilesUtils.getCTargetHeaders()
);
FileUtil.copyFilesFromClassPath(
"/lib/c/reactor-c/lib",
fileConfig.getSrcGenPath(),
CCoreFilesUtils.getCTargetSrc()
);
}

////////////////////////////////////////////
Expand Down Expand Up @@ -2340,7 +2348,7 @@ public TargetTypes getTargetTypes() {
protected void setUpParameters(LFGeneratorContext context) {
accommodatePhysicalActionsIfPresent();
targetConfig.compileDefinitions.put("LOG_LEVEL", targetConfig.logLevel.ordinal() + "");
targetConfig.compileAdditionalSources.add("ctarget.c");
targetConfig.compileAdditionalSources.addAll(CCoreFilesUtils.getCTargetSrc());
targetConfig.compileAdditionalSources.add("core" + File.separator + "mixed_radix.c");
setCSpecificDefaults(context);
// Create the main reactor instance if there is a main reactor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static String generateIncludeStatements(
) {
var tracing = targetConfig.tracing;
CodeBuilder code = new CodeBuilder();
code.pr("#include \"ctarget.h\"");
code.pr("#include \"ctarget_schedule.h\"");
if (targetConfig.threading) {
code.pr("#include \"core/threaded/reactor_threaded.c\"");
code.pr("#include \"core/threaded/scheduler.h\"");
Expand Down
14 changes: 8 additions & 6 deletions org.lflang/src/org/lflang/generator/c/CReactionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,15 @@ public static String generateIntendedTagInheritence(String body, Reaction reacti
if (ASTUtils.isMultiport(outputPort)) {
intendedTagInheritenceCode.pr(String.join("\n",
"for (int i=0; i < "+containerName+"."+generateWidthVariable(variableName)+"; i++) {",
" if (compare_tags("+containerName+"."+variableName+"[i]->intended_tag,",
" if (lf_compare_tags("+containerName+"."+variableName+"[i]->intended_tag,",
" inherited_min_intended_tag) < 0) {",
" inherited_min_intended_tag = "+containerName+"."+variableName+"[i]->intended_tag;",
" }",
"}"
));
} else
intendedTagInheritenceCode.pr(String.join("\n",
"if (compare_tags("+containerName+"."+variableName+"->intended_tag,",
"if (lf_compare_tags("+containerName+"."+variableName+"->intended_tag,",
" inherited_min_intended_tag) < 0) {",
" inherited_min_intended_tag = "+containerName+"."+variableName+"->intended_tag;",
"}"
Expand All @@ -376,21 +376,21 @@ public static String generateIntendedTagInheritence(String body, Reaction reacti
if (ASTUtils.isMultiport(inputPort)) {
intendedTagInheritenceCode.pr(String.join("\n",
"for (int i=0; i < "+generateWidthVariable(variableName)+"; i++) {",
" if (compare_tags("+variableName+"[i]->intended_tag, inherited_min_intended_tag) < 0) {",
" if (lf_compare_tags("+variableName+"[i]->intended_tag, inherited_min_intended_tag) < 0) {",
" inherited_min_intended_tag = "+variableName+"[i]->intended_tag;",
" }",
"}"
));
} else {
intendedTagInheritenceCode.pr(String.join("\n",
"if (compare_tags("+variableName+"->intended_tag, inherited_min_intended_tag) < 0) {",
"if (lf_compare_tags("+variableName+"->intended_tag, inherited_min_intended_tag) < 0) {",
" inherited_min_intended_tag = "+variableName+"->intended_tag;",
"}"
));
}
} else if (variable instanceof Action) {
intendedTagInheritenceCode.pr(String.join("\n",
"if (compare_tags("+variableName+"->trigger->intended_tag, inherited_min_intended_tag) < 0) {",
"if (lf_compare_tags("+variableName+"->trigger->intended_tag, inherited_min_intended_tag) < 0) {",
" inherited_min_intended_tag = "+variableName+"->trigger->intended_tag;",
"}"
));
Expand All @@ -404,7 +404,7 @@ public static String generateIntendedTagInheritence(String body, Reaction reacti
// NOTE: this does not include contained outputs.
for (Input input : ((Reactor) reaction.eContainer()).getInputs()) {
intendedTagInheritenceCode.pr(String.join("\n",
"if (compare_tags("+input.getName()+"->intended_tag, inherited_min_intended_tag) > 0) {",
"if (lf_compare_tags("+input.getName()+"->intended_tag, inherited_min_intended_tag) > 0) {",
" inherited_min_intended_tag = "+input.getName()+"->intended_tag;",
"}"
));
Expand Down Expand Up @@ -1187,6 +1187,7 @@ public static String generateReaction(
types, errorReporter, mainDef,
isFederatedAndDecentralized,
requiresType);
code.pr("#include \"ctarget_set.h\"");
code.pr(generateFunction(
generateReactionFunctionHeader(decl, reactionIndex),
init, reaction.getCode()
Expand All @@ -1207,6 +1208,7 @@ public static String generateReaction(
generateDeadlineFunctionHeader(decl, reactionIndex),
init, reaction.getDeadline().getCode()));
}
code.pr("#include \"ctarget_set_undef.h\"");
return code.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ private static String initializeFederate(

// If a test clock offset has been specified, insert code to set it here.
if (targetConfig.clockSyncOptions.testOffset != null) {
code.pr("set_physical_clock_offset((1 + "+federate.id+") * "+targetConfig.clockSyncOptions.testOffset.toNanoSeconds()+"LL);");
code.pr("lf_set_physical_clock_offset((1 + "+federate.id+") * "+targetConfig.clockSyncOptions.testOffset.toNanoSeconds()+"LL);");
}

code.pr(String.join("\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public static String generateCReaction(
types, errorReporter, mainDef,
isFederatedAndDecentralized,
Target.Python.requiresTypes);
code.pr("#include \"ctarget_set.h\"");
code.pr(generateFunction(
CReactionGenerator.generateReactionFunctionHeader(decl, reactionIndex),
cInit, reaction.getCode(),
Expand All @@ -143,6 +144,7 @@ public static String generateCReaction(
generateCPythonDeadlineCaller(decl, reactionIndex, pyObjects)
));
}
code.pr("#include \"ctarget_set_undef.h\"");
return code.toString();
}

Expand Down
2 changes: 1 addition & 1 deletion test/C/src/SetArray.lf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ reactor Source {
reaction(startup) -> out {=
// Dynamically allocate an output array of length 3.
int* array = (int*)malloc(3 * sizeof(int));
SET_ARRAY(out, array, 3);
SET_ARRAY(out, array, sizeof(int), 3);

// Above allocates the array, which then must be populated.
out->value[0] = 0;
Expand Down
6 changes: 3 additions & 3 deletions test/C/src/SetCopyConstructor.lf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This tests SET_DESTRUCTOR()
// This tests lf_set_copy_constructor()
// This tests the use of the "polymorphic" delay reactor on a struct.
// It delays by a logical time any pointer datatype.
target C {files: ["include/array.h"]};
Expand All @@ -10,8 +10,8 @@ preamble {=
reactor Source {
output out:int_array_t*;
reaction(startup) -> out {=
SET_COPY_CONSTRUCTOR(out, int_array_copy_constructor);
SET_DESTRUCTOR(out, int_array_destructor);
lf_set_copy_constructor(out, int_array_copy_constructor);
lf_set_destructor(out, int_array_destructor);
int_array_t* arr = int_array_constructor(1000);
arr->data[0] = 42;
arr->data[1] = 24;
Expand Down
4 changes: 2 additions & 2 deletions test/C/src/SetDestructor.lf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This tests SET_DESTRUCTOR()
// This tests lf_set_destructor()
// This tests the use of the "polymorphic" delay reactor on a struct.
// It delays by a logical time any pointer datatype.
target C {files: ["include/array.h"]};
Expand All @@ -11,7 +11,7 @@ reactor Source {
output out:int_array_t*;
reaction(startup) -> out {=
info_print("%d", out->destructor);
SET_DESTRUCTOR(out, int_array_destructor);
lf_set_destructor(out, int_array_destructor);
int_array_t* array = (int_array_t*) malloc(sizeof(int_array_t));
array->length = 1000;
array->data = (int*) calloc(array->length, sizeof(int));
Expand Down
4 changes: 3 additions & 1 deletion test/C/src/include/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ void* int_array_copy_constructor(void* arr) {
int_array_t* val = (int_array_t*) malloc(sizeof(int_array_t));
val->data = (int*) calloc(other_arr->length, sizeof(int));
val->length = other_arr->length;
memcpy(val->data, other_arr->data, other_arr->length * sizeof(int));
for (size_t i = 0; i < other_arr->length; i++) {
val->data[i] = other_arr->data[i];
}
return (void*) val;
}

Expand Down