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