authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-25 19:06:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-25 19:06:48-07:00
logc3d10dbda1fc133b7ca112787bbf0100b735ca36
tree9367d79b77d8910c1c538b84812c508e210ce88a
parent5b4885fe89939679ec2aa78f6bf16fe42faa781a

stage2 llvm backend: implement llvmType for error union and slices


2 files changed, 23 insertions(+), 4 deletions(-)

src/codegen/llvm.zig+17-3
...@@ -534,7 +534,14 @@ pub const DeclGen = struct {...@@ -534,7 +534,14 @@ pub const DeclGen = struct {
534 .Bool => return self.context.intType(1),534 .Bool => return self.context.intType(1),
535 .Pointer => {535 .Pointer => {
536 if (t.isSlice()) {536 if (t.isSlice()) {
537 return self.todo("implement slices", .{});537 var buf: Type.Payload.ElemType = undefined;
538 const ptr_type = t.slicePtrFieldType(&buf);
539
540 const fields: [2]*const llvm.Type = .{
541 try self.llvmType(ptr_type),
542 try self.llvmType(Type.initTag(.usize)),
543 };
544 return self.context.structType(&fields, 2, .False);
538 } else {545 } else {
539 const elem_type = try self.llvmType(t.elemType());546 const elem_type = try self.llvmType(t.elemType());
540 return elem_type.pointerType(0);547 return elem_type.pointerType(0);
...@@ -549,7 +556,7 @@ pub const DeclGen = struct {...@@ -549,7 +556,7 @@ pub const DeclGen = struct {
549 var buf: Type.Payload.ElemType = undefined;556 var buf: Type.Payload.ElemType = undefined;
550 const child_type = t.optionalChild(&buf);557 const child_type = t.optionalChild(&buf);
551558
552 var optional_types: [2]*const llvm.Type = .{559 const optional_types: [2]*const llvm.Type = .{
553 try self.llvmType(child_type),560 try self.llvmType(child_type),
554 self.context.intType(1),561 self.context.intType(1),
555 };562 };
...@@ -559,8 +566,16 @@ pub const DeclGen = struct {...@@ -559,8 +566,16 @@ pub const DeclGen = struct {
559 }566 }
560 },567 },
561 .ErrorUnion => {568 .ErrorUnion => {
569 const error_type = t.errorUnionSet();
570 const payload_type = t.errorUnionPayload();
571 if (!payload_type.hasCodeGenBits()) {
572 return self.llvmType(error_type);
573 }
562 return self.todo("implement llvmType for error unions", .{});574 return self.todo("implement llvmType for error unions", .{});
563 },575 },
576 .ErrorSet => {
577 return self.context.intType(16);
578 },
564 .ComptimeInt => unreachable,579 .ComptimeInt => unreachable,
565 .ComptimeFloat => unreachable,580 .ComptimeFloat => unreachable,
566 .Type => unreachable,581 .Type => unreachable,
...@@ -572,7 +587,6 @@ pub const DeclGen = struct {...@@ -572,7 +587,6 @@ pub const DeclGen = struct {
572587
573 .Float,588 .Float,
574 .Struct,589 .Struct,
575 .ErrorSet,
576 .Enum,590 .Enum,
577 .Union,591 .Union,
578 .Fn,592 .Fn,
src/codegen/llvm/bindings.zig+6-1
...@@ -35,7 +35,12 @@ pub const Context = opaque {...@@ -35,7 +35,12 @@ pub const Context = opaque {
35 extern fn LLVMVoidTypeInContext(C: *const Context) *const Type;35 extern fn LLVMVoidTypeInContext(C: *const Context) *const Type;
3636
37 pub const structType = LLVMStructTypeInContext;37 pub const structType = LLVMStructTypeInContext;
38 extern fn LLVMStructTypeInContext(C: *const Context, ElementTypes: [*]*const Type, ElementCount: c_uint, Packed: Bool) *const Type;38 extern fn LLVMStructTypeInContext(
39 C: *const Context,
40 ElementTypes: [*]const *const Type,
41 ElementCount: c_uint,
42 Packed: Bool,
43 ) *const Type;
3944
40 pub const constString = LLVMConstStringInContext;45 pub const constString = LLVMConstStringInContext;
41 extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) *const Value;46 extern fn LLVMConstStringInContext(C: *const Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) *const Value;