| ... | @@ -56,6 +56,7 @@ pub const Mir = struct { | ... | @@ -56,6 +56,7 @@ pub const Mir = struct { |
| 56 | /// less than the natural alignment. | 56 | /// less than the natural alignment. |
| 57 | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), | 57 | uavs: std.AutoArrayHashMapUnmanaged(InternPool.Index, Alignment), |
| 58 | // These remaining fields are essentially just an owned version of `link.C.AvBlock`. | 58 | // These remaining fields are essentially just an owned version of `link.C.AvBlock`. |
| | 59 | code_header: []u8, |
| 59 | code: []u8, | 60 | code: []u8, |
| 60 | fwd_decl: []u8, | 61 | fwd_decl: []u8, |
| 61 | ctype_pool: CType.Pool, | 62 | ctype_pool: CType.Pool, |
| ... | @@ -63,6 +64,7 @@ pub const Mir = struct { | ... | @@ -63,6 +64,7 @@ pub const Mir = struct { |
| 63 | | 64 | |
| 64 | pub fn deinit(mir: *Mir, gpa: Allocator) void { | 65 | pub fn deinit(mir: *Mir, gpa: Allocator) void { |
| 65 | mir.uavs.deinit(gpa); | 66 | mir.uavs.deinit(gpa); |
| | 67 | gpa.free(mir.code_header); |
| 66 | gpa.free(mir.code); | 68 | gpa.free(mir.code); |
| 67 | gpa.free(mir.fwd_decl); | 69 | gpa.free(mir.fwd_decl); |
| 68 | mir.ctype_pool.deinit(gpa); | 70 | mir.ctype_pool.deinit(gpa); |
| ... | @@ -345,23 +347,23 @@ fn isReservedIdent(ident: []const u8) bool { | ... | @@ -345,23 +347,23 @@ fn isReservedIdent(ident: []const u8) bool { |
| 345 | | 347 | |
| 346 | fn formatIdent( | 348 | fn formatIdent( |
| 347 | ident: []const u8, | 349 | ident: []const u8, |
| 348 | writer: *Writer, | 350 | w: *Writer, |
| 349 | comptime fmt_str: []const u8, | 351 | comptime fmt_str: []const u8, |
| 350 | ) Writer.Error!void { | 352 | ) Writer.Error!void { |
| 351 | const solo = fmt_str.len != 0 and fmt_str[0] == ' '; // space means solo; not part of a bigger ident. | 353 | const solo = fmt_str.len != 0 and fmt_str[0] == ' '; // space means solo; not part of a bigger ident. |
| 352 | if (solo and isReservedIdent(ident)) { | 354 | if (solo and isReservedIdent(ident)) { |
| 353 | try writer.writeAll("zig_e_"); | 355 | try w.writeAll("zig_e_"); |
| 354 | } | 356 | } |
| 355 | for (ident, 0..) |c, i| { | 357 | for (ident, 0..) |c, i| { |
| 356 | switch (c) { | 358 | switch (c) { |
| 357 | 'a'...'z', 'A'...'Z', '_' => try writer.writeByte(c), | 359 | 'a'...'z', 'A'...'Z', '_' => try w.writeByte(c), |
| 358 | '.' => try writer.writeByte('_'), | 360 | '.' => try w.writeByte('_'), |
| 359 | '0'...'9' => if (i == 0) { | 361 | '0'...'9' => if (i == 0) { |
| 360 | try writer.print("_{x:2}", .{c}); | 362 | try w.print("_{x:2}", .{c}); |
| 361 | } else { | 363 | } else { |
| 362 | try writer.writeByte(c); | 364 | try w.writeByte(c); |
| 363 | }, | 365 | }, |
| 364 | else => try writer.print("_{x:2}", .{c}), | 366 | else => try w.print("_{x:2}", .{c}), |
| 365 | } | 367 | } |
| 366 | } | 368 | } |
| 367 | } | 369 | } |
| ... | @@ -375,13 +377,13 @@ const CTypePoolStringFormatData = struct { | ... | @@ -375,13 +377,13 @@ const CTypePoolStringFormatData = struct { |
| 375 | }; | 377 | }; |
| 376 | fn formatCTypePoolString( | 378 | fn formatCTypePoolString( |
| 377 | data: CTypePoolStringFormatData, | 379 | data: CTypePoolStringFormatData, |
| 378 | writer: *Writer, | 380 | w: *Writer, |
| 379 | comptime fmt_str: []const u8, | 381 | comptime fmt_str: []const u8, |
| 380 | ) Writer.Error!void { | 382 | ) Writer.Error!void { |
| 381 | if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice| | 383 | if (data.ctype_pool_string.toSlice(data.ctype_pool)) |slice| |
| 382 | try formatIdent(slice, writer, fmt_str) | 384 | try formatIdent(slice, w, fmt_str) |
| 383 | else | 385 | else |
| 384 | try writer.print("{f}", .{data.ctype_pool_string.fmt(data.ctype_pool)}); | 386 | try w.print("{f}", .{data.ctype_pool_string.fmt(data.ctype_pool)}); |
| 385 | } | 387 | } |
| 386 | pub fn fmtCTypePoolString( | 388 | pub fn fmtCTypePoolString( |
| 387 | ctype_pool_string: CType.Pool.String, | 389 | ctype_pool_string: CType.Pool.String, |
| ... | @@ -441,18 +443,18 @@ pub const Function = struct { | ... | @@ -441,18 +443,18 @@ pub const Function = struct { |
| 441 | const ty = f.typeOf(ref); | 443 | const ty = f.typeOf(ref); |
| 442 | | 444 | |
| 443 | const result: CValue = if (lowersToArray(ty, pt)) result: { | 445 | const result: CValue = if (lowersToArray(ty, pt)) result: { |
| 444 | const writer = &f.object.code_header.buffered_writer; | 446 | const ch = &f.object.code_header.buffered_writer; |
| 445 | const decl_c_value = try f.allocLocalValue(.{ | 447 | const decl_c_value = try f.allocLocalValue(.{ |
| 446 | .ctype = try f.ctypeFromType(ty, .complete), | 448 | .ctype = try f.ctypeFromType(ty, .complete), |
| 447 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)), | 449 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt.zcu)), |
| 448 | }); | 450 | }); |
| 449 | const gpa = f.object.dg.gpa; | 451 | const gpa = f.object.dg.gpa; |
| 450 | try f.allocs.put(gpa, decl_c_value.new_local, false); | 452 | try f.allocs.put(gpa, decl_c_value.new_local, false); |
| 451 | try writer.writeAll("static "); | 453 | try ch.writeAll("static "); |
| 452 | try f.object.dg.renderTypeAndName(writer, ty, decl_c_value, Const, .none, .complete); | 454 | try f.object.dg.renderTypeAndName(ch, ty, decl_c_value, Const, .none, .complete); |
| 453 | try writer.writeAll(" = "); | 455 | try ch.writeAll(" = "); |
| 454 | try f.object.dg.renderValue(writer, val, .StaticInitializer); | 456 | try f.object.dg.renderValue(ch, val, .StaticInitializer); |
| 455 | try writer.writeAll(";\n "); | 457 | try ch.writeAll(";\n "); |
| 456 | break :result .{ .local = decl_c_value.new_local }; | 458 | break :result .{ .local = decl_c_value.new_local }; |
| 457 | } else .{ .constant = val }; | 459 | } else .{ .constant = val }; |
| 458 | | 460 | |
| ... | @@ -979,7 +981,7 @@ pub const DeclGen = struct { | ... | @@ -979,7 +981,7 @@ pub const DeclGen = struct { |
| 979 | | 981 | |
| 980 | fn renderValue( | 982 | fn renderValue( |
| 981 | dg: *DeclGen, | 983 | dg: *DeclGen, |
| 982 | writer: *Writer, | 984 | w: *Writer, |
| 983 | val: Value, | 985 | val: Value, |
| 984 | location: ValueRenderLocation, | 986 | location: ValueRenderLocation, |
| 985 | ) Error!void { | 987 | ) Error!void { |
| ... | @@ -995,7 +997,7 @@ pub const DeclGen = struct { | ... | @@ -995,7 +997,7 @@ pub const DeclGen = struct { |
| 995 | }; | 997 | }; |
| 996 | | 998 | |
| 997 | const ty = val.typeOf(zcu); | 999 | const ty = val.typeOf(zcu); |
| 998 | if (val.isUndefDeep(zcu)) return dg.renderUndefValue(writer, ty, location); | 1000 | if (val.isUndefDeep(zcu)) return dg.renderUndefValue(w, ty, location); |
| 999 | const ctype = try dg.ctypeFromType(ty, location.toCTypeKind()); | 1001 | const ctype = try dg.ctypeFromType(ty, location.toCTypeKind()); |
| 1000 | switch (ip.indexToKey(val.toIntern())) { | 1002 | switch (ip.indexToKey(val.toIntern())) { |
| 1001 | // types, not values | 1003 | // types, not values |
| ... | @@ -1028,8 +1030,8 @@ pub const DeclGen = struct { | ... | @@ -1028,8 +1030,8 @@ pub const DeclGen = struct { |
| 1028 | .empty_tuple => unreachable, | 1030 | .empty_tuple => unreachable, |
| 1029 | .@"unreachable" => unreachable, | 1031 | .@"unreachable" => unreachable, |
| 1030 | | 1032 | |
| 1031 | .false => try writer.writeAll("false"), | 1033 | .false => try w.writeAll("false"), |
| 1032 | .true => try writer.writeAll("true"), | 1034 | .true => try w.writeAll("true"), |
| 1033 | }, | 1035 | }, |
| 1034 | .variable, | 1036 | .variable, |
| 1035 | .@"extern", | 1037 | .@"extern", |
| ... | @@ -1038,45 +1040,45 @@ pub const DeclGen = struct { | ... | @@ -1038,45 +1040,45 @@ pub const DeclGen = struct { |
| 1038 | .empty_enum_value, | 1040 | .empty_enum_value, |
| 1039 | => unreachable, // non-runtime values | 1041 | => unreachable, // non-runtime values |
| 1040 | .int => |int| switch (int.storage) { | 1042 | .int => |int| switch (int.storage) { |
| 1041 | .u64, .i64, .big_int => try writer.print("{f}", .{try dg.fmtIntLiteral(val, location)}), | 1043 | .u64, .i64, .big_int => try w.print("{f}", .{try dg.fmtIntLiteral(val, location)}), |
| 1042 | .lazy_align, .lazy_size => { | 1044 | .lazy_align, .lazy_size => { |
| 1043 | try writer.writeAll("(("); | 1045 | try w.writeAll("(("); |
| 1044 | try dg.renderCType(writer, ctype); | 1046 | try dg.renderCType(w, ctype); |
| 1045 | try writer.print("){fx})", .{try dg.fmtIntLiteral( | 1047 | try w.print("){fx})", .{try dg.fmtIntLiteral( |
| 1046 | try pt.intValue(.usize, val.toUnsignedInt(zcu)), | 1048 | try pt.intValue(.usize, val.toUnsignedInt(zcu)), |
| 1047 | .Other, | 1049 | .Other, |
| 1048 | )}); | 1050 | )}); |
| 1049 | }, | 1051 | }, |
| 1050 | }, | 1052 | }, |
| 1051 | .err => |err| try dg.renderErrorName(writer, err.name), | 1053 | .err => |err| try dg.renderErrorName(w, err.name), |
| 1052 | .error_union => |error_union| switch (ctype.info(ctype_pool)) { | 1054 | .error_union => |error_union| switch (ctype.info(ctype_pool)) { |
| 1053 | .basic => switch (error_union.val) { | 1055 | .basic => switch (error_union.val) { |
| 1054 | .err_name => |err_name| try dg.renderErrorName(writer, err_name), | 1056 | .err_name => |err_name| try dg.renderErrorName(w, err_name), |
| 1055 | .payload => try writer.writeByte('0'), | 1057 | .payload => try w.writeByte('0'), |
| 1056 | }, | 1058 | }, |
| 1057 | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 1059 | .pointer, .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 1058 | .aggregate => |aggregate| { | 1060 | .aggregate => |aggregate| { |
| 1059 | if (!location.isInitializer()) { | 1061 | if (!location.isInitializer()) { |
| 1060 | try writer.writeByte('('); | 1062 | try w.writeByte('('); |
| 1061 | try dg.renderCType(writer, ctype); | 1063 | try dg.renderCType(w, ctype); |
| 1062 | try writer.writeByte(')'); | 1064 | try w.writeByte(')'); |
| 1063 | } | 1065 | } |
| 1064 | try writer.writeByte('{'); | 1066 | try w.writeByte('{'); |
| 1065 | for (0..aggregate.fields.len) |field_index| { | 1067 | for (0..aggregate.fields.len) |field_index| { |
| 1066 | if (field_index > 0) try writer.writeByte(','); | 1068 | if (field_index > 0) try w.writeByte(','); |
| 1067 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | 1069 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { |
| 1068 | .@"error" => switch (error_union.val) { | 1070 | .@"error" => switch (error_union.val) { |
| 1069 | .err_name => |err_name| try dg.renderErrorName(writer, err_name), | 1071 | .err_name => |err_name| try dg.renderErrorName(w, err_name), |
| 1070 | .payload => try writer.writeByte('0'), | 1072 | .payload => try w.writeByte('0'), |
| 1071 | }, | 1073 | }, |
| 1072 | .payload => switch (error_union.val) { | 1074 | .payload => switch (error_union.val) { |
| 1073 | .err_name => try dg.renderUndefValue( | 1075 | .err_name => try dg.renderUndefValue( |
| 1074 | writer, | 1076 | w, |
| 1075 | ty.errorUnionPayload(zcu), | 1077 | ty.errorUnionPayload(zcu), |
| 1076 | initializer_type, | 1078 | initializer_type, |
| 1077 | ), | 1079 | ), |
| 1078 | .payload => |payload| try dg.renderValue( | 1080 | .payload => |payload| try dg.renderValue( |
| 1079 | writer, | 1081 | w, |
| 1080 | Value.fromInterned(payload), | 1082 | Value.fromInterned(payload), |
| 1081 | initializer_type, | 1083 | initializer_type, |
| 1082 | ), | 1084 | ), |
| ... | @@ -1084,10 +1086,10 @@ pub const DeclGen = struct { | ... | @@ -1084,10 +1086,10 @@ pub const DeclGen = struct { |
| 1084 | else => unreachable, | 1086 | else => unreachable, |
| 1085 | } | 1087 | } |
| 1086 | } | 1088 | } |
| 1087 | try writer.writeByte('}'); | 1089 | try w.writeByte('}'); |
| 1088 | }, | 1090 | }, |
| 1089 | }, | 1091 | }, |
| 1090 | .enum_tag => |enum_tag| try dg.renderValue(writer, Value.fromInterned(enum_tag.int), location), | 1092 | .enum_tag => |enum_tag| try dg.renderValue(w, Value.fromInterned(enum_tag.int), location), |
| 1091 | .float => { | 1093 | .float => { |
| 1092 | const bits = ty.floatBits(target); | 1094 | const bits = ty.floatBits(target); |
| 1093 | const f128_val = val.toFloat(f128, zcu); | 1095 | const f128_val = val.toFloat(f128, zcu); |
| ... | @@ -1114,18 +1116,18 @@ pub const DeclGen = struct { | ... | @@ -1114,18 +1116,18 @@ pub const DeclGen = struct { |
| 1114 | | 1116 | |
| 1115 | var empty = true; | 1117 | var empty = true; |
| 1116 | if (std.math.isFinite(f128_val)) { | 1118 | if (std.math.isFinite(f128_val)) { |
| 1117 | try writer.writeAll("zig_make_"); | 1119 | try w.writeAll("zig_make_"); |
| 1118 | try dg.renderTypeForBuiltinFnName(writer, ty); | 1120 | try dg.renderTypeForBuiltinFnName(w, ty); |
| 1119 | try writer.writeByte('('); | 1121 | try w.writeByte('('); |
| 1120 | switch (bits) { | 1122 | switch (bits) { |
| 1121 | 16 => try writer.print("{x}", .{val.toFloat(f16, zcu)}), | 1123 | 16 => try w.print("{x}", .{val.toFloat(f16, zcu)}), |
| 1122 | 32 => try writer.print("{x}", .{val.toFloat(f32, zcu)}), | 1124 | 32 => try w.print("{x}", .{val.toFloat(f32, zcu)}), |
| 1123 | 64 => try writer.print("{x}", .{val.toFloat(f64, zcu)}), | 1125 | 64 => try w.print("{x}", .{val.toFloat(f64, zcu)}), |
| 1124 | 80 => try writer.print("{x}", .{val.toFloat(f80, zcu)}), | 1126 | 80 => try w.print("{x}", .{val.toFloat(f80, zcu)}), |
| 1125 | 128 => try writer.print("{x}", .{f128_val}), | 1127 | 128 => try w.print("{x}", .{f128_val}), |
| 1126 | else => unreachable, | 1128 | else => unreachable, |
| 1127 | } | 1129 | } |
| 1128 | try writer.writeAll(", "); | 1130 | try w.writeAll(", "); |
| 1129 | empty = false; | 1131 | empty = false; |
| 1130 | } else { | 1132 | } else { |
| 1131 | // isSignalNan is equivalent to isNan currently, and MSVC doesn't have nans, so prefer nan | 1133 | // isSignalNan is equivalent to isNan currently, and MSVC doesn't have nans, so prefer nan |
| ... | @@ -1149,45 +1151,45 @@ pub const DeclGen = struct { | ... | @@ -1149,45 +1151,45 @@ pub const DeclGen = struct { |
| 1149 | // return dg.fail("Only quiet nans are supported in global variable initializers", .{}); | 1151 | // return dg.fail("Only quiet nans are supported in global variable initializers", .{}); |
| 1150 | } | 1152 | } |
| 1151 | | 1153 | |
| 1152 | try writer.writeAll("zig_"); | 1154 | try w.writeAll("zig_"); |
| 1153 | try writer.writeAll(if (location == .StaticInitializer) "init" else "make"); | 1155 | try w.writeAll(if (location == .StaticInitializer) "init" else "make"); |
| 1154 | try writer.writeAll("_special_"); | 1156 | try w.writeAll("_special_"); |
| 1155 | try dg.renderTypeForBuiltinFnName(writer, ty); | 1157 | try dg.renderTypeForBuiltinFnName(w, ty); |
| 1156 | try writer.writeByte('('); | 1158 | try w.writeByte('('); |
| 1157 | if (std.math.signbit(f128_val)) try writer.writeByte('-'); | 1159 | if (std.math.signbit(f128_val)) try w.writeByte('-'); |
| 1158 | try writer.writeAll(", "); | 1160 | try w.writeAll(", "); |
| 1159 | try writer.writeAll(operation); | 1161 | try w.writeAll(operation); |
| 1160 | try writer.writeAll(", "); | 1162 | try w.writeAll(", "); |
| 1161 | if (std.math.isNan(f128_val)) switch (bits) { | 1163 | if (std.math.isNan(f128_val)) switch (bits) { |
| 1162 | // We only actually need to pass the significand, but it will get | 1164 | // We only actually need to pass the significand, but it will get |
| 1163 | // properly masked anyway, so just pass the whole value. | 1165 | // properly masked anyway, so just pass the whole value. |
| 1164 | 16 => try writer.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, zcu)))}), | 1166 | 16 => try w.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, zcu)))}), |
| 1165 | 32 => try writer.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, zcu)))}), | 1167 | 32 => try w.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, zcu)))}), |
| 1166 | 64 => try writer.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, zcu)))}), | 1168 | 64 => try w.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, zcu)))}), |
| 1167 | 80 => try writer.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, zcu)))}), | 1169 | 80 => try w.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, zcu)))}), |
| 1168 | 128 => try writer.print("\"0x{x}\"", .{@as(u128, @bitCast(f128_val))}), | 1170 | 128 => try w.print("\"0x{x}\"", .{@as(u128, @bitCast(f128_val))}), |
| 1169 | else => unreachable, | 1171 | else => unreachable, |
| 1170 | }; | 1172 | }; |
| 1171 | try writer.writeAll(", "); | 1173 | try w.writeAll(", "); |
| 1172 | empty = false; | 1174 | empty = false; |
| 1173 | } | 1175 | } |
| 1174 | try writer.print("{fx}", .{try dg.fmtIntLiteral( | 1176 | try w.print("{fx}", .{try dg.fmtIntLiteral( |
| 1175 | try pt.intValue_big(repr_ty, repr_val_big.toConst()), | 1177 | try pt.intValue_big(repr_ty, repr_val_big.toConst()), |
| 1176 | location, | 1178 | location, |
| 1177 | )}); | 1179 | )}); |
| 1178 | if (!empty) try writer.writeByte(')'); | 1180 | if (!empty) try w.writeByte(')'); |
| 1179 | }, | 1181 | }, |
| 1180 | .slice => |slice| { | 1182 | .slice => |slice| { |
| 1181 | const aggregate = ctype.info(ctype_pool).aggregate; | 1183 | const aggregate = ctype.info(ctype_pool).aggregate; |
| 1182 | if (!location.isInitializer()) { | 1184 | if (!location.isInitializer()) { |
| 1183 | try writer.writeByte('('); | 1185 | try w.writeByte('('); |
| 1184 | try dg.renderCType(writer, ctype); | 1186 | try dg.renderCType(w, ctype); |
| 1185 | try writer.writeByte(')'); | 1187 | try w.writeByte(')'); |
| 1186 | } | 1188 | } |
| 1187 | try writer.writeByte('{'); | 1189 | try w.writeByte('{'); |
| 1188 | for (0..aggregate.fields.len) |field_index| { | 1190 | for (0..aggregate.fields.len) |field_index| { |
| 1189 | if (field_index > 0) try writer.writeByte(','); | 1191 | if (field_index > 0) try w.writeByte(','); |
| 1190 | try dg.renderValue(writer, Value.fromInterned( | 1192 | try dg.renderValue(w, Value.fromInterned( |
| 1191 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | 1193 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { |
| 1192 | .ptr => slice.ptr, | 1194 | .ptr => slice.ptr, |
| 1193 | .len => slice.len, | 1195 | .len => slice.len, |
| ... | @@ -1195,33 +1197,33 @@ pub const DeclGen = struct { | ... | @@ -1195,33 +1197,33 @@ pub const DeclGen = struct { |
| 1195 | }, | 1197 | }, |
| 1196 | ), initializer_type); | 1198 | ), initializer_type); |
| 1197 | } | 1199 | } |
| 1198 | try writer.writeByte('}'); | 1200 | try w.writeByte('}'); |
| 1199 | }, | 1201 | }, |
| 1200 | .ptr => { | 1202 | .ptr => { |
| 1201 | var arena = std.heap.ArenaAllocator.init(zcu.gpa); | 1203 | var arena = std.heap.ArenaAllocator.init(zcu.gpa); |
| 1202 | defer arena.deinit(); | 1204 | defer arena.deinit(); |
| 1203 | const derivation = try val.pointerDerivation(arena.allocator(), pt); | 1205 | const derivation = try val.pointerDerivation(arena.allocator(), pt); |
| 1204 | try dg.renderPointer(writer, derivation, location); | 1206 | try dg.renderPointer(w, derivation, location); |
| 1205 | }, | 1207 | }, |
| 1206 | .opt => |opt| switch (ctype.info(ctype_pool)) { | 1208 | .opt => |opt| switch (ctype.info(ctype_pool)) { |
| 1207 | .basic => if (ctype.isBool()) try writer.writeAll(switch (opt.val) { | 1209 | .basic => if (ctype.isBool()) try w.writeAll(switch (opt.val) { |
| 1208 | .none => "true", | 1210 | .none => "true", |
| 1209 | else => "false", | 1211 | else => "false", |
| 1210 | }) else switch (opt.val) { | 1212 | }) else switch (opt.val) { |
| 1211 | .none => try writer.writeByte('0'), | 1213 | .none => try w.writeByte('0'), |
| 1212 | else => |payload| switch (ip.indexToKey(payload)) { | 1214 | else => |payload| switch (ip.indexToKey(payload)) { |
| 1213 | .undef => |err_ty| try dg.renderUndefValue( | 1215 | .undef => |err_ty| try dg.renderUndefValue( |
| 1214 | writer, | 1216 | w, |
| 1215 | .fromInterned(err_ty), | 1217 | .fromInterned(err_ty), |
| 1216 | location, | 1218 | location, |
| 1217 | ), | 1219 | ), |
| 1218 | .err => |err| try dg.renderErrorName(writer, err.name), | 1220 | .err => |err| try dg.renderErrorName(w, err.name), |
| 1219 | else => unreachable, | 1221 | else => unreachable, |
| 1220 | }, | 1222 | }, |
| 1221 | }, | 1223 | }, |
| 1222 | .pointer => switch (opt.val) { | 1224 | .pointer => switch (opt.val) { |
| 1223 | .none => try writer.writeAll("NULL"), | 1225 | .none => try w.writeAll("NULL"), |
| 1224 | else => |payload| try dg.renderValue(writer, Value.fromInterned(payload), location), | 1226 | else => |payload| try dg.renderValue(w, Value.fromInterned(payload), location), |
| 1225 | }, | 1227 | }, |
| 1226 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, | 1228 | .aligned, .array, .vector, .fwd_decl, .function => unreachable, |
| 1227 | .aggregate => |aggregate| { | 1229 | .aggregate => |aggregate| { |
| ... | @@ -1230,7 +1232,7 @@ pub const DeclGen = struct { | ... | @@ -1230,7 +1232,7 @@ pub const DeclGen = struct { |
| 1230 | else => |payload| switch (aggregate.fields.at(0, ctype_pool).name.index) { | 1232 | else => |payload| switch (aggregate.fields.at(0, ctype_pool).name.index) { |
| 1231 | .is_null, .payload => {}, | 1233 | .is_null, .payload => {}, |
| 1232 | .ptr, .len => return dg.renderValue( | 1234 | .ptr, .len => return dg.renderValue( |
| 1233 | writer, | 1235 | w, |
| 1234 | Value.fromInterned(payload), | 1236 | Value.fromInterned(payload), |
| 1235 | location, | 1237 | location, |
| 1236 | ), | 1238 | ), |
| ... | @@ -1238,48 +1240,48 @@ pub const DeclGen = struct { | ... | @@ -1238,48 +1240,48 @@ pub const DeclGen = struct { |
| 1238 | }, | 1240 | }, |
| 1239 | } | 1241 | } |
| 1240 | if (!location.isInitializer()) { | 1242 | if (!location.isInitializer()) { |
| 1241 | try writer.writeByte('('); | 1243 | try w.writeByte('('); |
| 1242 | try dg.renderCType(writer, ctype); | 1244 | try dg.renderCType(w, ctype); |
| 1243 | try writer.writeByte(')'); | 1245 | try w.writeByte(')'); |
| 1244 | } | 1246 | } |
| 1245 | try writer.writeByte('{'); | 1247 | try w.writeByte('{'); |
| 1246 | for (0..aggregate.fields.len) |field_index| { | 1248 | for (0..aggregate.fields.len) |field_index| { |
| 1247 | if (field_index > 0) try writer.writeByte(','); | 1249 | if (field_index > 0) try w.writeByte(','); |
| 1248 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { | 1250 | switch (aggregate.fields.at(field_index, ctype_pool).name.index) { |
| 1249 | .is_null => try writer.writeAll(switch (opt.val) { | 1251 | .is_null => try w.writeAll(switch (opt.val) { |
| 1250 | .none => "true", | 1252 | .none => "true", |
| 1251 | else => "false", | 1253 | else => "false", |
| 1252 | }), | 1254 | }), |
| 1253 | .payload => switch (opt.val) { | 1255 | .payload => switch (opt.val) { |
| 1254 | .none => try dg.renderUndefValue( | 1256 | .none => try dg.renderUndefValue( |
| 1255 | writer, | 1257 | w, |
| 1256 | ty.optionalChild(zcu), | 1258 | ty.optionalChild(zcu), |
| 1257 | initializer_type, | 1259 | initializer_type, |
| 1258 | ), | 1260 | ), |
| 1259 | else => |payload| try dg.renderValue( | 1261 | else => |payload| try dg.renderValue( |
| 1260 | writer, | 1262 | w, |
| 1261 | Value.fromInterned(payload), | 1263 | Value.fromInterned(payload), |
| 1262 | initializer_type, | 1264 | initializer_type, |
| 1263 | ), | 1265 | ), |
| 1264 | }, | 1266 | }, |
| 1265 | .ptr => try writer.writeAll("NULL"), | 1267 | .ptr => try w.writeAll("NULL"), |
| 1266 | .len => try dg.renderUndefValue(writer, .usize, initializer_type), | 1268 | .len => try dg.renderUndefValue(w, .usize, initializer_type), |
| 1267 | else => unreachable, | 1269 | else => unreachable, |
| 1268 | } | 1270 | } |
| 1269 | } | 1271 | } |
| 1270 | try writer.writeByte('}'); | 1272 | try w.writeByte('}'); |
| 1271 | }, | 1273 | }, |
| 1272 | }, | 1274 | }, |
| 1273 | .aggregate => switch (ip.indexToKey(ty.toIntern())) { | 1275 | .aggregate => switch (ip.indexToKey(ty.toIntern())) { |
| 1274 | .array_type, .vector_type => { | 1276 | .array_type, .vector_type => { |
| 1275 | if (location == .FunctionArgument) { | 1277 | if (location == .FunctionArgument) { |
| 1276 | try writer.writeByte('('); | 1278 | try w.writeByte('('); |
| 1277 | try dg.renderCType(writer, ctype); | 1279 | try dg.renderCType(w, ctype); |
| 1278 | try writer.writeByte(')'); | 1280 | try w.writeByte(')'); |
| 1279 | } | 1281 | } |
| 1280 | const ai = ty.arrayInfo(zcu); | 1282 | const ai = ty.arrayInfo(zcu); |
| 1281 | if (ai.elem_type.eql(.u8, zcu)) { | 1283 | if (ai.elem_type.eql(.u8, zcu)) { |
| 1282 | var literal: StringLiteral = .init(writer, ty.arrayLenIncludingSentinel(zcu)); | 1284 | var literal: StringLiteral = .init(w, ty.arrayLenIncludingSentinel(zcu)); |
| 1283 | try literal.start(); | 1285 | try literal.start(); |
| 1284 | var index: usize = 0; | 1286 | var index: usize = 0; |
| 1285 | while (index < ai.len) : (index += 1) { | 1287 | while (index < ai.len) : (index += 1) { |
| ... | @@ -1296,28 +1298,28 @@ pub const DeclGen = struct { | ... | @@ -1296,28 +1298,28 @@ pub const DeclGen = struct { |
| 1296 | } | 1298 | } |
| 1297 | try literal.end(); | 1299 | try literal.end(); |
| 1298 | } else { | 1300 | } else { |
| 1299 | try writer.writeByte('{'); | 1301 | try w.writeByte('{'); |
| 1300 | var index: usize = 0; | 1302 | var index: usize = 0; |
| 1301 | while (index < ai.len) : (index += 1) { | 1303 | while (index < ai.len) : (index += 1) { |
| 1302 | if (index != 0) try writer.writeByte(','); | 1304 | if (index != 0) try w.writeByte(','); |
| 1303 | const elem_val = try val.elemValue(pt, index); | 1305 | const elem_val = try val.elemValue(pt, index); |
| 1304 | try dg.renderValue(writer, elem_val, initializer_type); | 1306 | try dg.renderValue(w, elem_val, initializer_type); |
| 1305 | } | 1307 | } |
| 1306 | if (ai.sentinel) |s| { | 1308 | if (ai.sentinel) |s| { |
| 1307 | if (index != 0) try writer.writeByte(','); | 1309 | if (index != 0) try w.writeByte(','); |
| 1308 | try dg.renderValue(writer, s, initializer_type); | 1310 | try dg.renderValue(w, s, initializer_type); |
| 1309 | } | 1311 | } |
| 1310 | try writer.writeByte('}'); | 1312 | try w.writeByte('}'); |
| 1311 | } | 1313 | } |
| 1312 | }, | 1314 | }, |
| 1313 | .tuple_type => |tuple| { | 1315 | .tuple_type => |tuple| { |
| 1314 | if (!location.isInitializer()) { | 1316 | if (!location.isInitializer()) { |
| 1315 | try writer.writeByte('('); | 1317 | try w.writeByte('('); |
| 1316 | try dg.renderCType(writer, ctype); | 1318 | try dg.renderCType(w, ctype); |
| 1317 | try writer.writeByte(')'); | 1319 | try w.writeByte(')'); |
| 1318 | } | 1320 | } |
| 1319 | | 1321 | |
| 1320 | try writer.writeByte('{'); | 1322 | try w.writeByte('{'); |
| 1321 | var empty = true; | 1323 | var empty = true; |
| 1322 | for (0..tuple.types.len) |field_index| { | 1324 | for (0..tuple.types.len) |field_index| { |
| 1323 | const comptime_val = tuple.values.get(ip)[field_index]; | 1325 | const comptime_val = tuple.values.get(ip)[field_index]; |
| ... | @@ -1325,7 +1327,7 @@ pub const DeclGen = struct { | ... | @@ -1325,7 +1327,7 @@ pub const DeclGen = struct { |
| 1325 | const field_ty: Type = .fromInterned(tuple.types.get(ip)[field_index]); | 1327 | const field_ty: Type = .fromInterned(tuple.types.get(ip)[field_index]); |
| 1326 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | 1328 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1327 | | 1329 | |
| 1328 | if (!empty) try writer.writeByte(','); | 1330 | if (!empty) try w.writeByte(','); |
| 1329 | | 1331 | |
| 1330 | const field_val = Value.fromInterned( | 1332 | const field_val = Value.fromInterned( |
| 1331 | switch (ip.indexToKey(val.toIntern()).aggregate.storage) { | 1333 | switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| ... | @@ -1337,30 +1339,30 @@ pub const DeclGen = struct { | ... | @@ -1337,30 +1339,30 @@ pub const DeclGen = struct { |
| 1337 | .repeated_elem => |elem| elem, | 1339 | .repeated_elem => |elem| elem, |
| 1338 | }, | 1340 | }, |
| 1339 | ); | 1341 | ); |
| 1340 | try dg.renderValue(writer, field_val, initializer_type); | 1342 | try dg.renderValue(w, field_val, initializer_type); |
| 1341 | | 1343 | |
| 1342 | empty = false; | 1344 | empty = false; |
| 1343 | } | 1345 | } |
| 1344 | try writer.writeByte('}'); | 1346 | try w.writeByte('}'); |
| 1345 | }, | 1347 | }, |
| 1346 | .struct_type => { | 1348 | .struct_type => { |
| 1347 | const loaded_struct = ip.loadStructType(ty.toIntern()); | 1349 | const loaded_struct = ip.loadStructType(ty.toIntern()); |
| 1348 | switch (loaded_struct.layout) { | 1350 | switch (loaded_struct.layout) { |
| 1349 | .auto, .@"extern" => { | 1351 | .auto, .@"extern" => { |
| 1350 | if (!location.isInitializer()) { | 1352 | if (!location.isInitializer()) { |
| 1351 | try writer.writeByte('('); | 1353 | try w.writeByte('('); |
| 1352 | try dg.renderCType(writer, ctype); | 1354 | try dg.renderCType(w, ctype); |
| 1353 | try writer.writeByte(')'); | 1355 | try w.writeByte(')'); |
| 1354 | } | 1356 | } |
| 1355 | | 1357 | |
| 1356 | try writer.writeByte('{'); | 1358 | try w.writeByte('{'); |
| 1357 | var field_it = loaded_struct.iterateRuntimeOrder(ip); | 1359 | var field_it = loaded_struct.iterateRuntimeOrder(ip); |
| 1358 | var need_comma = false; | 1360 | var need_comma = false; |
| 1359 | while (field_it.next()) |field_index| { | 1361 | while (field_it.next()) |field_index| { |
| 1360 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); | 1362 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1361 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | 1363 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1362 | | 1364 | |
| 1363 | if (need_comma) try writer.writeByte(','); | 1365 | if (need_comma) try w.writeByte(','); |
| 1364 | need_comma = true; | 1366 | need_comma = true; |
| 1365 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { | 1367 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1366 | .bytes => |bytes| try pt.intern(.{ .int = .{ | 1368 | .bytes => |bytes| try pt.intern(.{ .int = .{ |
| ... | @@ -1370,9 +1372,9 @@ pub const DeclGen = struct { | ... | @@ -1370,9 +1372,9 @@ pub const DeclGen = struct { |
| 1370 | .elems => |elems| elems[field_index], | 1372 | .elems => |elems| elems[field_index], |
| 1371 | .repeated_elem => |elem| elem, | 1373 | .repeated_elem => |elem| elem, |
| 1372 | }; | 1374 | }; |
| 1373 | try dg.renderValue(writer, Value.fromInterned(field_val), initializer_type); | 1375 | try dg.renderValue(w, Value.fromInterned(field_val), initializer_type); |
| 1374 | } | 1376 | } |
| 1375 | try writer.writeByte('}'); | 1377 | try w.writeByte('}'); |
| 1376 | }, | 1378 | }, |
| 1377 | .@"packed" => { | 1379 | .@"packed" => { |
| 1378 | const int_info = ty.intInfo(zcu); | 1380 | const int_info = ty.intInfo(zcu); |
| ... | @@ -1390,16 +1392,16 @@ pub const DeclGen = struct { | ... | @@ -1390,16 +1392,16 @@ pub const DeclGen = struct { |
| 1390 | } | 1392 | } |
| 1391 | | 1393 | |
| 1392 | if (eff_num_fields == 0) { | 1394 | if (eff_num_fields == 0) { |
| 1393 | try writer.writeByte('('); | 1395 | try w.writeByte('('); |
| 1394 | try dg.renderUndefValue(writer, ty, location); | 1396 | try dg.renderUndefValue(w, ty, location); |
| 1395 | try writer.writeByte(')'); | 1397 | try w.writeByte(')'); |
| 1396 | } else if (ty.bitSize(zcu) > 64) { | 1398 | } else if (ty.bitSize(zcu) > 64) { |
| 1397 | // zig_or_u128(zig_or_u128(zig_shl_u128(a, a_off), zig_shl_u128(b, b_off)), zig_shl_u128(c, c_off)) | 1399 | // zig_or_u128(zig_or_u128(zig_shl_u128(a, a_off), zig_shl_u128(b, b_off)), zig_shl_u128(c, c_off)) |
| 1398 | var num_or = eff_num_fields - 1; | 1400 | var num_or = eff_num_fields - 1; |
| 1399 | while (num_or > 0) : (num_or -= 1) { | 1401 | while (num_or > 0) : (num_or -= 1) { |
| 1400 | try writer.writeAll("zig_or_"); | 1402 | try w.writeAll("zig_or_"); |
| 1401 | try dg.renderTypeForBuiltinFnName(writer, ty); | 1403 | try dg.renderTypeForBuiltinFnName(w, ty); |
| 1402 | try writer.writeByte('('); | 1404 | try w.writeByte('('); |
| 1403 | } | 1405 | } |
| 1404 | | 1406 | |
| 1405 | var eff_index: usize = 0; | 1407 | var eff_index: usize = 0; |
| ... | @@ -1418,36 +1420,36 @@ pub const DeclGen = struct { | ... | @@ -1418,36 +1420,36 @@ pub const DeclGen = struct { |
| 1418 | }; | 1420 | }; |
| 1419 | const cast_context = IntCastContext{ .value = .{ .value = Value.fromInterned(field_val) } }; | 1421 | const cast_context = IntCastContext{ .value = .{ .value = Value.fromInterned(field_val) } }; |
| 1420 | if (bit_offset != 0) { | 1422 | if (bit_offset != 0) { |
| 1421 | try writer.writeAll("zig_shl_"); | 1423 | try w.writeAll("zig_shl_"); |
| 1422 | try dg.renderTypeForBuiltinFnName(writer, ty); | 1424 | try dg.renderTypeForBuiltinFnName(w, ty); |
| 1423 | try writer.writeByte('('); | 1425 | try w.writeByte('('); |
| 1424 | try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument); | 1426 | try dg.renderIntCast(w, ty, cast_context, field_ty, .FunctionArgument); |
| 1425 | try writer.writeAll(", "); | 1427 | try w.writeAll(", "); |
| 1426 | try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument); | 1428 | try dg.renderValue(w, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument); |
| 1427 | try writer.writeByte(')'); | 1429 | try w.writeByte(')'); |
| 1428 | } else { | 1430 | } else { |
| 1429 | try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument); | 1431 | try dg.renderIntCast(w, ty, cast_context, field_ty, .FunctionArgument); |
| 1430 | } | 1432 | } |
| 1431 | | 1433 | |
| 1432 | if (needs_closing_paren) try writer.writeByte(')'); | 1434 | if (needs_closing_paren) try w.writeByte(')'); |
| 1433 | if (eff_index != eff_num_fields - 1) try writer.writeAll(", "); | 1435 | if (eff_index != eff_num_fields - 1) try w.writeAll(", "); |
| 1434 | | 1436 | |
| 1435 | bit_offset += field_ty.bitSize(zcu); | 1437 | bit_offset += field_ty.bitSize(zcu); |
| 1436 | needs_closing_paren = true; | 1438 | needs_closing_paren = true; |
| 1437 | eff_index += 1; | 1439 | eff_index += 1; |
| 1438 | } | 1440 | } |
| 1439 | } else { | 1441 | } else { |
| 1440 | try writer.writeByte('('); | 1442 | try w.writeByte('('); |
| 1441 | // a << a_off | b << b_off | c << c_off | 1443 | // a << a_off | b << b_off | c << c_off |
| 1442 | var empty = true; | 1444 | var empty = true; |
| 1443 | for (0..loaded_struct.field_types.len) |field_index| { | 1445 | for (0..loaded_struct.field_types.len) |field_index| { |
| 1444 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); | 1446 | const field_ty: Type = .fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1445 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | 1447 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; |
| 1446 | | 1448 | |
| 1447 | if (!empty) try writer.writeAll(" | "); | 1449 | if (!empty) try w.writeAll(" | "); |
| 1448 | try writer.writeByte('('); | 1450 | try w.writeByte('('); |
| 1449 | try dg.renderCType(writer, ctype); | 1451 | try dg.renderCType(w, ctype); |
| 1450 | try writer.writeByte(')'); | 1452 | try w.writeByte(')'); |
| 1451 | | 1453 | |
| 1452 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { | 1454 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1453 | .bytes => |bytes| try pt.intern(.{ .int = .{ | 1455 | .bytes => |bytes| try pt.intern(.{ .int = .{ |
| ... | @@ -1464,24 +1466,24 @@ pub const DeclGen = struct { | ... | @@ -1464,24 +1466,24 @@ pub const DeclGen = struct { |
| 1464 | .{ .signedness = .unsigned, .bits = undefined }; | 1466 | .{ .signedness = .unsigned, .bits = undefined }; |
| 1465 | switch (field_int_info.signedness) { | 1467 | switch (field_int_info.signedness) { |
| 1466 | .signed => { | 1468 | .signed => { |
| 1467 | try writer.writeByte('('); | 1469 | try w.writeByte('('); |
| 1468 | try dg.renderValue(writer, Value.fromInterned(field_val), .Other); | 1470 | try dg.renderValue(w, Value.fromInterned(field_val), .Other); |
| 1469 | try writer.writeAll(" & "); | 1471 | try w.writeAll(" & "); |
| 1470 | const field_uint_ty = try pt.intType(.unsigned, field_int_info.bits); | 1472 | const field_uint_ty = try pt.intType(.unsigned, field_int_info.bits); |
| 1471 | try dg.renderValue(writer, try field_uint_ty.maxIntScalar(pt, field_uint_ty), .Other); | 1473 | try dg.renderValue(w, try field_uint_ty.maxIntScalar(pt, field_uint_ty), .Other); |
| 1472 | try writer.writeByte(')'); | 1474 | try w.writeByte(')'); |
| 1473 | }, | 1475 | }, |
| 1474 | .unsigned => try dg.renderValue(writer, Value.fromInterned(field_val), .Other), | 1476 | .unsigned => try dg.renderValue(w, Value.fromInterned(field_val), .Other), |
| 1475 | } | 1477 | } |
| 1476 | if (bit_offset != 0) { | 1478 | if (bit_offset != 0) { |
| 1477 | try writer.writeAll(" << "); | 1479 | try w.writeAll(" << "); |
| 1478 | try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument); | 1480 | try dg.renderValue(w, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument); |
| 1479 | } | 1481 | } |
| 1480 | | 1482 | |
| 1481 | bit_offset += field_ty.bitSize(zcu); | 1483 | bit_offset += field_ty.bitSize(zcu); |
| 1482 | empty = false; | 1484 | empty = false; |
| 1483 | } | 1485 | } |
| 1484 | try writer.writeByte(')'); | 1486 | try w.writeByte(')'); |
| 1485 | } | 1487 | } |
| 1486 | }, | 1488 | }, |
| 1487 | } | 1489 | } |
| ... | @@ -1495,11 +1497,11 @@ pub const DeclGen = struct { | ... | @@ -1495,11 +1497,11 @@ pub const DeclGen = struct { |
| 1495 | switch (loaded_union.flagsUnordered(ip).layout) { | 1497 | switch (loaded_union.flagsUnordered(ip).layout) { |
| 1496 | .@"packed" => { | 1498 | .@"packed" => { |
| 1497 | if (!location.isInitializer()) { | 1499 | if (!location.isInitializer()) { |
| 1498 | try writer.writeByte('('); | 1500 | try w.writeByte('('); |
| 1499 | try dg.renderType(writer, backing_ty); | 1501 | try dg.renderType(w, backing_ty); |
| 1500 | try writer.writeByte(')'); | 1502 | try w.writeByte(')'); |
| 1501 | } | 1503 | } |
| 1502 | try dg.renderValue(writer, Value.fromInterned(un.val), location); | 1504 | try dg.renderValue(w, Value.fromInterned(un.val), location); |
| 1503 | }, | 1505 | }, |
| 1504 | .@"extern" => { | 1506 | .@"extern" => { |
| 1505 | if (location == .StaticInitializer) { | 1507 | if (location == .StaticInitializer) { |
| ... | @@ -1507,21 +1509,21 @@ pub const DeclGen = struct { | ... | @@ -1507,21 +1509,21 @@ pub const DeclGen = struct { |
| 1507 | } | 1509 | } |
| 1508 | | 1510 | |
| 1509 | const ptr_ty = try pt.singleConstPtrType(ty); | 1511 | const ptr_ty = try pt.singleConstPtrType(ty); |
| 1510 | try writer.writeAll("*(("); | 1512 | try w.writeAll("*(("); |
| 1511 | try dg.renderType(writer, ptr_ty); | 1513 | try dg.renderType(w, ptr_ty); |
| 1512 | try writer.writeAll(")("); | 1514 | try w.writeAll(")("); |
| 1513 | try dg.renderType(writer, backing_ty); | 1515 | try dg.renderType(w, backing_ty); |
| 1514 | try writer.writeAll("){"); | 1516 | try w.writeAll("){"); |
| 1515 | try dg.renderValue(writer, Value.fromInterned(un.val), location); | 1517 | try dg.renderValue(w, Value.fromInterned(un.val), location); |
| 1516 | try writer.writeAll("})"); | 1518 | try w.writeAll("})"); |
| 1517 | }, | 1519 | }, |
| 1518 | else => unreachable, | 1520 | else => unreachable, |
| 1519 | } | 1521 | } |
| 1520 | } else { | 1522 | } else { |
| 1521 | if (!location.isInitializer()) { | 1523 | if (!location.isInitializer()) { |
| 1522 | try writer.writeByte('('); | 1524 | try w.writeByte('('); |
| 1523 | try dg.renderCType(writer, ctype); | 1525 | try dg.renderCType(w, ctype); |
| 1524 | try writer.writeByte(')'); | 1526 | try w.writeByte(')'); |
| 1525 | } | 1527 | } |
| 1526 | | 1528 | |
| 1527 | const field_index = zcu.unionTagFieldIndex(loaded_union, Value.fromInterned(un.tag)).?; | 1529 | const field_index = zcu.unionTagFieldIndex(loaded_union, Value.fromInterned(un.tag)).?; |
| ... | @@ -1530,57 +1532,57 @@ pub const DeclGen = struct { | ... | @@ -1530,57 +1532,57 @@ pub const DeclGen = struct { |
| 1530 | if (loaded_union.flagsUnordered(ip).layout == .@"packed") { | 1532 | if (loaded_union.flagsUnordered(ip).layout == .@"packed") { |
| 1531 | if (field_ty.hasRuntimeBits(zcu)) { | 1533 | if (field_ty.hasRuntimeBits(zcu)) { |
| 1532 | if (field_ty.isPtrAtRuntime(zcu)) { | 1534 | if (field_ty.isPtrAtRuntime(zcu)) { |
| 1533 | try writer.writeByte('('); | 1535 | try w.writeByte('('); |
| 1534 | try dg.renderCType(writer, ctype); | 1536 | try dg.renderCType(w, ctype); |
| 1535 | try writer.writeByte(')'); | 1537 | try w.writeByte(')'); |
| 1536 | } else if (field_ty.zigTypeTag(zcu) == .float) { | 1538 | } else if (field_ty.zigTypeTag(zcu) == .float) { |
| 1537 | try writer.writeByte('('); | 1539 | try w.writeByte('('); |
| 1538 | try dg.renderCType(writer, ctype); | 1540 | try dg.renderCType(w, ctype); |
| 1539 | try writer.writeByte(')'); | 1541 | try w.writeByte(')'); |
| 1540 | } | 1542 | } |
| 1541 | try dg.renderValue(writer, Value.fromInterned(un.val), location); | 1543 | try dg.renderValue(w, Value.fromInterned(un.val), location); |
| 1542 | } else try writer.writeByte('0'); | 1544 | } else try w.writeByte('0'); |
| 1543 | return; | 1545 | return; |
| 1544 | } | 1546 | } |
| 1545 | | 1547 | |
| 1546 | const has_tag = loaded_union.hasTag(ip); | 1548 | const has_tag = loaded_union.hasTag(ip); |
| 1547 | if (has_tag) try writer.writeByte('{'); | 1549 | if (has_tag) try w.writeByte('{'); |
| 1548 | const aggregate = ctype.info(ctype_pool).aggregate; | 1550 | const aggregate = ctype.info(ctype_pool).aggregate; |
| 1549 | for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| { | 1551 | for (0..if (has_tag) aggregate.fields.len else 1) |outer_field_index| { |
| 1550 | if (outer_field_index > 0) try writer.writeByte(','); | 1552 | if (outer_field_index > 0) try w.writeByte(','); |
| 1551 | switch (if (has_tag) | 1553 | switch (if (has_tag) |
| 1552 | aggregate.fields.at(outer_field_index, ctype_pool).name.index | 1554 | aggregate.fields.at(outer_field_index, ctype_pool).name.index |
| 1553 | else | 1555 | else |
| 1554 | .payload) { | 1556 | .payload) { |
| 1555 | .tag => try dg.renderValue( | 1557 | .tag => try dg.renderValue( |
| 1556 | writer, | 1558 | w, |
| 1557 | Value.fromInterned(un.tag), | 1559 | Value.fromInterned(un.tag), |
| 1558 | initializer_type, | 1560 | initializer_type, |
| 1559 | ), | 1561 | ), |
| 1560 | .payload => { | 1562 | .payload => { |
| 1561 | try writer.writeByte('{'); | 1563 | try w.writeByte('{'); |
| 1562 | if (field_ty.hasRuntimeBits(zcu)) { | 1564 | if (field_ty.hasRuntimeBits(zcu)) { |
| 1563 | try writer.print(" .{f } = ", .{fmtIdent(field_name.toSlice(ip))}); | 1565 | try w.print(" .{f } = ", .{fmtIdent(field_name.toSlice(ip))}); |
| 1564 | try dg.renderValue( | 1566 | try dg.renderValue( |
| 1565 | writer, | 1567 | w, |
| 1566 | Value.fromInterned(un.val), | 1568 | Value.fromInterned(un.val), |
| 1567 | initializer_type, | 1569 | initializer_type, |
| 1568 | ); | 1570 | ); |
| 1569 | try writer.writeByte(' '); | 1571 | try w.writeByte(' '); |
| 1570 | } else for (0..loaded_union.field_types.len) |inner_field_index| { | 1572 | } else for (0..loaded_union.field_types.len) |inner_field_index| { |
| 1571 | const inner_field_ty: Type = .fromInterned( | 1573 | const inner_field_ty: Type = .fromInterned( |
| 1572 | loaded_union.field_types.get(ip)[inner_field_index], | 1574 | loaded_union.field_types.get(ip)[inner_field_index], |
| 1573 | ); | 1575 | ); |
| 1574 | if (!inner_field_ty.hasRuntimeBits(zcu)) continue; | 1576 | if (!inner_field_ty.hasRuntimeBits(zcu)) continue; |
| 1575 | try dg.renderUndefValue(writer, inner_field_ty, initializer_type); | 1577 | try dg.renderUndefValue(w, inner_field_ty, initializer_type); |
| 1576 | break; | 1578 | break; |
| 1577 | } | 1579 | } |
| 1578 | try writer.writeByte('}'); | 1580 | try w.writeByte('}'); |
| 1579 | }, | 1581 | }, |
| 1580 | else => unreachable, | 1582 | else => unreachable, |
| 1581 | } | 1583 | } |
| 1582 | } | 1584 | } |
| 1583 | if (has_tag) try writer.writeByte('}'); | 1585 | if (has_tag) try w.writeByte('}'); |
| 1584 | } | 1586 | } |
| 1585 | }, | 1587 | }, |
| 1586 | } | 1588 | } |
| ... | @@ -2999,18 +3001,20 @@ pub fn generate( | ... | @@ -2999,18 +3001,20 @@ pub fn generate( |
| 2999 | .pass = .{ .nav = func.owner_nav }, | 3001 | .pass = .{ .nav = func.owner_nav }, |
| 3000 | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, | 3002 | .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked, |
| 3001 | .expected_block = null, | 3003 | .expected_block = null, |
| 3002 | .fwd_decl = .init(gpa), | 3004 | .fwd_decl = undefined, |
| 3003 | .ctype_pool = .empty, | 3005 | .ctype_pool = .empty, |
| 3004 | .scratch = .empty, | 3006 | .scratch = .empty, |
| 3005 | .uavs = .empty, | 3007 | .uavs = .empty, |
| 3006 | }, | 3008 | }, |
| 3007 | .code = .init(gpa), | 3009 | .code_header = undefined, |
| 3008 | .indent_writer = undefined, // set later so we can get a pointer to object.code | 3010 | .code = undefined, |
| | 3011 | .indent_counter = 0, |
| 3009 | }, | 3012 | }, |
| 3010 | .lazy_fns = .empty, | 3013 | .lazy_fns = .empty, |
| 3011 | }; | 3014 | }; |
| 3012 | defer { | 3015 | defer { |
| 3013 | function.object.code.deinit(); | 3016 | function.object.code_header.init(gpa); |
| | 3017 | function.object.code.init(gpa); |
| 3014 | function.object.dg.fwd_decl.deinit(); | 3018 | function.object.dg.fwd_decl.deinit(); |
| 3015 | function.object.dg.ctype_pool.deinit(gpa); | 3019 | function.object.dg.ctype_pool.deinit(gpa); |
| 3016 | function.object.dg.scratch.deinit(gpa); | 3020 | function.object.dg.scratch.deinit(gpa); |
| ... | @@ -3018,7 +3022,9 @@ pub fn generate( | ... | @@ -3018,7 +3022,9 @@ pub fn generate( |
| 3018 | function.deinit(); | 3022 | function.deinit(); |
| 3019 | } | 3023 | } |
| 3020 | try function.object.dg.ctype_pool.init(gpa); | 3024 | try function.object.dg.ctype_pool.init(gpa); |
| 3021 | function.object.indent_writer = .{ .underlying_writer = function.object.code.writer() }; | 3025 | function.object.dg.fwd_decl.init(gpa); |
| | 3026 | function.object.code_header.init(gpa); |
| | 3027 | function.object.code.init(gpa); |
| 3022 | | 3028 | |
| 3023 | genFunc(&function) catch |err| switch (err) { | 3029 | genFunc(&function) catch |err| switch (err) { |
| 3024 | error.AnalysisFail => return zcu.codegenFailMsg(func.owner_nav, function.object.dg.error_msg.?), | 3030 | error.AnalysisFail => return zcu.codegenFailMsg(func.owner_nav, function.object.dg.error_msg.?), |
| ... | @@ -3034,6 +3040,7 @@ pub fn generate( | ... | @@ -3034,6 +3040,7 @@ pub fn generate( |
| 3034 | }; | 3040 | }; |
| 3035 | errdefer mir.deinit(gpa); | 3041 | errdefer mir.deinit(gpa); |
| 3036 | mir.uavs = function.object.dg.uavs.move(); | 3042 | mir.uavs = function.object.dg.uavs.move(); |
| | 3043 | mir.code_header = try function.object.code_header.toOwnedSlice(); |
| 3037 | mir.code = try function.object.code.toOwnedSlice(); | 3044 | mir.code = try function.object.code.toOwnedSlice(); |
| 3038 | mir.fwd_decl = try function.object.dg.fwd_decl.toOwnedSlice(); | 3045 | mir.fwd_decl = try function.object.dg.fwd_decl.toOwnedSlice(); |
| 3039 | mir.ctype_pool = function.object.dg.ctype_pool.move(); | 3046 | mir.ctype_pool = function.object.dg.ctype_pool.move(); |
| ... | @@ -3041,7 +3048,7 @@ pub fn generate( | ... | @@ -3041,7 +3048,7 @@ pub fn generate( |
| 3041 | return mir; | 3048 | return mir; |
| 3042 | } | 3049 | } |
| 3043 | | 3050 | |
| 3044 | fn genFunc(f: *Function) !void { | 3051 | pub fn genFunc(f: *Function) Error!void { |
| 3045 | const tracy = trace(@src()); | 3052 | const tracy = trace(@src()); |
| 3046 | defer tracy.end(); | 3053 | defer tracy.end(); |
| 3047 | | 3054 | |
| ... | @@ -4033,7 +4040,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { | ... | @@ -4033,7 +4040,7 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 4033 | try f.writeCValueDeref(w, ret_val) | 4040 | try f.writeCValueDeref(w, ret_val) |
| 4034 | else | 4041 | else |
| 4035 | try f.writeCValue(w, ret_val, .Other); | 4042 | try f.writeCValue(w, ret_val, .Other); |
| 4036 | try w.writeAll(";\n"); | 4043 | try w.write(";\n"); |
| 4037 | if (is_array) { | 4044 | if (is_array) { |
| 4038 | try freeLocal(f, inst, ret_val.new_local, null); | 4045 | try freeLocal(f, inst, ret_val.new_local, null); |
| 4039 | } | 4046 | } |
| ... | @@ -4347,7 +4354,8 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: | ... | @@ -4347,7 +4354,8 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 4347 | try f.writeCValue(w, rhs, .FunctionArgument); | 4354 | try f.writeCValue(w, rhs, .FunctionArgument); |
| 4348 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); | 4355 | if (f.typeOf(bin_op.rhs).isVector(zcu)) try v.elem(f, w); |
| 4349 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); | 4356 | try f.object.dg.renderBuiltinInfo(w, scalar_ty, info); |
| 4350 | try w.writeAll(");\n"); | 4357 | try w.writeAll(");"); |
| | 4358 | try f.object.newline(); |
| 4351 | try v.end(f, inst, w); | 4359 | try v.end(f, inst, w); |
| 4352 | | 4360 | |
| 4353 | return local; | 4361 | return local; |
| ... | @@ -4884,8 +4892,9 @@ fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -4884,8 +4892,9 @@ fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4884 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; | 4892 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4885 | const extra = f.air.extraData(Air.DbgInlineBlock, ty_pl.payload); | 4893 | const extra = f.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4886 | const owner_nav = ip.getNav(zcu.funcInfo(extra.data.func).owner_nav); | 4894 | const owner_nav = ip.getNav(zcu.funcInfo(extra.data.func).owner_nav); |
| 4887 | const writer = f.object.writer(); | 4895 | const w = &f.object.code.buffered_writer; |
| 4888 | try writer.print("/* inline:{f} */\n", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); | 4896 | try w.print("/* inline:{f} */", .{owner_nav.fqn.fmt(&zcu.intern_pool)}); |
| | 4897 | try f.object.newline(); |
| 4889 | return lowerBlock(f, inst, @ptrCast(f.air.extra.items[extra.end..][0..extra.data.body_len])); | 4898 | return lowerBlock(f, inst, @ptrCast(f.air.extra.items[extra.end..][0..extra.data.body_len])); |
| 4890 | } | 4899 | } |
| 4891 | | 4900 | |
| ... | @@ -7415,30 +7424,31 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7415,30 +7424,31 @@ fn airShuffleTwo(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7415 | const inst_ty = unwrapped.result_ty; | 7424 | const inst_ty = unwrapped.result_ty; |
| 7416 | const elem_ty = inst_ty.childType(zcu); | 7425 | const elem_ty = inst_ty.childType(zcu); |
| 7417 | | 7426 | |
| 7418 | const writer = &f.object.code.buffered_writer; | 7427 | const w = &f.object.code.buffered_writer; |
| 7419 | const local = try f.allocLocal(inst, inst_ty); | 7428 | const local = try f.allocLocal(inst, inst_ty); |
| 7420 | try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands | 7429 | try reap(f, inst, &.{ unwrapped.operand_a, unwrapped.operand_b }); // local cannot alias operands |
| 7421 | for (mask, 0..) |mask_elem, out_idx| { | 7430 | for (mask, 0..) |mask_elem, out_idx| { |
| 7422 | try f.writeCValue(writer, local, .Other); | 7431 | try f.writeCValue(w, local, .Other); |
| 7423 | try writer.writeByte('['); | 7432 | try w.writeByte('['); |
| 7424 | try f.object.dg.renderValue(writer, try pt.intValue(.usize, out_idx), .Other); | 7433 | try f.object.dg.renderValue(w, try pt.intValue(.usize, out_idx), .Other); |
| 7425 | try writer.writeAll("] = "); | 7434 | try w.writeAll("] = "); |
| 7426 | switch (mask_elem.unwrap()) { | 7435 | switch (mask_elem.unwrap()) { |
| 7427 | .a_elem => |src_idx| { | 7436 | .a_elem => |src_idx| { |
| 7428 | try f.writeCValue(writer, operand_a, .Other); | 7437 | try f.writeCValue(w, operand_a, .Other); |
| 7429 | try writer.writeByte('['); | 7438 | try w.writeByte('['); |
| 7430 | try f.object.dg.renderValue(writer, try pt.intValue(.usize, src_idx), .Other); | 7439 | try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other); |
| 7431 | try writer.writeByte(']'); | 7440 | try w.writeByte(']'); |
| 7432 | }, | 7441 | }, |
| 7433 | .b_elem => |src_idx| { | 7442 | .b_elem => |src_idx| { |
| 7434 | try f.writeCValue(writer, operand_b, .Other); | 7443 | try f.writeCValue(w, operand_b, .Other); |
| 7435 | try writer.writeByte('['); | 7444 | try w.writeByte('['); |
| 7436 | try f.object.dg.renderValue(writer, try pt.intValue(.usize, src_idx), .Other); | 7445 | try f.object.dg.renderValue(w, try pt.intValue(.usize, src_idx), .Other); |
| 7437 | try writer.writeByte(']'); | 7446 | try w.writeByte(']'); |
| 7438 | }, | 7447 | }, |
| 7439 | .undef => try f.object.dg.renderUndefValue(writer, elem_ty, .Other), | 7448 | .undef => try f.object.dg.renderUndefValue(w, elem_ty, .Other), |
| 7440 | } | 7449 | } |
| 7441 | try writer.writeAll(";\n"); | 7450 | try w.writeByte(';'); |
| | 7451 | try f.object.newline(); |
| 7442 | } | 7452 | } |
| 7443 | | 7453 | |
| 7444 | return local; | 7454 | return local; |
| ... | @@ -7889,12 +7899,13 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { | ... | @@ -7889,12 +7899,13 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7889 | | 7899 | |
| 7890 | fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue { | 7900 | fn airRuntimeNavPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7891 | const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; | 7901 | const ty_nav = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_nav; |
| 7892 | const writer = f.object.writer(); | 7902 | const w = &f.object.code.buffered_writer; |
| 7893 | const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty)); | 7903 | const local = try f.allocLocal(inst, .fromInterned(ty_nav.ty)); |
| 7894 | try f.writeCValue(writer, local, .Other); | 7904 | try f.writeCValue(w, local, .Other); |
| 7895 | try writer.writeAll(" = "); | 7905 | try w.writeAll(" = "); |
| 7896 | try f.object.dg.renderNav(writer, ty_nav.nav, .Other); | 7906 | try f.object.dg.renderNav(w, ty_nav.nav, .Other); |
| 7897 | try writer.writeAll(";\n"); | 7907 | try w.writeByte(';'); |
| | 7908 | try f.object.newline(); |
| 7898 | return local; | 7909 | return local; |
| 7899 | } | 7910 | } |
| 7900 | | 7911 | |
| ... | @@ -8480,19 +8491,19 @@ const Assignment = struct { | ... | @@ -8480,19 +8491,19 @@ const Assignment = struct { |
| 8480 | const Vectorize = struct { | 8491 | const Vectorize = struct { |
| 8481 | index: CValue = .none, | 8492 | index: CValue = .none, |
| 8482 | | 8493 | |
| 8483 | pub fn start(f: *Function, inst: Air.Inst.Index, writer: *Writer, ty: Type) !Vectorize { | 8494 | pub fn start(f: *Function, inst: Air.Inst.Index, w: *Writer, ty: Type) !Vectorize { |
| 8484 | const pt = f.object.dg.pt; | 8495 | const pt = f.object.dg.pt; |
| 8485 | const zcu = pt.zcu; | 8496 | const zcu = pt.zcu; |
| 8486 | return if (ty.zigTypeTag(zcu) == .vector) index: { | 8497 | return if (ty.zigTypeTag(zcu) == .vector) index: { |
| 8487 | const local = try f.allocLocal(inst, .usize); | 8498 | const local = try f.allocLocal(inst, .usize); |
| 8488 | | 8499 | |
| 8489 | try writer.writeAll("for ("); | 8500 | try w.writeAll("for ("); |
| 8490 | try f.writeCValue(writer, local, .Other); | 8501 | try f.writeCValue(w, local, .Other); |
| 8491 | try writer.print(" = {fd}; ", .{try f.fmtIntLiteral(.zero_usize)}); | 8502 | try w.print(" = {fd}; ", .{try f.fmtIntLiteral(.zero_usize)}); |
| 8492 | try f.writeCValue(writer, local, .Other); | 8503 | try f.writeCValue(w, local, .Other); |
| 8493 | try writer.print(" < {fd}; ", .{try f.fmtIntLiteral(try pt.intValue(.usize, ty.vectorLen(zcu)))}); | 8504 | try w.print(" < {fd}; ", .{try f.fmtIntLiteral(try pt.intValue(.usize, ty.vectorLen(zcu)))}); |
| 8494 | try f.writeCValue(writer, local, .Other); | 8505 | try f.writeCValue(w, local, .Other); |
| 8495 | try writer.print(" += {fd}) {{\n", .{try f.fmtIntLiteral(.one_usize)}); | 8506 | try w.print(" += {fd}) {{\n", .{try f.fmtIntLiteral(.one_usize)}); |
| 8496 | f.object.indent(); | 8507 | f.object.indent(); |
| 8497 | try f.object.newline(); | 8508 | try f.object.newline(); |
| 8498 | | 8509 | |