1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdlib.h>
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ci#include "compiler/brw_inst.h"
27bf215546Sopenharmony_ci#include "compiler/brw_eu.h"
28bf215546Sopenharmony_ci#include "compiler/brw_isa_info.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "intel_disasm.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistatic bool
33bf215546Sopenharmony_ciis_send(uint32_t opcode)
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci   return (opcode == BRW_OPCODE_SEND  ||
36bf215546Sopenharmony_ci           opcode == BRW_OPCODE_SENDC ||
37bf215546Sopenharmony_ci           opcode == BRW_OPCODE_SENDS ||
38bf215546Sopenharmony_ci           opcode == BRW_OPCODE_SENDSC );
39bf215546Sopenharmony_ci}
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_cistatic int
42bf215546Sopenharmony_ciintel_disasm_find_end(const struct brw_isa_info *isa,
43bf215546Sopenharmony_ci                      const void *assembly, int start)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = isa->devinfo;
46bf215546Sopenharmony_ci   int offset = start;
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_ci   /* This loop exits when send-with-EOT or when opcode is 0 */
49bf215546Sopenharmony_ci   while (true) {
50bf215546Sopenharmony_ci      const brw_inst *insn = assembly + offset;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci      if (brw_inst_cmpt_control(devinfo, insn)) {
53bf215546Sopenharmony_ci         offset += 8;
54bf215546Sopenharmony_ci      } else {
55bf215546Sopenharmony_ci         offset += 16;
56bf215546Sopenharmony_ci      }
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci      /* Simplistic, but efficient way to terminate disasm */
59bf215546Sopenharmony_ci      uint32_t opcode = brw_inst_opcode(isa, insn);
60bf215546Sopenharmony_ci      if (opcode == 0 || (is_send(opcode) && brw_inst_eot(devinfo, insn))) {
61bf215546Sopenharmony_ci         break;
62bf215546Sopenharmony_ci      }
63bf215546Sopenharmony_ci   }
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   return offset;
66bf215546Sopenharmony_ci}
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_civoid
69bf215546Sopenharmony_ciintel_disassemble(const struct brw_isa_info *isa,
70bf215546Sopenharmony_ci                  const void *assembly, int start, FILE *out)
71bf215546Sopenharmony_ci{
72bf215546Sopenharmony_ci   int end = intel_disasm_find_end(isa, assembly, start);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /* Make a dummy disasm structure that brw_validate_instructions
75bf215546Sopenharmony_ci    * can work from.
76bf215546Sopenharmony_ci    */
77bf215546Sopenharmony_ci   struct disasm_info *disasm_info = disasm_initialize(isa, NULL);
78bf215546Sopenharmony_ci   disasm_new_inst_group(disasm_info, start);
79bf215546Sopenharmony_ci   disasm_new_inst_group(disasm_info, end);
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci   brw_validate_instructions(isa, assembly, start, end, disasm_info);
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci   void *mem_ctx = ralloc_context(NULL);
84bf215546Sopenharmony_ci   const struct brw_label *root_label =
85bf215546Sopenharmony_ci      brw_label_assembly(isa, assembly, start, end, mem_ctx);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   foreach_list_typed(struct inst_group, group, link,
88bf215546Sopenharmony_ci                      &disasm_info->group_list) {
89bf215546Sopenharmony_ci      struct exec_node *next_node = exec_node_get_next(&group->link);
90bf215546Sopenharmony_ci      if (exec_node_is_tail_sentinel(next_node))
91bf215546Sopenharmony_ci         break;
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci      struct inst_group *next =
94bf215546Sopenharmony_ci         exec_node_data(struct inst_group, next_node, link);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci      int start_offset = group->offset;
97bf215546Sopenharmony_ci      int end_offset = next->offset;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci      brw_disassemble(isa, assembly, start_offset, end_offset,
100bf215546Sopenharmony_ci                      root_label, out);
101bf215546Sopenharmony_ci
102bf215546Sopenharmony_ci      if (group->error) {
103bf215546Sopenharmony_ci         fputs(group->error, out);
104bf215546Sopenharmony_ci      }
105bf215546Sopenharmony_ci   }
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   ralloc_free(mem_ctx);
108bf215546Sopenharmony_ci   ralloc_free(disasm_info);
109bf215546Sopenharmony_ci}
110