Skip to content

Commit

Permalink
Parser: fix bugs in creating number literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Nov 9, 2023
1 parent 7600d14 commit a27b536
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
21 changes: 17 additions & 4 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5484,7 +5484,7 @@ const Result = struct {
/// Saves value and replaces it with `.unavailable`.
fn saveValue(res: *Result, p: *Parser) !void {
assert(!p.in_macro);
if (res.val.opt_ref == .none or res.val.ref() == .null) return;
if (res.val.opt_ref == .none or res.val.opt_ref == .null) return;
if (!p.in_macro) try p.value_map.put(res.node, res.val);
res.val = .{};
}
Expand Down Expand Up @@ -7715,6 +7715,7 @@ fn stringLiteral(p: *Parser) Error!Result {
p.strings.appendNTimesAssumeCapacity(0, @intFromEnum(char_width));
const slice = p.strings.items[strings_top..];

// TODO this won't do anything if there is a cache hit
const interned_align = mem.alignForward(
usize,
p.comp.interner.strings.items.len,
Expand Down Expand Up @@ -7881,6 +7882,7 @@ fn parseFloat(p: *Parser, buf: []const u8, suffix: NumberSuffix) !Result {
res.ty = .{ .specifier = switch (suffix) {
.I => .complex_double,
.IF => .complex_float,
.IL => .complex_long_double,
else => unreachable,
} };
res.val = .{}; // TODO add complex values
Expand Down Expand Up @@ -7983,8 +7985,19 @@ fn fixedSizeInt(p: *Parser, base: u8, buf: []const u8, suffix: NumberSuffix, tok
else
&signed_oct_hex_specs;

const suffix_ty: Type = .{ .specifier = switch (suffix) {
.None, .I => .int,
.U, .IU => .uint,
.UL, .IUL => .ulong,
.ULL, .IULL => .ulong_long,
.L, .IL => .long,
.LL, .ILL => .long_long,
else => unreachable,
} };

for (specs) |spec| {
res.ty = Type{ .specifier = spec };
if (res.ty.compareIntegerRanks(suffix_ty, p.comp).compare(.lt)) continue;
const max_int = res.ty.maxInt(p.comp);
if (val <= max_int) break;
} else {
Expand Down Expand Up @@ -8049,9 +8062,8 @@ fn bitInt(p: *Parser, base: u8, buf: []const u8, suffix: NumberSuffix, tok_i: To
break :blk @intCast(bits_needed);
};

const val = try Value.intern(p.comp, .{ .int = .{ .big_int = c } });
var res: Result = .{
.val = val,
.val = try Value.intern(p.comp, .{ .int = .{ .big_int = c } }),
.ty = .{
.specifier = .bit_int,
.data = .{ .int = .{ .bits = bits_needed, .signedness = suffix.signedness() } },
Expand Down Expand Up @@ -8153,7 +8165,8 @@ fn ppNum(p: *Parser) Error!Result {
return error.ParsingFailed;
}
res.ty = if (res.ty.isUnsignedInt(p.comp)) p.comp.types.intmax.makeIntegerUnsigned() else p.comp.types.intmax;
} else {
} else if (res.val.opt_ref != .none) {
// TODO add complex values
try p.value_map.put(res.node, res.val);
}
return res;
Expand Down
7 changes: 2 additions & 5 deletions src/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -629,10 +629,7 @@ pub fn signedness(ty: Type, comp: *const Compilation) std.builtin.Signedness {
// zig fmt: off
.char, .complex_char => return comp.getCharSignedness(),
.uchar, .ushort, .uint, .ulong, .ulong_long, .bool, .complex_uchar, .complex_ushort,
.complex_uint, .complex_ulong, .complex_ulong_long, .complex_uint128,
.pointer, .decayed_array, .decayed_static_array, .decayed_incomplete_array,
.decayed_variable_len_array, .decayed_unspecified_variable_len_array,
.decayed_typeof_type, .decayed_typeof_expr => .unsigned,
.complex_uint, .complex_ulong, .complex_ulong_long, .complex_uint128 => .unsigned,
// zig fmt: on
.bit_int, .complex_bit_int => ty.data.int.signedness,
.typeof_type => ty.data.sub_type.signedness(comp),
Expand Down Expand Up @@ -765,7 +762,7 @@ pub fn getRecord(ty: Type) ?*const Type.Record {
};
}

fn compareIntegerRanks(a: Type, b: Type, comp: *const Compilation) std.math.Order {
pub fn compareIntegerRanks(a: Type, b: Type, comp: *const Compilation) std.math.Order {
std.debug.assert(a.isInt() and b.isInt());
if (a.eql(b, comp, false)) return .eq;

Expand Down
3 changes: 2 additions & 1 deletion src/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ pub fn intCast(v: *Value, dest_ty: Type, comp: *Compilation) !void {
/// `.none` value remains unchanged.
pub fn floatCast(v: *Value, dest_ty: Type, comp: *Compilation) !void {
if (v.opt_ref == .none) return;
const bits = dest_ty.bitSizeof(comp).?;
// TODO complex values
const bits = dest_ty.makeReal().bitSizeof(comp).?;
const f: Interner.Key.Float = switch (bits) {
16 => .{ .f16 = v.toFloat(f16, comp) },
32 => .{ .f32 = v.toFloat(f32, comp) },
Expand Down

0 comments on commit a27b536

Please sign in to comment.