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

lf_ prefix more functions and reorganize files in C target #1143

Merged
merged 8 commits into from
May 10, 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
39 changes: 28 additions & 11 deletions org.lflang/src/org/lflang/generator/c/CCoreFilesUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
public class CCoreFilesUtils {
public static List<String> getCoreFiles(
boolean isFederated,
boolean threading,
boolean isFederated,
boolean threading,
SchedulerOption scheduler
) {
List<String> coreFiles = new ArrayList<>();
Expand All @@ -26,13 +26,30 @@ public static List<String> getCoreFiles(
coreFiles.addAll(getThreadSupportFiles(threading, scheduler));
return coreFiles;
}

public static List<String> getCTargetSrc() {
return List.of(
"ctarget/schedule.c",
"ctarget/util.c"
"lib/schedule.c",
"lib/util.c",
"lib/tag.c",
"lib/time.c"
);
}

public static List<String> getCTargetHeader() {
return List.of(
"include/ctarget/ctarget.h"
);
}

public static String getCTargetSetHeader() {
return "include/ctarget/set.h";
}

public static String getCTargetSetUndefHeader() {
return "include/ctarget/set_undef.h";
}

private static List<String> getBaseCoreFiles() {
return List.of(
"reactor_common.c",
Expand All @@ -48,7 +65,7 @@ private static List<String> getBaseCoreFiles() {
"utils/vector.h",
"utils/semaphore.h",
"utils/semaphore.c",
"utils/util.h",
"utils/util.h",
"utils/util.c",
"platform.h",
"platform/Platform.cmake",
Expand Down Expand Up @@ -82,19 +99,19 @@ private static List<String> getFederatedFiles() {
return List.of(
"federated/net_util.c",
"federated/net_util.h",
"federated/net_common.h",
"federated/federate.c",
"federated/federate.h",
"federated/clock-sync.h",
"federated/net_common.h",
"federated/federate.c",
"federated/federate.h",
"federated/clock-sync.h",
"federated/clock-sync.c"
);
}

private static List<String> getThreadSupportFiles(
boolean threading,
boolean threading,
SchedulerOption scheduler
) {
return threading ?
return threading ?
List.of(
"threaded/scheduler.h",
"threaded/scheduler_instance.h",
Expand Down
4 changes: 2 additions & 2 deletions org.lflang/src/org/lflang/generator/c/CGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -1276,12 +1276,12 @@ protected void initializeClockSynchronization() {
private void copyTargetFiles() throws IOException{
FileUtil.copyDirectoryFromClassPath(
"/lib/c/reactor-c/include",
fileConfig.getSrcGenPath(),
fileConfig.getSrcGenPath().resolve("include"),
false
);
FileUtil.copyDirectoryFromClassPath(
"/lib/c/reactor-c/lib",
fileConfig.getSrcGenPath(),
fileConfig.getSrcGenPath().resolve("lib"),
false
);
}
Expand Down
21 changes: 12 additions & 9 deletions org.lflang/src/org/lflang/generator/c/CPreambleGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import org.lflang.TargetProperty.CoordinationType;
import org.lflang.generator.CodeBuilder;
import org.lflang.generator.GeneratorBase;
import org.lflang.util.StringUtil;

import static org.lflang.util.StringUtil.addDoubleQuotes;

/**
* Generates code for preambles for the C and CCpp target.
* This includes #include and #define directives at the top
* of each generated ".c" file.
*
* of each generated ".c" file.
*
* @author{Edward A. Lee <eal@berkeley.edu>}
* @author{Marten Lohstroh <marten@berkeley.edu>}
* @author{Mehrdad Niknami <mniknami@berkeley.edu>}
Expand All @@ -30,12 +31,14 @@
public class CPreambleGenerator {
/** Add necessary source files specific to the target language. */
public static String generateIncludeStatements(
TargetConfig targetConfig,
TargetConfig targetConfig,
boolean isFederated
) {
var tracing = targetConfig.tracing;
CodeBuilder code = new CodeBuilder();
code.pr("#include \"ctarget/ctarget.h\"");
CCoreFilesUtils.getCTargetHeader().forEach(
it -> code.pr("#include " + StringUtil.addDoubleQuotes(it))
);
if (targetConfig.threading) {
code.pr("#include \"core/threaded/reactor_threaded.c\"");
code.pr("#include \"core/threaded/scheduler.h\"");
Expand Down Expand Up @@ -71,7 +74,7 @@ public static String generateDefineDirectives(
code.pr("#define NUMBER_OF_FEDERATES " + numFederates);
code.pr(generateFederatedDefineDirective(coordinationType));
if (advanceMessageInterval != null) {
code.pr("#define ADVANCE_MESSAGE_INTERVAL " +
code.pr("#define ADVANCE_MESSAGE_INTERVAL " +
GeneratorBase.timeInTargetLanguage(advanceMessageInterval));
}
}
Expand All @@ -92,12 +95,12 @@ public static String generateDefineDirectives(

/**
* Returns the #define directive for the given coordination type.
*
*
* NOTE: Instead of checking #ifdef FEDERATED, we could
* use #if (NUMBER_OF_FEDERATES > 1).
* To Soroush Bateni, the former is more accurate.
*/
private static String generateFederatedDefineDirective(CoordinationType coordinationType) {
private static String generateFederatedDefineDirective(CoordinationType coordinationType) {
List<String> directives = new ArrayList<>();
directives.add("#define FEDERATED");
if (coordinationType == CoordinationType.CENTRALIZED) {
Expand All @@ -122,12 +125,12 @@ private static String generateTracingDefineDirective(

/**
* Initialize clock synchronization (if enabled) and its related options for a given federate.
*
*
* Clock synchronization can be enabled using the clock-sync target property.
* @see https://github.com/icyphy/lingua-franca/wiki/Distributed-Execution#clock-synchronization
*/
private static String generateClockSyncDefineDirective(
ClockSyncMode mode,
ClockSyncMode mode,
ClockSyncOptions options
) {
List<String> code = new ArrayList<>();
Expand Down
10 changes: 8 additions & 2 deletions org.lflang/src/org/lflang/generator/c/CReactionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.lflang.lf.TriggerRef;
import org.lflang.lf.VarRef;
import org.lflang.lf.Variable;
import org.lflang.util.StringUtil;

import static org.lflang.util.StringUtil.addDoubleQuotes;

public class CReactionGenerator {
Expand Down Expand Up @@ -1187,7 +1189,9 @@ public static String generateReaction(
types, errorReporter, mainDef,
isFederatedAndDecentralized,
requiresType);
code.pr("#include \"ctarget/set.h\"");
code.pr(
"#include " + StringUtil.addDoubleQuotes(
CCoreFilesUtils.getCTargetSetHeader()));
code.pr(generateFunction(
generateReactionFunctionHeader(decl, reactionIndex),
init, reaction.getCode()
Expand All @@ -1208,7 +1212,9 @@ public static String generateReaction(
generateDeadlineFunctionHeader(decl, reactionIndex),
init, reaction.getDeadline().getCode()));
}
code.pr("#include \"ctarget/set_undef.h\"");
code.pr(
"#include " + StringUtil.addDoubleQuotes(
CCoreFilesUtils.getCTargetSetUndefHeader()));
return code.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import org.lflang.lf.Code;
import org.lflang.lf.TriggerRef;
import org.lflang.lf.VarRef;
import org.lflang.util.StringUtil;
import org.lflang.lf.Instantiation;
import org.lflang.lf.Port;
import org.lflang.lf.Input;
import org.lflang.lf.Output;
import org.lflang.generator.c.CCoreFilesUtils;
import org.lflang.generator.c.CReactionGenerator;
import org.lflang.generator.c.CTypes;
import org.lflang.generator.c.CUtil;
Expand Down Expand Up @@ -129,7 +131,9 @@ public static String generateCReaction(
types, errorReporter, mainDef,
isFederatedAndDecentralized,
Target.Python.requiresTypes);
code.pr("#include \"ctarget/set.h\"");
code.pr(
"#include " + StringUtil.addDoubleQuotes(
CCoreFilesUtils.getCTargetSetHeader()));
code.pr(generateFunction(
CReactionGenerator.generateReactionFunctionHeader(decl, reactionIndex),
cInit, reaction.getCode(),
Expand All @@ -144,7 +148,9 @@ public static String generateCReaction(
generateCPythonDeadlineCaller(decl, reactionIndex, pyObjects)
));
}
code.pr("#include \"ctarget/set_undef.h\"");
code.pr(
"#include " + StringUtil.addDoubleQuotes(
CCoreFilesUtils.getCTargetSetUndefHeader()));
return code.toString();
}

Expand Down