From c3d10dbda1fc133b7ca112787bbf0100b735ca36 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 25 Jul 2021 19:06:48 -0700 Subject: [PATCH] stage2 llvm backend: implement llvmType for error union and slices --- src/codegen/llvm.zig | 20 +++++++++++++++++--- src/codegen/llvm/bindings.zig | 7 ++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index b6855409b0..d6a4449616 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -534,7 +534,14 @@ pub const DeclGen = struct { .Bool => return self.context.intType(1), .Pointer => { if (t.isSlice()) { - return self.todo("implement slices", .{}); + var buf: Type.Payload.ElemType = undefined; + const ptr_type = t.slicePtrFieldType(&buf); + + const fields: [2]*const llvm.Type = .{ + try self.llvmType(ptr_type), + try self.llvmType(Type.initTag(.usize)), + }; + return self.context.structType(&fields, 2, .False); } else { const elem_type = try self.llvmType(t.elemType()); return elem_type.pointerType(0); @@ -549,7 +556,7 @@ pub const DeclGen = struct { var buf: Type.Payload.ElemType = undefined; const child_type = t.optionalChild(&buf); - var optional_types: [2]*const llvm.Type = .{ + const optional_types: [2]*const llvm.Type = .{ try self.llvmType(child_type), self.context.intType(1), }; @@ -559,8 +566,16 @@ pub const DeclGen = struct { } }, .ErrorUnion => { + const error_type = t.errorUnionSet(); + const payload_type = t.errorUnionPayload(); + if (!payload_type.hasCodeGenBits()) { + return self.llvmType(error_type); + } return self.todo("implement llvmType for error unions", .{}); }, + .ErrorSet => { + return self.context.intType(16); + }, .ComptimeInt => unreachable, .ComptimeFloat => unreachable, .Type => unreachable, @@ -572,7 +587,6 @@ pub const DeclGen = struct { .Float, .Struct, - .ErrorSet, .Enum, .Union, .Fn, diff --git a/src/codegen/llvm/bindings.zig b/src/codegen/llvm/bindings.zig index 59c5b3dfe3..3d17172cfd 100644 --- a/src/codegen/llvm/bindings.zig +++ b/src/codegen/llvm/bindings.zig @@ -35,7 +35,12 @@ 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: Bool) *const Type; + extern fn LLVMStructTypeInContext( + C: *const Context, + ElementTypes: [*]const *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: Bool) *const Value;