aboutsummaryrefslogtreecommitdiff
path: root/lib/MC/MCDisassembler/EDMain.cpp
blob: c658717b02490090081a0e8d964e7c0125534d47 (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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//===-- EDMain.cpp - LLVM Enhanced Disassembly C API ----------------------===//
//
//                     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 disassembler's public C API.
//
//===----------------------------------------------------------------------===//

#include "EDDisassembler.h"
#include "EDInst.h"
#include "EDOperand.h"
#include "EDToken.h"
#include "llvm-c/EnhancedDisassembly.h"
using namespace llvm;

int EDGetDisassembler(EDDisassemblerRef *disassembler,
                      const char *triple,
                      EDAssemblySyntax_t syntax) {
  EDDisassembler::AssemblySyntax Syntax;
  switch (syntax) {
  default: llvm_unreachable("Unknown assembly syntax!");
  case kEDAssemblySyntaxX86Intel:
    Syntax = EDDisassembler::kEDAssemblySyntaxX86Intel;
    break;
  case kEDAssemblySyntaxX86ATT:
    Syntax = EDDisassembler::kEDAssemblySyntaxX86ATT;
    break;
  case kEDAssemblySyntaxARMUAL:
    Syntax = EDDisassembler::kEDAssemblySyntaxARMUAL;
    break;
  }
  
  EDDisassemblerRef ret = EDDisassembler::getDisassembler(triple, Syntax);
  
  if (!ret)
    return -1;
  *disassembler = ret;
  return 0;
}

int EDGetRegisterName(const char** regName,
                      EDDisassemblerRef disassembler,
                      unsigned regID) {
  const char *name = ((EDDisassembler*)disassembler)->nameWithRegisterID(regID);
  if (!name)
    return -1;
  *regName = name;
  return 0;
}

int EDRegisterIsStackPointer(EDDisassemblerRef disassembler,
                             unsigned regID) {
  return ((EDDisassembler*)disassembler)->registerIsStackPointer(regID) ? 1 : 0;
}

int EDRegisterIsProgramCounter(EDDisassemblerRef disassembler,
                               unsigned regID) {
  return ((EDDisassembler*)disassembler)->registerIsProgramCounter(regID) ? 1:0;
}

unsigned int EDCreateInsts(EDInstRef *insts,
                           unsigned int count,
                           EDDisassemblerRef disassembler,
                           ::EDByteReaderCallback byteReader,
                           uint64_t address,
                           void *arg) {
  unsigned int index;
  
  for (index = 0; index < count; ++index) {
    EDInst *inst = ((EDDisassembler*)disassembler)->createInst(byteReader,
                                                               address, arg);
    
    if (!inst)
      return index;
    
    insts[index] = inst;
    address += inst->byteSize();
  }
  
  return count;
}

void EDReleaseInst(EDInstRef inst) {
  delete ((EDInst*)inst);
}

int EDInstByteSize(EDInstRef inst) {
  return ((EDInst*)inst)->byteSize();
}

int EDGetInstString(const char **buf,
                    EDInstRef inst) {
  return ((EDInst*)inst)->getString(*buf);
}

int EDInstID(unsigned *instID, EDInstRef inst) {
  *instID = ((EDInst*)inst)->instID();
  return 0;
}

int EDInstIsBranch(EDInstRef inst) {
  return ((EDInst*)inst)->isBranch();
}

int EDInstIsMove(EDInstRef inst) {
  return ((EDInst*)inst)->isMove();
}

int EDBranchTargetID(EDInstRef inst) {
  return ((EDInst*)inst)->branchTargetID();
}

int EDMoveSourceID(EDInstRef inst) {
  return ((EDInst*)inst)->moveSourceID();
}

int EDMoveTargetID(EDInstRef inst) {
  return ((EDInst*)inst)->moveTargetID();
}

int EDNumTokens(EDInstRef inst) {
  return ((EDInst*)inst)->numTokens();
}

int EDGetToken(EDTokenRef *token,
               EDInstRef inst,
               int index) {
  return ((EDInst*)inst)->getToken(*(EDToken**)token, index);
}

int EDGetTokenString(const char **buf,
                     EDTokenRef token) {
  return ((EDToken*)token)->getString(*buf);
}

int EDOperandIndexForToken(EDTokenRef token) {
  return ((EDToken*)token)->operandID();
}

int EDTokenIsWhitespace(EDTokenRef token) {
  return ((EDToken*)token)->type() == EDToken::kTokenWhitespace;
}

int EDTokenIsPunctuation(EDTokenRef token) {
  return ((EDToken*)token)->type() == EDToken::kTokenPunctuation;
}

int EDTokenIsOpcode(EDTokenRef token) {
  return ((EDToken*)token)->type() == EDToken::kTokenOpcode;
}

int EDTokenIsLiteral(EDTokenRef token) {
  return ((EDToken*)token)->type() == EDToken::kTokenLiteral;
}

int EDTokenIsRegister(EDTokenRef token) {
  return ((EDToken*)token)->type() == EDToken::kTokenRegister;
}

int EDTokenIsNegativeLiteral(EDTokenRef token) {
  if (((EDToken*)token)->type() != EDToken::kTokenLiteral)
    return -1;
  
  return ((EDToken*)token)->literalSign();
}

int EDLiteralTokenAbsoluteValue(uint64_t *value, EDTokenRef token) {
  if (((EDToken*)token)->type() != EDToken::kTokenLiteral)
    return -1;
  
  return ((EDToken*)token)->literalAbsoluteValue(*value);
}

int EDRegisterTokenValue(unsigned *registerID,
                         EDTokenRef token) {
  if (((EDToken*)token)->type() != EDToken::kTokenRegister)
    return -1;
  
  return ((EDToken*)token)->registerID(*registerID);
}

int EDNumOperands(EDInstRef inst) {
  return ((EDInst*)inst)->numOperands();
}

int EDGetOperand(EDOperandRef *operand,
                 EDInstRef inst,
                 int index) {
  return ((EDInst*)inst)->getOperand(*(EDOperand**)operand, index);
}

int EDOperandIsRegister(EDOperandRef operand) {
  return ((EDOperand*)operand)->isRegister();
}

int EDOperandIsImmediate(EDOperandRef operand) {
  return ((EDOperand*)operand)->isImmediate();
}

int EDOperandIsMemory(EDOperandRef operand) {
  return ((EDOperand*)operand)->isMemory();
}

int EDRegisterOperandValue(unsigned *value, EDOperandRef operand) {
  if (!((EDOperand*)operand)->isRegister())
    return -1;
  *value = ((EDOperand*)operand)->regVal();
  return 0;
}

int EDImmediateOperandValue(uint64_t *value, EDOperandRef operand) {
  if (!((EDOperand*)operand)->isImmediate())
    return -1;
  *value = ((EDOperand*)operand)->immediateVal();
  return 0;
}

int EDEvaluateOperand(uint64_t *result, EDOperandRef operand,
                      ::EDRegisterReaderCallback regReader, void *arg) {
  return ((EDOperand*)operand)->evaluate(*result, regReader, arg);
}

#ifdef __BLOCKS__

struct ByteReaderWrapper {
  EDByteBlock_t byteBlock;
};

static int readerWrapperCallback(uint8_t *byte, 
                          uint64_t address,
                          void *arg) {
  struct ByteReaderWrapper *wrapper = (struct ByteReaderWrapper *)arg;
  return wrapper->byteBlock(byte, address);
}

unsigned int EDBlockCreateInsts(EDInstRef *insts,
                                int count,
                                EDDisassemblerRef disassembler,
                                EDByteBlock_t byteBlock,
                                uint64_t address) {
  struct ByteReaderWrapper wrapper;
  wrapper.byteBlock = byteBlock;
  
  return EDCreateInsts(insts,
                       count,
                       disassembler, 
                       readerWrapperCallback, 
                       address, 
                       (void*)&wrapper);
}

int EDBlockEvaluateOperand(uint64_t *result, EDOperandRef operand,
                           EDRegisterBlock_t regBlock) {
  return ((EDOperand*)operand)->evaluate(*result, regBlock);
}

int EDBlockVisitTokens(EDInstRef inst, ::EDTokenVisitor_t visitor) {
  return ((EDInst*)inst)->visitTokens((llvm::EDTokenVisitor_t)visitor);
}

#else

extern "C" unsigned int EDBlockCreateInsts() {
  return 0;
}

extern "C" int EDBlockEvaluateOperand() {
  return -1;
}

extern "C" int EDBlockVisitTokens() {
  return -1;
}

#endif