mirror of
https://github.com/ziglang/zig.git
synced 2026-02-17 23:10:09 +00:00
Merge branch 'msvc'
Now the supported compilers of Zig are: * GCC * Clang * MSVC * MinGW
This commit is contained in:
commit
5931a6b1a5
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,9 +1,3 @@
|
||||
zig-cache/
|
||||
build/
|
||||
build-release/
|
||||
build-windows/
|
||||
build-llvm4-debug/
|
||||
build-llvm5-debug/
|
||||
/.cproject
|
||||
/.project
|
||||
/.settings/
|
||||
build-*/
|
||||
|
||||
@ -26,10 +26,8 @@ option(ZIG_TEST_COVERAGE "Build Zig with test coverage instrumentation" OFF)
|
||||
# git log -p -- deps/lld
|
||||
option(ZIG_FORCE_EXTERNAL_LLD "If your system has the LLD patches use it instead of the embedded LLD" OFF)
|
||||
|
||||
|
||||
find_package(llvm)
|
||||
include_directories(${LLVM_INCLUDE_DIR})
|
||||
link_directories(${LLVM_LIBDIRS})
|
||||
include_directories(${LLVM_INCLUDE_DIRS})
|
||||
|
||||
find_package(clang)
|
||||
include_directories(${CLANG_INCLUDE_DIRS})
|
||||
@ -130,16 +128,21 @@ else()
|
||||
add_library(embedded_lld_lib ${EMBEDDED_LLD_LIB_SOURCES})
|
||||
add_library(embedded_lld_elf ${EMBEDDED_LLD_ELF_SOURCES})
|
||||
add_library(embedded_lld_coff ${EMBEDDED_LLD_COFF_SOURCES})
|
||||
if(MSVC)
|
||||
set(ZIG_LLD_COMPILE_FLAGS "-std=c++11")
|
||||
else()
|
||||
set(ZIG_LLD_COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment")
|
||||
endif()
|
||||
set_target_properties(embedded_lld_lib PROPERTIES
|
||||
COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment"
|
||||
COMPILE_FLAGS ${ZIG_LLD_COMPILE_FLAGS}
|
||||
LINK_FLAGS " "
|
||||
)
|
||||
set_target_properties(embedded_lld_elf PROPERTIES
|
||||
COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment"
|
||||
COMPILE_FLAGS ${ZIG_LLD_COMPILE_FLAGS}
|
||||
LINK_FLAGS " "
|
||||
)
|
||||
set_target_properties(embedded_lld_coff PROPERTIES
|
||||
COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment"
|
||||
COMPILE_FLAGS ${ZIG_LLD_COMPILE_FLAGS}
|
||||
LINK_FLAGS " "
|
||||
)
|
||||
target_include_directories(embedded_lld_lib PUBLIC
|
||||
@ -211,14 +214,20 @@ include_directories(
|
||||
"${CMAKE_SOURCE_DIR}/src"
|
||||
)
|
||||
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror -Wall")
|
||||
|
||||
|
||||
if(MINGW)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-error=format= -Wno-error=format -Wno-error=format-extra-args")
|
||||
if(MSVC)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -W4")
|
||||
elseif(MINGW)
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Werror -Wno-error=format= -Wno-error=format -Wno-error=format-extra-args")
|
||||
else()
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror -Wall")
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
set(EXE_CFLAGS "-std=c++11")
|
||||
else()
|
||||
set(EXE_CFLAGS "-std=c++11 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE -fno-exceptions -fno-rtti -Werror=strict-prototypes -Werror=old-style-definition -Werror=type-limits -Wno-missing-braces")
|
||||
endif()
|
||||
|
||||
set(EXE_CFLAGS "-std=c++11 -fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -Werror=strict-prototypes -Werror=old-style-definition -Werror=type-limits -Wno-missing-braces")
|
||||
set(EXE_LDFLAGS " ")
|
||||
if(ZIG_TEST_COVERAGE)
|
||||
set(EXE_CFLAGS "${EXE_CFLAGS} -fprofile-arcs -ftest-coverage")
|
||||
@ -230,15 +239,20 @@ set_target_properties(zig PROPERTIES
|
||||
COMPILE_FLAGS ${EXE_CFLAGS}
|
||||
LINK_FLAGS ${EXE_LDFLAGS}
|
||||
)
|
||||
|
||||
target_link_libraries(zig LINK_PUBLIC
|
||||
${CLANG_LIBRARIES}
|
||||
${LLD_LIBRARIES}
|
||||
${LLVM_LIBRARIES}
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
quadmath
|
||||
${PLATFORM_LIBRARIES}
|
||||
)
|
||||
if(MINGW)
|
||||
if(MSVC)
|
||||
target_link_libraries(zig LINK_PUBLIC version)
|
||||
elseif(MINGW)
|
||||
target_link_libraries(zig LINK_PUBLIC version quadmath)
|
||||
else()
|
||||
target_link_libraries(zig LINK_PUBLIC quadmath)
|
||||
endif()
|
||||
install(TARGETS zig DESTINATION bin)
|
||||
|
||||
@ -455,3 +469,4 @@ if (ZIG_TEST_COVERAGE)
|
||||
COMMAND rm coverage.info coverage.info.cleaned
|
||||
)
|
||||
endif()
|
||||
|
||||
|
||||
@ -6,37 +6,56 @@
|
||||
# CLANG_INCLUDE_DIRS
|
||||
# CLANG_LIBRARIES
|
||||
|
||||
find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
|
||||
PATHS
|
||||
/usr/lib/llvm/5/include
|
||||
/usr/lib/llvm-5.0/include
|
||||
/mingw64/include)
|
||||
if(MSVC)
|
||||
find_package(CLANG REQUIRED CONFIG)
|
||||
|
||||
macro(FIND_AND_ADD_CLANG_LIB _libname_)
|
||||
string(TOUPPER ${_libname_} _prettylibname_)
|
||||
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
|
||||
PATHS
|
||||
/usr/lib/llvm/5/lib
|
||||
/usr/lib/llvm-5.0/lib
|
||||
/mingw64/lib
|
||||
/c/msys64/mingw64/lib
|
||||
c:\\msys64\\mingw64\\lib)
|
||||
if(CLANG_${_prettylibname_}_LIB)
|
||||
set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})
|
||||
endif()
|
||||
endmacro(FIND_AND_ADD_CLANG_LIB)
|
||||
set(CLANG_LIBRARIES
|
||||
clangFrontend
|
||||
clangDriver
|
||||
clangSerialization
|
||||
clangSema
|
||||
clangAnalysis
|
||||
clangAST
|
||||
clangParse
|
||||
clangSema
|
||||
clangBasic
|
||||
clangEdit
|
||||
clangLex
|
||||
)
|
||||
|
||||
FIND_AND_ADD_CLANG_LIB(clangFrontend)
|
||||
FIND_AND_ADD_CLANG_LIB(clangDriver)
|
||||
FIND_AND_ADD_CLANG_LIB(clangSerialization)
|
||||
FIND_AND_ADD_CLANG_LIB(clangSema)
|
||||
FIND_AND_ADD_CLANG_LIB(clangAnalysis)
|
||||
FIND_AND_ADD_CLANG_LIB(clangAST)
|
||||
FIND_AND_ADD_CLANG_LIB(clangParse)
|
||||
FIND_AND_ADD_CLANG_LIB(clangSema)
|
||||
FIND_AND_ADD_CLANG_LIB(clangBasic)
|
||||
FIND_AND_ADD_CLANG_LIB(clangEdit)
|
||||
FIND_AND_ADD_CLANG_LIB(clangLex)
|
||||
else()
|
||||
find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
|
||||
PATHS
|
||||
/usr/lib/llvm/5/include
|
||||
/usr/lib/llvm-5.0/include
|
||||
/mingw64/include)
|
||||
|
||||
macro(FIND_AND_ADD_CLANG_LIB _libname_)
|
||||
string(TOUPPER ${_libname_} _prettylibname_)
|
||||
find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
|
||||
PATHS
|
||||
/usr/lib/llvm/5/lib
|
||||
/usr/lib/llvm-5.0/lib
|
||||
/mingw64/lib
|
||||
/c/msys64/mingw64/lib
|
||||
c:\\msys64\\mingw64\\lib)
|
||||
if(CLANG_${_prettylibname_}_LIB)
|
||||
set(CLANG_LIBRARIES ${CLANG_LIBRARIES} ${CLANG_${_prettylibname_}_LIB})
|
||||
endif()
|
||||
endmacro(FIND_AND_ADD_CLANG_LIB)
|
||||
|
||||
FIND_AND_ADD_CLANG_LIB(clangFrontend)
|
||||
FIND_AND_ADD_CLANG_LIB(clangDriver)
|
||||
FIND_AND_ADD_CLANG_LIB(clangSerialization)
|
||||
FIND_AND_ADD_CLANG_LIB(clangSema)
|
||||
FIND_AND_ADD_CLANG_LIB(clangAnalysis)
|
||||
FIND_AND_ADD_CLANG_LIB(clangAST)
|
||||
FIND_AND_ADD_CLANG_LIB(clangParse)
|
||||
FIND_AND_ADD_CLANG_LIB(clangSema)
|
||||
FIND_AND_ADD_CLANG_LIB(clangBasic)
|
||||
FIND_AND_ADD_CLANG_LIB(clangEdit)
|
||||
FIND_AND_ADD_CLANG_LIB(clangLex)
|
||||
endif()
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(CLANG DEFAULT_MSG CLANG_LIBRARIES CLANG_INCLUDE_DIRS)
|
||||
|
||||
@ -3,48 +3,114 @@
|
||||
# See http://opensource.org/licenses/MIT
|
||||
|
||||
# LLVM_FOUND
|
||||
# LLVM_INCLUDE_DIR
|
||||
# LLVM_INCLUDE_DIRS
|
||||
# LLVM_LIBRARIES
|
||||
# LLVM_LIBDIRS
|
||||
|
||||
find_program(LLVM_CONFIG_EXE
|
||||
NAMES llvm-config-5.0 llvm-config
|
||||
PATHS
|
||||
"/mingw64/bin"
|
||||
"/c/msys64/mingw64/bin"
|
||||
"c:/msys64/mingw64/bin"
|
||||
"C:/Libraries/llvm-5.0.0/bin")
|
||||
if(MSVC)
|
||||
find_package(LLVM REQUIRED CONFIG)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --libs
|
||||
OUTPUT_VARIABLE LLVM_LIBRARIES
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
# TODO: this currently doesn't work, it currently defines UNICODE but zig
|
||||
# uses MBCS
|
||||
#add_definitions(${LLVM_DEFINITIONS})
|
||||
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --system-libs
|
||||
OUTPUT_VARIABLE LLVM_SYSTEM_LIBS
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
link_directories(${LLVM_LIBRARY_DIRS})
|
||||
llvm_map_components_to_libnames(LLVM_LIBRARIES
|
||||
LTO
|
||||
Symbolize
|
||||
XCoreDisassembler
|
||||
XCoreCodeGen
|
||||
XCoreAsmPrinter
|
||||
SystemZDisassembler
|
||||
SystemZCodeGen
|
||||
SystemZAsmParser
|
||||
SystemZAsmPrinter
|
||||
SparcDisassembler
|
||||
SparcCodeGen
|
||||
SparcAsmParser
|
||||
SparcAsmPrinter
|
||||
PowerPCDisassembler
|
||||
PowerPCCodeGen
|
||||
PowerPCAsmParser
|
||||
PowerPCAsmPrinter
|
||||
NVPTXCodeGen
|
||||
NVPTXAsmPrinter
|
||||
MSP430CodeGen
|
||||
MSP430AsmPrinter
|
||||
MipsDisassembler
|
||||
MipsCodeGen
|
||||
MipsAsmParser
|
||||
MipsAsmPrinter
|
||||
LanaiDisassembler
|
||||
LanaiCodeGen
|
||||
LanaiAsmParser
|
||||
LanaiAsmPrinter
|
||||
HexagonDisassembler
|
||||
HexagonCodeGen
|
||||
HexagonAsmParser
|
||||
BPFDisassembler
|
||||
BPFCodeGen
|
||||
BPFAsmPrinter
|
||||
ARMDisassembler
|
||||
ARMCodeGen
|
||||
ARMAsmParser
|
||||
ARMAsmPrinter
|
||||
AMDGPUDisassembler
|
||||
AMDGPUCodeGen
|
||||
AMDGPUAsmParser
|
||||
AMDGPUAsmPrinter
|
||||
AArch64Disassembler
|
||||
AArch64CodeGen
|
||||
AArch64AsmParser
|
||||
AArch64AsmPrinter
|
||||
LibDriver
|
||||
X86Disassembler
|
||||
X86AsmParser
|
||||
X86CodeGen
|
||||
X86AsmPrinter
|
||||
Core
|
||||
)
|
||||
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --libdir
|
||||
OUTPUT_VARIABLE LLVM_LIBDIRS
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
else()
|
||||
find_program(LLVM_CONFIG_EXE
|
||||
NAMES llvm-config-5.0 llvm-config
|
||||
PATHS
|
||||
"/mingw64/bin"
|
||||
"/c/msys64/mingw64/bin"
|
||||
"c:/msys64/mingw64/bin"
|
||||
"C:/Libraries/llvm-5.0.0/bin")
|
||||
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --includedir
|
||||
OUTPUT_VARIABLE LLVM_INCLUDE_DIR
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --libs
|
||||
OUTPUT_VARIABLE LLVM_LIBRARIES
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
find_library(LLVM_LIBRARY NAMES LLVM)
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --system-libs
|
||||
OUTPUT_VARIABLE LLVM_SYSTEM_LIBS
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --libdir
|
||||
OUTPUT_VARIABLE LLVM_LIBDIRS
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
if(LLVM_LIBRARY)
|
||||
set(LLVM_LIBRARIES ${LLVM_LIBRARY})
|
||||
execute_process(
|
||||
COMMAND ${LLVM_CONFIG_EXE} --includedir
|
||||
OUTPUT_VARIABLE LLVM_INCLUDE_DIRS
|
||||
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||
|
||||
find_library(LLVM_LIBRARY NAMES LLVM)
|
||||
|
||||
set(LLVM_LIBRARIES ${LLVM_LIBRARIES} ${LLVM_SYSTEM_LIBS})
|
||||
|
||||
if(LLVM_LIBRARY)
|
||||
set(LLVM_LIBRARIES ${LLVM_LIBRARY})
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
include(FindPackageHandleStandardArgs)
|
||||
find_package_handle_standard_args(LLVM DEFAULT_MSG LLVM_LIBRARIES LLVM_INCLUDE_DIR)
|
||||
find_package_handle_standard_args(LLVM DEFAULT_MSG LLVM_LIBRARIES LLVM_INCLUDE_DIRS)
|
||||
|
||||
mark_as_advanced(LLVM_INCLUDE_DIR LLVM_LIBRARIES LLVM_LIBDIRS)
|
||||
mark_as_advanced(LLVM_INCLUDE_DIRS LLVM_LIBRARIES LLVM_LIBDIRS)
|
||||
|
||||
@ -163,7 +163,7 @@ static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *so
|
||||
}
|
||||
|
||||
static uint8_t log2_u64(uint64_t x) {
|
||||
return (63 - __builtin_clzll(x));
|
||||
return (63 - clzll(x));
|
||||
}
|
||||
|
||||
static uint8_t bits_needed_for_unsigned(uint64_t x) {
|
||||
|
||||
@ -13,6 +13,11 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
// TODO support 128 bit floats with msvc
|
||||
typedef long double __float128;
|
||||
#endif
|
||||
|
||||
struct BigFloat {
|
||||
__float128 value;
|
||||
};
|
||||
|
||||
@ -141,7 +141,7 @@ void bigint_init_unsigned(BigInt *dest, uint64_t x) {
|
||||
dest->is_negative = false;
|
||||
}
|
||||
|
||||
void bigint_init_u128(BigInt *dest, unsigned __int128 x) {
|
||||
void bigint_init_u128(BigInt *dest, uint128_t x) {
|
||||
uint64_t low = (uint64_t)(x & UINT64_MAX);
|
||||
uint64_t high = (uint64_t)(x >> 64);
|
||||
|
||||
@ -201,9 +201,9 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {
|
||||
|
||||
void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
|
||||
if (op->value >= 0) {
|
||||
bigint_init_u128(dest, (unsigned __int128)(op->value));
|
||||
bigint_init_u128(dest, (uint128_t)(op->value));
|
||||
} else {
|
||||
bigint_init_u128(dest, (unsigned __int128)(-op->value));
|
||||
bigint_init_u128(dest, (uint128_t)(-op->value));
|
||||
dest->is_negative = true;
|
||||
}
|
||||
}
|
||||
@ -377,6 +377,32 @@ void bigint_read_twos_complement(BigInt *dest, const uint8_t *buf, size_t bit_co
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
static bool add_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
*result = op1 + op2;
|
||||
return *result < op1 || *result < op2;
|
||||
}
|
||||
|
||||
static bool sub_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
*result = op1 - op2;
|
||||
return *result > op1;
|
||||
}
|
||||
|
||||
bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
*result = op1 * op2;
|
||||
|
||||
if (op1 == 0 || op2 == 0)
|
||||
return false;
|
||||
|
||||
if (op1 > UINT64_MAX / op2)
|
||||
return true;
|
||||
|
||||
if (op2 > UINT64_MAX / op1)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
static bool add_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
return __builtin_uaddll_overflow((unsigned long long)op1, (unsigned long long)op2,
|
||||
(unsigned long long *)result);
|
||||
@ -387,10 +413,11 @@ static bool sub_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
(unsigned long long *)result);
|
||||
}
|
||||
|
||||
static bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
|
||||
return __builtin_umulll_overflow((unsigned long long)op1, (unsigned long long)op2,
|
||||
(unsigned long long *)result);
|
||||
}
|
||||
#endif
|
||||
|
||||
void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
|
||||
if (op1->digit_count == 0) {
|
||||
@ -404,7 +431,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
|
||||
|
||||
const uint64_t *op1_digits = bigint_ptr(op1);
|
||||
const uint64_t *op2_digits = bigint_ptr(op2);
|
||||
uint64_t overflow = add_u64_overflow(op1_digits[0], op2_digits[0], &dest->data.digit);
|
||||
bool overflow = add_u64_overflow(op1_digits[0], op2_digits[0], &dest->data.digit);
|
||||
if (overflow == 0 && op1->digit_count == 1 && op2->digit_count == 1) {
|
||||
dest->digit_count = 1;
|
||||
bigint_normalize(dest);
|
||||
@ -534,9 +561,9 @@ static void mul_overflow(uint64_t x, uint64_t y, uint64_t *result, uint64_t *car
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned __int128 big_x = x;
|
||||
unsigned __int128 big_y = y;
|
||||
unsigned __int128 big_result = big_x * big_y;
|
||||
uint128_t big_x = x;
|
||||
uint128_t big_y = y;
|
||||
uint128_t big_result = big_x * big_y;
|
||||
*carry = big_result >> 64;
|
||||
}
|
||||
|
||||
|
||||
@ -11,6 +11,15 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
// TEMPORARY WORKAROUND FOR MSVC NOT SUPPORTING __int128
|
||||
typedef long long int128_t;
|
||||
typedef unsigned long long uint128_t;
|
||||
#else
|
||||
typedef __int128 int128_t;
|
||||
typedef unsigned __int128 uint128_t;
|
||||
#endif
|
||||
|
||||
struct BigInt {
|
||||
size_t digit_count;
|
||||
union {
|
||||
@ -30,7 +39,7 @@ enum Cmp {
|
||||
};
|
||||
|
||||
void bigint_init_unsigned(BigInt *dest, uint64_t x);
|
||||
void bigint_init_u128(BigInt *dest, unsigned __int128 x);
|
||||
void bigint_init_u128(BigInt *dest, uint128_t x);
|
||||
void bigint_init_signed(BigInt *dest, int64_t x);
|
||||
void bigint_init_bigint(BigInt *dest, const BigInt *src);
|
||||
void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);
|
||||
@ -89,4 +98,6 @@ size_t bigint_bits_needed(const BigInt *op);
|
||||
// convenience functions
|
||||
Cmp bigint_cmp_zero(const BigInt *op);
|
||||
|
||||
bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result);
|
||||
|
||||
#endif
|
||||
|
||||
@ -24,7 +24,7 @@ struct Buf {
|
||||
};
|
||||
|
||||
Buf *buf_sprintf(const char *format, ...)
|
||||
__attribute__ ((format (printf, 1, 2)));
|
||||
ATTRIBUTE_PRINTF(1, 2);
|
||||
Buf *buf_vprintf(const char *format, va_list ap);
|
||||
|
||||
static inline size_t buf_len(Buf *buf) {
|
||||
@ -124,7 +124,7 @@ static inline void buf_append_char(Buf *buf, uint8_t c) {
|
||||
}
|
||||
|
||||
void buf_appendf(Buf *buf, const char *format, ...)
|
||||
__attribute__ ((format (printf, 2, 3)));
|
||||
ATTRIBUTE_PRINTF(2, 3);
|
||||
|
||||
static inline bool buf_eql_mem(Buf *buf, const char *mem, size_t mem_len) {
|
||||
assert(buf->list.length);
|
||||
|
||||
@ -5194,16 +5194,16 @@ void codegen_add_object(CodeGen *g, Buf *object_path) {
|
||||
g->link_objects.append(object_path);
|
||||
}
|
||||
|
||||
|
||||
// Must be coordinated with with CIntType enum
|
||||
static const char *c_int_type_names[] = {
|
||||
[CIntTypeShort] = "short",
|
||||
[CIntTypeUShort] = "unsigned short",
|
||||
[CIntTypeInt] = "int",
|
||||
[CIntTypeUInt] = "unsigned int",
|
||||
[CIntTypeLong] = "long",
|
||||
[CIntTypeULong] = "unsigned long",
|
||||
[CIntTypeLongLong] = "long long",
|
||||
[CIntTypeULongLong] = "unsigned long long",
|
||||
"short",
|
||||
"unsigned short",
|
||||
"int",
|
||||
"unsigned int",
|
||||
"long",
|
||||
"unsigned long",
|
||||
"long long",
|
||||
"unsigned long long",
|
||||
};
|
||||
|
||||
static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
|
||||
|
||||
@ -6500,9 +6500,9 @@ static void float_init_bigint(BigInt *bigint, ConstExprValue *const_val) {
|
||||
break;
|
||||
case 128:
|
||||
if (const_val->data.x_f128 >= 0) {
|
||||
bigint_init_u128(bigint, (unsigned __int128)(const_val->data.x_f128));
|
||||
bigint_init_u128(bigint, (uint128_t)(const_val->data.x_f128));
|
||||
} else {
|
||||
bigint_init_u128(bigint, (unsigned __int128)(-const_val->data.x_f128));
|
||||
bigint_init_u128(bigint, (uint128_t)(-const_val->data.x_f128));
|
||||
bigint->is_negative = true;
|
||||
}
|
||||
break;
|
||||
@ -9731,8 +9731,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
|
||||
uint64_t old_array_len = array_type->data.array.len;
|
||||
uint64_t new_array_len;
|
||||
|
||||
if (__builtin_umulll_overflow((unsigned long long)old_array_len, (unsigned long long)mult_amt,
|
||||
(unsigned long long*)&new_array_len))
|
||||
if (mul_u64_overflow(old_array_len, mult_amt, &new_array_len))
|
||||
{
|
||||
ir_add_error(ira, &instruction->base, buf_sprintf("operation results in overflow"));
|
||||
return ira->codegen->builtin_types.entry_invalid;
|
||||
|
||||
12
src/os.cpp
12
src/os.cpp
@ -25,6 +25,8 @@
|
||||
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
|
||||
typedef SSIZE_T ssize_t;
|
||||
#else
|
||||
#define ZIG_OS_POSIX
|
||||
|
||||
@ -620,7 +622,7 @@ int os_get_cwd(Buf *out_cwd) {
|
||||
|
||||
bool os_stderr_tty(void) {
|
||||
#if defined(ZIG_OS_WINDOWS)
|
||||
return _isatty(STDERR_FILENO) != 0;
|
||||
return _isatty(_fileno(stderr)) != 0;
|
||||
#elif defined(ZIG_OS_POSIX)
|
||||
return isatty(STDERR_FILENO) != 0;
|
||||
#else
|
||||
@ -777,12 +779,12 @@ int os_make_path(Buf *path) {
|
||||
|
||||
int os_make_dir(Buf *path) {
|
||||
#if defined(ZIG_OS_WINDOWS)
|
||||
if (mkdir(buf_ptr(path)) == -1) {
|
||||
if (errno == EEXIST)
|
||||
if (!CreateDirectory(buf_ptr(path), NULL)) {
|
||||
if (GetLastError() == ERROR_ALREADY_EXISTS)
|
||||
return ErrorPathAlreadyExists;
|
||||
if (errno == ENOENT)
|
||||
if (GetLastError() == ERROR_PATH_NOT_FOUND)
|
||||
return ErrorFileNotFound;
|
||||
if (errno == EACCES)
|
||||
if (GetLastError() == ERROR_ACCESS_DENIED)
|
||||
return ErrorAccess;
|
||||
return ErrorUnexpected;
|
||||
}
|
||||
|
||||
@ -56,7 +56,7 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl);
|
||||
static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl);
|
||||
|
||||
|
||||
__attribute__ ((format (printf, 3, 4)))
|
||||
ATTRIBUTE_PRINTF(3, 4)
|
||||
static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {
|
||||
if (!c->warnings_on) {
|
||||
return;
|
||||
|
||||
@ -24,8 +24,8 @@ struct ParseContext {
|
||||
Buf *void_buf;
|
||||
};
|
||||
|
||||
__attribute__ ((format (printf, 4, 5)))
|
||||
__attribute__ ((noreturn))
|
||||
ATTRIBUTE_PRINTF(4, 5)
|
||||
ATTRIBUTE_NORETURN
|
||||
static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const char *format, ...) {
|
||||
assert(node->type == NodeTypeAsmExpr);
|
||||
|
||||
@ -46,8 +46,8 @@ static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
__attribute__ ((format (printf, 3, 4)))
|
||||
__attribute__ ((noreturn))
|
||||
ATTRIBUTE_PRINTF(3, 4)
|
||||
ATTRIBUTE_NORETURN
|
||||
static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, format);
|
||||
@ -205,7 +205,7 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {
|
||||
}
|
||||
}
|
||||
|
||||
__attribute__ ((noreturn))
|
||||
ATTRIBUTE_NORETURN
|
||||
static void ast_invalid_token_error(ParseContext *pc, Token *token) {
|
||||
Buf token_value = BUF_INIT;
|
||||
ast_buf_from_token(pc, token, &token_value);
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
#include "tokenizer.hpp"
|
||||
#include "errmsg.hpp"
|
||||
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
ATTRIBUTE_PRINTF(2, 3)
|
||||
void ast_token_error(Token *token, const char *format, ...);
|
||||
|
||||
|
||||
|
||||
@ -8,6 +8,37 @@
|
||||
#ifndef ZIG_QUADMATH_HPP
|
||||
#define ZIG_QUADMATH_HPP
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <cmath>
|
||||
|
||||
static inline __float128 fmodq(__float128 a, __float128 b) {
|
||||
return fmodl(a, b);
|
||||
}
|
||||
|
||||
static inline __float128 ceilq(__float128 a) {
|
||||
return ceill(a);
|
||||
}
|
||||
|
||||
static inline __float128 floorq(__float128 a) {
|
||||
return floorl(a);
|
||||
}
|
||||
|
||||
static inline __float128 strtoflt128(const char *s, char **sp) {
|
||||
return strtold(s, sp);
|
||||
}
|
||||
|
||||
static inline int quadmath_snprintf(char *s, size_t size, const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(format, args);
|
||||
int result = vsnprintf(s, size, format, args);
|
||||
va_end(args);
|
||||
return result;
|
||||
}
|
||||
|
||||
#else
|
||||
extern "C" {
|
||||
__float128 fmodq(__float128 a, __float128 b);
|
||||
__float128 ceilq(__float128 a);
|
||||
@ -15,5 +46,6 @@ extern "C" {
|
||||
__float128 strtoflt128 (const char *s, char **sp);
|
||||
int quadmath_snprintf (char *s, size_t size, const char *format, ...);
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@ -234,7 +234,7 @@ struct Tokenize {
|
||||
BigInt significand;
|
||||
};
|
||||
|
||||
__attribute__ ((format (printf, 2, 3)))
|
||||
ATTRIBUTE_PRINTF(2, 3)
|
||||
static void tokenize_error(Tokenize *t, const char *format, ...) {
|
||||
t->state = TokenizeStateError;
|
||||
|
||||
@ -331,7 +331,7 @@ static void end_float_token(Tokenize *t) {
|
||||
if (t->radix == 10) {
|
||||
zig_panic("TODO: decimal floats");
|
||||
} else {
|
||||
int significand_magnitude_in_bin = __builtin_clzll(1) - __builtin_clzll(significand);
|
||||
int significand_magnitude_in_bin = clzll(1) - clzll(significand);
|
||||
t->exponent_in_bin_or_dec += significand_magnitude_in_bin;
|
||||
if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {
|
||||
t->cur_tok->data.float_lit.overflow = true;
|
||||
|
||||
49
src/util.hpp
49
src/util.hpp
@ -15,21 +15,54 @@
|
||||
|
||||
#include <new>
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
|
||||
#include <intrin.h>
|
||||
|
||||
#define ATTRIBUTE_COLD __declspec(noinline)
|
||||
#define ATTRIBUTE_PRINTF(a, b)
|
||||
#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
|
||||
#define ATTRIBUTE_NORETURN __declspec(noreturn)
|
||||
|
||||
static inline int clzll(unsigned long long mask) {
|
||||
unsigned long lz;
|
||||
#if defined(_WIN64)
|
||||
if (_BitScanReverse64(&lz, mask))
|
||||
return static_cast<int>(63-lz);
|
||||
zig_unreachable();
|
||||
#else
|
||||
if (_BitScanReverse(&lz, mask >> 32))
|
||||
lz += 32;
|
||||
else
|
||||
_BitScanReverse(&lz, mask & 0xffffffff);
|
||||
return 63 - lz;
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
|
||||
#define ATTRIBUTE_COLD __attribute__((cold))
|
||||
#define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
|
||||
#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
|
||||
#define ATTRIBUTE_NORETURN __attribute__((noreturn))
|
||||
#define clzll(x) __builtin_clzll(x)
|
||||
|
||||
#endif
|
||||
|
||||
#define BREAKPOINT __asm("int $0x03")
|
||||
|
||||
void zig_panic(const char *format, ...)
|
||||
__attribute__((cold))
|
||||
__attribute__ ((noreturn))
|
||||
__attribute__ ((format (printf, 1, 2)));
|
||||
ATTRIBUTE_COLD
|
||||
ATTRIBUTE_NORETURN
|
||||
ATTRIBUTE_PRINTF(1, 2)
|
||||
void zig_panic(const char *format, ...);
|
||||
|
||||
__attribute__((cold))
|
||||
__attribute__ ((noreturn))
|
||||
ATTRIBUTE_COLD
|
||||
ATTRIBUTE_NORETURN
|
||||
static inline void zig_unreachable(void) {
|
||||
zig_panic("unreachable");
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
__attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {
|
||||
ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) {
|
||||
T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));
|
||||
if (!ptr)
|
||||
zig_panic("allocation failed");
|
||||
@ -37,7 +70,7 @@ __attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
__attribute__((malloc)) static inline T *allocate(size_t count) {
|
||||
ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
|
||||
T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));
|
||||
if (!ptr)
|
||||
zig_panic("allocation failed");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user