From 514395ae1fbabd1a39a63866116048ba8ff316ab Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 9 Oct 2024 11:25:01 +0300 Subject: [PATCH 1/4] Parser: use enum for offsetof bits/bytes --- src/aro/Parser.zig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/aro/Parser.zig b/src/aro/Parser.zig index 1d9c62f0..1fba809e 100644 --- a/src/aro/Parser.zig +++ b/src/aro/Parser.zig @@ -6843,8 +6843,8 @@ fn castExpr(p: *Parser) Error!Result { switch (p.tok_ids[p.tok_i]) { .builtin_choose_expr => return p.builtinChooseExpr(), .builtin_va_arg => return p.builtinVaArg(), - .builtin_offsetof => return p.builtinOffsetof(false), - .builtin_bitoffsetof => return p.builtinOffsetof(true), + .builtin_offsetof => return p.builtinOffsetof(.bytes), + .builtin_bitoffsetof => return p.builtinOffsetof(.bits), .builtin_types_compatible_p => return p.typesCompatible(), // TODO: other special-cased builtins else => {}, @@ -6968,7 +6968,9 @@ fn builtinVaArg(p: *Parser) Error!Result { }) }; } -fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result { +const OffsetKind = enum { bits, bytes }; + +fn builtinOffsetof(p: *Parser, offset_kind: OffsetKind) Error!Result { const builtin_tok = p.tok_i; p.tok_i += 1; @@ -6993,7 +6995,7 @@ fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result { _ = try p.expectToken(.comma); - const offsetof_expr = try p.offsetofMemberDesignator(ty, want_bits); + const offsetof_expr = try p.offsetofMemberDesignator(ty, offset_kind); try p.expectClosing(l_paren, .r_paren); @@ -7009,7 +7011,7 @@ fn builtinOffsetof(p: *Parser, want_bits: bool) Error!Result { } /// offsetofMemberDesignator: IDENTIFIER ('.' IDENTIFIER | '[' expr ']' )* -fn offsetofMemberDesignator(p: *Parser, base_ty: Type, want_bits: bool) Error!Result { +fn offsetofMemberDesignator(p: *Parser, base_ty: Type, offset_kind: OffsetKind) Error!Result { errdefer p.skipTo(.r_paren); const base_field_name_tok = try p.expectIdentifier(); const base_field_name = try StrInt.intern(p.comp, p.tokSlice(base_field_name_tok)); @@ -7062,7 +7064,7 @@ fn offsetofMemberDesignator(p: *Parser, base_ty: Type, want_bits: bool) Error!Re }, else => break, }; - const val = try Value.int(if (want_bits) total_offset else total_offset / 8, p.comp); + const val = try Value.int(if (offset_kind == .bits) total_offset else total_offset / 8, p.comp); return Result{ .ty = base_ty, .val = val, .node = lhs.node }; } From 78342b51b1a7b4e73583719c2cd59f0aff954ae3 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 9 Oct 2024 11:43:42 +0300 Subject: [PATCH 2/4] tests: fix test case missing target --- test/cases/fp16 parameter.c | 5 +++-- test/runner.zig | 22 ++++++++++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/test/cases/fp16 parameter.c b/test/cases/fp16 parameter.c index 9cee0061..582c44cd 100644 --- a/test/cases/fp16 parameter.c +++ b/test/cases/fp16 parameter.c @@ -1,7 +1,8 @@ +//aro-args --target=x86_64-linux-gnu __fp16 foo(__fp16 param) { return 0; } -#define EXPECTED_ERRORS "fp16 parameter.c:1:19: error: parameters cannot have __fp16 type; did you forget * ?" \ - "fp16 parameter.c:1:11: error: function return value cannot have __fp16 type; did you forget * ?" \ +#define EXPECTED_ERRORS "fp16 parameter.c:2:19: error: parameters cannot have __fp16 type; did you forget * ?" \ + "fp16 parameter.c:2:11: error: function return value cannot have __fp16 type; did you forget * ?" \ diff --git a/test/runner.zig b/test/runner.zig index 2481ff5c..9b084bce 100644 --- a/test/runner.zig +++ b/test/runner.zig @@ -253,6 +253,7 @@ pub fn main() !void { if (pp.defines.get("TESTS_SKIPPED")) |macro| { if (macro.is_func or macro.tokens.len != 1 or macro.tokens[0].id != .pp_num) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("invalid TESTS_SKIPPED, definition should contain exactly one integer literal {}\n", .{macro}); continue; } @@ -265,12 +266,14 @@ pub fn main() !void { if (only_preprocess) { if (try checkExpectedErrors(&pp, &buf)) |some| { if (!some) { + std.debug.print("in case {s}\n", .{case}); fail_count += 1; continue; } } else { aro.Diagnostics.render(&comp, std.io.tty.detectConfig(std.io.getStdErr())); if (comp.diagnostics.errors != 0) { + std.debug.print("in case {s}\n", .{case}); fail_count += 1; continue; } @@ -299,6 +302,7 @@ pub fn main() !void { ok_count += 1; } else { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("\n====== expected to find: =========\n", .{}); std.debug.print("{s}", .{expected_output}); std.debug.print("\n======== but did not find it in this: =========\n", .{}); @@ -319,7 +323,10 @@ pub fn main() !void { var tree = aro.Parser.parse(&pp) catch |err| switch (err) { error.FatalError => { if (try checkExpectedErrors(&pp, &buf)) |some| { - if (some) ok_count += 1 else fail_count += 1; + if (some) ok_count += 1 else { + std.debug.print("in case {s}\n", .{case}); + fail_count += 1; + } } continue; }, @@ -337,6 +344,7 @@ pub fn main() !void { try tree.dump(.no_color, actual_ast.writer()); std.testing.expectEqualStrings(expected_ast, actual_ast.items) catch { + std.debug.print("in case {s}\n", .{case}); fail_count += 1; break; }; @@ -347,6 +355,7 @@ pub fn main() !void { if (tree.nodes.items(.tag)[@intFromEnum(decl)] == .fn_def) break tree.nodes.items(.data)[@intFromEnum(decl)]; } else { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("EXPECTED_TYPES requires a function to be defined\n", .{}); break; }; @@ -363,6 +372,7 @@ pub fn main() !void { if (str.id == .macro_ws) continue; if (str.id != .string_literal) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("EXPECTED_TYPES tokens must be string literals (found {s})\n", .{@tagName(str.id)}); continue :next_test; } @@ -373,6 +383,7 @@ pub fn main() !void { const actual_type = actual.types.items[i]; if (!std.mem.eql(u8, expected_type, actual_type)) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("expected type '{s}' did not match actual type '{s}'\n", .{ expected_type, actual_type, @@ -382,6 +393,7 @@ pub fn main() !void { } if (i != actual.types.items.len) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print( "EXPECTED_TYPES count differs: expected {d} found {d}\n", .{ i, actual.types.items.len }, @@ -391,7 +403,10 @@ pub fn main() !void { } if (try checkExpectedErrors(&pp, &buf)) |some| { - if (some) ok_count += 1 else fail_count += 1; + if (some) ok_count += 1 else { + std.debug.print("in case {s}\n", .{case}); + fail_count += 1; + } continue; } @@ -408,12 +423,14 @@ pub fn main() !void { if (macro.is_func) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("invalid EXPECTED_OUTPUT {}\n", .{macro}); continue; } if (macro.tokens.len != 1 or macro.tokens[0].id != .string_literal) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print("EXPECTED_OUTPUT takes exactly one string", .{}); continue; } @@ -456,6 +473,7 @@ pub fn main() !void { if (!std.mem.eql(u8, expected_output, stdout)) { fail_count += 1; + std.debug.print("{s}:\n", .{case}); std.debug.print( \\ \\======= expected output ======= From b8809fb12c0b1b6237a30d822298be7b3e8b6d8f Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 9 Oct 2024 12:04:19 +0300 Subject: [PATCH 3/4] Parser: avoid checking long double size when casting float --- src/aro/Parser.zig | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/aro/Parser.zig b/src/aro/Parser.zig index 1fba809e..3fba464c 100644 --- a/src/aro/Parser.zig +++ b/src/aro/Parser.zig @@ -5804,8 +5804,8 @@ pub const Result = struct { // if either is a float cast to that type if (a.ty.isFloat() or b.ty.isFloat()) { const float_types = [6][2]Type.Specifier{ - .{ .complex_long_double, .long_double }, .{ .complex_float128, .float128 }, + .{ .complex_long_double, .long_double }, .{ .complex_double, .double }, .{ .complex_float, .float }, // No `_Complex __fp16` type @@ -5814,20 +5814,9 @@ pub const Result = struct { }; const a_spec = a.ty.canonicalize(.standard).specifier; const b_spec = b.ty.canonicalize(.standard).specifier; - if (p.comp.target.cTypeBitSize(.longdouble) == 128) { - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[0])) return; - } - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[1])) return; - if (p.comp.target.cTypeBitSize(.longdouble) >= p.comp.target.cTypeBitSize(.double)) { - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[0])) return; - } - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[2])) return; - if (p.comp.target.cTypeBitSize(.longdouble) >= p.comp.target.cTypeBitSize(.float)) { - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[0])) return; + for (float_types) |ft| { + if (try a.floatConversion(b, a_spec, b_spec, p, ft)) return; } - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[3])) return; - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[4])) return; - if (try a.floatConversion(b, a_spec, b_spec, p, float_types[5])) return; unreachable; } From 9906d474dd45ee1820e2a1b0a1f34f14638164ad Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Mon, 14 Oct 2024 14:44:36 +0300 Subject: [PATCH 4/4] zig update: rename elf Half type --- src/backend/Object/Elf.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/Object/Elf.zig b/src/backend/Object/Elf.zig index 2a303d34..105d0756 100644 --- a/src/backend/Object/Elf.zig +++ b/src/backend/Object/Elf.zig @@ -174,7 +174,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void { var buf_writer = std.io.bufferedWriter(file.writer()); const w = buf_writer.writer(); - var num_sections: std.elf.Elf64_Half = additional_sections; + var num_sections: std.elf.Half = additional_sections; var relocations_len: std.elf.Elf64_Off = 0; var sections_len: std.elf.Elf64_Off = 0; {