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 DecodeMEMiiOperand(MCInst &Inst, unsigned Val,
96 uint64_t Address, const void *Decoder);
98 static DecodeStatus Decode2RInstruction(MCInst &Inst,
101 const void *Decoder);
103 static DecodeStatus DecodeR2RInstruction(MCInst &Inst,
106 const void *Decoder);
108 static DecodeStatus Decode2RSrcDstInstruction(MCInst &Inst,
111 const void *Decoder);
113 static DecodeStatus DecodeRUSInstruction(MCInst &Inst,
116 const void *Decoder);
118 static DecodeStatus DecodeRUSBitpInstruction(MCInst &Inst,
121 const void *Decoder);
123 static DecodeStatus DecodeRUSSrcDstBitpInstruction(MCInst &Inst,
126 const void *Decoder);
128 static DecodeStatus DecodeL2RInstruction(MCInst &Inst,
131 const void *Decoder);
133 static DecodeStatus DecodeLR2RInstruction(MCInst &Inst,
136 const void *Decoder);
138 static DecodeStatus Decode3RInstruction(MCInst &Inst,
141 const void *Decoder);
143 static DecodeStatus Decode2RUSInstruction(MCInst &Inst,
146 const void *Decoder);
148 static DecodeStatus Decode2RUSBitpInstruction(MCInst &Inst,
151 const void *Decoder);
153 static DecodeStatus DecodeL3RInstruction(MCInst &Inst,
156 const void *Decoder);
158 static DecodeStatus DecodeL3RSrcDstInstruction(MCInst &Inst,
161 const void *Decoder);
163 static DecodeStatus DecodeL2RUSInstruction(MCInst &Inst,
166 const void *Decoder);
168 static DecodeStatus DecodeL2RUSBitpInstruction(MCInst &Inst,
171 const void *Decoder);
173 static DecodeStatus DecodeL6RInstruction(MCInst &Inst,
176 const void *Decoder);
178 static DecodeStatus DecodeL5RInstruction(MCInst &Inst,
181 const void *Decoder);
183 #include "XCoreGenDisassemblerTables.inc"
185 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
191 return MCDisassembler::Fail;
192 unsigned Reg = getReg(Decoder, XCore::GRRegsRegClassID, RegNo);
193 Inst.addOperand(MCOperand::CreateReg(Reg));
194 return MCDisassembler::Success;
197 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
198 uint64_t Address, const void *Decoder) {
200 return MCDisassembler::Fail;
201 static unsigned Values[] = {
202 32 /*bpw*/, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32
204 Inst.addOperand(MCOperand::CreateImm(Values[Val]));
205 return MCDisassembler::Success;
208 static DecodeStatus DecodeMEMiiOperand(MCInst &Inst, unsigned Val,
209 uint64_t Address, const void *Decoder) {
210 Inst.addOperand(MCOperand::CreateImm(Val));
211 Inst.addOperand(MCOperand::CreateImm(0));
212 return MCDisassembler::Success;
216 Decode2OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2) {
217 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
219 return MCDisassembler::Fail;
220 if (fieldFromInstruction(Insn, 5, 1)) {
222 return MCDisassembler::Fail;
226 unsigned Op1High = Combined % 3;
227 unsigned Op2High = Combined / 3;
228 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 2, 2);
229 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 0, 2);
230 return MCDisassembler::Success;
234 Decode3OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2,
236 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
238 return MCDisassembler::Fail;
240 unsigned Op1High = Combined % 3;
241 unsigned Op2High = (Combined / 3) % 3;
242 unsigned Op3High = Combined / 9;
243 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 4, 2);
244 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 2, 2);
245 Op3 = (Op3High << 2) | fieldFromInstruction(Insn, 0, 2);
246 return MCDisassembler::Success;
250 Decode2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
251 const void *Decoder) {
252 // Try and decode as a 3R instruction.
253 unsigned Opcode = fieldFromInstruction(Insn, 11, 5);
256 Inst.setOpcode(XCore::STW_2rus);
257 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
259 Inst.setOpcode(XCore::LDW_2rus);
260 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
262 Inst.setOpcode(XCore::ADD_3r);
263 return Decode3RInstruction(Inst, Insn, Address, Decoder);
265 Inst.setOpcode(XCore::SUB_3r);
266 return Decode3RInstruction(Inst, Insn, Address, Decoder);
268 Inst.setOpcode(XCore::SHL_3r);
269 return Decode3RInstruction(Inst, Insn, Address, Decoder);
271 Inst.setOpcode(XCore::SHR_3r);
272 return Decode3RInstruction(Inst, Insn, Address, Decoder);
274 Inst.setOpcode(XCore::EQ_3r);
275 return Decode3RInstruction(Inst, Insn, Address, Decoder);
277 Inst.setOpcode(XCore::AND_3r);
278 return Decode3RInstruction(Inst, Insn, Address, Decoder);
280 Inst.setOpcode(XCore::OR_3r);
281 return Decode3RInstruction(Inst, Insn, Address, Decoder);
283 Inst.setOpcode(XCore::LDW_3r);
284 return Decode3RInstruction(Inst, Insn, Address, Decoder);
286 Inst.setOpcode(XCore::LD16S_3r);
287 return Decode3RInstruction(Inst, Insn, Address, Decoder);
289 Inst.setOpcode(XCore::LD8U_3r);
290 return Decode3RInstruction(Inst, Insn, Address, Decoder);
292 Inst.setOpcode(XCore::ADD_2rus);
293 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
295 Inst.setOpcode(XCore::SUB_2rus);
296 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
298 Inst.setOpcode(XCore::SHL_2rus);
299 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
301 Inst.setOpcode(XCore::SHR_2rus);
302 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
304 Inst.setOpcode(XCore::EQ_2rus);
305 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
307 Inst.setOpcode(XCore::LSS_3r);
308 return Decode3RInstruction(Inst, Insn, Address, Decoder);
310 Inst.setOpcode(XCore::LSU_3r);
311 return Decode3RInstruction(Inst, Insn, Address, Decoder);
313 return MCDisassembler::Fail;
317 Decode2RInstruction(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 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
330 DecodeR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
331 const void *Decoder) {
333 DecodeStatus S = Decode2OpInstruction(Insn, Op2, Op1);
334 if (S != MCDisassembler::Success)
335 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
337 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
338 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
343 Decode2RSrcDstInstruction(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 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
357 DecodeRUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
358 const void *Decoder) {
360 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
361 if (S != MCDisassembler::Success)
362 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
364 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
365 Inst.addOperand(MCOperand::CreateImm(Op2));
370 DecodeRUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
371 const void *Decoder) {
373 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
374 if (S != MCDisassembler::Success)
375 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
377 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
378 DecodeBitpOperand(Inst, Op2, Address, Decoder);
383 DecodeRUSSrcDstBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
384 const void *Decoder) {
386 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
387 if (S != MCDisassembler::Success)
388 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
390 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
391 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
392 DecodeBitpOperand(Inst, Op2, Address, Decoder);
397 DecodeL2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
398 const void *Decoder) {
399 // Try and decode as a L3R / L2RUS instruction.
400 unsigned Opcode = fieldFromInstruction(Insn, 16, 4) |
401 fieldFromInstruction(Insn, 27, 5) << 4;
404 Inst.setOpcode(XCore::STW_l3r);
405 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
407 Inst.setOpcode(XCore::XOR_l3r);
408 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
410 Inst.setOpcode(XCore::ASHR_l3r);
411 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
413 Inst.setOpcode(XCore::LDAWF_l3r);
414 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
416 Inst.setOpcode(XCore::LDAWB_l3r);
417 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
419 Inst.setOpcode(XCore::LDA16F_l3r);
420 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
422 Inst.setOpcode(XCore::LDA16B_l3r);
423 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
425 Inst.setOpcode(XCore::MUL_l3r);
426 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
428 Inst.setOpcode(XCore::DIVS_l3r);
429 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
431 Inst.setOpcode(XCore::DIVU_l3r);
432 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
434 Inst.setOpcode(XCore::ST16_l3r);
435 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
437 Inst.setOpcode(XCore::ST8_l3r);
438 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
440 Inst.setOpcode(XCore::ASHR_l2rus);
441 return DecodeL2RUSBitpInstruction(Inst, Insn, Address, Decoder);
443 Inst.setOpcode(XCore::LDAWF_l2rus);
444 return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
446 Inst.setOpcode(XCore::LDAWB_l2rus);
447 return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
449 Inst.setOpcode(XCore::CRC_l3r);
450 return DecodeL3RSrcDstInstruction(Inst, Insn, Address, Decoder);
452 Inst.setOpcode(XCore::REMS_l3r);
453 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
455 Inst.setOpcode(XCore::REMU_l3r);
456 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
458 return MCDisassembler::Fail;
462 DecodeL2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
463 const void *Decoder) {
465 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
467 if (S != MCDisassembler::Success)
468 return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
470 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
471 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
476 DecodeLR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
477 const void *Decoder) {
479 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
481 if (S != MCDisassembler::Success)
482 return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
484 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
485 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
490 Decode3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
491 const void *Decoder) {
492 unsigned Op1, Op2, Op3;
493 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
494 if (S == MCDisassembler::Success) {
495 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
496 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
497 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
503 Decode2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
504 const void *Decoder) {
505 unsigned Op1, Op2, Op3;
506 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
507 if (S == MCDisassembler::Success) {
508 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
509 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
510 Inst.addOperand(MCOperand::CreateImm(Op3));
516 Decode2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
517 const void *Decoder) {
518 unsigned Op1, Op2, Op3;
519 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
520 if (S == MCDisassembler::Success) {
521 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
522 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
523 DecodeBitpOperand(Inst, Op3, Address, Decoder);
529 DecodeL3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
530 const void *Decoder) {
531 unsigned Op1, Op2, Op3;
533 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
534 if (S == MCDisassembler::Success) {
535 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
536 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
537 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
543 DecodeL3RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
544 const void *Decoder) {
545 unsigned Op1, Op2, Op3;
547 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
548 if (S == MCDisassembler::Success) {
549 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
550 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
551 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
552 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
558 DecodeL2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
559 const void *Decoder) {
560 unsigned Op1, Op2, Op3;
562 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
563 if (S == MCDisassembler::Success) {
564 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
565 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
566 Inst.addOperand(MCOperand::CreateImm(Op3));
572 DecodeL2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
573 const void *Decoder) {
574 unsigned Op1, Op2, Op3;
576 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
577 if (S == MCDisassembler::Success) {
578 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
579 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
580 DecodeBitpOperand(Inst, Op3, Address, Decoder);
586 DecodeL6RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
587 const void *Decoder) {
588 unsigned Op1, Op2, Op3, Op4, Op5, Op6;
590 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
591 if (S != MCDisassembler::Success)
593 S = Decode3OpInstruction(fieldFromInstruction(Insn, 16, 16), Op4, Op5, Op6);
594 if (S != MCDisassembler::Success)
596 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
597 DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
598 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
599 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
600 DecodeGRRegsRegisterClass(Inst, Op5, Address, Decoder);
601 DecodeGRRegsRegisterClass(Inst, Op6, Address, Decoder);
606 DecodeL5RInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
607 const void *Decoder) {
608 // Try and decode as a L6R instruction.
610 unsigned Opcode = fieldFromInstruction(Insn, 27, 5);
613 Inst.setOpcode(XCore::LMUL_l6r);
614 return DecodeL6RInstruction(Inst, Insn, Address, Decoder);
616 return MCDisassembler::Fail;
620 DecodeL5RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
621 const void *Decoder) {
622 unsigned Op1, Op2, Op3, Op4, Op5;
624 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
625 if (S != MCDisassembler::Success)
626 return DecodeL5RInstructionFail(Inst, Insn, Address, Decoder);
627 S = Decode2OpInstruction(fieldFromInstruction(Insn, 16, 16), Op4, Op5);
628 if (S != MCDisassembler::Success)
629 return DecodeL5RInstructionFail(Inst, Insn, Address, Decoder);
631 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
632 DecodeGRRegsRegisterClass(Inst, Op4, Address, Decoder);
633 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
634 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
635 DecodeGRRegsRegisterClass(Inst, Op5, Address, Decoder);
639 MCDisassembler::DecodeStatus
640 XCoreDisassembler::getInstruction(MCInst &instr,
642 const MemoryObject &Region,
644 raw_ostream &vStream,
645 raw_ostream &cStream) const {
648 if (!readInstruction16(Region, Address, Size, insn16)) {
652 // Calling the auto-generated decoder function.
653 DecodeStatus Result = decodeInstruction(DecoderTable16, instr, insn16,
655 if (Result != Fail) {
662 if (!readInstruction32(Region, Address, Size, insn32)) {
666 // Calling the auto-generated decoder function.
667 Result = decodeInstruction(DecoderTable32, instr, insn32, Address, this, STI);
668 if (Result != Fail) {
677 extern Target TheXCoreTarget;
680 static MCDisassembler *createXCoreDisassembler(const Target &T,
681 const MCSubtargetInfo &STI) {
682 return new XCoreDisassembler(STI, T.createMCRegInfo(""));
685 extern "C" void LLVMInitializeXCoreDisassembler() {
686 // Register the disassembler.
687 TargetRegistry::RegisterMCDisassembler(TheXCoreTarget,
688 createXCoreDisassembler);