| ... | ... | @@ -17,7 +17,7 @@ gpa: Allocator, |
| 17 | 17 | strip: bool, |
| 18 | 18 | |
| 19 | 19 | source_filename: String, |
| 20 | | data_layout: String, |
| 20 | data_layout: DataLayout, |
| 21 | 21 | target_triple: String, |
| 22 | 22 | module_asm: std.ArrayList(u8), |
| 23 | 23 | |
| ... | ... | @@ -87,6 +87,455 @@ pub const Options = struct { |
| 87 | 87 | triple: []const u8 = &.{}, |
| 88 | 88 | }; |
| 89 | 89 | |
| 90 | pub const DataLayout = struct { |
| 91 | endian: ?std.lang.Endian, |
| 92 | int_specs: PrimitiveSpec.Map, |
| 93 | float_specs: PrimitiveSpec.Map, |
| 94 | vector_specs: PrimitiveSpec.Map, |
| 95 | pointer_specs: PointerSpec.Map, |
| 96 | string_repr: String, |
| 97 | |
| 98 | const PrimitiveSpec = packed struct(u32) { |
| 99 | bit_width: BitWidth, |
| 100 | abi_align: Alignment, |
| 101 | pref_align: Alignment, |
| 102 | |
| 103 | const BitWidth = u20; |
| 104 | |
| 105 | const Map = std.array_hash_map.Custom(PrimitiveSpec, void, Context, false); |
| 106 | |
| 107 | const Context = struct { |
| 108 | pub fn hash(_: Context, spec: PrimitiveSpec) u32 { |
| 109 | return std.hash.int(spec.bit_width); |
| 110 | } |
| 111 | |
| 112 | pub fn eql(_: Context, lhs_spec: PrimitiveSpec, rhs_spec: PrimitiveSpec, _: usize) bool { |
| 113 | return lhs_spec.bit_width == rhs_spec.bit_width; |
| 114 | } |
| 115 | }; |
| 116 | }; |
| 117 | |
| 118 | const PointerSpec = struct { |
| 119 | bit_width: BitWidth, |
| 120 | index_bit_width: BitWidth, |
| 121 | flags: packed struct(u32) { |
| 122 | abi_align: Alignment, |
| 123 | pref_align: Alignment, |
| 124 | has_unstable_repr: bool, |
| 125 | has_external_state: bool, |
| 126 | null_ptr_repr: NullPtrRepr, |
| 127 | unused: u17 = 0, |
| 128 | }, |
| 129 | addr_space_name: String, |
| 130 | |
| 131 | const BitWidth = u32; |
| 132 | |
| 133 | const NullPtrRepr = enum(u1) { all_zeros, all_ones }; |
| 134 | |
| 135 | const Map = std.array_hash_map.Auto(AddrSpace, PointerSpec); |
| 136 | }; |
| 137 | |
| 138 | pub fn stringForTarget(target: *const std.Target) []const u8 { |
| 139 | // These data layouts should match Clang. |
| 140 | return switch (target.cpu.arch) { |
| 141 | .arc => "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-f32:32:32-i64:32-f64:32-a:0:32-n32", |
| 142 | .xcore => "e-m:e-p:32:32-i1:8:32-i8:8:32-i16:16:32-i64:32-f64:32-a:0:32-n32", |
| 143 | .hexagon => "e-m:e-p:32:32:32-a:0-n16:32-i64:64:64-i32:32:32-i16:16:16-i1:8:8-f32:32:32-f64:64:64-v32:32:32-v64:64:64-v512:512:512-v1024:1024:1024-v2048:2048:2048", |
| 144 | .lanai => "E-m:e-p:32:32-i64:64-a:0:32-n32-S64", |
| 145 | .aarch64 => if (target.ofmt == .macho) |
| 146 | if (target.os.tag == .windows or target.os.tag == .uefi) |
| 147 | "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32" |
| 148 | else if (target.abi == .ilp32) |
| 149 | "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32" |
| 150 | else |
| 151 | "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-n32:64-S128-Fn32" |
| 152 | else if (target.os.tag == .windows or target.os.tag == .uefi) |
| 153 | "e-m:w-p270:32:32-p271:32:32-p272:64:64-p:64:64-i32:32-i64:64-i128:128-n32:64-S128-Fn32" |
| 154 | else |
| 155 | "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32", |
| 156 | .aarch64_be => "E-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32", |
| 157 | .arm => if (target.ofmt == .macho) |
| 158 | "e-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" |
| 159 | else |
| 160 | "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64", |
| 161 | .armeb, .thumbeb => if (target.ofmt == .macho) |
| 162 | "E-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" |
| 163 | else |
| 164 | "E-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64", |
| 165 | .thumb => if (target.ofmt == .macho) |
| 166 | "e-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" |
| 167 | else if (target.os.tag == .windows or target.os.tag == .uefi) |
| 168 | "e-m:w-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64" |
| 169 | else |
| 170 | "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64", |
| 171 | .avr => "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8:16-a:8", |
| 172 | .bpfeb => "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128", |
| 173 | .bpfel => "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128", |
| 174 | .msp430 => "e-m:e-p:16:16-i32:16-i64:16-f32:16-f64:16-a:8-n8:16-S16", |
| 175 | .mips => "E-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64", |
| 176 | .mipsel => "e-m:m-p:32:32-i8:8:32-i16:16:32-i64:64-n32-S64", |
| 177 | .mips64 => switch (target.abi) { |
| 178 | .gnuabin32, .muslabin32, .abin32 => "E-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", |
| 179 | else => "E-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", |
| 180 | }, |
| 181 | .mips64el => switch (target.abi) { |
| 182 | .gnuabin32, .muslabin32, .abin32 => "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", |
| 183 | else => "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128", |
| 184 | }, |
| 185 | .m68k => "E-m:e-p:32:16:32-i8:8:8-i16:16:16-i32:16:32-n8:16:32-a:0:16-S16", |
| 186 | .powerpc => "E-m:e-p:32:32-Fn32-i64:64-n32", |
| 187 | .powerpcle => "e-m:e-p:32:32-Fn32-i64:64-n32", |
| 188 | .powerpc64 => switch (target.os.tag) { |
| 189 | .linux => "E-m:e-Fn32-i64:64-i128:128-n32:64-S128-v256:256:256-v512:512:512", |
| 190 | .ps3 => "E-m:e-p:32:32-Fi64-i64:64-i128:128-n32:64", |
| 191 | else => "E-m:e-Fn32-i64:64-i128:128-n32:64", |
| 192 | }, |
| 193 | .powerpc64le => if (target.os.tag == .linux) |
| 194 | "e-m:e-Fn32-i64:64-i128:128-n32:64-S128-v256:256:256-v512:512:512" |
| 195 | else |
| 196 | "e-m:e-Fn32-i64:64-i128:128-n32:64", |
| 197 | .nvptx => "e-p:32:32-p6:32:32-p7:32:32-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64", |
| 198 | .nvptx64 => "e-p6:32:32-i64:64-i128:128-i256:256-v16:16-v32:32-n16:32:64", |
| 199 | .amdgcn => "e-m:e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128:128:48-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-S32-A5-G1-ni:7:8:9", |
| 200 | .riscv32 => if (target.cpu.has(.riscv, .e)) |
| 201 | "e-m:e-p:32:32-i64:64-n32-S32" |
| 202 | else |
| 203 | "e-m:e-p:32:32-i64:64-n32-S128", |
| 204 | .riscv32be => if (target.cpu.has(.riscv, .e)) |
| 205 | "E-m:e-p:32:32-i64:64-n32-S32" |
| 206 | else |
| 207 | "E-m:e-p:32:32-i64:64-n32-S128", |
| 208 | .riscv64 => if (target.cpu.has(.riscv, .e)) |
| 209 | "e-m:e-p:64:64-i64:64-i128:128-n32:64-S64" |
| 210 | else |
| 211 | "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128", |
| 212 | .riscv64be => if (target.cpu.has(.riscv, .e)) |
| 213 | "E-m:e-p:64:64-i64:64-i128:128-n32:64-S64" |
| 214 | else |
| 215 | "E-m:e-p:64:64-i64:64-i128:128-n32:64-S128", |
| 216 | .sparc => "E-m:e-p:32:32-i64:64-i128:128-f128:64-n32-S64", |
| 217 | .sparc64 => "E-m:e-i64:64-i128:128-n32:64-S128", |
| 218 | .s390x => "E-m:e-i1:8:16-i8:8:16-i64:64-f128:64-v128:64-a:8:16-n32:64", |
| 219 | .x86 => if (target.os.tag == .windows or target.os.tag == .uefi) switch (target.abi) { |
| 220 | .gnu => if (target.ofmt == .coff) |
| 221 | "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32" |
| 222 | else |
| 223 | "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32", |
| 224 | else => blk: { |
| 225 | const msvc = switch (target.abi) { |
| 226 | .none, .msvc => true, |
| 227 | else => false, |
| 228 | }; |
| 229 | |
| 230 | break :blk if (target.ofmt == .coff) |
| 231 | if (msvc) |
| 232 | "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32-a:0:32-S32" |
| 233 | else |
| 234 | "e-m:x-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32" |
| 235 | else if (msvc) |
| 236 | "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32-a:0:32-S32" |
| 237 | else |
| 238 | "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:32-n8:16:32-a:0:32-S32"; |
| 239 | }, |
| 240 | } else if (target.ofmt == .macho) |
| 241 | "e-m:o-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128" |
| 242 | else |
| 243 | "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i128:128-f64:32:64-f80:32-n8:16:32-S128", |
| 244 | .x86_64 => if (target.os.tag.isDarwin() or target.ofmt == .macho) |
| 245 | "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" |
| 246 | else switch (target.abi) { |
| 247 | .gnux32, .muslx32, .x32 => "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", |
| 248 | else => if ((target.os.tag == .windows or target.os.tag == .uefi) and target.ofmt == .coff) |
| 249 | "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" |
| 250 | else |
| 251 | "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", |
| 252 | }, |
| 253 | .spirv32 => switch (target.os.tag) { |
| 254 | .vulkan, .opengl => "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1", |
| 255 | else => "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1", |
| 256 | }, |
| 257 | .spirv64 => "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1", |
| 258 | .wasm32 => if (target.os.tag == .emscripten) |
| 259 | "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-S128-ni:1:10:20" |
| 260 | else |
| 261 | "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20", |
| 262 | .wasm64 => if (target.os.tag == .emscripten) |
| 263 | "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-f128:64-n32:64-S128-ni:1:10:20" |
| 264 | else |
| 265 | "e-m:e-p:64:64-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20", |
| 266 | .ve => "e-m:e-i64:64-n32:64-S128-v64:64:64-v128:64:64-v256:64:64-v512:64:64-v1024:64:64-v2048:64:64-v4096:64:64-v8192:64:64-v16384:64:64", |
| 267 | .csky => "e-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32-v128:32:32-a:0:32-Fi32-n32", |
| 268 | .loongarch32 => "e-m:e-p:32:32-i64:64-n32-S128", |
| 269 | .loongarch64 => "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128", |
| 270 | .xtensa => "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32", |
| 271 | |
| 272 | .alpha, |
| 273 | .arceb, |
| 274 | .ez80, |
| 275 | .hppa, |
| 276 | .hppa64, |
| 277 | .kalimba, |
| 278 | .kvx, |
| 279 | .m88k, |
| 280 | .microblaze, |
| 281 | .microblazeel, |
| 282 | .or1k, |
| 283 | .propeller, |
| 284 | .sh, |
| 285 | .sheb, |
| 286 | .x86_16, |
| 287 | .xtensaeb, |
| 288 | => unreachable, |
| 289 | }; |
| 290 | } |
| 291 | |
| 292 | const default_int_specs: []const PrimitiveSpec = &.{ |
| 293 | .{ .bit_width = 8, .abi_align = .fromByteUnits(1), .pref_align = .fromByteUnits(1) }, // i8:8:8 |
| 294 | .{ .bit_width = 16, .abi_align = .fromByteUnits(2), .pref_align = .fromByteUnits(2) }, // i16:16:16 |
| 295 | .{ .bit_width = 32, .abi_align = .fromByteUnits(4), .pref_align = .fromByteUnits(4) }, // i32:32:32 |
| 296 | .{ .bit_width = 64, .abi_align = .fromByteUnits(4), .pref_align = .fromByteUnits(8) }, // i64:32:64 |
| 297 | }; |
| 298 | const default_float_specs: []const PrimitiveSpec = &.{ |
| 299 | .{ .bit_width = 16, .abi_align = .fromByteUnits(2), .pref_align = .fromByteUnits(2) }, // f16:16:16 |
| 300 | .{ .bit_width = 32, .abi_align = .fromByteUnits(4), .pref_align = .fromByteUnits(4) }, // f32:32:32 |
| 301 | .{ .bit_width = 64, .abi_align = .fromByteUnits(8), .pref_align = .fromByteUnits(8) }, // f64:64:64 |
| 302 | .{ .bit_width = 128, .abi_align = .fromByteUnits(16), .pref_align = .fromByteUnits(16) }, // f128:128:128 |
| 303 | }; |
| 304 | const default_vector_specs: []const PrimitiveSpec = &.{ |
| 305 | .{ .bit_width = 64, .abi_align = .fromByteUnits(8), .pref_align = .fromByteUnits(8) }, // v64:64:64 |
| 306 | .{ .bit_width = 128, .abi_align = .fromByteUnits(16), .pref_align = .fromByteUnits(16) }, // v128:128:128 |
| 307 | }; |
| 308 | |
| 309 | pub fn parseString(string_repr: String, builder: *Builder) Allocator.Error!DataLayout { |
| 310 | const gpa = builder.gpa; |
| 311 | |
| 312 | var int_specs: PrimitiveSpec.Map = .empty; |
| 313 | defer int_specs.deinit(gpa); |
| 314 | var float_specs: PrimitiveSpec.Map = .empty; |
| 315 | defer float_specs.deinit(gpa); |
| 316 | var vector_specs: PrimitiveSpec.Map = .empty; |
| 317 | defer vector_specs.deinit(gpa); |
| 318 | var pointer_specs: PointerSpec.Map = .empty; |
| 319 | defer pointer_specs.deinit(gpa); |
| 320 | var non_integral_addr_spaces: std.ArrayList(AddrSpace) = .empty; |
| 321 | defer non_integral_addr_spaces.deinit(gpa); |
| 322 | |
| 323 | try int_specs.ensureTotalCapacity(gpa, default_int_specs.len); |
| 324 | for (default_int_specs) |int_spec| int_specs.putAssumeCapacityNoClobber(int_spec, {}); |
| 325 | try float_specs.ensureTotalCapacity(gpa, default_float_specs.len); |
| 326 | for (default_float_specs) |float_spec| float_specs.putAssumeCapacityNoClobber(float_spec, {}); |
| 327 | try vector_specs.ensureTotalCapacity(gpa, default_vector_specs.len); |
| 328 | for (default_vector_specs) |vector_spec| vector_specs.putAssumeCapacityNoClobber(vector_spec, {}); |
| 329 | try pointer_specs.putNoClobber(gpa, .default, comptime .{ |
| 330 | .bit_width = 64, |
| 331 | .index_bit_width = 64, |
| 332 | .flags = .{ |
| 333 | .abi_align = .fromByteUnits(8), |
| 334 | .pref_align = .fromByteUnits(8), |
| 335 | .has_unstable_repr = false, |
| 336 | .has_external_state = false, |
| 337 | .null_ptr_repr = .all_zeros, |
| 338 | }, |
| 339 | .addr_space_name = .none, |
| 340 | }); |
| 341 | |
| 342 | var endian: ?std.lang.Endian = null; |
| 343 | var spec_it = std.mem.splitScalar(u8, string_repr.slice(builder).?, '-'); |
| 344 | while (spec_it.next()) |spec| switch (spec[0]) { |
| 345 | else => {}, |
| 346 | 'E' => { |
| 347 | assert(spec.len == 1); |
| 348 | assert(endian == null); |
| 349 | endian = .big; |
| 350 | }, |
| 351 | 'e' => { |
| 352 | assert(spec.len == 1); |
| 353 | assert(endian == null); |
| 354 | endian = .little; |
| 355 | }, |
| 356 | 'p' => { |
| 357 | var field_it = std.mem.splitScalar(u8, spec[1..], ':'); |
| 358 | |
| 359 | const first = field_it.first(); |
| 360 | var has_unstable_repr = false; |
| 361 | var has_external_state = false; |
| 362 | var null_ptr_repr: ?PointerSpec.NullPtrRepr = null; |
| 363 | var addr_space_name: String = .none; |
| 364 | const addr_space = for (first, 0..) |flag, as_start| switch (flag) { |
| 365 | 'u' => has_unstable_repr = true, |
| 366 | 'e' => has_external_state = true, |
| 367 | 'z' => { |
| 368 | assert(null_ptr_repr == null); |
| 369 | null_ptr_repr = .all_zeros; |
| 370 | }, |
| 371 | 'o' => { |
| 372 | assert(null_ptr_repr == null); |
| 373 | null_ptr_repr = .all_ones; |
| 374 | }, |
| 375 | else => { |
| 376 | if (first[first.len - ")".len] != ')') break first[as_start..]; |
| 377 | const name_start = std.mem.findScalarPos(u8, first, as_start, '(').?; |
| 378 | addr_space_name = try builder.string(first[name_start + "(".len .. first.len - ")".len]); |
| 379 | break first[as_start..name_start]; |
| 380 | }, |
| 381 | } else first[first.len..]; |
| 382 | const bit_width = std.fmt.parseInt(PointerSpec.BitWidth, field_it.next().?, 10) catch unreachable; |
| 383 | const abi_align: Alignment = .fromByteUnits(std.fmt.parseInt(u64, field_it.next().?, 10) catch unreachable); |
| 384 | const pref_align: Alignment = if (field_it.next()) |pref_align| |
| 385 | .fromByteUnits(std.fmt.parseInt(u64, pref_align, 10) catch unreachable) |
| 386 | else |
| 387 | abi_align; |
| 388 | const index_bit_width = if (field_it.next()) |index_bit_width| |
| 389 | std.fmt.parseInt(PointerSpec.BitWidth, index_bit_width, 10) catch unreachable |
| 390 | else |
| 391 | bit_width; |
| 392 | assert(field_it.peek() == null); |
| 393 | |
| 394 | try pointer_specs.put(gpa, switch (addr_space.len) { |
| 395 | 0 => .default, |
| 396 | else => @fromBackingInt(std.fmt.parseInt(u24, addr_space, 10) catch unreachable), |
| 397 | }, .{ |
| 398 | .bit_width = bit_width, |
| 399 | .index_bit_width = index_bit_width, |
| 400 | .flags = .{ |
| 401 | .abi_align = abi_align, |
| 402 | .pref_align = pref_align, |
| 403 | .has_unstable_repr = has_unstable_repr, |
| 404 | .has_external_state = has_external_state, |
| 405 | .null_ptr_repr = null_ptr_repr orelse .all_zeros, |
| 406 | }, |
| 407 | .addr_space_name = addr_space_name, |
| 408 | }); |
| 409 | }, |
| 410 | 'i', 'f', 'v' => |kind| { |
| 411 | if (std.mem.eql(u8, spec, "ve")) { |
| 412 | vector_specs.clearRetainingCapacity(); |
| 413 | continue; |
| 414 | } |
| 415 | var field_it = std.mem.splitScalar(u8, spec[1..], ':'); |
| 416 | const bit_width = std.fmt.parseInt(PrimitiveSpec.BitWidth, field_it.first(), 10) catch unreachable; |
| 417 | const abi_align: Alignment = .fromByteUnits(std.fmt.parseInt(u64, field_it.next().?, 10) catch unreachable); |
| 418 | const pref_align: Alignment = if (field_it.next()) |pref_align| |
| 419 | .fromByteUnits(std.fmt.parseInt(u64, pref_align, 10) catch unreachable) |
| 420 | else |
| 421 | abi_align; |
| 422 | assert(field_it.peek() == null); |
| 423 | const specs = switch (kind) { |
| 424 | else => unreachable, |
| 425 | 'i' => &int_specs, |
| 426 | 'f' => &float_specs, |
| 427 | 'v' => &vector_specs, |
| 428 | }; |
| 429 | try specs.put(gpa, .{ .bit_width = bit_width, .abi_align = abi_align, .pref_align = pref_align }, {}); |
| 430 | }, |
| 431 | 'n' => { |
| 432 | var field_it = std.mem.splitScalar(u8, spec[1..], ':'); |
| 433 | if (std.mem.eql(u8, field_it.first(), "i")) { |
| 434 | while (field_it.next()) |non_integral_addr_space| try non_integral_addr_spaces.append( |
| 435 | gpa, |
| 436 | @fromBackingInt(std.fmt.parseInt(u24, non_integral_addr_space, 10) catch unreachable), |
| 437 | ); |
| 438 | } else { |
| 439 | field_it.reset(); |
| 440 | while (field_it.next()) |native_bit_width| { |
| 441 | _ = std.fmt.parseInt(PrimitiveSpec.BitWidth, native_bit_width, 10) catch unreachable; |
| 442 | } |
| 443 | } |
| 444 | }, |
| 445 | }; |
| 446 | |
| 447 | for (non_integral_addr_spaces.items) |non_integral_addr_space| { |
| 448 | const pointer_spec_gop = try pointer_specs.getOrPut(gpa, non_integral_addr_space); |
| 449 | if (!pointer_spec_gop.found_existing) pointer_spec_gop.value_ptr.* = pointer_specs.get(.default).?; |
| 450 | pointer_spec_gop.value_ptr.flags.has_unstable_repr = true; |
| 451 | pointer_spec_gop.value_ptr.flags.has_external_state = false; |
| 452 | } |
| 453 | |
| 454 | { |
| 455 | const SortContext = struct { |
| 456 | specs: []const PrimitiveSpec, |
| 457 | pub fn lessThan(ctx: @This(), lhs_index: usize, rhs_index: usize) bool { |
| 458 | return ctx.specs[lhs_index].bit_width < ctx.specs[rhs_index].bit_width; |
| 459 | } |
| 460 | }; |
| 461 | int_specs.sortUnstable(SortContext{ .specs = int_specs.keys() }); |
| 462 | float_specs.sortUnstable(SortContext{ .specs = float_specs.keys() }); |
| 463 | vector_specs.sortUnstable(SortContext{ .specs = vector_specs.keys() }); |
| 464 | } |
| 465 | { |
| 466 | const SortContext = struct { |
| 467 | addr_spaces: []const AddrSpace, |
| 468 | pub fn lessThan(ctx: @This(), lhs_index: usize, rhs_index: usize) bool { |
| 469 | return @backingInt(ctx.addr_spaces[lhs_index]) < @backingInt(ctx.addr_spaces[rhs_index]); |
| 470 | } |
| 471 | }; |
| 472 | pointer_specs.sortUnstable(SortContext{ .addr_spaces = pointer_specs.keys() }); |
| 473 | assert(pointer_specs.keys()[0] == .default); |
| 474 | } |
| 475 | |
| 476 | return .{ |
| 477 | .endian = endian, |
| 478 | .int_specs = int_specs.move(), |
| 479 | .float_specs = float_specs.move(), |
| 480 | .vector_specs = vector_specs.move(), |
| 481 | .pointer_specs = pointer_specs.move(), |
| 482 | .string_repr = string_repr, |
| 483 | }; |
| 484 | } |
| 485 | |
| 486 | pub fn deinit(data_layout: *DataLayout, gpa: Allocator) void { |
| 487 | data_layout.int_specs.deinit(gpa); |
| 488 | data_layout.float_specs.deinit(gpa); |
| 489 | data_layout.vector_specs.deinit(gpa); |
| 490 | data_layout.pointer_specs.deinit(gpa); |
| 491 | } |
| 492 | |
| 493 | pub fn getIntegerSpec(data_layout: *const DataLayout, bit_width: PrimitiveSpec.BitWidth) PrimitiveSpec { |
| 494 | const specs = data_layout.int_specs.keys(); |
| 495 | return specs[ |
| 496 | @min(std.sort.lowerBound(PrimitiveSpec, specs, bit_width, struct { |
| 497 | fn order(ctx: PrimitiveSpec.BitWidth, spec: PrimitiveSpec) std.math.Order { |
| 498 | return std.math.order(ctx, spec.bit_width); |
| 499 | } |
| 500 | }.order), specs.len - 1) |
| 501 | ]; |
| 502 | } |
| 503 | |
| 504 | pub fn getFloatSpec(data_layout: *const DataLayout, bit_width: PrimitiveSpec.BitWidth) PrimitiveSpec { |
| 505 | if (data_layout.float_specs.getEntry(.{ |
| 506 | .bit_width = bit_width, |
| 507 | .abi_align = .default, |
| 508 | .pref_align = .default, |
| 509 | })) |entry| return entry.key_ptr.*; |
| 510 | const default_align: Alignment = .fromByteUnits( |
| 511 | std.math.ceilPowerOfTwoAssert(PrimitiveSpec.BitWidth, bit_width / 8), |
| 512 | ); |
| 513 | return .{ .bit_width = bit_width, .abi_align = default_align, .pref_align = default_align }; |
| 514 | } |
| 515 | |
| 516 | pub fn getVectorSpec( |
| 517 | data_layout: *const DataLayout, |
| 518 | bit_width: PrimitiveSpec.BitWidth, |
| 519 | store_size: Type.Size, |
| 520 | ) PrimitiveSpec { |
| 521 | if (data_layout.float_specs.getEntry(.{ |
| 522 | .bit_width = bit_width, |
| 523 | .abi_align = .default, |
| 524 | .pref_align = .default, |
| 525 | })) |entry| return entry.key_ptr.*; |
| 526 | const default_align: Alignment = .fromByteUnits( |
| 527 | std.math.ceilPowerOfTwoAssert(PrimitiveSpec.BitWidth, switch (store_size) { |
| 528 | .fixed, .scalable => |known_min| known_min, |
| 529 | }), |
| 530 | ); |
| 531 | return .{ .bit_width = bit_width, .abi_align = default_align, .pref_align = default_align }; |
| 532 | } |
| 533 | |
| 534 | pub fn getPointerSpec(data_layout: *const DataLayout, addr_space: AddrSpace) PointerSpec { |
| 535 | return data_layout.pointer_specs.get(addr_space) orelse data_layout.pointer_specs.values()[0]; |
| 536 | } |
| 537 | }; |
| 538 | |
| 90 | 539 | pub const String = enum(u32) { |
| 91 | 540 | none = maxInt(u31), |
| 92 | 541 | empty, |
| ... | ... | @@ -489,7 +938,10 @@ pub const Type = enum(u32) { |
| 489 | 938 | .double, .i64, .x86_mmx => 64, |
| 490 | 939 | .x86_fp80, .i80 => 80, |
| 491 | 940 | .fp128, .ppc_fp128, .i128 => 128, |
| 492 | | .ptr, .@"ptr addrspace(4)" => @panic("TODO: query data layout"), |
| 941 | .ptr => @intCast(builder.data_layout.getPointerSpec(.default).bit_width), |
| 942 | .@"ptr addrspace(4)" => @intCast( |
| 943 | builder.data_layout.getPointerSpec(@fromBackingInt(@intCast(4))).bit_width, |
| 944 | ), |
| 493 | 945 | _ => { |
| 494 | 946 | const item = builder.type_items.items[@backingInt(self)]; |
| 495 | 947 | return switch (item.tag) { |
| ... | ... | @@ -498,7 +950,9 @@ pub const Type = enum(u32) { |
| 498 | 950 | .vararg_function, |
| 499 | 951 | => unreachable, |
| 500 | 952 | .integer => @intCast(item.data), |
| 501 | | .pointer => @panic("TODO: query data layout"), |
| 953 | .pointer => @intCast( |
| 954 | builder.data_layout.getPointerSpec(@fromBackingInt(@intCast(item.data))).bit_width, |
| 955 | ), |
| 502 | 956 | .target => unreachable, |
| 503 | 957 | .vector, |
| 504 | 958 | .scalable_vector, |
| ... | ... | @@ -931,6 +1385,67 @@ pub const Type = enum(u32) { |
| 931 | 1385 | }, |
| 932 | 1386 | }; |
| 933 | 1387 | } |
| 1388 | |
| 1389 | const Size = union(enum) { fixed: u64, scalable: u64 }; |
| 1390 | pub fn bits(ty: Type, builder: *const Builder) Size { |
| 1391 | const item = builder.type_items.items[@backingInt(ty)]; |
| 1392 | return switch (item.tag) { |
| 1393 | else => unreachable, |
| 1394 | .simple => switch (@as(Simple, @fromBackingInt(@intCast(item.data)))) { |
| 1395 | else => unreachable, |
| 1396 | .label => .{ .fixed = builder.data_layout.getPointerSpec(.default).bit_width }, |
| 1397 | .half, .bfloat => .{ .fixed = 16 }, |
| 1398 | .float => .{ .fixed = 32 }, |
| 1399 | .double => .{ .fixed = 64 }, |
| 1400 | .ppc_fp128, .fp128 => .{ .fixed = 128 }, |
| 1401 | .x86_amx => .{ .fixed = 8192 }, |
| 1402 | .x86_fp80 => .{ .fixed = 80 }, |
| 1403 | }, |
| 1404 | .integer => .{ .fixed = item.data }, |
| 1405 | .pointer => .{ |
| 1406 | .fixed = builder.data_layout.getPointerSpec(@fromBackingInt(@intCast(item.data))).bit_width, |
| 1407 | }, |
| 1408 | }; |
| 1409 | } |
| 1410 | |
| 1411 | pub fn alignment(ty: Type, kind: enum { abi, pref }, builder: *const Builder) Alignment { |
| 1412 | const item = builder.type_items.items[@backingInt(ty)]; |
| 1413 | switch (item.tag) { |
| 1414 | else => unreachable, |
| 1415 | .simple => switch (@as(Simple, @fromBackingInt(@intCast(item.data)))) { |
| 1416 | else => unreachable, |
| 1417 | .label => { |
| 1418 | const spec = builder.data_layout.getPointerSpec(.default); |
| 1419 | return switch (kind) { |
| 1420 | .abi => spec.flags.abi_align, |
| 1421 | .pref => spec.flags.pref_align, |
| 1422 | }; |
| 1423 | }, |
| 1424 | .half, .bfloat, .float, .double, .ppc_fp128, .fp128, .x86_fp80 => { |
| 1425 | const spec = builder.data_layout.getFloatSpec(@intCast(ty.bits(builder).fixed)); |
| 1426 | return switch (kind) { |
| 1427 | .abi => spec.abi_align, |
| 1428 | .pref => spec.pref_align, |
| 1429 | }; |
| 1430 | }, |
| 1431 | .x86_amx => return comptime .fromByteUnits(64), |
| 1432 | }, |
| 1433 | .integer => { |
| 1434 | const spec = builder.data_layout.getIntegerSpec(@intCast(item.data)); |
| 1435 | return switch (kind) { |
| 1436 | .abi => spec.abi_align, |
| 1437 | .pref => spec.pref_align, |
| 1438 | }; |
| 1439 | }, |
| 1440 | .pointer => { |
| 1441 | const spec = builder.data_layout.getPointerSpec(@fromBackingInt(@intCast(item.data))); |
| 1442 | return switch (kind) { |
| 1443 | .abi => spec.flags.abi_align, |
| 1444 | .pref => spec.flags.pref_align, |
| 1445 | }; |
| 1446 | }, |
| 1447 | } |
| 1448 | } |
| 934 | 1449 | }; |
| 935 | 1450 | |
| 936 | 1451 | pub const Attribute = union(Kind) { |
| ... | ... | @@ -2182,11 +2697,18 @@ pub const Alignment = enum(u6) { |
| 2182 | 2697 | }; |
| 2183 | 2698 | } |
| 2184 | 2699 | |
| 2185 | | /// Asserts that neither `a` nor `b` is `.default`. |
| 2186 | | pub fn max(a: Alignment, b: Alignment) Alignment { |
| 2187 | | assert(a != .default); |
| 2188 | | assert(b != .default); |
| 2189 | | return @fromBackingInt(@intCast(@max(@backingInt(a), @backingInt(b)))); |
| 2700 | /// Asserts that neither `lhs` nor `rhs` is `.default`. |
| 2701 | pub fn max(lhs: Alignment, rhs: Alignment) Alignment { |
| 2702 | assert(lhs != .default); |
| 2703 | assert(rhs != .default); |
| 2704 | return @fromBackingInt(@max(@backingInt(lhs), @backingInt(rhs))); |
| 2705 | } |
| 2706 | |
| 2707 | /// Asserts that neither `lhs` nor `rhs` is `.default`. |
| 2708 | pub fn order(lhs: Alignment, rhs: Alignment) std.math.Order { |
| 2709 | assert(lhs != .default); |
| 2710 | assert(rhs != .default); |
| 2711 | return std.math.order(@backingInt(lhs), @backingInt(rhs)); |
| 2190 | 2712 | } |
| 2191 | 2713 | |
| 2192 | 2714 | pub fn toLlvm(self: Alignment) u6 { |
| ... | ... | @@ -9023,7 +9545,14 @@ pub fn init(options: Options) Allocator.Error!Builder { |
| 9023 | 9545 | .strip = options.strip, |
| 9024 | 9546 | |
| 9025 | 9547 | .source_filename = .none, |
| 9026 | | .data_layout = .none, |
| 9548 | .data_layout = .{ |
| 9549 | .endian = null, |
| 9550 | .int_specs = .empty, |
| 9551 | .float_specs = .empty, |
| 9552 | .vector_specs = .empty, |
| 9553 | .pointer_specs = .empty, |
| 9554 | .string_repr = .none, |
| 9555 | }, |
| 9027 | 9556 | .target_triple = .none, |
| 9028 | 9557 | .module_asm = .empty, |
| 9029 | 9558 | |
| ... | ... | @@ -9079,14 +9608,14 @@ pub fn init(options: Options) Allocator.Error!Builder { |
| 9079 | 9608 | try self.string_indices.append(self.gpa, 0); |
| 9080 | 9609 | assert(try self.string("") == .empty); |
| 9081 | 9610 | |
| 9611 | self.data_layout = try .parseString(try self.string(DataLayout.stringForTarget(options.target)), &self); |
| 9612 | |
| 9082 | 9613 | try self.strtab_string_indices.append(self.gpa, 0); |
| 9083 | 9614 | assert(try self.strtabString("") == .empty); |
| 9084 | 9615 | |
| 9085 | 9616 | if (options.name.len > 0) self.source_filename = try self.string(options.name); |
| 9086 | 9617 | |
| 9087 | | if (options.triple.len > 0) { |
| 9088 | | self.target_triple = try self.string(options.triple); |
| 9089 | | } |
| 9618 | if (options.triple.len > 0) self.target_triple = try self.string(options.triple); |
| 9090 | 9619 | |
| 9091 | 9620 | { |
| 9092 | 9621 | const static_len = @typeInfo(Type).@"enum".field_names.len - 1; |
| ... | ... | @@ -9179,6 +9708,8 @@ pub fn clearAndFree(self: *Builder) void { |
| 9179 | 9708 | pub fn deinit(self: *Builder) void { |
| 9180 | 9709 | const gpa = self.gpa; |
| 9181 | 9710 | |
| 9711 | self.data_layout.deinit(gpa); |
| 9712 | |
| 9182 | 9713 | self.module_asm.deinit(gpa); |
| 9183 | 9714 | |
| 9184 | 9715 | self.string_map.deinit(gpa); |
| ... | ... | @@ -9998,17 +10529,17 @@ pub fn print(self: *Builder, w: *Writer) (Writer.Error || Allocator.Error)!void |
| 9998 | 10529 | var metadata_formatter: Metadata.Formatter = .{ .builder = self, .need_comma = undefined }; |
| 9999 | 10530 | defer metadata_formatter.map.deinit(self.gpa); |
| 10000 | 10531 | |
| 10001 | | if (self.source_filename != .none or self.data_layout != .none or self.target_triple != .none) { |
| 10532 | if (self.source_filename != .none or self.data_layout.string_repr != .none or self.target_triple != .none) { |
| 10002 | 10533 | if (need_newline) try w.writeByte('\n') else need_newline = true; |
| 10003 | 10534 | if (self.source_filename != .none) try w.print( |
| 10004 | 10535 | \\; ModuleID = '{s}' |
| 10005 | 10536 | \\source_filename = {f} |
| 10006 | 10537 | \\ |
| 10007 | 10538 | , .{ self.source_filename.slice(self).?, self.source_filename.fmtQ(self) }); |
| 10008 | | if (self.data_layout != .none) try w.print( |
| 10539 | if (self.data_layout.string_repr != .none) try w.print( |
| 10009 | 10540 | \\target datalayout = {f} |
| 10010 | 10541 | \\ |
| 10011 | | , .{self.data_layout.fmtQ(self)}); |
| 10542 | , .{self.data_layout.string_repr.fmtQ(self)}); |
| 10012 | 10543 | if (self.target_triple != .none) try w.print( |
| 10013 | 10544 | \\target triple = {f} |
| 10014 | 10545 | \\ |
| ... | ... | @@ -13706,7 +14237,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator, producer: Producer) bitco |
| 13706 | 14237 | }); |
| 13707 | 14238 | } |
| 13708 | 14239 | |
| 13709 | | if (self.data_layout.slice(self)) |data_layout| { |
| 14240 | if (self.data_layout.string_repr.slice(self)) |data_layout| { |
| 13710 | 14241 | try module_block.writeAbbrev(ModuleBlock.String{ |
| 13711 | 14242 | .code = 3, |
| 13712 | 14243 | .string = data_layout, |