JuliaHubOSS / llvm-cbe

resurrected LLVM "C Backend", with improvements
Other
826 stars 141 forks source link

Version of C? #145

Closed trapexit closed 3 years ago

trapexit commented 3 years ago

What version of C does llvm-cbe target? Is it selectable?

vtjnash commented 3 years ago

Generally C89 (so that any old compiler would work), though if you use atomics or threads, those were not defined until C11, so that instead is required.

trapexit commented 3 years ago

I tried a minimal program and it includes stdint.h which was introduced in C99 as I recall.

int main() { return 0; }

resulted in

/* Provide Declarations */
#include <stdint.h>
#ifndef __cplusplus
typedef unsigned char bool;
#endif

#if defined(__GNUC__)
#define  __ATTRIBUTELIST__(x) __attribute__(x)
#else
#define  __ATTRIBUTELIST__(x)
#endif

#ifdef _MSC_VER  /* Can only support "linkonce" vars with GCC */
#define __attribute__(X)
#endif

/* Global Declarations */

/* Types Declarations */

/* Function definitions */

/* Types Definitions */

/* Function Declarations */
int main(void) __ATTRIBUTELIST__((noinline, nothrow));

/* LLVM Intrinsic Builtin Function Bodies */

/* Function Bodies */

int main(void) {
  uint32_t _1;    /* Address-exposed local */

  _1 = 0;
  return 0;
}
vtjnash commented 3 years ago

That is true. It is targeting a C89 parser, that has C99 stdint.h headers, and C11 atomic functions. It is not quite a clear target, but then C has been missing many essential features until quite recently.

hikari-no-yume commented 3 years ago

I added a command-line option to switch between C89 and C99 variable declarations a little while ago. That's so far the only configuration option for the language compatibility.

This project will probably always use stdint because LLVM IR uses explicit integer sizes, and stdint is the best way to get those in C. If you need to target a C89 compiler, then it shouldn't be too difficult to write your own header providing the stdint types, assuming the target actually has types of the right sizes.

vtjnash commented 3 years ago

It is even probably possible to teach CBE to handle targets without the required type sizes, since we know how to round non-power-of-two operations up to the next largest supported size.

hikari-no-yume commented 3 years ago

That is true, though I think the LLVM IR being consumed is going to assume things about pointer arithmetic that might limit its practical utility.