authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2020-07-24 17:51:24+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2020-07-24 17:51:24+02:00
log3019ab93917098c94fb7507c1aadeee330e5982a
tree1dc72ad2bd33a893e98a18c4da70b886e7ce8913
parent470264a4f57bf667553678cd51d8c70945b96fe4
signaturelock-open Commit is signed but in an unrecognized format.

Fix resolvepeertype() int signess and feedback improvements


1 files changed, 22 insertions(+), 17 deletions(-)

src-self-hosted/Module.zig+22-17
......@@ -3087,7 +3087,7 @@ fn analyzeInstArithmetic(self: *Module, scope: *Scope, inst: *zir.Inst.BinOp) In
30873087
30883088 if (casted_lhs.value()) |lhs_val| {
30893089 if (casted_rhs.value()) |rhs_val| {
3090 return self.analyzeInstScalar(scope, scalar_type, inst, lhs_val, rhs_val);
3090 return self.analyzeInstComptimeOp(scope, scalar_type, inst, lhs_val, rhs_val);
30913091 }
30923092 }
30933093
......@@ -3102,7 +3102,7 @@ fn analyzeInstArithmetic(self: *Module, scope: *Scope, inst: *zir.Inst.BinOp) In
31023102}
31033103
31043104/// Analyzes operands that are known at comptime
3105fn analyzeInstScalar(self: *Module, scope: *Scope, res_type: Type, inst: *zir.Inst.BinOp, lhs_val: Value, rhs_val: Value) InnerError!*Inst {
3105fn analyzeInstComptimeOp(self: *Module, scope: *Scope, res_type: Type, inst: *zir.Inst.BinOp, lhs_val: Value, rhs_val: Value) InnerError!*Inst {
31063106 // incase rhs is 0, simply return lhs without doing any calculations
31073107 // TODO Once division is implemented we should throw an error when dividing by 0.
31083108 if (rhs_val.tag() == .zero or rhs_val.tag() == .the_one_possible_value) {
......@@ -3116,16 +3116,16 @@ fn analyzeInstScalar(self: *Module, scope: *Scope, res_type: Type, inst: *zir.In
31163116 const value = try switch (inst.base.tag) {
31173117 .add => blk: {
31183118 const val = if (is_int)
3119 bigIntAdd(scope.arena(), lhs_val, rhs_val)
3119 intAdd(scope.arena(), lhs_val, rhs_val)
31203120 else
3121 floatAdd(self.target(), scope.arena(), res_type, lhs_val, rhs_val);
3121 self.floatAdd(scope, res_type, inst, lhs_val, rhs_val);
31223122 break :blk val;
31233123 },
31243124 .sub => blk: {
31253125 const val = if (is_int)
3126 bigIntSub(scope.arena(), lhs_val, rhs_val)
3126 intSub(scope.arena(), lhs_val, rhs_val)
31273127 else
3128 floatSub(self.target(), scope.arena(), res_type, lhs_val, rhs_val);
3128 self.floatSub(scope, res_type, inst, lhs_val, rhs_val);
31293129 break :blk val;
31303130 },
31313131 else => return self.fail(scope, inst.base.src, "TODO Implement arithmetic operand '{}'", .{@tagName(inst.base.tag)}),
......@@ -3556,7 +3556,10 @@ fn resolvePeerTypes(self: *Module, scope: *Scope, instructions: []*Inst) !Type {
35563556 prev_inst = next_inst;
35573557 continue;
35583558 }
3559 if (prev_inst.ty.isInt() and next_inst.ty.isInt()) {
3559 if (prev_inst.ty.isInt() and
3560 next_inst.ty.isInt() and
3561 prev_inst.ty.isSignedInt() == next_inst.ty.isSignedInt())
3562 {
35603563 if (prev_inst.ty.intInfo(self.target()).bits < next_inst.ty.intInfo(self.target()).bits) {
35613564 prev_inst = next_inst;
35623565 }
......@@ -3819,7 +3822,7 @@ fn srcHashEql(a: std.zig.SrcHash, b: std.zig.SrcHash) bool {
38193822 return @bitCast(u128, a) == @bitCast(u128, b);
38203823}
38213824
3822fn bigIntAdd(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
3825fn intAdd(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
38233826 // TODO is this a performance issue? maybe we should try the operation without
38243827 // resorting to BigInt first.
38253828 var lhs_space: Value.BigIntSpace = undefined;
......@@ -3847,7 +3850,7 @@ fn bigIntAdd(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
38473850 return Value.initPayload(val_payload);
38483851}
38493852
3850fn bigIntSub(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
3853fn intSub(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
38513854 // TODO is this a performance issue? maybe we should try the operation without
38523855 // resorting to BigInt first.
38533856 var lhs_space: Value.BigIntSpace = undefined;
......@@ -3875,15 +3878,16 @@ fn bigIntSub(allocator: *Allocator, lhs: Value, rhs: Value) !Value {
38753878 return Value.initPayload(val_payload);
38763879}
38773880
3878fn floatAdd(cur_target: Target, allocator: *Allocator, float_type: Type, lhs: Value, rhs: Value) !Value {
3881fn floatAdd(self: *Module, scope: *Scope, float_type: Type, inst: *zir.Inst.BinOp, lhs: Value, rhs: Value) !Value {
38793882 var bit_count = switch (float_type.tag()) {
38803883 .comptime_float => 128,
3881 else => float_type.floatBits(cur_target),
3884 else => float_type.floatBits(self.target()),
38823885 };
38833886
3887 const allocator = scope.arena();
38843888 const val_payload = switch (bit_count) {
38853889 16 => {
3886 @panic("TODO soft float");
3890 return self.fail(scope, inst.base.src, "TODO Implement addition for soft floats", .{});
38873891 },
38883892 32 => blk: {
38893893 const lhs_val = lhs.toFloat(f32);
......@@ -3900,7 +3904,7 @@ fn floatAdd(cur_target: Target, allocator: *Allocator, float_type: Type, lhs: Va
39003904 break :blk &val_payload.base;
39013905 },
39023906 128 => blk: {
3903 @panic("TODO Big float");
3907 return self.fail(scope, inst.base.src, "TODO Implement addition for big floats", .{});
39043908 },
39053909 else => unreachable,
39063910 };
......@@ -3908,15 +3912,16 @@ fn floatAdd(cur_target: Target, allocator: *Allocator, float_type: Type, lhs: Va
39083912 return Value.initPayload(val_payload);
39093913}
39103914
3911fn floatSub(cur_target: Target, allocator: *Allocator, float_type: Type, lhs: Value, rhs: Value) !Value {
3915fn floatSub(self: *Module, scope: *Scope, float_type: Type, inst: *zir.Inst.BinOp, lhs: Value, rhs: Value) !Value {
39123916 var bit_count = switch (float_type.tag()) {
39133917 .comptime_float => 128,
3914 else => float_type.floatBits(cur_target),
3918 else => float_type.floatBits(self.target()),
39153919 };
39163920
3921 const allocator = scope.arena();
39173922 const val_payload = switch (bit_count) {
39183923 16 => {
3919 @panic("TODO soft float");
3924 return self.fail(scope, inst.base.src, "TODO Implement substraction for soft floats", .{});
39203925 },
39213926 32 => blk: {
39223927 const lhs_val = lhs.toFloat(f32);
......@@ -3933,7 +3938,7 @@ fn floatSub(cur_target: Target, allocator: *Allocator, float_type: Type, lhs: Va
39333938 break :blk &val_payload.base;
39343939 },
39353940 128 => blk: {
3936 @panic("TODO Big float");
3941 return self.fail(scope, inst.base.src, "TODO Implement substraction for big floats", .{});
39373942 },
39383943 else => unreachable,
39393944 };