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 #include "XCoreGenDisassemblerTables.inc"
175 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
181 return MCDisassembler::Fail;
182 unsigned Reg = getReg(Decoder, XCore::GRRegsRegClassID, RegNo);
183 Inst.addOperand(MCOperand::CreateReg(Reg));
184 return MCDisassembler::Success;
187 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
188 uint64_t Address, const void *Decoder) {
190 return MCDisassembler::Fail;
191 static unsigned Values[] = {
192 32 /*bpw*/, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32
194 Inst.addOperand(MCOperand::CreateImm(Values[Val]));
195 return MCDisassembler::Success;
198 static DecodeStatus DecodeMEMiiOperand(MCInst &Inst, unsigned Val,
199 uint64_t Address, const void *Decoder) {
200 Inst.addOperand(MCOperand::CreateImm(Val));
201 Inst.addOperand(MCOperand::CreateImm(0));
202 return MCDisassembler::Success;
206 Decode2OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2) {
207 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
209 return MCDisassembler::Fail;
210 if (fieldFromInstruction(Insn, 5, 1)) {
212 return MCDisassembler::Fail;
216 unsigned Op1High = Combined % 3;
217 unsigned Op2High = Combined / 3;
218 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 2, 2);
219 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 0, 2);
220 return MCDisassembler::Success;
224 Decode3OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2,
226 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
228 return MCDisassembler::Fail;
230 unsigned Op1High = Combined % 3;
231 unsigned Op2High = (Combined / 3) % 3;
232 unsigned Op3High = Combined / 9;
233 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 4, 2);
234 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 2, 2);
235 Op3 = (Op3High << 2) | fieldFromInstruction(Insn, 0, 2);
236 return MCDisassembler::Success;
240 Decode2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
241 const void *Decoder) {
242 // Try and decode as a 3R instruction.
243 unsigned Opcode = fieldFromInstruction(Insn, 11, 5);
246 Inst.setOpcode(XCore::STW_2rus);
247 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
249 Inst.setOpcode(XCore::LDW_2rus);
250 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
252 Inst.setOpcode(XCore::ADD_3r);
253 return Decode3RInstruction(Inst, Insn, Address, Decoder);
255 Inst.setOpcode(XCore::SUB_3r);
256 return Decode3RInstruction(Inst, Insn, Address, Decoder);
258 Inst.setOpcode(XCore::SHL_3r);
259 return Decode3RInstruction(Inst, Insn, Address, Decoder);
261 Inst.setOpcode(XCore::SHR_3r);
262 return Decode3RInstruction(Inst, Insn, Address, Decoder);
264 Inst.setOpcode(XCore::EQ_3r);
265 return Decode3RInstruction(Inst, Insn, Address, Decoder);
267 Inst.setOpcode(XCore::AND_3r);
268 return Decode3RInstruction(Inst, Insn, Address, Decoder);
270 Inst.setOpcode(XCore::OR_3r);
271 return Decode3RInstruction(Inst, Insn, Address, Decoder);
273 Inst.setOpcode(XCore::LDW_3r);
274 return Decode3RInstruction(Inst, Insn, Address, Decoder);
276 Inst.setOpcode(XCore::LD16S_3r);
277 return Decode3RInstruction(Inst, Insn, Address, Decoder);
279 Inst.setOpcode(XCore::LD8U_3r);
280 return Decode3RInstruction(Inst, Insn, Address, Decoder);
282 Inst.setOpcode(XCore::ADD_2rus);
283 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
285 Inst.setOpcode(XCore::SUB_2rus);
286 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
288 Inst.setOpcode(XCore::SHL_2rus);
289 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
291 Inst.setOpcode(XCore::SHR_2rus);
292 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
294 Inst.setOpcode(XCore::EQ_2rus);
295 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
297 Inst.setOpcode(XCore::LSS_3r);
298 return Decode3RInstruction(Inst, Insn, Address, Decoder);
300 Inst.setOpcode(XCore::LSU_3r);
301 return Decode3RInstruction(Inst, Insn, Address, Decoder);
303 return MCDisassembler::Fail;
307 Decode2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
308 const void *Decoder) {
310 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
311 if (S != MCDisassembler::Success)
312 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
314 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
315 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
320 DecodeR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
321 const void *Decoder) {
323 DecodeStatus S = Decode2OpInstruction(Insn, Op2, Op1);
324 if (S != MCDisassembler::Success)
325 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
327 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
328 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
333 Decode2RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
334 const void *Decoder) {
336 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
337 if (S != MCDisassembler::Success)
338 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
340 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
341 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
342 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
347 DecodeRUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
348 const void *Decoder) {
350 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
351 if (S != MCDisassembler::Success)
352 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
354 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
355 Inst.addOperand(MCOperand::CreateImm(Op2));
360 DecodeRUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
361 const void *Decoder) {
363 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
364 if (S != MCDisassembler::Success)
365 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
367 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
368 DecodeBitpOperand(Inst, Op2, Address, Decoder);
373 DecodeRUSSrcDstBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
374 const void *Decoder) {
376 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
377 if (S != MCDisassembler::Success)
378 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
380 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
381 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
382 DecodeBitpOperand(Inst, Op2, Address, Decoder);
387 DecodeL2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
388 const void *Decoder) {
389 // Try and decode as a L3R / L2RUS instruction.
390 unsigned Opcode = fieldFromInstruction(Insn, 16, 4) |
391 fieldFromInstruction(Insn, 27, 5) << 4;
394 Inst.setOpcode(XCore::STW_3r);
395 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
397 Inst.setOpcode(XCore::XOR_l3r);
398 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
400 Inst.setOpcode(XCore::ASHR_l3r);
401 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
403 Inst.setOpcode(XCore::LDAWF_l3r);
404 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
406 Inst.setOpcode(XCore::LDAWB_l3r);
407 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
409 Inst.setOpcode(XCore::LDA16F_l3r);
410 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
412 Inst.setOpcode(XCore::LDA16B_l3r);
413 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
415 Inst.setOpcode(XCore::MUL_l3r);
416 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
418 Inst.setOpcode(XCore::DIVS_l3r);
419 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
421 Inst.setOpcode(XCore::DIVU_l3r);
422 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
424 Inst.setOpcode(XCore::ST16_l3r);
425 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
427 Inst.setOpcode(XCore::ST8_l3r);
428 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
430 Inst.setOpcode(XCore::ASHR_l2rus);
431 return DecodeL2RUSBitpInstruction(Inst, Insn, Address, Decoder);
433 Inst.setOpcode(XCore::LDAWF_l2rus);
434 return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
436 Inst.setOpcode(XCore::LDAWB_l2rus);
437 return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
439 Inst.setOpcode(XCore::CRC_l3r);
440 return DecodeL3RSrcDstInstruction(Inst, Insn, Address, Decoder);
442 Inst.setOpcode(XCore::REMS_l3r);
443 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
445 Inst.setOpcode(XCore::REMU_l3r);
446 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
448 return MCDisassembler::Fail;
452 DecodeL2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
453 const void *Decoder) {
455 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
457 if (S != MCDisassembler::Success)
458 return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
460 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
461 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
466 DecodeLR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
467 const void *Decoder) {
469 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
471 if (S != MCDisassembler::Success)
472 return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
474 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
475 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
480 Decode3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
481 const void *Decoder) {
482 unsigned Op1, Op2, Op3;
483 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
484 if (S == MCDisassembler::Success) {
485 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
486 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
487 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
493 Decode2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
494 const void *Decoder) {
495 unsigned Op1, Op2, Op3;
496 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
497 if (S == MCDisassembler::Success) {
498 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
499 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
500 Inst.addOperand(MCOperand::CreateImm(Op3));
506 Decode2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
507 const void *Decoder) {
508 unsigned Op1, Op2, Op3;
509 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
510 if (S == MCDisassembler::Success) {
511 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
512 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
513 DecodeBitpOperand(Inst, Op3, Address, Decoder);
519 DecodeL3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
520 const void *Decoder) {
521 unsigned Op1, Op2, Op3;
523 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
524 if (S == MCDisassembler::Success) {
525 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
526 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
527 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
533 DecodeL3RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
534 const void *Decoder) {
535 unsigned Op1, Op2, Op3;
537 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
538 if (S == MCDisassembler::Success) {
539 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
540 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
541 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
542 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
548 DecodeL2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
549 const void *Decoder) {
550 unsigned Op1, Op2, Op3;
552 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
553 if (S == MCDisassembler::Success) {
554 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
555 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
556 Inst.addOperand(MCOperand::CreateImm(Op3));
562 DecodeL2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
563 const void *Decoder) {
564 unsigned Op1, Op2, Op3;
566 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
567 if (S == MCDisassembler::Success) {
568 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
569 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
570 DecodeBitpOperand(Inst, Op3, Address, Decoder);
575 MCDisassembler::DecodeStatus
576 XCoreDisassembler::getInstruction(MCInst &instr,
578 const MemoryObject &Region,
580 raw_ostream &vStream,
581 raw_ostream &cStream) const {
584 if (!readInstruction16(Region, Address, Size, insn16)) {
588 // Calling the auto-generated decoder function.
589 DecodeStatus Result = decodeInstruction(DecoderTable16, instr, insn16,
591 if (Result != Fail) {
598 if (!readInstruction32(Region, Address, Size, insn32)) {
602 // Calling the auto-generated decoder function.
603 Result = decodeInstruction(DecoderTable32, instr, insn32, Address, this, STI);
604 if (Result != Fail) {
613 extern Target TheXCoreTarget;
616 static MCDisassembler *createXCoreDisassembler(const Target &T,
617 const MCSubtargetInfo &STI) {
618 return new XCoreDisassembler(STI, T.createMCRegInfo(""));
621 extern "C" void LLVMInitializeXCoreDisassembler() {
622 // Register the disassembler.
623 TargetRegistry::RegisterMCDisassembler(TheXCoreTarget,
624 createXCoreDisassembler);