From 277b01a089e059d63b6a01de011b666cc0702c38 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sun, 14 Mar 2021 22:32:26 -0600 Subject: stage2 llvm bindings: rename LLVMBool to Bool --- src/codegen/llvm/bindings.zig | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig index ccba3d9973..ff385efd08 100644 --- a/src/codegen/llvm/bindings.zig +++ b/src/codegen/llvm/bindings.zig @@ -1,7 +1,7 @@ //! We do this instead of @cImport because the self-hosted compiler is easier //! to bootstrap if it does not depend on translate-c. -const LLVMBool = bool; +const Bool = bool; pub const AttributeIndex = c_uint; /// Make sure to use the *InContext functions instead of the global ones. @@ -22,13 +22,13 @@ pub const Context = opaque { extern fn LLVMVoidTypeInContext(C: *const Context) *const Type; pub const structType = LLVMStructTypeInContext; - extern fn LLVMStructTypeInContext(C: *const Context, ElementTypes: [*]*const Type, ElementCount: c_uint, Packed: LLVMBool) *const Type; + extern fn LLVMStructTypeInContext(C: *const Context, ElementTypes: [*]*const Type, ElementCount: c_uint, Packed: Bool) *const Type; pub const constString = LLVMConstStringInContext; - extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: LLVMBool) *const Value; + extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) *const Value; pub const constStruct = LLVMConstStructInContext; - extern fn LLVMConstStructInContext(C: *const Context, ConstantVals: [*]*const Value, Count: c_uint, Packed: LLVMBool) *const Value; + extern fn LLVMConstStructInContext(C: *const Context, ConstantVals: [*]*const Value, Count: c_uint, Packed: Bool) *const Value; pub const createBasicBlock = LLVMCreateBasicBlockInContext; extern fn LLVMCreateBasicBlockInContext(C: *const Context, Name: [*:0]const u8) *const BasicBlock; @@ -59,7 +59,7 @@ pub const Value = opaque { pub const Type = opaque { pub const functionType = LLVMFunctionType; - extern fn LLVMFunctionType(ReturnType: *const Type, ParamTypes: ?[*]*const Type, ParamCount: c_uint, IsVarArg: LLVMBool) *const Type; + extern fn LLVMFunctionType(ReturnType: *const Type, ParamTypes: ?[*]*const Type, ParamCount: c_uint, IsVarArg: Bool) *const Type; pub const constNull = LLVMConstNull; extern fn LLVMConstNull(Ty: *const Type) *const Value; @@ -68,7 +68,7 @@ pub const Type = opaque { extern fn LLVMConstAllOnes(Ty: *const Type) *const Value; pub const constInt = LLVMConstInt; - extern fn LLVMConstInt(IntTy: *const Type, N: c_ulonglong, SignExtend: LLVMBool) *const Value; + extern fn LLVMConstInt(IntTy: *const Type, N: c_ulonglong, SignExtend: Bool) *const Value; pub const constArray = LLVMConstArray; extern fn LLVMConstArray(ElementTy: *const Type, ConstantVals: ?[*]*const Value, Length: c_uint) *const Value; @@ -91,7 +91,7 @@ pub const Module = opaque { extern fn LLVMDisposeModule(*const Module) void; pub const verify = LLVMVerifyModule; - extern fn LLVMVerifyModule(*const Module, Action: VerifierFailureAction, OutMessage: *[*:0]const u8) LLVMBool; + extern fn LLVMVerifyModule(*const Module, Action: VerifierFailureAction, OutMessage: *[*:0]const u8) Bool; pub const addFunction = LLVMAddFunction; extern fn LLVMAddFunction(*const Module, Name: [*:0]const u8, FunctionTy: *const Type) *const Value; @@ -191,7 +191,7 @@ pub const Builder = opaque { extern fn LLVMBuildNUWSub(*const Builder, LHS: *const Value, RHS: *const Value, Name: [*:0]const u8) *const Value; pub const buildIntCast2 = LLVMBuildIntCast2; - extern fn LLVMBuildIntCast2(*const Builder, Val: *const Value, DestTy: *const Type, IsSigned: LLVMBool, Name: [*:0]const u8) *const Value; + extern fn LLVMBuildIntCast2(*const Builder, Val: *const Value, DestTy: *const Type, IsSigned: Bool, Name: [*:0]const u8) *const Value; pub const buildBitCast = LLVMBuildBitCast; extern fn LLVMBuildBitCast(*const Builder, Val: *const Value, DestTy: *const Type, Name: [*:0]const u8) *const Value; @@ -258,7 +258,7 @@ pub const TargetMachine = opaque { Filename: [*:0]const u8, codegen: CodeGenFileType, ErrorMessage: *[*:0]const u8, - ) LLVMBool; + ) Bool; }; pub const CodeMode = extern enum { @@ -295,7 +295,7 @@ pub const CodeGenFileType = extern enum { pub const Target = opaque { pub const getFromTriple = LLVMGetTargetFromTriple; - extern fn LLVMGetTargetFromTriple(Triple: [*:0]const u8, T: **const Target, ErrorMessage: *[*:0]const u8) LLVMBool; + extern fn LLVMGetTargetFromTriple(Triple: [*:0]const u8, T: **const Target, ErrorMessage: *[*:0]const u8) Bool; }; extern fn LLVMInitializeAArch64TargetInfo() void; -- cgit v1.2.3 From e8aa6f90d6e8c69f3af32942d0ae2bdf44e34783 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sun, 14 Mar 2021 22:43:03 -0600 Subject: stage2 llvm bindings: use correct type for LLVMBool for ABI compat --- src/codegen/llvm.zig | 22 +++++++++++----------- src/codegen/llvm/bindings.zig | 15 ++++++++++++++- 2 files changed, 25 insertions(+), 12 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index f087957f1d..7233dbdd07 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -219,7 +219,7 @@ pub const LLVMIRModule = struct { var error_message: [*:0]const u8 = undefined; var target: *const llvm.Target = undefined; - if (llvm.Target.getFromTriple(llvm_target_triple.ptr, &target, &error_message)) { + if (llvm.Target.getFromTriple(llvm_target_triple.ptr, &target, &error_message).toBool()) { defer llvm.disposeMessage(error_message); const stderr = std.io.getStdErr().writer(); @@ -303,7 +303,7 @@ pub const LLVMIRModule = struct { // verifyModule always allocs the error_message even if there is no error defer llvm.disposeMessage(error_message); - if (self.llvm_module.verify(.ReturnStatus, &error_message)) { + if (self.llvm_module.verify(.ReturnStatus, &error_message).toBool()) { const stderr = std.io.getStdErr().writer(); try stderr.print("broken LLVM module found: {s}\nThis is a bug in the Zig compiler.", .{error_message}); return error.BrokenLLVMModule; @@ -319,7 +319,7 @@ pub const LLVMIRModule = struct { object_pathZ.ptr, .ObjectFile, &error_message, - )) { + ).toBool()) { defer llvm.disposeMessage(error_message); const stderr = std.io.getStdErr().writer(); @@ -614,7 +614,7 @@ pub const LLVMIRModule = struct { var indices: [2]*const llvm.Value = .{ index_type.constNull(), - index_type.constInt(1, false), + index_type.constInt(1, .False), }; return self.builder.buildLoad(self.builder.buildInBoundsGEP(operand, &indices, 2, ""), ""); @@ -676,7 +676,7 @@ pub const LLVMIRModule = struct { const signed = inst.base.ty.isSignedInt(); // TODO: Should we use intcast here or just a simple bitcast? // LLVM does truncation vs bitcast (+signed extension) in the intcast depending on the sizes - return self.builder.buildIntCast2(val, try self.getLLVMType(inst.base.ty, inst.base.src), signed, ""); + return self.builder.buildIntCast2(val, try self.getLLVMType(inst.base.ty, inst.base.src), llvm.Bool.fromBool(signed), ""); } fn genBitCast(self: *LLVMIRModule, inst: *Inst.UnOp) !?*const llvm.Value { @@ -782,7 +782,7 @@ pub const LLVMIRModule = struct { if (bigint.limbs.len != 1) { return self.fail(src, "TODO implement bigger bigint", .{}); } - const llvm_int = llvm_type.constInt(bigint.limbs[0], false); + const llvm_int = llvm_type.constInt(bigint.limbs[0], .False); if (!bigint.positive) { return llvm.constNeg(llvm_int); } @@ -820,7 +820,7 @@ pub const LLVMIRModule = struct { return self.fail(src, "TODO handle other sentinel values", .{}); } else false; - return self.context.constString(payload.data.ptr, @intCast(c_uint, payload.data.len), !zero_sentinel); + return self.context.constString(payload.data.ptr, @intCast(c_uint, payload.data.len), llvm.Bool.fromBool(!zero_sentinel)); } else { return self.fail(src, "TODO handle more array values", .{}); } @@ -836,13 +836,13 @@ pub const LLVMIRModule = struct { llvm_child_type.constNull(), self.context.intType(1).constNull(), }; - return self.context.constStruct(&optional_values, 2, false); + return self.context.constStruct(&optional_values, 2, .False); } else { var optional_values: [2]*const llvm.Value = .{ try self.genTypedValue(src, .{ .ty = child_type, .val = tv.val }), self.context.intType(1).constAllOnes(), }; - return self.context.constStruct(&optional_values, 2, false); + return self.context.constStruct(&optional_values, 2, .False); } } else { return self.fail(src, "TODO implement const of optional pointer", .{}); @@ -882,7 +882,7 @@ pub const LLVMIRModule = struct { try self.getLLVMType(child_type, src), self.context.intType(1), }; - return self.context.structType(&optional_types, 2, false); + return self.context.structType(&optional_types, 2, .False); } else { return self.fail(src, "TODO implement optional pointers as actual pointers", .{}); } @@ -934,7 +934,7 @@ pub const LLVMIRModule = struct { try self.getLLVMType(return_type, src), if (fn_param_len == 0) null else llvm_param.ptr, @intCast(c_uint, fn_param_len), - false, + .False, ); const llvm_fn = self.llvm_module.addFunction(func.name, fn_type); diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig index ff385efd08..7217ca381e 100644 --- a/src/codegen/llvm/bindings.zig +++ b/src/codegen/llvm/bindings.zig @@ -1,7 +1,20 @@ //! We do this instead of @cImport because the self-hosted compiler is easier //! to bootstrap if it does not depend on translate-c. -const Bool = bool; +/// Do not compare directly to .True, use toBool() instead. +pub const Bool = enum(c_int) { + False, + True, + _, + + pub fn fromBool(b: bool) Bool { + return @intToEnum(Bool, @boolToInt(b)); + } + + pub fn toBool(b: Bool) bool { + return b != .False; + } +}; pub const AttributeIndex = c_uint; /// Make sure to use the *InContext functions instead of the global ones. -- cgit v1.2.3