diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fde14b2e..00996f93 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,8 +28,13 @@ jobs: - name: Build run: zig build + - name: Build 32-bit + run: zig build -Dtarget=arm-linux + if: matrix.os == 'ubuntu-latest' + - name: Fmt run: zig fmt . --check + if: matrix.os == 'ubuntu-latest' - name: Run Tests run: zig build test diff --git a/src/Compilation.zig b/src/Compilation.zig index 5096b206..59edafc6 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -107,7 +107,7 @@ fn generateDateAndTime(w: anytype) !void { const day_names = [_][]const u8{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }; // days since Thu Oct 1 1970 - const day_name = day_names[(epoch_day.day + 3) % 7]; + const day_name = day_names[@truncate(u8, (epoch_day.day + 3) % 7)]; try w.print("#define __TIMESTAMP__ \"{s} {s} {d: >2} {d:0>2}:{d:0>2}:{d:0>2} {d}\"\n", .{ day_name, month_name, diff --git a/src/InitList.zig b/src/InitList.zig index 0e68e16d..b4eae645 100644 --- a/src/InitList.zig +++ b/src/InitList.zig @@ -79,7 +79,7 @@ pub fn put(il: *InitList, gpa: Allocator, index: usize, node: NodeIndex, tok: To } /// Find item at index, create new if one does not exist. -pub fn find(il: *InitList, gpa: Allocator, index: usize) !*InitList { +pub fn find(il: *InitList, gpa: Allocator, index: u64) !*InitList { const items = il.list.items; var left: usize = 0; var right: usize = items.len; diff --git a/src/Parser.zig b/src/Parser.zig index 6f2cbe53..bd4f8136 100644 --- a/src/Parser.zig +++ b/src/Parser.zig @@ -2782,7 +2782,7 @@ fn initializerItem(p: *Parser, il: *InitList, init_ty: Type) Error!bool { var count: u64 = 0; var warned_excess = false; var is_str_init = false; - var index_hint: ?usize = null; + var index_hint: ?u64 = null; while (true) : (count += 1) { errdefer p.skipTo(.r_brace); @@ -2790,7 +2790,7 @@ fn initializerItem(p: *Parser, il: *InitList, init_ty: Type) Error!bool { var cur_ty = init_ty; var cur_il = il; var designation = false; - var cur_index_hint: ?usize = null; + var cur_index_hint: ?u64 = null; while (true) { if (p.eatToken(.l_bracket)) |l_bracket| { if (!cur_ty.isArray()) { @@ -2949,13 +2949,13 @@ fn initializerItem(p: *Parser, il: *InitList, init_ty: Type) Error!bool { } /// Returns true if the value is unused. -fn findScalarInitializerAt(p: *Parser, il: **InitList, ty: *Type, actual_ty: Type, first_tok: TokenIndex, start_index: *usize) Error!bool { +fn findScalarInitializerAt(p: *Parser, il: **InitList, ty: *Type, actual_ty: Type, first_tok: TokenIndex, start_index: *u64) Error!bool { if (ty.isArray()) { if (il.*.node != .none) return false; start_index.* += 1; const arr_ty = ty.*; - const elem_count = arr_ty.arrayLen() orelse std.math.maxInt(usize); + const elem_count = arr_ty.arrayLen() orelse std.math.maxInt(u64); if (elem_count == 0) { try p.errTok(.empty_aggregate_init_braces, first_tok); return error.ParsingFailed; @@ -2980,7 +2980,7 @@ fn findScalarInitializerAt(p: *Parser, il: **InitList, ty: *Type, actual_ty: Typ } const struct_il = il.*; if (start_index.* < fields.len) { - const field = fields[start_index.*]; + const field = fields[@intCast(usize, start_index.*)]; ty.* = field.ty; il.* = try struct_il.find(p.gpa, start_index.*); _ = try p.findScalarInitializer(il, ty, actual_ty, first_tok); @@ -2997,11 +2997,11 @@ fn findScalarInitializerAt(p: *Parser, il: **InitList, ty: *Type, actual_ty: Typ fn findScalarInitializer(p: *Parser, il: **InitList, ty: *Type, actual_ty: Type, first_tok: TokenIndex) Error!bool { if (ty.isArray()) { if (il.*.node != .none) return false; - var index = il.*.list.items.len; - if (index != 0) index = il.*.list.items[index - 1].index; + const start_index = il.*.list.items.len; + var index = if (start_index != 0) il.*.list.items[start_index - 1].index else start_index; const arr_ty = ty.*; - const elem_count = arr_ty.arrayLen() orelse std.math.maxInt(usize); + const elem_count = arr_ty.arrayLen() orelse std.math.maxInt(u64); if (elem_count == 0) { try p.errTok(.empty_aggregate_init_braces, first_tok); return error.ParsingFailed; @@ -3018,8 +3018,8 @@ fn findScalarInitializer(p: *Parser, il: **InitList, ty: *Type, actual_ty: Type, } else if (ty.get(.@"struct")) |struct_ty| { if (il.*.node != .none) return false; if (actual_ty.eql(ty.*, p.pp.comp, false)) return true; - var index = il.*.list.items.len; - if (index != 0) index = il.*.list.items[index - 1].index + 1; + const start_index = il.*.list.items.len; + var index = if (start_index != 0) il.*.list.items[start_index - 1].index + 1 else start_index; const fields = struct_ty.data.record.fields; if (fields.len == 0) { @@ -3028,7 +3028,7 @@ fn findScalarInitializer(p: *Parser, il: **InitList, ty: *Type, actual_ty: Type, } const struct_il = il.*; while (index < fields.len) : (index += 1) { - const field = fields[index]; + const field = fields[@intCast(u32, index)]; ty.* = field.ty; il.* = try struct_il.find(p.gpa, index); if (il.*.node == .none and actual_ty.eql(field.ty, p.comp, false)) return true; @@ -3051,18 +3051,20 @@ fn findScalarInitializer(p: *Parser, il: **InitList, ty: *Type, actual_ty: Type, return il.*.node == .none; } -fn findAggregateInitializer(p: *Parser, il: **InitList, ty: *Type, start_index: *?usize) Error!bool { +fn findAggregateInitializer(p: *Parser, il: **InitList, ty: *Type, start_index: *?u64) Error!bool { if (ty.isArray()) { if (il.*.node != .none) return false; - var index = il.*.list.items.len; - if (index != 0) index = il.*.list.items[index - 1].index + 1; - if (start_index.*) |*some| { + const list_index = il.*.list.items.len; + const index = if (start_index.*) |*some| blk: { some.* += 1; - index = some.*; - } + break :blk some.*; + } else if (list_index != 0) + il.*.list.items[list_index - 1].index + 1 + else + list_index; const arr_ty = ty.*; - const elem_count = arr_ty.arrayLen() orelse std.math.maxInt(usize); + const elem_count = arr_ty.arrayLen() orelse std.math.maxInt(u64); const elem_ty = arr_ty.elemType(); if (index < elem_count) { ty.* = elem_ty; @@ -3072,16 +3074,18 @@ fn findAggregateInitializer(p: *Parser, il: **InitList, ty: *Type, start_index: return false; } else if (ty.get(.@"struct")) |struct_ty| { if (il.*.node != .none) return false; - var index = il.*.list.items.len; - if (index != 0) index = il.*.list.items[index - 1].index + 1; - if (start_index.*) |*some| { + const list_index = il.*.list.items.len; + const index = if (start_index.*) |*some| blk: { some.* += 1; - index = some.*; - } + break :blk some.*; + } else if (list_index != 0) + il.*.list.items[list_index - 1].index + 1 + else + list_index; const field_count = struct_ty.data.record.fields.len; if (index < field_count) { - ty.* = struct_ty.data.record.fields[index].ty; + ty.* = struct_ty.data.record.fields[@intCast(usize, index)].ty; il.* = try il.*.find(p.gpa, index); return true; } @@ -3293,9 +3297,10 @@ fn convertInitList(p: *Parser, il: InitList, init_ty: Type) Error!NodeIndex { }); } else { const init = il.list.items[0]; - const field_ty = union_ty.data.record.fields[init.index].ty; + const index = @truncate(u32, init.index); + const field_ty = union_ty.data.record.fields[index].ty; union_init_node.data.union_init = .{ - .field_index = @truncate(u32, init.index), + .field_index = index, .node = try p.convertInitList(init.list, field_ty), }; } diff --git a/src/Type.zig b/src/Type.zig index 5454d684..9e56c013 100644 --- a/src/Type.zig +++ b/src/Type.zig @@ -530,7 +530,7 @@ pub fn params(ty: Type) []Func.Param { }; } -pub fn arrayLen(ty: Type) ?usize { +pub fn arrayLen(ty: Type) ?u64 { return switch (ty.specifier) { .array, .static_array, .decayed_array, .decayed_static_array => ty.data.array.len, .typeof_type, .decayed_typeof_type => ty.data.sub_type.arrayLen(), @@ -761,7 +761,7 @@ pub fn sizeof(ty: Type, comp: *const Compilation) ?u64 { => comp.target.cpu.arch.ptrBitWidth() >> 3, .array, .vector => { const size = ty.data.array.elem.sizeof(comp) orelse return null; - return std.mem.alignForward(size * ty.data.array.len, ty.alignof(comp)); + return std.mem.alignForwardGeneric(u64, size * ty.data.array.len, ty.alignof(comp)); }, .@"struct", .@"union" => if (ty.data.record.isIncomplete()) null else ty.data.record.size, .@"enum" => if (ty.data.@"enum".isIncomplete() and !ty.data.@"enum".fixed) null else ty.data.@"enum".tag_ty.sizeof(comp), diff --git a/src/object/Elf.zig b/src/object/Elf.zig index f6921c85..2ff6fe2a 100644 --- a/src/object/Elf.zig +++ b/src/object/Elf.zig @@ -189,11 +189,11 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void { const symtab_len = (elf.local_symbols.count() + elf.global_symbols.count() + 1) * @sizeOf(std.elf.Elf64_Sym); const symtab_offset = @sizeOf(std.elf.Elf64_Ehdr) + sections_len; - const symtab_offset_aligned = std.mem.alignForward(symtab_offset, 8); + const symtab_offset_aligned = std.mem.alignForwardGeneric(u64, symtab_offset, 8); const rela_offset = symtab_offset_aligned + symtab_len; const strtab_offset = rela_offset + relocations_len; const sh_offset = strtab_offset + elf.strtab_len; - const sh_offset_aligned = std.mem.alignForward(sh_offset, 16); + const sh_offset_aligned = std.mem.alignForwardGeneric(u64, sh_offset, 16); var elf_header = std.elf.Elf64_Ehdr{ .e_ident = .{ 0x7F, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, @@ -220,7 +220,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void { } // pad to 8 bytes - try w.writeByteNTimes(0, symtab_offset_aligned - symtab_offset); + try w.writeByteNTimes(0, @intCast(usize, symtab_offset_aligned - symtab_offset)); var name_offset: u32 = strtab_default.len; // write symbols @@ -292,7 +292,7 @@ pub fn finish(elf: *Elf, file: std.fs.File) !void { } // pad to 16 bytes - try w.writeByteNTimes(0, sh_offset_aligned - sh_offset); + try w.writeByteNTimes(0, @intCast(usize, sh_offset_aligned - sh_offset)); // mandatory null header try w.writeStruct(std.mem.zeroes(std.elf.Elf64_Shdr));