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 "brw_context.h"
27 #include "brw_inst.h"
28 #include "brw_eu.h"
29 
30 #include "gen_disasm.h"
31 
32 uint64_t INTEL_DEBUG;
33 
34 struct gen_disasm {
35     struct gen_device_info devinfo;
36 };
37 
38 static bool
is_send(uint32_t opcode)39 is_send(uint32_t opcode)
40 {
41    return (opcode == BRW_OPCODE_SEND  ||
42            opcode == BRW_OPCODE_SENDC ||
43            opcode == BRW_OPCODE_SENDS ||
44            opcode == BRW_OPCODE_SENDSC );
45 }
46 
47 void
gen_disasm_disassemble(struct gen_disasm * disasm,void * assembly,int start,FILE * out)48 gen_disasm_disassemble(struct gen_disasm *disasm, void *assembly,
49                        int start, FILE *out)
50 {
51    struct gen_device_info *devinfo = &disasm->devinfo;
52    bool dump_hex = false;
53    int offset = start;
54 
55    /* This loop exits when send-with-EOT or when opcode is 0 */
56    while (true) {
57       brw_inst *insn = assembly + offset;
58       brw_inst uncompacted;
59       bool compacted = brw_inst_cmpt_control(devinfo, insn);
60       if (0)
61          fprintf(out, "0x%08x: ", offset);
62 
63       if (compacted) {
64          brw_compact_inst *compacted = (void *)insn;
65          if (dump_hex) {
66             fprintf(out, "0x%08x 0x%08x                       ",
67                    ((uint32_t *)insn)[1],
68                    ((uint32_t *)insn)[0]);
69          }
70 
71          brw_uncompact_instruction(devinfo, &uncompacted, compacted);
72          insn = &uncompacted;
73          offset += 8;
74       } else {
75          if (dump_hex) {
76             fprintf(out, "0x%08x 0x%08x 0x%08x 0x%08x ",
77                    ((uint32_t *)insn)[3],
78                    ((uint32_t *)insn)[2],
79                    ((uint32_t *)insn)[1],
80                    ((uint32_t *)insn)[0]);
81          }
82          offset += 16;
83       }
84 
85       brw_disassemble_inst(out, devinfo, insn, compacted);
86 
87       /* Simplistic, but efficient way to terminate disasm */
88       uint32_t opcode = brw_inst_opcode(devinfo, insn);
89       if (opcode == 0 || (is_send(opcode) && brw_inst_eot(devinfo, insn))) {
90          break;
91       }
92    }
93 }
94 
95 struct gen_disasm *
gen_disasm_create(int pciid)96 gen_disasm_create(int pciid)
97 {
98    struct gen_disasm *gd;
99 
100    gd = malloc(sizeof *gd);
101    if (gd == NULL)
102       return NULL;
103 
104    if (!gen_get_device_info(pciid, &gd->devinfo)) {
105       free(gd);
106       return NULL;
107    }
108 
109    brw_init_compaction_tables(&gd->devinfo);
110 
111    return gd;
112 }
113 
114 void
gen_disasm_destroy(struct gen_disasm * disasm)115 gen_disasm_destroy(struct gen_disasm *disasm)
116 {
117    free(disasm);
118 }
119