gallium/util: don't depend on implementation defined behavior in listen()
[mesa.git] / src / intel / tools / i965_asm.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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25 #include <stdio.h>
26 #include <getopt.h>
27 #include "i965_asm.h"
28
29 enum opt_output_type {
30 OPT_OUTPUT_HEX,
31 OPT_OUTPUT_C_LITERAL,
32 OPT_OUTPUT_BIN,
33 };
34
35 extern FILE *yyin;
36 struct brw_codegen *p;
37 static enum opt_output_type output_type = OPT_OUTPUT_BIN;
38 char *input_filename = NULL;
39 int errors;
40
41 static void
42 print_help(const char *progname, FILE *file)
43 {
44 fprintf(file,
45 "Usage: %s [OPTION] inputfile\n"
46 "Assemble i965 instructions from input file.\n\n"
47 " -h, --help display this help and exit\n"
48 " -t, --type=OUTPUT_TYPE OUTPUT_TYPE can be 'bin' (default if omitted),\n"
49 " 'c_literal', or 'hex'\n"
50 " -o, --output specify output file\n"
51 " --compact print compacted instructions\n"
52 " -g, --gen=platform assemble instructions for given \n"
53 " platform (3 letter platform name)\n"
54 "Example:\n"
55 " i965_asm -g kbl input.asm -t hex -o output\n",
56 progname);
57 }
58
59 static void
60 print_instruction(FILE *output, bool compact, const brw_inst *instruction)
61 {
62 int byte_limit;
63
64 byte_limit = (compact == true) ? 8 : 16;
65
66 switch (output_type) {
67 case OPT_OUTPUT_HEX: {
68 fprintf(output, "%02x", ((unsigned char *)instruction)[0]);
69
70 for (unsigned i = 1; i < byte_limit; i++) {
71 fprintf(output, " %02x", ((unsigned char *)instruction)[i]);
72 }
73 break;
74 }
75 case OPT_OUTPUT_C_LITERAL: {
76 fprintf(output, "\t0x%02x,", ((unsigned char *)instruction)[0]);
77
78 for (unsigned i = 1; i < byte_limit; i++) {
79 fprintf(output, " 0x%02x,", ((unsigned char *)instruction)[i]);
80 }
81 break;
82 }
83 case OPT_OUTPUT_BIN:
84 fwrite(instruction, 1, byte_limit, output);
85 break;
86 }
87
88 if (output_type != OPT_OUTPUT_BIN) {
89 fprintf(output, "\n");
90 }
91 }
92
93 static struct gen_device_info *
94 i965_disasm_init(uint16_t pci_id)
95 {
96 struct gen_device_info *devinfo;
97
98 devinfo = malloc(sizeof *devinfo);
99 if (devinfo == NULL)
100 return NULL;
101
102 if (!gen_get_device_info_from_pci_id(pci_id, devinfo)) {
103 fprintf(stderr, "can't find device information: pci_id=0x%x\n",
104 pci_id);
105 free(devinfo);
106 return NULL;
107 }
108
109 brw_init_compaction_tables(devinfo);
110
111 return devinfo;
112 }
113
114 int main(int argc, char **argv)
115 {
116 char *output_file = NULL;
117 char c;
118 FILE *output = stdout;
119 bool help = false, compact = false;
120 void *store;
121 uint64_t pci_id = 0;
122 int offset = 0, err;
123 int start_offset = 0;
124 struct disasm_info *disasm_info;
125 struct gen_device_info *devinfo = NULL;
126 int result = EXIT_FAILURE;
127
128 const struct option i965_asm_opts[] = {
129 { "help", no_argument, (int *) &help, true },
130 { "type", required_argument, NULL, 't' },
131 { "gen", required_argument, NULL, 'g' },
132 { "output", required_argument, NULL, 'o' },
133 { "compact", no_argument, (int *) &compact, true },
134 { NULL, 0, NULL, 0 }
135 };
136
137 while ((c = getopt_long(argc, argv, ":t:g:o:h", i965_asm_opts, NULL)) != -1) {
138 switch (c) {
139 case 'g': {
140 const int id = gen_device_name_to_pci_device_id(optarg);
141 if (id < 0) {
142 fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
143 "platform name\n", optarg);
144 goto end;
145 } else {
146 pci_id = id;
147 }
148 break;
149 }
150 case 'h':
151 help = true;
152 print_help(argv[0], stderr);
153 goto end;
154 case 't': {
155 if (strcmp(optarg, "hex") == 0) {
156 output_type = OPT_OUTPUT_HEX;
157 } else if (strcmp(optarg, "c_literal") == 0) {
158 output_type = OPT_OUTPUT_C_LITERAL;
159 } else if (strcmp(optarg, "bin") == 0) {
160 output_type = OPT_OUTPUT_BIN;
161 } else {
162 fprintf(stderr, "invalid value for --type: %s\n", optarg);
163 goto end;
164 }
165 break;
166 }
167 case 'o':
168 output_file = strdup(optarg);
169 break;
170 case 0:
171 break;
172 case ':':
173 fprintf(stderr, "%s: option `-%c' requires an argument\n",
174 argv[0], optopt);
175 goto end;
176 case '?':
177 default:
178 fprintf(stderr, "%s: option `-%c' is invalid: ignored\n",
179 argv[0], optopt);
180 goto end;
181 }
182 }
183
184 if (help || !pci_id) {
185 print_help(argv[0], stderr);
186 goto end;
187 }
188
189 if (!argv[optind]) {
190 fprintf(stderr, "Please specify input file\n");
191 goto end;
192 }
193
194 input_filename = strdup(argv[optind]);
195 yyin = fopen(input_filename, "r");
196 if (!yyin) {
197 fprintf(stderr, "Unable to read input file : %s\n",
198 input_filename);
199 goto end;
200 }
201
202 if (output_file) {
203 output = fopen(output_file, "w");
204 if (!output) {
205 fprintf(stderr, "Couldn't open output file\n");
206 goto end;
207 }
208 }
209
210 devinfo = i965_disasm_init(pci_id);
211 if (!devinfo) {
212 fprintf(stderr, "Unable to allocate memory for "
213 "gen_device_info struct instance.\n");
214 goto end;
215 }
216
217 p = rzalloc(NULL, struct brw_codegen);
218 brw_init_codegen(devinfo, p, p);
219 p->automatic_exec_sizes = false;
220
221 err = yyparse();
222 if (err || errors)
223 goto end;
224
225 store = p->store;
226
227 disasm_info = disasm_initialize(p->devinfo, NULL);
228 if (!disasm_info) {
229 fprintf(stderr, "Unable to initialize disasm_info struct instance\n");
230 goto end;
231 }
232
233 if (output_type == OPT_OUTPUT_C_LITERAL)
234 fprintf(output, "static const char gen_eu_bytes[] = {\n");
235
236 brw_validate_instructions(p->devinfo, p->store, 0,
237 p->next_insn_offset, disasm_info);
238
239 const int nr_insn = (p->next_insn_offset - start_offset) / 16;
240
241 if (compact)
242 brw_compact_instructions(p, start_offset, disasm_info);
243
244 for (int i = 0; i < nr_insn; i++) {
245 const brw_inst *insn = store + offset;
246 bool compacted = false;
247
248 if (compact && brw_inst_cmpt_control(p->devinfo, insn)) {
249 offset += 8;
250 compacted = true;
251 } else {
252 offset += 16;
253 }
254
255 print_instruction(output, compacted, insn);
256 }
257
258 ralloc_free(disasm_info);
259
260 if (output_type == OPT_OUTPUT_C_LITERAL)
261 fprintf(output, "}");
262
263 result = EXIT_SUCCESS;
264 goto end;
265
266 end:
267 free(input_filename);
268 free(output_file);
269
270 if (yyin)
271 fclose(yyin);
272
273 if (output)
274 fclose(output);
275
276 if (p)
277 ralloc_free(p);
278
279 if (devinfo)
280 free(devinfo);
281
282 exit(result);
283 }