authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2022-12-08 19:50:08-05:00
committergravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-01-01 16:44:27-05:00
loga43fdc1620fa24c8c606f748505766bfd53d1049
treea421b5989d7ec14361f16a5790faf946d21a3bb9
parent4172c2916684655a9742de99384be8110922ed07

cbe: first set of changes for msvc compatibility

- Forward declare int builtins, so the definitions aren't assumed incorrectly - Add define to handle MSVC not support static const in function parameter array lengths - Fixup several spots where int128 support was assumed. - Support zig_align - Support zig_export - Stub out some missing non-builtin functions - Added StringLiteral to automatically split string literals when they get to 16380 in size, which is the maxmimum pre-concatenation string literal size on MSVC.

2 files changed, 151 insertions(+), 57 deletions(-)

lib/zig.h+38-18
......@@ -38,6 +38,12 @@ typedef char bool;
3838#define zig_threadlocal zig_threadlocal_unavailable
3939#endif
4040
41#if _MSC_VER
42#define zig_const_arr
43#else
44#define zig_const_arr static const
45#endif
46
4147#if zig_has_attribute(naked) || defined(__GNUC__)
4248#define zig_naked __attribute__((naked))
4349#elif defined(_MSC_VER)
......@@ -65,7 +71,7 @@ typedef char bool;
6571#elif zig_has_attribute(aligned)
6672#define zig_align(alignment) __attribute__((aligned(alignment)))
6773#elif _MSC_VER
68#define zig_align zig_align_unavailable
74#define zig_align(alignment) __declspec(align(alignment))
6975#else
7076#define zig_align zig_align_unavailable
7177#endif
......@@ -73,7 +79,8 @@ typedef char bool;
7379#if zig_has_attribute(aligned)
7480#define zig_align_fn(alignment) __attribute__((aligned(alignment)))
7581#elif _MSC_VER
76#define zig_align_fn zig_align_fn_unavailable
82// TODO: Figure out how to do this under MSVC
83#define zig_align_fn(alignment)
7784#else
7885#define zig_align_fn zig_align_fn_unavailable
7986#endif
......@@ -92,6 +99,9 @@ typedef char bool;
9299
93100#if zig_has_attribute(alias)
94101#define zig_export(sig, symbol, name) zig_extern sig __attribute__((alias(symbol)))
102#elif _MSC_VER
103#define zig_export(sig, symbol, name) sig;\
104 __pragma(comment(linker, "/alternatename:" name "=" symbol ))
95105#else
96106#define zig_export(sig, symbol, name) __asm(name " = " symbol)
97107#endif
......@@ -1327,13 +1337,15 @@ static inline zig_i128 zig_sub_i128(zig_i128 lhs, zig_i128 rhs) {
13271337 return res;
13281338}
13291339
1330static inline zig_i128 zig_div_floor_i128(zig_i128 lhs, zig_i128 rhs) {
1331 return zig_sub_i128(zig_div_trunc_i128(lhs, rhs), (((lhs.hi ^ rhs.hi) & zig_rem_i128(lhs, rhs).hi) < zig_as_i64(0)) ? zig_as_i128(0, 1) : zig_as_i128(0, 0));
1332}
1340// TODO: Implement
1341static zig_i128 zig_div_trunc_i128(zig_i128 lhs, zig_i128 rhs);
1342
1343// TODO: Implement
1344static zig_i128 zig_rem_i128(zig_i128 lhs, zig_i128 rhs);
13331345
13341346static inline zig_i128 zig_mod_i128(zig_i128 lhs, zig_i128 rhs) {
13351347 zig_i128 rem = zig_rem_i128(lhs, rhs);
1336 return rem + (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0));
1348 return zig_add_i128(rem, (((lhs.hi ^ rhs.hi) & rem.hi) < zig_as_i64(0) ? rhs : zig_as_i128(0, 0)));
13371349}
13381350
13391351#endif /* zig_has_int128 */
......@@ -1358,7 +1370,7 @@ static inline zig_i128 zig_max_i128(zig_i128 lhs, zig_i128 rhs) {
13581370}
13591371
13601372static inline zig_i128 zig_shr_i128(zig_i128 lhs, zig_u8 rhs) {
1361 zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i32(0) ? -zig_as_i128(0, 1) : zig_as_i128(0, 0);
1373 zig_i128 sign_mask = zig_cmp_i128(lhs, zig_as_i128(0, 0)) < zig_as_i32(0) ? zig_sub_i128(zig_as_i128(0, 0), zig_as_i128(0, 1)) : zig_as_i128(0, 0);
13621374 return zig_xor_i128(zig_bitcast_i128(zig_shr_u128(zig_bitcast_u128(zig_xor_i128(lhs, sign_mask)), rhs)), sign_mask);
13631375}
13641376
......@@ -1375,7 +1387,7 @@ static inline zig_u128 zig_shlw_u128(zig_u128 lhs, zig_u8 rhs, zig_u8 bits) {
13751387}
13761388
13771389static inline zig_i128 zig_shlw_i128(zig_i128 lhs, zig_u8 rhs, zig_u8 bits) {
1378 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
1390 return zig_wrap_i128(zig_bitcast_i128(zig_shl_u128(zig_bitcast_u128(lhs), rhs)), bits);
13791391}
13801392
13811393static inline zig_u128 zig_addw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
......@@ -1394,6 +1406,9 @@ static inline zig_i128 zig_subw_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
13941406 return zig_wrap_i128(zig_bitcast_i128(zig_sub_u128(zig_bitcast_u128(lhs), zig_bitcast_u128(rhs))), bits);
13951407}
13961408
1409// TODO: Implement
1410static zig_u128 zig_mul_u128(zig_u128 lhs, zig_u128 rhs);
1411
13971412static inline zig_u128 zig_mulw_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
13981413 return zig_wrap_u128(zig_mul_u128(lhs, rhs), bits);
13991414}
......@@ -1496,15 +1511,15 @@ static inline bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_
14961511
14971512#else /* zig_has_int128 */
14981513
1499static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) {
1500 return zig_addo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) |
1501 zig_addo_u64(&res->hi, res->hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX));
1502}
1514/* static inline bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) { */
1515/* return zig_addo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) | */
1516/* zig_addo_u64(&res->hi, res->hi, zig_addo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX)); */
1517/* } */
15031518
1504static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) {
1505 return zig_subo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) |
1506 zig_subo_u64(&res->hi, res->hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX));
1507}
1519/* static inline bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs) { */
1520/* return zig_subo_u64(&res->hi, lhs.hi, rhs.hi, UINT64_MAX) | */
1521/* zig_subo_u64(&res->hi, res->hi, zig_subo_u64(&res->lo, lhs.lo, rhs.lo, UINT64_MAX)); */
1522/* } */
15081523
15091524#endif /* zig_has_int128 */
15101525
......@@ -1512,7 +1527,12 @@ static inline zig_u128 zig_shls_u128(zig_u128 lhs, zig_u128 rhs, zig_u8 bits) {
15121527 zig_u128 res;
15131528 if (zig_cmp_u128(rhs, zig_as_u128(0, bits)) >= zig_as_i32(0))
15141529 return zig_cmp_u128(lhs, zig_as_u128(0, 0)) != zig_as_i32(0) ? zig_maxInt(u128, bits) : lhs;
1530
1531#if zig_has_int128
15151532 return zig_shlo_u128(&res, lhs, (zig_u8)rhs, bits) ? zig_maxInt(u128, bits) : res;
1533#else
1534 return zig_shlo_u128(&res, lhs, (zig_u8)rhs.lo, bits) ? zig_maxInt(u128, bits) : res;
1535#endif
15161536}
15171537
15181538static inline zig_i128 zig_shls_i128(zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
......@@ -1593,7 +1613,7 @@ static inline zig_u128 zig_byte_swap_u128(zig_u128 val, zig_u8 bits) {
15931613}
15941614
15951615static inline zig_i128 zig_byte_swap_i128(zig_i128 val, zig_u8 bits) {
1596 return zig_byte_swap_u128(zig_bitcast_u128(val), bits);
1616 return zig_bitcast_i128(zig_byte_swap_u128(zig_bitcast_u128(val), bits));
15971617}
15981618
15991619static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) {
......@@ -1603,7 +1623,7 @@ static inline zig_u128 zig_bit_reverse_u128(zig_u128 val, zig_u8 bits) {
16031623}
16041624
16051625static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, zig_u8 bits) {
1606 return zig_bit_reverse_u128(zig_bitcast_u128(val), bits);
1626 return zig_bitcast_i128(zig_bit_reverse_u128(zig_bitcast_u128(val), bits));
16071627}
16081628
16091629/* ========================= Floating Point Support ========================= */
src/codegen/c.zig+113-39
......@@ -848,12 +848,13 @@ pub const DeclGen = struct {
848848
849849 const ai = ty.arrayInfo();
850850 if (ai.elem_type.eql(Type.u8, dg.module)) {
851 try writer.writeByte('"');
851 var literal = stringLiteral(writer);
852 try literal.start();
852853 const c_len = ty.arrayLenIncludingSentinel();
853854 var index: usize = 0;
854855 while (index < c_len) : (index += 1)
855 try writeStringLiteralChar(writer, 0xaa);
856 return writer.writeByte('"');
856 try literal.writeChar(0xaa);
857 return literal.end();
857858 } else {
858859 try writer.writeByte('{');
859860 const c_len = ty.arrayLenIncludingSentinel();
......@@ -1060,23 +1061,40 @@ pub const DeclGen = struct {
10601061 defer arena.deinit();
10611062 const arena_allocator = arena.allocator();
10621063
1064 // MSVC throws C2078 if an array of size 65536 or greater is initialized with a string literal
1065 const max_string_initializer_len = 65535;
1066
10631067 const ai = ty.arrayInfo();
10641068 if (ai.elem_type.eql(Type.u8, dg.module)) {
1065 try writer.writeByte('"');
1066 var index: usize = 0;
1067 while (index < ai.len) : (index += 1) {
1068 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
1069 const elem_val_u8 = if (elem_val.isUndef())
1070 undefPattern(u8)
1071 else
1072 @intCast(u8, elem_val.toUnsignedInt(target));
1073 try writeStringLiteralChar(writer, elem_val_u8);
1074 }
1075 if (ai.sentinel) |s| {
1076 const s_u8 = @intCast(u8, s.toUnsignedInt(target));
1077 try writeStringLiteralChar(writer, s_u8);
1069 if (ai.len <= max_string_initializer_len) {
1070 var literal = stringLiteral(writer);
1071 try literal.start();
1072 var index: usize = 0;
1073 while (index < ai.len) : (index += 1) {
1074 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
1075 const elem_val_u8 = if (elem_val.isUndef()) undefPattern(u8) else @intCast(u8, elem_val.toUnsignedInt(target));
1076 try literal.writeChar(elem_val_u8);
1077 }
1078 if (ai.sentinel) |s| {
1079 const s_u8 = @intCast(u8, s.toUnsignedInt(target));
1080 try literal.writeChar(s_u8);
1081 }
1082 try literal.end();
1083 } else {
1084 try writer.writeByte('{');
1085 var index: usize = 0;
1086 while (index < ai.len) : (index += 1) {
1087 if (index != 0) try writer.writeByte(',');
1088 const elem_val = try val.elemValue(dg.module, arena_allocator, index);
1089 const elem_val_u8 = if (elem_val.isUndef()) undefPattern(u8) else @intCast(u8, elem_val.toUnsignedInt(target));
1090 try writer.print("'\\x{x}'", .{ elem_val_u8 });
1091 }
1092 if (ai.sentinel) |s| {
1093 if (index != 0) try writer.writeByte(',');
1094 try dg.renderValue(writer, ai.elem_type, s, .Initializer);
1095 }
1096 try writer.writeByte('}');
10781097 }
1079 try writer.writeByte('"');
10801098 } else {
10811099 try writer.writeByte('{');
10821100 var index: usize = 0;
......@@ -2134,7 +2152,7 @@ pub const DeclGen = struct {
21342152 const c_len_val = Value.initPayload(&c_len_pl.base);
21352153
21362154 try suffix_writer.writeByte('[');
2137 if (mutability == .ConstArgument and depth == 0) try suffix_writer.writeAll("static const ");
2155 if (mutability == .ConstArgument and depth == 0) try suffix_writer.writeAll("zig_const_arr ");
21382156 try suffix.writer().print("{}]", .{try dg.fmtIntLiteral(Type.usize, c_len_val)});
21392157 render_ty = array_info.elem_type;
21402158 depth += 1;
......@@ -6793,6 +6811,68 @@ fn compilerRtAbbrev(ty: Type, target: std.Target) []const u8 {
67936811 } else unreachable;
67946812}
67956813
6814fn StringLiteral(comptime WriterType: type) type {
6815 // msvc has a length limit of 16380 per string literal (before concatenation)
6816 const max_char_len = 4;
6817 const max_len = 16380 - max_char_len;
6818
6819 return struct {
6820 cur_len: usize = 0,
6821 counting_writer: std.io.CountingWriter(WriterType),
6822
6823 pub const Error = WriterType.Error;
6824
6825 const Self = @This();
6826
6827 pub fn start(self: *Self) Error!void {
6828 const writer = self.counting_writer.writer();
6829 try writer.writeByte('\"');
6830 }
6831
6832 pub fn end(self: *Self) Error!void {
6833 const writer = self.counting_writer.writer();
6834 try writer.writeByte('\"');
6835 }
6836
6837 fn writeStringLiteralChar(writer: anytype, c: u8) !void {
6838 switch (c) {
6839 7 => try writer.writeAll("\\a"),
6840 8 => try writer.writeAll("\\b"),
6841 '\t' => try writer.writeAll("\\t"),
6842 '\n' => try writer.writeAll("\\n"),
6843 11 => try writer.writeAll("\\v"),
6844 12 => try writer.writeAll("\\f"),
6845 '\r' => try writer.writeAll("\\r"),
6846 '"', '\'', '?', '\\' => try writer.print("\\{c}", .{c}),
6847 else => switch (c) {
6848 ' '...'~' => try writer.writeByte(c),
6849 else => try writer.print("\\{o:0>3}", .{c}),
6850 },
6851 }
6852 }
6853
6854 pub fn writeChar(self: *Self, c: u8) Error!void {
6855 const writer = self.counting_writer.writer();
6856
6857 if (self.cur_len == 0 and self.counting_writer.bytes_written > 1)
6858 try writer.writeAll("\"\"");
6859
6860 const len = self.counting_writer.bytes_written;
6861 try writeStringLiteralChar(writer, c);
6862
6863 const char_length = self.counting_writer.bytes_written - len;
6864 assert(char_length <= max_char_len);
6865 self.cur_len += char_length;
6866
6867 if (self.cur_len >= max_len) self.cur_len = 0;
6868 }
6869 };
6870}
6871
6872fn stringLiteral(child_stream: anytype) StringLiteral(@TypeOf(child_stream)) {
6873 return .{ .counting_writer = std.io.countingWriter(child_stream) };
6874}
6875
67966876fn formatStringLiteral(
67976877 str: []const u8,
67986878 comptime fmt: []const u8,
......@@ -6800,33 +6880,18 @@ fn formatStringLiteral(
68006880 writer: anytype,
68016881) @TypeOf(writer).Error!void {
68026882 if (fmt.len != 1 or fmt[0] != 's') @compileError("Invalid fmt: " ++ fmt);
6803 try writer.writeByte('\"');
6883
6884 var literal = stringLiteral(writer);
6885 try literal.start();
68046886 for (str) |c|
6805 try writeStringLiteralChar(writer, c);
6806 try writer.writeByte('\"');
6887 try literal.writeChar(c);
6888 try literal.end();
68076889}
68086890
68096891fn fmtStringLiteral(str: []const u8) std.fmt.Formatter(formatStringLiteral) {
68106892 return .{ .data = str };
68116893}
68126894
6813fn writeStringLiteralChar(writer: anytype, c: u8) !void {
6814 switch (c) {
6815 7 => try writer.writeAll("\\a"),
6816 8 => try writer.writeAll("\\b"),
6817 '\t' => try writer.writeAll("\\t"),
6818 '\n' => try writer.writeAll("\\n"),
6819 11 => try writer.writeAll("\\v"),
6820 12 => try writer.writeAll("\\f"),
6821 '\r' => try writer.writeAll("\\r"),
6822 '"', '\'', '?', '\\' => try writer.print("\\{c}", .{c}),
6823 else => switch (c) {
6824 ' '...'~' => try writer.writeByte(c),
6825 else => try writer.print("\\{o:0>3}", .{c}),
6826 },
6827 }
6828}
6829
68306895fn undefPattern(comptime IntType: type) IntType {
68316896 const int_info = @typeInfo(IntType).Int;
68326897 const UnsignedType = std.meta.Int(.unsigned, int_info.bits);
......@@ -6905,7 +6970,15 @@ fn formatIntLiteral(
69056970 return writer.print("{s}_{s}", .{ abbrev, if (int.positive) "MAX" else "MIN" });
69066971 }
69076972
6908 if (!int.positive) try writer.writeByte('-');
6973 // TODO: If > 64 bit, need to use a subtract from zero fn here instead of negate
6974 if (!int.positive) {
6975 if (c_bits > 64) {
6976 try writer.print("zig_sub_{c}{d}(zig_as_{c}{d}(0, 0), ", .{ signAbbrev(int_info.signedness), c_bits, signAbbrev(int_info.signedness), c_bits });
6977 } else {
6978 try writer.writeByte('-');
6979 }
6980 }
6981
69096982 switch (data.ty.tag()) {
69106983 .c_short, .c_ushort, .c_int, .c_uint, .c_long, .c_ulong, .c_longlong, .c_ulonglong => {},
69116984 else => try writer.print("zig_as_{c}{d}(", .{ signAbbrev(int_info.signedness), c_bits }),
......@@ -6976,6 +7049,7 @@ fn formatIntLiteral(
69767049 .mod = data.mod,
69777050 }, fmt, options, writer);
69787051
7052 if (!int.positive and c_bits > 64) try writer.writeByte(')');
69797053 return writer.writeByte(')');
69807054 }
69817055