From 11a60e87791c13d89d770c7925c744b5fe75e5c4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 26 Oct 2021 16:42:44 -0700 Subject: stage2 LLVM backend: fix bitcast Properly handle when the operand type, the result type, or both, are by-ref values. --- src/codegen/llvm.zig | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index fd92dec413..9c2fd22df0 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -3201,14 +3201,23 @@ pub const FuncGen = struct { const operand = try self.resolveInst(ty_op.operand); const operand_ty = self.air.typeOf(ty_op.operand); const inst_ty = self.air.typeOfIndex(inst); + const operand_is_ref = isByRef(operand_ty); + const result_is_ref = isByRef(inst_ty); const llvm_dest_ty = try self.dg.llvmType(inst_ty); + if (operand_is_ref and result_is_ref) { + // They are both pointers; just do a bitcast on the pointers :) + return self.builder.buildBitCast(operand, llvm_dest_ty.pointerType(0), ""); + } + if (operand_ty.zigTypeTag() == .Int and inst_ty.zigTypeTag() == .Pointer) { return self.builder.buildIntToPtr(operand, llvm_dest_ty, ""); - } else if (operand_ty.zigTypeTag() == .Vector and inst_ty.zigTypeTag() == .Array) { + } + + if (operand_ty.zigTypeTag() == .Vector and inst_ty.zigTypeTag() == .Array) { const target = self.dg.module.getTarget(); const elem_ty = operand_ty.childType(); - if (!isByRef(inst_ty)) { + if (!result_is_ref) { return self.dg.todo("implement bitcast vector to non-ref array", .{}); } const array_ptr = self.buildAlloca(llvm_dest_ty); @@ -3239,7 +3248,7 @@ pub const FuncGen = struct { const target = self.dg.module.getTarget(); const elem_ty = operand_ty.childType(); const llvm_vector_ty = try self.dg.llvmType(inst_ty); - if (!isByRef(operand_ty)) { + if (!operand_is_ref) { return self.dg.todo("implement bitcast non-ref array to vector", .{}); } @@ -3274,6 +3283,21 @@ pub const FuncGen = struct { } } + if (operand_is_ref) { + // Bitcast the operand pointer, then load. + const casted_ptr = self.builder.buildBitCast(operand, llvm_dest_ty.pointerType(0), ""); + return self.builder.buildLoad(casted_ptr, ""); + } + + if (result_is_ref) { + // Bitcast the result pointer, then store. + const result_ptr = self.buildAlloca(llvm_dest_ty); + const operand_llvm_ty = try self.dg.llvmType(operand_ty); + const casted_ptr = self.builder.buildBitCast(result_ptr, operand_llvm_ty.pointerType(0), ""); + _ = self.builder.buildStore(operand, casted_ptr); + return result_ptr; + } + return self.builder.buildBitCast(operand, llvm_dest_ty, ""); } -- cgit v1.2.3