authorgravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2017-09-10 14:05:18-06:00
committergravatar for johnnymarler@gmail.comJonathan Marler <johnnymarler@gmail.com> 2017-09-11 09:26:26-06:00
log7c81cd30de876f8bc56a4c689372e4d388b8608c
tree792c0783c39807575e40abd2d467e64e17d5d889
parent373785ae8d49d0ae3785020f05573763268ee9e1

Add support for MSVC


17 files changed, 238 insertions(+), 60 deletions(-)

CMakeLists.txt+26-10
...@@ -130,16 +130,21 @@ else()...@@ -130,16 +130,21 @@ else()
130 add_library(embedded_lld_lib ${EMBEDDED_LLD_LIB_SOURCES})130 add_library(embedded_lld_lib ${EMBEDDED_LLD_LIB_SOURCES})
131 add_library(embedded_lld_elf ${EMBEDDED_LLD_ELF_SOURCES})131 add_library(embedded_lld_elf ${EMBEDDED_LLD_ELF_SOURCES})
132 add_library(embedded_lld_coff ${EMBEDDED_LLD_COFF_SOURCES})132 add_library(embedded_lld_coff ${EMBEDDED_LLD_COFF_SOURCES})
133 if(MINGW)
134 set(UNIQUE_COMPILE_FLAGS "-fno-exceptions -fno-rtti -Wno-comment")
135 elseif(MSVC)
136 set(UNIQUE_COMPILE_FLAGS "-D_MSVC")
137 endif()
133 set_target_properties(embedded_lld_lib PROPERTIES138 set_target_properties(embedded_lld_lib PROPERTIES
134 COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment"139 COMPILE_FLAGS "-std=c++11 ${UNIQUE_COMPILE_FLAGS}"
135 LINK_FLAGS " "140 LINK_FLAGS " "
136 )141 )
137 set_target_properties(embedded_lld_elf PROPERTIES142 set_target_properties(embedded_lld_elf PROPERTIES
138 COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment"143 COMPILE_FLAGS "-std=c++11 ${UNIQUE_COMPILE_FLAGS}"
139 LINK_FLAGS " "144 LINK_FLAGS " "
140 )145 )
141 set_target_properties(embedded_lld_coff PROPERTIES146 set_target_properties(embedded_lld_coff PROPERTIES
142 COMPILE_FLAGS "-std=c++11 -fno-exceptions -fno-rtti -Wno-comment"147 COMPILE_FLAGS "-std=c++11 ${UNIQUE_COMPILE_FLAGS}"
143 LINK_FLAGS " "148 LINK_FLAGS " "
144 )149 )
145 target_include_directories(embedded_lld_lib PUBLIC150 target_include_directories(embedded_lld_lib PUBLIC
...@@ -211,14 +216,17 @@ include_directories(...@@ -211,14 +216,17 @@ include_directories(
211 "${CMAKE_SOURCE_DIR}/src"216 "${CMAKE_SOURCE_DIR}/src"
212)217)
213218
214set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror -Wall")219set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
215
216
217if(MINGW)220if(MINGW)
218 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wno-error=format= -Wno-error=format -Wno-error=format-extra-args")221 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Werror -Wno-error=format= -Wno-error=format -Wno-error=format-extra-args")
219endif()222endif()
220223
221set(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")224set(EXE_CFLAGS "-std=c++11")
225if(MINGW)
226 set(EXE_CFLAGS "${EXE_CFLAGS} -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")
227elseif(MSVC)
228 set(EXE_CFLAGS "${EXE_CFLAGS} -D_MSVC")
229endif()
222set(EXE_LDFLAGS " ")230set(EXE_LDFLAGS " ")
223if(ZIG_TEST_COVERAGE)231if(ZIG_TEST_COVERAGE)
224 set(EXE_CFLAGS "${EXE_CFLAGS} -fprofile-arcs -ftest-coverage")232 set(EXE_CFLAGS "${EXE_CFLAGS} -fprofile-arcs -ftest-coverage")
...@@ -230,16 +238,23 @@ set_target_properties(zig PROPERTIES...@@ -230,16 +238,23 @@ set_target_properties(zig PROPERTIES
230 COMPILE_FLAGS ${EXE_CFLAGS}238 COMPILE_FLAGS ${EXE_CFLAGS}
231 LINK_FLAGS ${EXE_LDFLAGS}239 LINK_FLAGS ${EXE_LDFLAGS}
232)240)
241
242if(MINGW)
243 set(PLATFORM_LIBRARIES quadmath)
244endif()
233target_link_libraries(zig LINK_PUBLIC245target_link_libraries(zig LINK_PUBLIC
234 ${CLANG_LIBRARIES}246 ${CLANG_LIBRARIES}
235 ${LLD_LIBRARIES}247 ${LLD_LIBRARIES}
236 ${LLVM_LIBRARIES}248 ${LLVM_LIBRARIES}
237 ${CMAKE_THREAD_LIBS_INIT}249 ${CMAKE_THREAD_LIBS_INIT}
238 quadmath250 ${PLATFORM_LIBRARIES}
239)251)
240if(MINGW)252if(MINGW OR MSVC)
241 target_link_libraries(zig LINK_PUBLIC version)253 target_link_libraries(zig LINK_PUBLIC version)
242endif()254endif()
255if(MSVC)
256 target_link_libraries(zig LINK_PUBLIC "C:/Program Files (x86)/Microsoft Visual Studio 14.0/DIA SDK/lib/diaguids.lib")
257endif()
243install(TARGETS zig DESTINATION bin)258install(TARGETS zig DESTINATION bin)
244259
245install(FILES "${CMAKE_SOURCE_DIR}/c_headers/__clang_cuda_builtin_vars.h" DESTINATION "${C_HEADERS_DEST}")260install(FILES "${CMAKE_SOURCE_DIR}/c_headers/__clang_cuda_builtin_vars.h" DESTINATION "${C_HEADERS_DEST}")
...@@ -455,3 +470,4 @@ if (ZIG_TEST_COVERAGE)...@@ -455,3 +470,4 @@ if (ZIG_TEST_COVERAGE)
455 COMMAND rm coverage.info coverage.info.cleaned470 COMMAND rm coverage.info coverage.info.cleaned
456 )471 )
457endif()472endif()
473
cmake/Findclang.cmake+7-1
...@@ -8,14 +8,20 @@...@@ -8,14 +8,20 @@
88
9find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h9find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
10 PATHS10 PATHS
11 ${LLVM_INSTALL_PREFIX}/include
11 /usr/lib/llvm/5/include12 /usr/lib/llvm/5/include
12 /usr/lib/llvm-5.0/include13 /usr/lib/llvm-5.0/include
13 /mingw64/include)14 /mingw64/include)
1415
15 macro(FIND_AND_ADD_CLANG_LIB _libname_)16if(NOT CLANG_INCLUDE_DIRS)
17 message(FATAL_ERROR "Failed to find CLANG header files")
18endif()
19
20macro(FIND_AND_ADD_CLANG_LIB _libname_)
16 string(TOUPPER ${_libname_} _prettylibname_)21 string(TOUPPER ${_libname_} _prettylibname_)
17 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}22 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
18 PATHS23 PATHS
24 ${LLVM_INSTALL_PREFIX}/lib
19 /usr/lib/llvm/5/lib25 /usr/lib/llvm/5/lib
20 /usr/lib/llvm-5.0/lib26 /usr/lib/llvm-5.0/lib
21 /mingw64/lib27 /mingw64/lib
cmake/Findllvm.cmake+35-10
...@@ -2,28 +2,53 @@...@@ -2,28 +2,53 @@
2# This file is MIT licensed.2# This file is MIT licensed.
3# See http://opensource.org/licenses/MIT3# See http://opensource.org/licenses/MIT
44
5# LLVM_FOUND
6# LLVM_INCLUDE_DIR5# LLVM_INCLUDE_DIR
7# LLVM_LIBRARIES6# LLVM_LIBRARIES
8# LLVM_LIBDIRS7# LLVM_LIBDIRS
98
10find_program(LLVM_CONFIG_EXE9if(LLVM_INSTALL_PREFIX)
11 NAMES llvm-config-5.0 llvm-config10 find_program(LLVM_CONFIG_EXE
12 PATHS11 NAMES llvm-config-5.0 llvm-config
13 "/mingw64/bin"12 PATHS ${LLVM_INSTALL_PREFIX}/bin
14 "/c/msys64/mingw64/bin"13 NO_DEFAULT_PATH)
15 "c:/msys64/mingw64/bin"14 if(NOT LLVM_CONFIG_EXE)
16 "C:/Libraries/llvm-5.0.0/bin")15 message(FATAL_ERROR "Invalid LLVM_INSTALL_PREFIX \"${LLVM_INSTALL_PREFIX}\", could not find llvm-config")
16 endif()
17else()
18 find_program(LLVM_CONFIG_EXE
19 NAMES llvm-config-5.0 llvm-config
20 PATHS
21 "/mingw64/bin"
22 "/c/msys64/mingw64/bin"
23 "c:/msys64/mingw64/bin"
24 "C:/Libraries/llvm-5.0.0/bin")
25 if(NOT LLVM_CONFIG_EXE)
26 message(FATAL_ERROR "Could not find llvm-config, use -DLLVM_INSTALL_PREFIX to specify the install path")
27 endif()
28 execute_process(
29 COMMAND ${LLVM_CONFIG_EXE} --prefix
30 OUTPUT_VARIABLE LLVM_INSTALL_PREFIX
31 OUTPUT_STRIP_TRAILING_WHITESPACE)
32endif()
33
34if(${LLVM_INSTALL_PREFIX} MATCHES "^.* ")
35 # NOTE: this is a limitation due to llvm-config. If the path contains spaces then there's
36 # no way to tell from the output of llvm-config whether a space is seperating a filename, or
37 # just a space in the path name.
38 message(FATAL_ERROR "The LLVM install path \"${LLVM_INSTALL_PREFIX}\" cannot contain spaces")
39endif()
1740
18execute_process(41execute_process(
19 COMMAND ${LLVM_CONFIG_EXE} --libs42 COMMAND ${LLVM_CONFIG_EXE} --libs
20 OUTPUT_VARIABLE LLVM_LIBRARIES43 OUTPUT_VARIABLE LLVM_LIBRARIES_STRING
21 OUTPUT_STRIP_TRAILING_WHITESPACE)44 OUTPUT_STRIP_TRAILING_WHITESPACE)
45string(REPLACE " " ";" LLVM_LIBRARIES ${LLVM_LIBRARIES_STRING})
2246
23execute_process(47execute_process(
24 COMMAND ${LLVM_CONFIG_EXE} --system-libs48 COMMAND ${LLVM_CONFIG_EXE} --system-libs
25 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS49 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS_STRING
26 OUTPUT_STRIP_TRAILING_WHITESPACE)50 OUTPUT_STRIP_TRAILING_WHITESPACE)
51string(REPLACE " " ";" LLVM_SYSTEM_LIBS ${LLVM_SYSTEM_LIBS_STRING})
2752
28execute_process(53execute_process(
29 COMMAND ${LLVM_CONFIG_EXE} --libdir54 COMMAND ${LLVM_CONFIG_EXE} --libdir
src/analyze.cpp+1-1
...@@ -163,7 +163,7 @@ static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *so...@@ -163,7 +163,7 @@ static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *so
163}163}
164164
165static uint8_t log2_u64(uint64_t x) {165static uint8_t log2_u64(uint64_t x) {
166 return (63 - __builtin_clzll(x));166 return (63 - clzll(x));
167}167}
168168
169static uint8_t bits_needed_for_unsigned(uint64_t x) {169static uint8_t bits_needed_for_unsigned(uint64_t x) {
src/bigfloat.hpp+8
...@@ -13,6 +13,14 @@...@@ -13,6 +13,14 @@
13#include <stdint.h>13#include <stdint.h>
14#include <stddef.h>14#include <stddef.h>
1515
16#if defined(_MSVC)
17/*
18 * For now this is a placeholder until a better solution comes along to
19 * support 128-bit floats with MSVC.
20 */
21typedef long double __float128;
22#endif
23
16struct BigFloat {24struct BigFloat {
17 __float128 value;25 __float128 value;
18};26};
src/bigint.cpp+44-8
...@@ -141,7 +141,7 @@ void bigint_init_unsigned(BigInt *dest, uint64_t x) {...@@ -141,7 +141,7 @@ void bigint_init_unsigned(BigInt *dest, uint64_t x) {
141 dest->is_negative = false;141 dest->is_negative = false;
142}142}
143143
144void bigint_init_u128(BigInt *dest, unsigned __int128 x) {144void bigint_init_u128(BigInt *dest, uint128_t x) {
145 uint64_t low = (uint64_t)(x & UINT64_MAX);145 uint64_t low = (uint64_t)(x & UINT64_MAX);
146 uint64_t high = (uint64_t)(x >> 64);146 uint64_t high = (uint64_t)(x >> 64);
147147
...@@ -201,9 +201,9 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {...@@ -201,9 +201,9 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {
201201
202void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {202void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
203 if (op->value >= 0) {203 if (op->value >= 0) {
204 bigint_init_u128(dest, (unsigned __int128)(op->value));204 bigint_init_u128(dest, (uint128_t)(op->value));
205 } else {205 } else {
206 bigint_init_u128(dest, (unsigned __int128)(-op->value));206 bigint_init_u128(dest, (uint128_t)(-op->value));
207 dest->is_negative = true;207 dest->is_negative = true;
208 }208 }
209}209}
...@@ -377,6 +377,41 @@ void bigint_read_twos_complement(BigInt *dest, const uint8_t *buf, size_t bit_co...@@ -377,6 +377,41 @@ void bigint_read_twos_complement(BigInt *dest, const uint8_t *buf, size_t bit_co
377 }377 }
378}378}
379379
380#if defined(_MSVC)
381/*
382 * Inneficient implmentations for now
383 */
384static bool add_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
385 *result = op1 + op2;
386 if(*result - op2 != op1) {
387 return true; // overflow
388 }
389 return false; // no overflow
390}
391
392static bool sub_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
393 *result = op1 - op2;
394 if(*result > op1)
395 {
396 return true; // overflow
397 }
398 return false; // no overflow
399}
400
401bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
402 *result = op1 * op2;
403 if(op1 <= op2) {
404 if(*result / op1 != op2) {
405 return true; // overflow
406 }
407 } else {
408 if(*result / op2 != op1) {
409 return true; // overflow
410 }
411 }
412 return false; // no overflow
413}
414#else
380static bool add_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {415static bool add_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
381 return __builtin_uaddll_overflow((unsigned long long)op1, (unsigned long long)op2,416 return __builtin_uaddll_overflow((unsigned long long)op1, (unsigned long long)op2,
382 (unsigned long long *)result);417 (unsigned long long *)result);
...@@ -387,10 +422,11 @@ static bool sub_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {...@@ -387,10 +422,11 @@ static bool sub_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
387 (unsigned long long *)result);422 (unsigned long long *)result);
388}423}
389424
390static bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {425bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
391 return __builtin_umulll_overflow((unsigned long long)op1, (unsigned long long)op2,426 return __builtin_umulll_overflow((unsigned long long)op1, (unsigned long long)op2,
392 (unsigned long long *)result);427 (unsigned long long *)result);
393}428}
429#endif
394430
395void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {431void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
396 if (op1->digit_count == 0) {432 if (op1->digit_count == 0) {
...@@ -404,7 +440,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {...@@ -404,7 +440,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
404440
405 const uint64_t *op1_digits = bigint_ptr(op1);441 const uint64_t *op1_digits = bigint_ptr(op1);
406 const uint64_t *op2_digits = bigint_ptr(op2);442 const uint64_t *op2_digits = bigint_ptr(op2);
407 uint64_t overflow = add_u64_overflow(op1_digits[0], op2_digits[0], &dest->data.digit);443 bool overflow = add_u64_overflow(op1_digits[0], op2_digits[0], &dest->data.digit);
408 if (overflow == 0 && op1->digit_count == 1 && op2->digit_count == 1) {444 if (overflow == 0 && op1->digit_count == 1 && op2->digit_count == 1) {
409 dest->digit_count = 1;445 dest->digit_count = 1;
410 bigint_normalize(dest);446 bigint_normalize(dest);
...@@ -534,9 +570,9 @@ static void mul_overflow(uint64_t x, uint64_t y, uint64_t *result, uint64_t *car...@@ -534,9 +570,9 @@ static void mul_overflow(uint64_t x, uint64_t y, uint64_t *result, uint64_t *car
534 return;570 return;
535 }571 }
536572
537 unsigned __int128 big_x = x;573 uint128_t big_x = x;
538 unsigned __int128 big_y = y;574 uint128_t big_y = y;
539 unsigned __int128 big_result = big_x * big_y;575 uint128_t big_result = big_x * big_y;
540 *carry = big_result >> 64;576 *carry = big_result >> 64;
541}577}
542578
src/bigint.hpp+12-1
...@@ -11,6 +11,15 @@...@@ -11,6 +11,15 @@
11#include <stdint.h>11#include <stdint.h>
12#include <stddef.h>12#include <stddef.h>
1313
14#if defined(_MSVC)
15 // TEMPORARY WORKAROUND FOR MSVC NOT SUPPORTING __int128
16 typedef long long int128_t;
17 typedef unsigned long long uint128_t;
18#else
19 typedef __int128 int128_t;
20 typedef unsigned __int128 uint128_t;
21#endif
22
14struct BigInt {23struct BigInt {
15 size_t digit_count;24 size_t digit_count;
16 union {25 union {
...@@ -30,7 +39,7 @@ enum Cmp {...@@ -30,7 +39,7 @@ enum Cmp {
30};39};
3140
32void bigint_init_unsigned(BigInt *dest, uint64_t x);41void bigint_init_unsigned(BigInt *dest, uint64_t x);
33void bigint_init_u128(BigInt *dest, unsigned __int128 x);42void bigint_init_u128(BigInt *dest, uint128_t x);
34void bigint_init_signed(BigInt *dest, int64_t x);43void bigint_init_signed(BigInt *dest, int64_t x);
35void bigint_init_bigint(BigInt *dest, const BigInt *src);44void bigint_init_bigint(BigInt *dest, const BigInt *src);
36void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);45void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);
...@@ -89,4 +98,6 @@ size_t bigint_bits_needed(const BigInt *op);...@@ -89,4 +98,6 @@ size_t bigint_bits_needed(const BigInt *op);
89// convenience functions98// convenience functions
90Cmp bigint_cmp_zero(const BigInt *op);99Cmp bigint_cmp_zero(const BigInt *op);
91100
101bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result);
102
92#endif103#endif
src/buffer.hpp+2-2
...@@ -24,7 +24,7 @@ struct Buf {...@@ -24,7 +24,7 @@ struct Buf {
24};24};
2525
26Buf *buf_sprintf(const char *format, ...)26Buf *buf_sprintf(const char *format, ...)
27 __attribute__ ((format (printf, 1, 2)));27 ATTRIBUTE_FORMAT(printf, 1, 2);
28Buf *buf_vprintf(const char *format, va_list ap);28Buf *buf_vprintf(const char *format, va_list ap);
2929
30static inline size_t buf_len(Buf *buf) {30static inline size_t buf_len(Buf *buf) {
...@@ -124,7 +124,7 @@ static inline void buf_append_char(Buf *buf, uint8_t c) {...@@ -124,7 +124,7 @@ static inline void buf_append_char(Buf *buf, uint8_t c) {
124}124}
125125
126void buf_appendf(Buf *buf, const char *format, ...)126void buf_appendf(Buf *buf, const char *format, ...)
127 __attribute__ ((format (printf, 2, 3)));127 ATTRIBUTE_FORMAT(printf, 2, 3);
128128
129static inline bool buf_eql_mem(Buf *buf, const char *mem, size_t mem_len) {129static inline bool buf_eql_mem(Buf *buf, const char *mem, size_t mem_len) {
130 assert(buf->list.length);130 assert(buf->list.length);
src/codegen.cpp+14-1
...@@ -5194,7 +5194,19 @@ void codegen_add_object(CodeGen *g, Buf *object_path) {...@@ -5194,7 +5194,19 @@ void codegen_add_object(CodeGen *g, Buf *object_path) {
5194 g->link_objects.append(object_path);5194 g->link_objects.append(object_path);
5195}5195}
51965196
51975197#if defined(_MSVC)
5198// MSVC doesn't seem to support "designators" for array initialization
5199static const char *c_int_type_names[] = {
5200 "short",
5201 "unsigned short",
5202 "int",
5203 "unsigned int",
5204 "long",
5205 "unsigned long",
5206 "long long",
5207 "unsigned long long",
5208};
5209#else
5198static const char *c_int_type_names[] = {5210static const char *c_int_type_names[] = {
5199 [CIntTypeShort] = "short",5211 [CIntTypeShort] = "short",
5200 [CIntTypeUShort] = "unsigned short",5212 [CIntTypeUShort] = "unsigned short",
...@@ -5205,6 +5217,7 @@ static const char *c_int_type_names[] = {...@@ -5205,6 +5217,7 @@ static const char *c_int_type_names[] = {
5205 [CIntTypeLongLong] = "long long",5217 [CIntTypeLongLong] = "long long",
5206 [CIntTypeULongLong] = "unsigned long long",5218 [CIntTypeULongLong] = "unsigned long long",
5207};5219};
5220#endif
52085221
5209static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {5222static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
5210 assert(type_entry);5223 assert(type_entry);
src/ir.cpp+3-4
...@@ -6500,9 +6500,9 @@ static void float_init_bigint(BigInt *bigint, ConstExprValue *const_val) {...@@ -6500,9 +6500,9 @@ static void float_init_bigint(BigInt *bigint, ConstExprValue *const_val) {
6500 break;6500 break;
6501 case 128:6501 case 128:
6502 if (const_val->data.x_f128 >= 0) {6502 if (const_val->data.x_f128 >= 0) {
6503 bigint_init_u128(bigint, (unsigned __int128)(const_val->data.x_f128));6503 bigint_init_u128(bigint, (uint128_t)(const_val->data.x_f128));
6504 } else {6504 } else {
6505 bigint_init_u128(bigint, (unsigned __int128)(-const_val->data.x_f128));6505 bigint_init_u128(bigint, (uint128_t)(-const_val->data.x_f128));
6506 bigint->is_negative = true;6506 bigint->is_negative = true;
6507 }6507 }
6508 break;6508 break;
...@@ -9731,8 +9731,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp...@@ -9731,8 +9731,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
9731 uint64_t old_array_len = array_type->data.array.len;9731 uint64_t old_array_len = array_type->data.array.len;
9732 uint64_t new_array_len;9732 uint64_t new_array_len;
97339733
9734 if (__builtin_umulll_overflow((unsigned long long)old_array_len, (unsigned long long)mult_amt,9734 if (mul_u64_overflow(old_array_len, mult_amt, &new_array_len))
9735 (unsigned long long*)&new_array_len))
9736 {9735 {
9737 ir_add_error(ira, &instruction->base, buf_sprintf("operation results in overflow"));9736 ir_add_error(ira, &instruction->base, buf_sprintf("operation results in overflow"));
9738 return ira->codegen->builtin_types.entry_invalid;9737 return ira->codegen->builtin_types.entry_invalid;
src/os.cpp+7-5
...@@ -25,6 +25,8 @@...@@ -25,6 +25,8 @@
2525
26#include <windows.h>26#include <windows.h>
27#include <io.h>27#include <io.h>
28
29typedef SSIZE_T ssize_t;
28#else30#else
29#define ZIG_OS_POSIX31#define ZIG_OS_POSIX
3032
...@@ -620,7 +622,7 @@ int os_get_cwd(Buf *out_cwd) {...@@ -620,7 +622,7 @@ int os_get_cwd(Buf *out_cwd) {
620622
621bool os_stderr_tty(void) {623bool os_stderr_tty(void) {
622#if defined(ZIG_OS_WINDOWS)624#if defined(ZIG_OS_WINDOWS)
623 return _isatty(STDERR_FILENO) != 0;625 return _isatty(_fileno(stderr)) != 0;
624#elif defined(ZIG_OS_POSIX)626#elif defined(ZIG_OS_POSIX)
625 return isatty(STDERR_FILENO) != 0;627 return isatty(STDERR_FILENO) != 0;
626#else628#else
...@@ -777,12 +779,12 @@ int os_make_path(Buf *path) {...@@ -777,12 +779,12 @@ int os_make_path(Buf *path) {
777779
778int os_make_dir(Buf *path) {780int os_make_dir(Buf *path) {
779#if defined(ZIG_OS_WINDOWS)781#if defined(ZIG_OS_WINDOWS)
780 if (mkdir(buf_ptr(path)) == -1) {782 if (!CreateDirectory(buf_ptr(path), NULL)) {
781 if (errno == EEXIST)783 if (GetLastError() == ERROR_ALREADY_EXISTS)
782 return ErrorPathAlreadyExists;784 return ErrorPathAlreadyExists;
783 if (errno == ENOENT)785 if (GetLastError() == ERROR_PATH_NOT_FOUND)
784 return ErrorFileNotFound;786 return ErrorFileNotFound;
785 if (errno == EACCES)787 if (GetLastError() == ERROR_ACCESS_DENIED)
786 return ErrorAccess;788 return ErrorAccess;
787 return ErrorUnexpected;789 return ErrorUnexpected;
788 }790 }
src/parsec.cpp+1-1
...@@ -56,7 +56,7 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl);...@@ -56,7 +56,7 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl);
56static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl);56static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl);
5757
5858
59__attribute__ ((format (printf, 3, 4)))59ATTRIBUTE_FORMAT(printf, 3, 4)
60static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {60static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {
61 if (!c->warnings_on) {61 if (!c->warnings_on) {
62 return;62 return;
src/parser.cpp+5-5
...@@ -24,8 +24,8 @@ struct ParseContext {...@@ -24,8 +24,8 @@ struct ParseContext {
24 Buf *void_buf;24 Buf *void_buf;
25};25};
2626
27__attribute__ ((format (printf, 4, 5)))27ATTRIBUTE_FORMAT(printf, 4, 5)
28__attribute__ ((noreturn))28LLVM_ATTRIBUTE_NORETURN
29static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const char *format, ...) {29static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const char *format, ...) {
30 assert(node->type == NodeTypeAsmExpr);30 assert(node->type == NodeTypeAsmExpr);
3131
...@@ -46,8 +46,8 @@ static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const...@@ -46,8 +46,8 @@ static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const
46 exit(EXIT_FAILURE);46 exit(EXIT_FAILURE);
47}47}
4848
49__attribute__ ((format (printf, 3, 4)))49ATTRIBUTE_FORMAT(printf, 3, 4)
50__attribute__ ((noreturn))50LLVM_ATTRIBUTE_NORETURN
51static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {51static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
52 va_list ap;52 va_list ap;
53 va_start(ap, format);53 va_start(ap, format);
...@@ -205,7 +205,7 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {...@@ -205,7 +205,7 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {
205 }205 }
206}206}
207207
208__attribute__ ((noreturn))208LLVM_ATTRIBUTE_NORETURN
209static void ast_invalid_token_error(ParseContext *pc, Token *token) {209static void ast_invalid_token_error(ParseContext *pc, Token *token) {
210 Buf token_value = BUF_INIT;210 Buf token_value = BUF_INIT;
211 ast_buf_from_token(pc, token, &token_value);211 ast_buf_from_token(pc, token, &token_value);
src/parser.hpp+1-1
...@@ -12,7 +12,7 @@...@@ -12,7 +12,7 @@
12#include "tokenizer.hpp"12#include "tokenizer.hpp"
13#include "errmsg.hpp"13#include "errmsg.hpp"
1414
15__attribute__ ((format (printf, 2, 3)))15ATTRIBUTE_FORMAT(printf, 2, 3)
16void ast_token_error(Token *token, const char *format, ...);16void ast_token_error(Token *token, const char *format, ...);
1717
1818
src/quadmath.hpp+34
...@@ -8,12 +8,46 @@...@@ -8,12 +8,46 @@
8#ifndef ZIG_QUADMATH_HPP8#ifndef ZIG_QUADMATH_HPP
9#define ZIG_QUADMATH_HPP9#define ZIG_QUADMATH_HPP
1010
11#if defined(_MSVC)
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <stdarg.h>
15 #include <cmath>
16#endif
17
11extern "C" {18extern "C" {
19#if defined(_MSVC)
20 static __float128 fmodq(__float128 a, __float128 b)
21 {
22 return fmod(a, b);
23 }
24 static __float128 ceilq(__float128 a)
25 {
26 return ceil(a);
27 }
28 static __float128 floorq(__float128 a)
29 {
30 return floor(a);
31 }
32 static __float128 strtoflt128(const char *s, char **sp)
33 {
34 return strtold(s, sp);
35 }
36 static int quadmath_snprintf(char *s, size_t size, const char *format, ...)
37 {
38 va_list args;
39 va_start(format, args);
40 int result = vsnprintf(s, size, format, args);
41 va_end(args);
42 return result;
43 }
44#else
12 __float128 fmodq(__float128 a, __float128 b);45 __float128 fmodq(__float128 a, __float128 b);
13 __float128 ceilq(__float128 a);46 __float128 ceilq(__float128 a);
14 __float128 floorq(__float128 a);47 __float128 floorq(__float128 a);
15 __float128 strtoflt128 (const char *s, char **sp);48 __float128 strtoflt128 (const char *s, char **sp);
16 int quadmath_snprintf (char *s, size_t size, const char *format, ...);49 int quadmath_snprintf (char *s, size_t size, const char *format, ...);
50#endif
17}51}
1852
19#endif53#endif
src/tokenizer.cpp+2-2
...@@ -234,7 +234,7 @@ struct Tokenize {...@@ -234,7 +234,7 @@ struct Tokenize {
234 BigInt significand;234 BigInt significand;
235};235};
236236
237__attribute__ ((format (printf, 2, 3)))237ATTRIBUTE_FORMAT(printf, 2, 3)
238static void tokenize_error(Tokenize *t, const char *format, ...) {238static void tokenize_error(Tokenize *t, const char *format, ...) {
239 t->state = TokenizeStateError;239 t->state = TokenizeStateError;
240240
...@@ -331,7 +331,7 @@ static void end_float_token(Tokenize *t) {...@@ -331,7 +331,7 @@ static void end_float_token(Tokenize *t) {
331 if (t->radix == 10) {331 if (t->radix == 10) {
332 zig_panic("TODO: decimal floats");332 zig_panic("TODO: decimal floats");
333 } else {333 } else {
334 int significand_magnitude_in_bin = __builtin_clzll(1) - __builtin_clzll(significand);334 int significand_magnitude_in_bin = clzll(1) - clzll(significand);
335 t->exponent_in_bin_or_dec += significand_magnitude_in_bin;335 t->exponent_in_bin_or_dec += significand_magnitude_in_bin;
336 if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {336 if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {
337 t->cur_tok->data.float_lit.overflow = true;337 t->cur_tok->data.float_lit.overflow = true;
src/util.hpp+36-8
...@@ -12,24 +12,52 @@...@@ -12,24 +12,52 @@
12#include <stdint.h>12#include <stdint.h>
13#include <string.h>13#include <string.h>
14#include <assert.h>14#include <assert.h>
15#include <llvm/Support/Compiler.h>
1516
16#include <new>17#include <new>
1718
19#if defined(_MSVC)
20 #define ATTRIBUTE_COLD
21 #define ATTRIBUTE_FORMAT(args)
22 static inline uint32_t popcnt(unsigned long long x)
23 {
24 x -= ((x >> 1) & 0x55555555);
25 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
26 x = (((x >> 4) + x) & 0x0f0f0f0f);
27 x += (x >> 8);
28 x += (x >> 16);
29 return x & 0x0000003f;
30 }
31 static inline uint32_t clzll(unsigned long long x)
32 {
33 x |= (x >> 1);
34 x |= (x >> 2);
35 x |= (x >> 4);
36 x |= (x >> 8);
37 x |= (x >> 16);
38 return 32 - popcnt(x);
39 }
40#else
41 #define ATTRIBUTE_COLD __attribute__((cold))
42 #define ATTRIBUTE_FORMAT(args) __attribute__((format (args)))
43 #define clzll(x) __builtin_clzll(x)
44#endif
45
18#define BREAKPOINT __asm("int $0x03")46#define BREAKPOINT __asm("int $0x03")
1947
20void zig_panic(const char *format, ...)48LLVM_ATTRIBUTE_NOINLINE
21 __attribute__((cold))49ATTRIBUTE_COLD
22 __attribute__ ((noreturn))50ATTRIBUTE_FORMAT(printf, 1, 2)
23 __attribute__ ((format (printf, 1, 2)));51void zig_panic(const char *format, ...);
2452
25__attribute__((cold))53ATTRIBUTE_COLD
26__attribute__ ((noreturn))54LLVM_ATTRIBUTE_NOINLINE
27static inline void zig_unreachable(void) {55static inline void zig_unreachable(void) {
28 zig_panic("unreachable");56 zig_panic("unreachable");
29}57}
3058
31template<typename T>59template<typename T>
32__attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {60LLVM_ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate_nonzero(size_t count) {
33 T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));61 T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));
34 if (!ptr)62 if (!ptr)
35 zig_panic("allocation failed");63 zig_panic("allocation failed");
...@@ -37,7 +65,7 @@ __attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {...@@ -37,7 +65,7 @@ __attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {
37}65}
3866
39template<typename T>67template<typename T>
40__attribute__((malloc)) static inline T *allocate(size_t count) {68LLVM_ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
41 T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));69 T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));
42 if (!ptr)70 if (!ptr)
43 zig_panic("allocation failed");71 zig_panic("allocation failed");