2 * Copyright © 2018 Intel Corporation
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:
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
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
29 #include "compiler/brw_eu.h"
30 #include "dev/gen_device_info.h"
31 #include "util/u_dynarray.h"
38 static enum opt_input_type input_type
= OPT_INPUT_BINARY
;
40 /* Return size of file in bytes pointed by fp */
42 i965_disasm_get_file_size(FILE *fp
)
46 fseek(fp
, 0L, SEEK_END
);
48 fseek(fp
, 0L, SEEK_SET
);
53 /* Read hex file which should be in following format:
55 * { 0x00000000, 0x00000000, 0x00000000, 0x00000000 }
58 i965_disasm_read_c_literal_file(FILE *fp
, size_t *end
)
60 struct util_dynarray assembly
= {};
63 if (fscanf(fp
, " { ") == EOF
) {
64 fprintf(stderr
, "Couldn't find opening `{`\n");
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]);
72 fprintf(stderr
, "Couldn't read hex values\n");
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]);
81 if (fscanf(fp
, "}") == EOF
) {
82 fprintf(stderr
, "Couldn't find closing `}`\n");
91 i965_disasm_read_binary(FILE *fp
, size_t *end
)
96 *end
= i965_disasm_get_file_size(fp
);
100 assembly
= malloc(*end
+ 1);
101 if (assembly
== NULL
)
104 size
= fread(assembly
, *end
, 1, fp
);
111 static struct gen_device_info
*
112 i965_disasm_init(uint16_t pci_id
)
114 struct gen_device_info
*devinfo
;
116 devinfo
= malloc(sizeof *devinfo
);
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",
126 /* initialize compaction table in order to handle compacted instructions */
127 brw_init_compaction_tables(devinfo
);
133 print_help(const char *progname
, FILE *file
)
136 "Usage: %s [OPTION]...\n"
137 "Disassemble i965 instructions from binary file.\n\n"
138 " --help display this help and exit\n"
139 " --input-path=PATH read binary file from binary file PATH\n"
140 " --type=INPUT_TYPE INPUT_TYPE can be 'bin' (default if omitted),\n"
142 " --gen=platform disassemble instructions for given \n"
143 " platform (3 letter platform name)\n",
147 int main(int argc
, char *argv
[])
150 void *assembly
= NULL
;
151 char *file_path
= NULL
;
152 size_t start
= 0, end
= 0;
155 struct gen_device_info
*devinfo
= NULL
;
156 int result
= EXIT_FAILURE
;
159 const struct option i965_disasm_opts
[] = {
160 { "help", no_argument
, (int *) &help
, true },
161 { "input-path", required_argument
, NULL
, 'i' },
162 { "type", required_argument
, NULL
, 't' },
163 { "gen", required_argument
, NULL
, 'g'},
167 while ((c
= getopt_long(argc
, argv
, ":i:t:g:h", i965_disasm_opts
, NULL
)) != -1) {
170 const int id
= gen_device_name_to_pci_device_id(optarg
);
172 fprintf(stderr
, "can't parse gen: '%s', expected 3 letter "
173 "platform name\n", optarg
);
181 file_path
= strdup(optarg
);
182 fp
= fopen(file_path
, "r");
184 fprintf(stderr
, "Unable to read input file : %s\n",
190 if (strcmp(optarg
, "c_literal") == 0) {
191 input_type
= OPT_INPUT_C_LITERAL
;
192 } else if (strcmp(optarg
, "bin") == 0) {
193 input_type
= OPT_INPUT_BINARY
;
195 fprintf(stderr
, "invalid value for --type: %s\n", optarg
);
201 print_help(argv
[0], stderr
);
206 fprintf(stderr
, "%s: option `-%c' requires an argument\n",
211 fprintf(stderr
, "%s: option `-%c' is invalid: ignored\n",
217 if (help
|| !file_path
|| !pci_id
) {
218 print_help(argv
[0], stderr
);
222 devinfo
= i965_disasm_init(pci_id
);
224 fprintf(stderr
, "Unable to allocate memory for "
225 "gen_device_info struct instance.\n");
229 if (input_type
== OPT_INPUT_BINARY
)
230 assembly
= i965_disasm_read_binary(fp
, &end
);
231 else if (input_type
== OPT_INPUT_C_LITERAL
)
232 assembly
= i965_disasm_read_c_literal_file(fp
, &end
);
236 fprintf(stderr
, "Unable to allocate buffer to read input file\n");
238 fprintf(stderr
, "Failed to read input file\n");
243 /* Disassemble i965 instructions from buffer assembly */
244 brw_disassemble(devinfo
, assembly
, start
, end
, stdout
);
246 result
= EXIT_SUCCESS
;