Path: blob/master/libs/capstone/arch/ARM/ARMAddressingModes.h
4394 views
//===-- ARMAddressingModes.h - ARM Addressing Modes -------------*- C++ -*-===//1//2// The LLVM Compiler Infrastructure3//4// This file is distributed under the University of Illinois Open Source5// License. See LICENSE.TXT for details.6//7//===----------------------------------------------------------------------===//8//9// This file contains the ARM addressing mode implementation stuff.10//11//===----------------------------------------------------------------------===//1213/* Capstone Disassembly Engine */14/* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */1516#ifndef CS_LLVM_TARGET_ARM_ARMADDRESSINGMODES_H17#define CS_LLVM_TARGET_ARM_ARMADDRESSINGMODES_H1819#include "capstone/platform.h"20#include "../../MathExtras.h"2122/// ARM_AM - ARM Addressing Mode Stuff23typedef enum ARM_AM_ShiftOpc {24ARM_AM_no_shift = 0,25ARM_AM_asr,26ARM_AM_lsl,27ARM_AM_lsr,28ARM_AM_ror,29ARM_AM_rrx30} ARM_AM_ShiftOpc;3132typedef enum ARM_AM_AddrOpc {33ARM_AM_sub = 0,34ARM_AM_add35} ARM_AM_AddrOpc;3637static inline const char *ARM_AM_getAddrOpcStr(ARM_AM_AddrOpc Op)38{39return Op == ARM_AM_sub ? "-" : "";40}4142static inline const char *ARM_AM_getShiftOpcStr(ARM_AM_ShiftOpc Op)43{44switch (Op) {45default: return ""; //llvm_unreachable("Unknown shift opc!");46case ARM_AM_asr: return "asr";47case ARM_AM_lsl: return "lsl";48case ARM_AM_lsr: return "lsr";49case ARM_AM_ror: return "ror";50case ARM_AM_rrx: return "rrx";51}52}5354static inline unsigned ARM_AM_getShiftOpcEncoding(ARM_AM_ShiftOpc Op)55{56switch (Op) {57default: return (unsigned int)-1; //llvm_unreachable("Unknown shift opc!");58case ARM_AM_asr: return 2;59case ARM_AM_lsl: return 0;60case ARM_AM_lsr: return 1;61case ARM_AM_ror: return 3;62}63}6465typedef enum ARM_AM_AMSubMode {66ARM_AM_bad_am_submode = 0,67ARM_AM_ia,68ARM_AM_ib,69ARM_AM_da,70ARM_AM_db71} ARM_AM_AMSubMode;7273static inline const char *ARM_AM_getAMSubModeStr(ARM_AM_AMSubMode Mode)74{75switch (Mode) {76default: return "";77case ARM_AM_ia: return "ia";78case ARM_AM_ib: return "ib";79case ARM_AM_da: return "da";80case ARM_AM_db: return "db";81}82}8384/// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits.85///86static inline unsigned rotr32(unsigned Val, unsigned Amt)87{88//assert(Amt < 32 && "Invalid rotate amount");89return (Val >> Amt) | (Val << ((32-Amt)&31));90}9192/// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits.93///94static inline unsigned rotl32(unsigned Val, unsigned Amt)95{96//assert(Amt < 32 && "Invalid rotate amount");97return (Val << Amt) | (Val >> ((32-Amt)&31));98}99100//===--------------------------------------------------------------------===//101// Addressing Mode #1: shift_operand with registers102//===--------------------------------------------------------------------===//103//104// This 'addressing mode' is used for arithmetic instructions. It can105// represent things like:106// reg107// reg [asr|lsl|lsr|ror|rrx] reg108// reg [asr|lsl|lsr|ror|rrx] imm109//110// This is stored three operands [rega, regb, opc]. The first is the base111// reg, the second is the shift amount (or reg0 if not present or imm). The112// third operand encodes the shift opcode and the imm if a reg isn't present.113//114static inline unsigned getSORegOpc(ARM_AM_ShiftOpc ShOp, unsigned Imm)115{116return ShOp | (Imm << 3);117}118119static inline unsigned getSORegOffset(unsigned Op)120{121return Op >> 3;122}123124static inline ARM_AM_ShiftOpc ARM_AM_getSORegShOp(unsigned Op)125{126return (ARM_AM_ShiftOpc)(Op & 7);127}128129/// getSOImmValImm - Given an encoded imm field for the reg/imm form, return130/// the 8-bit imm value.131static inline unsigned getSOImmValImm(unsigned Imm)132{133return Imm & 0xFF;134}135136/// getSOImmValRot - Given an encoded imm field for the reg/imm form, return137/// the rotate amount.138static inline unsigned getSOImmValRot(unsigned Imm)139{140return (Imm >> 8) * 2;141}142143/// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,144/// computing the rotate amount to use. If this immediate value cannot be145/// handled with a single shifter-op, determine a good rotate amount that will146/// take a maximal chunk of bits out of the immediate.147static inline unsigned getSOImmValRotate(unsigned Imm)148{149unsigned TZ, RotAmt;150// 8-bit (or less) immediates are trivially shifter_operands with a rotate151// of zero.152if ((Imm & ~255U) == 0) return 0;153154// Use CTZ to compute the rotate amount.155TZ = CountTrailingZeros_32(Imm);156157// Rotate amount must be even. Something like 0x200 must be rotated 8 bits,158// not 9.159RotAmt = TZ & ~1;160161// If we can handle this spread, return it.162if ((rotr32(Imm, RotAmt) & ~255U) == 0)163return (32-RotAmt)&31; // HW rotates right, not left.164165// For values like 0xF000000F, we should ignore the low 6 bits, then166// retry the hunt.167if (Imm & 63U) {168unsigned TZ2 = CountTrailingZeros_32(Imm & ~63U);169unsigned RotAmt2 = TZ2 & ~1;170if ((rotr32(Imm, RotAmt2) & ~255U) == 0)171return (32-RotAmt2)&31; // HW rotates right, not left.172}173174// Otherwise, we have no way to cover this span of bits with a single175// shifter_op immediate. Return a chunk of bits that will be useful to176// handle.177return (32-RotAmt)&31; // HW rotates right, not left.178}179180/// getSOImmVal - Given a 32-bit immediate, if it is something that can fit181/// into an shifter_operand immediate operand, return the 12-bit encoding for182/// it. If not, return -1.183static inline int getSOImmVal(unsigned Arg)184{185unsigned RotAmt;186// 8-bit (or less) immediates are trivially shifter_operands with a rotate187// of zero.188if ((Arg & ~255U) == 0) return Arg;189190RotAmt = getSOImmValRotate(Arg);191192// If this cannot be handled with a single shifter_op, bail out.193if (rotr32(~255U, RotAmt) & Arg)194return -1;195196// Encode this correctly.197return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8);198}199200/// isSOImmTwoPartVal - Return true if the specified value can be obtained by201/// or'ing together two SOImmVal's.202static inline bool isSOImmTwoPartVal(unsigned V)203{204// If this can be handled with a single shifter_op, bail out.205V = rotr32(~255U, getSOImmValRotate(V)) & V;206if (V == 0)207return false;208209// If this can be handled with two shifter_op's, accept.210V = rotr32(~255U, getSOImmValRotate(V)) & V;211return V == 0;212}213214/// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,215/// return the first chunk of it.216static inline unsigned getSOImmTwoPartFirst(unsigned V)217{218return rotr32(255U, getSOImmValRotate(V)) & V;219}220221/// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,222/// return the second chunk of it.223static inline unsigned getSOImmTwoPartSecond(unsigned V)224{225// Mask out the first hunk.226V = rotr32(~255U, getSOImmValRotate(V)) & V;227228// Take what's left.229//assert(V == (rotr32(255U, getSOImmValRotate(V)) & V));230return V;231}232233/// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed234/// by a left shift. Returns the shift amount to use.235static inline unsigned getThumbImmValShift(unsigned Imm)236{237// 8-bit (or less) immediates are trivially immediate operand with a shift238// of zero.239if ((Imm & ~255U) == 0) return 0;240241// Use CTZ to compute the shift amount.242return CountTrailingZeros_32(Imm);243}244245/// isThumbImmShiftedVal - Return true if the specified value can be obtained246/// by left shifting a 8-bit immediate.247static inline bool isThumbImmShiftedVal(unsigned V)248{249// If this can be handled with250V = (~255U << getThumbImmValShift(V)) & V;251return V == 0;252}253254/// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed255/// by a left shift. Returns the shift amount to use.256static inline unsigned getThumbImm16ValShift(unsigned Imm)257{258// 16-bit (or less) immediates are trivially immediate operand with a shift259// of zero.260if ((Imm & ~65535U) == 0) return 0;261262// Use CTZ to compute the shift amount.263return CountTrailingZeros_32(Imm);264}265266/// isThumbImm16ShiftedVal - Return true if the specified value can be267/// obtained by left shifting a 16-bit immediate.268static inline bool isThumbImm16ShiftedVal(unsigned V)269{270// If this can be handled with271V = (~65535U << getThumbImm16ValShift(V)) & V;272return V == 0;273}274275/// getThumbImmNonShiftedVal - If V is a value that satisfies276/// isThumbImmShiftedVal, return the non-shiftd value.277static inline unsigned getThumbImmNonShiftedVal(unsigned V)278{279return V >> getThumbImmValShift(V);280}281282283/// getT2SOImmValSplat - Return the 12-bit encoded representation284/// if the specified value can be obtained by splatting the low 8 bits285/// into every other byte or every byte of a 32-bit value. i.e.,286/// 00000000 00000000 00000000 abcdefgh control = 0287/// 00000000 abcdefgh 00000000 abcdefgh control = 1288/// abcdefgh 00000000 abcdefgh 00000000 control = 2289/// abcdefgh abcdefgh abcdefgh abcdefgh control = 3290/// Return -1 if none of the above apply.291/// See ARM Reference Manual A6.3.2.292static inline int getT2SOImmValSplatVal(unsigned V)293{294unsigned u, Vs, Imm;295// control = 0296if ((V & 0xffffff00) == 0)297return V;298299// If the value is zeroes in the first byte, just shift those off300Vs = ((V & 0xff) == 0) ? V >> 8 : V;301// Any passing value only has 8 bits of payload, splatted across the word302Imm = Vs & 0xff;303// Likewise, any passing values have the payload splatted into the 3rd byte304u = Imm | (Imm << 16);305306// control = 1 or 2307if (Vs == u)308return (((Vs == V) ? 1 : 2) << 8) | Imm;309310// control = 3311if (Vs == (u | (u << 8)))312return (3 << 8) | Imm;313314return -1;315}316317/// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the318/// specified value is a rotated 8-bit value. Return -1 if no rotation319/// encoding is possible.320/// See ARM Reference Manual A6.3.2.321static inline int getT2SOImmValRotateVal(unsigned V)322{323unsigned RotAmt = CountLeadingZeros_32(V);324if (RotAmt >= 24)325return -1;326327// If 'Arg' can be handled with a single shifter_op return the value.328if ((rotr32(0xff000000U, RotAmt) & V) == V)329return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7);330331return -1;332}333334/// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit335/// into a Thumb-2 shifter_operand immediate operand, return the 12-bit336/// encoding for it. If not, return -1.337/// See ARM Reference Manual A6.3.2.338static inline int getT2SOImmVal(unsigned Arg)339{340int Rot;341// If 'Arg' is an 8-bit splat, then get the encoded value.342int Splat = getT2SOImmValSplatVal(Arg);343if (Splat != -1)344return Splat;345346// If 'Arg' can be handled with a single shifter_op return the value.347Rot = getT2SOImmValRotateVal(Arg);348if (Rot != -1)349return Rot;350351return -1;352}353354static inline unsigned getT2SOImmValRotate(unsigned V)355{356unsigned RotAmt;357358if ((V & ~255U) == 0)359return 0;360361// Use CTZ to compute the rotate amount.362RotAmt = CountTrailingZeros_32(V);363return (32 - RotAmt) & 31;364}365366static inline bool isT2SOImmTwoPartVal (unsigned Imm)367{368unsigned V = Imm;369// Passing values can be any combination of splat values and shifter370// values. If this can be handled with a single shifter or splat, bail371// out. Those should be handled directly, not with a two-part val.372if (getT2SOImmValSplatVal(V) != -1)373return false;374V = rotr32 (~255U, getT2SOImmValRotate(V)) & V;375if (V == 0)376return false;377378// If this can be handled as an immediate, accept.379if (getT2SOImmVal(V) != -1) return true;380381// Likewise, try masking out a splat value first.382V = Imm;383if (getT2SOImmValSplatVal(V & 0xff00ff00U) != -1)384V &= ~0xff00ff00U;385else if (getT2SOImmValSplatVal(V & 0x00ff00ffU) != -1)386V &= ~0x00ff00ffU;387// If what's left can be handled as an immediate, accept.388if (getT2SOImmVal(V) != -1) return true;389390// Otherwise, do not accept.391return false;392}393394static inline unsigned getT2SOImmTwoPartFirst(unsigned Imm)395{396//assert (isT2SOImmTwoPartVal(Imm) &&397// "Immedate cannot be encoded as two part immediate!");398// Try a shifter operand as one part399unsigned V = rotr32 (~(unsigned int)255, getT2SOImmValRotate(Imm)) & Imm;400// If the rest is encodable as an immediate, then return it.401if (getT2SOImmVal(V) != -1) return V;402403// Try masking out a splat value first.404if (getT2SOImmValSplatVal(Imm & 0xff00ff00U) != -1)405return Imm & 0xff00ff00U;406407// The other splat is all that's left as an option.408//assert (getT2SOImmValSplatVal(Imm & 0x00ff00ffU) != -1);409return Imm & 0x00ff00ffU;410}411412static inline unsigned getT2SOImmTwoPartSecond(unsigned Imm)413{414// Mask out the first hunk415Imm ^= getT2SOImmTwoPartFirst(Imm);416// Return what's left417//assert (getT2SOImmVal(Imm) != -1 &&418// "Unable to encode second part of T2 two part SO immediate");419return Imm;420}421422423//===--------------------------------------------------------------------===//424// Addressing Mode #2425//===--------------------------------------------------------------------===//426//427// This is used for most simple load/store instructions.428//429// addrmode2 := reg +/- reg shop imm430// addrmode2 := reg +/- imm12431//432// The first operand is always a Reg. The second operand is a reg if in433// reg/reg form, otherwise it's reg#0. The third field encodes the operation434// in bit 12, the immediate in bits 0-11, and the shift op in 13-15. The435// fourth operand 16-17 encodes the index mode.436//437// If this addressing mode is a frame index (before prolog/epilog insertion438// and code rewriting), this operand will have the form: FI#, reg0, <offs>439// with no shift amount for the frame offset.440//441static inline unsigned ARM_AM_getAM2Opc(ARM_AM_AddrOpc Opc, unsigned Imm12, ARM_AM_ShiftOpc SO,442unsigned IdxMode)443{444//assert(Imm12 < (1 << 12) && "Imm too large!");445bool isSub = Opc == ARM_AM_sub;446return Imm12 | ((int)isSub << 12) | (SO << 13) | (IdxMode << 16) ;447}448449static inline unsigned getAM2Offset(unsigned AM2Opc)450{451return AM2Opc & ((1 << 12)-1);452}453454static inline ARM_AM_AddrOpc getAM2Op(unsigned AM2Opc)455{456return ((AM2Opc >> 12) & 1) ? ARM_AM_sub : ARM_AM_add;457}458459static inline ARM_AM_ShiftOpc getAM2ShiftOpc(unsigned AM2Opc)460{461return (ARM_AM_ShiftOpc)((AM2Opc >> 13) & 7);462}463464static inline unsigned getAM2IdxMode(unsigned AM2Opc)465{466return (AM2Opc >> 16);467}468469//===--------------------------------------------------------------------===//470// Addressing Mode #3471//===--------------------------------------------------------------------===//472//473// This is used for sign-extending loads, and load/store-pair instructions.474//475// addrmode3 := reg +/- reg476// addrmode3 := reg +/- imm8477//478// The first operand is always a Reg. The second operand is a reg if in479// reg/reg form, otherwise it's reg#0. The third field encodes the operation480// in bit 8, the immediate in bits 0-7. The fourth operand 9-10 encodes the481// index mode.482483/// getAM3Opc - This function encodes the addrmode3 opc field.484static inline unsigned getAM3Opc(ARM_AM_AddrOpc Opc, unsigned char Offset,485unsigned IdxMode)486{487bool isSub = Opc == ARM_AM_sub;488return ((int)isSub << 8) | Offset | (IdxMode << 9);489}490491static inline unsigned char getAM3Offset(unsigned AM3Opc)492{493return AM3Opc & 0xFF;494}495496static inline ARM_AM_AddrOpc getAM3Op(unsigned AM3Opc)497{498return ((AM3Opc >> 8) & 1) ? ARM_AM_sub : ARM_AM_add;499}500501static inline unsigned getAM3IdxMode(unsigned AM3Opc)502{503return (AM3Opc >> 9);504}505506//===--------------------------------------------------------------------===//507// Addressing Mode #4508//===--------------------------------------------------------------------===//509//510// This is used for load / store multiple instructions.511//512// addrmode4 := reg, <mode>513//514// The four modes are:515// IA - Increment after516// IB - Increment before517// DA - Decrement after518// DB - Decrement before519// For VFP instructions, only the IA and DB modes are valid.520521static inline ARM_AM_AMSubMode getAM4SubMode(unsigned Mode)522{523return (ARM_AM_AMSubMode)(Mode & 0x7);524}525526static inline unsigned getAM4ModeImm(ARM_AM_AMSubMode SubMode)527{528return (int)SubMode;529}530531//===--------------------------------------------------------------------===//532// Addressing Mode #5533//===--------------------------------------------------------------------===//534//535// This is used for coprocessor instructions, such as FP load/stores.536//537// addrmode5 := reg +/- imm8*4538//539// The first operand is always a Reg. The second operand encodes the540// operation in bit 8 and the immediate in bits 0-7.541542/// getAM5Opc - This function encodes the addrmode5 opc field.543static inline unsigned ARM_AM_getAM5Opc(ARM_AM_AddrOpc Opc, unsigned char Offset)544{545bool isSub = Opc == ARM_AM_sub;546return ((int)isSub << 8) | Offset;547}548static inline unsigned char ARM_AM_getAM5Offset(unsigned AM5Opc)549{550return AM5Opc & 0xFF;551}552static inline ARM_AM_AddrOpc ARM_AM_getAM5Op(unsigned AM5Opc)553{554return ((AM5Opc >> 8) & 1) ? ARM_AM_sub : ARM_AM_add;555}556557//===--------------------------------------------------------------------===//558// Addressing Mode #5 FP16559//===--------------------------------------------------------------------===//560//561// This is used for coprocessor instructions, such as 16-bit FP load/stores.562//563// addrmode5fp16 := reg +/- imm8*2564//565// The first operand is always a Reg. The second operand encodes the566// operation (add or subtract) in bit 8 and the immediate in bits 0-7.567568/// getAM5FP16Opc - This function encodes the addrmode5fp16 opc field.569static inline unsigned getAM5FP16Opc(ARM_AM_AddrOpc Opc, unsigned char Offset)570{571bool isSub = Opc == ARM_AM_sub;572return ((int)isSub << 8) | Offset;573}574575static inline unsigned char getAM5FP16Offset(unsigned AM5Opc)576{577return AM5Opc & 0xFF;578}579580static inline ARM_AM_AddrOpc getAM5FP16Op(unsigned AM5Opc)581{582return ((AM5Opc >> 8) & 1) ? ARM_AM_sub : ARM_AM_add;583}584585//===--------------------------------------------------------------------===//586// Addressing Mode #6587//===--------------------------------------------------------------------===//588//589// This is used for NEON load / store instructions.590//591// addrmode6 := reg with optional alignment592//593// This is stored in two operands [regaddr, align]. The first is the594// address register. The second operand is the value of the alignment595// specifier in bytes or zero if no explicit alignment.596// Valid alignments depend on the specific instruction.597598//===--------------------------------------------------------------------===//599// NEON Modified Immediates600//===--------------------------------------------------------------------===//601//602// Several NEON instructions (e.g., VMOV) take a "modified immediate"603// vector operand, where a small immediate encoded in the instruction604// specifies a full NEON vector value. These modified immediates are605// represented here as encoded integers. The low 8 bits hold the immediate606// value; bit 12 holds the "Op" field of the instruction, and bits 11-8 hold607// the "Cmode" field of the instruction. The interfaces below treat the608// Op and Cmode values as a single 5-bit value.609610static inline unsigned createNEONModImm(unsigned OpCmode, unsigned Val)611{612return (OpCmode << 8) | Val;613}614static inline unsigned getNEONModImmOpCmode(unsigned ModImm)615{616return (ModImm >> 8) & 0x1f;617}618static inline unsigned getNEONModImmVal(unsigned ModImm)619{620return ModImm & 0xff;621}622623/// decodeNEONModImm - Decode a NEON modified immediate value into the624/// element value and the element size in bits. (If the element size is625/// smaller than the vector, it is splatted into all the elements.)626static inline uint64_t ARM_AM_decodeNEONModImm(unsigned ModImm, unsigned *EltBits)627{628unsigned OpCmode = getNEONModImmOpCmode(ModImm);629unsigned Imm8 = getNEONModImmVal(ModImm);630uint64_t Val = 0;631unsigned ByteNum;632633if (OpCmode == 0xe) {634// 8-bit vector elements635Val = Imm8;636*EltBits = 8;637} else if ((OpCmode & 0xc) == 0x8) {638// 16-bit vector elements639ByteNum = (OpCmode & 0x6) >> 1;640Val = (uint64_t)Imm8 << (8 * ByteNum);641*EltBits = 16;642} else if ((OpCmode & 0x8) == 0) {643// 32-bit vector elements, zero with one byte set644ByteNum = (OpCmode & 0x6) >> 1;645Val = (uint64_t)Imm8 << (8 * ByteNum);646*EltBits = 32;647} else if ((OpCmode & 0xe) == 0xc) {648// 32-bit vector elements, one byte with low bits set649ByteNum = 1 + (OpCmode & 0x1);650Val = (Imm8 << (8 * ByteNum)) | (0xffff >> (8 * (2 - ByteNum)));651*EltBits = 32;652} else if (OpCmode == 0x1e) {653// 64-bit vector elements654for (ByteNum = 0; ByteNum < 8; ++ByteNum) {655if ((ModImm >> ByteNum) & 1)656Val |= (uint64_t)0xff << (8 * ByteNum);657}658*EltBits = 64;659} else {660//llvm_unreachable("Unsupported NEON immediate");661}662return Val;663}664665ARM_AM_AMSubMode getLoadStoreMultipleSubMode(int Opcode);666667//===--------------------------------------------------------------------===//668// Floating-point Immediates669//670static inline float getFPImmFloat(unsigned Imm)671{672// We expect an 8-bit binary encoding of a floating-point number here.673union {674uint32_t I;675float F;676} FPUnion;677678uint8_t Sign = (Imm >> 7) & 0x1;679uint8_t Exp = (Imm >> 4) & 0x7;680uint8_t Mantissa = Imm & 0xf;681682// 8-bit FP iEEEE Float Encoding683// abcd efgh aBbbbbbc defgh000 00000000 00000000684//685// where B = NOT(b);686687FPUnion.I = 0;688FPUnion.I |= ((uint32_t) Sign) << 31;689FPUnion.I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;690FPUnion.I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;691FPUnion.I |= (Exp & 0x3) << 23;692FPUnion.I |= Mantissa << 19;693return FPUnion.F;694}695696#endif697698699