0acfb3ec2a08ffc7bf80829834b16765bc4b46e2
[oota-llvm.git] / lib / Target / MBlaze / Disassembler / MBlazeDisassembler.cpp
1 //===-- MBlazeDisassembler.cpp - Disassembler for MicroBlaze  -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is part of the MBlaze Disassembler. It contains code to translate
11 // the data produced by the decoder into MCInsts.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "MBlazeDisassembler.h"
16 #include "MBlaze.h"
17 #include "llvm/MC/MCDisassembler.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCInstrDesc.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/MemoryObject.h"
22 #include "llvm/Support/TargetRegistry.h"
23 #include "llvm/Support/raw_ostream.h"
24
25 // #include "MBlazeGenDecoderTables.inc"
26 // #include "MBlazeGenRegisterNames.inc"
27
28 namespace llvm {
29 extern const MCInstrDesc MBlazeInsts[];
30 }
31
32 using namespace llvm;
33
34 const uint16_t UNSUPPORTED = -1;
35
36 static const uint16_t mblazeBinary2Opcode[] = {
37   MBlaze::ADD,   MBlaze::RSUB,   MBlaze::ADDC,   MBlaze::RSUBC,   //00,01,02,03
38   MBlaze::ADDK,  MBlaze::RSUBK,  MBlaze::ADDKC,  MBlaze::RSUBKC,  //04,05,06,07
39   MBlaze::ADDI,  MBlaze::RSUBI,  MBlaze::ADDIC,  MBlaze::RSUBIC,  //08,09,0A,0B
40   MBlaze::ADDIK, MBlaze::RSUBIK, MBlaze::ADDIKC, MBlaze::RSUBIKC, //0C,0D,0E,0F
41
42   MBlaze::MUL,   MBlaze::BSRL,   MBlaze::IDIV,   MBlaze::GETD,    //10,11,12,13
43   UNSUPPORTED,   UNSUPPORTED,    MBlaze::FADD,   UNSUPPORTED,     //14,15,16,17
44   MBlaze::MULI,  MBlaze::BSRLI,  UNSUPPORTED,    MBlaze::GET,     //18,19,1A,1B
45   UNSUPPORTED,   UNSUPPORTED,    UNSUPPORTED,    UNSUPPORTED,     //1C,1D,1E,1F
46
47   MBlaze::OR,    MBlaze::AND,    MBlaze::XOR,    MBlaze::ANDN,    //20,21,22,23
48   MBlaze::SEXT8, MBlaze::MFS,    MBlaze::BR,     MBlaze::BEQ,     //24,25,26,27
49   MBlaze::ORI,   MBlaze::ANDI,   MBlaze::XORI,   MBlaze::ANDNI,   //28,29,2A,2B
50   MBlaze::IMM,   MBlaze::RTSD,   MBlaze::BRI,    MBlaze::BEQI,    //2C,2D,2E,2F
51
52   MBlaze::LBU,   MBlaze::LHU,    MBlaze::LW,     UNSUPPORTED,     //30,31,32,33
53   MBlaze::SB,    MBlaze::SH,     MBlaze::SW,     UNSUPPORTED,     //34,35,36,37
54   MBlaze::LBUI,  MBlaze::LHUI,   MBlaze::LWI,    UNSUPPORTED,     //38,39,3A,3B
55   MBlaze::SBI,   MBlaze::SHI,    MBlaze::SWI,    UNSUPPORTED,     //3C,3D,3E,3F
56 };
57
58 static unsigned getRD(uint32_t insn) {
59   if (!isMBlazeRegister((insn>>21)&0x1F))
60     return UNSUPPORTED;
61   return getMBlazeRegisterFromNumbering((insn>>21)&0x1F);
62 }
63
64 static unsigned getRA(uint32_t insn) {
65   if (!getMBlazeRegisterFromNumbering((insn>>16)&0x1F))
66     return UNSUPPORTED;
67   return getMBlazeRegisterFromNumbering((insn>>16)&0x1F);
68 }
69
70 static unsigned getRB(uint32_t insn) {
71   if (!getMBlazeRegisterFromNumbering((insn>>11)&0x1F))
72     return UNSUPPORTED;
73   return getMBlazeRegisterFromNumbering((insn>>11)&0x1F);
74 }
75
76 static int64_t getRS(uint32_t insn) {
77   if (!isSpecialMBlazeRegister(insn&0x3FFF))
78     return UNSUPPORTED;
79   return getSpecialMBlazeRegisterFromNumbering(insn&0x3FFF);
80 }
81
82 static int64_t getIMM(uint32_t insn) {
83     int16_t val = (insn & 0xFFFF);
84     return val;
85 }
86
87 static int64_t getSHT(uint32_t insn) {
88     int16_t val = (insn & 0x1F);
89     return val;
90 }
91
92 static unsigned getFLAGS(int32_t insn) {
93     return (insn & 0x7FF);
94 }
95
96 static int64_t getFSL(uint32_t insn) {
97     int16_t val = (insn & 0xF);
98     return val;
99 }
100
101 static unsigned decodeMUL(uint32_t insn) {
102     switch (getFLAGS(insn)) {
103     default: return UNSUPPORTED;
104     case 0:  return MBlaze::MUL;
105     case 1:  return MBlaze::MULH;
106     case 2:  return MBlaze::MULHSU;
107     case 3:  return MBlaze::MULHU;
108     }
109 }
110
111 static unsigned decodeSEXT(uint32_t insn) {
112     switch (insn&0x7FF) {
113     default:   return UNSUPPORTED;
114     case 0x60: return MBlaze::SEXT8;
115     case 0x68: return MBlaze::WIC;
116     case 0x64: return MBlaze::WDC;
117     case 0x66: return MBlaze::WDCC;
118     case 0x74: return MBlaze::WDCF;
119     case 0x61: return MBlaze::SEXT16;
120     case 0x41: return MBlaze::SRL;
121     case 0x21: return MBlaze::SRC;
122     case 0x01: return MBlaze::SRA;
123     case 0xE0: return MBlaze::CLZ;
124     }
125 }
126
127 static unsigned decodeBEQ(uint32_t insn) {
128     switch ((insn>>21)&0x1F) {
129     default:    return UNSUPPORTED;
130     case 0x00:  return MBlaze::BEQ;
131     case 0x10:  return MBlaze::BEQD;
132     case 0x05:  return MBlaze::BGE;
133     case 0x15:  return MBlaze::BGED;
134     case 0x04:  return MBlaze::BGT;
135     case 0x14:  return MBlaze::BGTD;
136     case 0x03:  return MBlaze::BLE;
137     case 0x13:  return MBlaze::BLED;
138     case 0x02:  return MBlaze::BLT;
139     case 0x12:  return MBlaze::BLTD;
140     case 0x01:  return MBlaze::BNE;
141     case 0x11:  return MBlaze::BNED;
142     }
143 }
144
145 static unsigned decodeBEQI(uint32_t insn) {
146     switch ((insn>>21)&0x1F) {
147     default:    return UNSUPPORTED;
148     case 0x00:  return MBlaze::BEQI;
149     case 0x10:  return MBlaze::BEQID;
150     case 0x05:  return MBlaze::BGEI;
151     case 0x15:  return MBlaze::BGEID;
152     case 0x04:  return MBlaze::BGTI;
153     case 0x14:  return MBlaze::BGTID;
154     case 0x03:  return MBlaze::BLEI;
155     case 0x13:  return MBlaze::BLEID;
156     case 0x02:  return MBlaze::BLTI;
157     case 0x12:  return MBlaze::BLTID;
158     case 0x01:  return MBlaze::BNEI;
159     case 0x11:  return MBlaze::BNEID;
160     }
161 }
162
163 static unsigned decodeBR(uint32_t insn) {
164     switch ((insn>>16)&0x1F) {
165     default:   return UNSUPPORTED;
166     case 0x00: return MBlaze::BR;
167     case 0x08: return MBlaze::BRA;
168     case 0x0C: return MBlaze::BRK;
169     case 0x10: return MBlaze::BRD;
170     case 0x14: return MBlaze::BRLD;
171     case 0x18: return MBlaze::BRAD;
172     case 0x1C: return MBlaze::BRALD;
173     }
174 }
175
176 static unsigned decodeBRI(uint32_t insn) {
177     switch (insn&0x3FFFFFF) {
178     default:        break;
179     case 0x0020004: return MBlaze::IDMEMBAR;
180     case 0x0220004: return MBlaze::DMEMBAR;
181     case 0x0420004: return MBlaze::IMEMBAR;
182     }
183
184     switch ((insn>>16)&0x1F) {
185     default:   return UNSUPPORTED;
186     case 0x00: return MBlaze::BRI;
187     case 0x08: return MBlaze::BRAI;
188     case 0x0C: return MBlaze::BRKI;
189     case 0x10: return MBlaze::BRID;
190     case 0x14: return MBlaze::BRLID;
191     case 0x18: return MBlaze::BRAID;
192     case 0x1C: return MBlaze::BRALID;
193     }
194 }
195
196 static unsigned decodeBSRL(uint32_t insn) {
197     switch ((insn>>9)&0x3) {
198     default:  return UNSUPPORTED;
199     case 0x2: return MBlaze::BSLL;
200     case 0x1: return MBlaze::BSRA;
201     case 0x0: return MBlaze::BSRL;
202     }
203 }
204
205 static unsigned decodeBSRLI(uint32_t insn) {
206     switch ((insn>>9)&0x3) {
207     default:  return UNSUPPORTED;
208     case 0x2: return MBlaze::BSLLI;
209     case 0x1: return MBlaze::BSRAI;
210     case 0x0: return MBlaze::BSRLI;
211     }
212 }
213
214 static unsigned decodeRSUBK(uint32_t insn) {
215     switch (getFLAGS(insn)) {
216     default:  return UNSUPPORTED;
217     case 0x0: return MBlaze::RSUBK;
218     case 0x1: return MBlaze::CMP;
219     case 0x3: return MBlaze::CMPU;
220     }
221 }
222
223 static unsigned decodeFADD(uint32_t insn) {
224     switch (getFLAGS(insn)) {
225     default:    return UNSUPPORTED;
226     case 0x000: return MBlaze::FADD;
227     case 0x080: return MBlaze::FRSUB;
228     case 0x100: return MBlaze::FMUL;
229     case 0x180: return MBlaze::FDIV;
230     case 0x200: return MBlaze::FCMP_UN;
231     case 0x210: return MBlaze::FCMP_LT;
232     case 0x220: return MBlaze::FCMP_EQ;
233     case 0x230: return MBlaze::FCMP_LE;
234     case 0x240: return MBlaze::FCMP_GT;
235     case 0x250: return MBlaze::FCMP_NE;
236     case 0x260: return MBlaze::FCMP_GE;
237     case 0x280: return MBlaze::FLT;
238     case 0x300: return MBlaze::FINT;
239     case 0x380: return MBlaze::FSQRT;
240     }
241 }
242
243 static unsigned decodeGET(uint32_t insn) {
244     switch ((insn>>10)&0x3F) {
245     default:   return UNSUPPORTED;
246     case 0x00: return MBlaze::GET;
247     case 0x01: return MBlaze::EGET;
248     case 0x02: return MBlaze::AGET;
249     case 0x03: return MBlaze::EAGET;
250     case 0x04: return MBlaze::TGET;
251     case 0x05: return MBlaze::TEGET;
252     case 0x06: return MBlaze::TAGET;
253     case 0x07: return MBlaze::TEAGET;
254     case 0x08: return MBlaze::CGET;
255     case 0x09: return MBlaze::ECGET;
256     case 0x0A: return MBlaze::CAGET;
257     case 0x0B: return MBlaze::ECAGET;
258     case 0x0C: return MBlaze::TCGET;
259     case 0x0D: return MBlaze::TECGET;
260     case 0x0E: return MBlaze::TCAGET;
261     case 0x0F: return MBlaze::TECAGET;
262     case 0x10: return MBlaze::NGET;
263     case 0x11: return MBlaze::NEGET;
264     case 0x12: return MBlaze::NAGET;
265     case 0x13: return MBlaze::NEAGET;
266     case 0x14: return MBlaze::TNGET;
267     case 0x15: return MBlaze::TNEGET;
268     case 0x16: return MBlaze::TNAGET;
269     case 0x17: return MBlaze::TNEAGET;
270     case 0x18: return MBlaze::NCGET;
271     case 0x19: return MBlaze::NECGET;
272     case 0x1A: return MBlaze::NCAGET;
273     case 0x1B: return MBlaze::NECAGET;
274     case 0x1C: return MBlaze::TNCGET;
275     case 0x1D: return MBlaze::TNECGET;
276     case 0x1E: return MBlaze::TNCAGET;
277     case 0x1F: return MBlaze::TNECAGET;
278     case 0x20: return MBlaze::PUT;
279     case 0x22: return MBlaze::APUT;
280     case 0x24: return MBlaze::TPUT;
281     case 0x26: return MBlaze::TAPUT;
282     case 0x28: return MBlaze::CPUT;
283     case 0x2A: return MBlaze::CAPUT;
284     case 0x2C: return MBlaze::TCPUT;
285     case 0x2E: return MBlaze::TCAPUT;
286     case 0x30: return MBlaze::NPUT;
287     case 0x32: return MBlaze::NAPUT;
288     case 0x34: return MBlaze::TNPUT;
289     case 0x36: return MBlaze::TNAPUT;
290     case 0x38: return MBlaze::NCPUT;
291     case 0x3A: return MBlaze::NCAPUT;
292     case 0x3C: return MBlaze::TNCPUT;
293     case 0x3E: return MBlaze::TNCAPUT;
294     }
295 }
296
297 static unsigned decodeGETD(uint32_t insn) {
298     switch ((insn>>5)&0x3F) {
299     default:   return UNSUPPORTED;
300     case 0x00: return MBlaze::GETD;
301     case 0x01: return MBlaze::EGETD;
302     case 0x02: return MBlaze::AGETD;
303     case 0x03: return MBlaze::EAGETD;
304     case 0x04: return MBlaze::TGETD;
305     case 0x05: return MBlaze::TEGETD;
306     case 0x06: return MBlaze::TAGETD;
307     case 0x07: return MBlaze::TEAGETD;
308     case 0x08: return MBlaze::CGETD;
309     case 0x09: return MBlaze::ECGETD;
310     case 0x0A: return MBlaze::CAGETD;
311     case 0x0B: return MBlaze::ECAGETD;
312     case 0x0C: return MBlaze::TCGETD;
313     case 0x0D: return MBlaze::TECGETD;
314     case 0x0E: return MBlaze::TCAGETD;
315     case 0x0F: return MBlaze::TECAGETD;
316     case 0x10: return MBlaze::NGETD;
317     case 0x11: return MBlaze::NEGETD;
318     case 0x12: return MBlaze::NAGETD;
319     case 0x13: return MBlaze::NEAGETD;
320     case 0x14: return MBlaze::TNGETD;
321     case 0x15: return MBlaze::TNEGETD;
322     case 0x16: return MBlaze::TNAGETD;
323     case 0x17: return MBlaze::TNEAGETD;
324     case 0x18: return MBlaze::NCGETD;
325     case 0x19: return MBlaze::NECGETD;
326     case 0x1A: return MBlaze::NCAGETD;
327     case 0x1B: return MBlaze::NECAGETD;
328     case 0x1C: return MBlaze::TNCGETD;
329     case 0x1D: return MBlaze::TNECGETD;
330     case 0x1E: return MBlaze::TNCAGETD;
331     case 0x1F: return MBlaze::TNECAGETD;
332     case 0x20: return MBlaze::PUTD;
333     case 0x22: return MBlaze::APUTD;
334     case 0x24: return MBlaze::TPUTD;
335     case 0x26: return MBlaze::TAPUTD;
336     case 0x28: return MBlaze::CPUTD;
337     case 0x2A: return MBlaze::CAPUTD;
338     case 0x2C: return MBlaze::TCPUTD;
339     case 0x2E: return MBlaze::TCAPUTD;
340     case 0x30: return MBlaze::NPUTD;
341     case 0x32: return MBlaze::NAPUTD;
342     case 0x34: return MBlaze::TNPUTD;
343     case 0x36: return MBlaze::TNAPUTD;
344     case 0x38: return MBlaze::NCPUTD;
345     case 0x3A: return MBlaze::NCAPUTD;
346     case 0x3C: return MBlaze::TNCPUTD;
347     case 0x3E: return MBlaze::TNCAPUTD;
348     }
349 }
350
351 static unsigned decodeIDIV(uint32_t insn) {
352     switch (insn&0x3) {
353     default:  return UNSUPPORTED;
354     case 0x0: return MBlaze::IDIV;
355     case 0x2: return MBlaze::IDIVU;
356     }
357 }
358
359 static unsigned decodeLBU(uint32_t insn) {
360     switch ((insn>>9)&0x1) {
361     default:  return UNSUPPORTED;
362     case 0x0: return MBlaze::LBU;
363     case 0x1: return MBlaze::LBUR;
364     }
365 }
366
367 static unsigned decodeLHU(uint32_t insn) {
368     switch ((insn>>9)&0x1) {
369     default:  return UNSUPPORTED;
370     case 0x0: return MBlaze::LHU;
371     case 0x1: return MBlaze::LHUR;
372     }
373 }
374
375 static unsigned decodeLW(uint32_t insn) {
376     switch ((insn>>9)&0x3) {
377     default:  return UNSUPPORTED;
378     case 0x0: return MBlaze::LW;
379     case 0x1: return MBlaze::LWR;
380     case 0x2: return MBlaze::LWX;
381     }
382 }
383
384 static unsigned decodeSB(uint32_t insn) {
385     switch ((insn>>9)&0x1) {
386     default:  return UNSUPPORTED;
387     case 0x0: return MBlaze::SB;
388     case 0x1: return MBlaze::SBR;
389     }
390 }
391
392 static unsigned decodeSH(uint32_t insn) {
393     switch ((insn>>9)&0x1) {
394     default:  return UNSUPPORTED;
395     case 0x0: return MBlaze::SH;
396     case 0x1: return MBlaze::SHR;
397     }
398 }
399
400 static unsigned decodeSW(uint32_t insn) {
401     switch ((insn>>9)&0x3) {
402     default:  return UNSUPPORTED;
403     case 0x0: return MBlaze::SW;
404     case 0x1: return MBlaze::SWR;
405     case 0x2: return MBlaze::SWX;
406     }
407 }
408
409 static unsigned decodeMFS(uint32_t insn) {
410     switch ((insn>>15)&0x1) {
411     default:   return UNSUPPORTED;
412     case 0x0:
413       switch ((insn>>16)&0x1) {
414       default:   return UNSUPPORTED;
415       case 0x0: return MBlaze::MSRSET;
416       case 0x1: return MBlaze::MSRCLR;
417       }
418     case 0x1:
419       switch ((insn>>14)&0x1) {
420       default:   return UNSUPPORTED;
421       case 0x0: return MBlaze::MFS;
422       case 0x1: return MBlaze::MTS;
423       }
424     }
425 }
426
427 static unsigned decodeOR(uint32_t insn) {
428     switch (getFLAGS(insn)) {
429     default:    return UNSUPPORTED;
430     case 0x000: return MBlaze::OR;
431     case 0x400: return MBlaze::PCMPBF;
432     }
433 }
434
435 static unsigned decodeXOR(uint32_t insn) {
436     switch (getFLAGS(insn)) {
437     default:    return UNSUPPORTED;
438     case 0x000: return MBlaze::XOR;
439     case 0x400: return MBlaze::PCMPEQ;
440     }
441 }
442
443 static unsigned decodeANDN(uint32_t insn) {
444     switch (getFLAGS(insn)) {
445     default:    return UNSUPPORTED;
446     case 0x000: return MBlaze::ANDN;
447     case 0x400: return MBlaze::PCMPNE;
448     }
449 }
450
451 static unsigned decodeRTSD(uint32_t insn) {
452     switch ((insn>>21)&0x1F) {
453     default:   return UNSUPPORTED;
454     case 0x10: return MBlaze::RTSD;
455     case 0x11: return MBlaze::RTID;
456     case 0x12: return MBlaze::RTBD;
457     case 0x14: return MBlaze::RTED;
458     }
459 }
460
461 static unsigned getOPCODE(uint32_t insn) {
462   unsigned opcode = mblazeBinary2Opcode[ (insn>>26)&0x3F ];
463   switch (opcode) {
464   case MBlaze::MUL:     return decodeMUL(insn);
465   case MBlaze::SEXT8:   return decodeSEXT(insn);
466   case MBlaze::BEQ:     return decodeBEQ(insn);
467   case MBlaze::BEQI:    return decodeBEQI(insn);
468   case MBlaze::BR:      return decodeBR(insn);
469   case MBlaze::BRI:     return decodeBRI(insn);
470   case MBlaze::BSRL:    return decodeBSRL(insn);
471   case MBlaze::BSRLI:   return decodeBSRLI(insn);
472   case MBlaze::RSUBK:   return decodeRSUBK(insn);
473   case MBlaze::FADD:    return decodeFADD(insn);
474   case MBlaze::GET:     return decodeGET(insn);
475   case MBlaze::GETD:    return decodeGETD(insn);
476   case MBlaze::IDIV:    return decodeIDIV(insn);
477   case MBlaze::LBU:     return decodeLBU(insn);
478   case MBlaze::LHU:     return decodeLHU(insn);
479   case MBlaze::LW:      return decodeLW(insn);
480   case MBlaze::SB:      return decodeSB(insn);
481   case MBlaze::SH:      return decodeSH(insn);
482   case MBlaze::SW:      return decodeSW(insn);
483   case MBlaze::MFS:     return decodeMFS(insn);
484   case MBlaze::OR:      return decodeOR(insn);
485   case MBlaze::XOR:     return decodeXOR(insn);
486   case MBlaze::ANDN:    return decodeANDN(insn);
487   case MBlaze::RTSD:    return decodeRTSD(insn);
488   default:              return opcode;
489   }
490 }
491
492 //
493 // Public interface for the disassembler
494 //
495
496 MCDisassembler::DecodeStatus MBlazeDisassembler::getInstruction(MCInst &instr,
497                                         uint64_t &size,
498                                         const MemoryObject &region,
499                                         uint64_t address,
500                                         raw_ostream &vStream,
501                                         raw_ostream &cStream) const {
502   // The machine instruction.
503   uint32_t insn;
504   uint8_t bytes[4];
505
506   // By default we consume 1 byte on failure
507   size = 1;
508
509   // We want to read exactly 4 bytes of data.
510   if (region.readBytes(address, 4, bytes) == -1)
511     return Fail;
512
513   // Encoded as a big-endian 32-bit word in the stream.
514   insn = (bytes[0]<<24) | (bytes[1]<<16) | (bytes[2]<< 8) | (bytes[3]<<0);
515
516   // Get the MCInst opcode from the binary instruction and make sure
517   // that it is a valid instruction.
518   unsigned opcode = getOPCODE(insn);
519   if (opcode == UNSUPPORTED)
520     return Fail;
521
522   instr.setOpcode(opcode);
523
524   unsigned RD = getRD(insn);
525   unsigned RA = getRA(insn);
526   unsigned RB = getRB(insn);
527   unsigned RS = getRS(insn);
528
529   uint64_t tsFlags = MBlazeInsts[opcode].TSFlags;
530   switch ((tsFlags & MBlazeII::FormMask)) {
531   default: 
532     return Fail;
533
534   case MBlazeII::FC:
535     break;
536
537   case MBlazeII::FRRRR:
538     if (RD == UNSUPPORTED || RA == UNSUPPORTED || RB == UNSUPPORTED)
539       return Fail;
540     instr.addOperand(MCOperand::CreateReg(RD));
541     instr.addOperand(MCOperand::CreateReg(RB));
542     instr.addOperand(MCOperand::CreateReg(RA));
543     break;
544
545   case MBlazeII::FRRR:
546     if (RD == UNSUPPORTED || RA == UNSUPPORTED || RB == UNSUPPORTED)
547       return Fail;
548     instr.addOperand(MCOperand::CreateReg(RD));
549     instr.addOperand(MCOperand::CreateReg(RA));
550     instr.addOperand(MCOperand::CreateReg(RB));
551     break;
552
553   case MBlazeII::FRR:
554     if (RD == UNSUPPORTED || RA == UNSUPPORTED)
555       return Fail;
556     instr.addOperand(MCOperand::CreateReg(RD));
557     instr.addOperand(MCOperand::CreateReg(RA));
558     break;
559
560   case MBlazeII::FRI:
561     switch (opcode) {
562     default: 
563       return Fail;
564     case MBlaze::MFS:
565       if (RD == UNSUPPORTED)
566         return Fail;
567       instr.addOperand(MCOperand::CreateReg(RD));
568       instr.addOperand(MCOperand::CreateImm(insn&0x3FFF));
569       break;
570     case MBlaze::MTS:
571       if (RA == UNSUPPORTED)
572         return Fail;
573       instr.addOperand(MCOperand::CreateImm(insn&0x3FFF));
574       instr.addOperand(MCOperand::CreateReg(RA));
575       break;
576     case MBlaze::MSRSET:
577     case MBlaze::MSRCLR:
578       if (RD == UNSUPPORTED)
579         return Fail;
580       instr.addOperand(MCOperand::CreateReg(RD));
581       instr.addOperand(MCOperand::CreateImm(insn&0x7FFF));
582       break;
583     }
584     break;
585
586   case MBlazeII::FRRI:
587     if (RD == UNSUPPORTED || RA == UNSUPPORTED)
588       return Fail;
589     instr.addOperand(MCOperand::CreateReg(RD));
590     instr.addOperand(MCOperand::CreateReg(RA));
591     switch (opcode) {
592     default:
593       instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
594       break;
595     case MBlaze::BSRLI:
596     case MBlaze::BSRAI:
597     case MBlaze::BSLLI:
598       instr.addOperand(MCOperand::CreateImm(insn&0x1F));
599       break;
600     }
601     break;
602
603   case MBlazeII::FCRR:
604     if (RA == UNSUPPORTED || RB == UNSUPPORTED)
605       return Fail;
606     instr.addOperand(MCOperand::CreateReg(RA));
607     instr.addOperand(MCOperand::CreateReg(RB));
608     break;
609
610   case MBlazeII::FCRI:
611     if (RA == UNSUPPORTED)
612       return Fail;
613     instr.addOperand(MCOperand::CreateReg(RA));
614     instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
615     break;
616
617   case MBlazeII::FRCR:
618     if (RD == UNSUPPORTED || RB == UNSUPPORTED)
619       return Fail;
620     instr.addOperand(MCOperand::CreateReg(RD));
621     instr.addOperand(MCOperand::CreateReg(RB));
622     break;
623
624   case MBlazeII::FRCI:
625     if (RD == UNSUPPORTED)
626       return Fail;
627     instr.addOperand(MCOperand::CreateReg(RD));
628     instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
629     break;
630
631   case MBlazeII::FCCR:
632     if (RB == UNSUPPORTED)
633       return Fail;
634     instr.addOperand(MCOperand::CreateReg(RB));
635     break;
636
637   case MBlazeII::FCCI:
638     instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
639     break;
640
641   case MBlazeII::FRRCI:
642     if (RD == UNSUPPORTED || RA == UNSUPPORTED)
643       return Fail;
644     instr.addOperand(MCOperand::CreateReg(RD));
645     instr.addOperand(MCOperand::CreateReg(RA));
646     instr.addOperand(MCOperand::CreateImm(getSHT(insn)));
647     break;
648
649   case MBlazeII::FRRC:
650     if (RD == UNSUPPORTED || RA == UNSUPPORTED)
651       return Fail;
652     instr.addOperand(MCOperand::CreateReg(RD));
653     instr.addOperand(MCOperand::CreateReg(RA));
654     break;
655
656   case MBlazeII::FRCX:
657     if (RD == UNSUPPORTED)
658       return Fail;
659     instr.addOperand(MCOperand::CreateReg(RD));
660     instr.addOperand(MCOperand::CreateImm(getFSL(insn)));
661     break;
662
663   case MBlazeII::FRCS:
664     if (RD == UNSUPPORTED || RS == UNSUPPORTED)
665       return Fail;
666     instr.addOperand(MCOperand::CreateReg(RD));
667     instr.addOperand(MCOperand::CreateReg(RS));
668     break;
669
670   case MBlazeII::FCRCS:
671     if (RS == UNSUPPORTED || RA == UNSUPPORTED)
672       return Fail;
673     instr.addOperand(MCOperand::CreateReg(RS));
674     instr.addOperand(MCOperand::CreateReg(RA));
675     break;
676
677   case MBlazeII::FCRCX:
678     if (RA == UNSUPPORTED)
679       return Fail;
680     instr.addOperand(MCOperand::CreateReg(RA));
681     instr.addOperand(MCOperand::CreateImm(getFSL(insn)));
682     break;
683
684   case MBlazeII::FCX:
685     instr.addOperand(MCOperand::CreateImm(getFSL(insn)));
686     break;
687
688   case MBlazeII::FCR:
689     if (RB == UNSUPPORTED)
690       return Fail;
691     instr.addOperand(MCOperand::CreateReg(RB));
692     break;
693
694   case MBlazeII::FRIR:
695     if (RD == UNSUPPORTED || RA == UNSUPPORTED)
696       return Fail;
697     instr.addOperand(MCOperand::CreateReg(RD));
698     instr.addOperand(MCOperand::CreateImm(getIMM(insn)));
699     instr.addOperand(MCOperand::CreateReg(RA));
700     break;
701   }
702
703   // We always consume 4 bytes of data on success
704   size = 4;
705
706   return Success;
707 }
708
709 static MCDisassembler *createMBlazeDisassembler(const Target &T,
710                                                 const MCSubtargetInfo &STI) {
711   return new MBlazeDisassembler(STI);
712 }
713
714 extern "C" void LLVMInitializeMBlazeDisassembler() {
715   // Register the disassembler.
716   TargetRegistry::RegisterMCDisassembler(TheMBlazeTarget,
717                                          createMBlazeDisassembler);
718 }