Skip to content

Commit

Permalink
Add minimal code to compile LF for FlexPRET with uC reactor. Does not…
Browse files Browse the repository at this point in the history
… print the expected "hello world"
  • Loading branch information
magnmaeh committed Oct 1, 2024
1 parent 25d939a commit 55322e8
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/org/lflang/target/Target.java
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,8 @@ public void initialize(TargetConfig config) {
WorkersProperty.INSTANCE);
case UC ->
config.register(
BuildTypeProperty.INSTANCE);
BuildTypeProperty.INSTANCE,
PlatformProperty.INSTANCE);
case CPP ->
config.register(
BuildTypeProperty.INSTANCE,
Expand Down
21 changes: 18 additions & 3 deletions core/src/main/kotlin/org/lflang/generator/uc/UcCmakeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.lflang.target.TargetConfig
import org.lflang.generator.PrependOperator
import org.lflang.joinWithLn
import org.lflang.target.property.BuildTypeProperty
import org.lflang.target.property.PlatformProperty
import org.lflang.target.property.CmakeIncludeProperty
import org.lflang.target.property.ExternalRuntimePathProperty
import org.lflang.target.property.RuntimeVersionProperty
Expand All @@ -14,22 +15,36 @@ import java.nio.file.Path

class UcCmakeGenerator(private val targetConfig: TargetConfig, private val fileConfig: FileConfig) {
private val S = '$' // a little trick to escape the dollar sign with $S
// FIXME: I literally don't know how to uppercase a string in Kotlin...
private val platformName = targetConfig.get(PlatformProperty.INSTANCE).platform
fun generateCmake(sources: List<Path>) = with(PrependOperator) {
"""
|cmake_minimum_required(VERSION 3.5)
|set(PLATFORM "FLEXPRET" CACHE STRING "Platform to target")
|
|if ($S{PLATFORM} STREQUAL "FLEXPRET")
| # Must include toolchain file before project(), otherwise cmake
| # will determine its own compiler settings
| include(${S}ENV{FP_SDK_PATH}/cmake/riscv-toolchain.cmake)
|endif()
|
|project(${fileConfig.name} VERSION 0.0.0 LANGUAGES C)
|set(PLATFORM "POSIX" CACHE STRING "Platform to target")
|
|set(LF_MAIN_TARGET ${fileConfig.name})
|set(SOURCES
${" | "..sources.joinWithLn { it.toUnixString() }}
|)
|if (PLATFORM STREQUAL "POSIX")
|if ($S{PLATFORM} STREQUAL "POSIX")
| add_library($S{LF_MAIN_TARGET} STATIC $S{SOURCES})
|elseif (PLATFORM STREQUAL "ZEPHYR")
|elseif ($S{PLATFORM} STREQUAL "ZEPHYR")
| zephyr_library_named($S{LF_MAIN_TARGET})
| zephyr_library_sources($S{SOURCES})
| zephyr_library_link_libraries(kernel)
|elseif ($S{PLATFORM} STREQUAL "FLEXPRET")
| add_library($S{LF_MAIN_TARGET} STATIC $S{SOURCES})
| # Include FlexPRET's SDK functionality
| include(${S}ENV{FP_SDK_PATH}/cmake/fp-app.cmake)
| add_subdirectory(${S}ENV{FP_SDK_PATH} BINARY_DIR)
|else()
| message(FATAL_ERROR "PLATFORM not defined")
|endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ class UcReactorGenerator(private val reactor: Reactor, fileConfig: UcFileConfig,
""".trimMargin()
}

// FIXME: 3rd argument must be replaced with self->parent if not main reactor
fun generateCtorDefinition() = with(PrependOperator) {
"""
| void ${reactor.name}_ctor(${reactor.name} *self, Environment *env) {
| size_t trigger_idx = 0;
| Reactor_ctor(&self->super, "${reactor.name}", env, ${if (numChildren > 0) "self->_children" else "NULL"}, $numChildren, ${if (reactor.reactions.size > 0) "self->_reactions" else "NULL"}, ${reactor.reactions.size}, ${if (numTriggers() > 0) "self->_triggers" else "NULL"}, ${numTriggers()});
| Reactor_ctor(&self->super, "${reactor.name}", env, NULL, ${if (numChildren > 0) "self->_children" else "NULL"}, $numChildren, ${if (reactor.reactions.size > 0) "self->_reactions" else "NULL"}, ${reactor.reactions.size}, ${if (numTriggers() > 0) "self->_triggers" else "NULL"}, ${numTriggers()});
${" | "..timers.generateReactorCtorCodes()}
${" | "..reactions.generateReactorCtorCodes()}
| }
Expand Down
26 changes: 26 additions & 0 deletions test/uC/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.22)

# Set toolchain to RISC-V; must be done before call to project()
include($ENV{FP_SDK_PATH}/cmake/riscv-toolchain.cmake)

project(lf-fp-uc
LANGUAGES C
DESCRIPTION "LF on FlexPRET using uC runtime"
VERSION 1.0.0
)

add_executable(fp-lf-uc main.c)

target_link_libraries(fp-lf-uc PRIVATE
${CMAKE_CURRENT_LIST_DIR}/../build/libFlexPRET_uC.a
${CMAKE_CURRENT_LIST_DIR}/../build/reactor-uc/libreactor-uc.a
)

set(DEBUG true)

add_subdirectory($ENV{FP_SDK_PATH} BINARY_DIR)
target_link_libraries(fp-lf-uc PRIVATE fp-sdk)

include($ENV{FP_SDK_PATH}/cmake/fp-app.cmake)

fp_add_outputs(fp-lf-uc)
4 changes: 4 additions & 0 deletions test/uC/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "../src-gen/lf_main.h"
int main(void) {
lf_main(0, 0);
}
10 changes: 10 additions & 0 deletions test/uC/src/FlexPRET_uC.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
target uC {
platform: "FlexPRET"
}

main reactor {
timer t(0, 100 msec)
reaction(t) {=
printf("Hello World from FlexPRET!\n");
=}
}

0 comments on commit 55322e8

Please sign in to comment.