diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/LinkStage.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/LinkStage.kt index 83c4b5e8994..9fa7f23e24c 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/LinkStage.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/LinkStage.kt @@ -120,10 +120,10 @@ internal class LinkStage(val context: Context) { hostLlvmTool("llvm-link", "-o", combinedBc, *bitcodeFiles.toTypedArray()) val optimizedBc = temporary("optimized", ".bc") - hostLlvmTool("opt", combinedBc, "-o=$optimizedBc", "-O3") + hostLlvmTool("opt", combinedBc, "-o=$optimizedBc", "-O3", "-internalize", "-globaldce") val combinedO = temporary("combined", ".o") - hostLlvmTool("llc", combinedBc, "-filetype=obj", "-o", combinedO, "-function-sections", "-data-sections") + hostLlvmTool("llc", optimizedBc, "-filetype=obj", "-o", combinedO, "-function-sections", "-data-sections") return combinedO } diff --git a/runtime/src/main/cpp/Porting.cpp b/runtime/src/main/cpp/Porting.cpp index 17f166bdb81..02e6e5b0736 100644 --- a/runtime/src/main/cpp/Porting.cpp +++ b/runtime/src/main/cpp/Porting.cpp @@ -444,35 +444,5 @@ extern "C" { RUNTIME_USED void Konan_abort(const char*) { while(1) {} } - - /* Support the alias for the __aeabi_memset which may - assume memory alignment. */ - RUNTIME_USED void __aeabi_memset4 (void *dest, size_t n, int c) - __attribute__ ((alias ("__aeabi_memset"))); - - RUNTIME_USED void __aeabi_memset8 (void *dest, size_t n, int c) - __attribute__ ((alias ("__aeabi_memset"))); - - /* Support the routine __aeabi_memset. Can't alias to memset - because it's not defined in the same translation unit. */ - RUNTIME_USED void __aeabi_memset (void *dest, size_t n, int c) - { - /*Note that relative to ANSI memset, __aeabi_memset hase the order - of its second and third arguments reversed. */ - memset (dest, c, n); - } - /* Support the alias for the __aeabi_memclr which may - assume memory alignment. */ - RUNTIME_USED void __aeabi_memclr4 (void *dest, size_t n) - __attribute__ ((alias ("__aeabi_memclr"))); - - RUNTIME_USED void __aeabi_memclr8 (void *dest, size_t n) - __attribute__ ((alias ("__aeabi_memclr"))); - - /* Support the routine __aeabi_memclr. */ - RUNTIME_USED void __aeabi_memclr (void *dest, size_t n) - { - __aeabi_memset (dest, n, 0); - } #endif // KONAN_ZEPHYR } diff --git a/samples/zephyr/CMakeLists.txt b/samples/zephyr/CMakeLists.txt index 3a7ab86fc85..987d38a10fd 100644 --- a/samples/zephyr/CMakeLists.txt +++ b/samples/zephyr/CMakeLists.txt @@ -1,4 +1,10 @@ include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) project(NONE) -target_sources(app PRIVATE build/program.o src/main.c src/blinky.c) +add_library(libsupcpp STATIC IMPORTED) + +set_property(TARGET libsupcpp PROPERTY IMPORTED_LOCATION $ENV{GCCARMEMB_TOOLCHAIN_PATH}/arm-none-eabi/lib/thumb/libsupc++.a ) + +target_link_libraries(app libsupcpp) + +target_sources(app PRIVATE src/empty.c build/program.o ) diff --git a/samples/zephyr/build.sh b/samples/zephyr/build.sh index 9df9276866f..8aa7922b56b 100755 --- a/samples/zephyr/build.sh +++ b/samples/zephyr/build.sh @@ -22,14 +22,35 @@ fi GCC_ARM="$KONAN_DEPS/$TOOLCHAIN" +rm -rf $DIR/build || exit 1 mkdir -p $DIR/build && cd $DIR/build -konanc $DIR/src/main.kt -target zephyr_$BOARD -linkerOpts -L$GCC_ARM/arm-none-eabi/lib/thumb -linkerOpts -lsupc++ -opt || exit 1 - export ZEPHYR_GCC_VARIANT=gccarmemb export GCCARMEMB_TOOLCHAIN_PATH=$GCC_ARM +# This is a kind of forward declaration for `program.o`. See below. +touch program.o + [ -f CMakeCache.txt ] || cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DBOARD=$BOARD .. || exit 1 + +make zephyr + +. $DIR/c_interop/platforms/$BOARD.sh + +# We need generated headers to be consumed by `cinterop`, +# so we preconfigure the project with `make zephyr`. +# But `program.o` should exist at that stage to be detected by the configuration tools. +# So we create an empty `program.o` above, and then override it with the compiler result below. +# TODO: do something with this mess. + +rm -f program.o + +konanc $DIR/src/main.kt \ + -target zephyr_$BOARD \ + -r $DIR/c_interop/platforms/build \ + -l $BOARD \ + -opt -g -o program || exit 1 + make || exit 1 # make flash diff --git a/samples/zephyr/c_interop/platforms/stm32f4_disco.def b/samples/zephyr/c_interop/platforms/stm32f4_disco.def new file mode 100644 index 00000000000..1eb0decf5d6 --- /dev/null +++ b/samples/zephyr/c_interop/platforms/stm32f4_disco.def @@ -0,0 +1,30 @@ +# Copyright 2010-2018 JetBrains s.r.o. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# Keep this one board independent. + +headers = autoconf.h zephyr.h board.h device.h gpio.h +compilerOpts = -DKERNEL -DUSE_FULL_LL_DRIVER -DUSE_HAL_DRIVER -D_FORTIFY_SOURCE=2 -D__ZEPHYR__=1 -ffreestanding -std=c99 -nostdinc + +--- + +// cinterop doesn't support `#define FOO "string"` constructs as of yet. +// so we provide this accessor for now. Because the port names +// are resolved to string literals. + +extern const char* led0_gpio_port(void) { + return LED0_GPIO_PORT; +} + diff --git a/samples/zephyr/c_interop/platforms/stm32f4_disco.sh b/samples/zephyr/c_interop/platforms/stm32f4_disco.sh new file mode 100644 index 00000000000..e3a93190dbf --- /dev/null +++ b/samples/zephyr/c_interop/platforms/stm32f4_disco.sh @@ -0,0 +1,46 @@ +# Copyright 2010-2017 JetBrains s.r.o. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# This is board specific. + +if [ x"$BOARD" == x ]; then + echo "This script is to be included by build.sh" + exit 0 +fi + +# TODO: we need a robust automated way to ask Zephyr for +# all these $ZEPHYR_BASE based paths and the proper defines. +# TODO: The D flag in `-copt -Dxxx` sequence is interpreted to be a jvm property, +# so we workaround the using -Xclang. + +cinterop -def $DIR/c_interop/platforms/$BOARD.def \ + -pkg platform.zephyr.$BOARD \ + -copt '-Xclang -DSTM32F407xx' \ + -copt -I$ZEPHYR_BASE/kernel/include \ + -copt -I$ZEPHYR_BASE/arch/arm/include \ + -copt -I$ZEPHYR_BASE/arch/arm/soc/st_stm32/stm32f4 \ + -copt -I$ZEPHYR_BASE/arch/arm/soc/st_stm32/stm32f4/include \ + -copt -I$ZEPHYR_BASE/arch/arm/soc/st_stm32/include \ + -copt -I$ZEPHYR_BASE/boards/arm/$BOARD \ + -copt -I$ZEPHYR_BASE/include \ + -copt -I$ZEPHYR_BASE/include/drivers \ + -copt -I$ZEPHYR_BASE/ext/hal/cmsis/Include \ + -copt -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/soc \ + -copt -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/drivers/include \ + -copt -I$ZEPHYR_BASE/ext/hal/st/stm32cube/stm32f4xx/drivers/include/Legacy \ + -copt -I$ZEPHYR_BASE/drivers \ + -copt -I$DIR/build/zephyr/include/generated \ + -copt -I$DIR/build/zephyr/include/generated/syscalls \ + -o $DIR/c_interop/platforms/build/$BOARD \ + -target zephyr_$BOARD || exit 1 diff --git a/samples/zephyr/src/blinky.c b/samples/zephyr/src/blinky.c deleted file mode 100644 index 13833717eb3..00000000000 --- a/samples/zephyr/src/blinky.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2016 Intel Corporation - * - * SPDX-License-Identifier: Apache-2.0 - */ - -#include -#include -#include -#include - -/* Change this if you have an LED connected to a custom port */ -#define PORT LED0_GPIO_PORT - -/* Change this if you have an LED connected to a custom pin */ -#define LED LED0_GPIO_PIN - -/* 1000 msec = 1 sec */ -#define SLEEP_TIME 1000 - -void blinky(int value) -{ - int cnt = 0; - struct device *dev; - - dev = device_get_binding(PORT); - /* Set LED pin as output */ - gpio_pin_configure(dev, LED, GPIO_DIR_OUT); - - while (1) { - /* Set pin to HIGH/LOW every 1 second */ - gpio_pin_write(dev, LED, cnt % 2); - cnt++; - k_sleep(SLEEP_TIME * value); - } -} diff --git a/samples/zephyr/src/empty.c b/samples/zephyr/src/empty.c new file mode 100644 index 00000000000..7912ed644f7 --- /dev/null +++ b/samples/zephyr/src/empty.c @@ -0,0 +1,2 @@ +// This is a placeholder empty C file. +// It is needed for Zephyr to detect the project as C project. diff --git a/samples/zephyr/src/main.c b/samples/zephyr/src/main.c deleted file mode 100644 index 04d3cba4833..00000000000 --- a/samples/zephyr/src/main.c +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -extern int Konan_main(int argc, const char** argv); - diff --git a/samples/zephyr/src/main.kt b/samples/zephyr/src/main.kt index 311e56dca2b..f335cdf5ac8 100644 --- a/samples/zephyr/src/main.kt +++ b/samples/zephyr/src/main.kt @@ -14,11 +14,27 @@ * limitations under the License. */ -@SymbolName("blinky") -external fun blinky(value: Int) +import platform.zephyr.stm32f4_disco.* +import kotlinx.cinterop.* -val x = 1 +fun blinky(value: Int) { + + // See the .def file for details of led0_gpio_port() + val port = led0_gpio_port()!!.toKString() + val led = LED0_GPIO_PIN + var toggler = false + val dev = device_get_binding(port) + + gpio_pin_configure(dev, led, GPIO_DIR_OUT) + + while (true) { + /* Set pin to HIGH/LOW every 1 second */ + gpio_pin_write(dev, led, if (toggler) 1 else 0); + toggler = !toggler + k_sleep(1000 * value); + } +} fun main(args: Array) { - blinky(x) + blinky(1) } diff --git a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt index 63ed57864c1..7ed0b7fcc63 100644 --- a/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt +++ b/shared/src/main/kotlin/org/jetbrains/kotlin/konan/target/ClangArgs.kt @@ -90,10 +90,13 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con "-fno-pie", "-fno-pic", "-nostdinc", + // TODO: make it a libGcc property? + // We need to get rid of wasm sysroot first. + "-isystem $targetToolchain/../lib/gcc/arm-none-eabi/7.2.1/include", + "-isystem $targetToolchain/../lib/gcc/arm-none-eabi/7.2.1/include-fixed", "-isystem$absoluteTargetSysRoot/include/libcxx", - "-isystem$absoluteTargetSysRoot/lib/libcxxabi/include", - "-isystem$absoluteTargetSysRoot/include/compat", - "-isystem$absoluteTargetSysRoot/include/libc") + + "-isystem$absoluteTargetSysRoot/include/libc" + ) + (configurables as ZephyrConfigurables).boardSpecificClangFlags } @@ -140,7 +143,7 @@ class ClangArgs(private val configurables: Configurables) : Configurables by con is KonanTarget.ZEPHYR -> listOf( "-DKONAN_ZEPHYR=1", "-DKONAN_NO_FFI=1", "-DKONAN_NO_THREADS=1", "-DKONAN_NO_EXCEPTIONS=1", "-DKONAN_NO_MATH=1", "-DKONAN_INTERNAL_SNPRINTF=1", "-DKONAN_INTERNAL_NOW=1", - "-DKONAN_NO_MEMMEM=1", "-DKONAN_NO_CTORS_SECTION") + "-DKONAN_NO_MEMMEM=1", "-DKONAN_NO_CTORS_SECTION=1") } private val host = HostManager.host