xref: /aosp_15_r20/external/mesa3d/src/intel/compiler/brw_disasm_tool.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2018 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <getopt.h>
28 
29 #include "compiler/brw_disasm.h"
30 #include "compiler/brw_isa_info.h"
31 #include "dev/intel_device_info.h"
32 #include "util/u_dynarray.h"
33 
34 enum opt_input_type {
35    OPT_INPUT_BINARY,
36    OPT_INPUT_C_LITERAL,
37 };
38 
39 static enum opt_input_type input_type = OPT_INPUT_BINARY;
40 
41 /* Return size of file in bytes pointed by fp */
42 static long
i965_disasm_get_file_size(FILE * fp)43 i965_disasm_get_file_size(FILE *fp)
44 {
45    long size;
46 
47    fseek(fp, 0L, SEEK_END);
48    size = ftell(fp);
49    fseek(fp, 0L, SEEK_SET);
50 
51    return size;
52 }
53 
54 /* Read hex file which should be in following format:
55  * for example :
56  *    { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }
57  */
58 static void *
i965_disasm_read_c_literal_file(FILE * fp,size_t * end)59 i965_disasm_read_c_literal_file(FILE *fp, size_t *end)
60 {
61    struct util_dynarray assembly = {};
62    uint32_t temp[2];
63 
64    if (fscanf(fp, " { ") == EOF) {
65       fprintf(stderr, "Couldn't find opening `{`\n");
66       return NULL;
67    }
68 
69    if (fscanf(fp, "0x%x , 0x%x", &temp[0], &temp[1]) == 2) {
70       util_dynarray_append(&assembly, uint32_t, temp[0]);
71       util_dynarray_append(&assembly, uint32_t, temp[1]);
72    } else {
73       fprintf(stderr, "Couldn't read hex values\n");
74       return NULL;
75    }
76 
77    while (fscanf(fp, " , 0x%x , 0x%x ", &temp[0], &temp[1]) == 2) {
78       util_dynarray_append(&assembly, uint32_t, temp[0]);
79       util_dynarray_append(&assembly, uint32_t, temp[1]);
80    }
81 
82    if (fscanf(fp, "}") == EOF) {
83       fprintf(stderr, "Couldn't find closing `}`\n");
84       return NULL;
85    }
86 
87    *end = assembly.size;
88    return assembly.data;
89 }
90 
91 static void *
i965_disasm_read_binary(FILE * fp,size_t * end)92 i965_disasm_read_binary(FILE *fp, size_t *end)
93 {
94    size_t size;
95    void *assembly;
96 
97    long sz = i965_disasm_get_file_size(fp);
98    if (sz < 0)
99       return NULL;
100 
101    *end = (size_t)sz;
102    if (!*end)
103       return NULL;
104 
105    assembly = malloc(*end + 1);
106    if (assembly == NULL)
107       return NULL;
108 
109    size = fread(assembly, *end, 1, fp);
110    if (!size) {
111       free(assembly);
112       return NULL;
113    }
114    return assembly;
115 }
116 
117 static void
print_help(const char * progname,FILE * file)118 print_help(const char *progname, FILE *file)
119 {
120    fprintf(file,
121            "Usage: %s [OPTION]...\n"
122            "Disassemble i965 instructions from binary file.\n\n"
123            "      --help             display this help and exit\n"
124            "      --input-path=PATH  read binary file from binary file PATH\n"
125            "      --type=INPUT_TYPE  INPUT_TYPE can be 'bin' (default if omitted),\n"
126            "                         'c_literal'.\n"
127            "      --gen=platform     disassemble instructions for given \n"
128            "                         platform (3 letter platform name)\n",
129            progname);
130 }
131 
main(int argc,char * argv[])132 int main(int argc, char *argv[])
133 {
134    FILE *fp = NULL;
135    void *assembly = NULL;
136    char *file_path = NULL;
137    size_t start = 0, end = 0;
138    uint16_t pci_id = 0;
139    int c;
140    int result = EXIT_FAILURE;
141 
142    bool help = false;
143    const struct option i965_disasm_opts[] = {
144       { "help",          no_argument,       (int *) &help,      true },
145       { "input-path",    required_argument, NULL,               'i' },
146       { "type",          required_argument, NULL,               't' },
147       { "gen",           required_argument, NULL,               'g'},
148       { NULL,            0,                 NULL,                0 }
149    };
150 
151    while ((c = getopt_long(argc, argv, ":i:t:g:h", i965_disasm_opts, NULL)) != -1) {
152       switch (c) {
153       case 'g': {
154          const int id = intel_device_name_to_pci_device_id(optarg);
155          if (id < 0) {
156             fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
157                             "platform name\n", optarg);
158             goto end;
159          } else {
160             pci_id = id;
161          }
162          break;
163       }
164       case 'i':
165          file_path = strdup(optarg);
166          fp = fopen(file_path, "r");
167          if (!fp) {
168             fprintf(stderr, "Unable to read input file : %s\n",
169                     file_path);
170             goto end;
171          }
172          break;
173       case 't':
174          if (strcmp(optarg, "c_literal") == 0) {
175             input_type = OPT_INPUT_C_LITERAL;
176          } else if (strcmp(optarg, "bin") == 0) {
177             input_type = OPT_INPUT_BINARY;
178          } else {
179             fprintf(stderr, "invalid value for --type: %s\n", optarg);
180             goto end;
181          }
182          break;
183       case 'h':
184          help = true;
185          print_help(argv[0], stderr);
186          goto end;
187       case 0:
188          break;
189       case ':':
190          fprintf(stderr, "%s: option `-%c' requires an argument\n",
191                  argv[0], optopt);
192          goto end;
193       case '?':
194       default:
195          fprintf(stderr, "%s: option `-%c' is invalid: ignored\n",
196                  argv[0], optopt);
197          goto end;
198       }
199    }
200 
201    if (help || !file_path || !pci_id) {
202       print_help(argv[0], stderr);
203       exit(0);
204    }
205 
206    struct intel_device_info devinfo;
207    if (!intel_get_device_info_from_pci_id(pci_id, &devinfo)) {
208       fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);
209       exit(EXIT_FAILURE);
210    }
211 
212    if (devinfo.ver < 9) {
213       fprintf(stderr, "device has gfx version %d but must be >= 9, try elk_disasm instead",
214               devinfo.ver);
215       exit(EXIT_FAILURE);
216    }
217 
218    struct brw_isa_info isa;
219    brw_init_isa_info(&isa, &devinfo);
220 
221    if (input_type == OPT_INPUT_BINARY)
222       assembly = i965_disasm_read_binary(fp, &end);
223    else if (input_type == OPT_INPUT_C_LITERAL)
224       assembly = i965_disasm_read_c_literal_file(fp, &end);
225 
226    if (!assembly) {
227       if (end)
228         fprintf(stderr, "Unable to allocate buffer to read input file\n");
229       else
230         fprintf(stderr, "Failed to read input file\n");
231 
232       goto end;
233    }
234 
235    /* Disassemble i965 instructions from buffer assembly */
236    brw_disassemble_with_labels(&isa, assembly, start, end, stdout);
237 
238    result = EXIT_SUCCESS;
239 
240 end:
241    if (fp)
242       fclose(fp);
243 
244    free(file_path);
245    free(assembly);
246 
247    exit(result);
248 }
249