| ... | @@ -19,6 +19,7 @@ const Zir = @import("../Zir.zig"); | ... | @@ -19,6 +19,7 @@ const Zir = @import("../Zir.zig"); |
| 19 | const Liveness = @import("../Liveness.zig"); | 19 | const Liveness = @import("../Liveness.zig"); |
| 20 | | 20 | |
| 21 | const Mutability = enum { Const, Mut }; | 21 | const Mutability = enum { Const, Mut }; |
| | 22 | const BigIntConst = std.math.big.int.Const; |
| 22 | | 23 | |
| 23 | pub const CValue = union(enum) { | 24 | pub const CValue = union(enum) { |
| 24 | none: void, | 25 | none: void, |
| ... | @@ -226,8 +227,7 @@ pub const DeclGen = struct { | ... | @@ -226,8 +227,7 @@ pub const DeclGen = struct { |
| 226 | try dg.renderDeclName(decl, writer); | 227 | try dg.renderDeclName(decl, writer); |
| 227 | } | 228 | } |
| 228 | | 229 | |
| 229 | /// Assumes that int_val is an int greater than maxInt(u64) and has > 64 and <= 128 bits. | 230 | fn renderInt128( |
| 230 | fn renderBigInt( | | |
| 231 | writer: anytype, | 231 | writer: anytype, |
| 232 | int_val: anytype, | 232 | int_val: anytype, |
| 233 | ) error{ OutOfMemory, AnalysisFail }!void { | 233 | ) error{ OutOfMemory, AnalysisFail }!void { |
| ... | @@ -256,6 +256,23 @@ pub const DeclGen = struct { | ... | @@ -256,6 +256,23 @@ pub const DeclGen = struct { |
| 256 | return writer.writeByte(')'); | 256 | return writer.writeByte(')'); |
| 257 | } | 257 | } |
| 258 | | 258 | |
| | 259 | fn renderBigIntConst( |
| | 260 | dg: *DeclGen, |
| | 261 | writer: anytype, |
| | 262 | val: BigIntConst, |
| | 263 | signed: bool, |
| | 264 | ) error{ OutOfMemory, AnalysisFail }!void { |
| | 265 | if (signed) { |
| | 266 | try renderInt128(writer, val.to(i128) catch { |
| | 267 | return dg.fail("TODO implement integer constants larger than 128 bits", .{}); |
| | 268 | }); |
| | 269 | } else { |
| | 270 | try renderInt128(writer, val.to(u128) catch { |
| | 271 | return dg.fail("TODO implement integer constants larger than 128 bits", .{}); |
| | 272 | }); |
| | 273 | } |
| | 274 | } |
| | 275 | |
| 259 | fn renderValue( | 276 | fn renderValue( |
| 260 | dg: *DeclGen, | 277 | dg: *DeclGen, |
| 261 | writer: anytype, | 278 | writer: anytype, |
| ... | @@ -274,7 +291,7 @@ pub const DeclGen = struct { | ... | @@ -274,7 +291,7 @@ pub const DeclGen = struct { |
| 274 | 16 => return writer.writeAll("0xaaaau"), | 291 | 16 => return writer.writeAll("0xaaaau"), |
| 275 | 32 => return writer.writeAll("0xaaaaaaaau"), | 292 | 32 => return writer.writeAll("0xaaaaaaaau"), |
| 276 | 64 => return writer.writeAll("0xaaaaaaaaaaaaaaaau"), | 293 | 64 => return writer.writeAll("0xaaaaaaaaaaaaaaaau"), |
| 277 | 128 => return renderBigInt(writer, @as(u128, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)), | 294 | 128 => return renderInt128(writer, @as(u128, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)), |
| 278 | else => unreachable, | 295 | else => unreachable, |
| 279 | } | 296 | } |
| 280 | }, | 297 | }, |
| ... | @@ -296,12 +313,8 @@ pub const DeclGen = struct { | ... | @@ -296,12 +313,8 @@ pub const DeclGen = struct { |
| 296 | } | 313 | } |
| 297 | switch (ty.zigTypeTag()) { | 314 | switch (ty.zigTypeTag()) { |
| 298 | .Int => switch (val.tag()) { | 315 | .Int => switch (val.tag()) { |
| 299 | .int_big_positive => try renderBigInt(writer, val.castTag(.int_big_positive).?.asBigInt().to(u128) catch { | 316 | .int_big_positive => try dg.renderBigIntConst(writer, val.castTag(.int_big_positive).?.asBigInt(), ty.isSignedInt()), |
| 300 | return dg.fail("TODO implement integer constants larger than 128 bits", .{}); | 317 | .int_big_negative => try dg.renderBigIntConst(writer, val.castTag(.int_big_negative).?.asBigInt(), true), |
| 301 | }), | | |
| 302 | .int_big_negative => try renderBigInt(writer, val.castTag(.int_big_negative).?.asBigInt().to(i128) catch { | | |
| 303 | return dg.fail("TODO implement integer constants larger than 128 bits", .{}); | | |
| 304 | }), | | |
| 305 | else => { | 318 | else => { |
| 306 | if (ty.isSignedInt()) | 319 | if (ty.isSignedInt()) |
| 307 | return writer.print("{d}", .{val.toSignedInt()}); | 320 | return writer.print("{d}", .{val.toSignedInt()}); |