• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "brw_asm.h"
28 #include "intel/compiler/brw_disasm_info.h"
29 
30 enum opt_output_type {
31    OPT_OUTPUT_HEX,
32    OPT_OUTPUT_C_LITERAL,
33    OPT_OUTPUT_BIN,
34 };
35 
36 extern FILE *yyin;
37 struct brw_codegen *p;
38 static enum opt_output_type output_type = OPT_OUTPUT_BIN;
39 char *input_filename = NULL;
40 int errors;
41 
42 struct list_head instr_labels;
43 struct list_head target_labels;
44 
45 static void
print_help(const char * progname,FILE * file)46 print_help(const char *progname, FILE *file)
47 {
48    fprintf(file,
49            "Usage: %s [OPTION] inputfile\n"
50            "Assemble i965 instructions from input file.\n\n"
51            "    -h, --help             display this help and exit\n"
52            "    -t, --type=OUTPUT_TYPE OUTPUT_TYPE can be 'bin' (default if omitted),\n"
53            "                           'c_literal', or 'hex'\n"
54            "    -o, --output           specify output file\n"
55            "        --compact          print compacted instructions\n"
56            "    -g, --gen=platform     assemble instructions for given \n"
57            "                           platform (3 letter platform name)\n"
58            "Example:\n"
59            "    i965_asm -g kbl input.asm -t hex -o output\n",
60            progname);
61 }
62 
63 static uint32_t
get_dword(const brw_inst * inst,int idx)64 get_dword(const brw_inst *inst, int idx)
65 {
66    uint32_t dword;
67    memcpy(&dword, (char *)inst + 4 * idx, sizeof(dword));
68    return dword;
69 }
70 
71 static void
print_instruction(FILE * output,bool compact,const brw_inst * instruction)72 print_instruction(FILE *output, bool compact, const brw_inst *instruction)
73 {
74    int byte_limit;
75 
76    byte_limit = (compact == true) ? 8 : 16;
77 
78    switch (output_type) {
79    case OPT_OUTPUT_HEX: {
80       fprintf(output, "%02x", ((unsigned char *)instruction)[0]);
81 
82       for (unsigned i = 1; i < byte_limit; i++) {
83          fprintf(output, " %02x", ((unsigned char *)instruction)[i]);
84       }
85       break;
86    }
87    case OPT_OUTPUT_C_LITERAL: {
88       fprintf(output, "\t0x%08x,", get_dword(instruction, 0));
89 
90       for (unsigned i = 1; i < byte_limit / 4; i++)
91          fprintf(output, " 0x%08x,", get_dword(instruction, i));
92 
93       break;
94    }
95    case OPT_OUTPUT_BIN:
96       fwrite(instruction, 1, byte_limit, output);
97       break;
98    }
99 
100    if (output_type != OPT_OUTPUT_BIN) {
101       fprintf(output, "\n");
102    }
103 }
104 
105 static struct intel_device_info *
i965_asm_init(uint16_t pci_id)106 i965_asm_init(uint16_t pci_id)
107 {
108    struct intel_device_info *devinfo;
109 
110    devinfo = malloc(sizeof *devinfo);
111    if (devinfo == NULL)
112       return NULL;
113 
114    if (!intel_get_device_info_from_pci_id(pci_id, devinfo)) {
115       fprintf(stderr, "can't find device information: pci_id=0x%x\n",
116               pci_id);
117       free(devinfo);
118       return NULL;
119    }
120 
121    if (devinfo->ver < 9) {
122       fprintf(stderr, "device has gfx version %d but must be >= 9, try elk_asm instead",
123               devinfo->ver);
124       exit(EXIT_FAILURE);
125    }
126 
127    return devinfo;
128 }
129 
130 static bool
i965_postprocess_labels()131 i965_postprocess_labels()
132 {
133    void *store = p->store;
134 
135    struct target_label *tlabel;
136    struct instr_label *ilabel, *s;
137 
138    const unsigned to_bytes_scale = brw_jump_scale(p->devinfo);
139 
140    LIST_FOR_EACH_ENTRY(tlabel, &target_labels, link) {
141       LIST_FOR_EACH_ENTRY_SAFE(ilabel, s, &instr_labels, link) {
142          if (!strcmp(tlabel->name, ilabel->name)) {
143             brw_inst *inst = store + ilabel->offset;
144 
145             int relative_offset = (tlabel->offset - ilabel->offset) / sizeof(brw_inst);
146             relative_offset *= to_bytes_scale;
147 
148             unsigned opcode = brw_inst_opcode(p->isa, inst);
149 
150             if (ilabel->type == INSTR_LABEL_JIP) {
151                switch (opcode) {
152                case BRW_OPCODE_IF:
153                case BRW_OPCODE_ELSE:
154                case BRW_OPCODE_ENDIF:
155                case BRW_OPCODE_WHILE:
156                   brw_inst_set_jip(p->devinfo, inst, relative_offset);
157                   break;
158                case BRW_OPCODE_BREAK:
159                case BRW_OPCODE_HALT:
160                case BRW_OPCODE_CONTINUE:
161                   brw_inst_set_jip(p->devinfo, inst, relative_offset);
162                   break;
163                default:
164                   fprintf(stderr, "Unknown opcode %d with JIP label\n", opcode);
165                   return false;
166                }
167             } else {
168                switch (opcode) {
169                case BRW_OPCODE_IF:
170                case BRW_OPCODE_ELSE:
171                   brw_inst_set_uip(p->devinfo, inst, relative_offset);
172                   break;
173                case BRW_OPCODE_WHILE:
174                case BRW_OPCODE_ENDIF:
175                   fprintf(stderr, "WHILE/ENDIF cannot have UIP offset\n");
176                   return false;
177                case BRW_OPCODE_BREAK:
178                case BRW_OPCODE_CONTINUE:
179                case BRW_OPCODE_HALT:
180                   brw_inst_set_uip(p->devinfo, inst, relative_offset);
181                   break;
182                default:
183                   fprintf(stderr, "Unknown opcode %d with UIP label\n", opcode);
184                   return false;
185                }
186             }
187 
188             list_del(&ilabel->link);
189          }
190       }
191    }
192 
193    LIST_FOR_EACH_ENTRY(ilabel, &instr_labels, link) {
194       fprintf(stderr, "Unknown label '%s'\n", ilabel->name);
195    }
196 
197    return list_is_empty(&instr_labels);
198 }
199 
main(int argc,char ** argv)200 int main(int argc, char **argv)
201 {
202    char *output_file = NULL;
203    char c;
204    FILE *output = stdout;
205    bool help = false, compact = false;
206    void *store;
207    uint64_t pci_id = 0;
208    int offset = 0, err;
209    int start_offset = 0;
210    struct disasm_info *disasm_info;
211    struct intel_device_info *devinfo = NULL;
212    int result = EXIT_FAILURE;
213    list_inithead(&instr_labels);
214    list_inithead(&target_labels);
215 
216    const struct option i965_asm_opts[] = {
217       { "help",          no_argument,       (int *) &help,      true },
218       { "type",          required_argument, NULL,               't' },
219       { "gen",           required_argument, NULL,               'g' },
220       { "output",        required_argument, NULL,               'o' },
221       { "compact",       no_argument,       (int *) &compact,   true },
222       { NULL,            0,                 NULL,               0 }
223    };
224 
225    while ((c = getopt_long(argc, argv, ":t:g:o:h", i965_asm_opts, NULL)) != -1) {
226       switch (c) {
227       case 'g': {
228          const int id = intel_device_name_to_pci_device_id(optarg);
229          if (id < 0) {
230             fprintf(stderr, "can't parse gen: '%s', expected 3 letter "
231                             "platform name\n", optarg);
232             goto end;
233          } else {
234             pci_id = id;
235          }
236          break;
237       }
238       case 'h':
239          help = true;
240          print_help(argv[0], stderr);
241          goto end;
242       case 't': {
243          if (strcmp(optarg, "hex") == 0) {
244             output_type = OPT_OUTPUT_HEX;
245          } else if (strcmp(optarg, "c_literal") == 0) {
246             output_type = OPT_OUTPUT_C_LITERAL;
247          } else if (strcmp(optarg, "bin") == 0) {
248             output_type = OPT_OUTPUT_BIN;
249          } else {
250             fprintf(stderr, "invalid value for --type: %s\n", optarg);
251             goto end;
252          }
253          break;
254       }
255       case 'o':
256          output_file = strdup(optarg);
257          break;
258       case 0:
259          break;
260       case ':':
261          fprintf(stderr, "%s: option `-%c' requires an argument\n",
262                  argv[0], optopt);
263          goto end;
264       case '?':
265       default:
266          fprintf(stderr, "%s: option `-%c' is invalid: ignored\n",
267                  argv[0], optopt);
268          goto end;
269       }
270    }
271 
272    if (help || !pci_id) {
273       print_help(argv[0], stderr);
274       goto end;
275    }
276 
277    if (!argv[optind]) {
278       fprintf(stderr, "Please specify input file\n");
279       goto end;
280    }
281 
282    input_filename = strdup(argv[optind]);
283    yyin = fopen(input_filename, "r");
284    if (!yyin) {
285       fprintf(stderr, "Unable to read input file : %s\n",
286               input_filename);
287       goto end;
288    }
289 
290    if (output_file) {
291       output = fopen(output_file, "w");
292       if (!output) {
293          fprintf(stderr, "Couldn't open output file\n");
294          goto end;
295       }
296    }
297 
298    devinfo = i965_asm_init(pci_id);
299    if (!devinfo) {
300       fprintf(stderr, "Unable to allocate memory for "
301                       "intel_device_info struct instance.\n");
302       goto end;
303    }
304 
305    struct brw_isa_info isa;
306    brw_init_isa_info(&isa, devinfo);
307 
308    p = rzalloc(NULL, struct brw_codegen);
309    brw_init_codegen(&isa, p, p);
310 
311    err = yyparse();
312    if (err || errors)
313       goto end;
314 
315    if (!i965_postprocess_labels())
316       goto end;
317 
318    store = p->store;
319 
320    disasm_info = disasm_initialize(p->isa, NULL);
321    if (!disasm_info) {
322       fprintf(stderr, "Unable to initialize disasm_info struct instance\n");
323       goto end;
324    }
325 
326    if (output_type == OPT_OUTPUT_C_LITERAL)
327       fprintf(output, "{\n");
328 
329    brw_validate_instructions(p->isa, p->store, 0,
330                              p->next_insn_offset, disasm_info);
331 
332    const int nr_insn = (p->next_insn_offset - start_offset) / 16;
333 
334    if (compact)
335       brw_compact_instructions(p, start_offset, disasm_info);
336 
337    for (int i = 0; i < nr_insn; i++) {
338       const brw_inst *insn = store + offset;
339       bool compacted = false;
340 
341       if (compact && brw_inst_cmpt_control(p->devinfo, insn)) {
342             offset += 8;
343             compacted = true;
344       } else {
345             offset += 16;
346       }
347 
348       print_instruction(output, compacted, insn);
349    }
350 
351    ralloc_free(disasm_info);
352 
353    if (output_type == OPT_OUTPUT_C_LITERAL)
354       fprintf(output, "}");
355 
356    result = EXIT_SUCCESS;
357    goto end;
358 
359 end:
360    free(input_filename);
361    free(output_file);
362 
363    if (yyin)
364       fclose(yyin);
365 
366    if (output)
367       fclose(output);
368 
369    if (p)
370       ralloc_free(p);
371 
372    if (devinfo)
373       free(devinfo);
374 
375    exit(result);
376 }
377