1 /*
2 * Copyright © 2014 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25
26 #include "compiler/brw_inst.h"
27 #include "compiler/brw_eu.h"
28
29 #include "gen_disasm.h"
30
31 struct gen_disasm {
32 struct gen_device_info devinfo;
33 };
34
35 static bool
is_send(uint32_t opcode)36 is_send(uint32_t opcode)
37 {
38 return (opcode == BRW_OPCODE_SEND ||
39 opcode == BRW_OPCODE_SENDC ||
40 opcode == BRW_OPCODE_SENDS ||
41 opcode == BRW_OPCODE_SENDSC );
42 }
43
44 static int
gen_disasm_find_end(struct gen_disasm * disasm,const void * assembly,int start)45 gen_disasm_find_end(struct gen_disasm *disasm,
46 const void *assembly, int start)
47 {
48 struct gen_device_info *devinfo = &disasm->devinfo;
49 int offset = start;
50
51 /* This loop exits when send-with-EOT or when opcode is 0 */
52 while (true) {
53 const brw_inst *insn = assembly + offset;
54
55 if (brw_inst_cmpt_control(devinfo, insn)) {
56 offset += 8;
57 } else {
58 offset += 16;
59 }
60
61 /* Simplistic, but efficient way to terminate disasm */
62 uint32_t opcode = brw_inst_opcode(devinfo, insn);
63 if (opcode == 0 || (is_send(opcode) && brw_inst_eot(devinfo, insn))) {
64 break;
65 }
66 }
67
68 return offset;
69 }
70
71 void
gen_disasm_disassemble(struct gen_disasm * disasm,const void * assembly,int start,FILE * out)72 gen_disasm_disassemble(struct gen_disasm *disasm, const void *assembly,
73 int start, FILE *out)
74 {
75 struct gen_device_info *devinfo = &disasm->devinfo;
76 int end = gen_disasm_find_end(disasm, assembly, start);
77
78 /* Make a dummy disasm structure that brw_validate_instructions
79 * can work from.
80 */
81 struct disasm_info *disasm_info = disasm_initialize(devinfo, NULL);
82 disasm_new_inst_group(disasm_info, start);
83 disasm_new_inst_group(disasm_info, end);
84
85 brw_validate_instructions(devinfo, assembly, start, end, disasm_info);
86
87 foreach_list_typed(struct inst_group, group, link,
88 &disasm_info->group_list) {
89 struct exec_node *next_node = exec_node_get_next(&group->link);
90 if (exec_node_is_tail_sentinel(next_node))
91 break;
92
93 struct inst_group *next =
94 exec_node_data(struct inst_group, next_node, link);
95
96 int start_offset = group->offset;
97 int end_offset = next->offset;
98
99 brw_disassemble(devinfo, assembly, start_offset, end_offset, out);
100
101 if (group->error) {
102 fputs(group->error, out);
103 }
104 }
105
106 ralloc_free(disasm_info);
107 }
108
109 struct gen_disasm *
gen_disasm_create(const struct gen_device_info * devinfo)110 gen_disasm_create(const struct gen_device_info *devinfo)
111 {
112 struct gen_disasm *gd;
113
114 gd = malloc(sizeof *gd);
115 if (gd == NULL)
116 return NULL;
117
118 gd->devinfo = *devinfo;
119
120 brw_init_compaction_tables(&gd->devinfo);
121
122 return gd;
123 }
124
125 void
gen_disasm_destroy(struct gen_disasm * disasm)126 gen_disasm_destroy(struct gen_disasm *disasm)
127 {
128 free(disasm);
129 }
130