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()
130130 add_library(embedded_lld_lib ${EMBEDDED_LLD_LIB_SOURCES})
131131 add_library(embedded_lld_elf ${EMBEDDED_LLD_ELF_SOURCES})
132132 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()
133138 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}"
135140 LINK_FLAGS " "
136141 )
137142 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}"
139144 LINK_FLAGS " "
140145 )
141146 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}"
143148 LINK_FLAGS " "
144149 )
145150 target_include_directories(embedded_lld_lib PUBLIC
......@@ -211,14 +216,17 @@ include_directories(
211216 "${CMAKE_SOURCE_DIR}/src"
212217)
213218
214set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror -Wall")
215
216
219set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
217220if(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")
219222endif()
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()
222230set(EXE_LDFLAGS " ")
223231if(ZIG_TEST_COVERAGE)
224232 set(EXE_CFLAGS "${EXE_CFLAGS} -fprofile-arcs -ftest-coverage")
......@@ -230,16 +238,23 @@ set_target_properties(zig PROPERTIES
230238 COMPILE_FLAGS ${EXE_CFLAGS}
231239 LINK_FLAGS ${EXE_LDFLAGS}
232240)
241
242if(MINGW)
243 set(PLATFORM_LIBRARIES quadmath)
244endif()
233245target_link_libraries(zig LINK_PUBLIC
234246 ${CLANG_LIBRARIES}
235247 ${LLD_LIBRARIES}
236248 ${LLVM_LIBRARIES}
237249 ${CMAKE_THREAD_LIBS_INIT}
238 quadmath
250 ${PLATFORM_LIBRARIES}
239251)
240if(MINGW)
252if(MINGW OR MSVC)
241253 target_link_libraries(zig LINK_PUBLIC version)
242254endif()
255if(MSVC)
256 target_link_libraries(zig LINK_PUBLIC "C:/Program Files (x86)/Microsoft Visual Studio 14.0/DIA SDK/lib/diaguids.lib")
257endif()
243258install(TARGETS zig DESTINATION bin)
244259
245260install(FILES "${CMAKE_SOURCE_DIR}/c_headers/__clang_cuda_builtin_vars.h" DESTINATION "${C_HEADERS_DEST}")
......@@ -455,3 +470,4 @@ if (ZIG_TEST_COVERAGE)
455470 COMMAND rm coverage.info coverage.info.cleaned
456471 )
457472endif()
473
cmake/Findclang.cmake+7-1
......@@ -8,14 +8,20 @@
88
99find_path(CLANG_INCLUDE_DIRS NAMES clang/Frontend/ASTUnit.h
1010 PATHS
11 ${LLVM_INSTALL_PREFIX}/include
1112 /usr/lib/llvm/5/include
1213 /usr/lib/llvm-5.0/include
1314 /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_)
1621 string(TOUPPER ${_libname_} _prettylibname_)
1722 find_library(CLANG_${_prettylibname_}_LIB NAMES ${_libname_}
1823 PATHS
24 ${LLVM_INSTALL_PREFIX}/lib
1925 /usr/lib/llvm/5/lib
2026 /usr/lib/llvm-5.0/lib
2127 /mingw64/lib
cmake/Findllvm.cmake+35-10
......@@ -2,28 +2,53 @@
22# This file is MIT licensed.
33# See http://opensource.org/licenses/MIT
44
5# LLVM_FOUND
65# LLVM_INCLUDE_DIR
76# LLVM_LIBRARIES
87# LLVM_LIBDIRS
98
10find_program(LLVM_CONFIG_EXE
11 NAMES llvm-config-5.0 llvm-config
12 PATHS
13 "/mingw64/bin"
14 "/c/msys64/mingw64/bin"
15 "c:/msys64/mingw64/bin"
16 "C:/Libraries/llvm-5.0.0/bin")
9if(LLVM_INSTALL_PREFIX)
10 find_program(LLVM_CONFIG_EXE
11 NAMES llvm-config-5.0 llvm-config
12 PATHS ${LLVM_INSTALL_PREFIX}/bin
13 NO_DEFAULT_PATH)
14 if(NOT LLVM_CONFIG_EXE)
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
1841execute_process(
1942 COMMAND ${LLVM_CONFIG_EXE} --libs
20 OUTPUT_VARIABLE LLVM_LIBRARIES
43 OUTPUT_VARIABLE LLVM_LIBRARIES_STRING
2144 OUTPUT_STRIP_TRAILING_WHITESPACE)
45string(REPLACE " " ";" LLVM_LIBRARIES ${LLVM_LIBRARIES_STRING})
2246
2347execute_process(
2448 COMMAND ${LLVM_CONFIG_EXE} --system-libs
25 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS
49 OUTPUT_VARIABLE LLVM_SYSTEM_LIBS_STRING
2650 OUTPUT_STRIP_TRAILING_WHITESPACE)
51string(REPLACE " " ";" LLVM_SYSTEM_LIBS ${LLVM_SYSTEM_LIBS_STRING})
2752
2853execute_process(
2954 COMMAND ${LLVM_CONFIG_EXE} --libdir
src/analyze.cpp+1-1
......@@ -163,7 +163,7 @@ static TypeTableEntry *new_container_type_entry(TypeTableEntryId id, AstNode *so
163163}
164164
165165static uint8_t log2_u64(uint64_t x) {
166 return (63 - __builtin_clzll(x));
166 return (63 - clzll(x));
167167}
168168
169169static uint8_t bits_needed_for_unsigned(uint64_t x) {
src/bigfloat.hpp+8
......@@ -13,6 +13,14 @@
1313#include <stdint.h>
1414#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
1624struct BigFloat {
1725 __float128 value;
1826};
src/bigint.cpp+44-8
......@@ -141,7 +141,7 @@ void bigint_init_unsigned(BigInt *dest, uint64_t x) {
141141 dest->is_negative = false;
142142}
143143
144void bigint_init_u128(BigInt *dest, unsigned __int128 x) {
144void bigint_init_u128(BigInt *dest, uint128_t x) {
145145 uint64_t low = (uint64_t)(x & UINT64_MAX);
146146 uint64_t high = (uint64_t)(x >> 64);
147147
......@@ -201,9 +201,9 @@ void bigint_init_bigint(BigInt *dest, const BigInt *src) {
201201
202202void bigint_init_bigfloat(BigInt *dest, const BigFloat *op) {
203203 if (op->value >= 0) {
204 bigint_init_u128(dest, (unsigned __int128)(op->value));
204 bigint_init_u128(dest, (uint128_t)(op->value));
205205 } else {
206 bigint_init_u128(dest, (unsigned __int128)(-op->value));
206 bigint_init_u128(dest, (uint128_t)(-op->value));
207207 dest->is_negative = true;
208208 }
209209}
......@@ -377,6 +377,41 @@ void bigint_read_twos_complement(BigInt *dest, const uint8_t *buf, size_t bit_co
377377 }
378378}
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
380415static bool add_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
381416 return __builtin_uaddll_overflow((unsigned long long)op1, (unsigned long long)op2,
382417 (unsigned long long *)result);
......@@ -387,10 +422,11 @@ static bool sub_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result) {
387422 (unsigned long long *)result);
388423}
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) {
391426 return __builtin_umulll_overflow((unsigned long long)op1, (unsigned long long)op2,
392427 (unsigned long long *)result);
393428}
429#endif
394430
395431void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
396432 if (op1->digit_count == 0) {
......@@ -404,7 +440,7 @@ void bigint_add(BigInt *dest, const BigInt *op1, const BigInt *op2) {
404440
405441 const uint64_t *op1_digits = bigint_ptr(op1);
406442 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);
408444 if (overflow == 0 && op1->digit_count == 1 && op2->digit_count == 1) {
409445 dest->digit_count = 1;
410446 bigint_normalize(dest);
......@@ -534,9 +570,9 @@ static void mul_overflow(uint64_t x, uint64_t y, uint64_t *result, uint64_t *car
534570 return;
535571 }
536572
537 unsigned __int128 big_x = x;
538 unsigned __int128 big_y = y;
539 unsigned __int128 big_result = big_x * big_y;
573 uint128_t big_x = x;
574 uint128_t big_y = y;
575 uint128_t big_result = big_x * big_y;
540576 *carry = big_result >> 64;
541577}
542578
src/bigint.hpp+12-1
......@@ -11,6 +11,15 @@
1111#include <stdint.h>
1212#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
1423struct BigInt {
1524 size_t digit_count;
1625 union {
......@@ -30,7 +39,7 @@ enum Cmp {
3039};
3140
3241void 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);
3443void bigint_init_signed(BigInt *dest, int64_t x);
3544void bigint_init_bigint(BigInt *dest, const BigInt *src);
3645void bigint_init_bigfloat(BigInt *dest, const BigFloat *op);
......@@ -89,4 +98,6 @@ size_t bigint_bits_needed(const BigInt *op);
8998// convenience functions
9099Cmp bigint_cmp_zero(const BigInt *op);
91100
101bool mul_u64_overflow(uint64_t op1, uint64_t op2, uint64_t *result);
102
92103#endif
src/buffer.hpp+2-2
......@@ -24,7 +24,7 @@ struct Buf {
2424};
2525
2626Buf *buf_sprintf(const char *format, ...)
27 __attribute__ ((format (printf, 1, 2)));
27 ATTRIBUTE_FORMAT(printf, 1, 2);
2828Buf *buf_vprintf(const char *format, va_list ap);
2929
3030static inline size_t buf_len(Buf *buf) {
......@@ -124,7 +124,7 @@ static inline void buf_append_char(Buf *buf, uint8_t c) {
124124}
125125
126126void buf_appendf(Buf *buf, const char *format, ...)
127 __attribute__ ((format (printf, 2, 3)));
127 ATTRIBUTE_FORMAT(printf, 2, 3);
128128
129129static inline bool buf_eql_mem(Buf *buf, const char *mem, size_t mem_len) {
130130 assert(buf->list.length);
src/codegen.cpp+14-1
......@@ -5194,7 +5194,19 @@ void codegen_add_object(CodeGen *g, Buf *object_path) {
51945194 g->link_objects.append(object_path);
51955195}
51965196
5197
5197#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
51985210static const char *c_int_type_names[] = {
51995211 [CIntTypeShort] = "short",
52005212 [CIntTypeUShort] = "unsigned short",
......@@ -5205,6 +5217,7 @@ static const char *c_int_type_names[] = {
52055217 [CIntTypeLongLong] = "long long",
52065218 [CIntTypeULongLong] = "unsigned long long",
52075219};
5220#endif
52085221
52095222static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
52105223 assert(type_entry);
src/ir.cpp+3-4
......@@ -6500,9 +6500,9 @@ static void float_init_bigint(BigInt *bigint, ConstExprValue *const_val) {
65006500 break;
65016501 case 128:
65026502 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));
65046504 } 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));
65066506 bigint->is_negative = true;
65076507 }
65086508 break;
......@@ -9731,8 +9731,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
97319731 uint64_t old_array_len = array_type->data.array.len;
97329732 uint64_t new_array_len;
97339733
9734 if (__builtin_umulll_overflow((unsigned long long)old_array_len, (unsigned long long)mult_amt,
9735 (unsigned long long*)&new_array_len))
9734 if (mul_u64_overflow(old_array_len, mult_amt, &new_array_len))
97369735 {
97379736 ir_add_error(ira, &instruction->base, buf_sprintf("operation results in overflow"));
97389737 return ira->codegen->builtin_types.entry_invalid;
src/os.cpp+7-5
......@@ -25,6 +25,8 @@
2525
2626#include <windows.h>
2727#include <io.h>
28
29typedef SSIZE_T ssize_t;
2830#else
2931#define ZIG_OS_POSIX
3032
......@@ -620,7 +622,7 @@ int os_get_cwd(Buf *out_cwd) {
620622
621623bool os_stderr_tty(void) {
622624#if defined(ZIG_OS_WINDOWS)
623 return _isatty(STDERR_FILENO) != 0;
625 return _isatty(_fileno(stderr)) != 0;
624626#elif defined(ZIG_OS_POSIX)
625627 return isatty(STDERR_FILENO) != 0;
626628#else
......@@ -777,12 +779,12 @@ int os_make_path(Buf *path) {
777779
778780int os_make_dir(Buf *path) {
779781#if defined(ZIG_OS_WINDOWS)
780 if (mkdir(buf_ptr(path)) == -1) {
781 if (errno == EEXIST)
782 if (!CreateDirectory(buf_ptr(path), NULL)) {
783 if (GetLastError() == ERROR_ALREADY_EXISTS)
782784 return ErrorPathAlreadyExists;
783 if (errno == ENOENT)
785 if (GetLastError() == ERROR_PATH_NOT_FOUND)
784786 return ErrorFileNotFound;
785 if (errno == EACCES)
787 if (GetLastError() == ERROR_ACCESS_DENIED)
786788 return ErrorAccess;
787789 return ErrorUnexpected;
788790 }
src/parsec.cpp+1-1
......@@ -56,7 +56,7 @@ static AstNode *resolve_enum_decl(Context *c, const EnumDecl *enum_decl);
5656static AstNode *resolve_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl);
5757
5858
59__attribute__ ((format (printf, 3, 4)))
59ATTRIBUTE_FORMAT(printf, 3, 4)
6060static void emit_warning(Context *c, const SourceLocation &sl, const char *format, ...) {
6161 if (!c->warnings_on) {
6262 return;
src/parser.cpp+5-5
......@@ -24,8 +24,8 @@ struct ParseContext {
2424 Buf *void_buf;
2525};
2626
27__attribute__ ((format (printf, 4, 5)))
28__attribute__ ((noreturn))
27ATTRIBUTE_FORMAT(printf, 4, 5)
28LLVM_ATTRIBUTE_NORETURN
2929static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const char *format, ...) {
3030 assert(node->type == NodeTypeAsmExpr);
3131
......@@ -46,8 +46,8 @@ static void ast_asm_error(ParseContext *pc, AstNode *node, size_t offset, const
4646 exit(EXIT_FAILURE);
4747}
4848
49__attribute__ ((format (printf, 3, 4)))
50__attribute__ ((noreturn))
49ATTRIBUTE_FORMAT(printf, 3, 4)
50LLVM_ATTRIBUTE_NORETURN
5151static void ast_error(ParseContext *pc, Token *token, const char *format, ...) {
5252 va_list ap;
5353 va_start(ap, format);
......@@ -205,7 +205,7 @@ static void ast_buf_from_token(ParseContext *pc, Token *token, Buf *buf) {
205205 }
206206}
207207
208__attribute__ ((noreturn))
208LLVM_ATTRIBUTE_NORETURN
209209static void ast_invalid_token_error(ParseContext *pc, Token *token) {
210210 Buf token_value = BUF_INIT;
211211 ast_buf_from_token(pc, token, &token_value);
src/parser.hpp+1-1
......@@ -12,7 +12,7 @@
1212#include "tokenizer.hpp"
1313#include "errmsg.hpp"
1414
15__attribute__ ((format (printf, 2, 3)))
15ATTRIBUTE_FORMAT(printf, 2, 3)
1616void ast_token_error(Token *token, const char *format, ...);
1717
1818
src/quadmath.hpp+34
......@@ -8,12 +8,46 @@
88#ifndef ZIG_QUADMATH_HPP
99#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
1118extern "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
1245 __float128 fmodq(__float128 a, __float128 b);
1346 __float128 ceilq(__float128 a);
1447 __float128 floorq(__float128 a);
1548 __float128 strtoflt128 (const char *s, char **sp);
1649 int quadmath_snprintf (char *s, size_t size, const char *format, ...);
50#endif
1751}
1852
1953#endif
src/tokenizer.cpp+2-2
......@@ -234,7 +234,7 @@ struct Tokenize {
234234 BigInt significand;
235235};
236236
237__attribute__ ((format (printf, 2, 3)))
237ATTRIBUTE_FORMAT(printf, 2, 3)
238238static void tokenize_error(Tokenize *t, const char *format, ...) {
239239 t->state = TokenizeStateError;
240240
......@@ -331,7 +331,7 @@ static void end_float_token(Tokenize *t) {
331331 if (t->radix == 10) {
332332 zig_panic("TODO: decimal floats");
333333 } else {
334 int significand_magnitude_in_bin = __builtin_clzll(1) - __builtin_clzll(significand);
334 int significand_magnitude_in_bin = clzll(1) - clzll(significand);
335335 t->exponent_in_bin_or_dec += significand_magnitude_in_bin;
336336 if (!(-1022 <= t->exponent_in_bin_or_dec && t->exponent_in_bin_or_dec <= 1023)) {
337337 t->cur_tok->data.float_lit.overflow = true;
src/util.hpp+36-8
......@@ -12,24 +12,52 @@
1212#include <stdint.h>
1313#include <string.h>
1414#include <assert.h>
15#include <llvm/Support/Compiler.h>
1516
1617#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
1846#define BREAKPOINT __asm("int $0x03")
1947
20void zig_panic(const char *format, ...)
21 __attribute__((cold))
22 __attribute__ ((noreturn))
23 __attribute__ ((format (printf, 1, 2)));
48LLVM_ATTRIBUTE_NOINLINE
49ATTRIBUTE_COLD
50ATTRIBUTE_FORMAT(printf, 1, 2)
51void zig_panic(const char *format, ...);
2452
25__attribute__((cold))
26__attribute__ ((noreturn))
53ATTRIBUTE_COLD
54LLVM_ATTRIBUTE_NOINLINE
2755static inline void zig_unreachable(void) {
2856 zig_panic("unreachable");
2957}
3058
3159template<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) {
3361 T *ptr = reinterpret_cast<T*>(malloc(count * sizeof(T)));
3462 if (!ptr)
3563 zig_panic("allocation failed");
......@@ -37,7 +65,7 @@ __attribute__((malloc)) static inline T *allocate_nonzero(size_t count) {
3765}
3866
3967template<typename T>
40__attribute__((malloc)) static inline T *allocate(size_t count) {
68LLVM_ATTRIBUTE_RETURNS_NOALIAS static inline T *allocate(size_t count) {
4169 T *ptr = reinterpret_cast<T*>(calloc(count, sizeof(T)));
4270 if (!ptr)
4371 zig_panic("allocation failed");