LLVM API Documentation

ARMAddressingModes.h
Go to the documentation of this file.
00001 //===-- ARMAddressingModes.h - ARM Addressing Modes -------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the ARM addressing mode implementation stuff.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_LIB_TARGET_ARM_MCTARGETDESC_ARMADDRESSINGMODES_H
00015 #define LLVM_LIB_TARGET_ARM_MCTARGETDESC_ARMADDRESSINGMODES_H
00016 
00017 #include "llvm/ADT/APFloat.h"
00018 #include "llvm/ADT/APInt.h"
00019 #include "llvm/Support/ErrorHandling.h"
00020 #include "llvm/Support/MathExtras.h"
00021 #include <cassert>
00022 
00023 namespace llvm {
00024 
00025 /// ARM_AM - ARM Addressing Mode Stuff
00026 namespace ARM_AM {
00027   enum ShiftOpc {
00028     no_shift = 0,
00029     asr,
00030     lsl,
00031     lsr,
00032     ror,
00033     rrx
00034   };
00035 
00036   enum AddrOpc {
00037     sub = 0,
00038     add
00039   };
00040 
00041   static inline const char *getAddrOpcStr(AddrOpc Op) {
00042     return Op == sub ? "-" : "";
00043   }
00044 
00045   static inline const char *getShiftOpcStr(ShiftOpc Op) {
00046     switch (Op) {
00047     default: llvm_unreachable("Unknown shift opc!");
00048     case ARM_AM::asr: return "asr";
00049     case ARM_AM::lsl: return "lsl";
00050     case ARM_AM::lsr: return "lsr";
00051     case ARM_AM::ror: return "ror";
00052     case ARM_AM::rrx: return "rrx";
00053     }
00054   }
00055 
00056   static inline unsigned getShiftOpcEncoding(ShiftOpc Op) {
00057     switch (Op) {
00058     default: llvm_unreachable("Unknown shift opc!");
00059     case ARM_AM::asr: return 2;
00060     case ARM_AM::lsl: return 0;
00061     case ARM_AM::lsr: return 1;
00062     case ARM_AM::ror: return 3;
00063     }
00064   }
00065 
00066   enum AMSubMode {
00067     bad_am_submode = 0,
00068     ia,
00069     ib,
00070     da,
00071     db
00072   };
00073 
00074   static inline const char *getAMSubModeStr(AMSubMode Mode) {
00075     switch (Mode) {
00076     default: llvm_unreachable("Unknown addressing sub-mode!");
00077     case ARM_AM::ia: return "ia";
00078     case ARM_AM::ib: return "ib";
00079     case ARM_AM::da: return "da";
00080     case ARM_AM::db: return "db";
00081     }
00082   }
00083 
00084   /// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits.
00085   ///
00086   static inline unsigned rotr32(unsigned Val, unsigned Amt) {
00087     assert(Amt < 32 && "Invalid rotate amount");
00088     return (Val >> Amt) | (Val << ((32-Amt)&31));
00089   }
00090 
00091   /// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits.
00092   ///
00093   static inline unsigned rotl32(unsigned Val, unsigned Amt) {
00094     assert(Amt < 32 && "Invalid rotate amount");
00095     return (Val << Amt) | (Val >> ((32-Amt)&31));
00096   }
00097 
00098   //===--------------------------------------------------------------------===//
00099   // Addressing Mode #1: shift_operand with registers
00100   //===--------------------------------------------------------------------===//
00101   //
00102   // This 'addressing mode' is used for arithmetic instructions.  It can
00103   // represent things like:
00104   //   reg
00105   //   reg [asr|lsl|lsr|ror|rrx] reg
00106   //   reg [asr|lsl|lsr|ror|rrx] imm
00107   //
00108   // This is stored three operands [rega, regb, opc].  The first is the base
00109   // reg, the second is the shift amount (or reg0 if not present or imm).  The
00110   // third operand encodes the shift opcode and the imm if a reg isn't present.
00111   //
00112   static inline unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm) {
00113     return ShOp | (Imm << 3);
00114   }
00115   static inline unsigned getSORegOffset(unsigned Op) {
00116     return Op >> 3;
00117   }
00118   static inline ShiftOpc getSORegShOp(unsigned Op) {
00119     return (ShiftOpc)(Op & 7);
00120   }
00121 
00122   /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return
00123   /// the 8-bit imm value.
00124   static inline unsigned getSOImmValImm(unsigned Imm) {
00125     return Imm & 0xFF;
00126   }
00127   /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return
00128   /// the rotate amount.
00129   static inline unsigned getSOImmValRot(unsigned Imm) {
00130     return (Imm >> 8) * 2;
00131   }
00132 
00133   /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,
00134   /// computing the rotate amount to use.  If this immediate value cannot be
00135   /// handled with a single shifter-op, determine a good rotate amount that will
00136   /// take a maximal chunk of bits out of the immediate.
00137   static inline unsigned getSOImmValRotate(unsigned Imm) {
00138     // 8-bit (or less) immediates are trivially shifter_operands with a rotate
00139     // of zero.
00140     if ((Imm & ~255U) == 0) return 0;
00141 
00142     // Use CTZ to compute the rotate amount.
00143     unsigned TZ = countTrailingZeros(Imm);
00144 
00145     // Rotate amount must be even.  Something like 0x200 must be rotated 8 bits,
00146     // not 9.
00147     unsigned RotAmt = TZ & ~1;
00148 
00149     // If we can handle this spread, return it.
00150     if ((rotr32(Imm, RotAmt) & ~255U) == 0)
00151       return (32-RotAmt)&31;  // HW rotates right, not left.
00152 
00153     // For values like 0xF000000F, we should ignore the low 6 bits, then
00154     // retry the hunt.
00155     if (Imm & 63U) {
00156       unsigned TZ2 = countTrailingZeros(Imm & ~63U);
00157       unsigned RotAmt2 = TZ2 & ~1;
00158       if ((rotr32(Imm, RotAmt2) & ~255U) == 0)
00159         return (32-RotAmt2)&31;  // HW rotates right, not left.
00160     }
00161 
00162     // Otherwise, we have no way to cover this span of bits with a single
00163     // shifter_op immediate.  Return a chunk of bits that will be useful to
00164     // handle.
00165     return (32-RotAmt)&31;  // HW rotates right, not left.
00166   }
00167 
00168   /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit
00169   /// into an shifter_operand immediate operand, return the 12-bit encoding for
00170   /// it.  If not, return -1.
00171   static inline int getSOImmVal(unsigned Arg) {
00172     // 8-bit (or less) immediates are trivially shifter_operands with a rotate
00173     // of zero.
00174     if ((Arg & ~255U) == 0) return Arg;
00175 
00176     unsigned RotAmt = getSOImmValRotate(Arg);
00177 
00178     // If this cannot be handled with a single shifter_op, bail out.
00179     if (rotr32(~255U, RotAmt) & Arg)
00180       return -1;
00181 
00182     // Encode this correctly.
00183     return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8);
00184   }
00185 
00186   /// isSOImmTwoPartVal - Return true if the specified value can be obtained by
00187   /// or'ing together two SOImmVal's.
00188   static inline bool isSOImmTwoPartVal(unsigned V) {
00189     // If this can be handled with a single shifter_op, bail out.
00190     V = rotr32(~255U, getSOImmValRotate(V)) & V;
00191     if (V == 0)
00192       return false;
00193 
00194     // If this can be handled with two shifter_op's, accept.
00195     V = rotr32(~255U, getSOImmValRotate(V)) & V;
00196     return V == 0;
00197   }
00198 
00199   /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,
00200   /// return the first chunk of it.
00201   static inline unsigned getSOImmTwoPartFirst(unsigned V) {
00202     return rotr32(255U, getSOImmValRotate(V)) & V;
00203   }
00204 
00205   /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,
00206   /// return the second chunk of it.
00207   static inline unsigned getSOImmTwoPartSecond(unsigned V) {
00208     // Mask out the first hunk.
00209     V = rotr32(~255U, getSOImmValRotate(V)) & V;
00210 
00211     // Take what's left.
00212     assert(V == (rotr32(255U, getSOImmValRotate(V)) & V));
00213     return V;
00214   }
00215 
00216   /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed
00217   /// by a left shift. Returns the shift amount to use.
00218   static inline unsigned getThumbImmValShift(unsigned Imm) {
00219     // 8-bit (or less) immediates are trivially immediate operand with a shift
00220     // of zero.
00221     if ((Imm & ~255U) == 0) return 0;
00222 
00223     // Use CTZ to compute the shift amount.
00224     return countTrailingZeros(Imm);
00225   }
00226 
00227   /// isThumbImmShiftedVal - Return true if the specified value can be obtained
00228   /// by left shifting a 8-bit immediate.
00229   static inline bool isThumbImmShiftedVal(unsigned V) {
00230     // If this can be handled with
00231     V = (~255U << getThumbImmValShift(V)) & V;
00232     return V == 0;
00233   }
00234 
00235   /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed
00236   /// by a left shift. Returns the shift amount to use.
00237   static inline unsigned getThumbImm16ValShift(unsigned Imm) {
00238     // 16-bit (or less) immediates are trivially immediate operand with a shift
00239     // of zero.
00240     if ((Imm & ~65535U) == 0) return 0;
00241 
00242     // Use CTZ to compute the shift amount.
00243     return countTrailingZeros(Imm);
00244   }
00245 
00246   /// isThumbImm16ShiftedVal - Return true if the specified value can be
00247   /// obtained by left shifting a 16-bit immediate.
00248   static inline bool isThumbImm16ShiftedVal(unsigned V) {
00249     // If this can be handled with
00250     V = (~65535U << getThumbImm16ValShift(V)) & V;
00251     return V == 0;
00252   }
00253 
00254   /// getThumbImmNonShiftedVal - If V is a value that satisfies
00255   /// isThumbImmShiftedVal, return the non-shiftd value.
00256   static inline unsigned getThumbImmNonShiftedVal(unsigned V) {
00257     return V >> getThumbImmValShift(V);
00258   }
00259 
00260 
00261   /// getT2SOImmValSplat - Return the 12-bit encoded representation
00262   /// if the specified value can be obtained by splatting the low 8 bits
00263   /// into every other byte or every byte of a 32-bit value. i.e.,
00264   ///     00000000 00000000 00000000 abcdefgh    control = 0
00265   ///     00000000 abcdefgh 00000000 abcdefgh    control = 1
00266   ///     abcdefgh 00000000 abcdefgh 00000000    control = 2
00267   ///     abcdefgh abcdefgh abcdefgh abcdefgh    control = 3
00268   /// Return -1 if none of the above apply.
00269   /// See ARM Reference Manual A6.3.2.
00270   static inline int getT2SOImmValSplatVal(unsigned V) {
00271     unsigned u, Vs, Imm;
00272     // control = 0
00273     if ((V & 0xffffff00) == 0)
00274       return V;
00275 
00276     // If the value is zeroes in the first byte, just shift those off
00277     Vs = ((V & 0xff) == 0) ? V >> 8 : V;
00278     // Any passing value only has 8 bits of payload, splatted across the word
00279     Imm = Vs & 0xff;
00280     // Likewise, any passing values have the payload splatted into the 3rd byte
00281     u = Imm | (Imm << 16);
00282 
00283     // control = 1 or 2
00284     if (Vs == u)
00285       return (((Vs == V) ? 1 : 2) << 8) | Imm;
00286 
00287     // control = 3
00288     if (Vs == (u | (u << 8)))
00289       return (3 << 8) | Imm;
00290 
00291     return -1;
00292   }
00293 
00294   /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the
00295   /// specified value is a rotated 8-bit value. Return -1 if no rotation
00296   /// encoding is possible.
00297   /// See ARM Reference Manual A6.3.2.
00298   static inline int getT2SOImmValRotateVal(unsigned V) {
00299     unsigned RotAmt = countLeadingZeros(V);
00300     if (RotAmt >= 24)
00301       return -1;
00302 
00303     // If 'Arg' can be handled with a single shifter_op return the value.
00304     if ((rotr32(0xff000000U, RotAmt) & V) == V)
00305       return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7);
00306 
00307     return -1;
00308   }
00309 
00310   /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit
00311   /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit
00312   /// encoding for it.  If not, return -1.
00313   /// See ARM Reference Manual A6.3.2.
00314   static inline int getT2SOImmVal(unsigned Arg) {
00315     // If 'Arg' is an 8-bit splat, then get the encoded value.
00316     int Splat = getT2SOImmValSplatVal(Arg);
00317     if (Splat != -1)
00318       return Splat;
00319 
00320     // If 'Arg' can be handled with a single shifter_op return the value.
00321     int Rot = getT2SOImmValRotateVal(Arg);
00322     if (Rot != -1)
00323       return Rot;
00324 
00325     return -1;
00326   }
00327 
00328   static inline unsigned getT2SOImmValRotate(unsigned V) {
00329     if ((V & ~255U) == 0) return 0;
00330     // Use CTZ to compute the rotate amount.
00331     unsigned RotAmt = countTrailingZeros(V);
00332     return (32 - RotAmt) & 31;
00333   }
00334 
00335   static inline bool isT2SOImmTwoPartVal (unsigned Imm) {
00336     unsigned V = Imm;
00337     // Passing values can be any combination of splat values and shifter
00338     // values. If this can be handled with a single shifter or splat, bail
00339     // out. Those should be handled directly, not with a two-part val.
00340     if (getT2SOImmValSplatVal(V) != -1)
00341       return false;
00342     V = rotr32 (~255U, getT2SOImmValRotate(V)) & V;
00343     if (V == 0)
00344       return false;
00345 
00346     // If this can be handled as an immediate, accept.
00347     if (getT2SOImmVal(V) != -1) return true;
00348 
00349     // Likewise, try masking out a splat value first.
00350     V = Imm;
00351     if (getT2SOImmValSplatVal(V & 0xff00ff00U) != -1)
00352       V &= ~0xff00ff00U;
00353     else if (getT2SOImmValSplatVal(V & 0x00ff00ffU) != -1)
00354       V &= ~0x00ff00ffU;
00355     // If what's left can be handled as an immediate, accept.
00356     if (getT2SOImmVal(V) != -1) return true;
00357 
00358     // Otherwise, do not accept.
00359     return false;
00360   }
00361 
00362   static inline unsigned getT2SOImmTwoPartFirst(unsigned Imm) {
00363     assert (isT2SOImmTwoPartVal(Imm) &&
00364             "Immedate cannot be encoded as two part immediate!");
00365     // Try a shifter operand as one part
00366     unsigned V = rotr32 (~255, getT2SOImmValRotate(Imm)) & Imm;
00367     // If the rest is encodable as an immediate, then return it.
00368     if (getT2SOImmVal(V) != -1) return V;
00369 
00370     // Try masking out a splat value first.
00371     if (getT2SOImmValSplatVal(Imm & 0xff00ff00U) != -1)
00372       return Imm & 0xff00ff00U;
00373 
00374     // The other splat is all that's left as an option.
00375     assert (getT2SOImmValSplatVal(Imm & 0x00ff00ffU) != -1);
00376     return Imm & 0x00ff00ffU;
00377   }
00378 
00379   static inline unsigned getT2SOImmTwoPartSecond(unsigned Imm) {
00380     // Mask out the first hunk
00381     Imm ^= getT2SOImmTwoPartFirst(Imm);
00382     // Return what's left
00383     assert (getT2SOImmVal(Imm) != -1 &&
00384             "Unable to encode second part of T2 two part SO immediate");
00385     return Imm;
00386   }
00387 
00388 
00389   //===--------------------------------------------------------------------===//
00390   // Addressing Mode #2
00391   //===--------------------------------------------------------------------===//
00392   //
00393   // This is used for most simple load/store instructions.
00394   //
00395   // addrmode2 := reg +/- reg shop imm
00396   // addrmode2 := reg +/- imm12
00397   //
00398   // The first operand is always a Reg.  The second operand is a reg if in
00399   // reg/reg form, otherwise it's reg#0.  The third field encodes the operation
00400   // in bit 12, the immediate in bits 0-11, and the shift op in 13-15. The
00401   // fourth operand 16-17 encodes the index mode.
00402   //
00403   // If this addressing mode is a frame index (before prolog/epilog insertion
00404   // and code rewriting), this operand will have the form:  FI#, reg0, <offs>
00405   // with no shift amount for the frame offset.
00406   //
00407   static inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO,
00408                                    unsigned IdxMode = 0) {
00409     assert(Imm12 < (1 << 12) && "Imm too large!");
00410     bool isSub = Opc == sub;
00411     return Imm12 | ((int)isSub << 12) | (SO << 13) | (IdxMode << 16) ;
00412   }
00413   static inline unsigned getAM2Offset(unsigned AM2Opc) {
00414     return AM2Opc & ((1 << 12)-1);
00415   }
00416   static inline AddrOpc getAM2Op(unsigned AM2Opc) {
00417     return ((AM2Opc >> 12) & 1) ? sub : add;
00418   }
00419   static inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) {
00420     return (ShiftOpc)((AM2Opc >> 13) & 7);
00421   }
00422   static inline unsigned getAM2IdxMode(unsigned AM2Opc) {
00423     return (AM2Opc >> 16);
00424   }
00425 
00426 
00427   //===--------------------------------------------------------------------===//
00428   // Addressing Mode #3
00429   //===--------------------------------------------------------------------===//
00430   //
00431   // This is used for sign-extending loads, and load/store-pair instructions.
00432   //
00433   // addrmode3 := reg +/- reg
00434   // addrmode3 := reg +/- imm8
00435   //
00436   // The first operand is always a Reg.  The second operand is a reg if in
00437   // reg/reg form, otherwise it's reg#0.  The third field encodes the operation
00438   // in bit 8, the immediate in bits 0-7. The fourth operand 9-10 encodes the
00439   // index mode.
00440 
00441   /// getAM3Opc - This function encodes the addrmode3 opc field.
00442   static inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset,
00443                                    unsigned IdxMode = 0) {
00444     bool isSub = Opc == sub;
00445     return ((int)isSub << 8) | Offset | (IdxMode << 9);
00446   }
00447   static inline unsigned char getAM3Offset(unsigned AM3Opc) {
00448     return AM3Opc & 0xFF;
00449   }
00450   static inline AddrOpc getAM3Op(unsigned AM3Opc) {
00451     return ((AM3Opc >> 8) & 1) ? sub : add;
00452   }
00453   static inline unsigned getAM3IdxMode(unsigned AM3Opc) {
00454     return (AM3Opc >> 9);
00455   }
00456 
00457   //===--------------------------------------------------------------------===//
00458   // Addressing Mode #4
00459   //===--------------------------------------------------------------------===//
00460   //
00461   // This is used for load / store multiple instructions.
00462   //
00463   // addrmode4 := reg, <mode>
00464   //
00465   // The four modes are:
00466   //    IA - Increment after
00467   //    IB - Increment before
00468   //    DA - Decrement after
00469   //    DB - Decrement before
00470   // For VFP instructions, only the IA and DB modes are valid.
00471 
00472   static inline AMSubMode getAM4SubMode(unsigned Mode) {
00473     return (AMSubMode)(Mode & 0x7);
00474   }
00475 
00476   static inline unsigned getAM4ModeImm(AMSubMode SubMode) {
00477     return (int)SubMode;
00478   }
00479 
00480   //===--------------------------------------------------------------------===//
00481   // Addressing Mode #5
00482   //===--------------------------------------------------------------------===//
00483   //
00484   // This is used for coprocessor instructions, such as FP load/stores.
00485   //
00486   // addrmode5 := reg +/- imm8*4
00487   //
00488   // The first operand is always a Reg.  The second operand encodes the
00489   // operation in bit 8 and the immediate in bits 0-7.
00490 
00491   /// getAM5Opc - This function encodes the addrmode5 opc field.
00492   static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) {
00493     bool isSub = Opc == sub;
00494     return ((int)isSub << 8) | Offset;
00495   }
00496   static inline unsigned char getAM5Offset(unsigned AM5Opc) {
00497     return AM5Opc & 0xFF;
00498   }
00499   static inline AddrOpc getAM5Op(unsigned AM5Opc) {
00500     return ((AM5Opc >> 8) & 1) ? sub : add;
00501   }
00502 
00503   //===--------------------------------------------------------------------===//
00504   // Addressing Mode #6
00505   //===--------------------------------------------------------------------===//
00506   //
00507   // This is used for NEON load / store instructions.
00508   //
00509   // addrmode6 := reg with optional alignment
00510   //
00511   // This is stored in two operands [regaddr, align].  The first is the
00512   // address register.  The second operand is the value of the alignment
00513   // specifier in bytes or zero if no explicit alignment.
00514   // Valid alignments depend on the specific instruction.
00515 
00516   //===--------------------------------------------------------------------===//
00517   // NEON Modified Immediates
00518   //===--------------------------------------------------------------------===//
00519   //
00520   // Several NEON instructions (e.g., VMOV) take a "modified immediate"
00521   // vector operand, where a small immediate encoded in the instruction
00522   // specifies a full NEON vector value.  These modified immediates are
00523   // represented here as encoded integers.  The low 8 bits hold the immediate
00524   // value; bit 12 holds the "Op" field of the instruction, and bits 11-8 hold
00525   // the "Cmode" field of the instruction.  The interfaces below treat the
00526   // Op and Cmode values as a single 5-bit value.
00527 
00528   static inline unsigned createNEONModImm(unsigned OpCmode, unsigned Val) {
00529     return (OpCmode << 8) | Val;
00530   }
00531   static inline unsigned getNEONModImmOpCmode(unsigned ModImm) {
00532     return (ModImm >> 8) & 0x1f;
00533   }
00534   static inline unsigned getNEONModImmVal(unsigned ModImm) {
00535     return ModImm & 0xff;
00536   }
00537 
00538   /// decodeNEONModImm - Decode a NEON modified immediate value into the
00539   /// element value and the element size in bits.  (If the element size is
00540   /// smaller than the vector, it is splatted into all the elements.)
00541   static inline uint64_t decodeNEONModImm(unsigned ModImm, unsigned &EltBits) {
00542     unsigned OpCmode = getNEONModImmOpCmode(ModImm);
00543     unsigned Imm8 = getNEONModImmVal(ModImm);
00544     uint64_t Val = 0;
00545 
00546     if (OpCmode == 0xe) {
00547       // 8-bit vector elements
00548       Val = Imm8;
00549       EltBits = 8;
00550     } else if ((OpCmode & 0xc) == 0x8) {
00551       // 16-bit vector elements
00552       unsigned ByteNum = (OpCmode & 0x6) >> 1;
00553       Val = Imm8 << (8 * ByteNum);
00554       EltBits = 16;
00555     } else if ((OpCmode & 0x8) == 0) {
00556       // 32-bit vector elements, zero with one byte set
00557       unsigned ByteNum = (OpCmode & 0x6) >> 1;
00558       Val = Imm8 << (8 * ByteNum);
00559       EltBits = 32;
00560     } else if ((OpCmode & 0xe) == 0xc) {
00561       // 32-bit vector elements, one byte with low bits set
00562       unsigned ByteNum = 1 + (OpCmode & 0x1);
00563       Val = (Imm8 << (8 * ByteNum)) | (0xffff >> (8 * (2 - ByteNum)));
00564       EltBits = 32;
00565     } else if (OpCmode == 0x1e) {
00566       // 64-bit vector elements
00567       for (unsigned ByteNum = 0; ByteNum < 8; ++ByteNum) {
00568         if ((ModImm >> ByteNum) & 1)
00569           Val |= (uint64_t)0xff << (8 * ByteNum);
00570       }
00571       EltBits = 64;
00572     } else {
00573       llvm_unreachable("Unsupported NEON immediate");
00574     }
00575     return Val;
00576   }
00577 
00578   AMSubMode getLoadStoreMultipleSubMode(int Opcode);
00579 
00580   //===--------------------------------------------------------------------===//
00581   // Floating-point Immediates
00582   //
00583   static inline float getFPImmFloat(unsigned Imm) {
00584     // We expect an 8-bit binary encoding of a floating-point number here.
00585     union {
00586       uint32_t I;
00587       float F;
00588     } FPUnion;
00589 
00590     uint8_t Sign = (Imm >> 7) & 0x1;
00591     uint8_t Exp = (Imm >> 4) & 0x7;
00592     uint8_t Mantissa = Imm & 0xf;
00593 
00594     //   8-bit FP    iEEEE Float Encoding
00595     //   abcd efgh   aBbbbbbc defgh000 00000000 00000000
00596     //
00597     // where B = NOT(b);
00598 
00599     FPUnion.I = 0;
00600     FPUnion.I |= Sign << 31;
00601     FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
00602     FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
00603     FPUnion.I |= (Exp & 0x3) << 23;
00604     FPUnion.I |= Mantissa << 19;
00605     return FPUnion.F;
00606   }
00607 
00608   /// getFP32Imm - Return an 8-bit floating-point version of the 32-bit
00609   /// floating-point value. If the value cannot be represented as an 8-bit
00610   /// floating-point value, then return -1.
00611   static inline int getFP32Imm(const APInt &Imm) {
00612     uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
00613     int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127;  // -126 to 127
00614     int64_t Mantissa = Imm.getZExtValue() & 0x7fffff;  // 23 bits
00615 
00616     // We can handle 4 bits of mantissa.
00617     // mantissa = (16+UInt(e:f:g:h))/16.
00618     if (Mantissa & 0x7ffff)
00619       return -1;
00620     Mantissa >>= 19;
00621     if ((Mantissa & 0xf) != Mantissa)
00622       return -1;
00623 
00624     // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
00625     if (Exp < -3 || Exp > 4)
00626       return -1;
00627     Exp = ((Exp+3) & 0x7) ^ 4;
00628 
00629     return ((int)Sign << 7) | (Exp << 4) | Mantissa;
00630   }
00631 
00632   static inline int getFP32Imm(const APFloat &FPImm) {
00633     return getFP32Imm(FPImm.bitcastToAPInt());
00634   }
00635 
00636   /// getFP64Imm - Return an 8-bit floating-point version of the 64-bit
00637   /// floating-point value. If the value cannot be represented as an 8-bit
00638   /// floating-point value, then return -1.
00639   static inline int getFP64Imm(const APInt &Imm) {
00640     uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
00641     int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
00642     uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffULL;
00643 
00644     // We can handle 4 bits of mantissa.
00645     // mantissa = (16+UInt(e:f:g:h))/16.
00646     if (Mantissa & 0xffffffffffffULL)
00647       return -1;
00648     Mantissa >>= 48;
00649     if ((Mantissa & 0xf) != Mantissa)
00650       return -1;
00651 
00652     // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
00653     if (Exp < -3 || Exp > 4)
00654       return -1;
00655     Exp = ((Exp+3) & 0x7) ^ 4;
00656 
00657     return ((int)Sign << 7) | (Exp << 4) | Mantissa;
00658   }
00659 
00660   static inline int getFP64Imm(const APFloat &FPImm) {
00661     return getFP64Imm(FPImm.bitcastToAPInt());
00662   }
00663 
00664 } // end namespace ARM_AM
00665 } // end namespace llvm
00666 
00667 #endif
00668