authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-07 23:50:40-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-08-08 21:32:50-04:00
log2499d8fb73b943640cbc7d0484377fffbee403c6
tree6cc4ea7466da93418239047de40de236490a7562
parenta66cd54f94cf675a67f6de594e3d59e7ca14b92f

Builder: fix enough bugs to pass the behavior tests

without using any information from the LLVM API

2 files changed, 42 insertions(+), 7 deletions(-)

src/codegen/llvm.zig+2-2
...@@ -6243,7 +6243,7 @@ pub const FuncGen = struct {...@@ -6243,7 +6243,7 @@ pub const FuncGen = struct {
6243 if (elem_ptr.ptrInfo(mod).flags.vector_index != .none) return base_ptr;6243 if (elem_ptr.ptrInfo(mod).flags.vector_index != .none) return base_ptr;
62446244
6245 const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty);6245 const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty);
6246 return try self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, if (ptr_ty.isSinglePointer(mod))6246 return self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, if (ptr_ty.isSinglePointer(mod))
6247 // If this is a single-item pointer to an array, we need another index in the GEP.6247 // If this is a single-item pointer to an array, we need another index in the GEP.
6248 &.{ try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs }6248 &.{ try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs }
6249 else6249 else
...@@ -10532,7 +10532,7 @@ pub const FuncGen = struct {...@@ -10532,7 +10532,7 @@ pub const FuncGen = struct {
10532 else => unreachable,10532 else => unreachable,
10533 };10533 };
1053410534
10535 return try fg.wip.callAsm(10535 return fg.wip.callAsm(
10536 .none,10536 .none,
10537 try o.builder.fnType(llvm_usize, &.{ llvm_usize, llvm_usize }, .normal),10537 try o.builder.fnType(llvm_usize, &.{ llvm_usize, llvm_usize }, .normal),
10538 .{ .sideeffect = true },10538 .{ .sideeffect = true },
src/codegen/llvm/Builder.zig+40-5
...@@ -5723,7 +5723,16 @@ pub const WipFunction = struct {...@@ -5723,7 +5723,16 @@ pub const WipFunction = struct {
5723 .void => null,5723 .void => null,
5724 else => name,5724 else => name,
5725 }, .{5725 }, .{
5726 .tag = .call,5726 .tag = switch (kind) {
5727 .normal => .call,
5728 .fast => .@"call fast",
5729 .musttail => .@"musttail call",
5730 .musttail_fast => .@"musttail call fast",
5731 .notail => .@"notail call",
5732 .notail_fast => .@"notail call fast",
5733 .tail => .@"tail call",
5734 .tail_fast => .@"tail call fast",
5735 },
5727 .data = self.addExtraAssumeCapacity(Instruction.Call{5736 .data = self.addExtraAssumeCapacity(Instruction.Call{
5728 .info = .{ .call_conv = call_conv },5737 .info = .{ .call_conv = call_conv },
5729 .attributes = function_attributes,5738 .attributes = function_attributes,
...@@ -7312,9 +7321,34 @@ pub const Constant = enum(u32) {...@@ -7312,9 +7321,34 @@ pub const Constant = enum(u32) {
7312 .bfloat => 16,7321 .bfloat => 16,
7313 else => unreachable,7322 else => unreachable,
7314 } }),7323 } }),
7315 .float => try writer.print("0x{X:0>16}", .{7324 .float => {
7316 @as(u64, @bitCast(@as(f64, @as(f32, @bitCast(item.data))))),7325 const Float = struct {
7317 }),7326 fn Repr(comptime T: type) type {
7327 return packed struct(std.meta.Int(.unsigned, @bitSizeOf(T))) {
7328 mantissa: std.meta.Int(.unsigned, std.math.floatMantissaBits(T)),
7329 exponent: std.meta.Int(.unsigned, std.math.floatExponentBits(T)),
7330 sign: u1,
7331 };
7332 }
7333 };
7334 const Exponent32 = std.meta.FieldType(Float.Repr(f32), .exponent);
7335 const Exponent64 = std.meta.FieldType(Float.Repr(f64), .exponent);
7336 const repr: Float.Repr(f32) = @bitCast(item.data);
7337 try writer.print("0x{X:0>16}", .{@as(u64, @bitCast(Float.Repr(f64){
7338 .mantissa = std.math.shl(
7339 std.meta.FieldType(Float.Repr(f64), .mantissa),
7340 repr.mantissa,
7341 std.math.floatMantissaBits(f64) - std.math.floatMantissaBits(f32),
7342 ),
7343 .exponent = switch (repr.exponent) {
7344 std.math.minInt(Exponent32) => std.math.minInt(Exponent64),
7345 else => @as(Exponent64, repr.exponent) +
7346 (std.math.floatExponentMax(f64) - std.math.floatExponentMax(f32)),
7347 std.math.maxInt(Exponent32) => std.math.maxInt(Exponent64),
7348 },
7349 .sign = repr.sign,
7350 }))});
7351 },
7318 .double => {7352 .double => {
7319 const extra = data.builder.constantExtraData(Double, item.data);7353 const extra = data.builder.constantExtraData(Double, item.data);
7320 try writer.print("0x{X:0>8}{X:0>8}", .{ extra.hi, extra.lo });7354 try writer.print("0x{X:0>8}{X:0>8}", .{ extra.hi, extra.lo });
...@@ -8361,6 +8395,7 @@ pub fn getIntrinsic(...@@ -8361,6 +8395,7 @@ pub fn getIntrinsic(
83618395
8362 var overload_index: usize = 0;8396 var overload_index: usize = 0;
8363 function_attributes[FunctionAttributes.function_index] = try attributes.get(signature.attrs);8397 function_attributes[FunctionAttributes.function_index] = try attributes.get(signature.attrs);
8398 function_attributes[FunctionAttributes.return_index] = .none; // needed for void return
8364 for (0.., param_types, signature.params) |param_index, *param_type, signature_param| {8399 for (0.., param_types, signature.params) |param_index, *param_type, signature_param| {
8365 switch (signature_param.kind) {8400 switch (signature_param.kind) {
8366 .type => |ty| param_type.* = ty,8401 .type => |ty| param_type.* = ty,
...@@ -9573,7 +9608,7 @@ fn fnTypeAssumeCapacity(...@@ -9573,7 +9608,7 @@ fn fnTypeAssumeCapacity(
9573 gop.key_ptr.* = {};9608 gop.key_ptr.* = {};
9574 gop.value_ptr.* = {};9609 gop.value_ptr.* = {};
9575 self.type_items.appendAssumeCapacity(.{9610 self.type_items.appendAssumeCapacity(.{
9576 .tag = .function,9611 .tag = tag,
9577 .data = self.addTypeExtraAssumeCapacity(Type.Function{9612 .data = self.addTypeExtraAssumeCapacity(Type.Function{
9578 .ret = ret,9613 .ret = ret,
9579 .params_len = @intCast(params.len),9614 .params_len = @intCast(params.len),