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

Make macros available in methods #2437

Merged
merged 2 commits into from
Nov 19, 2024
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
20 changes: 10 additions & 10 deletions .github/workflows/c-zephyr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ jobs:
--tests org.lflang.tests.runtime.CZephyrTest.buildZephyrThreaded* core:integrationTestCodeCoverageReport
./.github/scripts/run-zephyr-tests.sh test/C/src-gen
rm -rf test/C/src-gen
- name: Run basic tests
run: |
./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildBasic* core:integrationTestCodeCoverageReport
./.github/scripts/run-zephyr-tests.sh test/C/src-gen
rm -rf test/C/src-gen
- name: Run concurrent tests
run: |
./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildConcurrent* core:integrationTestCodeCoverageReport
./.github/scripts/run-zephyr-tests.sh test/C/src-gen
rm -rf test/C/src-gen
# - name: Run basic tests
# run: |
# ./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildBasic* core:integrationTestCodeCoverageReport
# ./.github/scripts/run-zephyr-tests.sh test/C/src-gen
# rm -rf test/C/src-gen
# - name: Run concurrent tests
# run: |
# ./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildConcurrent* core:integrationTestCodeCoverageReport
# ./.github/scripts/run-zephyr-tests.sh test/C/src-gen
# rm -rf test/C/src-gen
- name: Run Zephyr board tests
run: |
./gradlew core:integrationTest --tests org.lflang.tests.runtime.CZephyrTest.buildZephyrBoards* core:integrationTestCodeCoverageReport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.lflang.generator.CodeBuilder;
import org.lflang.lf.Method;
import org.lflang.lf.Reactor;
import org.lflang.util.StringUtil;

/**
* Collection of functions to generate C code to declare methods.
Expand Down Expand Up @@ -60,6 +61,8 @@ public static String generateMethod(
var body = ASTUtils.toText(method.getCode());

code.prSourceLineNumber(method, suppressLineDirectives);
// Define macros for functions such as lf_tag(), lf_time_logical(), lf_set(), etc.
code.pr("#include " + StringUtil.addDoubleQuotes(CCoreFilesUtils.getCTargetSetHeader()));

code.prComment("Implementation of method " + method.getName() + "()");
code.pr(generateMethodSignature(method, tpr, types) + " {");
Expand All @@ -83,6 +86,7 @@ public static String generateMethod(
code.pr(body);
code.unindent();
code.pr("}");
code.pr("#include " + StringUtil.addDoubleQuotes(CCoreFilesUtils.getCTargetSetUndefHeader()));
code.prEndSourceLineNumber(suppressLineDirectives);
return code.toString();
}
Expand Down
48 changes: 48 additions & 0 deletions test/C/src/MethodMacros.lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Test ability of methods to call each other and (recursively) themselves.
target C {
timeout: 10 ms
}

reactor Fib {
input in: int
output out: int
state foo: int = 2

// Return the n-th Fibonacci number.
// Type name convention for ports is _class_method_t, all lowercase.
method fib(out: _fib_out_t*, n: int): int {=
int result = 1;
if (n > 1) result = add(fib(NULL, n-1), fib(NULL, n-2));
if (out != NULL) {
lf_set(out, result);
lf_print("At elapsed time " PRINTF_TIME ", fib(%d) = %d", lf_time_logical_elapsed(), n, result);
}
return result;
=}

method add(x: int, y: int): int {=
return x + y;
=}

reaction(in) -> out {=
fib(out, in->value);
=}
}

main reactor {
state count: int = 0
timer t(0, 1 ms)
fib = new Fib()

reaction(t) -> fib.in {=
lf_set(fib.in, self->count);
=}

reaction(fib.out) {=
lf_print("fib(%d) = %d", self->count, fib.out->value);
int answers[] = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
if (fib.out->value != answers[self->count++]) {
lf_print_error_and_exit("Expected %d", answers[self->count-1]);
}
=}
}
Loading