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
29 enum opt_output_type
{
36 struct brw_codegen
*p
;
37 static enum opt_output_type output_type
= OPT_OUTPUT_BIN
;
38 char *input_filename
= NULL
;
42 print_help(const char *progname
, FILE *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"
55 " i965_asm -g kbl input.asm -t hex -o output\n",
60 print_instruction(FILE *output
, bool compact
, const brw_inst
*instruction
)
64 byte_limit
= (compact
== true) ? 8 : 16;
66 switch (output_type
) {
67 case OPT_OUTPUT_HEX
: {
68 fprintf(output
, "%02x", ((unsigned char *)instruction
)[0]);
70 for (unsigned i
= 1; i
< byte_limit
; i
++) {
71 fprintf(output
, " %02x", ((unsigned char *)instruction
)[i
]);
75 case OPT_OUTPUT_C_LITERAL
: {
76 fprintf(output
, "\t0x%02x,", ((unsigned char *)instruction
)[0]);
78 for (unsigned i
= 1; i
< byte_limit
; i
++) {
79 fprintf(output
, " 0x%02x,", ((unsigned char *)instruction
)[i
]);
84 fwrite(instruction
, 1, byte_limit
, output
);
88 if (output_type
!= OPT_OUTPUT_BIN
) {
89 fprintf(output
, "\n");
93 static struct gen_device_info
*
94 i965_disasm_init(uint16_t pci_id
)
96 struct gen_device_info
*devinfo
;
98 devinfo
= malloc(sizeof *devinfo
);
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",
109 brw_init_compaction_tables(devinfo
);
114 int main(int argc
, char **argv
)
116 char *output_file
= NULL
;
118 FILE *output
= stdout
;
119 bool help
= false, compact
= false;
123 int start_offset
= 0;
124 struct disasm_info
*disasm_info
;
125 struct gen_device_info
*devinfo
= NULL
;
126 int result
= EXIT_FAILURE
;
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 },
137 while ((c
= getopt_long(argc
, argv
, ":t:g:o:h", i965_asm_opts
, NULL
)) != -1) {
140 const int id
= gen_device_name_to_pci_device_id(optarg
);
142 fprintf(stderr
, "can't parse gen: '%s', expected 3 letter "
143 "platform name\n", optarg
);
152 print_help(argv
[0], stderr
);
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
;
162 fprintf(stderr
, "invalid value for --type: %s\n", optarg
);
168 output_file
= strdup(optarg
);
173 fprintf(stderr
, "%s: option `-%c' requires an argument\n",
178 fprintf(stderr
, "%s: option `-%c' is invalid: ignored\n",
184 if (help
|| !pci_id
) {
185 print_help(argv
[0], stderr
);
190 fprintf(stderr
, "Please specify input file\n");
194 input_filename
= strdup(argv
[optind
]);
195 yyin
= fopen(input_filename
, "r");
197 fprintf(stderr
, "Unable to read input file : %s\n",
203 output
= fopen(output_file
, "w");
205 fprintf(stderr
, "Couldn't open output file\n");
210 devinfo
= i965_disasm_init(pci_id
);
212 fprintf(stderr
, "Unable to allocate memory for "
213 "gen_device_info struct instance.\n");
217 p
= rzalloc(NULL
, struct brw_codegen
);
218 brw_init_codegen(devinfo
, p
, p
);
219 p
->automatic_exec_sizes
= false;
227 disasm_info
= disasm_initialize(p
->devinfo
, NULL
);
229 fprintf(stderr
, "Unable to initialize disasm_info struct instance\n");
233 if (output_type
== OPT_OUTPUT_C_LITERAL
)
234 fprintf(output
, "static const char gen_eu_bytes[] = {\n");
236 brw_validate_instructions(p
->devinfo
, p
->store
, 0,
237 p
->next_insn_offset
, disasm_info
);
239 const int nr_insn
= (p
->next_insn_offset
- start_offset
) / 16;
242 brw_compact_instructions(p
, start_offset
, disasm_info
);
244 for (int i
= 0; i
< nr_insn
; i
++) {
245 const brw_inst
*insn
= store
+ offset
;
246 bool compacted
= false;
248 if (compact
&& brw_inst_cmpt_control(p
->devinfo
, insn
)) {
255 print_instruction(output
, compacted
, insn
);
258 ralloc_free(disasm_info
);
260 if (output_type
== OPT_OUTPUT_C_LITERAL
)
261 fprintf(output
, "}");
263 result
= EXIT_SUCCESS
;
267 free(input_filename
);