| ... | @@ -13,7 +13,7 @@ pub const Class = enum { | ... | @@ -13,7 +13,7 @@ pub const Class = enum { |
| 13 | integer_per_element, | 13 | integer_per_element, |
| 14 | }; | 14 | }; |
| 15 | | 15 | |
| 16 | pub fn classifyWindows(ty: Type, mod: *Module) Class { | 16 | pub fn classifyWindows(ty: Type, zcu: *Zcu) Class { |
| 17 | // https://docs.microsoft.com/en-gb/cpp/build/x64-calling-convention?view=vs-2017 | 17 | // https://docs.microsoft.com/en-gb/cpp/build/x64-calling-convention?view=vs-2017 |
| 18 | // "There's a strict one-to-one correspondence between a function call's arguments | 18 | // "There's a strict one-to-one correspondence between a function call's arguments |
| 19 | // and the registers used for those arguments. Any argument that doesn't fit in 8 | 19 | // and the registers used for those arguments. Any argument that doesn't fit in 8 |
| ... | @@ -22,7 +22,7 @@ pub fn classifyWindows(ty: Type, mod: *Module) Class { | ... | @@ -22,7 +22,7 @@ pub fn classifyWindows(ty: Type, mod: *Module) Class { |
| 22 | // "All floating point operations are done using the 16 XMM registers." | 22 | // "All floating point operations are done using the 16 XMM registers." |
| 23 | // "Structs and unions of size 8, 16, 32, or 64 bits, and __m64 types, are passed | 23 | // "Structs and unions of size 8, 16, 32, or 64 bits, and __m64 types, are passed |
| 24 | // as if they were integers of the same size." | 24 | // as if they were integers of the same size." |
| 25 | switch (ty.zigTypeTag(mod)) { | 25 | switch (ty.zigTypeTag(zcu)) { |
| 26 | .Pointer, | 26 | .Pointer, |
| 27 | .Int, | 27 | .Int, |
| 28 | .Bool, | 28 | .Bool, |
| ... | @@ -37,12 +37,12 @@ pub fn classifyWindows(ty: Type, mod: *Module) Class { | ... | @@ -37,12 +37,12 @@ pub fn classifyWindows(ty: Type, mod: *Module) Class { |
| 37 | .ErrorUnion, | 37 | .ErrorUnion, |
| 38 | .AnyFrame, | 38 | .AnyFrame, |
| 39 | .Frame, | 39 | .Frame, |
| 40 | => switch (ty.abiSize(mod)) { | 40 | => switch (ty.abiSize(zcu)) { |
| 41 | 0 => unreachable, | 41 | 0 => unreachable, |
| 42 | 1, 2, 4, 8 => return .integer, | 42 | 1, 2, 4, 8 => return .integer, |
| 43 | else => switch (ty.zigTypeTag(mod)) { | 43 | else => switch (ty.zigTypeTag(zcu)) { |
| 44 | .Int => return .win_i128, | 44 | .Int => return .win_i128, |
| 45 | .Struct, .Union => if (ty.containerLayout(mod) == .@"packed") { | 45 | .Struct, .Union => if (ty.containerLayout(zcu) == .@"packed") { |
| 46 | return .win_i128; | 46 | return .win_i128; |
| 47 | } else { | 47 | } else { |
| 48 | return .memory; | 48 | return .memory; |
| ... | @@ -69,16 +69,16 @@ pub const Context = enum { ret, arg, field, other }; | ... | @@ -69,16 +69,16 @@ pub const Context = enum { ret, arg, field, other }; |
| 69 | | 69 | |
| 70 | /// There are a maximum of 8 possible return slots. Returned values are in | 70 | /// There are a maximum of 8 possible return slots. Returned values are in |
| 71 | /// the beginning of the array; unused slots are filled with .none. | 71 | /// the beginning of the array; unused slots are filled with .none. |
| 72 | pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | 72 | pub fn classifySystemV(ty: Type, zcu: *Zcu, ctx: Context) [8]Class { |
| 73 | const ip = &mod.intern_pool; | 73 | const ip = &zcu.intern_pool; |
| 74 | const target = mod.getTarget(); | 74 | const target = zcu.getTarget(); |
| 75 | const memory_class = [_]Class{ | 75 | const memory_class = [_]Class{ |
| 76 | .memory, .none, .none, .none, | 76 | .memory, .none, .none, .none, |
| 77 | .none, .none, .none, .none, | 77 | .none, .none, .none, .none, |
| 78 | }; | 78 | }; |
| 79 | var result = [1]Class{.none} ** 8; | 79 | var result = [1]Class{.none} ** 8; |
| 80 | switch (ty.zigTypeTag(mod)) { | 80 | switch (ty.zigTypeTag(zcu)) { |
| 81 | .Pointer => switch (ty.ptrSize(mod)) { | 81 | .Pointer => switch (ty.ptrSize(zcu)) { |
| 82 | .Slice => { | 82 | .Slice => { |
| 83 | result[0] = .integer; | 83 | result[0] = .integer; |
| 84 | result[1] = .integer; | 84 | result[1] = .integer; |
| ... | @@ -90,7 +90,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -90,7 +90,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 90 | }, | 90 | }, |
| 91 | }, | 91 | }, |
| 92 | .Int, .Enum, .ErrorSet => { | 92 | .Int, .Enum, .ErrorSet => { |
| 93 | const bits = ty.intInfo(mod).bits; | 93 | const bits = ty.intInfo(zcu).bits; |
| 94 | if (bits <= 64) { | 94 | if (bits <= 64) { |
| 95 | result[0] = .integer; | 95 | result[0] = .integer; |
| 96 | return result; | 96 | return result; |
| ... | @@ -160,8 +160,8 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -160,8 +160,8 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 160 | else => unreachable, | 160 | else => unreachable, |
| 161 | }, | 161 | }, |
| 162 | .Vector => { | 162 | .Vector => { |
| 163 | const elem_ty = ty.childType(mod); | 163 | const elem_ty = ty.childType(zcu); |
| 164 | const bits = elem_ty.bitSize(mod) * ty.arrayLen(mod); | 164 | const bits = elem_ty.bitSize(zcu) * ty.arrayLen(zcu); |
| 165 | if (elem_ty.toIntern() == .bool_type) { | 165 | if (elem_ty.toIntern() == .bool_type) { |
| 166 | if (bits <= 32) return .{ | 166 | if (bits <= 32) return .{ |
| 167 | .integer, .none, .none, .none, | 167 | .integer, .none, .none, .none, |
| ... | @@ -225,7 +225,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -225,7 +225,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 225 | return memory_class; | 225 | return memory_class; |
| 226 | }, | 226 | }, |
| 227 | .Optional => { | 227 | .Optional => { |
| 228 | if (ty.isPtrLikeOptional(mod)) { | 228 | if (ty.isPtrLikeOptional(zcu)) { |
| 229 | result[0] = .integer; | 229 | result[0] = .integer; |
| 230 | return result; | 230 | return result; |
| 231 | } | 231 | } |
| ... | @@ -236,9 +236,9 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -236,9 +236,9 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 236 | // it contains unaligned fields, it has class MEMORY" | 236 | // it contains unaligned fields, it has class MEMORY" |
| 237 | // "If the size of the aggregate exceeds a single eightbyte, each is classified | 237 | // "If the size of the aggregate exceeds a single eightbyte, each is classified |
| 238 | // separately.". | 238 | // separately.". |
| 239 | const struct_type = mod.typeToStruct(ty).?; | 239 | const loaded_struct = ip.loadStructType(ty.toIntern()); |
| 240 | const ty_size = ty.abiSize(mod); | 240 | const ty_size = ty.abiSize(zcu); |
| 241 | if (struct_type.layout == .@"packed") { | 241 | if (loaded_struct.layout == .@"packed") { |
| 242 | assert(ty_size <= 16); | 242 | assert(ty_size <= 16); |
| 243 | result[0] = .integer; | 243 | result[0] = .integer; |
| 244 | if (ty_size > 8) result[1] = .integer; | 244 | if (ty_size > 8) result[1] = .integer; |
| ... | @@ -247,82 +247,8 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -247,82 +247,8 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 247 | if (ty_size > 64) | 247 | if (ty_size > 64) |
| 248 | return memory_class; | 248 | return memory_class; |
| 249 | | 249 | |
| 250 | var result_i: usize = 0; // out of 8 | 250 | var byte_offset: u64 = 0; |
| 251 | var byte_i: usize = 0; // out of 8 | 251 | classifySystemVStruct(&result, &byte_offset, loaded_struct, zcu); |
| 252 | for (struct_type.field_types.get(ip), 0..) |field_ty_ip, i| { | | |
| 253 | const field_ty = Type.fromInterned(field_ty_ip); | | |
| 254 | const field_align = struct_type.fieldAlign(ip, i); | | |
| 255 | if (field_align != .none and field_align.compare(.lt, field_ty.abiAlignment(mod))) | | |
| 256 | return memory_class; | | |
| 257 | const field_size = field_ty.abiSize(mod); | | |
| 258 | const field_class_array = classifySystemV(field_ty, mod, .field); | | |
| 259 | const field_class = std.mem.sliceTo(&field_class_array, .none); | | |
| 260 | if (byte_i + field_size <= 8) { | | |
| 261 | // Combine this field with the previous one. | | |
| 262 | combine: { | | |
| 263 | // "If both classes are equal, this is the resulting class." | | |
| 264 | if (result[result_i] == field_class[0]) { | | |
| 265 | if (result[result_i] == .float) { | | |
| 266 | result[result_i] = .float_combine; | | |
| 267 | } | | |
| 268 | break :combine; | | |
| 269 | } | | |
| 270 | | | |
| 271 | // "If one of the classes is NO_CLASS, the resulting class | | |
| 272 | // is the other class." | | |
| 273 | if (result[result_i] == .none) { | | |
| 274 | result[result_i] = field_class[0]; | | |
| 275 | break :combine; | | |
| 276 | } | | |
| 277 | assert(field_class[0] != .none); | | |
| 278 | | | |
| 279 | // "If one of the classes is MEMORY, the result is the MEMORY class." | | |
| 280 | if (result[result_i] == .memory or field_class[0] == .memory) { | | |
| 281 | result[result_i] = .memory; | | |
| 282 | break :combine; | | |
| 283 | } | | |
| 284 | | | |
| 285 | // "If one of the classes is INTEGER, the result is the INTEGER." | | |
| 286 | if (result[result_i] == .integer or field_class[0] == .integer) { | | |
| 287 | result[result_i] = .integer; | | |
| 288 | break :combine; | | |
| 289 | } | | |
| 290 | | | |
| 291 | // "If one of the classes is X87, X87UP, COMPLEX_X87 class, | | |
| 292 | // MEMORY is used as class." | | |
| 293 | if (result[result_i] == .x87 or | | |
| 294 | result[result_i] == .x87up or | | |
| 295 | result[result_i] == .complex_x87 or | | |
| 296 | field_class[0] == .x87 or | | |
| 297 | field_class[0] == .x87up or | | |
| 298 | field_class[0] == .complex_x87) | | |
| 299 | { | | |
| 300 | result[result_i] = .memory; | | |
| 301 | break :combine; | | |
| 302 | } | | |
| 303 | | | |
| 304 | // "Otherwise class SSE is used." | | |
| 305 | result[result_i] = .sse; | | |
| 306 | } | | |
| 307 | byte_i += @as(usize, @intCast(field_size)); | | |
| 308 | if (byte_i == 8) { | | |
| 309 | byte_i = 0; | | |
| 310 | result_i += 1; | | |
| 311 | } | | |
| 312 | } else { | | |
| 313 | // Cannot combine this field with the previous one. | | |
| 314 | if (byte_i != 0) { | | |
| 315 | byte_i = 0; | | |
| 316 | result_i += 1; | | |
| 317 | } | | |
| 318 | @memcpy(result[result_i..][0..field_class.len], field_class); | | |
| 319 | result_i += field_class.len; | | |
| 320 | // If there are any bytes leftover, we have to try to combine | | |
| 321 | // the next field with them. | | |
| 322 | byte_i = @as(usize, @intCast(field_size % 8)); | | |
| 323 | if (byte_i != 0) result_i -= 1; | | |
| 324 | } | | |
| 325 | } | | |
| 326 | | 252 | |
| 327 | // Post-merger cleanup | 253 | // Post-merger cleanup |
| 328 | | 254 | |
| ... | @@ -354,8 +280,8 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -354,8 +280,8 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 354 | // it contains unaligned fields, it has class MEMORY" | 280 | // it contains unaligned fields, it has class MEMORY" |
| 355 | // "If the size of the aggregate exceeds a single eightbyte, each is classified | 281 | // "If the size of the aggregate exceeds a single eightbyte, each is classified |
| 356 | // separately.". | 282 | // separately.". |
| 357 | const union_obj = mod.typeToUnion(ty).?; | 283 | const union_obj = zcu.typeToUnion(ty).?; |
| 358 | const ty_size = mod.unionAbiSize(union_obj); | 284 | const ty_size = zcu.unionAbiSize(union_obj); |
| 359 | if (union_obj.getLayout(ip) == .@"packed") { | 285 | if (union_obj.getLayout(ip) == .@"packed") { |
| 360 | assert(ty_size <= 16); | 286 | assert(ty_size <= 16); |
| 361 | result[0] = .integer; | 287 | result[0] = .integer; |
| ... | @@ -368,12 +294,12 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -368,12 +294,12 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 368 | for (union_obj.field_types.get(ip), 0..) |field_ty, field_index| { | 294 | for (union_obj.field_types.get(ip), 0..) |field_ty, field_index| { |
| 369 | const field_align = union_obj.fieldAlign(ip, @intCast(field_index)); | 295 | const field_align = union_obj.fieldAlign(ip, @intCast(field_index)); |
| 370 | if (field_align != .none and | 296 | if (field_align != .none and |
| 371 | field_align.compare(.lt, Type.fromInterned(field_ty).abiAlignment(mod))) | 297 | field_align.compare(.lt, Type.fromInterned(field_ty).abiAlignment(zcu))) |
| 372 | { | 298 | { |
| 373 | return memory_class; | 299 | return memory_class; |
| 374 | } | 300 | } |
| 375 | // Combine this field with the previous one. | 301 | // Combine this field with the previous one. |
| 376 | const field_class = classifySystemV(Type.fromInterned(field_ty), mod, .field); | 302 | const field_class = classifySystemV(Type.fromInterned(field_ty), zcu, .field); |
| 377 | for (&result, 0..) |*result_item, i| { | 303 | for (&result, 0..) |*result_item, i| { |
| 378 | const field_item = field_class[i]; | 304 | const field_item = field_class[i]; |
| 379 | // "If both classes are equal, this is the resulting class." | 305 | // "If both classes are equal, this is the resulting class." |
| ... | @@ -447,7 +373,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -447,7 +373,7 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 447 | return result; | 373 | return result; |
| 448 | }, | 374 | }, |
| 449 | .Array => { | 375 | .Array => { |
| 450 | const ty_size = ty.abiSize(mod); | 376 | const ty_size = ty.abiSize(zcu); |
| 451 | if (ty_size <= 8) { | 377 | if (ty_size <= 8) { |
| 452 | result[0] = .integer; | 378 | result[0] = .integer; |
| 453 | return result; | 379 | return result; |
| ... | @@ -463,6 +389,82 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { | ... | @@ -463,6 +389,82 @@ pub fn classifySystemV(ty: Type, mod: *Module, ctx: Context) [8]Class { |
| 463 | } | 389 | } |
| 464 | } | 390 | } |
| 465 | | 391 | |
| | 392 | fn classifySystemVStruct( |
| | 393 | result: *[8]Class, |
| | 394 | byte_offset: *u64, |
| | 395 | loaded_struct: InternPool.LoadedStructType, |
| | 396 | zcu: *Zcu, |
| | 397 | ) void { |
| | 398 | const ip = &zcu.intern_pool; |
| | 399 | var field_it = loaded_struct.iterateRuntimeOrder(ip); |
| | 400 | while (field_it.next()) |field_index| { |
| | 401 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| | 402 | const field_align = loaded_struct.fieldAlign(ip, field_index); |
| | 403 | byte_offset.* = std.mem.alignForward( |
| | 404 | u64, |
| | 405 | byte_offset.*, |
| | 406 | field_align.toByteUnits() orelse field_ty.abiAlignment(zcu).toByteUnits().?, |
| | 407 | ); |
| | 408 | if (zcu.typeToStruct(field_ty)) |field_loaded_struct| { |
| | 409 | if (field_loaded_struct.layout != .@"packed") { |
| | 410 | classifySystemVStruct(result, byte_offset, field_loaded_struct, zcu); |
| | 411 | continue; |
| | 412 | } |
| | 413 | } |
| | 414 | const field_class = std.mem.sliceTo(&classifySystemV(field_ty, zcu, .field), .none); |
| | 415 | const field_size = field_ty.abiSize(zcu); |
| | 416 | combine: { |
| | 417 | // Combine this field with the previous one. |
| | 418 | const result_class = &result[@intCast(byte_offset.* / 8)]; |
| | 419 | // "If both classes are equal, this is the resulting class." |
| | 420 | if (result_class.* == field_class[0]) { |
| | 421 | if (result_class.* == .float) { |
| | 422 | result_class.* = .float_combine; |
| | 423 | } |
| | 424 | break :combine; |
| | 425 | } |
| | 426 | |
| | 427 | // "If one of the classes is NO_CLASS, the resulting class |
| | 428 | // is the other class." |
| | 429 | if (result_class.* == .none) { |
| | 430 | result_class.* = field_class[0]; |
| | 431 | break :combine; |
| | 432 | } |
| | 433 | assert(field_class[0] != .none); |
| | 434 | |
| | 435 | // "If one of the classes is MEMORY, the result is the MEMORY class." |
| | 436 | if (result_class.* == .memory or field_class[0] == .memory) { |
| | 437 | result_class.* = .memory; |
| | 438 | break :combine; |
| | 439 | } |
| | 440 | |
| | 441 | // "If one of the classes is INTEGER, the result is the INTEGER." |
| | 442 | if (result_class.* == .integer or field_class[0] == .integer) { |
| | 443 | result_class.* = .integer; |
| | 444 | break :combine; |
| | 445 | } |
| | 446 | |
| | 447 | // "If one of the classes is X87, X87UP, COMPLEX_X87 class, |
| | 448 | // MEMORY is used as class." |
| | 449 | if (result_class.* == .x87 or |
| | 450 | result_class.* == .x87up or |
| | 451 | result_class.* == .complex_x87 or |
| | 452 | field_class[0] == .x87 or |
| | 453 | field_class[0] == .x87up or |
| | 454 | field_class[0] == .complex_x87) |
| | 455 | { |
| | 456 | result_class.* = .memory; |
| | 457 | break :combine; |
| | 458 | } |
| | 459 | |
| | 460 | // "Otherwise class SSE is used." |
| | 461 | result_class.* = .sse; |
| | 462 | } |
| | 463 | @memcpy(result[@intCast(byte_offset.* / 8 + 1)..][0 .. field_class.len - 1], field_class[1..]); |
| | 464 | byte_offset.* += field_size; |
| | 465 | } |
| | 466 | } |
| | 467 | |
| 466 | pub const SysV = struct { | 468 | pub const SysV = struct { |
| 467 | /// Note that .rsp and .rbp also belong to this set, however, we never expect to use them | 469 | /// Note that .rsp and .rbp also belong to this set, however, we never expect to use them |
| 468 | /// for anything else but stack offset tracking therefore we exclude them from this set. | 470 | /// for anything else but stack offset tracking therefore we exclude them from this set. |
| ... | @@ -592,8 +594,9 @@ const std = @import("std"); | ... | @@ -592,8 +594,9 @@ const std = @import("std"); |
| 592 | const assert = std.debug.assert; | 594 | const assert = std.debug.assert; |
| 593 | const testing = std.testing; | 595 | const testing = std.testing; |
| 594 | | 596 | |
| 595 | const Module = @import("../../Module.zig"); | 597 | const InternPool = @import("../../InternPool.zig"); |
| 596 | const Register = @import("bits.zig").Register; | 598 | const Register = @import("bits.zig").Register; |
| 597 | const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; | 599 | const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; |
| 598 | const Type = @import("../../type.zig").Type; | 600 | const Type = @import("../../type.zig").Type; |
| 599 | const Value = @import("../../Value.zig"); | 601 | const Value = @import("../../Value.zig"); |
| | 602 | const Zcu = @import("../../Module.zig"); |