Skip to content

Commit

Permalink
Various fixes to get all standalone tests to pass again
Browse files Browse the repository at this point in the history
  • Loading branch information
erlingrj committed Jan 3, 2025
1 parent 8efd065 commit c049c73
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@ class UcConnectionChannel(val src: UcChannel, val dest: UcChannel, val conn: Con
}

open class UcGroupedConnection2(val src: VarRef, val channels: List<UcConnectionChannel>, val lfConn: Connection, val uid: Int) {
val delay = lfConn.delay
val delay = lfConn.delay.orNever().toCCode()
val isPhysical = lfConn.isPhysical
val isLogical = lfConn.isLogical
val srcInst = src.container
val srcPort = src.variable as Port

val isFederated = lfConn.isFederated && channels.first().dest.varRef.container != srcInst
val isDelayed = lfConn.isPhysical || !lfConn.isLogical

val serializeFunc = "serialize_payload_default"
val deserializeFunc = "deserialize_payload_default"
val bankWidth = srcInst?.width ?: 1
Expand Down Expand Up @@ -119,6 +120,7 @@ open class UcGroupedConnection2(val src: VarRef, val channels: List<UcConnection
val groupedChannels = others.plus(c)
val groupedConnection = UcGroupedConnection2(c.src.varRef, groupedChannels, c.conn, res.size)
res.add(groupedConnection)
channels.removeAll(groupedChannels)
}
return res
}
Expand Down Expand Up @@ -486,7 +488,7 @@ class UcConnectionGenerator(private val reactor: Reactor, private val federate:
postfix = "\n"
) {
if (it.isFederated) generateFederatedConnectionCtor(it)
else if (it.isPhysical || it.delay != null) generateDelayedCtor(it)
else if (it.isDelayed) generateDelayedCtor(it)
else generateLogicalCtor(it)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.lflang.generator.uc

import org.lflang.target.TargetConfig
import org.lflang.generator.PrependOperator
import org.lflang.generator.uc.UcInstanceGenerator.Companion.codeTypeFederate
import org.lflang.generator.uc.UcReactorGenerator.Companion.codeType
import org.lflang.inferredType
import org.lflang.lf.Instantiation
import org.lflang.lf.Parameter
import org.lflang.lf.Reactor
import org.lflang.reactor
import org.lflang.target.property.FastProperty
import org.lflang.target.property.KeepaliveProperty
import org.lflang.target.property.PlatformProperty
import org.lflang.target.property.TimeOutProperty
import org.lflang.target.property.type.PlatformType
import org.lflang.toUnixString

class UcFederatedMainGenerator(
private val main: Instantiation,
private val targetConfig: TargetConfig,
private val fileConfig: UcFileConfig,
) {

private val top = main.eContainer() as Reactor
private val ucParameterGenerator = UcParameterGenerator(main.reactor)
private val ucConnectionGenerator = UcConnectionGenerator(top, main)

fun getDuration() = if (targetConfig.isSet(TimeOutProperty.INSTANCE)) targetConfig.get(TimeOutProperty.INSTANCE).toCCode() else "FOREVER"

fun keepAlive() = if(targetConfig.isSet(KeepaliveProperty.INSTANCE)) "true" else "false"

fun fast() = if(targetConfig.isSet(FastProperty.INSTANCE)) "true" else "false"

fun generateStartSource() = with(PrependOperator) {
"""
|#include "reactor-uc/reactor-uc.h"
|#include "Federate.h"
|static ${main.codeTypeFederate} main_reactor;
|static Environment lf_environment;
|void lf_exit(void) {
| Environment_free(&lf_environment);
|}
|void lf_start(void) {
| Environment_ctor(&lf_environment, (Reactor *)&main_reactor);
| lf_environment.scheduler->duration = ${getDuration()};
| lf_environment.scheduler->keep_alive = ${keepAlive()};
| lf_environment.scheduler->leader = ${top.instantiations.first() == main};
| lf_environment.fast_mode = ${fast()};
| lf_environment.has_async_events = ${main.reactor.inputs.isNotEmpty()};
| ${main.codeTypeFederate}_ctor(&main_reactor, NULL, &lf_environment);
| lf_environment.net_bundles_size = ${ucConnectionGenerator.getNumFederatedConnectionBundles()};
| lf_environment.net_bundles = (FederatedConnectionBundle **) &main_reactor._bundles;
| lf_environment.assemble(&lf_environment);
| lf_environment.start(&lf_environment);
| lf_exit();
|}
""".trimMargin()
}

fun generateStartHeader() = with(PrependOperator) {
"""
|#ifndef REACTOR_UC_LF_MAIN_H
|#define REACTOR_UC_LF_MAIN_H
|
|void lf_start(void);
|
|#endif
|
""".trimMargin()
}

fun generateMainSource() = with(PrependOperator) {
"""
|#include "lf_start.h"
|int main(void) {
| lf_start();
|}
""".trimMargin()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class UcFederatedPlatformGenerator(generator: UcGenerator, val srcGenPath: Path)
return;
}
val runtimePath: Path = Paths.get(reactorUCEnvPath)
val mainGenerator = UcMainGenerator(mainReactor, generator.mainDef, generator.targetConfig, generator.fileConfig)
val mainGenerator = UcFederatedMainGenerator(generator.mainDef, generator.targetConfig, generator.fileConfig)

val startSourceFile = Paths.get("lf_start.c")
val startHeaderFile = Paths.get("lf_start.h")
val mainSourceFile = Paths.get("lf_main.c")

val startCodeMap = CodeMap.fromGeneratedCode(mainGenerator.generateFederatedStartSource())
val startCodeMap = CodeMap.fromGeneratedCode(mainGenerator.generateStartSource())
val mainCodeMap = CodeMap.fromGeneratedCode(mainGenerator.generateMainSource())

ucSources.addAll(listOf(startSourceFile, mainSourceFile))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ class UcGenerator(
fun doGenerateTopLevel(resource: Resource, context: LFGeneratorContext, srcGenPath: Path, platformGenerator: UcPlatformGenerator): GeneratorResult.Status {
if (!canGenerate(errorsOccurred(), mainDef, messageReporter, context)) return GeneratorResult.Status.FAILED


// generate all core files
generateFiles(mainDef, srcGenPath, getAllImportedResources(mainDef.eResource()))
generateFiles(mainDef, srcGenPath, getAllImportedResources(resource))

// generate platform specific files
platformGenerator.generatePlatformFiles()
Expand Down Expand Up @@ -96,7 +95,7 @@ class UcGenerator(
codeMaps.clear()
ucSources.clear()
val srcGenPath = fileConfig.srcGenPath.resolve(federate.name)
val res = doGenerateTopLevel(resource, context, srcGenPath, UcFederatedPlatformGenerator(this, srcGenPath))
val res = doGenerateTopLevel(federate.eResource()!!, context, srcGenPath, UcFederatedPlatformGenerator(this, srcGenPath))

if (res == GeneratorResult.Status.FAILED) {
context.unsuccessfulFinish()
Expand Down Expand Up @@ -159,6 +158,8 @@ class UcGenerator(

if (mainDef.isAFederate) {
generateFederateFiles(mainDef, srcGenPath)
} else {
generateReactorFiles(mainDef.reactor, srcGenPath)
}

for (r in resources) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import org.lflang.toUnixString

class UcMainGenerator(
private val main: Reactor,
private val mainDef: Instantiation,
private val targetConfig: TargetConfig,
private val fileConfig: UcFileConfig,
) {

private val top = mainDef.eContainer() as Reactor
private val ucParameterGenerator = UcParameterGenerator(main)
private val ucConnectionGenerator = UcConnectionGenerator(top, mainDef)
private val ucConnectionGenerator = UcConnectionGenerator(main, null)

fun getDuration() = if (targetConfig.isSet(TimeOutProperty.INSTANCE)) targetConfig.get(TimeOutProperty.INSTANCE).toCCode() else "FOREVER"

Expand Down Expand Up @@ -55,32 +53,6 @@ class UcMainGenerator(
""".trimMargin()
}

fun generateFederatedStartSource() = with(PrependOperator) {
"""
|#include "reactor-uc/reactor-uc.h"
|#include "Federate.h"
|static ${mainDef.codeTypeFederate} main_reactor;
|static Environment lf_environment;
|void lf_exit(void) {
| Environment_free(&lf_environment);
|}
|void lf_start(void) {
| Environment_ctor(&lf_environment, (Reactor *)&main_reactor);
| lf_environment.scheduler->duration = ${getDuration()};
| lf_environment.scheduler->keep_alive = ${keepAlive()};
| lf_environment.scheduler->leader = ${top.instantiations.first() == mainDef};
| lf_environment.fast_mode = ${fast()};
| lf_environment.has_async_events = ${mainDef.reactor.inputs.isNotEmpty()};
| ${mainDef.codeTypeFederate}_ctor(&main_reactor, NULL, &lf_environment);
| lf_environment.net_bundles_size = ${ucConnectionGenerator.getNumFederatedConnectionBundles()};
| lf_environment.net_bundles = (FederatedConnectionBundle **) &main_reactor._bundles;
| lf_environment.assemble(&lf_environment);
| lf_environment.start(&lf_environment);
| lf_exit();
|}
""".trimMargin()
}

fun generateStartHeader() = with(PrependOperator) {
"""
|#ifndef REACTOR_UC_LF_MAIN_H
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class UcStandalonePlatformGenerator(generator: UcGenerator, val srcGenPath: Path
}
val runtimePath: Path = Paths.get(reactorUCEnvPath)
// generate the main source file (containing main())
val mainGenerator = UcMainGenerator(mainReactor, generator.mainDef, generator.targetConfig, generator.fileConfig)
val mainGenerator = UcMainGenerator(mainReactor, generator.targetConfig, generator.fileConfig)

val startSourceFile = Paths.get("lf_start.c")
val startHeaderFile = Paths.get("lf_start.h")
Expand Down

0 comments on commit c049c73

Please sign in to comment.