Merge pull request #19031 from antlilja/llvm-bc

Emit LLVM bitcode without using LLVM
This commit is contained in:
Jacob Young 2024-02-24 22:18:30 +01:00 committed by GitHub
commit b344ff01d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 8528 additions and 5348 deletions

View File

@ -460,13 +460,12 @@ test "std.meta.FieldType" {
try testing.expect(FieldType(U, .d) == *const u8);
}
pub fn fieldNames(comptime T: type) *const [fields(T).len][]const u8 {
pub fn fieldNames(comptime T: type) *const [fields(T).len][:0]const u8 {
return comptime blk: {
const fieldInfos = fields(T);
var names: [fieldInfos.len][]const u8 = undefined;
for (fieldInfos, 0..) |field, i| {
names[i] = field.name;
}
var names: [fieldInfos.len][:0]const u8 = undefined;
// This concat can be removed with the next zig1 update.
for (&names, fieldInfos) |*name, field| name.* = field.name ++ "";
break :blk &names;
};
}

View File

@ -16683,36 +16683,44 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
else => null,
};
defer if (elem_lock) |lock| self.register_manager.unlockReg(lock);
const elem_reg = registerAlias(
try self.copyToTmpRegister(elem_ty, mat_elem_mcv),
elem_abi_size,
);
const elem_extra_bits = self.regExtraBits(elem_ty);
if (elem_bit_off < elem_extra_bits) {
try self.truncateRegister(elem_ty, elem_reg);
{
const temp_reg = try self.copyToTmpRegister(elem_ty, mat_elem_mcv);
const temp_alias = registerAlias(temp_reg, elem_abi_size);
const temp_lock = self.register_manager.lockRegAssumeUnused(temp_reg);
defer self.register_manager.unlockReg(temp_lock);
if (elem_bit_off < elem_extra_bits) {
try self.truncateRegister(elem_ty, temp_alias);
}
if (elem_bit_off > 0) try self.genShiftBinOpMir(
.{ ._l, .sh },
elem_ty,
.{ .register = temp_alias },
Type.u8,
.{ .immediate = elem_bit_off },
);
try self.genBinOpMir(
.{ ._, .@"or" },
elem_ty,
.{ .load_frame = .{ .index = frame_index, .off = elem_byte_off } },
.{ .register = temp_alias },
);
}
if (elem_bit_off > 0) try self.genShiftBinOpMir(
.{ ._l, .sh },
elem_ty,
.{ .register = elem_reg },
Type.u8,
.{ .immediate = elem_bit_off },
);
try self.genBinOpMir(
.{ ._, .@"or" },
elem_ty,
.{ .load_frame = .{ .index = frame_index, .off = elem_byte_off } },
.{ .register = elem_reg },
);
if (elem_bit_off > elem_extra_bits) {
const reg = try self.copyToTmpRegister(elem_ty, mat_elem_mcv);
const temp_reg = try self.copyToTmpRegister(elem_ty, mat_elem_mcv);
const temp_alias = registerAlias(temp_reg, elem_abi_size);
const temp_lock = self.register_manager.lockRegAssumeUnused(temp_reg);
defer self.register_manager.unlockReg(temp_lock);
if (elem_extra_bits > 0) {
try self.truncateRegister(elem_ty, registerAlias(reg, elem_abi_size));
try self.truncateRegister(elem_ty, temp_alias);
}
try self.genShiftBinOpMir(
.{ ._r, .sh },
elem_ty,
.{ .register = reg },
.{ .register = temp_reg },
Type.u8,
.{ .immediate = elem_abi_bits - elem_bit_off },
);
@ -16723,7 +16731,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void {
.index = frame_index,
.off = elem_byte_off + @as(i32, @intCast(elem_abi_size)),
} },
.{ .register = reg },
.{ .register = temp_alias },
);
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,421 @@
const std = @import("std");
pub const AbbrevOp = union(enum) {
literal: u32, // 0
fixed: u16, // 1
fixed_runtime: type, // 1
vbr: u16, // 2
char6: void, // 4
blob: void, // 5
array_fixed: u16, // 3, 1
array_fixed_runtime: type, // 3, 1
array_vbr: u16, // 3, 2
array_char6: void, // 3, 4
};
pub const Error = error{OutOfMemory};
pub fn BitcodeWriter(comptime types: []const type) type {
return struct {
const BcWriter = @This();
buffer: std.ArrayList(u32),
bit_buffer: u32 = 0,
bit_count: u5 = 0,
widths: [types.len]u16,
pub fn getTypeWidth(self: BcWriter, comptime Type: type) u16 {
return self.widths[comptime std.mem.indexOfScalar(type, types, Type).?];
}
pub fn init(allocator: std.mem.Allocator, widths: [types.len]u16) BcWriter {
return .{
.buffer = std.ArrayList(u32).init(allocator),
.widths = widths,
};
}
pub fn deinit(self: BcWriter) void {
self.buffer.deinit();
}
pub fn toSlice(self: BcWriter) []const u32 {
std.debug.assert(self.bit_count == 0);
return self.buffer.items;
}
pub fn length(self: BcWriter) usize {
std.debug.assert(self.bit_count == 0);
return self.buffer.items.len;
}
pub fn writeBits(self: *BcWriter, value: anytype, bits: u16) Error!void {
if (bits == 0) return;
var in_buffer = bufValue(value, 32);
var in_bits = bits;
// Store input bits in buffer if they fit otherwise store as many as possible and flush
if (self.bit_count > 0) {
const bits_remaining = 31 - self.bit_count + 1;
const n: u5 = @intCast(@min(bits_remaining, in_bits));
const v = @as(u32, @truncate(in_buffer)) << self.bit_count;
self.bit_buffer |= v;
in_buffer >>= n;
self.bit_count +%= n;
in_bits -= n;
if (self.bit_count != 0) return;
try self.buffer.append(self.bit_buffer);
self.bit_buffer = 0;
}
// Write 32-bit chunks of input bits
while (in_bits >= 32) {
try self.buffer.append(@truncate(in_buffer));
in_buffer >>= 31;
in_buffer >>= 1;
in_bits -= 32;
}
// Store remaining input bits in buffer
if (in_bits > 0) {
self.bit_count = @intCast(in_bits);
self.bit_buffer = @truncate(in_buffer);
}
}
pub fn writeVBR(self: *BcWriter, value: anytype, comptime vbr_bits: usize) Error!void {
comptime {
std.debug.assert(vbr_bits > 1);
if (@bitSizeOf(@TypeOf(value)) > 64) @compileError("Unsupported VBR block type: " ++ @typeName(@TypeOf(value)));
}
var in_buffer = bufValue(value, vbr_bits);
const continue_bit = @as(@TypeOf(in_buffer), 1) << @intCast(vbr_bits - 1);
const mask = continue_bit - 1;
// If input is larger than one VBR block can store
// then store vbr_bits - 1 bits and a continue bit
while (in_buffer > mask) {
try self.writeBits(in_buffer & mask | continue_bit, vbr_bits);
in_buffer >>= @intCast(vbr_bits - 1);
}
// Store remaining bits
try self.writeBits(in_buffer, vbr_bits);
}
pub fn bitsVBR(_: *const BcWriter, value: anytype, comptime vbr_bits: usize) u16 {
comptime {
std.debug.assert(vbr_bits > 1);
if (@bitSizeOf(@TypeOf(value)) > 64) @compileError("Unsupported VBR block type: " ++ @typeName(@TypeOf(value)));
}
var bits: u16 = 0;
var in_buffer = bufValue(value, vbr_bits);
const continue_bit = @as(@TypeOf(in_buffer), 1) << @intCast(vbr_bits - 1);
const mask = continue_bit - 1;
// If input is larger than one VBR block can store
// then store vbr_bits - 1 bits and a continue bit
while (in_buffer > mask) {
bits += @intCast(vbr_bits);
in_buffer >>= @intCast(vbr_bits - 1);
}
// Store remaining bits
bits += @intCast(vbr_bits);
return bits;
}
pub fn write6BitChar(self: *BcWriter, c: u8) Error!void {
try self.writeBits(charTo6Bit(c), 6);
}
pub fn alignTo32(self: *BcWriter) Error!void {
if (self.bit_count == 0) return;
try self.buffer.append(self.bit_buffer);
self.bit_buffer = 0;
self.bit_count = 0;
}
pub fn enterTopBlock(self: *BcWriter, comptime SubBlock: type) Error!BlockWriter(SubBlock) {
return BlockWriter(SubBlock).init(self, 2);
}
fn BlockWriter(comptime Block: type) type {
return struct {
const Self = @This();
// The minimum abbrev id length based on the number of abbrevs present in the block
pub const abbrev_len = std.math.log2_int_ceil(
u6,
4 + (if (@hasDecl(Block, "abbrevs")) Block.abbrevs.len else 0),
);
start: usize,
bitcode: *BcWriter,
pub fn init(bitcode: *BcWriter, comptime parent_abbrev_len: u6) Error!Self {
try bitcode.writeBits(1, parent_abbrev_len);
try bitcode.writeVBR(Block.id, 8);
try bitcode.writeVBR(abbrev_len, 4);
try bitcode.alignTo32();
// We store the index of the block size and store a dummy value as the number of words in the block
const start = bitcode.length();
try bitcode.writeBits(0, 32);
// Predefine all block abbrevs
inline for (Block.abbrevs) |Abbrev| {
try defineAbbrev(bitcode, &Abbrev.ops);
}
return .{
.start = start,
.bitcode = bitcode,
};
}
pub fn enterSubBlock(self: Self, comptime SubBlock: type) Error!BlockWriter(SubBlock) {
return BlockWriter(SubBlock).init(self.bitcode, abbrev_len);
}
pub fn end(self: *Self) Error!void {
try self.bitcode.writeBits(0, abbrev_len);
try self.bitcode.alignTo32();
// Set the number of words in the block at the start of the block
self.bitcode.buffer.items[self.start] = @truncate(self.bitcode.length() - self.start - 1);
}
pub fn writeUnabbrev(self: *Self, code: u32, values: []const u64) Error!void {
try self.bitcode.writeBits(3, abbrev_len);
try self.bitcode.writeVBR(code, 6);
try self.bitcode.writeVBR(values.len, 6);
for (values) |val| {
try self.bitcode.writeVBR(val, 6);
}
}
pub fn writeAbbrev(self: *Self, params: anytype) Error!void {
return self.writeAbbrevAdapted(params, struct {
pub fn get(_: @This(), param: anytype, comptime _: []const u8) @TypeOf(param) {
return param;
}
}{});
}
pub fn abbrevId(comptime Abbrev: type) u32 {
inline for (Block.abbrevs, 0..) |abbrev, i| {
if (Abbrev == abbrev) return i + 4;
}
@compileError("Unknown abbrev: " ++ @typeName(Abbrev));
}
pub fn writeAbbrevAdapted(
self: *Self,
params: anytype,
adapter: anytype,
) Error!void {
const Abbrev = @TypeOf(params);
try self.bitcode.writeBits(comptime abbrevId(Abbrev), abbrev_len);
const fields = std.meta.fields(Abbrev);
// This abbreviation might only contain literals
if (fields.len == 0) return;
comptime var field_index: usize = 0;
inline for (Abbrev.ops) |ty| {
const field_name = fields[field_index].name;
const param = @field(params, field_name);
switch (ty) {
.literal => continue,
.fixed => |len| try self.bitcode.writeBits(adapter.get(param, field_name), len),
.fixed_runtime => |width_ty| try self.bitcode.writeBits(
adapter.get(param, field_name),
self.bitcode.getTypeWidth(width_ty),
),
.vbr => |len| try self.bitcode.writeVBR(adapter.get(param, field_name), len),
.char6 => try self.bitcode.write6BitChar(adapter.get(param, field_name)),
.blob => {
try self.bitcode.writeVBR(param.len, 6);
try self.bitcode.alignTo32();
for (param) |x| {
try self.bitcode.writeBits(x, 8);
}
try self.bitcode.alignTo32();
},
.array_fixed => |len| {
try self.bitcode.writeVBR(param.len, 6);
for (param) |x| {
try self.bitcode.writeBits(adapter.get(x, field_name), len);
}
},
.array_fixed_runtime => |width_ty| {
try self.bitcode.writeVBR(param.len, 6);
for (param) |x| {
try self.bitcode.writeBits(
adapter.get(x, field_name),
self.bitcode.getTypeWidth(width_ty),
);
}
},
.array_vbr => |len| {
try self.bitcode.writeVBR(param.len, 6);
for (param) |x| {
try self.bitcode.writeVBR(adapter.get(x, field_name), len);
}
},
.array_char6 => {
try self.bitcode.writeVBR(param.len, 6);
for (param) |x| {
try self.bitcode.write6BitChar(adapter.get(x, field_name));
}
},
}
field_index += 1;
if (field_index == fields.len) break;
}
}
fn defineAbbrev(bitcode: *BcWriter, comptime ops: []const AbbrevOp) Error!void {
try bitcode.writeBits(2, abbrev_len);
// ops.len is not accurate because arrays are actually two ops
try bitcode.writeVBR(blk: {
var count: usize = 0;
inline for (ops) |op| {
count += switch (op) {
.literal, .fixed, .fixed_runtime, .vbr, .char6, .blob => 1,
.array_fixed, .array_fixed_runtime, .array_vbr, .array_char6 => 2,
};
}
break :blk count;
}, 5);
inline for (ops) |op| {
switch (op) {
.literal => |value| {
try bitcode.writeBits(1, 1);
try bitcode.writeVBR(value, 8);
},
.fixed => |width| {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVBR(width, 5);
},
.fixed_runtime => |width_ty| {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5);
},
.vbr => |width| {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(2, 3);
try bitcode.writeVBR(width, 5);
},
.char6 => {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(4, 3);
},
.blob => {
try bitcode.writeBits(0, 1);
try bitcode.writeBits(5, 3);
},
.array_fixed => |width| {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Fixed or VBR op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVBR(width, 5);
},
.array_fixed_runtime => |width_ty| {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Fixed or VBR op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(1, 3);
try bitcode.writeVBR(bitcode.getTypeWidth(width_ty), 5);
},
.array_vbr => |width| {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Fixed or VBR op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(2, 3);
try bitcode.writeVBR(width, 5);
},
.array_char6 => {
// Array op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(3, 3);
// Char6 op
try bitcode.writeBits(0, 1);
try bitcode.writeBits(4, 3);
},
}
}
}
};
}
};
}
fn charTo6Bit(c: u8) u8 {
return switch (c) {
'a'...'z' => c - 'a',
'A'...'Z' => c - 'A' + 26,
'0'...'9' => c - '0' + 52,
'.' => 62,
'_' => 63,
else => @panic("Failed to encode byte as 6-bit char"),
};
}
fn BufType(comptime T: type, comptime min_len: usize) type {
return std.meta.Int(.unsigned, @max(min_len, @bitSizeOf(switch (@typeInfo(T)) {
.ComptimeInt => u32,
.Int => |info| if (info.signedness == .unsigned)
T
else
@compileError("Unsupported type: " ++ @typeName(T)),
.Enum => |info| info.tag_type,
.Bool => u1,
.Struct => |info| switch (info.layout) {
.Auto, .Extern => @compileError("Unsupported type: " ++ @typeName(T)),
.Packed => std.meta.Int(.unsigned, @bitSizeOf(T)),
},
else => @compileError("Unsupported type: " ++ @typeName(T)),
})));
}
fn bufValue(value: anytype, comptime min_len: usize) BufType(@TypeOf(value), min_len) {
return switch (@typeInfo(@TypeOf(value))) {
.ComptimeInt, .Int => @intCast(value),
.Enum => @intFromEnum(value),
.Bool => @intFromBool(value),
.Struct => @intCast(@as(std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(value))), @bitCast(value))),
else => unreachable,
};
}

1636
src/codegen/llvm/ir.zig Normal file

File diff suppressed because it is too large Load Diff

View File

@ -839,10 +839,9 @@ pub const File = struct {
}
const llvm_bindings = @import("codegen/llvm/bindings.zig");
const Builder = @import("codegen/llvm/Builder.zig");
const llvm = @import("codegen/llvm.zig");
const target = comp.root_mod.resolved_target.result;
Builder.initializeLLVMTarget(target.cpu.arch);
llvm.initializeLLVMTarget(target.cpu.arch);
const os_tag = llvm.targetOs(target.os.tag);
const bad = llvm_bindings.WriteArchive(full_out_path_z, object_files.items.ptr, object_files.items.len, os_tag);
if (bad) return error.UnableToWriteArchive;

View File

@ -24,9 +24,7 @@
#include <llvm/Analysis/TargetLibraryInfo.h>
#include <llvm/Analysis/TargetTransformInfo.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/IR/DIBuilder.h>
#include <llvm/IR/DiagnosticInfo.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/InlineAsm.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/LegacyPassManager.h>
@ -382,566 +380,10 @@ void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit) {
unwrap(context_ref)->setOptPassGate(opt_bisect);
}
LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy, unsigned AddressSpace) {
Function* func = Function::Create(unwrap<FunctionType>(FunctionTy), GlobalValue::ExternalLinkage, AddressSpace, Name, unwrap(M));
return wrap(func);
}
void ZigLLVMSetTailCallKind(LLVMValueRef Call, enum ZigLLVMTailCallKind TailCallKind) {
CallInst::TailCallKind TCK;
switch (TailCallKind) {
case ZigLLVMTailCallKindNone:
TCK = CallInst::TCK_None;
break;
case ZigLLVMTailCallKindTail:
TCK = CallInst::TCK_Tail;
break;
case ZigLLVMTailCallKindMustTail:
TCK = CallInst::TCK_MustTail;
break;
case ZigLLVMTailCallKindNoTail:
TCK = CallInst::TCK_NoTail;
break;
}
unwrap<CallInst>(Call)->setTailCallKind(TCK);
}
void ZigLLVMFnSetSubprogram(LLVMValueRef fn, ZigLLVMDISubprogram *subprogram) {
assert( isa<Function>(unwrap(fn)) );
Function *unwrapped_function = reinterpret_cast<Function*>(unwrap(fn));
unwrapped_function->setSubprogram(reinterpret_cast<DISubprogram*>(subprogram));
}
ZigLLVMDIType *ZigLLVMCreateDebugPointerType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIType *pointee_type,
uint64_t size_in_bits, uint64_t align_in_bits, const char *name)
{
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createPointerType(
reinterpret_cast<DIType*>(pointee_type), size_in_bits, align_in_bits, std::optional<unsigned>(), name);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateDebugBasicType(ZigLLVMDIBuilder *dibuilder, const char *name,
uint64_t size_in_bits, unsigned encoding)
{
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createBasicType(
name, size_in_bits, encoding);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
struct ZigLLVMDIType *ZigLLVMDIBuilderCreateVectorType(struct ZigLLVMDIBuilder *dibuilder,
uint64_t SizeInBits, uint32_t AlignInBits, struct ZigLLVMDIType *Ty, uint32_t elem_count)
{
SmallVector<Metadata *, 1> subrange;
subrange.push_back(reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateSubrange(0, elem_count));
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createVectorType(
SizeInBits,
AlignInBits,
reinterpret_cast<DIType*>(Ty),
reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(subrange));
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateDebugArrayType(ZigLLVMDIBuilder *dibuilder, uint64_t size_in_bits,
uint64_t align_in_bits, ZigLLVMDIType *elem_type, int64_t elem_count)
{
SmallVector<Metadata *, 1> subrange;
subrange.push_back(reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateSubrange(0, elem_count));
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createArrayType(
size_in_bits, align_in_bits,
reinterpret_cast<DIType*>(elem_type),
reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(subrange));
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIEnumerator *ZigLLVMCreateDebugEnumerator(ZigLLVMDIBuilder *dibuilder, const char *name, uint64_t val, bool isUnsigned) {
DIEnumerator *di_enumerator = reinterpret_cast<DIBuilder*>(dibuilder)->createEnumerator(name, val, isUnsigned);
return reinterpret_cast<ZigLLVMDIEnumerator*>(di_enumerator);
}
ZigLLVMDIEnumerator *ZigLLVMCreateDebugEnumeratorOfArbitraryPrecision(ZigLLVMDIBuilder *dibuilder,
const char *name, unsigned NumWords, const uint64_t Words[], unsigned int bits, bool isUnsigned)
{
DIEnumerator *di_enumerator = reinterpret_cast<DIBuilder*>(dibuilder)->createEnumerator(name,
APSInt(APInt(bits, ArrayRef(Words, NumWords)), isUnsigned));
return reinterpret_cast<ZigLLVMDIEnumerator*>(di_enumerator);
}
ZigLLVMDIType *ZigLLVMCreateDebugEnumerationType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
const char *name, ZigLLVMDIFile *file, unsigned line_number, uint64_t size_in_bits,
uint64_t align_in_bits, ZigLLVMDIEnumerator **enumerator_array, int enumerator_array_len,
ZigLLVMDIType *underlying_type, const char *unique_id)
{
SmallVector<Metadata *, 8> fields;
for (int i = 0; i < enumerator_array_len; i += 1) {
DIEnumerator *dienumerator = reinterpret_cast<DIEnumerator*>(enumerator_array[i]);
fields.push_back(dienumerator);
}
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createEnumerationType(
reinterpret_cast<DIScope*>(scope),
name,
reinterpret_cast<DIFile*>(file),
line_number, size_in_bits, align_in_bits,
reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields),
reinterpret_cast<DIType*>(underlying_type),
unique_id);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateDebugMemberType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
const char *name, ZigLLVMDIFile *file, unsigned line, uint64_t size_in_bits,
uint64_t align_in_bits, uint64_t offset_in_bits, unsigned flags, ZigLLVMDIType *type)
{
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createMemberType(
reinterpret_cast<DIScope*>(scope),
name,
reinterpret_cast<DIFile*>(file),
line, size_in_bits, align_in_bits, offset_in_bits,
static_cast<DINode::DIFlags>(flags),
reinterpret_cast<DIType*>(type));
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateDebugUnionType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
const char *name, ZigLLVMDIFile *file, unsigned line_number, uint64_t size_in_bits,
uint64_t align_in_bits, unsigned flags, ZigLLVMDIType **types_array, int types_array_len,
unsigned run_time_lang, const char *unique_id)
{
SmallVector<Metadata *, 8> fields;
for (int i = 0; i < types_array_len; i += 1) {
DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
fields.push_back(ditype);
}
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createUnionType(
reinterpret_cast<DIScope*>(scope),
name,
reinterpret_cast<DIFile*>(file),
line_number, size_in_bits, align_in_bits,
static_cast<DINode::DIFlags>(flags),
reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields),
run_time_lang, unique_id);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateDebugStructType(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
const char *name, ZigLLVMDIFile *file, unsigned line_number, uint64_t size_in_bits,
uint64_t align_in_bits, unsigned flags, ZigLLVMDIType *derived_from,
ZigLLVMDIType **types_array, int types_array_len, unsigned run_time_lang, ZigLLVMDIType *vtable_holder,
const char *unique_id)
{
SmallVector<Metadata *, 8> fields;
for (int i = 0; i < types_array_len; i += 1) {
DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
fields.push_back(ditype);
}
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createStructType(
reinterpret_cast<DIScope*>(scope),
name,
reinterpret_cast<DIFile*>(file),
line_number, size_in_bits, align_in_bits,
static_cast<DINode::DIFlags>(flags),
reinterpret_cast<DIType*>(derived_from),
reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields),
run_time_lang,
reinterpret_cast<DIType*>(vtable_holder),
unique_id);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateReplaceableCompositeType(ZigLLVMDIBuilder *dibuilder, unsigned tag,
const char *name, ZigLLVMDIScope *scope, ZigLLVMDIFile *file, unsigned line)
{
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createReplaceableCompositeType(
tag, name,
reinterpret_cast<DIScope*>(scope),
reinterpret_cast<DIFile*>(file),
line);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
ZigLLVMDIType *ZigLLVMCreateDebugForwardDeclType(ZigLLVMDIBuilder *dibuilder, unsigned tag,
const char *name, ZigLLVMDIScope *scope, ZigLLVMDIFile *file, unsigned line)
{
DIType *di_type = reinterpret_cast<DIBuilder*>(dibuilder)->createForwardDecl(
tag, name,
reinterpret_cast<DIScope*>(scope),
reinterpret_cast<DIFile*>(file),
line);
return reinterpret_cast<ZigLLVMDIType*>(di_type);
}
void ZigLLVMReplaceTemporary(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIType *type,
ZigLLVMDIType *replacement)
{
reinterpret_cast<DIBuilder*>(dibuilder)->replaceTemporary(
TempDIType(reinterpret_cast<DIType*>(type)),
reinterpret_cast<DIType*>(replacement));
}
void ZigLLVMReplaceDebugArrays(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIType *type,
ZigLLVMDIType **types_array, int types_array_len)
{
SmallVector<Metadata *, 8> fields;
for (int i = 0; i < types_array_len; i += 1) {
DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
fields.push_back(ditype);
}
DICompositeType *composite_type = (DICompositeType*)reinterpret_cast<DIType*>(type);
reinterpret_cast<DIBuilder*>(dibuilder)->replaceArrays(
composite_type,
reinterpret_cast<DIBuilder*>(dibuilder)->getOrCreateArray(fields));
}
ZigLLVMDIType *ZigLLVMCreateSubroutineType(ZigLLVMDIBuilder *dibuilder_wrapped,
ZigLLVMDIType **types_array, int types_array_len, unsigned flags)
{
SmallVector<Metadata *, 8> types;
for (int i = 0; i < types_array_len; i += 1) {
DIType *ditype = reinterpret_cast<DIType*>(types_array[i]);
types.push_back(ditype);
}
DIBuilder *dibuilder = reinterpret_cast<DIBuilder*>(dibuilder_wrapped);
DISubroutineType *subroutine_type = dibuilder->createSubroutineType(
dibuilder->getOrCreateTypeArray(types),
static_cast<DINode::DIFlags>(flags));
DIType *ditype = subroutine_type;
return reinterpret_cast<ZigLLVMDIType*>(ditype);
}
unsigned ZigLLVMEncoding_DW_ATE_unsigned(void) {
return dwarf::DW_ATE_unsigned;
}
unsigned ZigLLVMEncoding_DW_ATE_signed(void) {
return dwarf::DW_ATE_signed;
}
unsigned ZigLLVMEncoding_DW_ATE_float(void) {
return dwarf::DW_ATE_float;
}
unsigned ZigLLVMEncoding_DW_ATE_boolean(void) {
return dwarf::DW_ATE_boolean;
}
unsigned ZigLLVMEncoding_DW_ATE_unsigned_char(void) {
return dwarf::DW_ATE_unsigned_char;
}
unsigned ZigLLVMEncoding_DW_ATE_signed_char(void) {
return dwarf::DW_ATE_signed_char;
}
unsigned ZigLLVMLang_DW_LANG_C99(void) {
return dwarf::DW_LANG_C99;
}
unsigned ZigLLVMTag_DW_variable(void) {
return dwarf::DW_TAG_variable;
}
unsigned ZigLLVMTag_DW_structure_type(void) {
return dwarf::DW_TAG_structure_type;
}
unsigned ZigLLVMTag_DW_enumeration_type(void) {
return dwarf::DW_TAG_enumeration_type;
}
unsigned ZigLLVMTag_DW_union_type(void) {
return dwarf::DW_TAG_union_type;
}
ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved) {
DIBuilder *di_builder = new(std::nothrow) DIBuilder(*unwrap(module), allow_unresolved);
if (di_builder == nullptr)
return nullptr;
return reinterpret_cast<ZigLLVMDIBuilder *>(di_builder);
}
void ZigLLVMDisposeDIBuilder(ZigLLVMDIBuilder *dbuilder) {
DIBuilder *di_builder = reinterpret_cast<DIBuilder *>(dbuilder);
delete di_builder;
}
void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder,
unsigned int line, unsigned int column, ZigLLVMDIScope *scope)
{
DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, column, di_scope, nullptr, false);
unwrap(builder)->SetCurrentDebugLocation(debug_loc);
}
void ZigLLVMSetCurrentDebugLocation2(LLVMBuilderRef builder, unsigned int line,
unsigned int column, ZigLLVMDIScope *scope, ZigLLVMDILocation *inlined_at)
{
DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, column, di_scope,
reinterpret_cast<DILocation *>(inlined_at), false);
unwrap(builder)->SetCurrentDebugLocation(debug_loc);
}
void ZigLLVMClearCurrentDebugLocation(LLVMBuilderRef builder) {
unwrap(builder)->SetCurrentDebugLocation(DebugLoc());
}
ZigLLVMDILexicalBlock *ZigLLVMCreateLexicalBlock(ZigLLVMDIBuilder *dbuilder, ZigLLVMDIScope *scope,
ZigLLVMDIFile *file, unsigned line, unsigned col)
{
DILexicalBlock *result = reinterpret_cast<DIBuilder*>(dbuilder)->createLexicalBlock(
reinterpret_cast<DIScope*>(scope),
reinterpret_cast<DIFile*>(file),
line,
col);
return reinterpret_cast<ZigLLVMDILexicalBlock*>(result);
}
ZigLLVMDILocalVariable *ZigLLVMCreateAutoVariable(ZigLLVMDIBuilder *dbuilder,
ZigLLVMDIScope *scope, const char *name, ZigLLVMDIFile *file, unsigned line_no,
ZigLLVMDIType *type, bool always_preserve, unsigned flags)
{
DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createAutoVariable(
reinterpret_cast<DIScope*>(scope),
name,
reinterpret_cast<DIFile*>(file),
line_no,
reinterpret_cast<DIType*>(type),
always_preserve,
static_cast<DINode::DIFlags>(flags));
return reinterpret_cast<ZigLLVMDILocalVariable*>(result);
}
ZigLLVMDIGlobalVariableExpression *ZigLLVMCreateGlobalVariableExpression(ZigLLVMDIBuilder *dbuilder,
ZigLLVMDIScope *scope, const char *name, const char *linkage_name, ZigLLVMDIFile *file,
unsigned line_no, ZigLLVMDIType *di_type, bool is_local_to_unit)
{
return reinterpret_cast<ZigLLVMDIGlobalVariableExpression*>(reinterpret_cast<DIBuilder*>(dbuilder)->createGlobalVariableExpression(
reinterpret_cast<DIScope*>(scope),
name,
linkage_name,
reinterpret_cast<DIFile*>(file),
line_no,
reinterpret_cast<DIType*>(di_type),
is_local_to_unit));
}
ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(ZigLLVMDIBuilder *dbuilder,
ZigLLVMDIScope *scope, const char *name, ZigLLVMDIFile *file, unsigned line_no,
ZigLLVMDIType *type, bool always_preserve, unsigned flags, unsigned arg_no)
{
assert(arg_no != 0);
DILocalVariable *result = reinterpret_cast<DIBuilder*>(dbuilder)->createParameterVariable(
reinterpret_cast<DIScope*>(scope),
name,
arg_no,
reinterpret_cast<DIFile*>(file),
line_no,
reinterpret_cast<DIType*>(type),
always_preserve,
static_cast<DINode::DIFlags>(flags));
return reinterpret_cast<ZigLLVMDILocalVariable*>(result);
}
ZigLLVMDIScope *ZigLLVMLexicalBlockToScope(ZigLLVMDILexicalBlock *lexical_block) {
DIScope *scope = reinterpret_cast<DILexicalBlock*>(lexical_block);
return reinterpret_cast<ZigLLVMDIScope*>(scope);
}
ZigLLVMDIScope *ZigLLVMCompileUnitToScope(ZigLLVMDICompileUnit *compile_unit) {
DIScope *scope = reinterpret_cast<DICompileUnit*>(compile_unit);
return reinterpret_cast<ZigLLVMDIScope*>(scope);
}
ZigLLVMDIScope *ZigLLVMFileToScope(ZigLLVMDIFile *difile) {
DIScope *scope = reinterpret_cast<DIFile*>(difile);
return reinterpret_cast<ZigLLVMDIScope*>(scope);
}
ZigLLVMDIScope *ZigLLVMSubprogramToScope(ZigLLVMDISubprogram *subprogram) {
DIScope *scope = reinterpret_cast<DISubprogram*>(subprogram);
return reinterpret_cast<ZigLLVMDIScope*>(scope);
}
ZigLLVMDIScope *ZigLLVMTypeToScope(ZigLLVMDIType *type) {
DIScope *scope = reinterpret_cast<DIType*>(type);
return reinterpret_cast<ZigLLVMDIScope*>(scope);
}
ZigLLVMDINode *ZigLLVMLexicalBlockToNode(ZigLLVMDILexicalBlock *lexical_block) {
DINode *node = reinterpret_cast<DILexicalBlock*>(lexical_block);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
ZigLLVMDINode *ZigLLVMCompileUnitToNode(ZigLLVMDICompileUnit *compile_unit) {
DINode *node = reinterpret_cast<DICompileUnit*>(compile_unit);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
ZigLLVMDINode *ZigLLVMFileToNode(ZigLLVMDIFile *difile) {
DINode *node = reinterpret_cast<DIFile*>(difile);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
ZigLLVMDINode *ZigLLVMSubprogramToNode(ZigLLVMDISubprogram *subprogram) {
DINode *node = reinterpret_cast<DISubprogram*>(subprogram);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
ZigLLVMDINode *ZigLLVMTypeToNode(ZigLLVMDIType *type) {
DINode *node = reinterpret_cast<DIType*>(type);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
ZigLLVMDINode *ZigLLVMScopeToNode(ZigLLVMDIScope *scope) {
DINode *node = reinterpret_cast<DIScope*>(scope);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
ZigLLVMDINode *ZigLLVMGlobalVariableToNode(ZigLLVMDIGlobalVariable *global_variable) {
DINode *node = reinterpret_cast<DIGlobalVariable*>(global_variable);
return reinterpret_cast<ZigLLVMDINode*>(node);
}
void ZigLLVMSubprogramReplaceLinkageName(ZigLLVMDISubprogram *subprogram,
ZigLLVMMDString *linkage_name)
{
MDString *linkage_name_md = reinterpret_cast<MDString*>(linkage_name);
reinterpret_cast<DISubprogram*>(subprogram)->replaceLinkageName(linkage_name_md);
}
void ZigLLVMGlobalVariableReplaceLinkageName(ZigLLVMDIGlobalVariable *global_variable,
ZigLLVMMDString *linkage_name)
{
Metadata *linkage_name_md = reinterpret_cast<MDString*>(linkage_name);
// NOTE: Operand index must match llvm::DIGlobalVariable
reinterpret_cast<DIGlobalVariable*>(global_variable)->replaceOperandWith(5, linkage_name_md);
}
ZigLLVMDICompileUnit *ZigLLVMCreateCompileUnit(ZigLLVMDIBuilder *dibuilder,
unsigned lang, ZigLLVMDIFile *difile, const char *producer,
bool is_optimized, const char *flags, unsigned runtime_version, const char *split_name,
uint64_t dwo_id, bool emit_debug_info)
{
DICompileUnit *result = reinterpret_cast<DIBuilder*>(dibuilder)->createCompileUnit(
lang,
reinterpret_cast<DIFile*>(difile),
producer, is_optimized, flags, runtime_version, split_name,
(emit_debug_info ? DICompileUnit::DebugEmissionKind::FullDebug : DICompileUnit::DebugEmissionKind::NoDebug),
dwo_id);
return reinterpret_cast<ZigLLVMDICompileUnit*>(result);
}
ZigLLVMDIFile *ZigLLVMCreateFile(ZigLLVMDIBuilder *dibuilder, const char *filename, const char *directory) {
DIFile *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFile(filename, directory);
return reinterpret_cast<ZigLLVMDIFile*>(result);
}
ZigLLVMDISubprogram *ZigLLVMCreateFunction(ZigLLVMDIBuilder *dibuilder, ZigLLVMDIScope *scope,
const char *name, const char *linkage_name, ZigLLVMDIFile *file, unsigned lineno,
ZigLLVMDIType *fn_di_type, bool is_local_to_unit, bool is_definition, unsigned scope_line,
unsigned flags, bool is_optimized, ZigLLVMDISubprogram *decl_subprogram)
{
DISubroutineType *di_sub_type = static_cast<DISubroutineType*>(reinterpret_cast<DIType*>(fn_di_type));
DISubprogram *result = reinterpret_cast<DIBuilder*>(dibuilder)->createFunction(
reinterpret_cast<DIScope*>(scope),
name, linkage_name,
reinterpret_cast<DIFile*>(file),
lineno,
di_sub_type,
scope_line,
static_cast<DINode::DIFlags>(flags),
DISubprogram::toSPFlags(is_local_to_unit, is_definition, is_optimized),
nullptr,
reinterpret_cast<DISubprogram *>(decl_subprogram),
nullptr);
return reinterpret_cast<ZigLLVMDISubprogram*>(result);
}
void ZigLLVMDIBuilderFinalize(ZigLLVMDIBuilder *dibuilder) {
reinterpret_cast<DIBuilder*>(dibuilder)->finalize();
}
LLVMValueRef ZigLLVMInsertDeclareAtEnd(ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref)
{
Instruction *result = reinterpret_cast<DIBuilder*>(dibuilder)->insertDeclare(
unwrap(storage),
reinterpret_cast<DILocalVariable *>(var_info),
reinterpret_cast<DIBuilder*>(dibuilder)->createExpression(),
reinterpret_cast<DILocation*>(debug_loc),
static_cast<BasicBlock*>(unwrap(basic_block_ref)));
return wrap(result);
}
LLVMValueRef ZigLLVMInsertDbgValueIntrinsicAtEnd(ZigLLVMDIBuilder *dib, LLVMValueRef val,
ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc,
LLVMBasicBlockRef basic_block_ref)
{
Instruction *result = reinterpret_cast<DIBuilder*>(dib)->insertDbgValueIntrinsic(
unwrap(val),
reinterpret_cast<DILocalVariable *>(var_info),
reinterpret_cast<DIBuilder*>(dib)->createExpression(),
reinterpret_cast<DILocation*>(debug_loc),
static_cast<BasicBlock*>(unwrap(basic_block_ref)));
return wrap(result);
}
LLVMValueRef ZigLLVMInsertDeclare(ZigLLVMDIBuilder *dibuilder, LLVMValueRef storage,
ZigLLVMDILocalVariable *var_info, ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr)
{
Instruction *result = reinterpret_cast<DIBuilder*>(dibuilder)->insertDeclare(
unwrap(storage),
reinterpret_cast<DILocalVariable *>(var_info),
reinterpret_cast<DIBuilder*>(dibuilder)->createExpression(),
reinterpret_cast<DILocation*>(debug_loc),
static_cast<Instruction*>(unwrap(insert_before_instr)));
return wrap(result);
}
ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigned col, ZigLLVMDIScope *scope) {
DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, col, di_scope, nullptr, false);
return reinterpret_cast<ZigLLVMDILocation*>(debug_loc.get());
}
ZigLLVMDILocation *ZigLLVMGetDebugLoc2(unsigned line, unsigned col, ZigLLVMDIScope *scope,
ZigLLVMDILocation *inlined_at) {
DIScope* di_scope = reinterpret_cast<DIScope*>(scope);
DebugLoc debug_loc = DILocation::get(di_scope->getContext(), line, col, di_scope,
reinterpret_cast<DILocation *>(inlined_at), false);
return reinterpret_cast<ZigLLVMDILocation*>(debug_loc.get());
}
void ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state) {
if (on_state) {
FastMathFlags fmf;
fmf.setFast();
unwrap(builder_wrapped)->setFastMathFlags(fmf);
} else {
unwrap(builder_wrapped)->clearFastMathFlags();
}
}
void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv) {
cl::ParseCommandLineOptions(argc, argv);
}
void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module, bool produce_dwarf64) {
unwrap(module)->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION);
unwrap(module)->addModuleFlag(Module::Warning, "Dwarf Version", 4);
if (produce_dwarf64) {
unwrap(module)->addModuleFlag(Module::Warning, "DWARF64", 1);
}
}
void ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module) {
unwrap(module)->addModuleFlag(Module::Warning, "Debug Info Version", DEBUG_METADATA_VERSION);
unwrap(module)->addModuleFlag(Module::Warning, "CodeView", 1);
}
void ZigLLVMSetModulePICLevel(LLVMModuleRef module) {
unwrap(module)->setPICLevel(PICLevel::Level::BigPIC);
}
@ -956,35 +398,6 @@ void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model) {
assert(!JIT);
}
LLVMValueRef ZigLLVMBuildNSWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name)
{
return wrap(unwrap(builder)->CreateShl(unwrap(LHS), unwrap(RHS), name, false, true));
}
LLVMValueRef ZigLLVMBuildNUWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name)
{
return wrap(unwrap(builder)->CreateShl(unwrap(LHS), unwrap(RHS), name, true, false));
}
LLVMValueRef ZigLLVMBuildLShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name)
{
return wrap(unwrap(builder)->CreateLShr(unwrap(LHS), unwrap(RHS), name, true));
}
LLVMValueRef ZigLLVMBuildAShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name)
{
return wrap(unwrap(builder)->CreateAShr(unwrap(LHS), unwrap(RHS), name, true));
}
LLVMValueRef ZigLLVMBuildAllocaInAddressSpace(LLVMBuilderRef builder, LLVMTypeRef Ty,
unsigned AddressSpace, const char *Name) {
return wrap(unwrap(builder)->CreateAlloca(unwrap(Ty), AddressSpace, nullptr, Name));
}
bool ZigLLVMWriteImportLibrary(const char *def_path, const ZigLLVM_ArchType arch,
const char *output_lib_path, bool kill_at)
{
@ -1134,43 +547,6 @@ bool ZigLLDLinkWasm(int argc, const char **argv, bool can_exit_early, bool disab
return lld::wasm::link(args, llvm::outs(), llvm::errs(), can_exit_early, disable_output);
}
void ZigLLVMTakeName(LLVMValueRef new_owner, LLVMValueRef victim) {
unwrap(new_owner)->takeName(unwrap(victim));
}
void ZigLLVMRemoveGlobalValue(LLVMValueRef GlobalVal) {
unwrap<GlobalValue>(GlobalVal)->removeFromParent();
}
void ZigLLVMEraseGlobalValue(LLVMValueRef GlobalVal) {
unwrap<GlobalValue>(GlobalVal)->eraseFromParent();
}
void ZigLLVMDeleteGlobalValue(LLVMValueRef GlobalVal) {
auto *GV = unwrap<GlobalValue>(GlobalVal);
assert(GV->getParent() == nullptr);
switch (GV->getValueID()) {
#define HANDLE_GLOBAL_VALUE(NAME) \
case Value::NAME##Val: \
delete static_cast<NAME *>(GV); \
break;
#include <llvm/IR/Value.def>
default: llvm_unreachable("Expected global value");
}
}
void ZigLLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal) {
unwrap<GlobalVariable>(GlobalVar)->setInitializer(ConstantVal ? unwrap<Constant>(ConstantVal) : nullptr);
}
ZigLLVMDIGlobalVariable* ZigLLVMGlobalGetVariable(ZigLLVMDIGlobalVariableExpression *global_variable_expression) {
return reinterpret_cast<ZigLLVMDIGlobalVariable*>(reinterpret_cast<DIGlobalVariableExpression*>(global_variable_expression)->getVariable());
}
void ZigLLVMAttachMetaData(LLVMValueRef Val, ZigLLVMDIGlobalVariableExpression *global_variable_expression) {
unwrap<GlobalVariable>(Val)->addDebugInfo(reinterpret_cast<DIGlobalVariableExpression*>(global_variable_expression));
}
static_assert((Triple::ArchType)ZigLLVM_UnknownArch == Triple::UnknownArch, "");
static_assert((Triple::ArchType)ZigLLVM_arm == Triple::arm, "");
static_assert((Triple::ArchType)ZigLLVM_armeb == Triple::armeb, "");

View File

@ -24,24 +24,6 @@
// ATTENTION: If you modify this file, be sure to update the corresponding
// extern function declarations in the self-hosted compiler.
struct ZigLLVMDIType;
struct ZigLLVMDIBuilder;
struct ZigLLVMDICompileUnit;
struct ZigLLVMDIScope;
struct ZigLLVMDIFile;
struct ZigLLVMDILexicalBlock;
struct ZigLLVMDISubprogram;
struct ZigLLVMDISubroutineType;
struct ZigLLVMDILocalVariable;
struct ZigLLVMDIGlobalVariableExpression;
struct ZigLLVMDIGlobalVariable;
struct ZigLLVMDIGlobalExpression;
struct ZigLLVMDILocation;
struct ZigLLVMDIEnumerator;
struct ZigLLVMInsertionPoint;
struct ZigLLVMDINode;
struct ZigLLVMMDString;
ZIG_EXTERN_C bool ZigLLVMTargetMachineEmitToFile(LLVMTargetMachineRef targ_machine_ref, LLVMModuleRef module_ref,
char **error_message, bool is_debug,
bool is_small, bool time_report, bool tsan, bool lto,
@ -62,9 +44,6 @@ ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, co
ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
ZIG_EXTERN_C LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name,
LLVMTypeRef FunctionTy, unsigned AddressSpace);
enum ZigLLVMTailCallKind {
ZigLLVMTailCallKindNone,
ZigLLVMTailCallKindTail,
@ -72,8 +51,6 @@ enum ZigLLVMTailCallKind {
ZigLLVMTailCallKindNoTail,
};
ZIG_EXTERN_C void ZigLLVMSetTailCallKind(LLVMValueRef Call, enum ZigLLVMTailCallKind TailCallKind);
enum ZigLLVM_CallingConv {
ZigLLVM_C = 0,
ZigLLVM_Fast = 8,
@ -122,176 +99,12 @@ enum ZigLLVM_CallingConv {
ZigLLVM_MaxID = 1023,
};
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildNSWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildNUWShl(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildLShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildAShrExact(LLVMBuilderRef builder, LLVMValueRef LHS, LLVMValueRef RHS,
const char *name);
ZIG_EXTERN_C LLVMValueRef ZigLLVMBuildAllocaInAddressSpace(LLVMBuilderRef builder, LLVMTypeRef Ty, unsigned AddressSpace,
const char *Name);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugPointerType(struct ZigLLVMDIBuilder *dibuilder,
struct ZigLLVMDIType *pointee_type, uint64_t size_in_bits, uint64_t align_in_bits, const char *name);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugBasicType(struct ZigLLVMDIBuilder *dibuilder, const char *name,
uint64_t size_in_bits, unsigned encoding);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugArrayType(struct ZigLLVMDIBuilder *dibuilder,
uint64_t size_in_bits, uint64_t align_in_bits, struct ZigLLVMDIType *elem_type,
int64_t elem_count);
ZIG_EXTERN_C struct ZigLLVMDIEnumerator *ZigLLVMCreateDebugEnumerator(struct ZigLLVMDIBuilder *dibuilder,
const char *name, uint64_t val, bool isUnsigned);
ZIG_EXTERN_C struct ZigLLVMDIEnumerator *ZigLLVMCreateDebugEnumeratorOfArbitraryPrecision(struct ZigLLVMDIBuilder *dibuilder,
const char *name, unsigned NumWords, const uint64_t Words[], unsigned int bits, bool isUnsigned);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugEnumerationType(struct ZigLLVMDIBuilder *dibuilder,
struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line_number,
uint64_t size_in_bits, uint64_t align_in_bits, struct ZigLLVMDIEnumerator **enumerator_array,
int enumerator_array_len, struct ZigLLVMDIType *underlying_type, const char *unique_id);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugStructType(struct ZigLLVMDIBuilder *dibuilder,
struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line_number,
uint64_t size_in_bits, uint64_t align_in_bits, unsigned flags, struct ZigLLVMDIType *derived_from,
struct ZigLLVMDIType **types_array, int types_array_len, unsigned run_time_lang,
struct ZigLLVMDIType *vtable_holder, const char *unique_id);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugUnionType(struct ZigLLVMDIBuilder *dibuilder,
struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line_number,
uint64_t size_in_bits, uint64_t align_in_bits, unsigned flags, struct ZigLLVMDIType **types_array,
int types_array_len, unsigned run_time_lang, const char *unique_id);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugMemberType(struct ZigLLVMDIBuilder *dibuilder,
struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line,
uint64_t size_in_bits, uint64_t align_in_bits, uint64_t offset_in_bits, unsigned flags,
struct ZigLLVMDIType *type);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateReplaceableCompositeType(struct ZigLLVMDIBuilder *dibuilder,
unsigned tag, const char *name, struct ZigLLVMDIScope *scope, struct ZigLLVMDIFile *file, unsigned line);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateDebugForwardDeclType(struct ZigLLVMDIBuilder *dibuilder, unsigned tag,
const char *name, struct ZigLLVMDIScope *scope, struct ZigLLVMDIFile *file, unsigned line);
ZIG_EXTERN_C void ZigLLVMReplaceTemporary(struct ZigLLVMDIBuilder *dibuilder, struct ZigLLVMDIType *type,
struct ZigLLVMDIType *replacement);
ZIG_EXTERN_C void ZigLLVMReplaceDebugArrays(struct ZigLLVMDIBuilder *dibuilder, struct ZigLLVMDIType *type,
struct ZigLLVMDIType **types_array, int types_array_len);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMCreateSubroutineType(struct ZigLLVMDIBuilder *dibuilder_wrapped,
struct ZigLLVMDIType **types_array, int types_array_len, unsigned flags);
ZIG_EXTERN_C unsigned ZigLLVMEncoding_DW_ATE_unsigned(void);
ZIG_EXTERN_C unsigned ZigLLVMEncoding_DW_ATE_signed(void);
ZIG_EXTERN_C unsigned ZigLLVMEncoding_DW_ATE_float(void);
ZIG_EXTERN_C unsigned ZigLLVMEncoding_DW_ATE_boolean(void);
ZIG_EXTERN_C unsigned ZigLLVMEncoding_DW_ATE_unsigned_char(void);
ZIG_EXTERN_C unsigned ZigLLVMEncoding_DW_ATE_signed_char(void);
ZIG_EXTERN_C unsigned ZigLLVMLang_DW_LANG_C99(void);
ZIG_EXTERN_C unsigned ZigLLVMTag_DW_variable(void);
ZIG_EXTERN_C unsigned ZigLLVMTag_DW_structure_type(void);
ZIG_EXTERN_C unsigned ZigLLVMTag_DW_enumeration_type(void);
ZIG_EXTERN_C unsigned ZigLLVMTag_DW_union_type(void);
ZIG_EXTERN_C struct ZigLLVMDIBuilder *ZigLLVMCreateDIBuilder(LLVMModuleRef module, bool allow_unresolved);
ZIG_EXTERN_C void ZigLLVMDisposeDIBuilder(struct ZigLLVMDIBuilder *dbuilder);
ZIG_EXTERN_C void ZigLLVMAddModuleDebugInfoFlag(LLVMModuleRef module, bool produce_dwarf64);
ZIG_EXTERN_C void ZigLLVMAddModuleCodeViewFlag(LLVMModuleRef module);
ZIG_EXTERN_C void ZigLLVMSetModulePICLevel(LLVMModuleRef module);
ZIG_EXTERN_C void ZigLLVMSetModulePIELevel(LLVMModuleRef module);
ZIG_EXTERN_C void ZigLLVMSetModuleCodeModel(LLVMModuleRef module, LLVMCodeModel code_model);
ZIG_EXTERN_C void ZigLLVMSetCurrentDebugLocation(LLVMBuilderRef builder,
unsigned int line, unsigned int column, struct ZigLLVMDIScope *scope);
ZIG_EXTERN_C void ZigLLVMSetCurrentDebugLocation2(LLVMBuilderRef builder, unsigned int line,
unsigned int column, struct ZigLLVMDIScope *scope, struct ZigLLVMDILocation *inlined_at);
ZIG_EXTERN_C void ZigLLVMClearCurrentDebugLocation(LLVMBuilderRef builder);
ZIG_EXTERN_C struct ZigLLVMDIScope *ZigLLVMLexicalBlockToScope(struct ZigLLVMDILexicalBlock *lexical_block);
ZIG_EXTERN_C struct ZigLLVMDIScope *ZigLLVMCompileUnitToScope(struct ZigLLVMDICompileUnit *compile_unit);
ZIG_EXTERN_C struct ZigLLVMDIScope *ZigLLVMFileToScope(struct ZigLLVMDIFile *difile);
ZIG_EXTERN_C struct ZigLLVMDIScope *ZigLLVMSubprogramToScope(struct ZigLLVMDISubprogram *subprogram);
ZIG_EXTERN_C struct ZigLLVMDIScope *ZigLLVMTypeToScope(struct ZigLLVMDIType *type);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMLexicalBlockToNode(struct ZigLLVMDILexicalBlock *lexical_block);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMCompileUnitToNode(struct ZigLLVMDICompileUnit *compile_unit);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMFileToNode(struct ZigLLVMDIFile *difile);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMSubprogramToNode(struct ZigLLVMDISubprogram *subprogram);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMTypeToNode(struct ZigLLVMDIType *type);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMScopeToNode(struct ZigLLVMDIScope *scope);
ZIG_EXTERN_C struct ZigLLVMDINode *ZigLLVMGlobalVariableToNode(struct ZigLLVMDIGlobalVariable *global_variable);
ZIG_EXTERN_C void ZigLLVMSubprogramReplaceLinkageName(struct ZigLLVMDISubprogram *subprogram,
struct ZigLLVMMDString *linkage_name);
ZIG_EXTERN_C void ZigLLVMGlobalVariableReplaceLinkageName(struct ZigLLVMDIGlobalVariable *global_variable,
struct ZigLLVMMDString *linkage_name);
ZIG_EXTERN_C struct ZigLLVMDILocalVariable *ZigLLVMCreateAutoVariable(struct ZigLLVMDIBuilder *dbuilder,
struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line_no,
struct ZigLLVMDIType *type, bool always_preserve, unsigned flags);
ZIG_EXTERN_C struct ZigLLVMDIGlobalVariableExpression *ZigLLVMCreateGlobalVariableExpression(struct ZigLLVMDIBuilder *dbuilder,
struct ZigLLVMDIScope *scope, const char *name, const char *linkage_name, struct ZigLLVMDIFile *file,
unsigned line_no, struct ZigLLVMDIType *di_type, bool is_local_to_unit);
ZIG_EXTERN_C struct ZigLLVMDILocalVariable *ZigLLVMCreateParameterVariable(struct ZigLLVMDIBuilder *dbuilder,
struct ZigLLVMDIScope *scope, const char *name, struct ZigLLVMDIFile *file, unsigned line_no,
struct ZigLLVMDIType *type, bool always_preserve, unsigned flags, unsigned arg_no);
ZIG_EXTERN_C struct ZigLLVMDILexicalBlock *ZigLLVMCreateLexicalBlock(struct ZigLLVMDIBuilder *dbuilder,
struct ZigLLVMDIScope *scope, struct ZigLLVMDIFile *file, unsigned line, unsigned col);
ZIG_EXTERN_C struct ZigLLVMDICompileUnit *ZigLLVMCreateCompileUnit(struct ZigLLVMDIBuilder *dibuilder,
unsigned lang, struct ZigLLVMDIFile *difile, const char *producer,
bool is_optimized, const char *flags, unsigned runtime_version, const char *split_name,
uint64_t dwo_id, bool emit_debug_info);
ZIG_EXTERN_C struct ZigLLVMDIFile *ZigLLVMCreateFile(struct ZigLLVMDIBuilder *dibuilder, const char *filename,
const char *directory);
ZIG_EXTERN_C struct ZigLLVMDISubprogram *ZigLLVMCreateFunction(struct ZigLLVMDIBuilder *dibuilder,
struct ZigLLVMDIScope *scope, const char *name, const char *linkage_name, struct ZigLLVMDIFile *file,
unsigned lineno, struct ZigLLVMDIType *fn_di_type, bool is_local_to_unit, bool is_definition,
unsigned scope_line, unsigned flags, bool is_optimized, struct ZigLLVMDISubprogram *decl_subprogram);
ZIG_EXTERN_C struct ZigLLVMDIType *ZigLLVMDIBuilderCreateVectorType(struct ZigLLVMDIBuilder *dibuilder,
uint64_t SizeInBits, uint32_t AlignInBits, struct ZigLLVMDIType *Ty, uint32_t elem_count);
ZIG_EXTERN_C void ZigLLVMFnSetSubprogram(LLVMValueRef fn, struct ZigLLVMDISubprogram *subprogram);
ZIG_EXTERN_C void ZigLLVMDIBuilderFinalize(struct ZigLLVMDIBuilder *dibuilder);
ZIG_EXTERN_C struct ZigLLVMDILocation *ZigLLVMGetDebugLoc(unsigned line, unsigned col,
struct ZigLLVMDIScope *scope);
ZIG_EXTERN_C struct ZigLLVMDILocation *ZigLLVMGetDebugLoc2(unsigned line, unsigned col,
struct ZigLLVMDIScope *scope, struct ZigLLVMDILocation *inlined_at);
ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDeclareAtEnd(struct ZigLLVMDIBuilder *dib,
LLVMValueRef storage, struct ZigLLVMDILocalVariable *var_info,
struct ZigLLVMDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref);
ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDeclare(struct ZigLLVMDIBuilder *dib,
LLVMValueRef storage, struct ZigLLVMDILocalVariable *var_info,
struct ZigLLVMDILocation *debug_loc, LLVMValueRef insert_before_instr);
ZIG_EXTERN_C LLVMValueRef ZigLLVMInsertDbgValueIntrinsicAtEnd(struct ZigLLVMDIBuilder *dib,
LLVMValueRef val, struct ZigLLVMDILocalVariable *var_info,
struct ZigLLVMDILocation *debug_loc, LLVMBasicBlockRef basic_block_ref);
ZIG_EXTERN_C void ZigLLVMSetFastMath(LLVMBuilderRef builder_wrapped, bool on_state);
ZIG_EXTERN_C void ZigLLVMParseCommandLineOptions(size_t argc, const char *const *argv);
ZIG_EXTERN_C ZigLLVMDIGlobalVariable* ZigLLVMGlobalGetVariable(ZigLLVMDIGlobalVariableExpression *global_variable_expression);
ZIG_EXTERN_C void ZigLLVMAttachMetaData(LLVMValueRef Val, ZigLLVMDIGlobalVariableExpression *global_variable_expression);
// synchronize with llvm/include/ADT/Triple.h::ArchType
// synchronize with std.Target.Cpu.Arch
// synchronize with codegen/llvm/bindings.zig::ArchType
@ -494,12 +307,6 @@ enum ZigLLVM_ObjectFormatType {
ZigLLVM_XCOFF,
};
ZIG_EXTERN_C void ZigLLVMTakeName(LLVMValueRef new_owner, LLVMValueRef victim);
ZIG_EXTERN_C void ZigLLVMRemoveGlobalValue(LLVMValueRef GlobalVal);
ZIG_EXTERN_C void ZigLLVMEraseGlobalValue(LLVMValueRef GlobalVal);
ZIG_EXTERN_C void ZigLLVMDeleteGlobalValue(LLVMValueRef GlobalVal);
ZIG_EXTERN_C void ZigLLVMSetInitializer(LLVMValueRef GlobalVar, LLVMValueRef ConstantVal);
#define ZigLLVM_DIFlags_Zero 0U
#define ZigLLVM_DIFlags_Private 1U
#define ZigLLVM_DIFlags_Protected 2U