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

Partial refactoring of CGenerator.xtend #1045

Merged
merged 18 commits into from
Mar 16, 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
32 changes: 16 additions & 16 deletions org.lflang/src/org/lflang/federated/CGeneratorExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
package org.lflang.federated;

import org.lflang.ASTUtils;
import org.lflang.TargetConfig;
import org.lflang.ASTUtils;
import org.lflang.TimeValue;
import org.lflang.generator.ReactorInstance;
Expand Down Expand Up @@ -73,16 +74,17 @@ public class CGeneratorExtension {
*/
public static String allocateTriggersForFederate(
FederateInstance federate,
CGenerator generator,
int startTimeStepIsPresentCount
int startTimeStepIsPresentCount,
boolean isFederated,
boolean isFederatedAndDecentralized
) {

StringBuilder builder = new StringBuilder();

// Create the table to initialize intended tag fields to 0 between time
// steps.
if (generator.isFederatedAndDecentralized()
&& startTimeStepIsPresentCount > 0) {
if (isFederatedAndDecentralized &&
startTimeStepIsPresentCount > 0) {
// Allocate the initial (before mutations) array of pointers to
// intended_tag fields.
// There is a 1-1 map between structs containing is_present and
Expand All @@ -96,7 +98,7 @@ public static String allocateTriggersForFederate(
+ "_lf_intended_tag_fields_size * sizeof(tag_t*));\n");
}

if (generator.isFederated) {
if (isFederated) {
if (federate.networkInputControlReactionsTriggers.size() > 0) {
// Proliferate the network input control reaction trigger array
builder.append(
Expand Down Expand Up @@ -133,16 +135,16 @@ public static String allocateTriggersForFederate(
* extension function static.
* @return A string that initializes the aforementioned three structures.
*/
public static StringBuilder initializeTriggerForControlReactions(
ReactorInstance instance, FederateInstance federate,
CGenerator generator) {

public static String initializeTriggerForControlReactions(
ReactorInstance instance,
ReactorInstance main,
FederateInstance federate
) {
StringBuilder builder = new StringBuilder();

// The network control reactions are always in the main federated
// reactor
if (instance != generator.main) {
return builder;
if (instance != main) {
return "";
}

ReactorDecl reactorClass = instance.getDefinition().getReactorClass();
Expand Down Expand Up @@ -181,19 +183,17 @@ public static StringBuilder initializeTriggerForControlReactions(
+ "->_lf__outputControlReactionTrigger;\n");
}

return builder;
return builder.toString();
}

/**
* Create a port status field variable for a network input port "input" in
* the self struct of a reactor.
*
* @param input The network input port
* @param generator The instance of the CGenerator
* @return A string containing the appropriate variable
*/
public static String createPortStatusFieldForInput(Input input,
CGenerator generator) {
public static String createPortStatusFieldForInput(Input input) {
StringBuilder builder = new StringBuilder();
// Check if the port is a multiport
if (ASTUtils.isMultiport(input)) {
Expand Down
73 changes: 71 additions & 2 deletions org.lflang/src/org/lflang/generator/c/CActionGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.util.List;
import java.util.ArrayList;

import org.lflang.ASTUtils;
import org.lflang.Target;
import org.lflang.federated.FederateInstance;
import org.lflang.generator.ActionInstance;
import org.lflang.generator.CodeBuilder;
Expand All @@ -12,7 +12,7 @@
import org.lflang.lf.Action;
import org.lflang.lf.Reactor;
import org.lflang.lf.ReactorDecl;

import static org.lflang.generator.c.CGenerator.variableStructType;
/**
* Generates code for actions (logical or physical) for the C and CCpp target.
*
Expand Down Expand Up @@ -93,6 +93,15 @@ public static String generateTokenInitializer(
);
}

/**
* Generate the declarations of actions in the self struct
*
* @param reactor The reactor to generatet declarations for
* @param decl The reactor's declaration
* @param currentFederate The federate that is being generated
* @param body The content of the self struct
* @param constructorCode The constructor code of the reactor
*/
public static void generateDeclarations(
Reactor reactor,
ReactorDecl decl,
Expand All @@ -109,4 +118,64 @@ public static void generateDeclarations(
}
}
}

/**
* Generate the struct type definitions for the action of the
* reactor
*
* @param decl The reactor declaration
* @param action The action to generate the struct for
* @param target The target of the code generation (C, CCpp or Python)
* @param types The helper object for types related stuff
* @param federatedExtension The code needed to support federated execution
* @return The auxiliary struct for the port as a string
*/
public static String generateAuxiliaryStruct(
ReactorDecl decl,
Action action,
Target target,
CTypes types,
CodeBuilder federatedExtension
) {
var code = new CodeBuilder();
code.pr("typedef struct {");
code.indent();
code.pr("trigger_t* trigger;");
code.pr(valueDeclaration(action, target, types));
code.pr(String.join("\n",
"bool is_present;",
"bool has_value;",
"lf_token_t* token;"
));
code.pr(federatedExtension.toString());
code.unindent();
code.pr("} " + variableStructType(action, decl) + ";");
return code.toString();
}

/**
* For the specified action, return a declaration for action struct to
* contain the value of the action. An action of
* type int[10], for example, will result in this:
* ```
* int* value;
* ```
* This will return an empty string for an action with no type.
* @param action The action.
* @return A string providing the value field of the action struct.
*/
private static String valueDeclaration(
Action action,
Target target,
CTypes types
) {
if (target == Target.Python) {
return "PyObject* value;";
}
// Do not convert to lf_token_t* using lfTypeToTokenType because there
// will be a separate field pointing to the token.
return action.getType() == null && target.requiresTypes == true ?
"" :
types.getTargetType(action) + " value;";
}
}
Loading