1 //===-- EDMain.cpp - LLVM Enhanced Disassembly C API ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
23 int EDGetDisassembler(EDDisassemblerRef
*disassembler
,
25 EDAssemblySyntax_t syntax
) {
26 EDDisassembler::initialize();
28 EDDisassembler::AssemblySyntax Syntax
;
30 default: assert(0 && "Unknown assembly syntax!");
31 case kEDAssemblySyntaxX86Intel
:
32 Syntax
= EDDisassembler::kEDAssemblySyntaxX86Intel
;
34 case kEDAssemblySyntaxX86ATT
:
35 Syntax
= EDDisassembler::kEDAssemblySyntaxX86ATT
;
37 case kEDAssemblySyntaxARMUAL
:
38 Syntax
= EDDisassembler::kEDAssemblySyntaxARMUAL
;
42 EDDisassemblerRef ret
= EDDisassembler::getDisassembler(triple
, Syntax
);
50 int EDGetRegisterName(const char** regName
,
51 EDDisassemblerRef disassembler
,
53 const char *name
= ((EDDisassembler
*)disassembler
)->nameWithRegisterID(regID
);
60 int EDRegisterIsStackPointer(EDDisassemblerRef disassembler
,
62 return ((EDDisassembler
*)disassembler
)->registerIsStackPointer(regID
) ? 1 : 0;
65 int EDRegisterIsProgramCounter(EDDisassemblerRef disassembler
,
67 return ((EDDisassembler
*)disassembler
)->registerIsProgramCounter(regID
) ? 1:0;
70 unsigned int EDCreateInsts(EDInstRef
*insts
,
72 EDDisassemblerRef disassembler
,
73 ::EDByteReaderCallback byteReader
,
78 for (index
= 0; index
< count
; ++index
) {
79 EDInst
*inst
= ((EDDisassembler
*)disassembler
)->createInst(byteReader
,
86 address
+= inst
->byteSize();
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
,
102 return ((EDInst
*)inst
)->getString(*buf
);
105 int EDInstID(unsigned *instID
, EDInstRef inst
) {
106 *instID
= ((EDInst
*)inst
)->instID();
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
,
137 return ((EDInst
*)inst
)->getToken(*(EDToken
**)token
, index
);
140 int EDGetTokenString(const char **buf
,
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
)
173 return ((EDToken
*)token
)->literalSign();
176 int EDLiteralTokenAbsoluteValue(uint64_t *value
, EDTokenRef token
) {
177 if (((EDToken
*)token
)->type() != EDToken::kTokenLiteral
)
180 return ((EDToken
*)token
)->literalAbsoluteValue(*value
);
183 int EDRegisterTokenValue(unsigned *registerID
,
185 if (((EDToken
*)token
)->type() != EDToken::kTokenRegister
)
188 return ((EDToken
*)token
)->registerID(*registerID
);
191 int EDNumOperands(EDInstRef inst
) {
192 return ((EDInst
*)inst
)->numOperands();
195 int EDGetOperand(EDOperandRef
*operand
,
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())
216 *value
= ((EDOperand
*)operand
)->regVal();
220 int EDImmediateOperandValue(uint64_t *value
, EDOperandRef operand
) {
221 if (!((EDOperand
*)operand
)->isImmediate())
223 *value
= ((EDOperand
*)operand
)->immediateVal();
227 int EDEvaluateOperand(uint64_t *result
, EDOperandRef operand
,
228 ::EDRegisterReaderCallback regReader
, void *arg
) {
229 return ((EDOperand
*)operand
)->evaluate(*result
, regReader
, arg
);
234 struct ByteReaderWrapper
{
235 EDByteBlock_t byteBlock
;
238 static int readerWrapperCallback(uint8_t *byte
,
241 struct ByteReaderWrapper
*wrapper
= (struct ByteReaderWrapper
*)arg
;
242 return wrapper
->byteBlock(byte
, address
);
245 unsigned int EDBlockCreateInsts(EDInstRef
*insts
,
247 EDDisassemblerRef disassembler
,
248 EDByteBlock_t byteBlock
,
250 struct ByteReaderWrapper wrapper
;
251 wrapper
.byteBlock
= byteBlock
;
253 return EDCreateInsts(insts
,
256 readerWrapperCallback
,
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
);
272 extern "C" unsigned int EDBlockCreateInsts() {
276 extern "C" int EDBlockEvaluateOperand() {
280 extern "C" int EDBlockVisitTokens() {