1 //===- XCoreDisassembler.cpp - Disassembler for XCore -----------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
11 /// \brief This file is part of the XCore Disassembler.
13 //===----------------------------------------------------------------------===//
16 #include "XCoreRegisterInfo.h"
17 #include "llvm/MC/MCDisassembler.h"
18 #include "llvm/MC/MCFixedLenDisassembler.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSubtargetInfo.h"
21 #include "llvm/Support/MemoryObject.h"
22 #include "llvm/Support/TargetRegistry.h"
26 typedef MCDisassembler::DecodeStatus DecodeStatus;
30 /// \brief A disassembler class for XCore.
31 class XCoreDisassembler : public MCDisassembler {
32 const MCRegisterInfo *RegInfo;
34 XCoreDisassembler(const MCSubtargetInfo &STI, const MCRegisterInfo *Info) :
35 MCDisassembler(STI), RegInfo(Info) {}
37 /// \brief See MCDisassembler.
38 virtual DecodeStatus getInstruction(MCInst &instr,
40 const MemoryObject ®ion,
43 raw_ostream &cStream) const;
45 const MCRegisterInfo *getRegInfo() const { return RegInfo; }
49 static bool readInstruction16(const MemoryObject ®ion,
55 // We want to read exactly 2 Bytes of data.
56 if (region.readBytes(address, 2, Bytes, NULL) == -1) {
60 // Encoded as a little-endian 16-bit word in the stream.
61 insn = (Bytes[0] << 0) | (Bytes[1] << 8);
65 static bool readInstruction32(const MemoryObject ®ion,
71 // We want to read exactly 4 Bytes of data.
72 if (region.readBytes(address, 4, Bytes, NULL) == -1) {
76 // Encoded as a little-endian 32-bit word in the stream.
77 insn = (Bytes[0] << 0) | (Bytes[1] << 8) | (Bytes[2] << 16) |
82 static unsigned getReg(const void *D, unsigned RC, unsigned RegNo) {
83 const XCoreDisassembler *Dis = static_cast<const XCoreDisassembler*>(D);
84 return *(Dis->getRegInfo()->getRegClass(RC).begin() + RegNo);
87 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
92 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
93 uint64_t Address, const void *Decoder);
95 static DecodeStatus Decode2RInstruction(MCInst &Inst,
100 static DecodeStatus DecodeR2RInstruction(MCInst &Inst,
103 const void *Decoder);
105 static DecodeStatus Decode2RSrcDstInstruction(MCInst &Inst,
108 const void *Decoder);
110 static DecodeStatus DecodeRUSInstruction(MCInst &Inst,
113 const void *Decoder);
115 static DecodeStatus DecodeRUSBitpInstruction(MCInst &Inst,
118 const void *Decoder);
120 static DecodeStatus DecodeRUSSrcDstBitpInstruction(MCInst &Inst,
123 const void *Decoder);
125 static DecodeStatus DecodeL2RInstruction(MCInst &Inst,
128 const void *Decoder);
130 static DecodeStatus DecodeLR2RInstruction(MCInst &Inst,
133 const void *Decoder);
135 static DecodeStatus Decode3RInstruction(MCInst &Inst,
138 const void *Decoder);
140 static DecodeStatus Decode2RUSInstruction(MCInst &Inst,
143 const void *Decoder);
145 static DecodeStatus Decode2RUSBitpInstruction(MCInst &Inst,
148 const void *Decoder);
150 #include "XCoreGenDisassemblerTables.inc"
152 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
158 return MCDisassembler::Fail;
159 unsigned Reg = getReg(Decoder, XCore::GRRegsRegClassID, RegNo);
160 Inst.addOperand(MCOperand::CreateReg(Reg));
161 return MCDisassembler::Success;
164 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
165 uint64_t Address, const void *Decoder) {
167 return MCDisassembler::Fail;
168 static unsigned Values[] = {
169 32 /*bpw*/, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32
171 Inst.addOperand(MCOperand::CreateImm(Values[Val]));
172 return MCDisassembler::Success;
176 Decode2OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2) {
177 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
179 return MCDisassembler::Fail;
180 if (fieldFromInstruction(Insn, 5, 1)) {
182 return MCDisassembler::Fail;
186 unsigned Op1High = Combined % 3;
187 unsigned Op2High = Combined / 3;
188 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 2, 2);
189 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 0, 2);
190 return MCDisassembler::Success;
194 Decode3OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2,
196 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
198 return MCDisassembler::Fail;
200 unsigned Op1High = Combined % 3;
201 unsigned Op2High = (Combined / 3) % 3;
202 unsigned Op3High = Combined / 9;
203 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 4, 2);
204 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 2, 2);
205 Op3 = (Op3High << 2) | fieldFromInstruction(Insn, 0, 2);
206 return MCDisassembler::Success;
210 Decode2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
211 const void *Decoder) {
212 // Try and decode as a 3R instruction.
213 unsigned Opcode = fieldFromInstruction(Insn, 11, 5);
216 Inst.setOpcode(XCore::STW_2rus);
217 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
219 Inst.setOpcode(XCore::LDW_2rus);
220 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
222 Inst.setOpcode(XCore::ADD_3r);
223 return Decode3RInstruction(Inst, Insn, Address, Decoder);
225 Inst.setOpcode(XCore::SUB_3r);
226 return Decode3RInstruction(Inst, Insn, Address, Decoder);
228 Inst.setOpcode(XCore::SHL_3r);
229 return Decode3RInstruction(Inst, Insn, Address, Decoder);
231 Inst.setOpcode(XCore::SHR_3r);
232 return Decode3RInstruction(Inst, Insn, Address, Decoder);
234 Inst.setOpcode(XCore::EQ_3r);
235 return Decode3RInstruction(Inst, Insn, Address, Decoder);
237 Inst.setOpcode(XCore::AND_3r);
238 return Decode3RInstruction(Inst, Insn, Address, Decoder);
240 Inst.setOpcode(XCore::OR_3r);
241 return Decode3RInstruction(Inst, Insn, Address, Decoder);
243 Inst.setOpcode(XCore::LDW_3r);
244 return Decode3RInstruction(Inst, Insn, Address, Decoder);
246 Inst.setOpcode(XCore::LD16S_3r);
247 return Decode3RInstruction(Inst, Insn, Address, Decoder);
249 Inst.setOpcode(XCore::LD8U_3r);
250 return Decode3RInstruction(Inst, Insn, Address, Decoder);
252 Inst.setOpcode(XCore::ADD_2rus);
253 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
255 Inst.setOpcode(XCore::SUB_2rus);
256 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
258 Inst.setOpcode(XCore::SHL_2rus);
259 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
261 Inst.setOpcode(XCore::SHR_2rus);
262 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
264 Inst.setOpcode(XCore::EQ_2rus);
265 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
267 Inst.setOpcode(XCore::LSS_3r);
268 return Decode3RInstruction(Inst, Insn, Address, Decoder);
270 Inst.setOpcode(XCore::LSU_3r);
271 return Decode3RInstruction(Inst, Insn, Address, Decoder);
273 return MCDisassembler::Fail;
277 Decode2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
278 const void *Decoder) {
280 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
281 if (S != MCDisassembler::Success)
282 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
284 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
285 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
290 DecodeR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
291 const void *Decoder) {
293 DecodeStatus S = Decode2OpInstruction(Insn, Op2, Op1);
294 if (S != MCDisassembler::Success)
295 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
297 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
298 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
303 Decode2RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
304 const void *Decoder) {
306 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
307 if (S != MCDisassembler::Success)
308 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
310 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
311 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
312 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
317 DecodeRUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
318 const void *Decoder) {
320 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
321 if (S != MCDisassembler::Success)
322 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
324 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
325 Inst.addOperand(MCOperand::CreateImm(Op2));
330 DecodeRUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
331 const void *Decoder) {
333 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
334 if (S != MCDisassembler::Success)
335 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
337 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
338 DecodeBitpOperand(Inst, Op2, Address, Decoder);
343 DecodeRUSSrcDstBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
344 const void *Decoder) {
346 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
347 if (S != MCDisassembler::Success)
348 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
350 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
351 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
352 DecodeBitpOperand(Inst, Op2, Address, Decoder);
357 DecodeL2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
358 const void *Decoder) {
360 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
362 if (S == MCDisassembler::Success) {
363 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
364 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
370 DecodeLR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
371 const void *Decoder) {
373 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
375 if (S == MCDisassembler::Success) {
376 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
377 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
383 Decode3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
384 const void *Decoder) {
385 unsigned Op1, Op2, Op3;
386 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
387 if (S == MCDisassembler::Success) {
388 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
389 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
390 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
396 Decode2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
397 const void *Decoder) {
398 unsigned Op1, Op2, Op3;
399 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
400 if (S == MCDisassembler::Success) {
401 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
402 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
403 Inst.addOperand(MCOperand::CreateImm(Op3));
409 Decode2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
410 const void *Decoder) {
411 unsigned Op1, Op2, Op3;
412 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
413 if (S == MCDisassembler::Success) {
414 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
415 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
416 DecodeBitpOperand(Inst, Op3, Address, Decoder);
421 MCDisassembler::DecodeStatus
422 XCoreDisassembler::getInstruction(MCInst &instr,
424 const MemoryObject &Region,
426 raw_ostream &vStream,
427 raw_ostream &cStream) const {
430 if (!readInstruction16(Region, Address, Size, insn16)) {
434 // Calling the auto-generated decoder function.
435 DecodeStatus Result = decodeInstruction(DecoderTable16, instr, insn16,
437 if (Result != Fail) {
444 if (!readInstruction32(Region, Address, Size, insn32)) {
448 // Calling the auto-generated decoder function.
449 Result = decodeInstruction(DecoderTable32, instr, insn32, Address, this, STI);
450 if (Result != Fail) {
459 extern Target TheXCoreTarget;
462 static MCDisassembler *createXCoreDisassembler(const Target &T,
463 const MCSubtargetInfo &STI) {
464 return new XCoreDisassembler(STI, T.createMCRegInfo(""));
467 extern "C" void LLVMInitializeXCoreDisassembler() {
468 // Register the disassembler.
469 TargetRegistry::RegisterMCDisassembler(TheXCoreTarget,
470 createXCoreDisassembler);