From c049c730b6e17134463bf8d5fef8eac204eaf659 Mon Sep 17 00:00:00 2001 From: erlingrj Date: Fri, 3 Jan 2025 11:53:07 +0100 Subject: [PATCH] Various fixes to get all standalone tests to pass again --- .../generator/uc/UcConnectionGenerator.kt | 8 +- .../generator/uc/UcFederatedMainGenerator.kt | 81 +++++++++++++++++++ .../uc/UcFederatedPlatformGenerator.kt | 4 +- .../org/lflang/generator/uc/UcGenerator.kt | 7 +- .../lflang/generator/uc/UcMainGenerator.kt | 30 +------ .../uc/UcStandalonePlatformGenerator.kt | 2 +- 6 files changed, 94 insertions(+), 38 deletions(-) create mode 100644 lfc/core/src/main/kotlin/org/lflang/generator/uc/UcFederatedMainGenerator.kt diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcConnectionGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcConnectionGenerator.kt index 521740d1..b3308138 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcConnectionGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcConnectionGenerator.kt @@ -70,13 +70,14 @@ class UcConnectionChannel(val src: UcChannel, val dest: UcChannel, val conn: Con } open class UcGroupedConnection2(val src: VarRef, val channels: List, 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 @@ -119,6 +120,7 @@ open class UcGroupedConnection2(val src: VarRef, val channels: Listduration = ${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() + } +} diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcFederatedPlatformGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcFederatedPlatformGenerator.kt index e372e4b2..3a9e8e0e 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcFederatedPlatformGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcFederatedPlatformGenerator.kt @@ -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)) diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcGenerator.kt index eb161f33..89d3e73a 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcGenerator.kt @@ -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() @@ -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() @@ -159,6 +158,8 @@ class UcGenerator( if (mainDef.isAFederate) { generateFederateFiles(mainDef, srcGenPath) + } else { + generateReactorFiles(mainDef.reactor, srcGenPath) } for (r in resources) { diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcMainGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcMainGenerator.kt index f88b7161..ad2c8fd6 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcMainGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcMainGenerator.kt @@ -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" @@ -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 diff --git a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcStandalonePlatformGenerator.kt b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcStandalonePlatformGenerator.kt index 3fb1b9e4..efcc1224 100644 --- a/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcStandalonePlatformGenerator.kt +++ b/lfc/core/src/main/kotlin/org/lflang/generator/uc/UcStandalonePlatformGenerator.kt @@ -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")