summaryrefslogtreecommitdiff
path: root/backend/src/backend/gen_insn_selection_output.cpp
blob: f23e8c8d42d3c34d7146aba111816fbce8b7ad28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "backend/gen_insn_selection.hpp"
#include "backend/gen_insn_selection_output.hpp"
#include "sys/cvar.hpp"
#include "sys/intrusive_list.hpp"
#include <string.h>
#include <iostream>
#include <iomanip>
using namespace std;

namespace gbe
{
  static void outputGenReg(GenRegister& reg, bool dst)
  {
    if (reg.file == GEN_IMMEDIATE_VALUE || reg.file == GEN_GENERAL_REGISTER_FILE) {
      if (reg.file == GEN_IMMEDIATE_VALUE) {
        switch (reg.type) {
          case GEN_TYPE_UD:
          case GEN_TYPE_UW:
          case GEN_TYPE_UB:
          case GEN_TYPE_HF_IMM:
            cout << hex << "0x" << reg.value.ud  << dec;
            break;
          case GEN_TYPE_D:
          case GEN_TYPE_W:
          case GEN_TYPE_B:
            cout << reg.value.d;
            break;
          case GEN_TYPE_V:
            cout << hex << "0x" << reg.value.ud << dec;
            break;
          case GEN_TYPE_UL:
            cout << reg.value.u64;
            break;
          case GEN_TYPE_L:
            cout << reg.value.i64;
            break;
          case GEN_TYPE_F:
            cout << reg.value.f;
            break;
        }
      }else {
        if (reg.negation)
          cout << "-";
        if (reg.absolute)
          cout << "(abs)";
        cout << "%" << reg.value.reg;
        if (reg.subphysical)
          cout << "." << reg.subnr + reg.nr * GEN_REG_SIZE;

        if (dst)
          cout << "<" << GenRegister::hstride_size(reg) << ">";
        else
          cout << "<" << GenRegister::vstride_size(reg) << "," << GenRegister::width_size(reg) << "," << GenRegister::hstride_size(reg) << ">";
      }

      cout << ":";
      switch (reg.type) {
        case GEN_TYPE_UD:
          cout << "UD";
          break;
        case GEN_TYPE_UW:
          cout << "UW";
          break;
        case GEN_TYPE_UB:
          cout << "UB";
          break;
        case GEN_TYPE_HF_IMM:
          cout << "HF";
          break;
        case GEN_TYPE_D:
          cout << "D";
          break;
        case GEN_TYPE_W:
          cout << "W";
          break;
        case GEN_TYPE_B:
          cout << "B";
          break;
        case GEN_TYPE_V:
          cout << "V";
          break;
        case GEN_TYPE_UL:
          cout << "UL";
          break;
        case GEN_TYPE_L:
          cout << "L";
          break;
        case GEN_TYPE_F:
          cout << "F";
          break;
      }
    } else if (reg.file == GEN_ARCHITECTURE_REGISTER_FILE) {
      cout << setw(8) << "arf";
    } else
      assert(!"should not reach here");
  }

#define OP_NAME_LENGTH 512
  void outputSelectionInst(SelectionInstruction &insn) {
    cout<<"["<<insn.ID<<"]";
    if (insn.state.predicate != GEN_PREDICATE_NONE) {
      if (insn.state.physicalFlag == 0)
        cout << "(f" << insn.state.flagIndex << ")\t";
      else
        cout << "(f" << insn.state.flag << "." << insn.state.subFlag << ")\t";
    }
    else
      cout << "    \t";

    char opname[OP_NAME_LENGTH];
    if (insn.isLabel()) {
        cout << "  L" << insn.index << ":" << endl;
        return;
    } else {
      switch (insn.opcode) {
        #define DECL_SELECTION_IR(OP, FAMILY) case SEL_OP_##OP: sprintf(opname, "%s", #OP); break;
        #include "backend/gen_insn_selection.hxx"
        #undef DECL_SELECTION_IR
      }
    }

    if (insn.opcode == SEL_OP_CMP) {
      switch (insn.extra.function) {
        case GEN_CONDITIONAL_LE:
          strcat(opname, ".le");
          break;
        case GEN_CONDITIONAL_L:
          strcat(opname, ".l");
          break;
        case GEN_CONDITIONAL_GE:
          strcat(opname, ".ge");
          break;
        case GEN_CONDITIONAL_G:
          strcat(opname, ".g");
          break;
        case GEN_CONDITIONAL_EQ:
          strcat(opname, ".eq");
          break;
        case GEN_CONDITIONAL_NEQ:
          strcat(opname, ".neq");
          break;
      }
    }

    int n = strlen(opname);
    if(n >= OP_NAME_LENGTH - 20) {
      cout << "opname too long: " << opname << endl;
      return;
    }

    sprintf(&opname[n], "(%d)", insn.state.execWidth);
    cout << left << setw(20) << opname;

    for (int i = 0; i < insn.dstNum; ++i)
    {
      GenRegister dst = insn.dst(i);
      outputGenReg(dst, true);
      cout << "\t";
    }

    cout << ":\t";

    for (int i = 0; i < insn.srcNum; ++i)
    {
      GenRegister src = insn.src(i);
      outputGenReg(src, false);
      cout << "\t";
    }

    cout << endl;
  }

  void outputSelectionIR(GenContext &ctx, Selection* sel, const char* KernelName)
  {
    cout << KernelName <<"'s SELECTION IR begin:" << endl;
    cout << "WARNING: not completed yet, welcome for the FIX!" << endl;
    for (SelectionBlock &block : *sel->blockList) {
      for (SelectionInstruction &insn : block.insnList) {
        outputSelectionInst(insn);
      }
      cout << endl;
    }
    cout <<KernelName << "'s SELECTION IR end." << endl << endl;
  }

}