Jim Grosbach | 2cee75a | 2010-10-08 17:28:40 +0000 | [diff] [blame] | 1 | //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===// |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the pass that transforms the ARM machine instructions into |
| 11 | // relocatable machine code. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 16 | #include "ARM.h" |
| 17 | #include "ARMAddressingModes.h" |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 18 | #include "ARMConstantPoolValue.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 19 | #include "ARMInstrInfo.h" |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 20 | #include "ARMRelocations.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 21 | #include "ARMSubtarget.h" |
| 22 | #include "ARMTargetMachine.h" |
Jim Grosbach | bc6d876 | 2008-10-28 18:25:49 +0000 | [diff] [blame] | 23 | #include "llvm/Constants.h" |
| 24 | #include "llvm/DerivedTypes.h" |
Evan Cheng | 42d5ee06 | 2008-09-13 01:15:21 +0000 | [diff] [blame] | 25 | #include "llvm/Function.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 26 | #include "llvm/PassManager.h" |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineConstantPool.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 30 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Daniel Dunbar | 003de66 | 2009-09-21 05:58:35 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/Passes.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 42d5ee06 | 2008-09-13 01:15:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | ab7c09b | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 38 | #ifndef NDEBUG |
| 39 | #include <iomanip> |
| 40 | #endif |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 41 | using namespace llvm; |
| 42 | |
| 43 | STATISTIC(NumEmitted, "Number of machine instructions emitted"); |
| 44 | |
| 45 | namespace { |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 47 | class ARMCodeEmitter : public MachineFunctionPass { |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 48 | ARMJITInfo *JTI; |
| 49 | const ARMInstrInfo *II; |
| 50 | const TargetData *TD; |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 51 | const ARMSubtarget *Subtarget; |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 52 | TargetMachine &TM; |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 53 | JITCodeEmitter &MCE; |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 54 | MachineModuleInfo *MMI; |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 55 | const std::vector<MachineConstantPoolEntry> *MCPEs; |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 56 | const std::vector<MachineJumpTableEntry> *MJTEs; |
| 57 | bool IsPIC; |
Bob Wilson | 62d24a4 | 2010-06-28 22:23:17 +0000 | [diff] [blame] | 58 | bool IsThumb; |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 59 | |
Daniel Dunbar | 003de66 | 2009-09-21 05:58:35 +0000 | [diff] [blame] | 60 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 61 | AU.addRequired<MachineModuleInfo>(); |
| 62 | MachineFunctionPass::getAnalysisUsage(AU); |
| 63 | } |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 64 | |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 65 | static char ID; |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 66 | public: |
| 67 | ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 68 | : MachineFunctionPass(ID), JTI(0), |
Dan Gohman | 3fb150a | 2010-04-17 17:42:52 +0000 | [diff] [blame] | 69 | II((const ARMInstrInfo *)tm.getInstrInfo()), |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 70 | TD(tm.getTargetData()), TM(tm), |
Bob Wilson | 62d24a4 | 2010-06-28 22:23:17 +0000 | [diff] [blame] | 71 | MCE(mce), MCPEs(0), MJTEs(0), |
| 72 | IsPIC(TM.getRelocationModel() == Reloc::PIC_), IsThumb(false) {} |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 74 | /// getBinaryCodeForInstr - This function, generated by the |
| 75 | /// CodeEmitterGenerator using TableGen, produces the binary encoding for |
| 76 | /// machine instructions. |
Jim Grosbach | bade37b | 2010-10-08 00:21:28 +0000 | [diff] [blame] | 77 | unsigned getBinaryCodeForInstr(const MachineInstr &MI) const; |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 78 | |
| 79 | bool runOnMachineFunction(MachineFunction &MF); |
| 80 | |
| 81 | virtual const char *getPassName() const { |
| 82 | return "ARM Machine Code Emitter"; |
| 83 | } |
| 84 | |
| 85 | void emitInstruction(const MachineInstr &MI); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 86 | |
| 87 | private: |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 88 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 89 | void emitWordLE(unsigned Binary); |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 90 | void emitDWordLE(uint64_t Binary); |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 91 | void emitConstPoolInstruction(const MachineInstr &MI); |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 92 | void emitMOVi32immInstruction(const MachineInstr &MI); |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 93 | void emitMOVi2piecesInstruction(const MachineInstr &MI); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 94 | void emitLEApcrelJTInstruction(const MachineInstr &MI); |
Evan Cheng | a956255 | 2008-11-14 20:09:11 +0000 | [diff] [blame] | 95 | void emitPseudoMoveInstruction(const MachineInstr &MI); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 96 | void addPCLabel(unsigned LabelID); |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 97 | void emitPseudoInstruction(const MachineInstr &MI); |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 98 | unsigned getMachineSoRegOpValue(const MachineInstr &MI, |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 99 | const TargetInstrDesc &TID, |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 100 | const MachineOperand &MO, |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 101 | unsigned OpIdx); |
| 102 | |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 103 | unsigned getMachineSoImmOpValue(unsigned SoImm); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 104 | |
Jim Grosbach | 0a4b9dc | 2008-11-03 18:38:31 +0000 | [diff] [blame] | 105 | unsigned getAddrModeSBit(const MachineInstr &MI, |
| 106 | const TargetInstrDesc &TID) const; |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 107 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 108 | void emitDataProcessingInstruction(const MachineInstr &MI, |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 109 | unsigned ImplicitRd = 0, |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 110 | unsigned ImplicitRn = 0); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 111 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 112 | void emitLoadStoreInstruction(const MachineInstr &MI, |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 113 | unsigned ImplicitRd = 0, |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 114 | unsigned ImplicitRn = 0); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 115 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 116 | void emitMiscLoadStoreInstruction(const MachineInstr &MI, |
| 117 | unsigned ImplicitRn = 0); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 118 | |
| 119 | void emitLoadStoreMultipleInstruction(const MachineInstr &MI); |
| 120 | |
Evan Cheng | fbc9d41 | 2008-11-06 01:21:28 +0000 | [diff] [blame] | 121 | void emitMulFrmInstruction(const MachineInstr &MI); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 122 | |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 123 | void emitExtendInstruction(const MachineInstr &MI); |
| 124 | |
Evan Cheng | 8b59db3 | 2008-11-07 01:41:35 +0000 | [diff] [blame] | 125 | void emitMiscArithInstruction(const MachineInstr &MI); |
| 126 | |
Bob Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 127 | void emitSaturateInstruction(const MachineInstr &MI); |
| 128 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 129 | void emitBranchInstruction(const MachineInstr &MI); |
| 130 | |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 131 | void emitInlineJumpTable(unsigned JTIndex); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 132 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 133 | void emitMiscBranchInstruction(const MachineInstr &MI); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 134 | |
Evan Cheng | 96581d3 | 2008-11-11 02:11:05 +0000 | [diff] [blame] | 135 | void emitVFPArithInstruction(const MachineInstr &MI); |
| 136 | |
Evan Cheng | 78be83d | 2008-11-11 19:40:26 +0000 | [diff] [blame] | 137 | void emitVFPConversionInstruction(const MachineInstr &MI); |
| 138 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 139 | void emitVFPLoadStoreInstruction(const MachineInstr &MI); |
| 140 | |
| 141 | void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI); |
| 142 | |
| 143 | void emitMiscInstruction(const MachineInstr &MI); |
| 144 | |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 145 | void emitNEONLaneInstruction(const MachineInstr &MI); |
Bob Wilson | 21773e7 | 2010-06-29 20:13:29 +0000 | [diff] [blame] | 146 | void emitNEONDupInstruction(const MachineInstr &MI); |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 147 | void emitNEON1RegModImmInstruction(const MachineInstr &MI); |
| 148 | void emitNEON2RegInstruction(const MachineInstr &MI); |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 149 | void emitNEON3RegInstruction(const MachineInstr &MI); |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 150 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 151 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 152 | /// operand requires relocation, record the relocation and return zero. |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 153 | unsigned getMachineOpValue(const MachineInstr &MI, |
| 154 | const MachineOperand &MO) const; |
| 155 | unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) const { |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 156 | return getMachineOpValue(MI, MI.getOperand(OpIdx)); |
| 157 | } |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 158 | |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 159 | // FIXME: The legacy JIT ARMCodeEmitter doesn't rely on the the |
| 160 | // TableGen'erated getBinaryCodeForInstr() function to encode any |
| 161 | // operand values, instead querying getMachineOpValue() directly for |
| 162 | // each operand it needs to encode. Thus, any of the new encoder |
| 163 | // helper functions can simply return 0 as the values the return |
| 164 | // are already handled elsewhere. They are placeholders to allow this |
| 165 | // encoder to continue to function until the MC encoder is sufficiently |
| 166 | // far along that this one can be eliminated entirely. |
| 167 | unsigned getCCOutOpValue(const MachineInstr &MI, unsigned Op) |
| 168 | const { return 0; } |
Jim Grosbach | 2a6a93d | 2010-10-12 23:18:08 +0000 | [diff] [blame] | 169 | unsigned getSOImmOpValue(const MachineInstr &MI, unsigned Op) |
| 170 | const { return 0; } |
Jim Grosbach | ef324d7 | 2010-10-12 23:53:58 +0000 | [diff] [blame] | 171 | unsigned getSORegOpValue(const MachineInstr &MI, unsigned Op) |
| 172 | const { return 0; } |
Jim Grosbach | b35ad41 | 2010-10-13 19:56:10 +0000 | [diff] [blame^] | 173 | unsigned getRotImmOpValue(const MachineInstr &MI, unsigned Op) |
| 174 | const { return 0; } |
Jim Grosbach | 08bd549 | 2010-10-12 23:00:24 +0000 | [diff] [blame] | 175 | |
Shih-wei Liao | 5170b71 | 2010-05-26 00:02:28 +0000 | [diff] [blame] | 176 | /// getMovi32Value - Return binary encoding of operand for movw/movt. If the |
Jim Grosbach | 18f30e6 | 2010-06-02 21:53:11 +0000 | [diff] [blame] | 177 | /// machine operand requires relocation, record the relocation and return |
| 178 | /// zero. |
Shih-wei Liao | 5170b71 | 2010-05-26 00:02:28 +0000 | [diff] [blame] | 179 | unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO, |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 180 | unsigned Reloc); |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 181 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 182 | /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 183 | /// |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 184 | unsigned getShiftOp(unsigned Imm) const ; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 185 | |
| 186 | /// Routines that handle operands which add machine relocations which are |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 187 | /// fixed up by the relocation stage. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 188 | void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, |
Jeffrey Yasskin | 2d27441 | 2009-11-07 08:51:52 +0000 | [diff] [blame] | 189 | bool MayNeedFarStub, bool Indirect, |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 190 | intptr_t ACPV = 0) const; |
| 191 | void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const; |
| 192 | void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const; |
| 193 | void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const; |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 194 | void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc, |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 195 | intptr_t JTBase = 0) const; |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 196 | }; |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 199 | char ARMCodeEmitter::ID = 0; |
| 200 | |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 201 | /// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM |
Chris Lattner | e0faa54 | 2010-02-02 21:38:59 +0000 | [diff] [blame] | 202 | /// code to the specified MCE object. |
Bruno Cardoso Lopes | ac57e6e | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 203 | FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM, |
| 204 | JITCodeEmitter &JCE) { |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 205 | return new ARMCodeEmitter(TM, JCE); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 206 | } |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 207 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 208 | bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 209 | assert((MF.getTarget().getRelocationModel() != Reloc::Default || |
| 210 | MF.getTarget().getRelocationModel() != Reloc::Static) && |
| 211 | "JIT relocation model must be set to static or default!"); |
Dan Gohman | 3fb150a | 2010-04-17 17:42:52 +0000 | [diff] [blame] | 212 | JTI = ((ARMTargetMachine &)MF.getTarget()).getJITInfo(); |
| 213 | II = ((const ARMTargetMachine &)MF.getTarget()).getInstrInfo(); |
| 214 | TD = ((const ARMTargetMachine &)MF.getTarget()).getTargetData(); |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 215 | Subtarget = &TM.getSubtarget<ARMSubtarget>(); |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 216 | MCPEs = &MF.getConstantPool()->getConstants(); |
Chris Lattner | b1e8039 | 2010-01-25 23:22:00 +0000 | [diff] [blame] | 217 | MJTEs = 0; |
| 218 | if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 219 | IsPIC = TM.getRelocationModel() == Reloc::PIC_; |
Bob Wilson | 62d24a4 | 2010-06-28 22:23:17 +0000 | [diff] [blame] | 220 | IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction(); |
Evan Cheng | 3cc8223 | 2008-11-08 07:38:22 +0000 | [diff] [blame] | 221 | JTI->Initialize(MF, IsPIC); |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 222 | MMI = &getAnalysis<MachineModuleInfo>(); |
| 223 | MCE.setModuleInfo(MMI); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 224 | |
| 225 | do { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 226 | DEBUG(errs() << "JITTing function '" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 227 | << MF.getFunction()->getName() << "'\n"); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 228 | MCE.startFunction(MF); |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 229 | for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 230 | MBB != E; ++MBB) { |
| 231 | MCE.StartMachineBasicBlock(MBB); |
| 232 | for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end(); |
| 233 | I != E; ++I) |
| 234 | emitInstruction(*I); |
| 235 | } |
| 236 | } while (MCE.finishFunction(MF)); |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 241 | /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 242 | /// |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 243 | unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const { |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 244 | switch (ARM_AM::getAM2ShiftOpc(Imm)) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 245 | default: llvm_unreachable("Unknown shift opc!"); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 246 | case ARM_AM::asr: return 2; |
| 247 | case ARM_AM::lsl: return 0; |
| 248 | case ARM_AM::lsr: return 1; |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 249 | case ARM_AM::ror: |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 250 | case ARM_AM::rrx: return 3; |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 251 | } |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 252 | return 0; |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Shih-wei Liao | 5170b71 | 2010-05-26 00:02:28 +0000 | [diff] [blame] | 255 | /// getMovi32Value - Return binary encoding of operand for movw/movt. If the |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 256 | /// machine operand requires relocation, record the relocation and return zero. |
| 257 | unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI, |
Shih-wei Liao | 5170b71 | 2010-05-26 00:02:28 +0000 | [diff] [blame] | 258 | const MachineOperand &MO, |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 259 | unsigned Reloc) { |
Shih-wei Liao | 5170b71 | 2010-05-26 00:02:28 +0000 | [diff] [blame] | 260 | assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw)) |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 261 | && "Relocation to this function should be for movt or movw"); |
| 262 | |
| 263 | if (MO.isImm()) |
| 264 | return static_cast<unsigned>(MO.getImm()); |
| 265 | else if (MO.isGlobal()) |
| 266 | emitGlobalAddress(MO.getGlobal(), Reloc, true, false); |
| 267 | else if (MO.isSymbol()) |
| 268 | emitExternalSymbolAddress(MO.getSymbolName(), Reloc); |
| 269 | else if (MO.isMBB()) |
| 270 | emitMachineBasicBlock(MO.getMBB(), Reloc); |
| 271 | else { |
| 272 | #ifndef NDEBUG |
| 273 | errs() << MO; |
| 274 | #endif |
| 275 | llvm_unreachable("Unsupported operand type for movw/movt"); |
| 276 | } |
| 277 | return 0; |
| 278 | } |
| 279 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 280 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 281 | /// operand requires relocation, record the relocation and return zero. |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 282 | unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI, |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 283 | const MachineOperand &MO) const { |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 284 | if (MO.isReg()) |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 285 | return getARMRegisterNumbering(MO.getReg()); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 286 | else if (MO.isImm()) |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 287 | return static_cast<unsigned>(MO.getImm()); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 288 | else if (MO.isGlobal()) |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 289 | emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 290 | else if (MO.isSymbol()) |
Evan Cheng | 1033251 | 2008-11-08 07:22:33 +0000 | [diff] [blame] | 291 | emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch); |
Evan Cheng | 580c0df | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 292 | else if (MO.isCPI()) { |
| 293 | const TargetInstrDesc &TID = MI.getDesc(); |
| 294 | // For VFP load, the immediate offset is multiplied by 4. |
| 295 | unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm) |
| 296 | ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry; |
| 297 | emitConstPoolAddress(MO.getIndex(), Reloc); |
| 298 | } else if (MO.isJTI()) |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 299 | emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 300 | else if (MO.isMBB()) |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 301 | emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch); |
Evan Cheng | 2aa0e64 | 2008-09-13 01:55:59 +0000 | [diff] [blame] | 302 | else { |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 303 | #ifndef NDEBUG |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 304 | errs() << MO; |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 305 | #endif |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 306 | llvm_unreachable(0); |
Evan Cheng | 2aa0e64 | 2008-09-13 01:55:59 +0000 | [diff] [blame] | 307 | } |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 308 | return 0; |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 311 | /// emitGlobalAddress - Emit the specified address to the code stream. |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 312 | /// |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 313 | void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 314 | bool MayNeedFarStub, bool Indirect, |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 315 | intptr_t ACPV) const { |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 316 | MachineRelocation MR = Indirect |
| 317 | ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 318 | const_cast<GlobalValue *>(GV), |
| 319 | ACPV, MayNeedFarStub) |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 320 | : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 321 | const_cast<GlobalValue *>(GV), ACPV, |
| 322 | MayNeedFarStub); |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 323 | MCE.addRelocation(MR); |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /// emitExternalSymbolAddress - Arrange for the address of an external symbol to |
| 327 | /// be emitted to the current location in the function, and allow it to be PC |
| 328 | /// relative. |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 329 | void ARMCodeEmitter:: |
| 330 | emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 331 | MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), |
| 332 | Reloc, ES)); |
| 333 | } |
| 334 | |
| 335 | /// emitConstPoolAddress - Arrange for the address of an constant pool |
| 336 | /// to be emitted to the current location in the function, and allow it to be PC |
| 337 | /// relative. |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 338 | void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 339 | // Tell JIT emitter we'll resolve the address. |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 340 | MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 341 | Reloc, CPI, 0, true)); |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /// emitJumpTableAddress - Arrange for the address of a jump table to |
| 345 | /// be emitted to the current location in the function, and allow it to be PC |
| 346 | /// relative. |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 347 | void ARMCodeEmitter:: |
| 348 | emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 349 | MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 350 | Reloc, JTIndex, 0, true)); |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Raul Herbster | 9c1a382 | 2007-08-30 23:29:26 +0000 | [diff] [blame] | 353 | /// emitMachineBasicBlock - Emit the specified address basic block. |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 354 | void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, |
Jim Grosbach | 3e09413 | 2010-10-08 17:45:54 +0000 | [diff] [blame] | 355 | unsigned Reloc, |
| 356 | intptr_t JTBase) const { |
Raul Herbster | 9c1a382 | 2007-08-30 23:29:26 +0000 | [diff] [blame] | 357 | MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 358 | Reloc, BB, JTBase)); |
Raul Herbster | 9c1a382 | 2007-08-30 23:29:26 +0000 | [diff] [blame] | 359 | } |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 361 | void ARMCodeEmitter::emitWordLE(unsigned Binary) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 362 | DEBUG(errs() << " 0x"; |
| 363 | errs().write_hex(Binary) << "\n"); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 364 | MCE.emitWordLE(Binary); |
| 365 | } |
| 366 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 367 | void ARMCodeEmitter::emitDWordLE(uint64_t Binary) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 368 | DEBUG(errs() << " 0x"; |
| 369 | errs().write_hex(Binary) << "\n"); |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 370 | MCE.emitDWordLE(Binary); |
| 371 | } |
| 372 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 373 | void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 374 | DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI); |
Evan Cheng | 42d5ee06 | 2008-09-13 01:15:21 +0000 | [diff] [blame] | 375 | |
Devang Patel | af0e272 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 376 | MCE.processDebugLoc(MI.getDebugLoc(), true); |
Jeffrey Yasskin | 7540282 | 2009-07-17 18:49:39 +0000 | [diff] [blame] | 377 | |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 378 | ++NumEmitted; // Keep track of the # of mi's emitted |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 379 | switch (MI.getDesc().TSFlags & ARMII::FormMask) { |
Evan Cheng | ffa6d96 | 2008-11-13 23:36:57 +0000 | [diff] [blame] | 380 | default: { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 381 | llvm_unreachable("Unhandled instruction encoding format!"); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 382 | break; |
Evan Cheng | ffa6d96 | 2008-11-13 23:36:57 +0000 | [diff] [blame] | 383 | } |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 384 | case ARMII::Pseudo: |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 385 | emitPseudoInstruction(MI); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 386 | break; |
| 387 | case ARMII::DPFrm: |
| 388 | case ARMII::DPSoRegFrm: |
| 389 | emitDataProcessingInstruction(MI); |
| 390 | break; |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 391 | case ARMII::LdFrm: |
| 392 | case ARMII::StFrm: |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 393 | emitLoadStoreInstruction(MI); |
| 394 | break; |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 395 | case ARMII::LdMiscFrm: |
| 396 | case ARMII::StMiscFrm: |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 397 | emitMiscLoadStoreInstruction(MI); |
| 398 | break; |
Evan Cheng | 3c4a4ff | 2008-11-12 07:18:38 +0000 | [diff] [blame] | 399 | case ARMII::LdStMulFrm: |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 400 | emitLoadStoreMultipleInstruction(MI); |
| 401 | break; |
Evan Cheng | fbc9d41 | 2008-11-06 01:21:28 +0000 | [diff] [blame] | 402 | case ARMII::MulFrm: |
| 403 | emitMulFrmInstruction(MI); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 404 | break; |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 405 | case ARMII::ExtFrm: |
| 406 | emitExtendInstruction(MI); |
| 407 | break; |
Evan Cheng | 8b59db3 | 2008-11-07 01:41:35 +0000 | [diff] [blame] | 408 | case ARMII::ArithMiscFrm: |
| 409 | emitMiscArithInstruction(MI); |
| 410 | break; |
Bob Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 411 | case ARMII::SatFrm: |
| 412 | emitSaturateInstruction(MI); |
| 413 | break; |
Evan Cheng | 12c3a53 | 2008-11-06 17:48:05 +0000 | [diff] [blame] | 414 | case ARMII::BrFrm: |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 415 | emitBranchInstruction(MI); |
| 416 | break; |
Evan Cheng | 12c3a53 | 2008-11-06 17:48:05 +0000 | [diff] [blame] | 417 | case ARMII::BrMiscFrm: |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 418 | emitMiscBranchInstruction(MI); |
| 419 | break; |
Evan Cheng | 96581d3 | 2008-11-11 02:11:05 +0000 | [diff] [blame] | 420 | // VFP instructions. |
| 421 | case ARMII::VFPUnaryFrm: |
| 422 | case ARMII::VFPBinaryFrm: |
| 423 | emitVFPArithInstruction(MI); |
| 424 | break; |
Evan Cheng | 78be83d | 2008-11-11 19:40:26 +0000 | [diff] [blame] | 425 | case ARMII::VFPConv1Frm: |
| 426 | case ARMII::VFPConv2Frm: |
Evan Cheng | 0a0ab13 | 2008-11-11 22:46:12 +0000 | [diff] [blame] | 427 | case ARMII::VFPConv3Frm: |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 428 | case ARMII::VFPConv4Frm: |
| 429 | case ARMII::VFPConv5Frm: |
Evan Cheng | 78be83d | 2008-11-11 19:40:26 +0000 | [diff] [blame] | 430 | emitVFPConversionInstruction(MI); |
| 431 | break; |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 432 | case ARMII::VFPLdStFrm: |
| 433 | emitVFPLoadStoreInstruction(MI); |
| 434 | break; |
| 435 | case ARMII::VFPLdStMulFrm: |
| 436 | emitVFPLoadStoreMultipleInstruction(MI); |
| 437 | break; |
| 438 | case ARMII::VFPMiscFrm: |
| 439 | emitMiscInstruction(MI); |
| 440 | break; |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 441 | // NEON instructions. |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 442 | case ARMII::NGetLnFrm: |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 443 | case ARMII::NSetLnFrm: |
| 444 | emitNEONLaneInstruction(MI); |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 445 | break; |
Bob Wilson | 21773e7 | 2010-06-29 20:13:29 +0000 | [diff] [blame] | 446 | case ARMII::NDupFrm: |
| 447 | emitNEONDupInstruction(MI); |
| 448 | break; |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 449 | case ARMII::N1RegModImmFrm: |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 450 | emitNEON1RegModImmInstruction(MI); |
| 451 | break; |
| 452 | case ARMII::N2RegFrm: |
| 453 | emitNEON2RegInstruction(MI); |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 454 | break; |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 455 | case ARMII::N3RegFrm: |
| 456 | emitNEON3RegInstruction(MI); |
| 457 | break; |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 458 | } |
Devang Patel | af0e272 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 459 | MCE.processDebugLoc(MI.getDebugLoc(), false); |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 462 | void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) { |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 463 | unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index. |
| 464 | unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index. |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 465 | const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex]; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 466 | |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 467 | // Remember the CONSTPOOL_ENTRY address for later relocation. |
| 468 | JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue()); |
| 469 | |
| 470 | // Emit constpool island entry. In most cases, the actual values will be |
| 471 | // resolved and relocated after code emission. |
| 472 | if (MCPE.isMachineConstantPoolEntry()) { |
| 473 | ARMConstantPoolValue *ACPV = |
| 474 | static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal); |
| 475 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 476 | DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ " |
| 477 | << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n'); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 478 | |
Bob Wilson | 28989a8 | 2009-11-02 16:59:06 +0000 | [diff] [blame] | 479 | assert(ACPV->isGlobalValue() && "unsupported constant pool value"); |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 480 | const GlobalValue *GV = ACPV->getGV(); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 481 | if (GV) { |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 482 | Reloc::Model RelocM = TM.getRelocationModel(); |
Evan Cheng | e4e4ed3 | 2009-08-28 23:18:09 +0000 | [diff] [blame] | 483 | emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry, |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 484 | isa<Function>(GV), |
| 485 | Subtarget->GVIsIndirectSymbol(GV, RelocM), |
| 486 | (intptr_t)ACPV); |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 487 | } else { |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 488 | emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute); |
| 489 | } |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 490 | emitWordLE(0); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 491 | } else { |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 492 | const Constant *CV = MCPE.Val.ConstVal; |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 493 | |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 494 | DEBUG({ |
| 495 | errs() << " ** Constant pool #" << CPI << " @ " |
| 496 | << (void*)MCE.getCurrentPCValue() << " "; |
| 497 | if (const Function *F = dyn_cast<Function>(CV)) |
| 498 | errs() << F->getName(); |
| 499 | else |
| 500 | errs() << *CV; |
| 501 | errs() << '\n'; |
| 502 | }); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 503 | |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 504 | if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { |
Evan Cheng | 0866974 | 2009-09-10 01:23:53 +0000 | [diff] [blame] | 505 | emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 506 | emitWordLE(0); |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 507 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 508 | uint32_t Val = *(uint32_t*)CI->getValue().getRawData(); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 509 | emitWordLE(Val); |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 510 | } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 511 | if (CFP->getType()->isFloatTy()) |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 512 | emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); |
Chris Lattner | cf0fe8d | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 513 | else if (CFP->getType()->isDoubleTy()) |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 514 | emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); |
| 515 | else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 516 | llvm_unreachable("Unable to handle this constantpool entry!"); |
Evan Cheng | cb5201f | 2008-11-11 22:19:31 +0000 | [diff] [blame] | 517 | } |
| 518 | } else { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 519 | llvm_unreachable("Unable to handle this constantpool entry!"); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 524 | void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) { |
| 525 | const MachineOperand &MO0 = MI.getOperand(0); |
| 526 | const MachineOperand &MO1 = MI.getOperand(1); |
| 527 | |
| 528 | // Emit the 'movw' instruction. |
| 529 | unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000 |
| 530 | |
| 531 | unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF; |
| 532 | |
| 533 | // Set the conditional execution predicate. |
| 534 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 535 | |
| 536 | // Encode Rd. |
| 537 | Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; |
| 538 | |
| 539 | // Encode imm16 as imm4:imm12 |
| 540 | Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12 |
| 541 | Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4 |
| 542 | emitWordLE(Binary); |
| 543 | |
| 544 | unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16; |
| 545 | // Emit the 'movt' instruction. |
| 546 | Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100 |
| 547 | |
| 548 | // Set the conditional execution predicate. |
| 549 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 550 | |
| 551 | // Encode Rd. |
| 552 | Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; |
| 553 | |
| 554 | // Encode imm16 as imm4:imm1, same as movw above. |
| 555 | Binary |= Hi16 & 0xFFF; |
| 556 | Binary |= ((Hi16 >> 12) & 0xF) << 16; |
| 557 | emitWordLE(Binary); |
| 558 | } |
| 559 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 560 | void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) { |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 561 | const MachineOperand &MO0 = MI.getOperand(0); |
| 562 | const MachineOperand &MO1 = MI.getOperand(1); |
Bob Wilson | 5265a12 | 2010-03-11 00:46:22 +0000 | [diff] [blame] | 563 | assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) && |
| 564 | "Not a valid so_imm value!"); |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 565 | unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm()); |
| 566 | unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm()); |
| 567 | |
| 568 | // Emit the 'mov' instruction. |
| 569 | unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101 |
| 570 | |
| 571 | // Set the conditional execution predicate. |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 572 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 573 | |
| 574 | // Encode Rd. |
| 575 | Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; |
| 576 | |
| 577 | // Encode so_imm. |
| 578 | // Set bit I(25) to identify this is the immediate form of <shifter_op> |
| 579 | Binary |= 1 << ARMII::I_BitShift; |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 580 | Binary |= getMachineSoImmOpValue(V1); |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 581 | emitWordLE(Binary); |
| 582 | |
| 583 | // Now the 'orr' instruction. |
| 584 | Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100 |
| 585 | |
| 586 | // Set the conditional execution predicate. |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 587 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 588 | |
| 589 | // Encode Rd. |
| 590 | Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; |
| 591 | |
| 592 | // Encode Rn. |
| 593 | Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift; |
| 594 | |
| 595 | // Encode so_imm. |
| 596 | // Set bit I(25) to identify this is the immediate form of <shifter_op> |
| 597 | Binary |= 1 << ARMII::I_BitShift; |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 598 | Binary |= getMachineSoImmOpValue(V2); |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 599 | emitWordLE(Binary); |
| 600 | } |
| 601 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 602 | void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) { |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 603 | // It's basically add r, pc, (LJTI - $+8) |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 604 | |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 605 | const TargetInstrDesc &TID = MI.getDesc(); |
| 606 | |
| 607 | // Emit the 'add' instruction. |
| 608 | unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100 |
| 609 | |
| 610 | // Set the conditional execution predicate |
| 611 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 612 | |
| 613 | // Encode S bit if MI modifies CPSR. |
| 614 | Binary |= getAddrModeSBit(MI, TID); |
| 615 | |
| 616 | // Encode Rd. |
| 617 | Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; |
| 618 | |
| 619 | // Encode Rn which is PC. |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 620 | Binary |= getARMRegisterNumbering(ARM::PC) << ARMII::RegRnShift; |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 621 | |
| 622 | // Encode the displacement. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 623 | Binary |= 1 << ARMII::I_BitShift; |
| 624 | emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base); |
| 625 | |
| 626 | emitWordLE(Binary); |
| 627 | } |
| 628 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 629 | void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) { |
Evan Cheng | a956255 | 2008-11-14 20:09:11 +0000 | [diff] [blame] | 630 | unsigned Opcode = MI.getDesc().Opcode; |
| 631 | |
| 632 | // Part of binary is determined by TableGn. |
| 633 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 634 | |
| 635 | // Set the conditional execution predicate |
| 636 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 637 | |
| 638 | // Encode S bit if MI modifies CPSR. |
| 639 | if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag) |
| 640 | Binary |= 1 << ARMII::S_BitShift; |
| 641 | |
| 642 | // Encode register def if there is one. |
| 643 | Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; |
| 644 | |
| 645 | // Encode the shift operation. |
| 646 | switch (Opcode) { |
| 647 | default: break; |
| 648 | case ARM::MOVrx: |
| 649 | // rrx |
| 650 | Binary |= 0x6 << 4; |
| 651 | break; |
| 652 | case ARM::MOVsrl_flag: |
| 653 | // lsr #1 |
| 654 | Binary |= (0x2 << 4) | (1 << 7); |
| 655 | break; |
| 656 | case ARM::MOVsra_flag: |
| 657 | // asr #1 |
| 658 | Binary |= (0x4 << 4) | (1 << 7); |
| 659 | break; |
| 660 | } |
| 661 | |
| 662 | // Encode register Rm. |
| 663 | Binary |= getMachineOpValue(MI, 1); |
| 664 | |
| 665 | emitWordLE(Binary); |
| 666 | } |
| 667 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 668 | void ARMCodeEmitter::addPCLabel(unsigned LabelID) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 669 | DEBUG(errs() << " ** LPC" << LabelID << " @ " |
| 670 | << (void*)MCE.getCurrentPCValue() << '\n'); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 671 | JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue()); |
| 672 | } |
| 673 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 674 | void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) { |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 675 | unsigned Opcode = MI.getDesc().Opcode; |
| 676 | switch (Opcode) { |
| 677 | default: |
Evan Cheng | 5adb66a | 2009-09-28 09:14:39 +0000 | [diff] [blame] | 678 | llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction"); |
Xerxes Ranby | 99ccffe | 2010-07-22 17:28:34 +0000 | [diff] [blame] | 679 | case ARM::BX: |
| 680 | case ARM::BMOVPCRX: |
| 681 | case ARM::BXr9: |
| 682 | case ARM::BMOVPCRXr9: { |
| 683 | // First emit mov lr, pc |
| 684 | unsigned Binary = 0x01a0e00f; |
| 685 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 686 | emitWordLE(Binary); |
| 687 | |
| 688 | // and then emit the branch. |
| 689 | emitMiscBranchInstruction(MI); |
| 690 | break; |
| 691 | } |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 692 | case TargetOpcode::INLINEASM: { |
Evan Cheng | e3066ab | 2008-11-19 23:21:33 +0000 | [diff] [blame] | 693 | // We allow inline assembler nodes with empty bodies - they can |
| 694 | // implicitly define registers, which is ok for JIT. |
| 695 | if (MI.getOperand(0).getSymbolName()[0]) { |
Chris Lattner | 75361b6 | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 696 | report_fatal_error("JIT does not support inline asm!"); |
Evan Cheng | e3066ab | 2008-11-19 23:21:33 +0000 | [diff] [blame] | 697 | } |
Evan Cheng | ffa6d96 | 2008-11-13 23:36:57 +0000 | [diff] [blame] | 698 | break; |
| 699 | } |
Bill Wendling | 7431bea | 2010-07-16 22:20:36 +0000 | [diff] [blame] | 700 | case TargetOpcode::PROLOG_LABEL: |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 701 | case TargetOpcode::EH_LABEL: |
| 702 | MCE.emitLabel(MI.getOperand(0).getMCSymbol()); |
| 703 | break; |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 704 | case TargetOpcode::IMPLICIT_DEF: |
| 705 | case TargetOpcode::KILL: |
Evan Cheng | ffa6d96 | 2008-11-13 23:36:57 +0000 | [diff] [blame] | 706 | // Do nothing. |
| 707 | break; |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 708 | case ARM::CONSTPOOL_ENTRY: |
| 709 | emitConstPoolInstruction(MI); |
| 710 | break; |
| 711 | case ARM::PICADD: { |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 712 | // Remember of the address of the PC label for relocation later. |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 713 | addPCLabel(MI.getOperand(2).getImm()); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 714 | // PICADD is just an add instruction that implicitly read pc. |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 715 | emitDataProcessingInstruction(MI, 0, ARM::PC); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 716 | break; |
| 717 | } |
| 718 | case ARM::PICLDR: |
| 719 | case ARM::PICLDRB: |
| 720 | case ARM::PICSTR: |
| 721 | case ARM::PICSTRB: { |
| 722 | // Remember of the address of the PC label for relocation later. |
| 723 | addPCLabel(MI.getOperand(2).getImm()); |
| 724 | // These are just load / store instructions that implicitly read pc. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 725 | emitLoadStoreInstruction(MI, 0, ARM::PC); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | case ARM::PICLDRH: |
| 729 | case ARM::PICLDRSH: |
| 730 | case ARM::PICLDRSB: |
| 731 | case ARM::PICSTRH: { |
| 732 | // Remember of the address of the PC label for relocation later. |
| 733 | addPCLabel(MI.getOperand(2).getImm()); |
| 734 | // These are just load / store instructions that implicitly read pc. |
| 735 | emitMiscLoadStoreInstruction(MI, ARM::PC); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 736 | break; |
| 737 | } |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 738 | |
| 739 | case ARM::MOVi32imm: |
| 740 | emitMOVi32immInstruction(MI); |
| 741 | break; |
| 742 | |
Evan Cheng | 9092213 | 2008-11-06 02:25:39 +0000 | [diff] [blame] | 743 | case ARM::MOVi2pieces: |
| 744 | // Two instructions to materialize a constant. |
| 745 | emitMOVi2piecesInstruction(MI); |
| 746 | break; |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 747 | case ARM::LEApcrelJT: |
| 748 | // Materialize jumptable address. |
| 749 | emitLEApcrelJTInstruction(MI); |
| 750 | break; |
Evan Cheng | a956255 | 2008-11-14 20:09:11 +0000 | [diff] [blame] | 751 | case ARM::MOVrx: |
| 752 | case ARM::MOVsrl_flag: |
| 753 | case ARM::MOVsra_flag: |
| 754 | emitPseudoMoveInstruction(MI); |
| 755 | break; |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 759 | unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI, |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 760 | const TargetInstrDesc &TID, |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 761 | const MachineOperand &MO, |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 762 | unsigned OpIdx) { |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 763 | unsigned Binary = getMachineOpValue(MI, MO); |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 764 | |
| 765 | const MachineOperand &MO1 = MI.getOperand(OpIdx + 1); |
| 766 | const MachineOperand &MO2 = MI.getOperand(OpIdx + 2); |
| 767 | ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm()); |
| 768 | |
| 769 | // Encode the shift opcode. |
| 770 | unsigned SBits = 0; |
| 771 | unsigned Rs = MO1.getReg(); |
| 772 | if (Rs) { |
| 773 | // Set shift operand (bit[7:4]). |
| 774 | // LSL - 0001 |
| 775 | // LSR - 0011 |
| 776 | // ASR - 0101 |
| 777 | // ROR - 0111 |
| 778 | // RRX - 0110 and bit[11:8] clear. |
| 779 | switch (SOpc) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 780 | default: llvm_unreachable("Unknown shift opc!"); |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 781 | case ARM_AM::lsl: SBits = 0x1; break; |
| 782 | case ARM_AM::lsr: SBits = 0x3; break; |
| 783 | case ARM_AM::asr: SBits = 0x5; break; |
| 784 | case ARM_AM::ror: SBits = 0x7; break; |
| 785 | case ARM_AM::rrx: SBits = 0x6; break; |
| 786 | } |
| 787 | } else { |
| 788 | // Set shift operand (bit[6:4]). |
| 789 | // LSL - 000 |
| 790 | // LSR - 010 |
| 791 | // ASR - 100 |
| 792 | // ROR - 110 |
| 793 | switch (SOpc) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 794 | default: llvm_unreachable("Unknown shift opc!"); |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 795 | case ARM_AM::lsl: SBits = 0x0; break; |
| 796 | case ARM_AM::lsr: SBits = 0x2; break; |
| 797 | case ARM_AM::asr: SBits = 0x4; break; |
| 798 | case ARM_AM::ror: SBits = 0x6; break; |
| 799 | } |
| 800 | } |
| 801 | Binary |= SBits << 4; |
| 802 | if (SOpc == ARM_AM::rrx) |
| 803 | return Binary; |
| 804 | |
| 805 | // Encode the shift operation Rs or shift_imm (except rrx). |
| 806 | if (Rs) { |
| 807 | // Encode Rs bit[11:8]. |
| 808 | assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0); |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 809 | return Binary | (getARMRegisterNumbering(Rs) << ARMII::RegRsShift); |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Encode shift_imm bit[11:7]. |
| 813 | return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7; |
| 814 | } |
| 815 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 816 | unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) { |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 817 | int SoImmVal = ARM_AM::getSOImmVal(SoImm); |
| 818 | assert(SoImmVal != -1 && "Not a valid so_imm value!"); |
| 819 | |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 820 | // Encode rotate_imm. |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 821 | unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1) |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 822 | << ARMII::SoRotImmShift; |
| 823 | |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 824 | // Encode immed_8. |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 825 | Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 826 | return Binary; |
| 827 | } |
| 828 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 829 | unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI, |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 830 | const TargetInstrDesc &TID) const { |
Evan Cheng | 97c573d | 2008-11-20 02:25:51 +0000 | [diff] [blame] | 831 | for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){ |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 832 | const MachineOperand &MO = MI.getOperand(i-1); |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 833 | if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 834 | return 1 << ARMII::S_BitShift; |
| 835 | } |
| 836 | return 0; |
| 837 | } |
| 838 | |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 839 | void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI, |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 840 | unsigned ImplicitRd, |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 841 | unsigned ImplicitRn) { |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 842 | const TargetInstrDesc &TID = MI.getDesc(); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 843 | |
| 844 | // Part of binary is determined by TableGn. |
| 845 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 846 | |
Jim Grosbach | 3341262 | 2008-10-07 19:05:35 +0000 | [diff] [blame] | 847 | // Set the conditional execution predicate |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 848 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 849 | |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 850 | // Encode S bit if MI modifies CPSR. |
Jim Grosbach | 0a4b9dc | 2008-11-03 18:38:31 +0000 | [diff] [blame] | 851 | Binary |= getAddrModeSBit(MI, TID); |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 852 | |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 853 | // Encode register def if there is one. |
Evan Cheng | 49a9f29 | 2008-09-12 22:45:55 +0000 | [diff] [blame] | 854 | unsigned NumDefs = TID.getNumDefs(); |
Evan Cheng | a964b7d | 2008-09-12 23:15:39 +0000 | [diff] [blame] | 855 | unsigned OpIdx = 0; |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 856 | if (NumDefs) |
| 857 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; |
| 858 | else if (ImplicitRd) |
| 859 | // Special handling for implicit use (e.g. PC). |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 860 | Binary |= (getARMRegisterNumbering(ImplicitRd) << ARMII::RegRdShift); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 861 | |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 862 | if (TID.Opcode == ARM::MOVi16) { |
| 863 | // Get immediate from MI. |
| 864 | unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx), |
| 865 | ARM::reloc_arm_movw); |
| 866 | // Encode imm which is the same as in emitMOVi32immInstruction(). |
| 867 | Binary |= Lo16 & 0xFFF; |
| 868 | Binary |= ((Lo16 >> 12) & 0xF) << 16; |
| 869 | emitWordLE(Binary); |
| 870 | return; |
| 871 | } else if(TID.Opcode == ARM::MOVTi16) { |
| 872 | unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx), |
| 873 | ARM::reloc_arm_movt) >> 16); |
| 874 | Binary |= Hi16 & 0xFFF; |
| 875 | Binary |= ((Hi16 >> 12) & 0xF) << 16; |
| 876 | emitWordLE(Binary); |
| 877 | return; |
Shih-wei Liao | 9f3b6a3 | 2010-05-26 04:46:50 +0000 | [diff] [blame] | 878 | } else if ((TID.Opcode == ARM::BFC) || (TID.Opcode == ARM::BFI)) { |
Shih-wei Liao | 6d37a29 | 2010-05-26 00:25:05 +0000 | [diff] [blame] | 879 | uint32_t v = ~MI.getOperand(2).getImm(); |
| 880 | int32_t lsb = CountTrailingZeros_32(v); |
| 881 | int32_t msb = (32 - CountLeadingZeros_32(v)) - 1; |
Shih-wei Liao | 45469f3 | 2010-05-26 03:21:39 +0000 | [diff] [blame] | 882 | // Instr{20-16} = msb, Instr{11-7} = lsb |
Shih-wei Liao | 6d37a29 | 2010-05-26 00:25:05 +0000 | [diff] [blame] | 883 | Binary |= (msb & 0x1F) << 16; |
| 884 | Binary |= (lsb & 0x1F) << 7; |
| 885 | emitWordLE(Binary); |
| 886 | return; |
Shih-wei Liao | 45469f3 | 2010-05-26 03:21:39 +0000 | [diff] [blame] | 887 | } else if ((TID.Opcode == ARM::UBFX) || (TID.Opcode == ARM::SBFX)) { |
| 888 | // Encode Rn in Instr{0-3} |
| 889 | Binary |= getMachineOpValue(MI, OpIdx++); |
| 890 | |
| 891 | uint32_t lsb = MI.getOperand(OpIdx++).getImm(); |
| 892 | uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1; |
| 893 | |
| 894 | // Instr{20-16} = widthm1, Instr{11-7} = lsb |
| 895 | Binary |= (widthm1 & 0x1F) << 16; |
| 896 | Binary |= (lsb & 0x1F) << 7; |
| 897 | emitWordLE(Binary); |
| 898 | return; |
Zonr Chang | f86399b | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Evan Cheng | d87293c | 2008-11-06 08:47:38 +0000 | [diff] [blame] | 901 | // If this is a two-address operand, skip it. e.g. MOVCCr operand 1. |
| 902 | if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
| 903 | ++OpIdx; |
| 904 | |
Jim Grosbach | efd30ba | 2008-10-01 18:16:49 +0000 | [diff] [blame] | 905 | // Encode first non-shifter register operand if there is one. |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 906 | bool isUnary = TID.TSFlags & ARMII::UnaryDP; |
| 907 | if (!isUnary) { |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 908 | if (ImplicitRn) |
| 909 | // Special handling for implicit use (e.g. PC). |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 910 | Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift); |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 911 | else { |
| 912 | Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift; |
| 913 | ++OpIdx; |
| 914 | } |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 917 | // Encode shifter operand. |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 918 | const MachineOperand &MO = MI.getOperand(OpIdx); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 919 | if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) { |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 920 | // Encode SoReg. |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 921 | emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx)); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 922 | return; |
| 923 | } |
Evan Cheng | eb4ed4b | 2008-10-31 19:10:44 +0000 | [diff] [blame] | 924 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 925 | if (MO.isReg()) { |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 926 | // Encode register Rm. |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 927 | emitWordLE(Binary | getARMRegisterNumbering(MO.getReg())); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 930 | |
Evan Cheng | 5f1db7b | 2008-09-12 22:01:15 +0000 | [diff] [blame] | 931 | // Encode so_imm. |
Evan Cheng | e7cbe41 | 2009-07-08 21:03:57 +0000 | [diff] [blame] | 932 | Binary |= getMachineSoImmOpValue((unsigned)MO.getImm()); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 933 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 934 | emitWordLE(Binary); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 937 | void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI, |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 938 | unsigned ImplicitRd, |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 939 | unsigned ImplicitRn) { |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 940 | const TargetInstrDesc &TID = MI.getDesc(); |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 941 | unsigned Form = TID.TSFlags & ARMII::FormMask; |
| 942 | bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0; |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 943 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 944 | // Part of binary is determined by TableGn. |
| 945 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 946 | |
Jim Grosbach | 3341262 | 2008-10-07 19:05:35 +0000 | [diff] [blame] | 947 | // Set the conditional execution predicate |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 948 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 949 | |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 950 | unsigned OpIdx = 0; |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 951 | |
| 952 | // Operand 0 of a pre- and post-indexed store is the address base |
| 953 | // writeback. Skip it. |
| 954 | bool Skipped = false; |
| 955 | if (IsPrePost && Form == ARMII::StFrm) { |
| 956 | ++OpIdx; |
| 957 | Skipped = true; |
| 958 | } |
| 959 | |
| 960 | // Set first operand |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 961 | if (ImplicitRd) |
| 962 | // Special handling for implicit use (e.g. PC). |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 963 | Binary |= (getARMRegisterNumbering(ImplicitRd) << ARMII::RegRdShift); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 964 | else |
| 965 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 966 | |
| 967 | // Set second operand |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 968 | if (ImplicitRn) |
| 969 | // Special handling for implicit use (e.g. PC). |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 970 | Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 971 | else |
| 972 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 973 | |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 974 | // If this is a two-address operand, skip it. e.g. LDR_PRE. |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 975 | if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 976 | ++OpIdx; |
| 977 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 978 | const MachineOperand &MO2 = MI.getOperand(OpIdx); |
Evan Cheng | d87293c | 2008-11-06 08:47:38 +0000 | [diff] [blame] | 979 | unsigned AM2Opc = (ImplicitRn == ARM::PC) |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 980 | ? 0 : MI.getOperand(OpIdx+1).getImm(); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 981 | |
Evan Cheng | e7de7e3 | 2008-09-13 01:44:01 +0000 | [diff] [blame] | 982 | // Set bit U(23) according to sign of immed value (positive or negative). |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 983 | Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) << |
Evan Cheng | e7de7e3 | 2008-09-13 01:44:01 +0000 | [diff] [blame] | 984 | ARMII::U_BitShift); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 985 | if (!MO2.getReg()) { // is immediate |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 986 | if (ARM_AM::getAM2Offset(AM2Opc)) |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 987 | // Set the value of offset_12 field |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 988 | Binary |= ARM_AM::getAM2Offset(AM2Opc); |
| 989 | emitWordLE(Binary); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 990 | return; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | // Set bit I(25), because this is not in immediate enconding. |
| 994 | Binary |= 1 << ARMII::I_BitShift; |
| 995 | assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg())); |
| 996 | // Set bit[3:0] to the corresponding Rm register |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 997 | Binary |= getARMRegisterNumbering(MO2.getReg()); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 998 | |
Evan Cheng | 7063291 | 2008-11-12 07:34:37 +0000 | [diff] [blame] | 999 | // If this instr is in scaled register offset/index instruction, set |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1000 | // shift_immed(bit[11:7]) and shift(bit[6:5]) fields. |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1001 | if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) { |
Evan Cheng | 7063291 | 2008-11-12 07:34:37 +0000 | [diff] [blame] | 1002 | Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift |
| 1003 | Binary |= ShImm << ARMII::ShiftShift; // shift_immed |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1006 | emitWordLE(Binary); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1009 | void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI, |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 1010 | unsigned ImplicitRn) { |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 1011 | const TargetInstrDesc &TID = MI.getDesc(); |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 1012 | unsigned Form = TID.TSFlags & ARMII::FormMask; |
| 1013 | bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0; |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 1014 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1015 | // Part of binary is determined by TableGn. |
| 1016 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1017 | |
Jim Grosbach | 3341262 | 2008-10-07 19:05:35 +0000 | [diff] [blame] | 1018 | // Set the conditional execution predicate |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1019 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | 057d0c3 | 2008-09-18 07:28:19 +0000 | [diff] [blame] | 1020 | |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 1021 | unsigned OpIdx = 0; |
| 1022 | |
| 1023 | // Operand 0 of a pre- and post-indexed store is the address base |
| 1024 | // writeback. Skip it. |
| 1025 | bool Skipped = false; |
| 1026 | if (IsPrePost && Form == ARMII::StMiscFrm) { |
| 1027 | ++OpIdx; |
| 1028 | Skipped = true; |
| 1029 | } |
| 1030 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1031 | // Set first operand |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 1032 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1033 | |
Evan Cheng | 358dec5 | 2009-06-15 08:28:29 +0000 | [diff] [blame] | 1034 | // Skip LDRD and STRD's second operand. |
| 1035 | if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD) |
| 1036 | ++OpIdx; |
| 1037 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1038 | // Set second operand |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1039 | if (ImplicitRn) |
| 1040 | // Special handling for implicit use (e.g. PC). |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1041 | Binary |= (getARMRegisterNumbering(ImplicitRn) << ARMII::RegRnShift); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 1042 | else |
| 1043 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1044 | |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 1045 | // If this is a two-address operand, skip it. e.g. LDRH_POST. |
Evan Cheng | 148cad8 | 2008-11-13 07:34:59 +0000 | [diff] [blame] | 1046 | if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
Evan Cheng | 05c356e | 2008-11-08 01:44:13 +0000 | [diff] [blame] | 1047 | ++OpIdx; |
| 1048 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1049 | const MachineOperand &MO2 = MI.getOperand(OpIdx); |
Evan Cheng | d87293c | 2008-11-06 08:47:38 +0000 | [diff] [blame] | 1050 | unsigned AM3Opc = (ImplicitRn == ARM::PC) |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1051 | ? 0 : MI.getOperand(OpIdx+1).getImm(); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1052 | |
Evan Cheng | e7de7e3 | 2008-09-13 01:44:01 +0000 | [diff] [blame] | 1053 | // Set bit U(23) according to sign of immed value (positive or negative) |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1054 | Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) << |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1055 | ARMII::U_BitShift); |
| 1056 | |
| 1057 | // If this instr is in register offset/index encoding, set bit[3:0] |
| 1058 | // to the corresponding Rm register. |
| 1059 | if (MO2.getReg()) { |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1060 | Binary |= getARMRegisterNumbering(MO2.getReg()); |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1061 | emitWordLE(Binary); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1062 | return; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Evan Cheng | d87293c | 2008-11-06 08:47:38 +0000 | [diff] [blame] | 1065 | // This instr is in immediate offset/index encoding, set bit 22 to 1. |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1066 | Binary |= 1 << ARMII::AM3_I_BitShift; |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1067 | if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) { |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1068 | // Set operands |
Evan Cheng | 7063291 | 2008-11-12 07:34:37 +0000 | [diff] [blame] | 1069 | Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH |
| 1070 | Binary |= (ImmOffs & 0xF); // immedL |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1073 | emitWordLE(Binary); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1076 | static unsigned getAddrModeUPBits(unsigned Mode) { |
| 1077 | unsigned Binary = 0; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1078 | |
| 1079 | // Set addressing mode by modifying bits U(23) and P(24) |
| 1080 | // IA - Increment after - bit U = 1 and bit P = 0 |
| 1081 | // IB - Increment before - bit U = 1 and bit P = 1 |
| 1082 | // DA - Decrement after - bit U = 0 and bit P = 0 |
| 1083 | // DB - Decrement before - bit U = 0 and bit P = 1 |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1084 | switch (Mode) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1085 | default: llvm_unreachable("Unknown addressing sub-mode!"); |
Evan Cheng | 10bf734 | 2009-09-09 23:55:03 +0000 | [diff] [blame] | 1086 | case ARM_AM::da: break; |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1087 | case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break; |
| 1088 | case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break; |
| 1089 | case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1092 | return Binary; |
| 1093 | } |
| 1094 | |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1095 | void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) { |
| 1096 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1097 | bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0; |
| 1098 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1099 | // Part of binary is determined by TableGn. |
| 1100 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1101 | |
| 1102 | // Set the conditional execution predicate |
| 1103 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1104 | |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1105 | // Skip operand 0 of an instruction with base register update. |
| 1106 | unsigned OpIdx = 0; |
| 1107 | if (IsUpdating) |
| 1108 | ++OpIdx; |
| 1109 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1110 | // Set base address operand |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1111 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1112 | |
| 1113 | // Set addressing mode by modifying bits U(23) and P(24) |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1114 | const MachineOperand &MO = MI.getOperand(OpIdx++); |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1115 | Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm())); |
| 1116 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1117 | // Set bit W(21) |
Bob Wilson | ab34605 | 2010-03-16 17:46:45 +0000 | [diff] [blame] | 1118 | if (IsUpdating) |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1119 | Binary |= 0x1 << ARMII::W_BitShift; |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1120 | |
| 1121 | // Set registers |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1122 | for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) { |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1123 | const MachineOperand &MO = MI.getOperand(i); |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1124 | if (!MO.isReg() || MO.isImplicit()) |
| 1125 | break; |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1126 | unsigned RegNum = getARMRegisterNumbering(MO.getReg()); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1127 | assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && |
| 1128 | RegNum < 16); |
| 1129 | Binary |= 0x1 << RegNum; |
| 1130 | } |
| 1131 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1132 | emitWordLE(Binary); |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1135 | void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) { |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1136 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1137 | |
| 1138 | // Part of binary is determined by TableGn. |
| 1139 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1140 | |
Jim Grosbach | 0a4b9dc | 2008-11-03 18:38:31 +0000 | [diff] [blame] | 1141 | // Set the conditional execution predicate |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1142 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Jim Grosbach | 0a4b9dc | 2008-11-03 18:38:31 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Encode S bit if MI modifies CPSR. |
| 1145 | Binary |= getAddrModeSBit(MI, TID); |
| 1146 | |
| 1147 | // 32x32->64bit operations have two destination registers. The number |
| 1148 | // of register definitions will tell us if that's what we're dealing with. |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1149 | unsigned OpIdx = 0; |
Jim Grosbach | 0a4b9dc | 2008-11-03 18:38:31 +0000 | [diff] [blame] | 1150 | if (TID.getNumDefs() == 2) |
| 1151 | Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift; |
| 1152 | |
| 1153 | // Encode Rd |
| 1154 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift; |
| 1155 | |
| 1156 | // Encode Rm |
| 1157 | Binary |= getMachineOpValue(MI, OpIdx++); |
| 1158 | |
| 1159 | // Encode Rs |
| 1160 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift; |
| 1161 | |
Evan Cheng | fbc9d41 | 2008-11-06 01:21:28 +0000 | [diff] [blame] | 1162 | // Many multiple instructions (e.g. MLA) have three src operands. Encode |
| 1163 | // it as Rn (for multiply, that's in the same offset as RdLo. |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1164 | if (TID.getNumOperands() > OpIdx && |
| 1165 | !TID.OpInfo[OpIdx].isPredicate() && |
| 1166 | !TID.OpInfo[OpIdx].isOptionalDef()) |
| 1167 | Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift; |
| 1168 | |
| 1169 | emitWordLE(Binary); |
| 1170 | } |
| 1171 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1172 | void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) { |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1173 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1174 | |
| 1175 | // Part of binary is determined by TableGn. |
| 1176 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1177 | |
| 1178 | // Set the conditional execution predicate |
| 1179 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1180 | |
| 1181 | unsigned OpIdx = 0; |
| 1182 | |
| 1183 | // Encode Rd |
| 1184 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; |
| 1185 | |
| 1186 | const MachineOperand &MO1 = MI.getOperand(OpIdx++); |
| 1187 | const MachineOperand &MO2 = MI.getOperand(OpIdx); |
| 1188 | if (MO2.isReg()) { |
| 1189 | // Two register operand form. |
| 1190 | // Encode Rn. |
| 1191 | Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift; |
| 1192 | |
| 1193 | // Encode Rm. |
| 1194 | Binary |= getMachineOpValue(MI, MO2); |
| 1195 | ++OpIdx; |
| 1196 | } else { |
| 1197 | Binary |= getMachineOpValue(MI, MO1); |
| 1198 | } |
| 1199 | |
| 1200 | // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand. |
| 1201 | if (MI.getOperand(OpIdx).isImm() && |
| 1202 | !TID.OpInfo[OpIdx].isPredicate() && |
| 1203 | !TID.OpInfo[OpIdx].isOptionalDef()) |
| 1204 | Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift; |
Evan Cheng | fbc9d41 | 2008-11-06 01:21:28 +0000 | [diff] [blame] | 1205 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1206 | emitWordLE(Binary); |
Jim Grosbach | 0a4b9dc | 2008-11-03 18:38:31 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1209 | void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) { |
Evan Cheng | 8b59db3 | 2008-11-07 01:41:35 +0000 | [diff] [blame] | 1210 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1211 | |
| 1212 | // Part of binary is determined by TableGn. |
| 1213 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1214 | |
| 1215 | // Set the conditional execution predicate |
| 1216 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1217 | |
| 1218 | unsigned OpIdx = 0; |
| 1219 | |
| 1220 | // Encode Rd |
| 1221 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; |
| 1222 | |
| 1223 | const MachineOperand &MO = MI.getOperand(OpIdx++); |
| 1224 | if (OpIdx == TID.getNumOperands() || |
| 1225 | TID.OpInfo[OpIdx].isPredicate() || |
| 1226 | TID.OpInfo[OpIdx].isOptionalDef()) { |
| 1227 | // Encode Rm and it's done. |
| 1228 | Binary |= getMachineOpValue(MI, MO); |
| 1229 | emitWordLE(Binary); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | // Encode Rn. |
| 1234 | Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift; |
| 1235 | |
| 1236 | // Encode Rm. |
| 1237 | Binary |= getMachineOpValue(MI, OpIdx++); |
| 1238 | |
| 1239 | // Encode shift_imm. |
| 1240 | unsigned ShiftAmt = MI.getOperand(OpIdx).getImm(); |
Bob Wilson | f955f29 | 2010-08-17 17:23:19 +0000 | [diff] [blame] | 1241 | if (TID.Opcode == ARM::PKHTB) { |
| 1242 | assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!"); |
| 1243 | if (ShiftAmt == 32) |
| 1244 | ShiftAmt = 0; |
| 1245 | } |
Evan Cheng | 8b59db3 | 2008-11-07 01:41:35 +0000 | [diff] [blame] | 1246 | assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!"); |
| 1247 | Binary |= ShiftAmt << ARMII::ShiftShift; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1248 | |
Evan Cheng | 8b59db3 | 2008-11-07 01:41:35 +0000 | [diff] [blame] | 1249 | emitWordLE(Binary); |
| 1250 | } |
| 1251 | |
Bob Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 1252 | void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) { |
| 1253 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1254 | |
| 1255 | // Part of binary is determined by TableGen. |
| 1256 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1257 | |
| 1258 | // Set the conditional execution predicate |
| 1259 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1260 | |
| 1261 | // Encode Rd |
| 1262 | Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; |
| 1263 | |
| 1264 | // Encode saturate bit position. |
| 1265 | unsigned Pos = MI.getOperand(1).getImm(); |
Bob Wilson | eaf1c98 | 2010-08-11 23:10:46 +0000 | [diff] [blame] | 1266 | if (TID.Opcode == ARM::SSAT || TID.Opcode == ARM::SSAT16) |
Bob Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 1267 | Pos -= 1; |
| 1268 | assert((Pos < 16 || (Pos < 32 && |
| 1269 | TID.Opcode != ARM::SSAT16 && |
| 1270 | TID.Opcode != ARM::USAT16)) && |
| 1271 | "saturate bit position out of range"); |
| 1272 | Binary |= Pos << 16; |
| 1273 | |
| 1274 | // Encode Rm |
| 1275 | Binary |= getMachineOpValue(MI, 2); |
| 1276 | |
| 1277 | // Encode shift_imm. |
| 1278 | if (TID.getNumOperands() == 4) { |
Bob Wilson | eaf1c98 | 2010-08-11 23:10:46 +0000 | [diff] [blame] | 1279 | unsigned ShiftOp = MI.getOperand(3).getImm(); |
| 1280 | ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp); |
| 1281 | if (Opc == ARM_AM::asr) |
| 1282 | Binary |= (1 << 6); |
Bob Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 1283 | unsigned ShiftAmt = MI.getOperand(3).getImm(); |
Bob Wilson | eaf1c98 | 2010-08-11 23:10:46 +0000 | [diff] [blame] | 1284 | if (ShiftAmt == 32 && Opc == ARM_AM::asr) |
Bob Wilson | 9a1c189 | 2010-08-11 00:01:18 +0000 | [diff] [blame] | 1285 | ShiftAmt = 0; |
| 1286 | assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!"); |
| 1287 | Binary |= ShiftAmt << ARMII::ShiftShift; |
| 1288 | } |
| 1289 | |
| 1290 | emitWordLE(Binary); |
| 1291 | } |
| 1292 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1293 | void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) { |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1294 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1295 | |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 1296 | if (TID.Opcode == ARM::TPsoft) { |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1297 | llvm_unreachable("ARM::TPsoft FIXME"); // FIXME |
Torok Edwin | dac237e | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 1298 | } |
Evan Cheng | 12c3a53 | 2008-11-06 17:48:05 +0000 | [diff] [blame] | 1299 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1300 | // Part of binary is determined by TableGn. |
| 1301 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1302 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1303 | // Set the conditional execution predicate |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1304 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1305 | |
| 1306 | // Set signed_immed_24 field |
| 1307 | Binary |= getMachineOpValue(MI, 0); |
| 1308 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1309 | emitWordLE(Binary); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1312 | void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) { |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 1313 | // Remember the base address of the inline jump table. |
Evan Cheng | 5788d1a | 2008-12-10 02:32:19 +0000 | [diff] [blame] | 1314 | uintptr_t JTBase = MCE.getCurrentPCValue(); |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 1315 | JTI->addJumpTableBaseAddr(JTIndex, JTBase); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1316 | DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase |
| 1317 | << '\n'); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 1318 | |
| 1319 | // Now emit the jump table entries. |
| 1320 | const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs; |
| 1321 | for (unsigned i = 0, e = MBBs.size(); i != e; ++i) { |
| 1322 | if (IsPIC) |
| 1323 | // DestBB address - JT base. |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 1324 | emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 1325 | else |
| 1326 | // Absolute DestBB address. |
| 1327 | emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute); |
| 1328 | emitWordLE(0); |
| 1329 | } |
| 1330 | } |
| 1331 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1332 | void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) { |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1333 | const TargetInstrDesc &TID = MI.getDesc(); |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1334 | |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 1335 | // Handle jump tables. |
Evan Cheng | 90daf4d | 2009-07-25 00:13:11 +0000 | [diff] [blame] | 1336 | if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) { |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 1337 | // First emit a ldr pc, [] instruction. |
| 1338 | emitDataProcessingInstruction(MI, ARM::PC); |
| 1339 | |
| 1340 | // Then emit the inline jump table. |
Evan Cheng | c9a4153 | 2009-07-08 00:05:05 +0000 | [diff] [blame] | 1341 | unsigned JTIndex = |
Evan Cheng | 90daf4d | 2009-07-25 00:13:11 +0000 | [diff] [blame] | 1342 | (TID.Opcode == ARM::BR_JTr) |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 1343 | ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex(); |
| 1344 | emitInlineJumpTable(JTIndex); |
| 1345 | return; |
Evan Cheng | 90daf4d | 2009-07-25 00:13:11 +0000 | [diff] [blame] | 1346 | } else if (TID.Opcode == ARM::BR_JTm) { |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 1347 | // First emit a ldr pc, [] instruction. |
| 1348 | emitLoadStoreInstruction(MI, ARM::PC); |
| 1349 | |
| 1350 | // Then emit the inline jump table. |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 1351 | emitInlineJumpTable(MI.getOperand(3).getIndex()); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 1352 | return; |
| 1353 | } |
| 1354 | |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1355 | // Part of binary is determined by TableGn. |
| 1356 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1357 | |
| 1358 | // Set the conditional execution predicate |
Evan Cheng | 97f48c3 | 2008-11-06 22:15:19 +0000 | [diff] [blame] | 1359 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1360 | |
Anton Korobeynikov | ce7bf1c | 2010-03-06 19:39:36 +0000 | [diff] [blame] | 1361 | if (TID.Opcode == ARM::BX_RET || TID.Opcode == ARM::MOVPCLR) |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1362 | // The return register is LR. |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1363 | Binary |= getARMRegisterNumbering(ARM::LR); |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1364 | else |
Evan Cheng | edda31c | 2008-11-05 18:35:52 +0000 | [diff] [blame] | 1365 | // otherwise, set the return register |
| 1366 | Binary |= getMachineOpValue(MI, 0); |
| 1367 | |
Evan Cheng | 83b5cf0 | 2008-11-05 23:22:34 +0000 | [diff] [blame] | 1368 | emitWordLE(Binary); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 1369 | } |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1370 | |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1371 | static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) { |
Evan Cheng | d06d48d | 2008-11-12 02:19:38 +0000 | [diff] [blame] | 1372 | unsigned RegD = MI.getOperand(OpIdx).getReg(); |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1373 | unsigned Binary = 0; |
Jim Grosbach | 7e2c04f | 2010-09-15 19:44:57 +0000 | [diff] [blame] | 1374 | bool isSPVFP = ARM::SPRRegisterClass->contains(RegD); |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1375 | RegD = getARMRegisterNumbering(RegD); |
Evan Cheng | d06d48d | 2008-11-12 02:19:38 +0000 | [diff] [blame] | 1376 | if (!isSPVFP) |
| 1377 | Binary |= RegD << ARMII::RegRdShift; |
| 1378 | else { |
| 1379 | Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift; |
| 1380 | Binary |= (RegD & 0x01) << ARMII::D_BitShift; |
| 1381 | } |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1382 | return Binary; |
| 1383 | } |
Evan Cheng | 78be83d | 2008-11-11 19:40:26 +0000 | [diff] [blame] | 1384 | |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1385 | static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) { |
Evan Cheng | d06d48d | 2008-11-12 02:19:38 +0000 | [diff] [blame] | 1386 | unsigned RegN = MI.getOperand(OpIdx).getReg(); |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1387 | unsigned Binary = 0; |
Jim Grosbach | 7e2c04f | 2010-09-15 19:44:57 +0000 | [diff] [blame] | 1388 | bool isSPVFP = ARM::SPRRegisterClass->contains(RegN); |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1389 | RegN = getARMRegisterNumbering(RegN); |
Evan Cheng | d06d48d | 2008-11-12 02:19:38 +0000 | [diff] [blame] | 1390 | if (!isSPVFP) |
| 1391 | Binary |= RegN << ARMII::RegRnShift; |
| 1392 | else { |
| 1393 | Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift; |
| 1394 | Binary |= (RegN & 0x01) << ARMII::N_BitShift; |
| 1395 | } |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1396 | return Binary; |
| 1397 | } |
Evan Cheng | d06d48d | 2008-11-12 02:19:38 +0000 | [diff] [blame] | 1398 | |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1399 | static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) { |
| 1400 | unsigned RegM = MI.getOperand(OpIdx).getReg(); |
| 1401 | unsigned Binary = 0; |
Jim Grosbach | 7e2c04f | 2010-09-15 19:44:57 +0000 | [diff] [blame] | 1402 | bool isSPVFP = ARM::SPRRegisterClass->contains(RegM); |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1403 | RegM = getARMRegisterNumbering(RegM); |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1404 | if (!isSPVFP) |
| 1405 | Binary |= RegM; |
| 1406 | else { |
| 1407 | Binary |= ((RegM & 0x1E) >> 1); |
| 1408 | Binary |= (RegM & 0x01) << ARMII::M_BitShift; |
Evan Cheng | 78be83d | 2008-11-11 19:40:26 +0000 | [diff] [blame] | 1409 | } |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1410 | return Binary; |
| 1411 | } |
| 1412 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1413 | void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) { |
Evan Cheng | 3c4a4ff | 2008-11-12 07:18:38 +0000 | [diff] [blame] | 1414 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1415 | |
| 1416 | // Part of binary is determined by TableGn. |
| 1417 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1418 | |
| 1419 | // Set the conditional execution predicate |
| 1420 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1421 | |
| 1422 | unsigned OpIdx = 0; |
| 1423 | assert((Binary & ARMII::D_BitShift) == 0 && |
| 1424 | (Binary & ARMII::N_BitShift) == 0 && |
| 1425 | (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!"); |
| 1426 | |
| 1427 | // Encode Dd / Sd. |
| 1428 | Binary |= encodeVFPRd(MI, OpIdx++); |
| 1429 | |
| 1430 | // If this is a two-address operand, skip it, e.g. FMACD. |
| 1431 | if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
| 1432 | ++OpIdx; |
| 1433 | |
| 1434 | // Encode Dn / Sn. |
| 1435 | if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm) |
Evan Cheng | 3f4924e | 2008-11-12 08:14:21 +0000 | [diff] [blame] | 1436 | Binary |= encodeVFPRn(MI, OpIdx++); |
Evan Cheng | 3c4a4ff | 2008-11-12 07:18:38 +0000 | [diff] [blame] | 1437 | |
| 1438 | if (OpIdx == TID.getNumOperands() || |
| 1439 | TID.OpInfo[OpIdx].isPredicate() || |
| 1440 | TID.OpInfo[OpIdx].isOptionalDef()) { |
| 1441 | // FCMPEZD etc. has only one operand. |
| 1442 | emitWordLE(Binary); |
| 1443 | return; |
| 1444 | } |
| 1445 | |
| 1446 | // Encode Dm / Sm. |
| 1447 | Binary |= encodeVFPRm(MI, OpIdx); |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1448 | |
Evan Cheng | 3c4a4ff | 2008-11-12 07:18:38 +0000 | [diff] [blame] | 1449 | emitWordLE(Binary); |
| 1450 | } |
| 1451 | |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 1452 | void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) { |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1453 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1454 | unsigned Form = TID.TSFlags & ARMII::FormMask; |
| 1455 | |
| 1456 | // Part of binary is determined by TableGn. |
| 1457 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1458 | |
| 1459 | // Set the conditional execution predicate |
| 1460 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1461 | |
| 1462 | switch (Form) { |
| 1463 | default: break; |
| 1464 | case ARMII::VFPConv1Frm: |
| 1465 | case ARMII::VFPConv2Frm: |
| 1466 | case ARMII::VFPConv3Frm: |
| 1467 | // Encode Dd / Sd. |
| 1468 | Binary |= encodeVFPRd(MI, 0); |
| 1469 | break; |
| 1470 | case ARMII::VFPConv4Frm: |
| 1471 | // Encode Dn / Sn. |
| 1472 | Binary |= encodeVFPRn(MI, 0); |
| 1473 | break; |
| 1474 | case ARMII::VFPConv5Frm: |
| 1475 | // Encode Dm / Sm. |
| 1476 | Binary |= encodeVFPRm(MI, 0); |
| 1477 | break; |
| 1478 | } |
| 1479 | |
| 1480 | switch (Form) { |
| 1481 | default: break; |
| 1482 | case ARMII::VFPConv1Frm: |
| 1483 | // Encode Dm / Sm. |
| 1484 | Binary |= encodeVFPRm(MI, 1); |
Evan Cheng | 67fd91f | 2008-11-13 07:46:59 +0000 | [diff] [blame] | 1485 | break; |
Evan Cheng | 80a1198 | 2008-11-12 06:41:41 +0000 | [diff] [blame] | 1486 | case ARMII::VFPConv2Frm: |
| 1487 | case ARMII::VFPConv3Frm: |
| 1488 | // Encode Dn / Sn. |
| 1489 | Binary |= encodeVFPRn(MI, 1); |
| 1490 | break; |
| 1491 | case ARMII::VFPConv4Frm: |
| 1492 | case ARMII::VFPConv5Frm: |
| 1493 | // Encode Dd / Sd. |
| 1494 | Binary |= encodeVFPRd(MI, 1); |
| 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | if (Form == ARMII::VFPConv5Frm) |
| 1499 | // Encode Dn / Sn. |
| 1500 | Binary |= encodeVFPRn(MI, 2); |
| 1501 | else if (Form == ARMII::VFPConv3Frm) |
| 1502 | // Encode Dm / Sm. |
| 1503 | Binary |= encodeVFPRm(MI, 2); |
Evan Cheng | 78be83d | 2008-11-11 19:40:26 +0000 | [diff] [blame] | 1504 | |
| 1505 | emitWordLE(Binary); |
| 1506 | } |
| 1507 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1508 | void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) { |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1509 | // Part of binary is determined by TableGn. |
| 1510 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1511 | |
| 1512 | // Set the conditional execution predicate |
| 1513 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1514 | |
| 1515 | unsigned OpIdx = 0; |
| 1516 | |
| 1517 | // Encode Dd / Sd. |
Evan Cheng | 3c4a4ff | 2008-11-12 07:18:38 +0000 | [diff] [blame] | 1518 | Binary |= encodeVFPRd(MI, OpIdx++); |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1519 | |
| 1520 | // Encode address base. |
| 1521 | const MachineOperand &Base = MI.getOperand(OpIdx++); |
| 1522 | Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift; |
| 1523 | |
| 1524 | // If there is a non-zero immediate offset, encode it. |
| 1525 | if (Base.isReg()) { |
| 1526 | const MachineOperand &Offset = MI.getOperand(OpIdx); |
| 1527 | if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) { |
| 1528 | if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add) |
| 1529 | Binary |= 1 << ARMII::U_BitShift; |
Evan Cheng | 607f1b4 | 2008-11-12 08:21:12 +0000 | [diff] [blame] | 1530 | Binary |= ImmOffs; |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1531 | emitWordLE(Binary); |
| 1532 | return; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | // If immediate offset is omitted, default to +0. |
| 1537 | Binary |= 1 << ARMII::U_BitShift; |
| 1538 | |
| 1539 | emitWordLE(Binary); |
| 1540 | } |
| 1541 | |
Bob Wilson | 87949d4 | 2010-03-17 21:16:45 +0000 | [diff] [blame] | 1542 | void |
| 1543 | ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) { |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1544 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1545 | bool IsUpdating = (TID.TSFlags & ARMII::IndexModeMask) != 0; |
| 1546 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1547 | // Part of binary is determined by TableGn. |
| 1548 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1549 | |
| 1550 | // Set the conditional execution predicate |
| 1551 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1552 | |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1553 | // Skip operand 0 of an instruction with base register update. |
| 1554 | unsigned OpIdx = 0; |
| 1555 | if (IsUpdating) |
| 1556 | ++OpIdx; |
| 1557 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1558 | // Set base address operand |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1559 | Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1560 | |
| 1561 | // Set addressing mode by modifying bits U(23) and P(24) |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1562 | const MachineOperand &MO = MI.getOperand(OpIdx++); |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 1563 | Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm())); |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1564 | |
| 1565 | // Set bit W(21) |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 1566 | if (IsUpdating) |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1567 | Binary |= 0x1 << ARMII::W_BitShift; |
| 1568 | |
| 1569 | // First register is encoded in Dd. |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1570 | Binary |= encodeVFPRd(MI, OpIdx+2); |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1571 | |
Bob Wilson | d4bfd54 | 2010-08-27 23:18:17 +0000 | [diff] [blame] | 1572 | // Count the number of registers. |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1573 | unsigned NumRegs = 1; |
Bob Wilson | bffb5b3 | 2010-03-13 07:34:35 +0000 | [diff] [blame] | 1574 | for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) { |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1575 | const MachineOperand &MO = MI.getOperand(i); |
| 1576 | if (!MO.isReg() || MO.isImplicit()) |
| 1577 | break; |
| 1578 | ++NumRegs; |
| 1579 | } |
Shih-wei Liao | 5170b71 | 2010-05-26 00:02:28 +0000 | [diff] [blame] | 1580 | // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0) |
| 1581 | // Otherwise, it will be 0, in the case of 32-bit registers. |
| 1582 | if(Binary & 0x100) |
| 1583 | Binary |= NumRegs * 2; |
| 1584 | else |
| 1585 | Binary |= NumRegs; |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1586 | |
| 1587 | emitWordLE(Binary); |
| 1588 | } |
| 1589 | |
Chris Lattner | 33fabd7 | 2010-02-02 21:48:51 +0000 | [diff] [blame] | 1590 | void ARMCodeEmitter::emitMiscInstruction(const MachineInstr &MI) { |
Zonr Chang | f3c770a | 2010-05-25 10:23:52 +0000 | [diff] [blame] | 1591 | unsigned Opcode = MI.getDesc().Opcode; |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1592 | // Part of binary is determined by TableGn. |
| 1593 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1594 | |
| 1595 | // Set the conditional execution predicate |
| 1596 | Binary |= II->getPredicate(&MI) << ARMII::CondShift; |
| 1597 | |
Zonr Chang | f3c770a | 2010-05-25 10:23:52 +0000 | [diff] [blame] | 1598 | switch(Opcode) { |
| 1599 | default: |
| 1600 | llvm_unreachable("ARMCodeEmitter::emitMiscInstruction"); |
| 1601 | |
| 1602 | case ARM::FMSTAT: |
| 1603 | // No further encoding needed. |
| 1604 | break; |
| 1605 | |
| 1606 | case ARM::VMRS: |
| 1607 | case ARM::VMSR: { |
| 1608 | const MachineOperand &MO0 = MI.getOperand(0); |
| 1609 | // Encode Rt. |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1610 | Binary |= getARMRegisterNumbering(MO0.getReg()) << ARMII::RegRdShift; |
Zonr Chang | f3c770a | 2010-05-25 10:23:52 +0000 | [diff] [blame] | 1611 | break; |
| 1612 | } |
| 1613 | |
| 1614 | case ARM::FCONSTD: |
| 1615 | case ARM::FCONSTS: { |
| 1616 | // Encode Dd / Sd. |
| 1617 | Binary |= encodeVFPRd(MI, 0); |
| 1618 | |
| 1619 | // Encode imm., Table A7-18 VFP modified immediate constants |
| 1620 | const MachineOperand &MO1 = MI.getOperand(1); |
| 1621 | unsigned Imm = static_cast<unsigned>(MO1.getFPImm()->getValueAPF() |
| 1622 | .bitcastToAPInt().getHiBits(32).getLimitedValue()); |
| 1623 | unsigned ModifiedImm; |
| 1624 | |
| 1625 | if(Opcode == ARM::FCONSTS) |
| 1626 | ModifiedImm = (Imm & 0x80000000) >> 24 | // a |
| 1627 | (Imm & 0x03F80000) >> 19; // bcdefgh |
| 1628 | else // Opcode == ARM::FCONSTD |
| 1629 | ModifiedImm = (Imm & 0x80000000) >> 24 | // a |
| 1630 | (Imm & 0x007F0000) >> 16; // bcdefgh |
| 1631 | |
| 1632 | // Insts{19-16} = abcd, Insts{3-0} = efgh |
| 1633 | Binary |= ((ModifiedImm & 0xF0) >> 4) << 16; |
| 1634 | Binary |= (ModifiedImm & 0xF); |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | |
Evan Cheng | cd8e66a | 2008-11-11 21:48:44 +0000 | [diff] [blame] | 1639 | emitWordLE(Binary); |
| 1640 | } |
| 1641 | |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1642 | static unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) { |
| 1643 | unsigned RegD = MI.getOperand(OpIdx).getReg(); |
| 1644 | unsigned Binary = 0; |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1645 | RegD = getARMRegisterNumbering(RegD); |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1646 | Binary |= (RegD & 0xf) << ARMII::RegRdShift; |
| 1647 | Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift; |
| 1648 | return Binary; |
| 1649 | } |
| 1650 | |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 1651 | static unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) { |
| 1652 | unsigned RegN = MI.getOperand(OpIdx).getReg(); |
| 1653 | unsigned Binary = 0; |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1654 | RegN = getARMRegisterNumbering(RegN); |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 1655 | Binary |= (RegN & 0xf) << ARMII::RegRnShift; |
| 1656 | Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift; |
| 1657 | return Binary; |
| 1658 | } |
| 1659 | |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 1660 | static unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) { |
| 1661 | unsigned RegM = MI.getOperand(OpIdx).getReg(); |
| 1662 | unsigned Binary = 0; |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1663 | RegM = getARMRegisterNumbering(RegM); |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 1664 | Binary |= (RegM & 0xf); |
| 1665 | Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift; |
| 1666 | return Binary; |
| 1667 | } |
| 1668 | |
Bob Wilson | d896a97 | 2010-06-28 21:12:19 +0000 | [diff] [blame] | 1669 | /// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON |
| 1670 | /// data-processing instruction to the corresponding Thumb encoding. |
| 1671 | static unsigned convertNEONDataProcToThumb(unsigned Binary) { |
| 1672 | assert((Binary & 0xfe000000) == 0xf2000000 && |
| 1673 | "not an ARM NEON data-processing instruction"); |
| 1674 | unsigned UBit = (Binary >> 24) & 1; |
| 1675 | return 0xef000000 | (UBit << 28) | (Binary & 0xffffff); |
| 1676 | } |
| 1677 | |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 1678 | void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) { |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 1679 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1680 | |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 1681 | unsigned RegTOpIdx, RegNOpIdx, LnOpIdx; |
| 1682 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1683 | if ((TID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) { |
| 1684 | RegTOpIdx = 0; |
| 1685 | RegNOpIdx = 1; |
| 1686 | LnOpIdx = 2; |
| 1687 | } else { // ARMII::NSetLnFrm |
| 1688 | RegTOpIdx = 2; |
| 1689 | RegNOpIdx = 0; |
| 1690 | LnOpIdx = 3; |
| 1691 | } |
| 1692 | |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 1693 | // Set the conditional execution predicate |
Bob Wilson | 5cdede4 | 2010-06-29 00:26:13 +0000 | [diff] [blame] | 1694 | Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift; |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 1695 | |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 1696 | unsigned RegT = MI.getOperand(RegTOpIdx).getReg(); |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1697 | RegT = getARMRegisterNumbering(RegT); |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 1698 | Binary |= (RegT << ARMII::RegRdShift); |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 1699 | Binary |= encodeNEONRn(MI, RegNOpIdx); |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 1700 | |
| 1701 | unsigned LaneShift; |
| 1702 | if ((Binary & (1 << 22)) != 0) |
| 1703 | LaneShift = 0; // 8-bit elements |
| 1704 | else if ((Binary & (1 << 5)) != 0) |
| 1705 | LaneShift = 1; // 16-bit elements |
| 1706 | else |
| 1707 | LaneShift = 2; // 32-bit elements |
| 1708 | |
Bob Wilson | d5a563d | 2010-06-29 17:34:07 +0000 | [diff] [blame] | 1709 | unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift; |
Bob Wilson | 52e4a0a | 2010-06-26 04:07:15 +0000 | [diff] [blame] | 1710 | unsigned Opc1 = Lane >> 2; |
| 1711 | unsigned Opc2 = Lane & 3; |
| 1712 | assert((Opc1 & 3) == 0 && "out-of-range lane number operand"); |
| 1713 | Binary |= (Opc1 << 21); |
| 1714 | Binary |= (Opc2 << 5); |
| 1715 | |
| 1716 | emitWordLE(Binary); |
| 1717 | } |
| 1718 | |
Bob Wilson | 21773e7 | 2010-06-29 20:13:29 +0000 | [diff] [blame] | 1719 | void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) { |
| 1720 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1721 | |
| 1722 | // Set the conditional execution predicate |
| 1723 | Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift; |
| 1724 | |
| 1725 | unsigned RegT = MI.getOperand(1).getReg(); |
Jim Grosbach | a4c3c8f | 2010-09-15 20:26:25 +0000 | [diff] [blame] | 1726 | RegT = getARMRegisterNumbering(RegT); |
Bob Wilson | 21773e7 | 2010-06-29 20:13:29 +0000 | [diff] [blame] | 1727 | Binary |= (RegT << ARMII::RegRdShift); |
| 1728 | Binary |= encodeNEONRn(MI, 0); |
| 1729 | emitWordLE(Binary); |
| 1730 | } |
| 1731 | |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 1732 | void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) { |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1733 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1734 | // Destination register is encoded in Dd. |
| 1735 | Binary |= encodeNEONRd(MI, 0); |
| 1736 | // Immediate fields: Op, Cmode, I, Imm3, Imm4 |
| 1737 | unsigned Imm = MI.getOperand(1).getImm(); |
| 1738 | unsigned Op = (Imm >> 12) & 1; |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1739 | unsigned Cmode = (Imm >> 8) & 0xf; |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1740 | unsigned I = (Imm >> 7) & 1; |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1741 | unsigned Imm3 = (Imm >> 4) & 0x7; |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1742 | unsigned Imm4 = Imm & 0xf; |
Bob Wilson | 08baddb | 2010-06-28 21:16:30 +0000 | [diff] [blame] | 1743 | Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4; |
Bob Wilson | 62d24a4 | 2010-06-28 22:23:17 +0000 | [diff] [blame] | 1744 | if (IsThumb) |
Bob Wilson | d896a97 | 2010-06-28 21:12:19 +0000 | [diff] [blame] | 1745 | Binary = convertNEONDataProcToThumb(Binary); |
Bob Wilson | 1a913ed | 2010-06-11 21:34:50 +0000 | [diff] [blame] | 1746 | emitWordLE(Binary); |
| 1747 | } |
| 1748 | |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 1749 | void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) { |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 1750 | const TargetInstrDesc &TID = MI.getDesc(); |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 1751 | unsigned Binary = getBinaryCodeForInstr(MI); |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 1752 | // Destination register is encoded in Dd; source register in Dm. |
| 1753 | unsigned OpIdx = 0; |
| 1754 | Binary |= encodeNEONRd(MI, OpIdx++); |
| 1755 | if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
| 1756 | ++OpIdx; |
| 1757 | Binary |= encodeNEONRm(MI, OpIdx); |
Bob Wilson | 62d24a4 | 2010-06-28 22:23:17 +0000 | [diff] [blame] | 1758 | if (IsThumb) |
Bob Wilson | d896a97 | 2010-06-28 21:12:19 +0000 | [diff] [blame] | 1759 | Binary = convertNEONDataProcToThumb(Binary); |
Bob Wilson | 583a2a0 | 2010-06-25 21:17:19 +0000 | [diff] [blame] | 1760 | // FIXME: This does not handle VDUPfdf or VDUPfqf. |
| 1761 | emitWordLE(Binary); |
| 1762 | } |
| 1763 | |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 1764 | void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) { |
| 1765 | const TargetInstrDesc &TID = MI.getDesc(); |
| 1766 | unsigned Binary = getBinaryCodeForInstr(MI); |
| 1767 | // Destination register is encoded in Dd; source registers in Dn and Dm. |
| 1768 | unsigned OpIdx = 0; |
| 1769 | Binary |= encodeNEONRd(MI, OpIdx++); |
| 1770 | if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
| 1771 | ++OpIdx; |
| 1772 | Binary |= encodeNEONRn(MI, OpIdx++); |
| 1773 | if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) |
| 1774 | ++OpIdx; |
| 1775 | Binary |= encodeNEONRm(MI, OpIdx); |
Bob Wilson | 62d24a4 | 2010-06-28 22:23:17 +0000 | [diff] [blame] | 1776 | if (IsThumb) |
Bob Wilson | d896a97 | 2010-06-28 21:12:19 +0000 | [diff] [blame] | 1777 | Binary = convertNEONDataProcToThumb(Binary); |
Bob Wilson | 5e7b607 | 2010-06-25 22:40:46 +0000 | [diff] [blame] | 1778 | // FIXME: This does not handle VMOVDneon or VMOVQ. |
| 1779 | emitWordLE(Binary); |
| 1780 | } |
| 1781 | |
Evan Cheng | 7602e11 | 2008-09-02 06:52:38 +0000 | [diff] [blame] | 1782 | #include "ARMGenCodeEmitter.inc" |