Skip to content

Commit e50789f

Browse files
authored
Merge pull request #13389 from jacobly0/fix-only-c
cbe: enough fixes for `-Donly-c` to be able to produce an executable
2 parents 57dbeb9 + 37c104a commit e50789f

File tree

18 files changed

+332
-184
lines changed

18 files changed

+332
-184
lines changed

build.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn build(b: *Builder) !void {
1717
b.setPreferredReleaseMode(.ReleaseFast);
1818
const test_step = b.step("test", "Run all the tests");
1919
const mode = b.standardReleaseOptions();
20-
const target = b.standardTargetOptions(.{});
20+
var target = b.standardTargetOptions(.{});
2121
const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
2222
const use_zig_libcxx = b.option(bool, "use-zig-libcxx", "If libc++ is needed, use zig's bundled version, don't try to integrate with the system") orelse false;
2323

@@ -141,6 +141,10 @@ pub fn build(b: *Builder) !void {
141141
break :blk 4;
142142
};
143143

144+
if (only_c) {
145+
target.ofmt = .c;
146+
}
147+
144148
const main_file: ?[]const u8 = mf: {
145149
if (!have_stage1) break :mf "src/main.zig";
146150
if (use_zig0) break :mf null;
@@ -172,10 +176,6 @@ pub fn build(b: *Builder) !void {
172176
test_cases.want_lto = false;
173177
}
174178

175-
if (only_c) {
176-
exe.ofmt = .c;
177-
}
178-
179179
const exe_options = b.addOptions();
180180
exe.addOptions("build_options", exe_options);
181181

lib/include/zig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#endif
2020

2121
#if __STDC_VERSION__ >= 201112L
22-
#define zig_threadlocal thread_local
22+
#define zig_threadlocal _Thread_local
2323
#elif defined(__GNUC__)
2424
#define zig_threadlocal __thread
2525
#elif _MSC_VER

lib/std/build.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,7 +1622,6 @@ pub const LibExeObjStep = struct {
16221622
use_stage1: ?bool = null,
16231623
use_llvm: ?bool = null,
16241624
use_lld: ?bool = null,
1625-
ofmt: ?std.Target.ObjectFormat = null,
16261625

16271626
output_path_source: GeneratedFile,
16281627
output_lib_path_source: GeneratedFile,
@@ -2490,7 +2489,7 @@ pub const LibExeObjStep = struct {
24902489
}
24912490
}
24922491

2493-
if (self.ofmt) |ofmt| {
2492+
if (self.target.ofmt) |ofmt| {
24942493
try zig_args.append(try std.fmt.allocPrint(builder.allocator, "-ofmt={s}", .{@tagName(ofmt)}));
24952494
}
24962495

lib/std/crypto/blake3.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,10 @@ const CompressGeneric = struct {
200200
}
201201
};
202202

203-
const compress = if (builtin.cpu.arch == .x86_64) CompressVectorized.compress else CompressGeneric.compress;
203+
const compress = if (builtin.cpu.arch == .x86_64 and builtin.zig_backend != .stage2_c)
204+
CompressVectorized.compress
205+
else
206+
CompressGeneric.compress;
204207

205208
fn first8Words(words: [16]u32) [8]u32 {
206209
return @ptrCast(*const [8]u32, &words).*;

lib/std/crypto/gimli.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ pub const State = struct {
152152
self.endianSwap();
153153
}
154154

155-
pub const permute = if (builtin.cpu.arch == .x86_64) impl: {
155+
pub const permute = if (builtin.cpu.arch == .x86_64 and builtin.zig_backend != .stage2_c) impl: {
156156
break :impl permute_vectorized;
157157
} else if (builtin.mode == .ReleaseSmall) impl: {
158158
break :impl permute_small;

lib/std/multi_array_list.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -436,9 +436,15 @@ pub fn MultiArrayList(comptime S: type) type {
436436
}
437437

438438
fn capacityInBytes(capacity: usize) usize {
439-
const sizes_vector: @Vector(sizes.bytes.len, usize) = sizes.bytes;
440-
const capacity_vector = @splat(sizes.bytes.len, capacity);
441-
return @reduce(.Add, capacity_vector * sizes_vector);
439+
if (builtin.zig_backend == .stage2_c) {
440+
var bytes: usize = 0;
441+
for (sizes.bytes) |size| bytes += size * capacity;
442+
return bytes;
443+
} else {
444+
const sizes_vector: @Vector(sizes.bytes.len, usize) = sizes.bytes;
445+
const capacity_vector = @splat(sizes.bytes.len, capacity);
446+
return @reduce(.Add, capacity_vector * sizes_vector);
447+
}
442448
}
443449

444450
fn allocatedBytes(self: Self) []align(@alignOf(S)) u8 {

lib/std/target.zig

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std.zig");
2+
const builtin = @import("builtin");
23
const mem = std.mem;
34
const Version = std.builtin.Version;
45

@@ -719,7 +720,11 @@ pub const Target = struct {
719720

720721
/// Adds the specified feature set but not its dependencies.
721722
pub fn addFeatureSet(set: *Set, other_set: Set) void {
722-
set.ints = @as(@Vector(usize_count, usize), set.ints) | @as(@Vector(usize_count, usize), other_set.ints);
723+
if (builtin.zig_backend == .stage2_c) {
724+
for (set.ints) |*int, i| int.* |= other_set.ints[i];
725+
} else {
726+
set.ints = @as(@Vector(usize_count, usize), set.ints) | @as(@Vector(usize_count, usize), other_set.ints);
727+
}
723728
}
724729

725730
/// Removes the specified feature but not its dependents.
@@ -731,7 +736,11 @@ pub const Target = struct {
731736

732737
/// Removes the specified feature but not its dependents.
733738
pub fn removeFeatureSet(set: *Set, other_set: Set) void {
734-
set.ints = @as(@Vector(usize_count, usize), set.ints) & ~@as(@Vector(usize_count, usize), other_set.ints);
739+
if (builtin.zig_backend == .stage2_c) {
740+
for (set.ints) |*int, i| int.* &= ~other_set.ints[i];
741+
} else {
742+
set.ints = @as(@Vector(usize_count, usize), set.ints) & ~@as(@Vector(usize_count, usize), other_set.ints);
743+
}
735744
}
736745

737746
pub fn populateDependencies(set: *Set, all_features_list: []const Cpu.Feature) void {

lib/std/zig/system/x86.zig

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -528,35 +528,29 @@ const CpuidLeaf = packed struct {
528528
};
529529

530530
fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf {
531-
// Workaround for https://github.com/ziglang/zig/issues/215
532-
// Inline assembly in zig only supports one output,
533-
// so we pass a pointer to the struct.
534-
var cpuid_leaf: CpuidLeaf = undefined;
535-
536531
// valid for both x86 and x86_64
537-
asm volatile (
538-
\\ cpuid
539-
\\ movl %%eax, 0(%[leaf_ptr])
540-
\\ movl %%ebx, 4(%[leaf_ptr])
541-
\\ movl %%ecx, 8(%[leaf_ptr])
542-
\\ movl %%edx, 12(%[leaf_ptr])
543-
:
544-
: [leaf_id] "{eax}" (leaf_id),
545-
[subid] "{ecx}" (subid),
546-
[leaf_ptr] "r" (&cpuid_leaf),
547-
: "eax", "ebx", "ecx", "edx"
532+
var eax: u32 = undefined;
533+
var ebx: u32 = undefined;
534+
var ecx: u32 = undefined;
535+
var edx: u32 = undefined;
536+
asm volatile ("cpuid"
537+
: [_] "={eax}" (eax),
538+
[_] "={ebx}" (ebx),
539+
[_] "={ecx}" (ecx),
540+
[_] "={edx}" (edx),
541+
: [_] "{eax}" (leaf_id),
542+
[_] "{ecx}" (subid),
548543
);
549-
550-
return cpuid_leaf;
544+
return .{ .eax = eax, .ebx = ebx, .ecx = ecx, .edx = edx };
551545
}
552546

553547
// Read control register 0 (XCR0). Used to detect features such as AVX.
554548
fn getXCR0() u32 {
555549
return asm volatile (
556550
\\ xor %%ecx, %%ecx
557551
\\ xgetbv
558-
: [ret] "={eax}" (-> u32),
552+
: [_] "={eax}" (-> u32),
559553
:
560-
: "eax", "edx", "ecx"
554+
: "edx", "ecx"
561555
);
562556
}

0 commit comments

Comments
 (0)