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/intel_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
long sz = i965_disasm_get_file_size(fp);
104
assembly = malloc(*end + 1);
105
if (assembly == NULL)
108
size = fread(assembly, *end, 1, fp);
116
static struct intel_device_info *
117
i965_disasm_init(uint16_t pci_id)
119
struct intel_device_info *devinfo;
121
devinfo = malloc(sizeof *devinfo);
125
if (!intel_get_device_info_from_pci_id(pci_id, devinfo)) {
126
fprintf(stderr, "can't find device information: pci_id=0x%x\n",
135
print_help(const char *progname, FILE *file)
138
"Usage: %s [OPTION]...\n"
139
"Disassemble i965 instructions from binary file.\n\n"
140
" --help display this help and exit\n"
141
" --input-path=PATH read binary file from binary file PATH\n"
142
" --type=INPUT_TYPE INPUT_TYPE can be 'bin' (default if omitted),\n"
144
" --gen=platform disassemble instructions for given \n"
145
" platform (3 letter platform name)\n",
149
int main(int argc, char *argv[])
152
void *assembly = NULL;
153
char *file_path = NULL;
154
size_t start = 0, end = 0;
157
struct intel_device_info *devinfo = NULL;
158
int result = EXIT_FAILURE;
161
const struct option i965_disasm_opts[] = {
162
{ "help", no_argument, (int *) &help, true },
163
{ "input-path", required_argument, NULL, 'i' },
164
{ "type", required_argument, NULL, 't' },
165
{ "gen", required_argument, NULL, 'g'},
169
while ((c = getopt_long(argc, argv, ":i:t:g:h", i965_disasm_opts, NULL)) != -1) {
172
const int id = intel_device_name_to_pci_device_id(optarg);
174
fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
175
"platform name\n", optarg);
183
file_path = strdup(optarg);
184
fp = fopen(file_path, "r");
186
fprintf(stderr, "Unable to read input file : %s\n",
192
if (strcmp(optarg, "c_literal") == 0) {
193
input_type = OPT_INPUT_C_LITERAL;
194
} else if (strcmp(optarg, "bin") == 0) {
195
input_type = OPT_INPUT_BINARY;
197
fprintf(stderr, "invalid value for --type: %s\n", optarg);
203
print_help(argv[0], stderr);
208
fprintf(stderr, "%s: option `-%c' requires an argument\n",
213
fprintf(stderr, "%s: option `-%c' is invalid: ignored\n",
219
if (help || !file_path || !pci_id) {
220
print_help(argv[0], stderr);
224
devinfo = i965_disasm_init(pci_id);
226
fprintf(stderr, "Unable to allocate memory for "
227
"intel_device_info struct instance.\n");
231
if (input_type == OPT_INPUT_BINARY)
232
assembly = i965_disasm_read_binary(fp, &end);
233
else if (input_type == OPT_INPUT_C_LITERAL)
234
assembly = i965_disasm_read_c_literal_file(fp, &end);
238
fprintf(stderr, "Unable to allocate buffer to read input file\n");
240
fprintf(stderr, "Failed to read input file\n");
245
/* Disassemble i965 instructions from buffer assembly */
246
brw_disassemble_with_labels(devinfo, assembly, start, end, stdout);
248
result = EXIT_SUCCESS;