Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_sm121_disasm.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_sm121_disasm.c` 411 lines · 23199 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_sm121_disasm.c -- see omega_sm121_disasm.h. 3 * 4 * Layout of a word (lo = bits 0..63, hi = bits 64..127): opcode bits 0..11; guard predicate bits 12..14 with its 5 * negation at 15 (7 and no negation = no guard); destination register 16..23, first source 24..31, second source 6 * 32..39 or a 32-bit immediate at 32..63; third source at hi 0..7; modifiers in hi 8..40; control from hi 41 on. 7 * 8 * A row of the table is one instruction form: its opcode and fixed bits (lov, hiv) and a format. Each operand code 9 * in the format names the bits it reads; every other bit of lo and of hi 0..40 (outside the guard) must equal the 10 * row's fixed value, or the row does not match. The rows come from the words Omega's builders write 11 * (src/omega_blackwell_codegen.c encode_single_insn, the em helpers, src/omega_osc_gpu.c); the text of each was 12 * checked against nvdisasm 13.0 -b SM121 by tools/sm121dis/check.sh. 13 * 14 * CUDA++ C6 (DRA-475): the decoder is seeded Omega, om/disasm/sm121_dis.om, linked through the bridge-generated 15 * binding src/cudapp_om_sm121_dis.c (om/units.sh). Which decoder answers (make CUDAPP_DISASM=om|c|diff): 16 * - default (om): omega_sm121_disasm and omega_sm121_padding_start call omega_sm121_dis; this file only moves bytes 17 * and copies the text; 18 * - CUDAPP_MIGRATION_PENDING_C_DISASM: the hand-written C decoder below, migration-pending, the differential oracle; 19 * - CUDAPP_MIGRATION_PENDING_DISASM_DIFF: both on every word; any difference in status or text aborts 20 * (CUDAPP_DISASM_DIFF_SELFTEST makes the C side read the word with bit 16 flipped: that build must abort). */ 21 #include "omega_sm121_disasm.h" 22 #include 23 #include 24 #include 25 #include 26 27 #if defined(CUDAPP_MIGRATION_PENDING_C_DISASM) && defined(CUDAPP_MIGRATION_PENDING_DISASM_DIFF) 28 #error "CUDAPP_MIGRATION_PENDING_DISASM_DIFF needs the .om decoder: not with CUDAPP_MIGRATION_PENDING_C_DISASM" 29 #endif 30 #ifndef CUDAPP_MIGRATION_PENDING_C_DISASM 31 #include "cudapp_native.h" 32 #endif 33 34 #if defined(CUDAPP_MIGRATION_PENDING_C_DISASM) || defined(CUDAPP_MIGRATION_PENDING_DISASM_DIFF) 35 /* ---- migration-pending C decoder: the differential oracle (make CUDAPP_DISASM=c|diff) ---- */ 36 37 #define HI_BITS 41 38 #define HI_MASK ((1ULL << HI_BITS) - 1) 39 40 typedef struct { 41 uint64_t lov, hiv; /* fixed bits (operand fields zero) */ 42 const char *fmt; /* text; %x codes are operands, see field() */ 43 } Row; 44 45 /* operand codes: which bits they read (lo or hi, first bit, width); several fields for the branch target */ 46 typedef struct { char code; int hi; int at; int n; } Field; 47 static const Field FIELDS[] = { 48 {'d', 0, 16, 8}, {'a', 0, 24, 8}, {'b', 0, 32, 8}, {'c', 1, 0, 8}, /* R registers */ 49 {'D', 0, 16, 8}, {'A', 0, 24, 8}, {'B', 0, 32, 8}, {'C', 1, 0, 8}, /* uniform registers */ 50 {'i', 0, 32, 32}, {'s', 0, 32, 32}, {'f', 0, 32, 32}, /* 32-bit immediate: unsigned, signed, float */ 51 {'K', 0, 38, 21}, /* c[bank][offset]: offset 38..53, bank 54..58 */ 52 {'U', 0, 37, 22}, /* uniform load c[bank][offset]: offset 37..53 */ 53 {'o', 0, 40, 24}, /* signed 24-bit address offset */ 54 {'Y', 0, 32, 32}, /* BSSY target: signed byte distance */ 55 {'y', 0, 16, 4}, /* convergence barrier B0..B15 */ 56 {'P', 1, 17, 3}, {'Q', 1, 20, 3}, {'R', 1, 23, 4}, /* predicates; R carries its negation (hi 26) */ 57 {'l', 1, 8, 8}, /* LOP3 truth table */ 58 {'h', 0, 53, 5}, {'m', 0, 40, 13}, /* SHFL lane distance, clamp */ 59 {'S', 1, 8, 8}, /* special register */ 60 {'G', 1, 12, 3}, {'g', 1, 12, 4}, /* integer and float compare */ 61 {'Z', 1, 9, 1}, {'z', 1, 9, 3}, /* ISETP signed flag; memory access size */ 62 {'N', 1, 8, 1}, {'M', 0, 63, 1}, /* IADD3 negations: first, second source */ 63 {'j', 0, 32, 32}, /* ISETP immediate (printed signed) */ 64 }; 65 66 static const Field *field_of(char c) { 67 for (size_t i = 0; i < sizeof FIELDS / sizeof FIELDS[0]; i++) if (FIELDS[i].code == c) return &FIELDS[i]; 68 return NULL; 69 } 70 71 static uint64_t bits(uint64_t v, int at, int n) { return n >= 64 ? v >> at : (v >> at) & ((1ULL << n) - 1); } 72 static uint64_t mask(int at, int n) { return (n >= 64 ? ~0ULL : ((1ULL << n) - 1)) << at; } 73 74 /* the BRA distance: 4-byte units, low 8 bits at 16..23, the rest (signed) at 34..81 */ 75 #define BRA_LO_MASK (mask(16, 8) | mask(34, 30)) 76 #define BRA_HI_MASK mask(0, 18) 77 78 typedef struct { char *o; size_t n, at; int ok, unk; } Out; 79 80 static void put(Out *w, const char *fmt, ...) { 81 if (w->at >= w->n) { w->ok = 0; return; } 82 va_list ap; va_start(ap, fmt); 83 int k = vsnprintf(w->o + w->at, w->n - w->at, fmt, ap); 84 va_end(ap); 85 if (k < 0 || (size_t)k >= w->n - w->at) { w->ok = 0; w->at = w->n; return; } 86 w->at += (size_t)k; 87 } 88 89 static void reg(Out *w, const char *p, uint64_t r) { if (r == 255) put(w, "%sZ", p); else put(w, "%s%u", p, (unsigned)r); } 90 static void hex_s(Out *w, int64_t v) { if (v < 0) put(w, "-0x%llx", (unsigned long long)(-v)); else put(w, "0x%llx", (unsigned long long)v); } 91 static void pred(Out *w, uint64_t p, int neg) { put(w, "%s", neg ? "!" : ""); if (p == 7) put(w, "PT"); else put(w, "P%u", (unsigned)p); } 92 93 static const char *SR[256] = { 94 [0x21] = "SR_TID.X", [0x22] = "SR_TID.Y", [0x23] = "SR_TID.Z", 95 [0x25] = "SR_CTAID.X", [0x26] = "SR_CTAID.Y", [0x27] = "SR_CTAID.Z", 96 [0x00] = "SR_LANEID", [0x50] = "SR_CLOCKLO", [0x51] = "SR_CLOCKHI", 97 /* the device-side trace emitter's timer (tests/device-trace/run-emitter-checks.sh), nvdisasm 13.0.85 text */ 98 [0x52] = "SR_GLOBALTIMERLO", [0x53] = "SR_GLOBALTIMERHI", 99 }; 100 /* The special registers nvdisasm 13.0.85 prints by name (107 of 256; a sweep of S2R over 0..255, Wolfram bitmap, 101 * evidence/C6-DISASM-OM): one this table does not name is reported (?? ...), never printed as SR. */ 102 static const uint64_t SR_NAMED[4] = { 0x7ffff7efffff800full, 0xffffffff003f001full, 0x000000000000ef3full, 103 0x8000000000000000ull }; 104 static int sr_named(uint64_t v) { return (int)((SR_NAMED[(v >> 6) & 3] >> (v & 63)) & 1); } 105 static const char *ICMP[8] = { "F", "LT", "EQ", "LE", "GT", "NE", "GE", "T" }; 106 static const char *FCMP[16] = { "F", "LT", "EQ", "LE", "GT", "NE", "GE", "NUM", "NAN", "LTU", "EQU", "LEU", "GTU", "NEU", "GEU", "T" }; 107 static const char *MSIZE[8] = { ".U8" , ".S8", ".U16", ".S16", "", ".64", ".128", ".U.128" }; 108 109 /* nvdisasm prints a float immediate the shortest way that reads back exactly, with INF and NAN by name */ 110 static void fimm(Out *w, uint32_t u) { 111 float f; memcpy(&f, &u, 4); 112 if ((u & 0x7f800000u) == 0x7f800000u) { 113 if (u & 0x007fffffu) put(w, "%sQNAN", (u >> 31) ? "-" : "+"); 114 else put(w, "%sINF", (u >> 31) ? "-" : "+"); 115 return; 116 } 117 char b[64]; 118 for (int p = 1; p <= 20; p++) { 119 snprintf(b, sizeof b, "%.*g", p, (double)f); 120 float g; if (sscanf(b, "%g", &g) == 1 && memcmp(&g, &f, 4) == 0) break; 121 } 122 put(w, "%s", b); 123 } 124 125 /* Prints row's format for (lo, hi); 0 when every operand could be printed */ 126 static void render(Out *w, const char *fmt, uint64_t lo, uint64_t hi, uint64_t pc) { 127 for (const char *p = fmt; *p; p++) { 128 if (*p != '%') { put(w, "%c", *p); continue; } 129 char c = *++p; 130 const Field *f = field_of(c); 131 uint64_t v = f ? bits(f->hi ? hi : lo, f->at, f->n) : 0; 132 switch (c) { 133 #ifdef SM121DIS_MUTANT 134 case 'a': reg(w, "R", v == 255 ? v : v ^ 1); break; /* the red check: first source register off by one bit */ 135 case 'd': case 'b': case 'c': reg(w, "R", v); break; 136 #else 137 case 'd': case 'a': case 'b': case 'c': reg(w, "R", v); break; 138 #endif 139 case 'D': case 'A': case 'B': case 'C': reg(w, "UR", v); break; 140 case 'i': put(w, "0x%x", (unsigned)v); break; 141 case 's': hex_s(w, (int32_t)(uint32_t)v); break; 142 case 'f': fimm(w, (uint32_t)v); break; 143 case 'K': put(w, "c[0x%x][0x%x]", (unsigned)bits(lo, 54, 5), (unsigned)bits(lo, 38, 16)); break; 144 case 'U': put(w, "c[0x%x][0x%x]", (unsigned)bits(lo, 54, 5), (unsigned)bits(lo, 37, 17)); break; 145 case 'o': { int32_t off = (int32_t)((uint32_t)v << 8) >> 8; if (off) { put(w, "+"); hex_s(w, off); } /* nvdisasm writes a negative offset as +-0x4 */ break; } 146 case 'Y': put(w, "0x%llx", (unsigned long long)(pc + 16 + (int64_t)(int32_t)(uint32_t)v)); break; 147 case 'y': put(w, "B%u", (unsigned)v); break; 148 case 'P': case 'Q': pred(w, v, 0); break; 149 case 'R': pred(w, v & 7, (int)((v >> 3) & 1)); break; 150 case 'l': put(w, "0x%x", (unsigned)v); break; 151 case 'h': case 'm': put(w, "0x%x", (unsigned)v); break; 152 case 'S': if (SR[v]) put(w, "%s", SR[v]); else if (sr_named(v)) w->unk = 1; else put(w, "SR%u", (unsigned)v); break; 153 case 'G': put(w, "%s", ICMP[v]); break; 154 case 'Z': if (!v) put(w, ".U32"); break; 155 case 'z': put(w, "%s", MSIZE[v]); break; 156 case 'N': case 'M': if (v) put(w, "-"); break; 157 case 'j': hex_s(w, (int32_t)(uint32_t)v); break; /* signed even in a .U32 compare, as nvdisasm prints it */ 158 case 'g': put(w, "%s", FCMP[v]); break; 159 case 'T': { 160 uint64_t units = bits(lo, 16, 8) | (bits(lo, 34, 30) << 8) | (bits(hi, 0, 18) << 38); 161 int64_t s = (int64_t)(units << 8) >> 8; /* 56-bit signed */ 162 put(w, "0x%llx", (unsigned long long)(pc + 16 + s * 4)); 163 break; 164 } 165 default: w->ok = 0; return; 166 } 167 } 168 } 169 170 /* the bits a format reads */ 171 static void fields_mask(const char *fmt, uint64_t *lom, uint64_t *him) { 172 *lom = 0; *him = 0; 173 for (const char *p = fmt; *p; p++) { 174 if (*p != '%') continue; 175 char c = *++p; 176 if (c == 'T') { *lom |= BRA_LO_MASK; *him |= BRA_HI_MASK; continue; } 177 const Field *f = field_of(c); 178 if (!f) continue; 179 if (f->hi) *him |= mask(f->at, f->n); else *lom |= mask(f->at, f->n); 180 } 181 } 182 183 /* The forms. lo values include the opcode (bits 0..11); guard bits 12..15 are never part of a row. */ 184 static const Row ROWS[] = { 185 /* moves, constants, special registers */ 186 { 0x0000000000007802ull, 0x00000f00ull, "MOV %d, %i" }, 187 { 0x00000000ff007202ull, 0x00000f00ull, "MOV %d, %b" }, 188 { 0x0000000000007202ull, 0x00000f00ull, "MOV %d, %b" }, 189 { 0x0000000000007919ull, 0x00000000ull, "S2R %d, %S" }, 190 { 0x00000000ff007b82ull, 0x00000800ull, "LDC %d, %K" }, 191 { 0x00000000ff007b82ull, 0x00000a00ull, "LDC.64 %d, %K" }, 192 { 0x00000000ff0077acull, 0x08000800ull, "LDCU %D, %U" }, 193 { 0x00000000ff0077acull, 0x08000a00ull, "LDCU.64 %D, %U" }, 194 /* integer */ 195 { 0x0000000000007224ull, 0x078e0200ull, "IMAD %d, %a, %b, %c" }, 196 { 0x0000000000007c24ull, 0x0f8e0200ull, "IMAD %d, %a, %B, %c" }, 197 { 0x0000000000007825ull, 0x078e0000ull, "IMAD.WIDE.U32 %d, %a, %s, %c" }, 198 { 0x0000000000007227ull, 0x078e0000ull, "IMAD.HI.U32 %d, %a, %b, %c" }, 199 { 0x0000000000007210ull, 0x07ffe000ull, "IADD3 %d, PT, PT, %N%a, %M%b, %c" }, 200 { 0x0000000000007810ull, 0x07ffe000ull, "IADD3 %d, PT, PT, %N%a, %s, %c" }, 201 { 0x0000000000007212ull, 0x000e0000ull, "LOP3.LUT %d, %a, %b, %c, %l, %R" }, 202 { 0x0000000000007812ull, 0x000e0000ull, "LOP3.LUT %d, %a, %i, %c, %l, %R" }, 203 { 0x0000000000007819ull, 0x00011600ull, "SHF.R.U32.HI %d, %a, %i, %c" }, 204 { 0x0000000000007219ull, 0x00011600ull, "SHF.R.U32.HI %d, %a, %b, %c" }, 205 { 0x0000000000007219ull, 0x00000600ull, "SHF.L.U32 %d, %a, %b, %c" }, 206 { 0x0000000000007819ull, 0x00000600ull, "SHF.L.U32 %d, %a, %i, %c" }, 207 { 0x000000000000720cull, 0x00000070ull, "ISETP.%G%Z.AND %P, %Q, %a, %b, %R" }, 208 { 0x000000000000780cull, 0x00000070ull, "ISETP.%G%Z.AND %P, %Q, %a, %j, %R" }, 209 { 0x0000000000007c0cull, 0x08000070ull, "ISETP.%G%Z.AND %P, %Q, %a, %B, %R" }, 210 { 0x0000000000007207ull, 0x00000000ull, "SEL %d, %a, %b, %R" }, 211 { 0x0000000000007807ull, 0x00000000ull, "SEL %d, %a, %i, %R" }, 212 /* float */ 213 { 0x0000000000007221ull, 0x00000000ull, "FADD %d, %a, %b" }, 214 { 0x8000000000007221ull, 0x00000000ull, "FADD %d, %a, -%b" }, 215 { 0x0000000000007220ull, 0x00400000ull, "FMUL %d, %a, %b" }, 216 { 0x0000000000007223ull, 0x00000000ull, "FFMA %d, %a, %b, %c" }, 217 { 0x0000000000007209ull, 0x00000000ull, "FMNMX %d, %a, %b, %R" }, 218 { 0x000000000000720bull, 0x00000000ull, "FSETP.%g.AND %P, %Q, %a, %b, %R" }, 219 { 0x0000000000007245ull, 0x00201000ull, "I2FP.F32.U32 %d, %b" }, 220 { 0x0000000000007245ull, 0x00201400ull, "I2FP.F32.S32 %d, %b" }, 221 { 0x0000000000007305ull, 0x0020f000ull, "F2I.U32.TRUNC.NTZ %d, %b" }, 222 { 0x000000000000723eull, 0x0004807000ull, "F2FP.SATFINITE.E4M3.F32.PACK_AB_MERGE_C %d, %a, %b, %c" }, 223 { 0x000000000000723eull, 0x00000010ffull, "F2FP.BF16.F32.PACK_AB %d, %a, %b" }, 224 /* the #280 integration's conversions and binary64 forms (BlackwellIR ops 65..79): only the exact forms its encoder 225 * writes, checked with nvdisasm 13.0.85 -b SM121 on 2026-11-10 (om/disasm/opmove_test.om); other modifiers stay 226 * reported, not guessed */ 227 { 0x20000000ff007230ull, 0x00004100ull, "HADD2.F32 %d, -RZ, %b.H0_H0" }, 228 { 0x0000000000007304ull, 0x00200800ull, "F2F.F16.F32 %d, %b" }, 229 { 0x0000000000007229ull, 0x00000000ull, "DADD %d, %a, %c" }, 230 { 0x0000000000007228ull, 0x00000000ull, "DMUL %d, %a, %b" }, 231 { 0x000000000000722bull, 0x00000000ull, "DFMA %d, %a, %b, %c" }, 232 { 0x000000000000722aull, 0x00000000ull, "DSETP.%g.AND %P, %Q, %a, %b, %R" }, 233 { 0x0000000000007310ull, 0x00201800ull, "F2F.F64.F32 %d, %b" }, 234 { 0x0000000000007310ull, 0x00301000ull, "F2F.F32.F64 %d, %b" }, 235 { 0x0000000000007312ull, 0x00201c00ull, "I2F.F64 %d, %b" }, 236 { 0x0000000000007312ull, 0x00201800ull, "I2F.F64.U32 %d, %b" }, 237 { 0x0000000000007311ull, 0x0030d100ull, "F2I.F64.TRUNC %d, %b" }, 238 /* tensor cores */ 239 { 0x000000000000723cull, 0x00041800ull, "HMMA.16816.F32.BF16 %d, %a, %b, %c" }, 240 { 0x000000000000723cull, 0x00001800ull, "HMMA.16816.F32 %d, %a, %b, %c" }, 241 { 0x000000000000727aull, 0x00002c00ull, "QMMA.16832.F32.E4M3.E4M3 %d, %a, %b, %c" }, 242 { 0x0000000000007308ull, 0x00000800ull, "MUFU.EX2 %d, %b" }, 243 { 0x0000000000007308ull, 0x00001000ull, "MUFU.RCP %d, %b" }, 244 { 0x0000000000007308ull, 0x00001400ull, "MUFU.RSQ %d, %b" }, 245 { 0x0000000000007308ull, 0x00000c00ull, "MUFU.LG2 %d, %b" }, 246 { 0x0000000000007308ull, 0x00001800ull, "MUFU.RCP64H %d, %b" }, 247 { 0x0000000000007308ull, 0x00001c00ull, "MUFU.RSQ64H %d, %b" }, 248 /* memory */ 249 { 0x0000000000007981ull, 0x0c1e1100ull, "LDG.E%z %d, desc[%B][%a.64%o]" }, 250 { 0x0000000000007986ull, 0x0c101100ull, "STG.E%z desc[%C][%a.64%o], %b" }, 251 /* the .STRONG.SYS forms (and ATOMG) are the ones the bw_hazard_test sweep builds (decode_attn); only the 32-bit 252 * size and the PT predicate seen there are rows, every other bit pattern stays reported, not guessed. The sweep 253 * has these forms only with a zero offset, so their %o is checked by the plain LDG/STG rows above (same opcode, 254 * same offset field, nonzero offsets throughout the corpus), not by a STRONG listing of its own */ 255 { 0x0000000000007981ull, 0x0c1f5900ull, "LDG.E.STRONG.SYS %d, desc[%B][%a.64%o]" }, 256 /* BW_IR_LDG_MMIO (issue #205): 32-bit only, the one form the encoder writes; text checked with nvdisasm 13.0.85 */ 257 { 0x0000000000007981ull, 0x0c1f1900ull, "LDG.E.MMIO.GPU %d, desc[%B][%a.64%o]" }, 258 { 0x0000000000007986ull, 0x0c115900ull, "STG.E.STRONG.SYS desc[%C][%a.64%o], %b" }, 259 { 0x80000000000079a8ull, 0x081f5100ull, "ATOMG.E.ADD.STRONG.SYS PT, %d, desc[%C][%a.64], %b" }, 260 { 0x80000000000079a8ull, 0x089f5500ull, "ATOMG.E.MIN.64.STRONG.SYS PT, %d, desc[%C][%a.64], %b" }, 261 /* BW_IR_ATOMG_EXCH_STRONG_SYS: the resident kernel's grid barrier (om/resident); text checked with nvdisasm 13.0.85 262 * on that kernel's own bytes, 2026-11-12 */ 263 { 0x80000000000079a8ull, 0x0c1f5100ull, "ATOMG.E.EXCH.STRONG.SYS PT, %d, desc[%C][%a.64], %b" }, 264 { 0x0000000000007984ull, 0x08000800ull, "LDS %d, [%a+%B]" }, 265 { 0x0000000000007988ull, 0x08000800ull, "STS [%a+%C], %b" }, 266 { 0x0000000000007984ull, 0x00000000ull, "LDS%z %d, [%a%o]" }, 267 { 0x0000000000007992ull, 0x0000b000ull, "MEMBAR.ALL.SYS" }, 268 { 0x0800000000007f89ull, 0x000e0000ull, "SHFL.DOWN PT, %d, %a, %h, %m" }, 269 { 0x0c00000000007f89ull, 0x000e0000ull, "SHFL.BFLY PT, %d, %a, %h, %m" }, 270 /* control */ 271 { 0x0000000000007918ull, 0x00000000ull, "NOP" }, 272 { 0x000000000000794dull, 0x03800000ull, "EXIT" }, 273 { 0x0000000000007947ull, 0x03800000ull, "BRA %T" }, 274 { 0x0000000000007945ull, 0x03800200ull, "BSSY.RECONVERGENT %y, %Y" }, 275 { 0x0000000000007941ull, 0x03800200ull, "BSYNC.RECONVERGENT %y" }, 276 { 0x0000000000007b1dull, 0x00010000ull, "BAR.SYNC.DEFER_BLOCKING 0x0" }, 277 }; 278 279 /* IMAD with an immediate: nvdisasm names the special cases. Returns 0 for another instruction, 1 when printed. */ 280 static int imad_imm(Out *w, uint64_t lo, uint64_t hi) { 281 if (bits(lo, 0, 12) != 0x824 || (hi & HI_MASK & ~0xffull) != 0x078e0200ull) return 0; 282 uint32_t imm = (uint32_t)bits(lo, 32, 32); 283 uint64_t c = bits(hi, 0, 8); 284 const char *name = "IMAD"; 285 if (imm == 0) name = "IMAD.MOV"; /* d = c */ 286 else if (imm == 1) name = c == 255 ? "IMAD.MOV" : "IMAD.IADD"; /* d = a, d = a + c */ 287 else if (c == 255 && (imm & (imm - 1)) == 0) { 288 /* a shift. nvdisasm (CUDA 13.0, -b SM121) on 2^1..2^31 with Rc = RZ, 2026-10-27: plain IMAD for 2^16 and 289 * 2^31, .SHL for every other power */ 290 int n = __builtin_ctz(imm); 291 name = (n == 16 || n == 31) ? "IMAD" : "IMAD.SHL"; 292 } 293 put(w, "%s ", name); 294 reg(w, "R", bits(lo, 16, 8)); put(w, ", "); reg(w, "R", bits(lo, 24, 8)); put(w, ", "); 295 hex_s(w, (int32_t)imm); put(w, ", "); reg(w, "R", c); 296 return 1; 297 } 298 299 static int c_sm121_disasm(uint64_t lo, uint64_t hi, uint64_t pc, char *out, size_t outn) { 300 if (!out || outn < 40) return -2; 301 Out w = { out, outn, 0, 1, 0 }; 302 out[0] = 0; 303 uint64_t g = bits(lo, 12, 3); int gneg = (int)bits(lo, 15, 1); 304 if (!(g == 7 && !gneg)) { put(&w, "@"); pred(&w, g, gneg); put(&w, " "); } 305 const uint64_t lo_body = lo & ~mask(12, 4), hi_body = hi & HI_MASK; 306 size_t start = w.at; 307 int im = imad_imm(&w, lo_body, hi_body); 308 if (im > 0) return w.ok ? 0 : -2; 309 for (size_t i = 0; im == 0 && i < sizeof ROWS / sizeof ROWS[0]; i++) { 310 const Row *r = &ROWS[i]; 311 if ((lo_body & 0xfff) != (r->lov & 0xfff)) continue; 312 uint64_t lom, him; fields_mask(r->fmt, &lom, &him); 313 lom = ~(lom | mask(12, 4)); him = HI_MASK & ~him; 314 if ((lo_body & lom) != (r->lov & lom) || (hi_body & him) != (r->hiv & him)) continue; 315 w.at = start; render(&w, r->fmt, lo, hi, pc); 316 if (w.unk) break; /* a field value nvdisasm names but this table does not: reported, never guessed */ 317 return w.ok ? 0 : -2; 318 } 319 w.at = 0; 320 put(&w, "?? 0x%016llx%016llx", (unsigned long long)hi, (unsigned long long)lo); 321 return w.ok ? -1 : -2; 322 } 323 #endif /* the migration-pending C decoder */ 324 325 static uint64_t word_half(const uint8_t *w) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = v << 8 | w[i]; return v; } 326 327 #if defined(CUDAPP_MIGRATION_PENDING_C_DISASM) || defined(CUDAPP_MIGRATION_PENDING_DISASM_DIFF) 328 static int c_is_exit(uint64_t lo) { return (lo & 0xfff) == 0x94d; } /* EXIT, any guard */ 329 #endif 330 331 #ifndef CUDAPP_MIGRATION_PENDING_C_DISASM 332 /* The Omega decoder. Result word of omega_sm121_dis: bits 0..15 text length, 16 no form matched, 17 EXIT, 18 the 333 * text did not fit (om/disasm/sm121_dis.om). Returns as omega_sm121_disasm; *flags gets the result word. */ 334 #define OM_DIS_TEXT 256u 335 static int om_sm121_disasm(uint64_t lo, uint64_t hi, uint64_t pc, char *out, size_t outn, uint64_t *flags) { 336 uint8_t t[OM_DIS_TEXT]; 337 uint64_t r = 0; 338 *flags = 0; 339 if (omega_sm121_dis(&r, lo, hi, pc, t, OM_DIS_TEXT) != OMEGA_OK) return -2; 340 *flags = r; 341 size_t n = (size_t)(r & 0xffffu); 342 if ((r >> 18) & 1u || n >= OM_DIS_TEXT) return -2; 343 if (!out || outn < 40) return -2; 344 if (n >= outn) { out[0] = 0; return -2; } 345 memcpy(out, t, n); 346 out[n] = 0; 347 return (r >> 16) & 1u ? -1 : 0; 348 } 349 #endif 350 351 int omega_sm121_disasm(uint64_t lo, uint64_t hi, uint64_t pc, char *out, size_t outn) { 352 #ifdef CUDAPP_MIGRATION_PENDING_C_DISASM 353 return c_sm121_disasm(lo, hi, pc, out, outn); 354 #else 355 if (!out || outn < 40) return -2; 356 uint64_t flags; 357 int rc = om_sm121_disasm(lo, hi, pc, out, outn, &flags); 358 #ifdef CUDAPP_MIGRATION_PENDING_DISASM_DIFF 359 char c[256]; 360 #ifdef CUDAPP_DISASM_DIFF_SELFTEST 361 int crc = c_sm121_disasm(lo ^ (1ull << 16), hi, pc, c, outn < sizeof c ? outn : sizeof c); 362 #else 363 int crc = c_sm121_disasm(lo, hi, pc, c, outn < sizeof c ? outn : sizeof c); 364 #endif 365 if (crc != rc || (rc != -2 && strcmp(c, out) != 0) || (((flags >> 17) & 1u) != 0) != (c_is_exit(lo) != 0)) { 366 fprintf(stderr, "DISASM DIFFER pc 0x%llx word 0x%016llx%016llx: om %d \"%s\" c %d \"%s\"\n", 367 (unsigned long long)pc, (unsigned long long)hi, (unsigned long long)lo, rc, rc == -2 ? "" : out, 368 crc, crc == -2 ? "" : c); 369 abort(); 370 } 371 #endif 372 return rc; 373 #endif 374 } 375 376 size_t omega_sm121_padding_start(const uint8_t *code, size_t len) { 377 size_t end = 0; 378 for (size_t at = 0; code && at + 16 <= len; at += 16) { 379 #ifdef CUDAPP_MIGRATION_PENDING_C_DISASM 380 int ex = c_is_exit(word_half(code + at)); 381 #else 382 char t[OM_DIS_TEXT]; 383 uint64_t flags; 384 (void)om_sm121_disasm(word_half(code + at), word_half(code + at + 8), at, t, sizeof t, &flags); 385 int ex = (int)((flags >> 17) & 1u); 386 #ifdef CUDAPP_MIGRATION_PENDING_DISASM_DIFF 387 if (ex != c_is_exit(word_half(code + at))) { 388 fprintf(stderr, "DISASM DIFFER pc 0x%llx: EXIT flag om %d c %d\n", (unsigned long long)at, ex, 389 c_is_exit(word_half(code + at))); 390 abort(); 391 } 392 #endif 393 #endif 394 if (ex) end = at + 16; 395 } 396 return end; 397 } 398 399 int omega_sm121_listing(const uint8_t *code, size_t len, FILE *out) { 400 if (!code || !out || len % 16) return -1; 401 size_t pad = omega_sm121_padding_start(code, len); 402 int unknown = 0; 403 for (size_t at = 0; at < len; at += 16) { 404 char t[256]; 405 int rc = omega_sm121_disasm(word_half(code + at), word_half(code + at + 8), at, t, sizeof t); 406 if (rc == -2) return -1; 407 if (rc) unknown++; 408 fprintf(out, " /*%04llx*/ %s%s;\n", (unsigned long long)at, t, pad && at >= pad ? "" : " "); 409 } 410 return unknown; 411 }