Page 1 of 4

Clang/LLVM

Posted: Sun May 18, 2014 12:06 pm
by davidji
It seems to be the case that clang on any platform can generate code for any target platform supported by LLVM. That means that of you install clang from your favourite Linux distribution, you will have a cross compiler for cortex-m3, for example.

It seems to me that using that is vastly simpler and more convenient than building a gcc cross compiler.

- Has anyone tried doing this?
- What problems do you foresee?
- Are there any plans to support CLang in the future?

Cheers
David

Re: Clang/LLVM

Posted: Sun May 18, 2014 1:28 pm
by Giovanni
It would be interesting, especially in order to compare the result.

The problem I can see is that there is no real support except for Linux, I cannot find a maintained toolchain based on LLVM while there are several based on GNU.

Giovanni

Re: Clang/LLVM

Posted: Mon Oct 06, 2014 9:43 am
by JetForMe
http://ellcc.org just made OS X binaries available (they had Linux ones already). The project contains everything you need (clang/LLVM in the form of "ecc"), binutils, and a C library.

I tried building my SAM7X (arm7tdmi) project with it. I had to change a couple of assembler files to use the ARM standard syntax, and I had to add an include path to ellcc/libecc/include/arm, but it mostly compiles.

Unfortunately, clang does not support anything but asm statements inside naked functions, and so the PORT_IRQ_HANDLER() fails to compile. I think this is a limitation of clang, but it's a deliberate choice made by the the developers (http://comments.gmane.org/gmane.comp.co ... scm/107562), so it's unlikely to change any time soon. Changing "naked" to "interrupt" seems to allow it to compile, but I don't know what impact this might have on performance or what other problems it will cause.

There are some warnings about r13 being uninitialized when used:

Code: Select all

ecc -c -target arm-none-eabi -O2 -ggdb -fomit-frame-pointer -mabi=apcs-gnu --sysroot=/Users/rmann/Desktop/ellcc -ffunction-sections -fdata-sections -std=gnu99 -Wall -Wextra -Wstrict-prototypes -DSTDOUT_SD=SD1 -DSTDIN_SD=SD1 -DCHPRINTF_USE_FLOAT=1   -MD -MP -MF .dep/chschd.o.d  -I. -I../../os/ports/GCC/ARM -I../../os/ports/GCC/ARM/AT91SAM7 -I../../os/kernel/include -I../../test -I../../os/hal/include -I../../os/hal/platforms/AT91SAM7 -I../../boards/LZBalloonController -I../../ext/fatfs/src -I../../os/various -I/Users/rmann/Desktop/ellcc/libecc/include/arm -Ilib ../../os/kernel/src/chschd.c -o build/obj/chschd.o
../../os/kernel/src/chschd.c:128:3: warning: variable 'r13' is uninitialized when used here [-Wuninitialized]
  chSysSwitch(currp, otp);
  ^~~~~~~~~~~~~~~~~~~~~~~
../../os/kernel/include/chsys.h:91:3: note: expanded from macro 'chSysSwitch'
  port_switch(ntp, otp);                                                    \
  ^~~~~~~~~~~~~~~~~~~~~
../../os/ports/GCC/ARM/chcore.h:460:22: note: expanded from macro 'port_switch'
  if ((stkalign_t *)(r13 - 1) < otp->p_stklimit)                            \
                     ^~~
../../os/kernel/src/chschd.c:128:3: note: variable 'r13' is declared here
../../os/kernel/include/chsys.h:91:3: note: expanded from macro 'chSysSwitch'
  port_switch(ntp, otp);                                                    \
  ^
../../os/ports/GCC/ARM/chcore.h:459:3: note: expanded from macro 'port_switch'
  register struct intctx *r13 asm ("r13");                                  \
  ^


I also ran into a problem wherein syscalls.c include <sys/reent.h>, which does not appear to exist in ELLCC's standard library. I'm currently investigating this issue.

Re: Clang/LLVM

Posted: Sun Jan 07, 2018 8:31 pm
by faisal
Any updates on this topic? Would be really nice to have Clang/LLVM support. I'm interested in using the clang static analyzer, I've read good things about it.

Re: Clang/LLVM

Posted: Sun Jan 07, 2018 9:11 pm
by Giovanni
Hi,

No progress on this, anyway, PORT_IRQ_HANDLER is no more a naked function so it should work now.

About the static analyzer, I think it can be used regardless if the compiled code works or not. Is there a guide to get started?

Giovanni

Re: Clang/LLVM

Posted: Tue Jan 09, 2018 1:20 am
by faisal
Giovanni wrote:Hi,

No progress on this, anyway, PORT_IRQ_HANDLER is no more a naked function so it should work now.

About the static analyzer, I think it can be used regardless if the compiled code works or not. Is there a guide to get started?

Giovanni


I've been using this today: http://clang.llvm.org/get_started.html , but ran out of disk space when attempting to link :D . Will expand the drive of my VM, and try again some time ...

Re: Clang/LLVM

Posted: Tue Jan 09, 2018 5:01 pm
by faisal
Here's how to get the latest clang version without compiling from sources. I'm having some trouble expanding the drive on my VM, so I went ahead with this. I've tried it on some PC applications so far and it works. I haven't tried targeting embedded yet ..

https://blog.kowalczyk.info/article/k/h ... l-wsl.html

Re: Clang/LLVM

Posted: Tue Jan 09, 2018 5:03 pm
by Giovanni
Thanks, I could give it a try over weekend.

Giovanni

Re: Clang/LLVM

Posted: Sun May 27, 2018 5:02 am
by faisal
Have anyone been able to give Clang/LLVM a try yet? I'm looking forward to being able to use the various clang tools out there (static analyzer, tidy, formatter, etc ..).

Re: Clang/LLVM

Posted: Sun May 27, 2018 12:32 pm
by apmorton
clang will build chibios and pretty much most any general code that will build in modern GCC compilers.

For the record, at least for ARM, clang is not generating better code than gcc-arm-none-eabi. In most cases it is reported to be on par, and in others it is worse.

That being said, I think the main benefit to clang is, as faisal mentioned, the static analyzer, tidy, formatter, etc.
Those tools can be used already with varying degrees of fiddling.

To use clang-format on its own (without automatic support from say an IDE plugin) you can effectively run the tool manually from a shell script - the command line flags are fairly intuitive.

To use clang-tidy is a bit more involved, since you need to actually get your code to compile with clang, but it can be done.

I continue to compile my code with gcc in the end, but leverage tools from clang where possible.