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 static DecodeStatus DecodeL3RInstruction(MCInst &Inst,
153 const void *Decoder);
155 static DecodeStatus DecodeL3RSrcDstInstruction(MCInst &Inst,
158 const void *Decoder);
160 static DecodeStatus DecodeL2RUSInstruction(MCInst &Inst,
163 const void *Decoder);
165 static DecodeStatus DecodeL2RUSBitpInstruction(MCInst &Inst,
168 const void *Decoder);
170 #include "XCoreGenDisassemblerTables.inc"
172 static DecodeStatus DecodeGRRegsRegisterClass(MCInst &Inst,
178 return MCDisassembler::Fail;
179 unsigned Reg = getReg(Decoder, XCore::GRRegsRegClassID, RegNo);
180 Inst.addOperand(MCOperand::CreateReg(Reg));
181 return MCDisassembler::Success;
184 static DecodeStatus DecodeBitpOperand(MCInst &Inst, unsigned Val,
185 uint64_t Address, const void *Decoder) {
187 return MCDisassembler::Fail;
188 static unsigned Values[] = {
189 32 /*bpw*/, 1, 2, 3, 4, 5, 6, 7, 8, 16, 24, 32
191 Inst.addOperand(MCOperand::CreateImm(Values[Val]));
192 return MCDisassembler::Success;
196 Decode2OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2) {
197 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
199 return MCDisassembler::Fail;
200 if (fieldFromInstruction(Insn, 5, 1)) {
202 return MCDisassembler::Fail;
206 unsigned Op1High = Combined % 3;
207 unsigned Op2High = Combined / 3;
208 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 2, 2);
209 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 0, 2);
210 return MCDisassembler::Success;
214 Decode3OpInstruction(unsigned Insn, unsigned &Op1, unsigned &Op2,
216 unsigned Combined = fieldFromInstruction(Insn, 6, 5);
218 return MCDisassembler::Fail;
220 unsigned Op1High = Combined % 3;
221 unsigned Op2High = (Combined / 3) % 3;
222 unsigned Op3High = Combined / 9;
223 Op1 = (Op1High << 2) | fieldFromInstruction(Insn, 4, 2);
224 Op2 = (Op2High << 2) | fieldFromInstruction(Insn, 2, 2);
225 Op3 = (Op3High << 2) | fieldFromInstruction(Insn, 0, 2);
226 return MCDisassembler::Success;
230 Decode2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
231 const void *Decoder) {
232 // Try and decode as a 3R instruction.
233 unsigned Opcode = fieldFromInstruction(Insn, 11, 5);
236 Inst.setOpcode(XCore::STW_2rus);
237 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
239 Inst.setOpcode(XCore::LDW_2rus);
240 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
242 Inst.setOpcode(XCore::ADD_3r);
243 return Decode3RInstruction(Inst, Insn, Address, Decoder);
245 Inst.setOpcode(XCore::SUB_3r);
246 return Decode3RInstruction(Inst, Insn, Address, Decoder);
248 Inst.setOpcode(XCore::SHL_3r);
249 return Decode3RInstruction(Inst, Insn, Address, Decoder);
251 Inst.setOpcode(XCore::SHR_3r);
252 return Decode3RInstruction(Inst, Insn, Address, Decoder);
254 Inst.setOpcode(XCore::EQ_3r);
255 return Decode3RInstruction(Inst, Insn, Address, Decoder);
257 Inst.setOpcode(XCore::AND_3r);
258 return Decode3RInstruction(Inst, Insn, Address, Decoder);
260 Inst.setOpcode(XCore::OR_3r);
261 return Decode3RInstruction(Inst, Insn, Address, Decoder);
263 Inst.setOpcode(XCore::LDW_3r);
264 return Decode3RInstruction(Inst, Insn, Address, Decoder);
266 Inst.setOpcode(XCore::LD16S_3r);
267 return Decode3RInstruction(Inst, Insn, Address, Decoder);
269 Inst.setOpcode(XCore::LD8U_3r);
270 return Decode3RInstruction(Inst, Insn, Address, Decoder);
272 Inst.setOpcode(XCore::ADD_2rus);
273 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
275 Inst.setOpcode(XCore::SUB_2rus);
276 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
278 Inst.setOpcode(XCore::SHL_2rus);
279 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
281 Inst.setOpcode(XCore::SHR_2rus);
282 return Decode2RUSBitpInstruction(Inst, Insn, Address, Decoder);
284 Inst.setOpcode(XCore::EQ_2rus);
285 return Decode2RUSInstruction(Inst, Insn, Address, Decoder);
287 Inst.setOpcode(XCore::LSS_3r);
288 return Decode3RInstruction(Inst, Insn, Address, Decoder);
290 Inst.setOpcode(XCore::LSU_3r);
291 return Decode3RInstruction(Inst, Insn, Address, Decoder);
293 return MCDisassembler::Fail;
297 Decode2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
298 const void *Decoder) {
300 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
301 if (S != MCDisassembler::Success)
302 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
304 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
305 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
310 DecodeR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
311 const void *Decoder) {
313 DecodeStatus S = Decode2OpInstruction(Insn, Op2, Op1);
314 if (S != MCDisassembler::Success)
315 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
317 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
318 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
323 Decode2RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
324 const void *Decoder) {
326 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
327 if (S != MCDisassembler::Success)
328 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
330 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
331 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
332 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
337 DecodeRUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
338 const void *Decoder) {
340 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
341 if (S != MCDisassembler::Success)
342 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
344 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
345 Inst.addOperand(MCOperand::CreateImm(Op2));
350 DecodeRUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
351 const void *Decoder) {
353 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
354 if (S != MCDisassembler::Success)
355 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
357 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
358 DecodeBitpOperand(Inst, Op2, Address, Decoder);
363 DecodeRUSSrcDstBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
364 const void *Decoder) {
366 DecodeStatus S = Decode2OpInstruction(Insn, Op1, Op2);
367 if (S != MCDisassembler::Success)
368 return Decode2OpInstructionFail(Inst, Insn, Address, Decoder);
370 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
371 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
372 DecodeBitpOperand(Inst, Op2, Address, Decoder);
377 DecodeL2OpInstructionFail(MCInst &Inst, unsigned Insn, uint64_t Address,
378 const void *Decoder) {
379 // Try and decode as a L3R / L2RUS instruction.
380 unsigned Opcode = fieldFromInstruction(Insn, 16, 4) |
381 fieldFromInstruction(Insn, 27, 5) << 4;
384 Inst.setOpcode(XCore::STW_3r);
385 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
387 Inst.setOpcode(XCore::XOR_l3r);
388 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
390 Inst.setOpcode(XCore::ASHR_l3r);
391 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
393 Inst.setOpcode(XCore::LDAWF_l3r);
394 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
396 Inst.setOpcode(XCore::LDAWB_l3r);
397 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
399 Inst.setOpcode(XCore::LDA16F_l3r);
400 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
402 Inst.setOpcode(XCore::LDA16B_l3r);
403 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
405 Inst.setOpcode(XCore::MUL_l3r);
406 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
408 Inst.setOpcode(XCore::DIVS_l3r);
409 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
411 Inst.setOpcode(XCore::DIVU_l3r);
412 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
414 Inst.setOpcode(XCore::ST16_l3r);
415 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
417 Inst.setOpcode(XCore::ST8_l3r);
418 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
420 Inst.setOpcode(XCore::ASHR_l2rus);
421 return DecodeL2RUSBitpInstruction(Inst, Insn, Address, Decoder);
423 Inst.setOpcode(XCore::LDAWF_l2rus);
424 return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
426 Inst.setOpcode(XCore::LDAWB_l2rus);
427 return DecodeL2RUSInstruction(Inst, Insn, Address, Decoder);
429 Inst.setOpcode(XCore::CRC_l3r);
430 return DecodeL3RSrcDstInstruction(Inst, Insn, Address, Decoder);
432 Inst.setOpcode(XCore::REMS_l3r);
433 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
435 Inst.setOpcode(XCore::REMU_l3r);
436 return DecodeL3RInstruction(Inst, Insn, Address, Decoder);
438 return MCDisassembler::Fail;
442 DecodeL2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
443 const void *Decoder) {
445 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
447 if (S != MCDisassembler::Success)
448 return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
450 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
451 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
456 DecodeLR2RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
457 const void *Decoder) {
459 DecodeStatus S = Decode2OpInstruction(fieldFromInstruction(Insn, 0, 16),
461 if (S != MCDisassembler::Success)
462 return DecodeL2OpInstructionFail(Inst, Insn, Address, Decoder);
464 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
465 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
470 Decode3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
471 const void *Decoder) {
472 unsigned Op1, Op2, Op3;
473 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
474 if (S == MCDisassembler::Success) {
475 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
476 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
477 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
483 Decode2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
484 const void *Decoder) {
485 unsigned Op1, Op2, Op3;
486 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
487 if (S == MCDisassembler::Success) {
488 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
489 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
490 Inst.addOperand(MCOperand::CreateImm(Op3));
496 Decode2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
497 const void *Decoder) {
498 unsigned Op1, Op2, Op3;
499 DecodeStatus S = Decode3OpInstruction(Insn, Op1, Op2, Op3);
500 if (S == MCDisassembler::Success) {
501 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
502 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
503 DecodeBitpOperand(Inst, Op3, Address, Decoder);
509 DecodeL3RInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
510 const void *Decoder) {
511 unsigned Op1, Op2, Op3;
513 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
514 if (S == MCDisassembler::Success) {
515 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
516 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
517 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
523 DecodeL3RSrcDstInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
524 const void *Decoder) {
525 unsigned Op1, Op2, Op3;
527 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
528 if (S == MCDisassembler::Success) {
529 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
530 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
531 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
532 DecodeGRRegsRegisterClass(Inst, Op3, Address, Decoder);
538 DecodeL2RUSInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
539 const void *Decoder) {
540 unsigned Op1, Op2, Op3;
542 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
543 if (S == MCDisassembler::Success) {
544 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
545 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
546 Inst.addOperand(MCOperand::CreateImm(Op3));
552 DecodeL2RUSBitpInstruction(MCInst &Inst, unsigned Insn, uint64_t Address,
553 const void *Decoder) {
554 unsigned Op1, Op2, Op3;
556 Decode3OpInstruction(fieldFromInstruction(Insn, 0, 16), Op1, Op2, Op3);
557 if (S == MCDisassembler::Success) {
558 DecodeGRRegsRegisterClass(Inst, Op1, Address, Decoder);
559 DecodeGRRegsRegisterClass(Inst, Op2, Address, Decoder);
560 DecodeBitpOperand(Inst, Op3, Address, Decoder);
565 MCDisassembler::DecodeStatus
566 XCoreDisassembler::getInstruction(MCInst &instr,
568 const MemoryObject &Region,
570 raw_ostream &vStream,
571 raw_ostream &cStream) const {
574 if (!readInstruction16(Region, Address, Size, insn16)) {
578 // Calling the auto-generated decoder function.
579 DecodeStatus Result = decodeInstruction(DecoderTable16, instr, insn16,
581 if (Result != Fail) {
588 if (!readInstruction32(Region, Address, Size, insn32)) {
592 // Calling the auto-generated decoder function.
593 Result = decodeInstruction(DecoderTable32, instr, insn32, Address, this, STI);
594 if (Result != Fail) {
603 extern Target TheXCoreTarget;
606 static MCDisassembler *createXCoreDisassembler(const Target &T,
607 const MCSubtargetInfo &STI) {
608 return new XCoreDisassembler(STI, T.createMCRegInfo(""));
611 extern "C" void LLVMInitializeXCoreDisassembler() {
612 // Register the disassembler.
613 TargetRegistry::RegisterMCDisassembler(TheXCoreTarget,
614 createXCoreDisassembler);