diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-08-05 23:32:42 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-05 23:32:42 -0700 |
| commit | ea7bdeb67d474526732b117992971603e4065f98 (patch) | |
| tree | 5dcd7d8c1cdd311cb40505d986c8fbceab52dfc9 /src/codegen/llvm.zig | |
| parent | 9fd3aeb8088cd9a3b0744d5f508ca256a2bbf19f (diff) | |
| parent | 7e9b23e6dce4d87615acd635f3731731a8601d39 (diff) | |
| download | zig-ea7bdeb67d474526732b117992971603e4065f98.tar.gz zig-ea7bdeb67d474526732b117992971603e4065f98.zip | |
Merge pull request #9517 from ziglang/generic-functions
stage2 generic functions
Diffstat (limited to 'src/codegen/llvm.zig')
| -rw-r--r-- | src/codegen/llvm.zig | 50 |
1 files changed, 44 insertions, 6 deletions
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 8b3edcfe23..91b48ffcfb 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -575,6 +575,14 @@ pub const DeclGen = struct { const info = t.intInfo(self.module.getTarget()); return self.context.intType(info.bits); }, + .Float => switch (t.floatBits(self.module.getTarget())) { + 16 => return self.context.halfType(), + 32 => return self.context.floatType(), + 64 => return self.context.doubleType(), + 80 => return self.context.x86FP80Type(), + 128 => return self.context.fp128Type(), + else => unreachable, + }, .Bool => return self.context.intType(1), .Pointer => { if (t.isSlice()) { @@ -661,7 +669,6 @@ pub const DeclGen = struct { .BoundFn => @panic("TODO remove BoundFn from the language"), - .Float, .Enum, .Union, .Opaque, @@ -699,13 +706,40 @@ pub const DeclGen = struct { } return llvm_int; }, + .Float => { + if (tv.ty.floatBits(self.module.getTarget()) <= 64) { + const llvm_ty = try self.llvmType(tv.ty); + return llvm_ty.constReal(tv.val.toFloat(f64)); + } + return self.todo("bitcast to f128 from an integer", .{}); + }, .Pointer => switch (tv.val.tag()) { .decl_ref => { - const decl = tv.val.castTag(.decl_ref).?.data; - decl.alive = true; - const val = try self.resolveGlobalDecl(decl); - const llvm_type = try self.llvmType(tv.ty); - return val.constBitCast(llvm_type); + if (tv.ty.isSlice()) { + var buf: Type.Payload.ElemType = undefined; + const ptr_ty = tv.ty.slicePtrFieldType(&buf); + var slice_len: Value.Payload.U64 = .{ + .base = .{ .tag = .int_u64 }, + .data = tv.val.sliceLen(), + }; + const fields: [2]*const llvm.Value = .{ + try self.genTypedValue(.{ + .ty = ptr_ty, + .val = tv.val, + }), + try self.genTypedValue(.{ + .ty = Type.initTag(.usize), + .val = Value.initPayload(&slice_len.base), + }), + }; + return self.context.constStruct(&fields, fields.len, .False); + } else { + const decl = tv.val.castTag(.decl_ref).?.data; + decl.alive = true; + const val = try self.resolveGlobalDecl(decl); + const llvm_type = try self.llvmType(tv.ty); + return val.constBitCast(llvm_type); + } }, .variable => { const decl = tv.val.castTag(.variable).?.data.owner_decl; @@ -839,6 +873,10 @@ pub const DeclGen = struct { .False, ); }, + .ComptimeInt => unreachable, + .ComptimeFloat => unreachable, + .Type => unreachable, + .EnumLiteral => unreachable, else => return self.todo("implement const of type '{}'", .{tv.ty}), } } |
