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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 struct brw_codegen
*p
;
31 static int c_literal_output
= 0;
32 char *input_filename
= NULL
;
36 print_help(const char *progname
, FILE *file
)
39 "Usage: %s [OPTION] inputfile\n"
40 "Assemble i965 instructions from input file.\n\n"
41 " -h, --help display this help and exit\n"
42 " -l, --c-literal C literal\n"
43 " -o, --output specify output file\n"
44 " --compact print compacted instructions\n"
45 " -g, --gen=platform assemble instructions for given \n"
46 " platform (3 letter platform name)\n"
48 " i965_asm -g kbl input.asm -o output\n",
53 print_instruction(FILE *output
, bool compact
, const brw_inst
*instruction
)
57 byte_limit
= (compact
== true) ? 8 : 16;
59 if (c_literal_output
) {
60 fprintf(output
, "\t0x%02x,", ((unsigned char *)instruction
)[0]);
62 for (unsigned i
= 1; i
< byte_limit
; i
++)
63 fprintf(output
, " 0x%02x,", ((unsigned char *)instruction
)[i
]);
65 fprintf(output
, "%02x", ((unsigned char *)instruction
)[0]);
67 for (unsigned i
= 1; i
< byte_limit
; i
++)
68 fprintf(output
, " %02x", ((unsigned char *)instruction
)[i
]);
70 fprintf(output
, "\n");
73 static struct gen_device_info
*
74 i965_disasm_init(uint16_t pci_id
)
76 struct gen_device_info
*devinfo
;
78 devinfo
= malloc(sizeof *devinfo
);
82 if (!gen_get_device_info_from_pci_id(pci_id
, devinfo
)) {
83 fprintf(stderr
, "can't find device information: pci_id=0x%x\n",
89 brw_init_compaction_tables(devinfo
);
94 int main(int argc
, char **argv
)
96 char *output_file
= NULL
;
98 FILE *output
= stdout
;
99 bool help
= false, compact
= false;
103 int start_offset
= 0;
104 struct disasm_info
*disasm_info
;
105 struct gen_device_info
*devinfo
= NULL
;
106 int result
= EXIT_FAILURE
;
108 const struct option i965_asm_opts
[] = {
109 { "help", no_argument
, (int *) &help
, true },
110 { "c-literal", no_argument
, NULL
, 'c' },
111 { "gen", required_argument
, NULL
, 'g' },
112 { "output", required_argument
, NULL
, 'o' },
113 { "compact", no_argument
, (int *) &compact
, true },
117 while ((c
= getopt_long(argc
, argv
, ":g:o:lh", i965_asm_opts
, NULL
)) != -1) {
120 const int id
= gen_device_name_to_pci_device_id(optarg
);
122 fprintf(stderr
, "can't parse gen: '%s', expected 3 letter "
123 "platform name\n", optarg
);
132 print_help(argv
[0], stderr
);
135 c_literal_output
= 1;
138 output_file
= strdup(optarg
);
143 fprintf(stderr
, "%s: option `-%c' requires an argument\n",
148 fprintf(stderr
, "%s: option `-%c' is invalid: ignored\n",
154 if (help
|| !pci_id
) {
155 print_help(argv
[0], stderr
);
160 fprintf(stderr
, "Please specify input file\n");
164 input_filename
= strdup(argv
[optind
]);
165 yyin
= fopen(input_filename
, "r");
167 fprintf(stderr
, "Unable to read input file : %s\n",
173 output
= fopen(output_file
, "w");
175 fprintf(stderr
, "Couldn't open output file\n");
180 devinfo
= i965_disasm_init(pci_id
);
182 fprintf(stderr
, "Unable to allocate memory for "
183 "gen_device_info struct instance.\n");
187 p
= rzalloc(NULL
, struct brw_codegen
);
188 brw_init_codegen(devinfo
, p
, p
);
189 p
->automatic_exec_sizes
= false;
197 disasm_info
= disasm_initialize(p
->devinfo
, NULL
);
199 fprintf(stderr
, "Unable to initialize disasm_info struct instance\n");
203 if (c_literal_output
)
204 fprintf(output
, "static const char gen_eu_bytes[] = {\n");
206 brw_validate_instructions(p
->devinfo
, p
->store
, 0,
207 p
->next_insn_offset
, disasm_info
);
209 const int nr_insn
= (p
->next_insn_offset
- start_offset
) / 16;
212 brw_compact_instructions(p
, start_offset
, disasm_info
);
214 for (int i
= 0; i
< nr_insn
; i
++) {
215 const brw_inst
*insn
= store
+ offset
;
216 bool compacted
= false;
218 if (compact
&& brw_inst_cmpt_control(p
->devinfo
, insn
)) {
225 print_instruction(output
, compacted
, insn
);
228 ralloc_free(disasm_info
);
230 if (c_literal_output
)
231 fprintf(output
, "}");
233 result
= EXIT_SUCCESS
;
237 free(input_filename
);