authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-03 01:48:13-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-05 02:59:01-05:00
loge96a0fd0a1a05fe8c3b4d87df03d78ae99b7dbcb
treedc0711cb982c31ac4b385ddfd14d84e1882c666b
parent93d696e84ef17a32d5c2f1520a295ebcda968e91

CBE: "compute" max int alignment the lazy way


2 files changed, 15 insertions(+), 9 deletions(-)

lib/zig.h+1-1
...@@ -1921,7 +1921,7 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) {...@@ -1921,7 +1921,7 @@ static inline zig_i128 zig_bit_reverse_i128(zig_i128 val, uint8_t bits) {
19211921
1922static inline uint16_t zig_int_bytes(uint16_t bits) {1922static inline uint16_t zig_int_bytes(uint16_t bits) {
1923 uint16_t bytes = (bits + CHAR_BIT - 1) / CHAR_BIT;1923 uint16_t bytes = (bits + CHAR_BIT - 1) / CHAR_BIT;
1924 uint16_t alignment = 16;1924 uint16_t alignment = ZIG_TARGET_MAX_INT_ALIGNMENT;
1925 while (alignment / 2 >= bytes) alignment /= 2;1925 while (alignment / 2 >= bytes) alignment /= 2;
1926 return (bytes + alignment - 1) / alignment * alignment;1926 return (bytes + alignment - 1) / alignment * alignment;
1927}1927}
src/link/C.zig+14-8
...@@ -221,14 +221,19 @@ pub fn flush(self: *C, comp: *Compilation, prog_node: *std.Progress.Node) !void...@@ -221,14 +221,19 @@ pub fn flush(self: *C, comp: *Compilation, prog_node: *std.Progress.Node) !void
221 return self.flushModule(comp, prog_node);221 return self.flushModule(comp, prog_node);
222}222}
223223
224fn abiDefine(comp: *Compilation) ?[]const u8 {224fn abiDefines(self: *C, target: std.Target) !std.ArrayList(u8) {
225 return switch (comp.getTarget().abi) {225 var defines = std.ArrayList(u8).init(self.base.allocator);
226 .msvc => "#define ZIG_TARGET_ABI_MSVC\n",226 errdefer defines.deinit();
227 else => null,227 const writer = defines.writer();
228 };228 switch (target.abi) {
229 .msvc => try writer.writeAll("#define ZIG_TARGET_ABI_MSVC\n"),
230 else => {},
231 }
232 try writer.print("#define ZIG_TARGET_MAX_INT_ALIGNMENT {d}\n", .{target.maxIntAlignment()});
233 return defines;
229}234}
230235
231pub fn flushModule(self: *C, comp: *Compilation, prog_node: *std.Progress.Node) !void {236pub fn flushModule(self: *C, _: *Compilation, prog_node: *std.Progress.Node) !void {
232 const tracy = trace(@src());237 const tracy = trace(@src());
233 defer tracy.end();238 defer tracy.end();
234239
...@@ -245,12 +250,13 @@ pub fn flushModule(self: *C, comp: *Compilation, prog_node: *std.Progress.Node)...@@ -245,12 +250,13 @@ pub fn flushModule(self: *C, comp: *Compilation, prog_node: *std.Progress.Node)
245 var f: Flush = .{};250 var f: Flush = .{};
246 defer f.deinit(gpa);251 defer f.deinit(gpa);
247252
248 const abi_define = abiDefine(comp);253 const abi_defines = try self.abiDefines(module.getTarget());
254 defer abi_defines.deinit();
249255
250 // Covers defines, zig.h, ctypes, asm, lazy fwd.256 // Covers defines, zig.h, ctypes, asm, lazy fwd.
251 try f.all_buffers.ensureUnusedCapacity(gpa, 5);257 try f.all_buffers.ensureUnusedCapacity(gpa, 5);
252258
253 if (abi_define) |buf| f.appendBufAssumeCapacity(buf);259 f.appendBufAssumeCapacity(abi_defines.items);
254 f.appendBufAssumeCapacity(zig_h);260 f.appendBufAssumeCapacity(zig_h);
255261
256 const ctypes_index = f.all_buffers.items.len;262 const ctypes_index = f.all_buffers.items.len;