intel/tools: Refactor gen_disasm_disassemble() to use annotations
[mesa.git] / src / intel / tools / 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, void *assembly, int start)
48 {
49 struct gen_device_info *devinfo = &disasm->devinfo;
50 int offset = start;
51
52 /* This loop exits when send-with-EOT or when opcode is 0 */
53 while (true) {
54 brw_inst *insn = assembly + offset;
55
56 if (brw_inst_cmpt_control(devinfo, insn)) {
57 offset += 8;
58 } else {
59 offset += 16;
60 }
61
62 /* Simplistic, but efficient way to terminate disasm */
63 uint32_t opcode = brw_inst_opcode(devinfo, insn);
64 if (opcode == 0 || (is_send(opcode) && brw_inst_eot(devinfo, insn))) {
65 break;
66 }
67 }
68
69 return offset;
70 }
71
72 void
73 gen_disasm_disassemble(struct gen_disasm *disasm, void *assembly,
74 int start, FILE *out)
75 {
76 struct gen_device_info *devinfo = &disasm->devinfo;
77 int end = gen_disasm_find_end(disasm, assembly, start);
78
79 /* Make a dummy annotation structure that brw_validate_instructions
80 * can work from.
81 */
82 struct annotation_info annotation_info = {
83 .ann_count = 1,
84 .ann_size = 2,
85 };
86 annotation_info.mem_ctx = ralloc_context(NULL);
87 annotation_info.ann = rzalloc_array(annotation_info.mem_ctx,
88 struct annotation,
89 annotation_info.ann_size);
90 annotation_info.ann[0].offset = start;
91 annotation_info.ann[1].offset = end;
92 brw_validate_instructions(devinfo, assembly, start, end, &annotation_info);
93 struct annotation *annotation = annotation_info.ann;
94
95 for (int i = 0; i < annotation_info.ann_count; i++) {
96 int start_offset = annotation[i].offset;
97 int end_offset = annotation[i + 1].offset;
98
99 brw_disassemble(devinfo, assembly, start_offset, end_offset, stdout);
100
101 if (annotation[i].error) {
102 fputs(annotation[i].error, stdout);
103 }
104 }
105
106 ralloc_free(annotation_info.mem_ctx);
107 }
108
109 struct gen_disasm *
110 gen_disasm_create(int pciid)
111 {
112 struct gen_disasm *gd;
113
114 gd = malloc(sizeof *gd);
115 if (gd == NULL)
116 return NULL;
117
118 if (!gen_get_device_info(pciid, &gd->devinfo)) {
119 free(gd);
120 return NULL;
121 }
122
123 brw_init_compaction_tables(&gd->devinfo);
124
125 return gd;
126 }
127
128 void
129 gen_disasm_destroy(struct gen_disasm *disasm)
130 {
131 free(disasm);
132 }