mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-05-15 20:45:56 +03:00
d1901c744c
See: https://devblogs.microsoft.com/directx/directx-adopting-spir-v Since we never hooked up the (experimental) DirectX LLVM backend, we've never actually supported targeting DXIL in Zig. With Microsoft moving away from DXIL, that seems very unlikely to change.
623 lines
18 KiB
Zig
623 lines
18 KiB
Zig
const std = @import("std");
|
|
const Type = @import("Type.zig");
|
|
const AddressSpace = std.builtin.AddressSpace;
|
|
const Alignment = @import("InternPool.zig").Alignment;
|
|
const Feature = @import("Zcu.zig").Feature;
|
|
|
|
pub const default_stack_protector_buffer_size = 4;
|
|
|
|
pub fn cannotDynamicLink(target: std.Target) bool {
|
|
return switch (target.os.tag) {
|
|
.freestanding => true,
|
|
else => target.isSpirV(),
|
|
};
|
|
}
|
|
|
|
/// On Darwin, we always link libSystem which contains libc.
|
|
/// Similarly on FreeBSD and NetBSD we always link system libc
|
|
/// since this is the stable syscall interface.
|
|
pub fn osRequiresLibC(target: std.Target) bool {
|
|
return target.os.requiresLibC();
|
|
}
|
|
|
|
pub fn libcNeedsLibUnwind(target: std.Target) bool {
|
|
return switch (target.os.tag) {
|
|
.macos,
|
|
.ios,
|
|
.watchos,
|
|
.tvos,
|
|
.visionos,
|
|
.freestanding,
|
|
.wasi, // Wasm/WASI currently doesn't offer support for libunwind, so don't link it.
|
|
=> false,
|
|
|
|
.windows => target.abi != .msvc,
|
|
else => true,
|
|
};
|
|
}
|
|
|
|
pub fn requiresPIE(target: std.Target) bool {
|
|
return target.isAndroid() or target.isDarwin() or target.os.tag == .openbsd;
|
|
}
|
|
|
|
/// This function returns whether non-pic code is completely invalid on the given target.
|
|
pub fn requiresPIC(target: std.Target, linking_libc: bool) bool {
|
|
return target.isAndroid() or
|
|
target.os.tag == .windows or target.os.tag == .uefi or
|
|
osRequiresLibC(target) or
|
|
(linking_libc and target.isGnuLibC()) or
|
|
(target.abi == .ohos and target.cpu.arch == .aarch64);
|
|
}
|
|
|
|
pub fn picLevel(target: std.Target) u32 {
|
|
// MIPS always uses PIC level 1; other platforms vary in their default PIC levels, but they
|
|
// support both level 1 and 2, in which case we prefer 2.
|
|
return if (target.cpu.arch.isMIPS()) 1 else 2;
|
|
}
|
|
|
|
/// This is not whether the target supports Position Independent Code, but whether the -fPIC
|
|
/// C compiler argument is valid to Clang.
|
|
pub fn supports_fpic(target: std.Target) bool {
|
|
return target.os.tag != .windows and target.os.tag != .uefi;
|
|
}
|
|
|
|
pub fn alwaysSingleThreaded(target: std.Target) bool {
|
|
_ = target;
|
|
return false;
|
|
}
|
|
|
|
pub fn defaultSingleThreaded(target: std.Target) bool {
|
|
switch (target.cpu.arch) {
|
|
.wasm32, .wasm64 => return true,
|
|
else => {},
|
|
}
|
|
switch (target.os.tag) {
|
|
.haiku => return true,
|
|
else => {},
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// Valgrind supports more, but Zig does not support them yet.
|
|
pub fn hasValgrindSupport(target: std.Target) bool {
|
|
switch (target.cpu.arch) {
|
|
.x86,
|
|
.x86_64,
|
|
.aarch64,
|
|
.aarch64_be,
|
|
=> {
|
|
return target.os.tag == .linux or target.os.tag == .solaris or target.os.tag == .illumos or
|
|
(target.os.tag == .windows and target.abi != .msvc);
|
|
},
|
|
else => return false,
|
|
}
|
|
}
|
|
|
|
/// The set of targets that LLVM has non-experimental support for.
|
|
/// Used to select between LLVM backend and self-hosted backend when compiling in
|
|
/// release modes.
|
|
pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool {
|
|
switch (ofmt) {
|
|
// LLVM does not support these object formats:
|
|
.c,
|
|
.plan9,
|
|
=> return false,
|
|
|
|
.coff,
|
|
.elf,
|
|
.goff,
|
|
.hex,
|
|
.macho,
|
|
.nvptx,
|
|
.spirv,
|
|
.raw,
|
|
.wasm,
|
|
.xcoff,
|
|
=> {},
|
|
}
|
|
|
|
return switch (target.cpu.arch) {
|
|
.arm,
|
|
.armeb,
|
|
.aarch64,
|
|
.aarch64_be,
|
|
.arc,
|
|
.avr,
|
|
.bpfel,
|
|
.bpfeb,
|
|
.csky,
|
|
.hexagon,
|
|
.loongarch32,
|
|
.loongarch64,
|
|
.m68k,
|
|
.mips,
|
|
.mipsel,
|
|
.mips64,
|
|
.mips64el,
|
|
.msp430,
|
|
.powerpc,
|
|
.powerpcle,
|
|
.powerpc64,
|
|
.powerpc64le,
|
|
.amdgcn,
|
|
.riscv32,
|
|
.riscv64,
|
|
.sparc,
|
|
.sparc64,
|
|
.s390x,
|
|
.thumb,
|
|
.thumbeb,
|
|
.x86,
|
|
.x86_64,
|
|
.xcore,
|
|
.xtensa,
|
|
.nvptx,
|
|
.nvptx64,
|
|
.lanai,
|
|
.wasm32,
|
|
.wasm64,
|
|
.ve,
|
|
=> true,
|
|
|
|
// An LLVM backend exists but we don't currently support using it.
|
|
.spirv,
|
|
.spirv32,
|
|
.spirv64,
|
|
=> false,
|
|
|
|
// No LLVM backend exists.
|
|
.kalimba,
|
|
.spu_2,
|
|
=> false,
|
|
};
|
|
}
|
|
|
|
/// The set of targets that Zig supports using LLD to link for.
|
|
pub fn hasLldSupport(ofmt: std.Target.ObjectFormat) bool {
|
|
return switch (ofmt) {
|
|
.elf, .coff, .wasm => true,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
/// The set of targets that our own self-hosted backends have robust support for.
|
|
/// Used to select between LLVM backend and self-hosted backend when compiling in
|
|
/// debug mode. A given target should only return true here if it is passing greater
|
|
/// than or equal to the number of behavior tests as the respective LLVM backend.
|
|
pub fn selfHostedBackendIsAsRobustAsLlvm(target: std.Target) bool {
|
|
_ = target;
|
|
return false;
|
|
}
|
|
|
|
pub fn supportsStackProbing(target: std.Target) bool {
|
|
return target.os.tag != .windows and target.os.tag != .uefi and
|
|
(target.cpu.arch == .x86 or target.cpu.arch == .x86_64);
|
|
}
|
|
|
|
pub fn supportsStackProtector(target: std.Target, backend: std.builtin.CompilerBackend) bool {
|
|
switch (target.os.tag) {
|
|
.plan9 => return false,
|
|
else => {},
|
|
}
|
|
switch (target.cpu.arch) {
|
|
.spirv, .spirv32, .spirv64 => return false,
|
|
else => {},
|
|
}
|
|
return switch (backend) {
|
|
.stage2_llvm => true,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn clangSupportsStackProtector(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.spirv, .spirv32, .spirv64 => return false,
|
|
else => true,
|
|
};
|
|
}
|
|
|
|
pub fn libcProvidesStackProtector(target: std.Target) bool {
|
|
return !target.isMinGW() and target.os.tag != .wasi and !target.isSpirV();
|
|
}
|
|
|
|
pub fn supportsReturnAddress(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.wasm32, .wasm64 => target.os.tag == .emscripten,
|
|
.bpfel, .bpfeb => false,
|
|
.spirv, .spirv32, .spirv64 => false,
|
|
else => true,
|
|
};
|
|
}
|
|
|
|
pub const CompilerRtClassification = enum { none, only_compiler_rt, only_libunwind, both };
|
|
|
|
pub fn classifyCompilerRtLibName(target: std.Target, name: []const u8) CompilerRtClassification {
|
|
if (target.abi.isGnu() and std.mem.eql(u8, name, "gcc_s")) {
|
|
// libgcc_s includes exception handling functions, so if linking this library
|
|
// is requested, zig needs to instead link libunwind. Otherwise we end up with
|
|
// the linker unable to find `_Unwind_RaiseException` and other related symbols.
|
|
return .both;
|
|
}
|
|
if (std.mem.eql(u8, name, "compiler_rt")) {
|
|
return .only_compiler_rt;
|
|
}
|
|
if (std.mem.eql(u8, name, "unwind")) {
|
|
return .only_libunwind;
|
|
}
|
|
return .none;
|
|
}
|
|
|
|
pub fn hasDebugInfo(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.nvptx, .nvptx64 => std.Target.nvptx.featureSetHas(target.cpu.features, .ptx75) or
|
|
std.Target.nvptx.featureSetHas(target.cpu.features, .ptx76) or
|
|
std.Target.nvptx.featureSetHas(target.cpu.features, .ptx77) or
|
|
std.Target.nvptx.featureSetHas(target.cpu.features, .ptx78) or
|
|
std.Target.nvptx.featureSetHas(target.cpu.features, .ptx80) or
|
|
std.Target.nvptx.featureSetHas(target.cpu.features, .ptx81),
|
|
.bpfel, .bpfeb => false,
|
|
else => true,
|
|
};
|
|
}
|
|
|
|
pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.OptimizeMode {
|
|
if (target.cpu.arch.isWasm() and target.os.tag == .freestanding) {
|
|
return .ReleaseSmall;
|
|
} else {
|
|
return .ReleaseFast;
|
|
}
|
|
}
|
|
|
|
pub fn hasRedZone(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.x86_64,
|
|
.x86,
|
|
.aarch64,
|
|
.aarch64_be,
|
|
=> true,
|
|
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn libcFullLinkFlags(target: std.Target) []const []const u8 {
|
|
// The linking order of these is significant and should match the order other
|
|
// c compilers such as gcc or clang use.
|
|
return switch (target.os.tag) {
|
|
.netbsd, .openbsd => &[_][]const u8{
|
|
"-lm",
|
|
"-lpthread",
|
|
"-lc",
|
|
"-lutil",
|
|
},
|
|
.solaris, .illumos => &[_][]const u8{
|
|
"-lm",
|
|
"-lsocket",
|
|
"-lnsl",
|
|
// Solaris releases after 10 merged the threading libraries into libc.
|
|
"-lc",
|
|
},
|
|
.haiku => &[_][]const u8{
|
|
"-lm",
|
|
"-lroot",
|
|
"-lpthread",
|
|
"-lc",
|
|
"-lnetwork",
|
|
},
|
|
else => switch (target.abi) {
|
|
.android => &[_][]const u8{
|
|
"-lm",
|
|
"-lc",
|
|
"-ldl",
|
|
},
|
|
else => &[_][]const u8{
|
|
"-lm",
|
|
"-lpthread",
|
|
"-lc",
|
|
"-ldl",
|
|
"-lrt",
|
|
"-lutil",
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
pub fn clangMightShellOutForAssembly(target: std.Target) bool {
|
|
// Clang defaults to using the system assembler over the internal one
|
|
// when targeting a non-BSD OS.
|
|
return target.cpu.arch.isSPARC();
|
|
}
|
|
|
|
/// Each backend architecture in Clang has a different codepath which may or may not
|
|
/// support an -mcpu flag.
|
|
pub fn clangAssemblerSupportsMcpuArg(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.arm, .armeb, .thumb, .thumbeb => true,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn clangSupportsFloatAbiArg(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.arm,
|
|
.armeb,
|
|
.thumb,
|
|
.thumbeb,
|
|
.csky,
|
|
.mips,
|
|
.mipsel,
|
|
.mips64,
|
|
.mips64el,
|
|
.powerpc,
|
|
.powerpcle,
|
|
.powerpc64,
|
|
.powerpc64le,
|
|
.s390x,
|
|
.sparc,
|
|
.sparc64,
|
|
=> true,
|
|
// We use the target triple for LoongArch.
|
|
.loongarch32, .loongarch64 => false,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn clangSupportsNoImplicitFloatArg(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.aarch64,
|
|
.aarch64_be,
|
|
.arm,
|
|
.armeb,
|
|
.thumb,
|
|
.thumbeb,
|
|
.riscv32,
|
|
.riscv64,
|
|
.x86,
|
|
.x86_64,
|
|
=> true,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn needUnwindTables(target: std.Target) bool {
|
|
return target.os.tag == .windows or target.isDarwin() or std.debug.Dwarf.abi.supportsUnwinding(target);
|
|
}
|
|
|
|
pub fn defaultAddressSpace(
|
|
target: std.Target,
|
|
context: enum {
|
|
/// Query the default address space for global constant values.
|
|
global_constant,
|
|
/// Query the default address space for global mutable values.
|
|
global_mutable,
|
|
/// Query the default address space for function-local values.
|
|
local,
|
|
/// Query the default address space for functions themselves.
|
|
function,
|
|
},
|
|
) AddressSpace {
|
|
// The default address space for functions on AVR is .flash to produce
|
|
// correct fixups into progmem.
|
|
if (context == .function and target.cpu.arch == .avr) return .flash;
|
|
return .generic;
|
|
}
|
|
|
|
/// Returns true if pointers in `from` can be converted to a pointer in `to`.
|
|
pub fn addrSpaceCastIsValid(
|
|
target: std.Target,
|
|
from: AddressSpace,
|
|
to: AddressSpace,
|
|
) bool {
|
|
const arch = target.cpu.arch;
|
|
switch (arch) {
|
|
.x86_64, .x86 => return arch.supportsAddressSpace(from) and arch.supportsAddressSpace(to),
|
|
.nvptx64, .nvptx, .amdgcn => {
|
|
const to_generic = arch.supportsAddressSpace(from) and to == .generic;
|
|
const from_generic = arch.supportsAddressSpace(to) and from == .generic;
|
|
return to_generic or from_generic;
|
|
},
|
|
else => return from == .generic and to == .generic,
|
|
}
|
|
}
|
|
|
|
pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
|
|
// LLD does not support ELFv1. Rather than having LLVM produce ELFv1 code and then linking it
|
|
// into a broken ELFv2 binary, just force LLVM to use ELFv2 as well. This will break when glibc
|
|
// is linked as glibc only supports ELFv2 for little endian, but there's nothing we can do about
|
|
// that. With this hack, `powerpc64-linux-none` will at least work.
|
|
//
|
|
// Once our self-hosted linker can handle both ABIs, this hack should go away.
|
|
if (target.cpu.arch == .powerpc64) return "elfv2";
|
|
|
|
const have_float = switch (target.abi) {
|
|
.gnueabihf, .musleabihf, .eabihf => true,
|
|
else => false,
|
|
};
|
|
|
|
switch (target.cpu.arch) {
|
|
.riscv64 => {
|
|
const featureSetHas = std.Target.riscv.featureSetHas;
|
|
if (featureSetHas(target.cpu.features, .d)) {
|
|
return "lp64d";
|
|
} else if (have_float) {
|
|
return "lp64f";
|
|
} else {
|
|
return "lp64";
|
|
}
|
|
},
|
|
.riscv32 => {
|
|
const featureSetHas = std.Target.riscv.featureSetHas;
|
|
if (featureSetHas(target.cpu.features, .d)) {
|
|
return "ilp32d";
|
|
} else if (have_float) {
|
|
return "ilp32f";
|
|
} else if (featureSetHas(target.cpu.features, .e)) {
|
|
return "ilp32e";
|
|
} else {
|
|
return "ilp32";
|
|
}
|
|
},
|
|
else => return null,
|
|
}
|
|
}
|
|
|
|
/// This function returns 1 if function alignment is not observable or settable.
|
|
pub fn defaultFunctionAlignment(target: std.Target) Alignment {
|
|
return switch (target.cpu.arch) {
|
|
.arm, .armeb => .@"4",
|
|
.aarch64, .aarch64_be => .@"4",
|
|
.sparc, .sparc64 => .@"4",
|
|
.riscv64 => .@"2",
|
|
else => .@"1",
|
|
};
|
|
}
|
|
|
|
pub fn minFunctionAlignment(target: std.Target) Alignment {
|
|
return switch (target.cpu.arch) {
|
|
.arm,
|
|
.armeb,
|
|
.aarch64,
|
|
.aarch64_be,
|
|
.riscv32,
|
|
.riscv64,
|
|
.sparc,
|
|
.sparc64,
|
|
=> .@"2",
|
|
else => .@"1",
|
|
};
|
|
}
|
|
|
|
pub fn supportsFunctionAlignment(target: std.Target) bool {
|
|
return switch (target.cpu.arch) {
|
|
.wasm32, .wasm64 => false,
|
|
else => true,
|
|
};
|
|
}
|
|
|
|
pub fn supportsTailCall(target: std.Target, backend: std.builtin.CompilerBackend) bool {
|
|
switch (backend) {
|
|
.stage1, .stage2_llvm => return @import("codegen/llvm.zig").supportsTailCall(target),
|
|
.stage2_c => return true,
|
|
else => return false,
|
|
}
|
|
}
|
|
|
|
pub fn supportsThreads(target: std.Target, backend: std.builtin.CompilerBackend) bool {
|
|
return switch (backend) {
|
|
.stage2_x86_64 => target.ofmt == .macho or target.ofmt == .elf,
|
|
else => true,
|
|
};
|
|
}
|
|
|
|
pub fn libcFloatPrefix(float_bits: u16) []const u8 {
|
|
return switch (float_bits) {
|
|
16, 80 => "__",
|
|
32, 64, 128 => "",
|
|
else => unreachable,
|
|
};
|
|
}
|
|
|
|
pub fn libcFloatSuffix(float_bits: u16) []const u8 {
|
|
return switch (float_bits) {
|
|
16 => "h", // Non-standard
|
|
32 => "f",
|
|
64 => "",
|
|
80 => "x", // Non-standard
|
|
128 => "q", // Non-standard (mimics convention in GCC libquadmath)
|
|
else => unreachable,
|
|
};
|
|
}
|
|
|
|
pub fn compilerRtFloatAbbrev(float_bits: u16) []const u8 {
|
|
return switch (float_bits) {
|
|
16 => "h",
|
|
32 => "s",
|
|
64 => "d",
|
|
80 => "x",
|
|
128 => "t",
|
|
else => unreachable,
|
|
};
|
|
}
|
|
|
|
pub fn compilerRtIntAbbrev(bits: u16) []const u8 {
|
|
return switch (bits) {
|
|
16 => "h",
|
|
32 => "s",
|
|
64 => "d",
|
|
128 => "t",
|
|
else => "o", // Non-standard
|
|
};
|
|
}
|
|
|
|
pub fn fnCallConvAllowsZigTypes(target: std.Target, cc: std.builtin.CallingConvention) bool {
|
|
return switch (cc) {
|
|
.Unspecified, .Async, .Inline => true,
|
|
// For now we want to authorize PTX kernel to use zig objects, even if
|
|
// we end up exposing the ABI. The goal is to experiment with more
|
|
// integrated CPU/GPU code.
|
|
.Kernel => target.cpu.arch == .nvptx or target.cpu.arch == .nvptx64,
|
|
else => false,
|
|
};
|
|
}
|
|
|
|
pub fn zigBackend(target: std.Target, use_llvm: bool) std.builtin.CompilerBackend {
|
|
if (use_llvm) return .stage2_llvm;
|
|
if (target.ofmt == .c) return .stage2_c;
|
|
return switch (target.cpu.arch) {
|
|
.wasm32, .wasm64 => .stage2_wasm,
|
|
.arm, .armeb, .thumb, .thumbeb => .stage2_arm,
|
|
.x86_64 => .stage2_x86_64,
|
|
.x86 => .stage2_x86,
|
|
.aarch64, .aarch64_be => .stage2_aarch64,
|
|
.riscv64 => .stage2_riscv64,
|
|
.sparc64 => .stage2_sparc64,
|
|
.spirv64 => .stage2_spirv64,
|
|
else => .other,
|
|
};
|
|
}
|
|
|
|
pub inline fn backendSupportsFeature(backend: std.builtin.CompilerBackend, comptime feature: Feature) bool {
|
|
return switch (feature) {
|
|
.panic_fn => switch (backend) {
|
|
.stage2_c,
|
|
.stage2_llvm,
|
|
.stage2_x86_64,
|
|
.stage2_riscv64,
|
|
=> true,
|
|
else => false,
|
|
},
|
|
.panic_unwrap_error => switch (backend) {
|
|
.stage2_c, .stage2_llvm => true,
|
|
else => false,
|
|
},
|
|
.safety_check_formatted => switch (backend) {
|
|
.stage2_c, .stage2_llvm => true,
|
|
else => false,
|
|
},
|
|
.error_return_trace => switch (backend) {
|
|
.stage2_llvm => true,
|
|
else => false,
|
|
},
|
|
.is_named_enum_value => switch (backend) {
|
|
.stage2_llvm => true,
|
|
else => false,
|
|
},
|
|
.error_set_has_value => switch (backend) {
|
|
.stage2_llvm, .stage2_wasm => true,
|
|
else => false,
|
|
},
|
|
.field_reordering => switch (backend) {
|
|
.stage2_c, .stage2_llvm => true,
|
|
else => false,
|
|
},
|
|
.safety_checked_instructions => switch (backend) {
|
|
.stage2_llvm => true,
|
|
else => false,
|
|
},
|
|
.separate_thread => switch (backend) {
|
|
.stage2_llvm => false,
|
|
else => true,
|
|
},
|
|
};
|
|
}
|