the various ConstantExpr::get*Ty methods existed to work with issues around
[llvm/stm8.git] / tools / edis / EDMain.cpp
blob16855b3f45d8d314349e1817ce9d3214be49ef98
1 //===-- EDMain.cpp - LLVM Enhanced Disassembly C API ----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the enhanced disassembler's public C API.
12 //===----------------------------------------------------------------------===//
14 // FIXME: This code isn't layered right, the headers should be moved to
15 // include llvm/MC/MCDisassembler or something.
16 #include "../../lib/MC/MCDisassembler/EDDisassembler.h"
17 #include "../../lib/MC/MCDisassembler/EDInst.h"
18 #include "../../lib/MC/MCDisassembler/EDOperand.h"
19 #include "../../lib/MC/MCDisassembler/EDToken.h"
20 #include "llvm-c/EnhancedDisassembly.h"
21 using namespace llvm;
23 int EDGetDisassembler(EDDisassemblerRef *disassembler,
24 const char *triple,
25 EDAssemblySyntax_t syntax) {
26 EDDisassembler::initialize();
28 EDDisassembler::AssemblySyntax Syntax;
29 switch (syntax) {
30 default: assert(0 && "Unknown assembly syntax!");
31 case kEDAssemblySyntaxX86Intel:
32 Syntax = EDDisassembler::kEDAssemblySyntaxX86Intel;
33 break;
34 case kEDAssemblySyntaxX86ATT:
35 Syntax = EDDisassembler::kEDAssemblySyntaxX86ATT;
36 break;
37 case kEDAssemblySyntaxARMUAL:
38 Syntax = EDDisassembler::kEDAssemblySyntaxARMUAL;
39 break;
42 EDDisassemblerRef ret = EDDisassembler::getDisassembler(triple, Syntax);
44 if (!ret)
45 return -1;
46 *disassembler = ret;
47 return 0;
50 int EDGetRegisterName(const char** regName,
51 EDDisassemblerRef disassembler,
52 unsigned regID) {
53 const char *name = ((EDDisassembler*)disassembler)->nameWithRegisterID(regID);
54 if (!name)
55 return -1;
56 *regName = name;
57 return 0;
60 int EDRegisterIsStackPointer(EDDisassemblerRef disassembler,
61 unsigned regID) {
62 return ((EDDisassembler*)disassembler)->registerIsStackPointer(regID) ? 1 : 0;
65 int EDRegisterIsProgramCounter(EDDisassemblerRef disassembler,
66 unsigned regID) {
67 return ((EDDisassembler*)disassembler)->registerIsProgramCounter(regID) ? 1:0;
70 unsigned int EDCreateInsts(EDInstRef *insts,
71 unsigned int count,
72 EDDisassemblerRef disassembler,
73 ::EDByteReaderCallback byteReader,
74 uint64_t address,
75 void *arg) {
76 unsigned int index;
78 for (index = 0; index < count; ++index) {
79 EDInst *inst = ((EDDisassembler*)disassembler)->createInst(byteReader,
80 address, arg);
82 if (!inst)
83 return index;
85 insts[index] = inst;
86 address += inst->byteSize();
89 return count;
92 void EDReleaseInst(EDInstRef inst) {
93 delete ((EDInst*)inst);
96 int EDInstByteSize(EDInstRef inst) {
97 return ((EDInst*)inst)->byteSize();
100 int EDGetInstString(const char **buf,
101 EDInstRef inst) {
102 return ((EDInst*)inst)->getString(*buf);
105 int EDInstID(unsigned *instID, EDInstRef inst) {
106 *instID = ((EDInst*)inst)->instID();
107 return 0;
110 int EDInstIsBranch(EDInstRef inst) {
111 return ((EDInst*)inst)->isBranch();
114 int EDInstIsMove(EDInstRef inst) {
115 return ((EDInst*)inst)->isMove();
118 int EDBranchTargetID(EDInstRef inst) {
119 return ((EDInst*)inst)->branchTargetID();
122 int EDMoveSourceID(EDInstRef inst) {
123 return ((EDInst*)inst)->moveSourceID();
126 int EDMoveTargetID(EDInstRef inst) {
127 return ((EDInst*)inst)->moveTargetID();
130 int EDNumTokens(EDInstRef inst) {
131 return ((EDInst*)inst)->numTokens();
134 int EDGetToken(EDTokenRef *token,
135 EDInstRef inst,
136 int index) {
137 return ((EDInst*)inst)->getToken(*(EDToken**)token, index);
140 int EDGetTokenString(const char **buf,
141 EDTokenRef token) {
142 return ((EDToken*)token)->getString(*buf);
145 int EDOperandIndexForToken(EDTokenRef token) {
146 return ((EDToken*)token)->operandID();
149 int EDTokenIsWhitespace(EDTokenRef token) {
150 return ((EDToken*)token)->type() == EDToken::kTokenWhitespace;
153 int EDTokenIsPunctuation(EDTokenRef token) {
154 return ((EDToken*)token)->type() == EDToken::kTokenPunctuation;
157 int EDTokenIsOpcode(EDTokenRef token) {
158 return ((EDToken*)token)->type() == EDToken::kTokenOpcode;
161 int EDTokenIsLiteral(EDTokenRef token) {
162 return ((EDToken*)token)->type() == EDToken::kTokenLiteral;
165 int EDTokenIsRegister(EDTokenRef token) {
166 return ((EDToken*)token)->type() == EDToken::kTokenRegister;
169 int EDTokenIsNegativeLiteral(EDTokenRef token) {
170 if (((EDToken*)token)->type() != EDToken::kTokenLiteral)
171 return -1;
173 return ((EDToken*)token)->literalSign();
176 int EDLiteralTokenAbsoluteValue(uint64_t *value, EDTokenRef token) {
177 if (((EDToken*)token)->type() != EDToken::kTokenLiteral)
178 return -1;
180 return ((EDToken*)token)->literalAbsoluteValue(*value);
183 int EDRegisterTokenValue(unsigned *registerID,
184 EDTokenRef token) {
185 if (((EDToken*)token)->type() != EDToken::kTokenRegister)
186 return -1;
188 return ((EDToken*)token)->registerID(*registerID);
191 int EDNumOperands(EDInstRef inst) {
192 return ((EDInst*)inst)->numOperands();
195 int EDGetOperand(EDOperandRef *operand,
196 EDInstRef inst,
197 int index) {
198 return ((EDInst*)inst)->getOperand(*(EDOperand**)operand, index);
201 int EDOperandIsRegister(EDOperandRef operand) {
202 return ((EDOperand*)operand)->isRegister();
205 int EDOperandIsImmediate(EDOperandRef operand) {
206 return ((EDOperand*)operand)->isImmediate();
209 int EDOperandIsMemory(EDOperandRef operand) {
210 return ((EDOperand*)operand)->isMemory();
213 int EDRegisterOperandValue(unsigned *value, EDOperandRef operand) {
214 if (!((EDOperand*)operand)->isRegister())
215 return -1;
216 *value = ((EDOperand*)operand)->regVal();
217 return 0;
220 int EDImmediateOperandValue(uint64_t *value, EDOperandRef operand) {
221 if (!((EDOperand*)operand)->isImmediate())
222 return -1;
223 *value = ((EDOperand*)operand)->immediateVal();
224 return 0;
227 int EDEvaluateOperand(uint64_t *result, EDOperandRef operand,
228 ::EDRegisterReaderCallback regReader, void *arg) {
229 return ((EDOperand*)operand)->evaluate(*result, regReader, arg);
232 #ifdef __BLOCKS__
234 struct ByteReaderWrapper {
235 EDByteBlock_t byteBlock;
238 static int readerWrapperCallback(uint8_t *byte,
239 uint64_t address,
240 void *arg) {
241 struct ByteReaderWrapper *wrapper = (struct ByteReaderWrapper *)arg;
242 return wrapper->byteBlock(byte, address);
245 unsigned int EDBlockCreateInsts(EDInstRef *insts,
246 int count,
247 EDDisassemblerRef disassembler,
248 EDByteBlock_t byteBlock,
249 uint64_t address) {
250 struct ByteReaderWrapper wrapper;
251 wrapper.byteBlock = byteBlock;
253 return EDCreateInsts(insts,
254 count,
255 disassembler,
256 readerWrapperCallback,
257 address,
258 (void*)&wrapper);
261 int EDBlockEvaluateOperand(uint64_t *result, EDOperandRef operand,
262 EDRegisterBlock_t regBlock) {
263 return ((EDOperand*)operand)->evaluate(*result, regBlock);
266 int EDBlockVisitTokens(EDInstRef inst, ::EDTokenVisitor_t visitor) {
267 return ((EDInst*)inst)->visitTokens((llvm::EDTokenVisitor_t)visitor);
270 #else
272 extern "C" unsigned int EDBlockCreateInsts() {
273 return 0;
276 extern "C" int EDBlockEvaluateOperand() {
277 return -1;
280 extern "C" int EDBlockVisitTokens() {
281 return -1;
284 #endif