interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / tools / sm121dis / sm121dis.c

60 lines · 2983 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* sm121dis -- Omega's sm_121 disassembler on the command line (C6 of docs/OMEGA.md). The decoder is
3 * om/disasm/sm121_dis.om through src/cudapp_om_sm121_dis.c; CUDAPP_DISASM=c|diff selects the C oracle in
4 * src/omega_sm121_disasm.c or both (Makefile).
5 *
6 * sm121dis <code.bin> a listing in nvdisasm's layout: the address in a comment, then TEXT ;
7 * sm121dis -b SM121 <code.bin> the same, called the way the checks used to call nvdisasm
8 * sm121dis --pairs <code.bin> one line per word: <addr> <lo hex> <hi hex> <text> ; (what check.sh compares)
9 * sm121dis --version names the tool, for the check logs
10 *
11 * Exit 0 when every word was a known form, 1 when some were not (printed "?? ..."), 2 for a usage or file error. */
12#include "omega_sm121_disasm.h"
13#include "omega_dis_words.h"
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17
18static int usage(void) {
19 fprintf(stderr, "usage: sm121dis [--pairs | -b SM121] <code.bin> | --version\n");
20 return 2;
21}
22
23int main(int argc, char **argv) {
24 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
25 printf("sm121dis: Omega's own sm_121 disassembler (om/disasm/sm121_dis.om; C oracle src/omega_sm121_disasm.c)\n");
26 return 0;
27 }
28 int pairs = argc == 3 && strcmp(argv[1], "--pairs") == 0;
29 int arch = argc == 4 && strcmp(argv[1], "-b") == 0;
30 if (arch && strcmp(argv[2], "SM121") != 0) { fprintf(stderr, "sm121dis: only -b SM121\n"); return 2; }
31 if (argc != 2 + pairs + 2 * arch) return usage();
32 const char *path = argv[argc - 1];
33 FILE *f = fopen(path, "rb");
34 if (!f) { perror(path); return 2; }
35 size_t cap = 1 << 16, len = 0, n;
36 unsigned char *code = malloc(cap);
37 while (code && (n = fread(code + len, 1, cap - len, f)) > 0) {
38 len += n;
39 if (len == cap) { unsigned char *c = realloc(code, cap *= 2); if (!c) { free(code); code = NULL; } else code = c; }
40 }
41 fclose(f);
42 if (!code) { fprintf(stderr, "sm121dis: out of memory\n"); return 2; }
43 if (len % 16) { fprintf(stderr, "sm121dis: %s is not a whole number of 16-byte words\n", path); free(code); return 2; }
44 int unknown = 0;
45 size_t pad = omega_sm121_padding_start(code, len);
46 if (!pairs) unknown = omega_sm121_listing(code, len, stdout);
47 else for (size_t at = 0; at < len; at += 16) {
48 uint64_t lo = omega_dis_word(code + at, 0, 0, 0);
49 uint64_t hi = omega_dis_word(code + at + 8, 0, 0, 0);
50 char t[256];
51 int rc = omega_sm121_disasm(lo, hi, at, t, sizeof t);
52 if (rc == -2) { unknown = -1; break; }
53 if (rc) unknown++;
54 printf("%04llx %016llx %016llx %s%s;\n", (unsigned long long)at, (unsigned long long)lo, (unsigned long long)hi, t,
55 pad && at >= pad ? "" : " ");
56 }
57 free(code);
58 if (unknown < 0) { fprintf(stderr, "sm121dis: text too long\n"); return 2; }
59 return unknown ? 1 : 0;
60}