| ... | ... | @@ -164,27 +164,33 @@ pub const Instruction = union(enum) { |
| 164 | 164 | // name them with letters since there's no official naming scheme. |
| 165 | 165 | // TODO: need to rename the minor formats to a more descriptive name. |
| 166 | 166 | |
| 167 | // I am using regular structs instead of packed ones to avoid |
| 168 | // endianness-dependent behavior when constructing the actual |
| 169 | // assembly instructions. |
| 170 | // See also: https://github.com/ziglang/zig/issues/10113 |
| 171 | // TODO: change it back to packed structs once the issue is resolved. |
| 172 | |
| 167 | 173 | // Format 1 (op = 1): CALL |
| 168 | | format_1: packed struct { |
| 174 | format_1: struct { |
| 169 | 175 | op: u2 = 0b01, |
| 170 | 176 | disp30: u30, |
| 171 | 177 | }, |
| 172 | 178 | |
| 173 | 179 | // Format 2 (op = 0): SETHI & Branches (Bicc, BPcc, BPr, FBfcc, FBPfcc) |
| 174 | | format_2a: packed struct { |
| 180 | format_2a: struct { |
| 175 | 181 | op: u2 = 0b00, |
| 176 | 182 | rd: u5, |
| 177 | 183 | op2: u3, |
| 178 | 184 | imm22: u22, |
| 179 | 185 | }, |
| 180 | | format_2b: packed struct { |
| 186 | format_2b: struct { |
| 181 | 187 | op: u2 = 0b00, |
| 182 | 188 | a: u1, |
| 183 | 189 | cond: u4, |
| 184 | 190 | op2: u3, |
| 185 | 191 | disp22: u22, |
| 186 | 192 | }, |
| 187 | | format_2c: packed struct { |
| 193 | format_2c: struct { |
| 188 | 194 | op: u2 = 0b00, |
| 189 | 195 | a: u1, |
| 190 | 196 | cond: u4, |
| ... | ... | @@ -194,7 +200,7 @@ pub const Instruction = union(enum) { |
| 194 | 200 | p: u1, |
| 195 | 201 | disp19: u19, |
| 196 | 202 | }, |
| 197 | | format_2d: packed struct { |
| 203 | format_2d: struct { |
| 198 | 204 | op: u2 = 0b00, |
| 199 | 205 | a: u1, |
| 200 | 206 | fixed: u1 = 0b0, |
| ... | ... | @@ -207,7 +213,7 @@ pub const Instruction = union(enum) { |
| 207 | 213 | }, |
| 208 | 214 | |
| 209 | 215 | // Format 3 (op = 2 or 3): Arithmetic, Logical, MOVr, MEMBAR, Load, and Store |
| 210 | | format_3a: packed struct { |
| 216 | format_3a: struct { |
| 211 | 217 | op: u2, |
| 212 | 218 | rd: u5, |
| 213 | 219 | op3: u6, |
| ... | ... | @@ -224,7 +230,7 @@ pub const Instruction = union(enum) { |
| 224 | 230 | i: u1 = 0b1, |
| 225 | 231 | simm13: u13, |
| 226 | 232 | }, |
| 227 | | format_3c: packed struct { |
| 233 | format_3c: struct { |
| 228 | 234 | op: u2, |
| 229 | 235 | reserved1: u5 = 0b00000, |
| 230 | 236 | op3: u6, |
| ... | ... | @@ -241,7 +247,7 @@ pub const Instruction = union(enum) { |
| 241 | 247 | i: u1 = 0b1, |
| 242 | 248 | simm13: u13, |
| 243 | 249 | }, |
| 244 | | format_3e: packed struct { |
| 250 | format_3e: struct { |
| 245 | 251 | op: u2, |
| 246 | 252 | rd: u5, |
| 247 | 253 | op3: u6, |
| ... | ... | @@ -260,7 +266,7 @@ pub const Instruction = union(enum) { |
| 260 | 266 | rcond: u3, |
| 261 | 267 | simm10: u10, |
| 262 | 268 | }, |
| 263 | | format_3g: packed struct { |
| 269 | format_3g: struct { |
| 264 | 270 | op: u2, |
| 265 | 271 | rd: u5, |
| 266 | 272 | op3: u6, |
| ... | ... | @@ -269,7 +275,7 @@ pub const Instruction = union(enum) { |
| 269 | 275 | reserved: u8 = 0b00000000, |
| 270 | 276 | rs2: u5, |
| 271 | 277 | }, |
| 272 | | format_3h: packed struct { |
| 278 | format_3h: struct { |
| 273 | 279 | op: u2 = 0b10, |
| 274 | 280 | fixed1: u5 = 0b00000, |
| 275 | 281 | op3: u6 = 0b101000, |
| ... | ... | @@ -279,7 +285,7 @@ pub const Instruction = union(enum) { |
| 279 | 285 | cmask: u3, |
| 280 | 286 | mmask: u4, |
| 281 | 287 | }, |
| 282 | | format_3i: packed struct { |
| 288 | format_3i: struct { |
| 283 | 289 | op: u2, |
| 284 | 290 | rd: u5, |
| 285 | 291 | op3: u6, |
| ... | ... | @@ -288,13 +294,13 @@ pub const Instruction = union(enum) { |
| 288 | 294 | imm_asi: u8, |
| 289 | 295 | rs2: u5, |
| 290 | 296 | }, |
| 291 | | format_3j: packed struct { |
| 297 | format_3j: struct { |
| 292 | 298 | op: u2, |
| 293 | 299 | impl_dep1: u5, |
| 294 | 300 | op3: u6, |
| 295 | 301 | impl_dep2: u19, |
| 296 | 302 | }, |
| 297 | | format_3k: packed struct { |
| 303 | format_3k: struct { |
| 298 | 304 | op: u2, |
| 299 | 305 | rd: u5, |
| 300 | 306 | op3: u6, |
| ... | ... | @@ -304,7 +310,7 @@ pub const Instruction = union(enum) { |
| 304 | 310 | reserved: u7 = 0b0000000, |
| 305 | 311 | rs2: u5, |
| 306 | 312 | }, |
| 307 | | format_3l: packed struct { |
| 313 | format_3l: struct { |
| 308 | 314 | op: u2, |
| 309 | 315 | rd: u5, |
| 310 | 316 | op3: u6, |
| ... | ... | @@ -314,7 +320,7 @@ pub const Instruction = union(enum) { |
| 314 | 320 | reserved: u7 = 0b0000000, |
| 315 | 321 | shcnt32: u5, |
| 316 | 322 | }, |
| 317 | | format_3m: packed struct { |
| 323 | format_3m: struct { |
| 318 | 324 | op: u2, |
| 319 | 325 | rd: u5, |
| 320 | 326 | op3: u6, |
| ... | ... | @@ -324,7 +330,7 @@ pub const Instruction = union(enum) { |
| 324 | 330 | reserved: u6 = 0b000000, |
| 325 | 331 | shcnt64: u6, |
| 326 | 332 | }, |
| 327 | | format_3n: packed struct { |
| 333 | format_3n: struct { |
| 328 | 334 | op: u2, |
| 329 | 335 | rd: u5, |
| 330 | 336 | op3: u6, |
| ... | ... | @@ -332,7 +338,7 @@ pub const Instruction = union(enum) { |
| 332 | 338 | opf: u9, |
| 333 | 339 | rs2: u5, |
| 334 | 340 | }, |
| 335 | | format_3o: packed struct { |
| 341 | format_3o: struct { |
| 336 | 342 | op: u2, |
| 337 | 343 | fixed: u3 = 0b000, |
| 338 | 344 | cc1: u1, |
| ... | ... | @@ -342,7 +348,7 @@ pub const Instruction = union(enum) { |
| 342 | 348 | opf: u9, |
| 343 | 349 | rs2: u5, |
| 344 | 350 | }, |
| 345 | | format_3p: packed struct { |
| 351 | format_3p: struct { |
| 346 | 352 | op: u2, |
| 347 | 353 | rd: u5, |
| 348 | 354 | op3: u6, |
| ... | ... | @@ -350,20 +356,20 @@ pub const Instruction = union(enum) { |
| 350 | 356 | opf: u9, |
| 351 | 357 | rs2: u5, |
| 352 | 358 | }, |
| 353 | | format_3q: packed struct { |
| 359 | format_3q: struct { |
| 354 | 360 | op: u2, |
| 355 | 361 | rd: u5, |
| 356 | 362 | op3: u6, |
| 357 | 363 | rs1: u5, |
| 358 | 364 | reserved: u14 = 0b00000000000000, |
| 359 | 365 | }, |
| 360 | | format_3r: packed struct { |
| 366 | format_3r: struct { |
| 361 | 367 | op: u2, |
| 362 | 368 | fcn: u5, |
| 363 | 369 | op3: u6, |
| 364 | 370 | reserved: u19 = 0b0000000000000000000, |
| 365 | 371 | }, |
| 366 | | format_3s: packed struct { |
| 372 | format_3s: struct { |
| 367 | 373 | op: u2, |
| 368 | 374 | rd: u5, |
| 369 | 375 | op3: u6, |
| ... | ... | @@ -371,7 +377,7 @@ pub const Instruction = union(enum) { |
| 371 | 377 | }, |
| 372 | 378 | |
| 373 | 379 | //Format 4 (op = 2): MOVcc, FMOVr, FMOVcc, and Tcc |
| 374 | | format_4a: packed struct { |
| 380 | format_4a: struct { |
| 375 | 381 | op: u2 = 0b10, |
| 376 | 382 | rd: u5, |
| 377 | 383 | op3: u6, |
| ... | ... | @@ -392,7 +398,7 @@ pub const Instruction = union(enum) { |
| 392 | 398 | cc0: u1, |
| 393 | 399 | simm11: u11, |
| 394 | 400 | }, |
| 395 | | format_4c: packed struct { |
| 401 | format_4c: struct { |
| 396 | 402 | op: u2 = 0b10, |
| 397 | 403 | rd: u5, |
| 398 | 404 | op3: u6, |
| ... | ... | @@ -415,7 +421,7 @@ pub const Instruction = union(enum) { |
| 415 | 421 | cc0: u1, |
| 416 | 422 | simm11: u11, |
| 417 | 423 | }, |
| 418 | | format_4e: packed struct { |
| 424 | format_4e: struct { |
| 419 | 425 | op: u2 = 0b10, |
| 420 | 426 | rd: u5, |
| 421 | 427 | op3: u6, |
| ... | ... | @@ -426,7 +432,7 @@ pub const Instruction = union(enum) { |
| 426 | 432 | reserved: u4 = 0b0000, |
| 427 | 433 | sw_trap: u7, |
| 428 | 434 | }, |
| 429 | | format_4f: packed struct { |
| 435 | format_4f: struct { |
| 430 | 436 | op: u2 = 0b10, |
| 431 | 437 | rd: u5, |
| 432 | 438 | op3: u6, |
| ... | ... | @@ -436,7 +442,7 @@ pub const Instruction = union(enum) { |
| 436 | 442 | opf_low: u5, |
| 437 | 443 | rs2: u5, |
| 438 | 444 | }, |
| 439 | | format_4g: packed struct { |
| 445 | format_4g: struct { |
| 440 | 446 | op: u2 = 0b10, |
| 441 | 447 | rd: u5, |
| 442 | 448 | op3: u6, |
| ... | ... | @@ -512,37 +518,37 @@ pub const Instruction = union(enum) { |
| 512 | 518 | pub fn toU32(self: Instruction) u32 { |
| 513 | 519 | // TODO: Remove this once packed structs work. |
| 514 | 520 | return switch (self) { |
| 515 | | .format_1 => |v| @bitCast(u32, v), |
| 516 | | .format_2a => |v| @bitCast(u32, v), |
| 517 | | .format_2b => |v| @bitCast(u32, v), |
| 518 | | .format_2c => |v| @bitCast(u32, v), |
| 519 | | .format_2d => |v| @bitCast(u32, v), |
| 520 | | .format_3a => |v| @bitCast(u32, v), |
| 521 | .format_1 => |v| (@as(u32, v.op) << 30) | @as(u32, v.disp30), |
| 522 | .format_2a => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op2) << 22) | @as(u32, v.imm22), |
| 523 | .format_2b => |v| (@as(u32, v.op) << 30) | (@as(u32, v.a) << 29) | (@as(u32, v.cond) << 25) | (@as(u32, v.op2) << 22) | @as(u32, v.disp22), |
| 524 | .format_2c => |v| (@as(u32, v.op) << 30) | (@as(u32, v.a) << 29) | (@as(u32, v.cond) << 25) | (@as(u32, v.op2) << 22) | (@as(u32, v.cc1) << 21) | (@as(u32, v.cc0) << 20) | (@as(u32, v.p) << 19) | @as(u32, v.disp19), |
| 525 | .format_2d => |v| (@as(u32, v.op) << 30) | (@as(u32, v.a) << 29) | (@as(u32, v.fixed) << 28) | (@as(u32, v.rcond) << 25) | (@as(u32, v.op2) << 22) | (@as(u32, v.d16hi) << 20) | (@as(u32, v.p) << 19) | (@as(u32, v.rs1) << 14) | @as(u32, v.d16lo), |
| 526 | .format_3a => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.reserved) << 5) | @as(u32, v.rs2), |
| 521 | 527 | .format_3b => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | @as(u32, v.simm13), |
| 522 | | .format_3c => |v| @bitCast(u32, v), |
| 528 | .format_3c => |v| (@as(u32, v.op) << 30) | (@as(u32, v.reserved1) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.reserved2) << 5) | @as(u32, v.rs2), |
| 523 | 529 | .format_3d => |v| (@as(u32, v.op) << 30) | (@as(u32, v.reserved) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | @as(u32, v.simm13), |
| 524 | | .format_3e => |v| @bitCast(u32, v), |
| 530 | .format_3e => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.rcond) << 10) | (@as(u32, v.reserved) << 5) | @as(u32, v.rs2), |
| 525 | 531 | .format_3f => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.rcond) << 10) | @as(u32, v.simm10), |
| 526 | | .format_3g => |v| @bitCast(u32, v), |
| 527 | | .format_3h => |v| @bitCast(u32, v), |
| 528 | | .format_3i => |v| @bitCast(u32, v), |
| 529 | | .format_3j => |v| @bitCast(u32, v), |
| 530 | | .format_3k => |v| @bitCast(u32, v), |
| 531 | | .format_3l => |v| @bitCast(u32, v), |
| 532 | | .format_3m => |v| @bitCast(u32, v), |
| 533 | | .format_3n => |v| @bitCast(u32, v), |
| 534 | | .format_3o => |v| @bitCast(u32, v), |
| 535 | | .format_3p => |v| @bitCast(u32, v), |
| 536 | | .format_3q => |v| @bitCast(u32, v), |
| 537 | | .format_3r => |v| @bitCast(u32, v), |
| 538 | | .format_3s => |v| @bitCast(u32, v), |
| 539 | | .format_4a => |v| @bitCast(u32, v), |
| 532 | .format_3g => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.reserved) << 5) | @as(u32, v.rs2), |
| 533 | .format_3h => |v| (@as(u32, v.op) << 30) | (@as(u32, v.fixed1) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.fixed2) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.reserved) << 7) | (@as(u32, v.cmask) << 4) | @as(u32, v.mmask), |
| 534 | .format_3i => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.imm_asi) << 5) | @as(u32, v.rs2), |
| 535 | .format_3j => |v| (@as(u32, v.op) << 30) | (@as(u32, v.impl_dep1) << 25) | (@as(u32, v.op3) << 19) | @as(u32, v.impl_dep2), |
| 536 | .format_3k => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.x) << 12) | (@as(u32, v.reserved) << 5) | @as(u32, v.rs2), |
| 537 | .format_3l => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.x) << 12) | (@as(u32, v.reserved) << 5) | @as(u32, v.shcnt32), |
| 538 | .format_3m => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.x) << 12) | (@as(u32, v.reserved) << 6) | @as(u32, v.shcnt64), |
| 539 | .format_3n => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.reserved) << 14) | (@as(u32, v.opf) << 5) | @as(u32, v.rs2), |
| 540 | .format_3o => |v| (@as(u32, v.op) << 30) | (@as(u32, v.fixed) << 27) | (@as(u32, v.cc1) << 26) | (@as(u32, v.cc0) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.opf) << 5) | @as(u32, v.rs2), |
| 541 | .format_3p => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.opf) << 5) | @as(u32, v.rs2), |
| 542 | .format_3q => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | @as(u32, v.reserved), |
| 543 | .format_3r => |v| (@as(u32, v.op) << 30) | (@as(u32, v.fcn) << 25) | (@as(u32, v.op3) << 19) | @as(u32, v.reserved), |
| 544 | .format_3s => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | @as(u32, v.reserved), |
| 545 | .format_4a => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.cc1) << 12) | (@as(u32, v.cc0) << 11) | (@as(u32, v.reserved) << 5) | @as(u32, v.rs2), |
| 540 | 546 | .format_4b => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.cc1) << 12) | (@as(u32, v.cc0) << 11) | @as(u32, v.simm11), |
| 541 | | .format_4c => |v| @bitCast(u32, v), |
| 547 | .format_4c => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.cc2) << 18) | (@as(u32, v.cond) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.cc1) << 12) | (@as(u32, v.cc0) << 11) | (@as(u32, v.reserved) << 5) | @as(u32, v.rs2), |
| 542 | 548 | .format_4d => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.cc2) << 18) | (@as(u32, v.cond) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.cc1) << 12) | (@as(u32, v.cc0) << 11) | @as(u32, v.simm11), |
| 543 | | .format_4e => |v| @bitCast(u32, v), |
| 544 | | .format_4f => |v| @bitCast(u32, v), |
| 545 | | .format_4g => |v| @bitCast(u32, v), |
| 549 | .format_4e => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.i) << 13) | (@as(u32, v.cc1) << 12) | (@as(u32, v.cc0) << 11) | (@as(u32, v.reserved) << 7) | @as(u32, v.sw_trap), |
| 550 | .format_4f => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.rs1) << 14) | (@as(u32, v.fixed) << 13) | (@as(u32, v.rcond) << 10) | (@as(u32, v.opf_low) << 5) | @as(u32, v.rs2), |
| 551 | .format_4g => |v| (@as(u32, v.op) << 30) | (@as(u32, v.rd) << 25) | (@as(u32, v.op3) << 19) | (@as(u32, v.fixed) << 18) | (@as(u32, v.cond) << 14) | (@as(u32, v.opf_cc) << 11) | (@as(u32, v.opf_low) << 5) | @as(u32, v.rs2), |
| 546 | 552 | }; |
| 547 | 553 | } |
| 548 | 554 | |