diff --git a/resources/lib/std/cinterop.c3 b/resources/lib/std/cinterop.c3 index 4259d8603..9df255e46 100644 --- a/resources/lib/std/cinterop.c3 +++ b/resources/lib/std/cinterop.c3 @@ -71,7 +71,8 @@ $endif; define CSChar = ichar; define CUChar = char; -/* -define CChar = ${C_CHAR_TYPE}; -define CSChar = ${C_SCHAR_TYPE}; -*/ +$if (${C_CHAR_IS_SIGNED}): + define CChar = ichar; +$else: + define CChar = char; +$endif; diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 30a9be8d9..8f70ff7a6 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -279,6 +279,7 @@ void compiler_compile(void) setup_int_define("C_INT_SIZE", platform_target.width_c_int); setup_int_define("C_LONG_SIZE", platform_target.width_c_long); setup_int_define("C_LONG_LONG_SIZE", platform_target.width_c_long_long); + setup_bool_define("C_CHAR_IS_SIGNED", platform_target.signed_c_char); setup_bool_define("PLATFORM_LITTLE_ENDIAN", platform_target.little_endian); setup_bool_define("PLATFORM_I128_SUPPORTED", platform_target.int128); setup_int_define("COMPILER_OPT_LEVEL", (int)active_target.optimization_level); diff --git a/src/compiler/target.c b/src/compiler/target.c index 955a96c9e..8b54e1617 100644 --- a/src/compiler/target.c +++ b/src/compiler/target.c @@ -20,6 +20,7 @@ static unsigned os_target_supports_float128(OsType os, ArchType arch); static unsigned os_target_supports_vec(OsType os, ArchType arch, int bits, bool is_int); static bool os_requires_libc(OsType os); + PlatformTarget platform_target = {}; int target_alloca_addr_space() @@ -207,6 +208,34 @@ static inline bool os_is_apple(OsType os_type) os_type == OS_TYPE_MACOSX || os_type == OS_TYPE_IOS; } +static bool os_target_signed_c_char_type(OsType os, ArchType arch) +{ + switch (arch) + { + case ARCH_TYPE_AARCH64: + case ARCH_TYPE_AARCH64_32: + case ARCH_TYPE_AARCH64_BE: + case ARCH_TYPE_ARM: + case ARCH_TYPE_ARMB: + case ARCH_TYPE_THUMB: + case ARCH_TYPE_THUMBEB: + if (os_is_apple(os) || os == OS_TYPE_WIN32) return true; + return false; + case ARCH_TYPE_PPC: + case ARCH_TYPE_PPC64: + return os_is_apple(os); + case ARCH_TYPE_HEXAGON: + case ARCH_TYPE_PPC64LE: + case ARCH_TYPE_RISCV32: + case ARCH_TYPE_RISCV64: + case ARCH_TYPE_SYSTEMZ: + case ARCH_TYPE_XCORE: + return false; + default: + return true; + } +} + static inline void target_setup_arm_abi(void) { platform_target.abi = ABI_ARM; @@ -1197,7 +1226,7 @@ void target_setup(BuildTarget *target) platform_target.width_c_int = os_target_c_type_bits(platform_target.os, platform_target.arch, CTYPE_INT); platform_target.width_c_long = os_target_c_type_bits(platform_target.os, platform_target.arch, CTYPE_LONG); platform_target.width_c_long_long = os_target_c_type_bits(platform_target.os, platform_target.arch, CTYPE_LONG_LONG); - + platform_target.signed_c_char = os_target_signed_c_char_type(platform_target.os, platform_target.arch); /** * x86-64: CMOV, CMPXCHG8B, FPU, FXSR, MMX, FXSR, SCE, SSE, SSE2 @@ -1307,3 +1336,4 @@ void target_setup(BuildTarget *target) // TODO remove type_setup(&platform_target); } + diff --git a/src/compiler/target.h b/src/compiler/target.h index dae60a448..5646729e1 100644 --- a/src/compiler/target.h +++ b/src/compiler/target.h @@ -249,6 +249,7 @@ typedef struct PicGeneration pic : 3; PieGeneration pie : 3; bool pic_required : 1; + bool signed_c_char : 1; FloatABI float_abi : 3; unsigned default_number_regs_x86 : 8; union