aboutsummaryrefslogtreecommitdiff
path: root/lib/MC/MCDisassembler/EDInst.cpp
blob: 6057e169e3474b20a3151d177d94ab0c85dc450e (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//===-EDInst.cpp - LLVM Enhanced Disassembler -----------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
// 
//===----------------------------------------------------------------------===//
//
// This file implements the Enhanced Disassembly library's instruction class.
// The instruction is responsible for vending the string representation, 
// individual tokens, and operands for a single instruction.
//
//===----------------------------------------------------------------------===//

#include "EDInst.h"
#include "EDDisassembler.h"
#include "EDOperand.h"
#include "EDToken.h"

#include "llvm/MC/EDInstInfo.h"
#include "llvm/MC/MCInst.h"

using namespace llvm;

EDInst::EDInst(llvm::MCInst *inst,
               uint64_t byteSize, 
               EDDisassembler &disassembler,
               const llvm::EDInstInfo *info) :
  Disassembler(disassembler),
  Inst(inst),
  ThisInstInfo(info),
  ByteSize(byteSize),
  BranchTarget(-1),
  MoveSource(-1),
  MoveTarget(-1) {
  OperandOrder = ThisInstInfo->operandOrders[Disassembler.llvmSyntaxVariant()];
}

EDInst::~EDInst() {
  unsigned int index;
  unsigned int numOperands = Operands.size();
  
  for (index = 0; index < numOperands; ++index)
    delete Operands[index];
  
  unsigned int numTokens = Tokens.size();
  
  for (index = 0; index < numTokens; ++index)
    delete Tokens[index];
  
  delete Inst;
}

uint64_t EDInst::byteSize() {
  return ByteSize;
}

int EDInst::stringify() {
  if (StringifyResult.valid())
    return StringifyResult.result();
  
  if (Disassembler.printInst(String, *Inst))
    return StringifyResult.setResult(-1);

  String.push_back('\n');
  
  return StringifyResult.setResult(0);
}

int EDInst::getString(const char*& str) {
  if (stringify())
    return -1;
  
  str = String.c_str();
  
  return 0;
}

unsigned EDInst::instID() {
  return Inst->getOpcode();
}

bool EDInst::isBranch() {
  if (ThisInstInfo)
    return 
      ThisInstInfo->instructionType == kInstructionTypeBranch ||
      ThisInstInfo->instructionType == kInstructionTypeCall;
  else
    return false;
}

bool EDInst::isMove() {
  if (ThisInstInfo)
    return ThisInstInfo->instructionType == kInstructionTypeMove;
  else
    return false;
}

int EDInst::parseOperands() {
  if (ParseResult.valid())
    return ParseResult.result();
  
  if (!ThisInstInfo)
    return ParseResult.setResult(-1);
  
  unsigned int opIndex;
  unsigned int mcOpIndex = 0;
  
  for (opIndex = 0; opIndex < ThisInstInfo->numOperands; ++opIndex) {
    if (isBranch() &&
        (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)) {
      BranchTarget = opIndex;
    }
    else if (isMove()) {
      if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagSource)
        MoveSource = opIndex;
      else if (ThisInstInfo->operandFlags[opIndex] & kOperandFlagTarget)
        MoveTarget = opIndex;
    }
    
    EDOperand *operand = new EDOperand(Disassembler, *this, opIndex, mcOpIndex);
    
    Operands.push_back(operand);
  }
  
  return ParseResult.setResult(0);
}

int EDInst::branchTargetID() {
  if (parseOperands())
    return -1;
  return BranchTarget;
}

int EDInst::moveSourceID() {
  if (parseOperands())
    return -1;
  return MoveSource;
}

int EDInst::moveTargetID() {
  if (parseOperands())
    return -1;
  return MoveTarget;
}

int EDInst::numOperands() {
  if (parseOperands())
    return -1;
  return Operands.size();
}

int EDInst::getOperand(EDOperand *&operand, unsigned int index) {
  if (parseOperands())
    return -1;
  
  if (index >= Operands.size())
    return -1;
  
  operand = Operands[index];
  return 0;
}

int EDInst::tokenize() {
  if (TokenizeResult.valid())
    return TokenizeResult.result();
    
  if (ThisInstInfo == NULL)
    return TokenizeResult.setResult(-1);
  
  if (stringify())
    return TokenizeResult.setResult(-1);
    
  return TokenizeResult.setResult(EDToken::tokenize(Tokens,
                                                    String,
                                                    OperandOrder,
                                                    Disassembler));
    
}

int EDInst::numTokens() {
  if (tokenize())
    return -1;
  return Tokens.size();
}

int EDInst::getToken(EDToken *&token, unsigned int index) {
  if (tokenize())
    return -1;
  token = Tokens[index];
  return 0;
}

#ifdef __BLOCKS__
int EDInst::visitTokens(EDTokenVisitor_t visitor) {
  if (tokenize())
    return -1;
  
  tokvec_t::iterator iter;
  
  for (iter = Tokens.begin(); iter != Tokens.end(); ++iter) {
    int ret = visitor(*iter);
    if (ret == 1)
      return 0;
    if (ret != 0)
      return -1;
  }
  
  return 0;
}
#endif