intel/compiler: Get rid of the global compaction table pointers
[mesa.git] / src / intel / tools / i965_disasm.c
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_eu.h"
30 #include "dev/gen_device_info.h"
31 #include "util/u_dynarray.h"
32
33 enum opt_input_type {
34 OPT_INPUT_BINARY,
35 OPT_INPUT_C_LITERAL,
36 };
37
38 static enum opt_input_type input_type = OPT_INPUT_BINARY;
39
40 /* Return size of file in bytes pointed by fp */
41 static size_t
42 i965_disasm_get_file_size(FILE *fp)
43 {
44 size_t size;
45
46 fseek(fp, 0L, SEEK_END);
47 size = ftell(fp);
48 fseek(fp, 0L, SEEK_SET);
49
50 return size;
51 }
52
53 /* Read hex file which should be in following format:
54 * for example :
55 * { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }
56 */
57 static void *
58 i965_disasm_read_c_literal_file(FILE *fp, size_t *end)
59 {
60 struct util_dynarray assembly = {};
61 uint32_t temp[2];
62
63 if (fscanf(fp, " { ") == EOF) {
64 fprintf(stderr, "Couldn't find opening `{`\n");
65 return NULL;
66 }
67
68 if (fscanf(fp, "0x%x , 0x%x", &temp[0], &temp[1]) == 2) {
69 util_dynarray_append(&assembly, uint32_t, temp[0]);
70 util_dynarray_append(&assembly, uint32_t, temp[1]);
71 } else {
72 fprintf(stderr, "Couldn't read hex values\n");
73 return NULL;
74 }
75
76 while (fscanf(fp, " , 0x%x , 0x%x ", &temp[0], &temp[1]) == 2) {
77 util_dynarray_append(&assembly, uint32_t, temp[0]);
78 util_dynarray_append(&assembly, uint32_t, temp[1]);
79 }
80
81 if (fscanf(fp, "}") == EOF) {
82 fprintf(stderr, "Couldn't find closing `}`\n");
83 return NULL;
84 }
85
86 *end = assembly.size;
87 return assembly.data;
88 }
89
90 static void *
91 i965_disasm_read_binary(FILE *fp, size_t *end)
92 {
93 size_t size;
94 void *assembly;
95
96 *end = i965_disasm_get_file_size(fp);
97 if (!*end)
98 return NULL;
99
100 assembly = malloc(*end + 1);
101 if (assembly == NULL)
102 return NULL;
103
104 size = fread(assembly, *end, 1, fp);
105 if (!size) {
106 return NULL;
107 }
108 return assembly;
109 }
110
111 static struct gen_device_info *
112 i965_disasm_init(uint16_t pci_id)
113 {
114 struct gen_device_info *devinfo;
115
116 devinfo = malloc(sizeof *devinfo);
117 if (devinfo == NULL)
118 return NULL;
119
120 if (!gen_get_device_info_from_pci_id(pci_id, devinfo)) {
121 fprintf(stderr, "can't find device information: pci_id=0x%x\n",
122 pci_id);
123 exit(EXIT_FAILURE);
124 }
125
126 return devinfo;
127 }
128
129 static void
130 print_help(const char *progname, FILE *file)
131 {
132 fprintf(file,
133 "Usage: %s [OPTION]...\n"
134 "Disassemble i965 instructions from binary file.\n\n"
135 " --help display this help and exit\n"
136 " --input-path=PATH read binary file from binary file PATH\n"
137 " --type=INPUT_TYPE INPUT_TYPE can be 'bin' (default if omitted),\n"
138 " 'c_literal'.\n"
139 " --gen=platform disassemble instructions for given \n"
140 " platform (3 letter platform name)\n",
141 progname);
142 }
143
144 int main(int argc, char *argv[])
145 {
146 FILE *fp = NULL;
147 void *assembly = NULL;
148 char *file_path = NULL;
149 size_t start = 0, end = 0;
150 uint16_t pci_id = 0;
151 int c;
152 struct gen_device_info *devinfo = NULL;
153 int result = EXIT_FAILURE;
154
155 bool help = false;
156 const struct option i965_disasm_opts[] = {
157 { "help", no_argument, (int *) &help, true },
158 { "input-path", required_argument, NULL, 'i' },
159 { "type", required_argument, NULL, 't' },
160 { "gen", required_argument, NULL, 'g'},
161 { NULL, 0, NULL, 0 }
162 };
163
164 while ((c = getopt_long(argc, argv, ":i:t:g:h", i965_disasm_opts, NULL)) != -1) {
165 switch (c) {
166 case 'g': {
167 const int id = gen_device_name_to_pci_device_id(optarg);
168 if (id < 0) {
169 fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
170 "platform name\n", optarg);
171 goto end;
172 } else {
173 pci_id = id;
174 }
175 break;
176 }
177 case 'i':
178 file_path = strdup(optarg);
179 fp = fopen(file_path, "r");
180 if (!fp) {
181 fprintf(stderr, "Unable to read input file : %s\n",
182 file_path);
183 goto end;
184 }
185 break;
186 case 't':
187 if (strcmp(optarg, "c_literal") == 0) {
188 input_type = OPT_INPUT_C_LITERAL;
189 } else if (strcmp(optarg, "bin") == 0) {
190 input_type = OPT_INPUT_BINARY;
191 } else {
192 fprintf(stderr, "invalid value for --type: %s\n", optarg);
193 goto end;
194 }
195 break;
196 case 'h':
197 help = true;
198 print_help(argv[0], stderr);
199 goto end;
200 case 0:
201 break;
202 case ':':
203 fprintf(stderr, "%s: option `-%c' requires an argument\n",
204 argv[0], optopt);
205 goto end;
206 case '?':
207 default:
208 fprintf(stderr, "%s: option `-%c' is invalid: ignored\n",
209 argv[0], optopt);
210 goto end;
211 }
212 }
213
214 if (help || !file_path || !pci_id) {
215 print_help(argv[0], stderr);
216 exit(0);
217 }
218
219 devinfo = i965_disasm_init(pci_id);
220 if (!devinfo) {
221 fprintf(stderr, "Unable to allocate memory for "
222 "gen_device_info struct instance.\n");
223 goto end;
224 }
225
226 if (input_type == OPT_INPUT_BINARY)
227 assembly = i965_disasm_read_binary(fp, &end);
228 else if (input_type == OPT_INPUT_C_LITERAL)
229 assembly = i965_disasm_read_c_literal_file(fp, &end);
230
231 if (!assembly) {
232 if (end)
233 fprintf(stderr, "Unable to allocate buffer to read input file\n");
234 else
235 fprintf(stderr, "Failed to read input file\n");
236
237 goto end;
238 }
239
240 /* Disassemble i965 instructions from buffer assembly */
241 brw_disassemble_with_labels(devinfo, assembly, start, end, stdout);
242
243 result = EXIT_SUCCESS;
244
245 end:
246 if (fp)
247 fclose(fp);
248
249 free(file_path);
250 free(assembly);
251 free(devinfo);
252
253 exit(result);
254 }