diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2024-04-09 00:50:58 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-09 00:50:58 -0700 |
| commit | ac21ade667f0f42b8b1aec5831cbc99cbaed8565 (patch) | |
| tree | f66d847662b81eefe8c30de88dfedb0d93cdc524 /src/codegen/c.zig | |
| parent | d979df585d05de8d7385495fe6aee2b1d4e1380f (diff) | |
| parent | bc0e2089bc9408fa76716652a8ede5b5b44a817a (diff) | |
| download | zig-ac21ade667f0f42b8b1aec5831cbc99cbaed8565.tar.gz zig-ac21ade667f0f42b8b1aec5831cbc99cbaed8565.zip | |
Merge pull request #19572 from jacobly0/haiku-bootstrap
haiku: get cmake bootstrap working
Diffstat (limited to 'src/codegen/c.zig')
| -rw-r--r-- | src/codegen/c.zig | 178 |
1 files changed, 73 insertions, 105 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 95f4ecc4ac..0b3d2f9836 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -505,7 +505,7 @@ pub const Function = struct { .never_inline, => |owner_decl| try ctype_pool.fmt(gpa, "zig_{s}_{}__{d}", .{ @tagName(key), - fmtIdent(zcu.intern_pool.stringToSlice(zcu.declPtr(owner_decl).name)), + fmtIdent(zcu.declPtr(owner_decl).name.toSlice(&zcu.intern_pool)), @intFromEnum(owner_decl), }), }, @@ -822,19 +822,6 @@ pub const DeclGen = struct { try dg.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset), .Other), }); }, - .end => { - const ptr_base_ctype = try dg.ctypeFromType(ptr_base_ty, .complete); - if (!ptr_ctype.eql(ptr_base_ctype)) { - try writer.writeByte('('); - try dg.renderCType(writer, ptr_ctype); - try writer.writeByte(')'); - } - try writer.writeAll("(("); - try dg.renderParentPtr(writer, field.base, location); - try writer.print(") + {})", .{ - try dg.fmtIntLiteral(try zcu.intValue(Type.usize, 1), .Other), - }); - }, } }, .comptime_field, .comptime_alloc => unreachable, @@ -911,7 +898,7 @@ pub const DeclGen = struct { }, }, .err => |err| try writer.print("zig_error_{}", .{ - fmtIdent(ip.stringToSlice(err.name)), + fmtIdent(err.name.toSlice(ip)), }), .error_union => |error_union| { const payload_ty = ty.errorUnionPayload(zcu); @@ -1191,7 +1178,7 @@ pub const DeclGen = struct { switch (ip.indexToKey(val.toIntern()).aggregate.storage) { .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ .ty = field_ty.toIntern(), - .storage = .{ .u64 = bytes[field_index] }, + .storage = .{ .u64 = bytes.at(field_index, ip) }, } }), .elems => |elems| elems[field_index], .repeated_elem => |elem| elem, @@ -1225,7 +1212,7 @@ pub const DeclGen = struct { const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ .ty = field_ty.toIntern(), - .storage = .{ .u64 = bytes[field_index] }, + .storage = .{ .u64 = bytes.at(field_index, ip) }, } }), .elems => |elems| elems[field_index], .repeated_elem => |elem| elem, @@ -1271,7 +1258,7 @@ pub const DeclGen = struct { const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ .ty = field_ty.toIntern(), - .storage = .{ .u64 = bytes[field_index] }, + .storage = .{ .u64 = bytes.at(field_index, ip) }, } }), .elems => |elems| elems[field_index], .repeated_elem => |elem| elem, @@ -1312,7 +1299,7 @@ pub const DeclGen = struct { const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ .ty = field_ty.toIntern(), - .storage = .{ .u64 = bytes[field_index] }, + .storage = .{ .u64 = bytes.at(field_index, ip) }, } }), .elems => |elems| elems[field_index], .repeated_elem => |elem| elem, @@ -1405,7 +1392,7 @@ pub const DeclGen = struct { try writer.writeAll(" .payload = {"); } if (field_ty.hasRuntimeBits(zcu)) { - try writer.print(" .{ } = ", .{fmtIdent(ip.stringToSlice(field_name))}); + try writer.print(" .{ } = ", .{fmtIdent(field_name.toSlice(ip))}); try dg.renderValue(writer, Value.fromInterned(un.val), initializer_type); try writer.writeByte(' '); } else for (0..loaded_union.field_types.len) |this_field_index| { @@ -1754,14 +1741,12 @@ pub const DeclGen = struct { switch (name) { .export_index => |export_index| mangled: { const maybe_exports = zcu.decl_exports.get(fn_decl_index); - const external_name = ip.stringToSlice( - if (maybe_exports) |exports| - exports.items[export_index].opts.name - else if (fn_decl.isExtern(zcu)) - fn_decl.name - else - break :mangled, - ); + const external_name = (if (maybe_exports) |exports| + exports.items[export_index].opts.name + else if (fn_decl.isExtern(zcu)) + fn_decl.name + else + break :mangled).toSlice(ip); const is_mangled = isMangledIdent(external_name, true); const is_export = export_index > 0; if (is_mangled and is_export) { @@ -1769,7 +1754,7 @@ pub const DeclGen = struct { fmtIdent(external_name), fmtStringLiteral(external_name, null), fmtStringLiteral( - ip.stringToSlice(maybe_exports.?.items[0].opts.name), + maybe_exports.?.items[0].opts.name.toSlice(ip), null, ), }); @@ -1780,7 +1765,7 @@ pub const DeclGen = struct { } else if (is_export) { try w.print(" zig_export({s}, {s})", .{ fmtStringLiteral( - ip.stringToSlice(maybe_exports.?.items[0].opts.name), + maybe_exports.?.items[0].opts.name.toSlice(ip), null, ), fmtStringLiteral(external_name, null), @@ -2088,12 +2073,12 @@ pub const DeclGen = struct { .complete, ); mangled: { - const external_name = zcu.intern_pool.stringToSlice(if (maybe_exports) |exports| + const external_name = (if (maybe_exports) |exports| exports.items[0].opts.name else if (variable.is_extern) decl.name else - break :mangled); + break :mangled).toSlice(&zcu.intern_pool); if (isMangledIdent(external_name, true)) { try fwd.print(" zig_mangled_{s}({ }, {s})", .{ @tagName(fwd_kind), @@ -2107,15 +2092,16 @@ pub const DeclGen = struct { fn renderDeclName(dg: *DeclGen, writer: anytype, decl_index: InternPool.DeclIndex, export_index: u32) !void { const zcu = dg.zcu; + const ip = &zcu.intern_pool; const decl = zcu.declPtr(decl_index); if (zcu.decl_exports.get(decl_index)) |exports| { try writer.print("{ }", .{ - fmtIdent(zcu.intern_pool.stringToSlice(exports.items[export_index].opts.name)), + fmtIdent(exports.items[export_index].opts.name.toSlice(ip)), }); } else if (decl.getExternDecl(zcu).unwrap()) |extern_decl_index| { try writer.print("{ }", .{ - fmtIdent(zcu.intern_pool.stringToSlice(zcu.declPtr(extern_decl_index).name)), + fmtIdent(zcu.declPtr(extern_decl_index).name.toSlice(ip)), }); } else { // MSVC has a limit of 4095 character token length limit, and fmtIdent can (worst case), @@ -2239,7 +2225,7 @@ fn renderFwdDeclTypeName( switch (fwd_decl.name) { .anon => try w.print("anon__lazy_{d}", .{@intFromEnum(ctype.index)}), .owner_decl => |owner_decl| try w.print("{}__{d}", .{ - fmtIdent(zcu.intern_pool.stringToSlice(zcu.declPtr(owner_decl).name)), + fmtIdent(zcu.declPtr(owner_decl).name.toSlice(&zcu.intern_pool)), @intFromEnum(owner_decl), }), } @@ -2561,7 +2547,7 @@ pub fn genErrDecls(o: *Object) !void { try writer.writeAll("enum {\n"); o.indent_writer.pushIndent(); for (zcu.global_error_set.keys()[1..], 1..) |name_nts, value| { - const name = ip.stringToSlice(name_nts); + const name = name_nts.toSlice(ip); max_name_len = @max(name.len, max_name_len); const err_val = try zcu.intern(.{ .err = .{ .ty = .anyerror_type, @@ -2579,19 +2565,19 @@ pub fn genErrDecls(o: *Object) !void { defer o.dg.gpa.free(name_buf); @memcpy(name_buf[0..name_prefix.len], name_prefix); - for (zcu.global_error_set.keys()) |name_ip| { - const name = ip.stringToSlice(name_ip); - @memcpy(name_buf[name_prefix.len..][0..name.len], name); - const identifier = name_buf[0 .. name_prefix.len + name.len]; + for (zcu.global_error_set.keys()) |name| { + const name_slice = name.toSlice(ip); + @memcpy(name_buf[name_prefix.len..][0..name_slice.len], name_slice); + const identifier = name_buf[0 .. name_prefix.len + name_slice.len]; const name_ty = try zcu.arrayType(.{ - .len = name.len, + .len = name_slice.len, .child = .u8_type, .sentinel = .zero_u8, }); const name_val = try zcu.intern(.{ .aggregate = .{ .ty = name_ty.toIntern(), - .storage = .{ .bytes = name }, + .storage = .{ .bytes = name.toString() }, } }); try writer.writeAll("static "); @@ -2624,7 +2610,7 @@ pub fn genErrDecls(o: *Object) !void { ); try writer.writeAll(" = {"); for (zcu.global_error_set.keys(), 0..) |name_nts, value| { - const name = ip.stringToSlice(name_nts); + const name = name_nts.toSlice(ip); if (value != 0) try writer.writeByte(','); try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{ fmtIdent(name), @@ -2672,7 +2658,7 @@ fn genExports(o: *Object) !void { for (exports.items[1..]) |@"export"| { try fwd.writeAll("zig_extern "); if (@"export".opts.linkage == .weak) try fwd.writeAll("zig_weak_linkage "); - const export_name = ip.stringToSlice(@"export".opts.name); + const export_name = @"export".opts.name.toSlice(ip); try o.dg.renderTypeAndName( fwd, decl.typeOf(zcu), @@ -2685,11 +2671,11 @@ fn genExports(o: *Object) !void { try fwd.print(" zig_mangled_export({ }, {s}, {s})", .{ fmtIdent(export_name), fmtStringLiteral(export_name, null), - fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), + fmtStringLiteral(exports.items[0].opts.name.toSlice(ip), null), }); } else { try fwd.print(" zig_export({s}, {s})", .{ - fmtStringLiteral(ip.stringToSlice(exports.items[0].opts.name), null), + fmtStringLiteral(exports.items[0].opts.name.toSlice(ip), null), fmtStringLiteral(export_name, null), }); } @@ -2719,17 +2705,18 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn try w.writeAll(") {\n switch (tag) {\n"); const tag_names = enum_ty.enumFields(zcu); for (0..tag_names.len) |tag_index| { - const tag_name = ip.stringToSlice(tag_names.get(ip)[tag_index]); + const tag_name = tag_names.get(ip)[tag_index]; + const tag_name_len = tag_name.length(ip); const tag_val = try zcu.enumValueFieldIndex(enum_ty, @intCast(tag_index)); const name_ty = try zcu.arrayType(.{ - .len = tag_name.len, + .len = tag_name_len, .child = .u8_type, .sentinel = .zero_u8, }); const name_val = try zcu.intern(.{ .aggregate = .{ .ty = name_ty.toIntern(), - .storage = .{ .bytes = tag_name }, + .storage = .{ .bytes = tag_name.toString() }, } }); try w.print(" case {}: {{\n static ", .{ @@ -2742,7 +2729,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn try o.dg.renderType(w, name_slice_ty); try w.print("){{{}, {}}};\n", .{ fmtIdent("name"), - try o.dg.fmtIntLiteral(try zcu.intValue(Type.usize, tag_name.len), .Other), + try o.dg.fmtIntLiteral(try zcu.intValue(Type.usize, tag_name_len), .Other), }); try w.writeAll(" }\n"); @@ -2810,7 +2797,7 @@ pub fn genFunc(f: *Function) !void { try o.indent_writer.insertNewline(); if (!is_global) try o.writer().writeAll("static "); - if (zcu.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| + if (decl.@"linksection".toSlice(&zcu.intern_pool)) |s| try o.writer().print("zig_linksection_fn({s}) ", .{fmtStringLiteral(s, null)}); try o.dg.renderFunctionSignature(o.writer(), decl_index, .complete, .{ .export_index = 0 }); try o.writer().writeByte(' '); @@ -2900,7 +2887,7 @@ pub fn genDecl(o: *Object) !void { if (!is_global) try w.writeAll("static "); if (variable.is_weak_linkage) try w.writeAll("zig_weak_linkage "); if (variable.is_threadlocal and !o.dg.mod.single_threaded) try w.writeAll("zig_threadlocal "); - if (zcu.intern_pool.stringToSliceUnwrap(decl.@"linksection")) |s| + if (decl.@"linksection".toSlice(&zcu.intern_pool)) |s| try w.print("zig_linksection({s}) ", .{fmtStringLiteral(s, null)}); const decl_c_value = .{ .decl = decl_index }; try o.dg.renderTypeAndName(w, decl_ty, decl_c_value, .{}, decl.alignment, .complete); @@ -2933,7 +2920,7 @@ pub fn genDeclValue( switch (o.dg.pass) { .decl => |decl_index| { if (zcu.decl_exports.get(decl_index)) |exports| { - const export_name = zcu.intern_pool.stringToSlice(exports.items[0].opts.name); + const export_name = exports.items[0].opts.name.toSlice(&zcu.intern_pool); if (isMangledIdent(export_name, true)) { try fwd_decl_writer.print(" zig_mangled_final({ }, {s})", .{ fmtIdent(export_name), fmtStringLiteral(export_name, null), @@ -2949,7 +2936,7 @@ pub fn genDeclValue( const w = o.writer(); if (!is_global) try w.writeAll("static "); - if (zcu.intern_pool.stringToSliceUnwrap(@"linksection")) |s| + if (@"linksection".toSlice(&zcu.intern_pool)) |s| try w.print("zig_linksection({s}) ", .{fmtStringLiteral(s, null)}); try o.dg.renderTypeAndName(w, ty, decl_c_value, Const, alignment, .complete); try w.writeAll(" = "); @@ -5453,49 +5440,39 @@ fn fieldLocation( ) union(enum) { begin: void, field: CValue, - byte_offset: u32, - end: void, + byte_offset: u64, } { const ip = &zcu.intern_pool; const container_ty = Type.fromInterned(ip.indexToKey(container_ptr_ty.toIntern()).ptr_type.child); switch (ip.indexToKey(container_ty.toIntern())) { .struct_type => { const loaded_struct = ip.loadStructType(container_ty.toIntern()); - switch (loaded_struct.layout) { - .auto, .@"extern" => { - var field_it = loaded_struct.iterateRuntimeOrder(ip); - var before = true; - while (field_it.next()) |next_field_index| { - if (next_field_index == field_index) before = false; - if (before) continue; - const field_type = Type.fromInterned(loaded_struct.field_types.get(ip)[next_field_index]); - if (!field_type.hasRuntimeBitsIgnoreComptime(zcu)) continue; - return .{ .field = if (loaded_struct.fieldName(ip, next_field_index).unwrap()) |field_name| - .{ .identifier = ip.stringToSlice(field_name) } - else - .{ .field = next_field_index } }; - } - return if (container_ty.hasRuntimeBitsIgnoreComptime(zcu)) .end else .begin; - }, - .@"packed" => return if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0) + return switch (loaded_struct.layout) { + .auto, .@"extern" => if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu)) + .begin + else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu)) + .{ .byte_offset = loaded_struct.offsets.get(ip)[field_index] } + else + .{ .field = if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name| + .{ .identifier = field_name.toSlice(ip) } + else + .{ .field = field_index } }, + .@"packed" => if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0) .{ .byte_offset = @divExact(zcu.structPackedFieldBitOffset(loaded_struct, field_index) + container_ptr_ty.ptrInfo(zcu).packed_offset.bit_offset, 8) } else .begin, - } - }, - .anon_struct_type => |anon_struct_info| { - for (field_index..anon_struct_info.types.len) |next_field_index| { - if (anon_struct_info.values.get(ip)[next_field_index] != .none) continue; - const field_type = Type.fromInterned(anon_struct_info.types.get(ip)[next_field_index]); - if (!field_type.hasRuntimeBitsIgnoreComptime(zcu)) continue; - return .{ .field = if (anon_struct_info.fieldName(ip, next_field_index).unwrap()) |field_name| - .{ .identifier = ip.stringToSlice(field_name) } - else - .{ .field = next_field_index } }; - } - return if (container_ty.hasRuntimeBitsIgnoreComptime(zcu)) .end else .begin; + }; }, + .anon_struct_type => |anon_struct_info| return if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu)) + .begin + else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu)) + .{ .byte_offset = container_ty.structFieldOffset(field_index, zcu) } + else + .{ .field = if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name| + .{ .identifier = field_name.toSlice(ip) } + else + .{ .field = field_index } }, .union_type => { const loaded_union = ip.loadUnionType(container_ty.toIntern()); switch (loaded_union.getLayout(ip)) { @@ -5508,9 +5485,9 @@ fn fieldLocation( .begin; const field_name = loaded_union.loadTagType(ip).names.get(ip)[field_index]; return .{ .field = if (loaded_union.hasTag(ip)) - .{ .payload_identifier = ip.stringToSlice(field_name) } + .{ .payload_identifier = field_name.toSlice(ip) } else - .{ .identifier = ip.stringToSlice(field_name) } }; + .{ .identifier = field_name.toSlice(ip) } }; }, .@"packed" => return .begin, } @@ -5591,10 +5568,6 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)), }); }, - .end => { - try f.writeCValue(writer, field_ptr_val, .Other); - try writer.print(" - {}", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))}); - }, } try writer.writeAll(";\n"); @@ -5639,11 +5612,6 @@ fn fieldPtr( try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)), }); }, - .end => { - try writer.writeByte('('); - try f.writeCValue(writer, container_ptr_val, .Other); - try writer.print(" + {})", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))}); - }, } try writer.writeAll(";\n"); @@ -5675,7 +5643,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { const loaded_struct = ip.loadStructType(struct_ty.toIntern()); switch (loaded_struct.layout) { .auto, .@"extern" => break :field_name if (loaded_struct.fieldName(ip, extra.field_index).unwrap()) |field_name| - .{ .identifier = ip.stringToSlice(field_name) } + .{ .identifier = field_name.toSlice(ip) } else .{ .field = extra.field_index }, .@"packed" => { @@ -5733,7 +5701,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { } }, .anon_struct_type => |anon_struct_info| if (anon_struct_info.fieldName(ip, extra.field_index).unwrap()) |field_name| - .{ .identifier = ip.stringToSlice(field_name) } + .{ .identifier = field_name.toSlice(ip) } else .{ .field = extra.field_index }, .union_type => field_name: { @@ -5742,9 +5710,9 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { .auto, .@"extern" => { const name = loaded_union.loadTagType(ip).names.get(ip)[extra.field_index]; break :field_name if (loaded_union.hasTag(ip)) - .{ .payload_identifier = ip.stringToSlice(name) } + .{ .payload_identifier = name.toSlice(ip) } else - .{ .identifier = ip.stringToSlice(name) }; + .{ .identifier = name.toSlice(ip) }; }, .@"packed" => { const operand_lval = if (struct_byval == .constant) blk: { @@ -7094,7 +7062,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { const a = try Assignment.start(f, writer, field_ty); try f.writeCValueMember(writer, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name| - .{ .identifier = ip.stringToSlice(field_name) } + .{ .identifier = field_name.toSlice(ip) } else .{ .field = field_index }); try a.assign(f, writer); @@ -7174,7 +7142,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { const a = try Assignment.start(f, writer, field_ty); try f.writeCValueMember(writer, local, if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name| - .{ .identifier = ip.stringToSlice(field_name) } + .{ .identifier = field_name.toSlice(ip) } else .{ .field = field_index }); try a.assign(f, writer); @@ -7222,8 +7190,8 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { try writer.print("{}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, zcu))}); try a.end(f, writer); } - break :field .{ .payload_identifier = ip.stringToSlice(field_name) }; - } else .{ .identifier = ip.stringToSlice(field_name) }; + break :field .{ .payload_identifier = field_name.toSlice(ip) }; + } else .{ .identifier = field_name.toSlice(ip) }; const a = try Assignment.start(f, writer, payload_ty); try f.writeCValueMember(writer, local, field); |
