LLVM API Documentation
00001 //===-- ARM/ARMCodeEmitter.cpp - Convert ARM code to machine code ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the pass that transforms the ARM machine instructions into 00011 // relocatable machine code. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "jit" 00016 #include "ARM.h" 00017 #include "ARMBaseInstrInfo.h" 00018 #include "ARMConstantPoolValue.h" 00019 #include "ARMRelocations.h" 00020 #include "ARMSubtarget.h" 00021 #include "ARMTargetMachine.h" 00022 #include "MCTargetDesc/ARMAddressingModes.h" 00023 #include "llvm/ADT/Statistic.h" 00024 #include "llvm/CodeGen/JITCodeEmitter.h" 00025 #include "llvm/CodeGen/MachineConstantPool.h" 00026 #include "llvm/CodeGen/MachineFunctionPass.h" 00027 #include "llvm/CodeGen/MachineInstr.h" 00028 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00029 #include "llvm/CodeGen/MachineModuleInfo.h" 00030 #include "llvm/CodeGen/Passes.h" 00031 #include "llvm/IR/Constants.h" 00032 #include "llvm/IR/DerivedTypes.h" 00033 #include "llvm/IR/Function.h" 00034 #include "llvm/PassManager.h" 00035 #include "llvm/Support/Debug.h" 00036 #include "llvm/Support/ErrorHandling.h" 00037 #include "llvm/Support/raw_ostream.h" 00038 #ifndef NDEBUG 00039 #include <iomanip> 00040 #endif 00041 using namespace llvm; 00042 00043 STATISTIC(NumEmitted, "Number of machine instructions emitted"); 00044 00045 namespace { 00046 00047 class ARMCodeEmitter : public MachineFunctionPass { 00048 ARMJITInfo *JTI; 00049 const ARMBaseInstrInfo *II; 00050 const DataLayout *TD; 00051 const ARMSubtarget *Subtarget; 00052 TargetMachine &TM; 00053 JITCodeEmitter &MCE; 00054 MachineModuleInfo *MMI; 00055 const std::vector<MachineConstantPoolEntry> *MCPEs; 00056 const std::vector<MachineJumpTableEntry> *MJTEs; 00057 bool IsPIC; 00058 bool IsThumb; 00059 00060 void getAnalysisUsage(AnalysisUsage &AU) const { 00061 AU.addRequired<MachineModuleInfo>(); 00062 MachineFunctionPass::getAnalysisUsage(AU); 00063 } 00064 00065 static char ID; 00066 public: 00067 ARMCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) 00068 : MachineFunctionPass(ID), JTI(0), 00069 II((const ARMBaseInstrInfo *)tm.getInstrInfo()), 00070 TD(tm.getDataLayout()), TM(tm), 00071 MCE(mce), MCPEs(0), MJTEs(0), 00072 IsPIC(TM.getRelocationModel() == Reloc::PIC_), IsThumb(false) {} 00073 00074 /// getBinaryCodeForInstr - This function, generated by the 00075 /// CodeEmitterGenerator using TableGen, produces the binary encoding for 00076 /// machine instructions. 00077 uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const; 00078 00079 bool runOnMachineFunction(MachineFunction &MF); 00080 00081 virtual const char *getPassName() const { 00082 return "ARM Machine Code Emitter"; 00083 } 00084 00085 void emitInstruction(const MachineInstr &MI); 00086 00087 private: 00088 00089 void emitWordLE(unsigned Binary); 00090 void emitDWordLE(uint64_t Binary); 00091 void emitConstPoolInstruction(const MachineInstr &MI); 00092 void emitMOVi32immInstruction(const MachineInstr &MI); 00093 void emitMOVi2piecesInstruction(const MachineInstr &MI); 00094 void emitLEApcrelJTInstruction(const MachineInstr &MI); 00095 void emitPseudoMoveInstruction(const MachineInstr &MI); 00096 void addPCLabel(unsigned LabelID); 00097 void emitPseudoInstruction(const MachineInstr &MI); 00098 unsigned getMachineSoRegOpValue(const MachineInstr &MI, 00099 const MCInstrDesc &MCID, 00100 const MachineOperand &MO, 00101 unsigned OpIdx); 00102 00103 unsigned getMachineSoImmOpValue(unsigned SoImm); 00104 unsigned getAddrModeSBit(const MachineInstr &MI, 00105 const MCInstrDesc &MCID) const; 00106 00107 void emitDataProcessingInstruction(const MachineInstr &MI, 00108 unsigned ImplicitRd = 0, 00109 unsigned ImplicitRn = 0); 00110 00111 void emitLoadStoreInstruction(const MachineInstr &MI, 00112 unsigned ImplicitRd = 0, 00113 unsigned ImplicitRn = 0); 00114 00115 void emitMiscLoadStoreInstruction(const MachineInstr &MI, 00116 unsigned ImplicitRn = 0); 00117 00118 void emitLoadStoreMultipleInstruction(const MachineInstr &MI); 00119 00120 void emitMulFrmInstruction(const MachineInstr &MI); 00121 00122 void emitExtendInstruction(const MachineInstr &MI); 00123 00124 void emitMiscArithInstruction(const MachineInstr &MI); 00125 00126 void emitSaturateInstruction(const MachineInstr &MI); 00127 00128 void emitBranchInstruction(const MachineInstr &MI); 00129 00130 void emitInlineJumpTable(unsigned JTIndex); 00131 00132 void emitMiscBranchInstruction(const MachineInstr &MI); 00133 00134 void emitVFPArithInstruction(const MachineInstr &MI); 00135 00136 void emitVFPConversionInstruction(const MachineInstr &MI); 00137 00138 void emitVFPLoadStoreInstruction(const MachineInstr &MI); 00139 00140 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI); 00141 00142 void emitNEONLaneInstruction(const MachineInstr &MI); 00143 void emitNEONDupInstruction(const MachineInstr &MI); 00144 void emitNEON1RegModImmInstruction(const MachineInstr &MI); 00145 void emitNEON2RegInstruction(const MachineInstr &MI); 00146 void emitNEON3RegInstruction(const MachineInstr &MI); 00147 00148 /// getMachineOpValue - Return binary encoding of operand. If the machine 00149 /// operand requires relocation, record the relocation and return zero. 00150 unsigned getMachineOpValue(const MachineInstr &MI, 00151 const MachineOperand &MO) const; 00152 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) const { 00153 return getMachineOpValue(MI, MI.getOperand(OpIdx)); 00154 } 00155 00156 // FIXME: The legacy JIT ARMCodeEmitter doesn't rely on the the 00157 // TableGen'erated getBinaryCodeForInstr() function to encode any 00158 // operand values, instead querying getMachineOpValue() directly for 00159 // each operand it needs to encode. Thus, any of the new encoder 00160 // helper functions can simply return 0 as the values the return 00161 // are already handled elsewhere. They are placeholders to allow this 00162 // encoder to continue to function until the MC encoder is sufficiently 00163 // far along that this one can be eliminated entirely. 00164 unsigned NEONThumb2DataIPostEncoder(const MachineInstr &MI, unsigned Val) 00165 const { return 0; } 00166 unsigned NEONThumb2LoadStorePostEncoder(const MachineInstr &MI,unsigned Val) 00167 const { return 0; } 00168 unsigned NEONThumb2DupPostEncoder(const MachineInstr &MI,unsigned Val) 00169 const { return 0; } 00170 unsigned VFPThumb2PostEncoder(const MachineInstr&MI, unsigned Val) 00171 const { return 0; } 00172 unsigned getAdrLabelOpValue(const MachineInstr &MI, unsigned Op) 00173 const { return 0; } 00174 unsigned getThumbAdrLabelOpValue(const MachineInstr &MI, unsigned Op) 00175 const { return 0; } 00176 unsigned getThumbBLTargetOpValue(const MachineInstr &MI, unsigned Op) 00177 const { return 0; } 00178 unsigned getThumbBLXTargetOpValue(const MachineInstr &MI, unsigned Op) 00179 const { return 0; } 00180 unsigned getThumbBRTargetOpValue(const MachineInstr &MI, unsigned Op) 00181 const { return 0; } 00182 unsigned getThumbBCCTargetOpValue(const MachineInstr &MI, unsigned Op) 00183 const { return 0; } 00184 unsigned getThumbCBTargetOpValue(const MachineInstr &MI, unsigned Op) 00185 const { return 0; } 00186 unsigned getBranchTargetOpValue(const MachineInstr &MI, unsigned Op) 00187 const { return 0; } 00188 unsigned getUnconditionalBranchTargetOpValue(const MachineInstr &MI, 00189 unsigned Op) const { return 0; } 00190 unsigned getARMBranchTargetOpValue(const MachineInstr &MI, unsigned Op) 00191 const { return 0; } 00192 unsigned getARMBLTargetOpValue(const MachineInstr &MI, unsigned Op) 00193 const { return 0; } 00194 unsigned getARMBLXTargetOpValue(const MachineInstr &MI, unsigned Op) 00195 const { return 0; } 00196 unsigned getCCOutOpValue(const MachineInstr &MI, unsigned Op) 00197 const { return 0; } 00198 unsigned getSOImmOpValue(const MachineInstr &MI, unsigned Op) 00199 const { return 0; } 00200 unsigned getT2SOImmOpValue(const MachineInstr &MI, unsigned Op) 00201 const { return 0; } 00202 unsigned getSORegRegOpValue(const MachineInstr &MI, unsigned Op) 00203 const { return 0; } 00204 unsigned getSORegImmOpValue(const MachineInstr &MI, unsigned Op) 00205 const { return 0; } 00206 unsigned getThumbAddrModeRegRegOpValue(const MachineInstr &MI, unsigned Op) 00207 const { return 0; } 00208 unsigned getT2AddrModeImm12OpValue(const MachineInstr &MI, unsigned Op) 00209 const { return 0; } 00210 unsigned getT2AddrModeImm8OpValue(const MachineInstr &MI, unsigned Op) 00211 const { return 0; } 00212 unsigned getT2Imm8s4OpValue(const MachineInstr &MI, unsigned Op) 00213 const { return 0; } 00214 unsigned getT2AddrModeImm8s4OpValue(const MachineInstr &MI, unsigned Op) 00215 const { return 0; } 00216 unsigned getT2AddrModeImm0_1020s4OpValue(const MachineInstr &MI,unsigned Op) 00217 const { return 0; } 00218 unsigned getT2AddrModeImm8OffsetOpValue(const MachineInstr &MI, unsigned Op) 00219 const { return 0; } 00220 unsigned getT2AddrModeImm12OffsetOpValue(const MachineInstr &MI,unsigned Op) 00221 const { return 0; } 00222 unsigned getT2AddrModeSORegOpValue(const MachineInstr &MI, unsigned Op) 00223 const { return 0; } 00224 unsigned getT2SORegOpValue(const MachineInstr &MI, unsigned Op) 00225 const { return 0; } 00226 unsigned getT2AdrLabelOpValue(const MachineInstr &MI, unsigned Op) 00227 const { return 0; } 00228 unsigned getAddrMode6AddressOpValue(const MachineInstr &MI, unsigned Op) 00229 const { return 0; } 00230 unsigned getAddrMode6OneLane32AddressOpValue(const MachineInstr &MI, 00231 unsigned Op) 00232 const { return 0; } 00233 unsigned getAddrMode6DupAddressOpValue(const MachineInstr &MI, unsigned Op) 00234 const { return 0; } 00235 unsigned getAddrMode6OffsetOpValue(const MachineInstr &MI, unsigned Op) 00236 const { return 0; } 00237 unsigned getBitfieldInvertedMaskOpValue(const MachineInstr &MI, 00238 unsigned Op) const { return 0; } 00239 unsigned getSsatBitPosValue(const MachineInstr &MI, 00240 unsigned Op) const { return 0; } 00241 uint32_t getLdStmModeOpValue(const MachineInstr &MI, unsigned OpIdx) 00242 const {return 0; } 00243 uint32_t getLdStSORegOpValue(const MachineInstr &MI, unsigned OpIdx) 00244 const { return 0; } 00245 00246 unsigned getAddrModeImm12OpValue(const MachineInstr &MI, unsigned Op) 00247 const { 00248 // {17-13} = reg 00249 // {12} = (U)nsigned (add == '1', sub == '0') 00250 // {11-0} = imm12 00251 const MachineOperand &MO = MI.getOperand(Op); 00252 const MachineOperand &MO1 = MI.getOperand(Op + 1); 00253 if (!MO.isReg()) { 00254 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry); 00255 return 0; 00256 } 00257 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg()); 00258 int32_t Imm12 = MO1.getImm(); 00259 uint32_t Binary; 00260 Binary = Imm12 & 0xfff; 00261 if (Imm12 >= 0) 00262 Binary |= (1 << 12); 00263 Binary |= (Reg << 13); 00264 return Binary; 00265 } 00266 00267 unsigned getHiLo16ImmOpValue(const MachineInstr &MI, unsigned Op) const { 00268 return 0; 00269 } 00270 00271 uint32_t getAddrMode2OpValue(const MachineInstr &MI, unsigned OpIdx) 00272 const { return 0;} 00273 uint32_t getAddrMode2OffsetOpValue(const MachineInstr &MI, unsigned OpIdx) 00274 const { return 0;} 00275 uint32_t getPostIdxRegOpValue(const MachineInstr &MI, unsigned OpIdx) 00276 const { return 0;} 00277 uint32_t getAddrMode3OffsetOpValue(const MachineInstr &MI, unsigned OpIdx) 00278 const { return 0;} 00279 uint32_t getAddrMode3OpValue(const MachineInstr &MI, unsigned Op) 00280 const { return 0; } 00281 uint32_t getAddrModeThumbSPOpValue(const MachineInstr &MI, unsigned Op) 00282 const { return 0; } 00283 uint32_t getAddrModeSOpValue(const MachineInstr &MI, unsigned Op) 00284 const { return 0; } 00285 uint32_t getAddrModeISOpValue(const MachineInstr &MI, unsigned Op) 00286 const { return 0; } 00287 uint32_t getAddrModePCOpValue(const MachineInstr &MI, unsigned Op) 00288 const { return 0; } 00289 uint32_t getAddrMode5OpValue(const MachineInstr &MI, unsigned Op) const { 00290 // {17-13} = reg 00291 // {12} = (U)nsigned (add == '1', sub == '0') 00292 // {11-0} = imm12 00293 const MachineOperand &MO = MI.getOperand(Op); 00294 const MachineOperand &MO1 = MI.getOperand(Op + 1); 00295 if (!MO.isReg()) { 00296 emitConstPoolAddress(MO.getIndex(), ARM::reloc_arm_cp_entry); 00297 return 0; 00298 } 00299 unsigned Reg = II->getRegisterInfo().getEncodingValue(MO.getReg()); 00300 int32_t Imm12 = MO1.getImm(); 00301 00302 // Special value for #-0 00303 if (Imm12 == INT32_MIN) 00304 Imm12 = 0; 00305 00306 // Immediate is always encoded as positive. The 'U' bit controls add vs 00307 // sub. 00308 bool isAdd = true; 00309 if (Imm12 < 0) { 00310 Imm12 = -Imm12; 00311 isAdd = false; 00312 } 00313 00314 uint32_t Binary = Imm12 & 0xfff; 00315 if (isAdd) 00316 Binary |= (1 << 12); 00317 Binary |= (Reg << 13); 00318 return Binary; 00319 } 00320 unsigned getNEONVcvtImm32OpValue(const MachineInstr &MI, unsigned Op) 00321 const { return 0; } 00322 00323 unsigned getRegisterListOpValue(const MachineInstr &MI, unsigned Op) 00324 const { return 0; } 00325 00326 unsigned getShiftRight8Imm(const MachineInstr &MI, unsigned Op) 00327 const { return 0; } 00328 unsigned getShiftRight16Imm(const MachineInstr &MI, unsigned Op) 00329 const { return 0; } 00330 unsigned getShiftRight32Imm(const MachineInstr &MI, unsigned Op) 00331 const { return 0; } 00332 unsigned getShiftRight64Imm(const MachineInstr &MI, unsigned Op) 00333 const { return 0; } 00334 00335 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the 00336 /// machine operand requires relocation, record the relocation and return 00337 /// zero. 00338 unsigned getMovi32Value(const MachineInstr &MI,const MachineOperand &MO, 00339 unsigned Reloc); 00340 00341 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. 00342 /// 00343 unsigned getShiftOp(unsigned Imm) const ; 00344 00345 /// Routines that handle operands which add machine relocations which are 00346 /// fixed up by the relocation stage. 00347 void emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, 00348 bool MayNeedFarStub, bool Indirect, 00349 intptr_t ACPV = 0) const; 00350 void emitExternalSymbolAddress(const char *ES, unsigned Reloc) const; 00351 void emitConstPoolAddress(unsigned CPI, unsigned Reloc) const; 00352 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const; 00353 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc, 00354 intptr_t JTBase = 0) const; 00355 unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) const; 00356 unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) const; 00357 unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) const; 00358 unsigned encodeNEONRd(const MachineInstr &MI, unsigned OpIdx) const; 00359 unsigned encodeNEONRn(const MachineInstr &MI, unsigned OpIdx) const; 00360 unsigned encodeNEONRm(const MachineInstr &MI, unsigned OpIdx) const; 00361 }; 00362 } 00363 00364 char ARMCodeEmitter::ID = 0; 00365 00366 /// createARMJITCodeEmitterPass - Return a pass that emits the collected ARM 00367 /// code to the specified MCE object. 00368 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM, 00369 JITCodeEmitter &JCE) { 00370 return new ARMCodeEmitter(TM, JCE); 00371 } 00372 00373 bool ARMCodeEmitter::runOnMachineFunction(MachineFunction &MF) { 00374 TargetMachine &Target = const_cast<TargetMachine&>(MF.getTarget()); 00375 00376 assert((Target.getRelocationModel() != Reloc::Default || 00377 Target.getRelocationModel() != Reloc::Static) && 00378 "JIT relocation model must be set to static or default!"); 00379 00380 JTI = static_cast<ARMJITInfo*>(Target.getJITInfo()); 00381 II = static_cast<const ARMBaseInstrInfo*>(Target.getInstrInfo()); 00382 TD = Target.getDataLayout(); 00383 00384 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 00385 MCPEs = &MF.getConstantPool()->getConstants(); 00386 MJTEs = 0; 00387 if (MF.getJumpTableInfo()) MJTEs = &MF.getJumpTableInfo()->getJumpTables(); 00388 IsPIC = TM.getRelocationModel() == Reloc::PIC_; 00389 IsThumb = MF.getInfo<ARMFunctionInfo>()->isThumbFunction(); 00390 JTI->Initialize(MF, IsPIC); 00391 MMI = &getAnalysis<MachineModuleInfo>(); 00392 MCE.setModuleInfo(MMI); 00393 00394 do { 00395 DEBUG(errs() << "JITTing function '" 00396 << MF.getName() << "'\n"); 00397 MCE.startFunction(MF); 00398 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 00399 MBB != E; ++MBB) { 00400 MCE.StartMachineBasicBlock(MBB); 00401 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); 00402 I != E; ++I) 00403 emitInstruction(*I); 00404 } 00405 } while (MCE.finishFunction(MF)); 00406 00407 return false; 00408 } 00409 00410 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. 00411 /// 00412 unsigned ARMCodeEmitter::getShiftOp(unsigned Imm) const { 00413 switch (ARM_AM::getAM2ShiftOpc(Imm)) { 00414 default: llvm_unreachable("Unknown shift opc!"); 00415 case ARM_AM::asr: return 2; 00416 case ARM_AM::lsl: return 0; 00417 case ARM_AM::lsr: return 1; 00418 case ARM_AM::ror: 00419 case ARM_AM::rrx: return 3; 00420 } 00421 } 00422 00423 /// getMovi32Value - Return binary encoding of operand for movw/movt. If the 00424 /// machine operand requires relocation, record the relocation and return zero. 00425 unsigned ARMCodeEmitter::getMovi32Value(const MachineInstr &MI, 00426 const MachineOperand &MO, 00427 unsigned Reloc) { 00428 assert(((Reloc == ARM::reloc_arm_movt) || (Reloc == ARM::reloc_arm_movw)) 00429 && "Relocation to this function should be for movt or movw"); 00430 00431 if (MO.isImm()) 00432 return static_cast<unsigned>(MO.getImm()); 00433 else if (MO.isGlobal()) 00434 emitGlobalAddress(MO.getGlobal(), Reloc, true, false); 00435 else if (MO.isSymbol()) 00436 emitExternalSymbolAddress(MO.getSymbolName(), Reloc); 00437 else if (MO.isMBB()) 00438 emitMachineBasicBlock(MO.getMBB(), Reloc); 00439 else { 00440 #ifndef NDEBUG 00441 errs() << MO; 00442 #endif 00443 llvm_unreachable("Unsupported operand type for movw/movt"); 00444 } 00445 return 0; 00446 } 00447 00448 /// getMachineOpValue - Return binary encoding of operand. If the machine 00449 /// operand requires relocation, record the relocation and return zero. 00450 unsigned ARMCodeEmitter::getMachineOpValue(const MachineInstr &MI, 00451 const MachineOperand &MO) const { 00452 if (MO.isReg()) 00453 return II->getRegisterInfo().getEncodingValue(MO.getReg()); 00454 else if (MO.isImm()) 00455 return static_cast<unsigned>(MO.getImm()); 00456 else if (MO.isGlobal()) 00457 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false); 00458 else if (MO.isSymbol()) 00459 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch); 00460 else if (MO.isCPI()) { 00461 const MCInstrDesc &MCID = MI.getDesc(); 00462 // For VFP load, the immediate offset is multiplied by 4. 00463 unsigned Reloc = ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm) 00464 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry; 00465 emitConstPoolAddress(MO.getIndex(), Reloc); 00466 } else if (MO.isJTI()) 00467 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative); 00468 else if (MO.isMBB()) 00469 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch); 00470 else 00471 llvm_unreachable("Unable to encode MachineOperand!"); 00472 return 0; 00473 } 00474 00475 /// emitGlobalAddress - Emit the specified address to the code stream. 00476 /// 00477 void ARMCodeEmitter::emitGlobalAddress(const GlobalValue *GV, unsigned Reloc, 00478 bool MayNeedFarStub, bool Indirect, 00479 intptr_t ACPV) const { 00480 MachineRelocation MR = Indirect 00481 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc, 00482 const_cast<GlobalValue *>(GV), 00483 ACPV, MayNeedFarStub) 00484 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 00485 const_cast<GlobalValue *>(GV), ACPV, 00486 MayNeedFarStub); 00487 MCE.addRelocation(MR); 00488 } 00489 00490 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to 00491 /// be emitted to the current location in the function, and allow it to be PC 00492 /// relative. 00493 void ARMCodeEmitter:: 00494 emitExternalSymbolAddress(const char *ES, unsigned Reloc) const { 00495 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 00496 Reloc, ES)); 00497 } 00498 00499 /// emitConstPoolAddress - Arrange for the address of an constant pool 00500 /// to be emitted to the current location in the function, and allow it to be PC 00501 /// relative. 00502 void ARMCodeEmitter::emitConstPoolAddress(unsigned CPI, unsigned Reloc) const { 00503 // Tell JIT emitter we'll resolve the address. 00504 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 00505 Reloc, CPI, 0, true)); 00506 } 00507 00508 /// emitJumpTableAddress - Arrange for the address of a jump table to 00509 /// be emitted to the current location in the function, and allow it to be PC 00510 /// relative. 00511 void ARMCodeEmitter:: 00512 emitJumpTableAddress(unsigned JTIndex, unsigned Reloc) const { 00513 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 00514 Reloc, JTIndex, 0, true)); 00515 } 00516 00517 /// emitMachineBasicBlock - Emit the specified address basic block. 00518 void ARMCodeEmitter::emitMachineBasicBlock(MachineBasicBlock *BB, 00519 unsigned Reloc, 00520 intptr_t JTBase) const { 00521 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 00522 Reloc, BB, JTBase)); 00523 } 00524 00525 void ARMCodeEmitter::emitWordLE(unsigned Binary) { 00526 DEBUG(errs() << " 0x"; 00527 errs().write_hex(Binary) << "\n"); 00528 MCE.emitWordLE(Binary); 00529 } 00530 00531 void ARMCodeEmitter::emitDWordLE(uint64_t Binary) { 00532 DEBUG(errs() << " 0x"; 00533 errs().write_hex(Binary) << "\n"); 00534 MCE.emitDWordLE(Binary); 00535 } 00536 00537 void ARMCodeEmitter::emitInstruction(const MachineInstr &MI) { 00538 DEBUG(errs() << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI); 00539 00540 MCE.processDebugLoc(MI.getDebugLoc(), true); 00541 00542 ++NumEmitted; // Keep track of the # of mi's emitted 00543 switch (MI.getDesc().TSFlags & ARMII::FormMask) { 00544 default: { 00545 llvm_unreachable("Unhandled instruction encoding format!"); 00546 } 00547 case ARMII::MiscFrm: 00548 if (MI.getOpcode() == ARM::LEApcrelJT) { 00549 // Materialize jumptable address. 00550 emitLEApcrelJTInstruction(MI); 00551 break; 00552 } 00553 llvm_unreachable("Unhandled instruction encoding!"); 00554 case ARMII::Pseudo: 00555 emitPseudoInstruction(MI); 00556 break; 00557 case ARMII::DPFrm: 00558 case ARMII::DPSoRegFrm: 00559 emitDataProcessingInstruction(MI); 00560 break; 00561 case ARMII::LdFrm: 00562 case ARMII::StFrm: 00563 emitLoadStoreInstruction(MI); 00564 break; 00565 case ARMII::LdMiscFrm: 00566 case ARMII::StMiscFrm: 00567 emitMiscLoadStoreInstruction(MI); 00568 break; 00569 case ARMII::LdStMulFrm: 00570 emitLoadStoreMultipleInstruction(MI); 00571 break; 00572 case ARMII::MulFrm: 00573 emitMulFrmInstruction(MI); 00574 break; 00575 case ARMII::ExtFrm: 00576 emitExtendInstruction(MI); 00577 break; 00578 case ARMII::ArithMiscFrm: 00579 emitMiscArithInstruction(MI); 00580 break; 00581 case ARMII::SatFrm: 00582 emitSaturateInstruction(MI); 00583 break; 00584 case ARMII::BrFrm: 00585 emitBranchInstruction(MI); 00586 break; 00587 case ARMII::BrMiscFrm: 00588 emitMiscBranchInstruction(MI); 00589 break; 00590 // VFP instructions. 00591 case ARMII::VFPUnaryFrm: 00592 case ARMII::VFPBinaryFrm: 00593 emitVFPArithInstruction(MI); 00594 break; 00595 case ARMII::VFPConv1Frm: 00596 case ARMII::VFPConv2Frm: 00597 case ARMII::VFPConv3Frm: 00598 case ARMII::VFPConv4Frm: 00599 case ARMII::VFPConv5Frm: 00600 emitVFPConversionInstruction(MI); 00601 break; 00602 case ARMII::VFPLdStFrm: 00603 emitVFPLoadStoreInstruction(MI); 00604 break; 00605 case ARMII::VFPLdStMulFrm: 00606 emitVFPLoadStoreMultipleInstruction(MI); 00607 break; 00608 00609 // NEON instructions. 00610 case ARMII::NGetLnFrm: 00611 case ARMII::NSetLnFrm: 00612 emitNEONLaneInstruction(MI); 00613 break; 00614 case ARMII::NDupFrm: 00615 emitNEONDupInstruction(MI); 00616 break; 00617 case ARMII::N1RegModImmFrm: 00618 emitNEON1RegModImmInstruction(MI); 00619 break; 00620 case ARMII::N2RegFrm: 00621 emitNEON2RegInstruction(MI); 00622 break; 00623 case ARMII::N3RegFrm: 00624 emitNEON3RegInstruction(MI); 00625 break; 00626 } 00627 MCE.processDebugLoc(MI.getDebugLoc(), false); 00628 } 00629 00630 void ARMCodeEmitter::emitConstPoolInstruction(const MachineInstr &MI) { 00631 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index. 00632 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index. 00633 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex]; 00634 00635 // Remember the CONSTPOOL_ENTRY address for later relocation. 00636 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue()); 00637 00638 // Emit constpool island entry. In most cases, the actual values will be 00639 // resolved and relocated after code emission. 00640 if (MCPE.isMachineConstantPoolEntry()) { 00641 ARMConstantPoolValue *ACPV = 00642 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal); 00643 00644 DEBUG(errs() << " ** ARM constant pool #" << CPI << " @ " 00645 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n'); 00646 00647 assert(ACPV->isGlobalValue() && "unsupported constant pool value"); 00648 const GlobalValue *GV = cast<ARMConstantPoolConstant>(ACPV)->getGV(); 00649 if (GV) { 00650 Reloc::Model RelocM = TM.getRelocationModel(); 00651 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry, 00652 isa<Function>(GV), 00653 Subtarget->GVIsIndirectSymbol(GV, RelocM), 00654 (intptr_t)ACPV); 00655 } else { 00656 const char *Sym = cast<ARMConstantPoolSymbol>(ACPV)->getSymbol(); 00657 emitExternalSymbolAddress(Sym, ARM::reloc_arm_absolute); 00658 } 00659 emitWordLE(0); 00660 } else { 00661 const Constant *CV = MCPE.Val.ConstVal; 00662 00663 DEBUG({ 00664 errs() << " ** Constant pool #" << CPI << " @ " 00665 << (void*)MCE.getCurrentPCValue() << " "; 00666 if (const Function *F = dyn_cast<Function>(CV)) 00667 errs() << F->getName(); 00668 else 00669 errs() << *CV; 00670 errs() << '\n'; 00671 }); 00672 00673 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 00674 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false); 00675 emitWordLE(0); 00676 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 00677 uint32_t Val = uint32_t(*CI->getValue().getRawData()); 00678 emitWordLE(Val); 00679 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 00680 if (CFP->getType()->isFloatTy()) 00681 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 00682 else if (CFP->getType()->isDoubleTy()) 00683 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 00684 else { 00685 llvm_unreachable("Unable to handle this constantpool entry!"); 00686 } 00687 } else { 00688 llvm_unreachable("Unable to handle this constantpool entry!"); 00689 } 00690 } 00691 } 00692 00693 void ARMCodeEmitter::emitMOVi32immInstruction(const MachineInstr &MI) { 00694 const MachineOperand &MO0 = MI.getOperand(0); 00695 const MachineOperand &MO1 = MI.getOperand(1); 00696 00697 // Emit the 'movw' instruction. 00698 unsigned Binary = 0x30 << 20; // mov: Insts{27-20} = 0b00110000 00699 00700 unsigned Lo16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movw) & 0xFFFF; 00701 00702 // Set the conditional execution predicate. 00703 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00704 00705 // Encode Rd. 00706 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; 00707 00708 // Encode imm16 as imm4:imm12 00709 Binary |= Lo16 & 0xFFF; // Insts{11-0} = imm12 00710 Binary |= ((Lo16 >> 12) & 0xF) << 16; // Insts{19-16} = imm4 00711 emitWordLE(Binary); 00712 00713 unsigned Hi16 = getMovi32Value(MI, MO1, ARM::reloc_arm_movt) >> 16; 00714 // Emit the 'movt' instruction. 00715 Binary = 0x34 << 20; // movt: Insts{27-20} = 0b00110100 00716 00717 // Set the conditional execution predicate. 00718 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00719 00720 // Encode Rd. 00721 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; 00722 00723 // Encode imm16 as imm4:imm1, same as movw above. 00724 Binary |= Hi16 & 0xFFF; 00725 Binary |= ((Hi16 >> 12) & 0xF) << 16; 00726 emitWordLE(Binary); 00727 } 00728 00729 void ARMCodeEmitter::emitMOVi2piecesInstruction(const MachineInstr &MI) { 00730 const MachineOperand &MO0 = MI.getOperand(0); 00731 const MachineOperand &MO1 = MI.getOperand(1); 00732 assert(MO1.isImm() && ARM_AM::isSOImmTwoPartVal(MO1.getImm()) && 00733 "Not a valid so_imm value!"); 00734 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm()); 00735 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm()); 00736 00737 // Emit the 'mov' instruction. 00738 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101 00739 00740 // Set the conditional execution predicate. 00741 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00742 00743 // Encode Rd. 00744 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; 00745 00746 // Encode so_imm. 00747 // Set bit I(25) to identify this is the immediate form of <shifter_op> 00748 Binary |= 1 << ARMII::I_BitShift; 00749 Binary |= getMachineSoImmOpValue(V1); 00750 emitWordLE(Binary); 00751 00752 // Now the 'orr' instruction. 00753 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100 00754 00755 // Set the conditional execution predicate. 00756 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00757 00758 // Encode Rd. 00759 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; 00760 00761 // Encode Rn. 00762 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift; 00763 00764 // Encode so_imm. 00765 // Set bit I(25) to identify this is the immediate form of <shifter_op> 00766 Binary |= 1 << ARMII::I_BitShift; 00767 Binary |= getMachineSoImmOpValue(V2); 00768 emitWordLE(Binary); 00769 } 00770 00771 void ARMCodeEmitter::emitLEApcrelJTInstruction(const MachineInstr &MI) { 00772 // It's basically add r, pc, (LJTI - $+8) 00773 00774 const MCInstrDesc &MCID = MI.getDesc(); 00775 00776 // Emit the 'add' instruction. 00777 unsigned Binary = 0x4 << 21; // add: Insts{24-21} = 0b0100 00778 00779 // Set the conditional execution predicate 00780 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00781 00782 // Encode S bit if MI modifies CPSR. 00783 Binary |= getAddrModeSBit(MI, MCID); 00784 00785 // Encode Rd. 00786 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; 00787 00788 // Encode Rn which is PC. 00789 Binary |= II->getRegisterInfo().getEncodingValue(ARM::PC) << ARMII::RegRnShift; 00790 00791 // Encode the displacement. 00792 Binary |= 1 << ARMII::I_BitShift; 00793 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base); 00794 00795 emitWordLE(Binary); 00796 } 00797 00798 void ARMCodeEmitter::emitPseudoMoveInstruction(const MachineInstr &MI) { 00799 unsigned Opcode = MI.getDesc().Opcode; 00800 00801 // Part of binary is determined by TableGn. 00802 unsigned Binary = getBinaryCodeForInstr(MI); 00803 00804 // Set the conditional execution predicate 00805 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00806 00807 // Encode S bit if MI modifies CPSR. 00808 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag) 00809 Binary |= 1 << ARMII::S_BitShift; 00810 00811 // Encode register def if there is one. 00812 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; 00813 00814 // Encode the shift operation. 00815 switch (Opcode) { 00816 default: break; 00817 case ARM::RRX: 00818 // rrx 00819 Binary |= 0x6 << 4; 00820 break; 00821 case ARM::MOVsrl_flag: 00822 // lsr #1 00823 Binary |= (0x2 << 4) | (1 << 7); 00824 break; 00825 case ARM::MOVsra_flag: 00826 // asr #1 00827 Binary |= (0x4 << 4) | (1 << 7); 00828 break; 00829 } 00830 00831 // Encode register Rm. 00832 Binary |= getMachineOpValue(MI, 1); 00833 00834 emitWordLE(Binary); 00835 } 00836 00837 void ARMCodeEmitter::addPCLabel(unsigned LabelID) { 00838 DEBUG(errs() << " ** LPC" << LabelID << " @ " 00839 << (void*)MCE.getCurrentPCValue() << '\n'); 00840 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue()); 00841 } 00842 00843 void ARMCodeEmitter::emitPseudoInstruction(const MachineInstr &MI) { 00844 unsigned Opcode = MI.getDesc().Opcode; 00845 switch (Opcode) { 00846 default: 00847 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction"); 00848 case ARM::BX_CALL: 00849 case ARM::BMOVPCRX_CALL: { 00850 // First emit mov lr, pc 00851 unsigned Binary = 0x01a0e00f; 00852 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00853 emitWordLE(Binary); 00854 00855 // and then emit the branch. 00856 emitMiscBranchInstruction(MI); 00857 break; 00858 } 00859 case TargetOpcode::INLINEASM: { 00860 // We allow inline assembler nodes with empty bodies - they can 00861 // implicitly define registers, which is ok for JIT. 00862 if (MI.getOperand(0).getSymbolName()[0]) { 00863 report_fatal_error("JIT does not support inline asm!"); 00864 } 00865 break; 00866 } 00867 case TargetOpcode::PROLOG_LABEL: 00868 case TargetOpcode::EH_LABEL: 00869 MCE.emitLabel(MI.getOperand(0).getMCSymbol()); 00870 break; 00871 case TargetOpcode::IMPLICIT_DEF: 00872 case TargetOpcode::KILL: 00873 // Do nothing. 00874 break; 00875 case ARM::CONSTPOOL_ENTRY: 00876 emitConstPoolInstruction(MI); 00877 break; 00878 case ARM::PICADD: { 00879 // Remember of the address of the PC label for relocation later. 00880 addPCLabel(MI.getOperand(2).getImm()); 00881 // PICADD is just an add instruction that implicitly read pc. 00882 emitDataProcessingInstruction(MI, 0, ARM::PC); 00883 break; 00884 } 00885 case ARM::PICLDR: 00886 case ARM::PICLDRB: 00887 case ARM::PICSTR: 00888 case ARM::PICSTRB: { 00889 // Remember of the address of the PC label for relocation later. 00890 addPCLabel(MI.getOperand(2).getImm()); 00891 // These are just load / store instructions that implicitly read pc. 00892 emitLoadStoreInstruction(MI, 0, ARM::PC); 00893 break; 00894 } 00895 case ARM::PICLDRH: 00896 case ARM::PICLDRSH: 00897 case ARM::PICLDRSB: 00898 case ARM::PICSTRH: { 00899 // Remember of the address of the PC label for relocation later. 00900 addPCLabel(MI.getOperand(2).getImm()); 00901 // These are just load / store instructions that implicitly read pc. 00902 emitMiscLoadStoreInstruction(MI, ARM::PC); 00903 break; 00904 } 00905 00906 case ARM::MOVi32imm: 00907 // Two instructions to materialize a constant. 00908 if (Subtarget->hasV6T2Ops()) 00909 emitMOVi32immInstruction(MI); 00910 else 00911 emitMOVi2piecesInstruction(MI); 00912 break; 00913 00914 case ARM::LEApcrelJT: 00915 // Materialize jumptable address. 00916 emitLEApcrelJTInstruction(MI); 00917 break; 00918 case ARM::RRX: 00919 case ARM::MOVsrl_flag: 00920 case ARM::MOVsra_flag: 00921 emitPseudoMoveInstruction(MI); 00922 break; 00923 } 00924 } 00925 00926 unsigned ARMCodeEmitter::getMachineSoRegOpValue(const MachineInstr &MI, 00927 const MCInstrDesc &MCID, 00928 const MachineOperand &MO, 00929 unsigned OpIdx) { 00930 unsigned Binary = getMachineOpValue(MI, MO); 00931 00932 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1); 00933 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2); 00934 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm()); 00935 00936 // Encode the shift opcode. 00937 unsigned SBits = 0; 00938 unsigned Rs = MO1.getReg(); 00939 if (Rs) { 00940 // Set shift operand (bit[7:4]). 00941 // LSL - 0001 00942 // LSR - 0011 00943 // ASR - 0101 00944 // ROR - 0111 00945 // RRX - 0110 and bit[11:8] clear. 00946 switch (SOpc) { 00947 default: llvm_unreachable("Unknown shift opc!"); 00948 case ARM_AM::lsl: SBits = 0x1; break; 00949 case ARM_AM::lsr: SBits = 0x3; break; 00950 case ARM_AM::asr: SBits = 0x5; break; 00951 case ARM_AM::ror: SBits = 0x7; break; 00952 case ARM_AM::rrx: SBits = 0x6; break; 00953 } 00954 } else { 00955 // Set shift operand (bit[6:4]). 00956 // LSL - 000 00957 // LSR - 010 00958 // ASR - 100 00959 // ROR - 110 00960 switch (SOpc) { 00961 default: llvm_unreachable("Unknown shift opc!"); 00962 case ARM_AM::lsl: SBits = 0x0; break; 00963 case ARM_AM::lsr: SBits = 0x2; break; 00964 case ARM_AM::asr: SBits = 0x4; break; 00965 case ARM_AM::ror: SBits = 0x6; break; 00966 } 00967 } 00968 Binary |= SBits << 4; 00969 if (SOpc == ARM_AM::rrx) 00970 return Binary; 00971 00972 // Encode the shift operation Rs or shift_imm (except rrx). 00973 if (Rs) { 00974 // Encode Rs bit[11:8]. 00975 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0); 00976 return Binary | (II->getRegisterInfo().getEncodingValue(Rs) << ARMII::RegRsShift); 00977 } 00978 00979 // Encode shift_imm bit[11:7]. 00980 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7; 00981 } 00982 00983 unsigned ARMCodeEmitter::getMachineSoImmOpValue(unsigned SoImm) { 00984 int SoImmVal = ARM_AM::getSOImmVal(SoImm); 00985 assert(SoImmVal != -1 && "Not a valid so_imm value!"); 00986 00987 // Encode rotate_imm. 00988 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1) 00989 << ARMII::SoRotImmShift; 00990 00991 // Encode immed_8. 00992 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal); 00993 return Binary; 00994 } 00995 00996 unsigned ARMCodeEmitter::getAddrModeSBit(const MachineInstr &MI, 00997 const MCInstrDesc &MCID) const { 00998 for (unsigned i = MI.getNumOperands(), e = MCID.getNumOperands(); i >= e;--i){ 00999 const MachineOperand &MO = MI.getOperand(i-1); 01000 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) 01001 return 1 << ARMII::S_BitShift; 01002 } 01003 return 0; 01004 } 01005 01006 void ARMCodeEmitter::emitDataProcessingInstruction(const MachineInstr &MI, 01007 unsigned ImplicitRd, 01008 unsigned ImplicitRn) { 01009 const MCInstrDesc &MCID = MI.getDesc(); 01010 01011 // Part of binary is determined by TableGn. 01012 unsigned Binary = getBinaryCodeForInstr(MI); 01013 01014 // Set the conditional execution predicate 01015 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01016 01017 // Encode S bit if MI modifies CPSR. 01018 Binary |= getAddrModeSBit(MI, MCID); 01019 01020 // Encode register def if there is one. 01021 unsigned NumDefs = MCID.getNumDefs(); 01022 unsigned OpIdx = 0; 01023 if (NumDefs) 01024 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01025 else if (ImplicitRd) 01026 // Special handling for implicit use (e.g. PC). 01027 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift); 01028 01029 if (MCID.Opcode == ARM::MOVi16) { 01030 // Get immediate from MI. 01031 unsigned Lo16 = getMovi32Value(MI, MI.getOperand(OpIdx), 01032 ARM::reloc_arm_movw); 01033 // Encode imm which is the same as in emitMOVi32immInstruction(). 01034 Binary |= Lo16 & 0xFFF; 01035 Binary |= ((Lo16 >> 12) & 0xF) << 16; 01036 emitWordLE(Binary); 01037 return; 01038 } else if(MCID.Opcode == ARM::MOVTi16) { 01039 unsigned Hi16 = (getMovi32Value(MI, MI.getOperand(OpIdx), 01040 ARM::reloc_arm_movt) >> 16); 01041 Binary |= Hi16 & 0xFFF; 01042 Binary |= ((Hi16 >> 12) & 0xF) << 16; 01043 emitWordLE(Binary); 01044 return; 01045 } else if ((MCID.Opcode == ARM::BFC) || (MCID.Opcode == ARM::BFI)) { 01046 uint32_t v = ~MI.getOperand(2).getImm(); 01047 int32_t lsb = CountTrailingZeros_32(v); 01048 int32_t msb = (32 - CountLeadingZeros_32(v)) - 1; 01049 // Instr{20-16} = msb, Instr{11-7} = lsb 01050 Binary |= (msb & 0x1F) << 16; 01051 Binary |= (lsb & 0x1F) << 7; 01052 emitWordLE(Binary); 01053 return; 01054 } else if ((MCID.Opcode == ARM::UBFX) || (MCID.Opcode == ARM::SBFX)) { 01055 // Encode Rn in Instr{0-3} 01056 Binary |= getMachineOpValue(MI, OpIdx++); 01057 01058 uint32_t lsb = MI.getOperand(OpIdx++).getImm(); 01059 uint32_t widthm1 = MI.getOperand(OpIdx++).getImm() - 1; 01060 01061 // Instr{20-16} = widthm1, Instr{11-7} = lsb 01062 Binary |= (widthm1 & 0x1F) << 16; 01063 Binary |= (lsb & 0x1F) << 7; 01064 emitWordLE(Binary); 01065 return; 01066 } 01067 01068 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1. 01069 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01070 ++OpIdx; 01071 01072 // Encode first non-shifter register operand if there is one. 01073 bool isUnary = MCID.TSFlags & ARMII::UnaryDP; 01074 if (!isUnary) { 01075 if (ImplicitRn) 01076 // Special handling for implicit use (e.g. PC). 01077 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift); 01078 else { 01079 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift; 01080 ++OpIdx; 01081 } 01082 } 01083 01084 // Encode shifter operand. 01085 const MachineOperand &MO = MI.getOperand(OpIdx); 01086 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) { 01087 // Encode SoReg. 01088 emitWordLE(Binary | getMachineSoRegOpValue(MI, MCID, MO, OpIdx)); 01089 return; 01090 } 01091 01092 if (MO.isReg()) { 01093 // Encode register Rm. 01094 emitWordLE(Binary | II->getRegisterInfo().getEncodingValue(MO.getReg())); 01095 return; 01096 } 01097 01098 // Encode so_imm. 01099 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm()); 01100 01101 emitWordLE(Binary); 01102 } 01103 01104 void ARMCodeEmitter::emitLoadStoreInstruction(const MachineInstr &MI, 01105 unsigned ImplicitRd, 01106 unsigned ImplicitRn) { 01107 const MCInstrDesc &MCID = MI.getDesc(); 01108 unsigned Form = MCID.TSFlags & ARMII::FormMask; 01109 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0; 01110 01111 // Part of binary is determined by TableGn. 01112 unsigned Binary = getBinaryCodeForInstr(MI); 01113 01114 // If this is an LDRi12, STRi12 or LDRcp, nothing more needs be done. 01115 if (MI.getOpcode() == ARM::LDRi12 || MI.getOpcode() == ARM::LDRcp || 01116 MI.getOpcode() == ARM::STRi12) { 01117 emitWordLE(Binary); 01118 return; 01119 } 01120 01121 // Set the conditional execution predicate 01122 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01123 01124 unsigned OpIdx = 0; 01125 01126 // Operand 0 of a pre- and post-indexed store is the address base 01127 // writeback. Skip it. 01128 bool Skipped = false; 01129 if (IsPrePost && Form == ARMII::StFrm) { 01130 ++OpIdx; 01131 Skipped = true; 01132 } 01133 01134 // Set first operand 01135 if (ImplicitRd) 01136 // Special handling for implicit use (e.g. PC). 01137 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRd) << ARMII::RegRdShift); 01138 else 01139 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01140 01141 // Set second operand 01142 if (ImplicitRn) 01143 // Special handling for implicit use (e.g. PC). 01144 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift); 01145 else 01146 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; 01147 01148 // If this is a two-address operand, skip it. e.g. LDR_PRE. 01149 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01150 ++OpIdx; 01151 01152 const MachineOperand &MO2 = MI.getOperand(OpIdx); 01153 unsigned AM2Opc = (ImplicitRn == ARM::PC) 01154 ? 0 : MI.getOperand(OpIdx+1).getImm(); 01155 01156 // Set bit U(23) according to sign of immed value (positive or negative). 01157 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) << 01158 ARMII::U_BitShift); 01159 if (!MO2.getReg()) { // is immediate 01160 if (ARM_AM::getAM2Offset(AM2Opc)) 01161 // Set the value of offset_12 field 01162 Binary |= ARM_AM::getAM2Offset(AM2Opc); 01163 emitWordLE(Binary); 01164 return; 01165 } 01166 01167 // Set bit I(25), because this is not in immediate encoding. 01168 Binary |= 1 << ARMII::I_BitShift; 01169 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg())); 01170 // Set bit[3:0] to the corresponding Rm register 01171 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg()); 01172 01173 // If this instr is in scaled register offset/index instruction, set 01174 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields. 01175 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) { 01176 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift 01177 Binary |= ShImm << ARMII::ShiftShift; // shift_immed 01178 } 01179 01180 emitWordLE(Binary); 01181 } 01182 01183 void ARMCodeEmitter::emitMiscLoadStoreInstruction(const MachineInstr &MI, 01184 unsigned ImplicitRn) { 01185 const MCInstrDesc &MCID = MI.getDesc(); 01186 unsigned Form = MCID.TSFlags & ARMII::FormMask; 01187 bool IsPrePost = (MCID.TSFlags & ARMII::IndexModeMask) != 0; 01188 01189 // Part of binary is determined by TableGn. 01190 unsigned Binary = getBinaryCodeForInstr(MI); 01191 01192 // Set the conditional execution predicate 01193 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01194 01195 unsigned OpIdx = 0; 01196 01197 // Operand 0 of a pre- and post-indexed store is the address base 01198 // writeback. Skip it. 01199 bool Skipped = false; 01200 if (IsPrePost && Form == ARMII::StMiscFrm) { 01201 ++OpIdx; 01202 Skipped = true; 01203 } 01204 01205 // Set first operand 01206 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01207 01208 // Skip LDRD and STRD's second operand. 01209 if (MCID.Opcode == ARM::LDRD || MCID.Opcode == ARM::STRD) 01210 ++OpIdx; 01211 01212 // Set second operand 01213 if (ImplicitRn) 01214 // Special handling for implicit use (e.g. PC). 01215 Binary |= (II->getRegisterInfo().getEncodingValue(ImplicitRn) << ARMII::RegRnShift); 01216 else 01217 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; 01218 01219 // If this is a two-address operand, skip it. e.g. LDRH_POST. 01220 if (!Skipped && MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01221 ++OpIdx; 01222 01223 const MachineOperand &MO2 = MI.getOperand(OpIdx); 01224 unsigned AM3Opc = (ImplicitRn == ARM::PC) 01225 ? 0 : MI.getOperand(OpIdx+1).getImm(); 01226 01227 // Set bit U(23) according to sign of immed value (positive or negative) 01228 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) << 01229 ARMII::U_BitShift); 01230 01231 // If this instr is in register offset/index encoding, set bit[3:0] 01232 // to the corresponding Rm register. 01233 if (MO2.getReg()) { 01234 Binary |= II->getRegisterInfo().getEncodingValue(MO2.getReg()); 01235 emitWordLE(Binary); 01236 return; 01237 } 01238 01239 // This instr is in immediate offset/index encoding, set bit 22 to 1. 01240 Binary |= 1 << ARMII::AM3_I_BitShift; 01241 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) { 01242 // Set operands 01243 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH 01244 Binary |= (ImmOffs & 0xF); // immedL 01245 } 01246 01247 emitWordLE(Binary); 01248 } 01249 01250 static unsigned getAddrModeUPBits(unsigned Mode) { 01251 unsigned Binary = 0; 01252 01253 // Set addressing mode by modifying bits U(23) and P(24) 01254 // IA - Increment after - bit U = 1 and bit P = 0 01255 // IB - Increment before - bit U = 1 and bit P = 1 01256 // DA - Decrement after - bit U = 0 and bit P = 0 01257 // DB - Decrement before - bit U = 0 and bit P = 1 01258 switch (Mode) { 01259 default: llvm_unreachable("Unknown addressing sub-mode!"); 01260 case ARM_AM::da: break; 01261 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break; 01262 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break; 01263 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break; 01264 } 01265 01266 return Binary; 01267 } 01268 01269 void ARMCodeEmitter::emitLoadStoreMultipleInstruction(const MachineInstr &MI) { 01270 const MCInstrDesc &MCID = MI.getDesc(); 01271 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0; 01272 01273 // Part of binary is determined by TableGn. 01274 unsigned Binary = getBinaryCodeForInstr(MI); 01275 01276 // Set the conditional execution predicate 01277 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01278 01279 // Skip operand 0 of an instruction with base register update. 01280 unsigned OpIdx = 0; 01281 if (IsUpdating) 01282 ++OpIdx; 01283 01284 // Set base address operand 01285 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; 01286 01287 // Set addressing mode by modifying bits U(23) and P(24) 01288 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode()); 01289 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode)); 01290 01291 // Set bit W(21) 01292 if (IsUpdating) 01293 Binary |= 0x1 << ARMII::W_BitShift; 01294 01295 // Set registers 01296 for (unsigned i = OpIdx+2, e = MI.getNumOperands(); i != e; ++i) { 01297 const MachineOperand &MO = MI.getOperand(i); 01298 if (!MO.isReg() || MO.isImplicit()) 01299 break; 01300 unsigned RegNum = II->getRegisterInfo().getEncodingValue(MO.getReg()); 01301 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && 01302 RegNum < 16); 01303 Binary |= 0x1 << RegNum; 01304 } 01305 01306 emitWordLE(Binary); 01307 } 01308 01309 void ARMCodeEmitter::emitMulFrmInstruction(const MachineInstr &MI) { 01310 const MCInstrDesc &MCID = MI.getDesc(); 01311 01312 // Part of binary is determined by TableGn. 01313 unsigned Binary = getBinaryCodeForInstr(MI); 01314 01315 // Set the conditional execution predicate 01316 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01317 01318 // Encode S bit if MI modifies CPSR. 01319 Binary |= getAddrModeSBit(MI, MCID); 01320 01321 // 32x32->64bit operations have two destination registers. The number 01322 // of register definitions will tell us if that's what we're dealing with. 01323 unsigned OpIdx = 0; 01324 if (MCID.getNumDefs() == 2) 01325 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift; 01326 01327 // Encode Rd 01328 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift; 01329 01330 // Encode Rm 01331 Binary |= getMachineOpValue(MI, OpIdx++); 01332 01333 // Encode Rs 01334 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift; 01335 01336 // Many multiple instructions (e.g. MLA) have three src operands. Encode 01337 // it as Rn (for multiply, that's in the same offset as RdLo. 01338 if (MCID.getNumOperands() > OpIdx && 01339 !MCID.OpInfo[OpIdx].isPredicate() && 01340 !MCID.OpInfo[OpIdx].isOptionalDef()) 01341 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift; 01342 01343 emitWordLE(Binary); 01344 } 01345 01346 void ARMCodeEmitter::emitExtendInstruction(const MachineInstr &MI) { 01347 const MCInstrDesc &MCID = MI.getDesc(); 01348 01349 // Part of binary is determined by TableGn. 01350 unsigned Binary = getBinaryCodeForInstr(MI); 01351 01352 // Set the conditional execution predicate 01353 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01354 01355 unsigned OpIdx = 0; 01356 01357 // Encode Rd 01358 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01359 01360 const MachineOperand &MO1 = MI.getOperand(OpIdx++); 01361 const MachineOperand &MO2 = MI.getOperand(OpIdx); 01362 if (MO2.isReg()) { 01363 // Two register operand form. 01364 // Encode Rn. 01365 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift; 01366 01367 // Encode Rm. 01368 Binary |= getMachineOpValue(MI, MO2); 01369 ++OpIdx; 01370 } else { 01371 Binary |= getMachineOpValue(MI, MO1); 01372 } 01373 01374 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand. 01375 if (MI.getOperand(OpIdx).isImm() && 01376 !MCID.OpInfo[OpIdx].isPredicate() && 01377 !MCID.OpInfo[OpIdx].isOptionalDef()) 01378 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift; 01379 01380 emitWordLE(Binary); 01381 } 01382 01383 void ARMCodeEmitter::emitMiscArithInstruction(const MachineInstr &MI) { 01384 const MCInstrDesc &MCID = MI.getDesc(); 01385 01386 // Part of binary is determined by TableGn. 01387 unsigned Binary = getBinaryCodeForInstr(MI); 01388 01389 // Set the conditional execution predicate 01390 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01391 01392 // PKH instructions are finished at this point 01393 if (MCID.Opcode == ARM::PKHBT || MCID.Opcode == ARM::PKHTB) { 01394 emitWordLE(Binary); 01395 return; 01396 } 01397 01398 unsigned OpIdx = 0; 01399 01400 // Encode Rd 01401 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01402 01403 const MachineOperand &MO = MI.getOperand(OpIdx++); 01404 if (OpIdx == MCID.getNumOperands() || 01405 MCID.OpInfo[OpIdx].isPredicate() || 01406 MCID.OpInfo[OpIdx].isOptionalDef()) { 01407 // Encode Rm and it's done. 01408 Binary |= getMachineOpValue(MI, MO); 01409 emitWordLE(Binary); 01410 return; 01411 } 01412 01413 // Encode Rn. 01414 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift; 01415 01416 // Encode Rm. 01417 Binary |= getMachineOpValue(MI, OpIdx++); 01418 01419 // Encode shift_imm. 01420 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm(); 01421 if (MCID.Opcode == ARM::PKHTB) { 01422 assert(ShiftAmt != 0 && "PKHTB shift_imm is 0!"); 01423 if (ShiftAmt == 32) 01424 ShiftAmt = 0; 01425 } 01426 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!"); 01427 Binary |= ShiftAmt << ARMII::ShiftShift; 01428 01429 emitWordLE(Binary); 01430 } 01431 01432 void ARMCodeEmitter::emitSaturateInstruction(const MachineInstr &MI) { 01433 const MCInstrDesc &MCID = MI.getDesc(); 01434 01435 // Part of binary is determined by TableGen. 01436 unsigned Binary = getBinaryCodeForInstr(MI); 01437 01438 // Set the conditional execution predicate 01439 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01440 01441 // Encode Rd 01442 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; 01443 01444 // Encode saturate bit position. 01445 unsigned Pos = MI.getOperand(1).getImm(); 01446 if (MCID.Opcode == ARM::SSAT || MCID.Opcode == ARM::SSAT16) 01447 Pos -= 1; 01448 assert((Pos < 16 || (Pos < 32 && 01449 MCID.Opcode != ARM::SSAT16 && 01450 MCID.Opcode != ARM::USAT16)) && 01451 "saturate bit position out of range"); 01452 Binary |= Pos << 16; 01453 01454 // Encode Rm 01455 Binary |= getMachineOpValue(MI, 2); 01456 01457 // Encode shift_imm. 01458 if (MCID.getNumOperands() == 4) { 01459 unsigned ShiftOp = MI.getOperand(3).getImm(); 01460 ARM_AM::ShiftOpc Opc = ARM_AM::getSORegShOp(ShiftOp); 01461 if (Opc == ARM_AM::asr) 01462 Binary |= (1 << 6); 01463 unsigned ShiftAmt = MI.getOperand(3).getImm(); 01464 if (ShiftAmt == 32 && Opc == ARM_AM::asr) 01465 ShiftAmt = 0; 01466 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!"); 01467 Binary |= ShiftAmt << ARMII::ShiftShift; 01468 } 01469 01470 emitWordLE(Binary); 01471 } 01472 01473 void ARMCodeEmitter::emitBranchInstruction(const MachineInstr &MI) { 01474 const MCInstrDesc &MCID = MI.getDesc(); 01475 01476 if (MCID.Opcode == ARM::TPsoft) { 01477 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME 01478 } 01479 01480 // Part of binary is determined by TableGn. 01481 unsigned Binary = getBinaryCodeForInstr(MI); 01482 01483 // Set the conditional execution predicate 01484 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01485 01486 // Set signed_immed_24 field 01487 Binary |= getMachineOpValue(MI, 0); 01488 01489 emitWordLE(Binary); 01490 } 01491 01492 void ARMCodeEmitter::emitInlineJumpTable(unsigned JTIndex) { 01493 // Remember the base address of the inline jump table. 01494 uintptr_t JTBase = MCE.getCurrentPCValue(); 01495 JTI->addJumpTableBaseAddr(JTIndex, JTBase); 01496 DEBUG(errs() << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase 01497 << '\n'); 01498 01499 // Now emit the jump table entries. 01500 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs; 01501 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) { 01502 if (IsPIC) 01503 // DestBB address - JT base. 01504 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase); 01505 else 01506 // Absolute DestBB address. 01507 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute); 01508 emitWordLE(0); 01509 } 01510 } 01511 01512 void ARMCodeEmitter::emitMiscBranchInstruction(const MachineInstr &MI) { 01513 const MCInstrDesc &MCID = MI.getDesc(); 01514 01515 // Handle jump tables. 01516 if (MCID.Opcode == ARM::BR_JTr || MCID.Opcode == ARM::BR_JTadd) { 01517 // First emit a ldr pc, [] instruction. 01518 emitDataProcessingInstruction(MI, ARM::PC); 01519 01520 // Then emit the inline jump table. 01521 unsigned JTIndex = 01522 (MCID.Opcode == ARM::BR_JTr) 01523 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex(); 01524 emitInlineJumpTable(JTIndex); 01525 return; 01526 } else if (MCID.Opcode == ARM::BR_JTm) { 01527 // First emit a ldr pc, [] instruction. 01528 emitLoadStoreInstruction(MI, ARM::PC); 01529 01530 // Then emit the inline jump table. 01531 emitInlineJumpTable(MI.getOperand(3).getIndex()); 01532 return; 01533 } 01534 01535 // Part of binary is determined by TableGn. 01536 unsigned Binary = getBinaryCodeForInstr(MI); 01537 01538 // Set the conditional execution predicate 01539 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01540 01541 if (MCID.Opcode == ARM::BX_RET || MCID.Opcode == ARM::MOVPCLR) 01542 // The return register is LR. 01543 Binary |= II->getRegisterInfo().getEncodingValue(ARM::LR); 01544 else 01545 // otherwise, set the return register 01546 Binary |= getMachineOpValue(MI, 0); 01547 01548 emitWordLE(Binary); 01549 } 01550 01551 unsigned ARMCodeEmitter::encodeVFPRd(const MachineInstr &MI, 01552 unsigned OpIdx) const { 01553 unsigned RegD = MI.getOperand(OpIdx).getReg(); 01554 unsigned Binary = 0; 01555 bool isSPVFP = ARM::SPRRegClass.contains(RegD); 01556 RegD = II->getRegisterInfo().getEncodingValue(RegD); 01557 if (!isSPVFP) 01558 Binary |= RegD << ARMII::RegRdShift; 01559 else { 01560 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift; 01561 Binary |= (RegD & 0x01) << ARMII::D_BitShift; 01562 } 01563 return Binary; 01564 } 01565 01566 unsigned ARMCodeEmitter::encodeVFPRn(const MachineInstr &MI, 01567 unsigned OpIdx) const { 01568 unsigned RegN = MI.getOperand(OpIdx).getReg(); 01569 unsigned Binary = 0; 01570 bool isSPVFP = ARM::SPRRegClass.contains(RegN); 01571 RegN = II->getRegisterInfo().getEncodingValue(RegN); 01572 if (!isSPVFP) 01573 Binary |= RegN << ARMII::RegRnShift; 01574 else { 01575 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift; 01576 Binary |= (RegN & 0x01) << ARMII::N_BitShift; 01577 } 01578 return Binary; 01579 } 01580 01581 unsigned ARMCodeEmitter::encodeVFPRm(const MachineInstr &MI, 01582 unsigned OpIdx) const { 01583 unsigned RegM = MI.getOperand(OpIdx).getReg(); 01584 unsigned Binary = 0; 01585 bool isSPVFP = ARM::SPRRegClass.contains(RegM); 01586 RegM = II->getRegisterInfo().getEncodingValue(RegM); 01587 if (!isSPVFP) 01588 Binary |= RegM; 01589 else { 01590 Binary |= ((RegM & 0x1E) >> 1); 01591 Binary |= (RegM & 0x01) << ARMII::M_BitShift; 01592 } 01593 return Binary; 01594 } 01595 01596 void ARMCodeEmitter::emitVFPArithInstruction(const MachineInstr &MI) { 01597 const MCInstrDesc &MCID = MI.getDesc(); 01598 01599 // Part of binary is determined by TableGn. 01600 unsigned Binary = getBinaryCodeForInstr(MI); 01601 01602 // Set the conditional execution predicate 01603 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01604 01605 unsigned OpIdx = 0; 01606 assert((Binary & ARMII::D_BitShift) == 0 && 01607 (Binary & ARMII::N_BitShift) == 0 && 01608 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!"); 01609 01610 // Encode Dd / Sd. 01611 Binary |= encodeVFPRd(MI, OpIdx++); 01612 01613 // If this is a two-address operand, skip it, e.g. FMACD. 01614 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01615 ++OpIdx; 01616 01617 // Encode Dn / Sn. 01618 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm) 01619 Binary |= encodeVFPRn(MI, OpIdx++); 01620 01621 if (OpIdx == MCID.getNumOperands() || 01622 MCID.OpInfo[OpIdx].isPredicate() || 01623 MCID.OpInfo[OpIdx].isOptionalDef()) { 01624 // FCMPEZD etc. has only one operand. 01625 emitWordLE(Binary); 01626 return; 01627 } 01628 01629 // Encode Dm / Sm. 01630 Binary |= encodeVFPRm(MI, OpIdx); 01631 01632 emitWordLE(Binary); 01633 } 01634 01635 void ARMCodeEmitter::emitVFPConversionInstruction(const MachineInstr &MI) { 01636 const MCInstrDesc &MCID = MI.getDesc(); 01637 unsigned Form = MCID.TSFlags & ARMII::FormMask; 01638 01639 // Part of binary is determined by TableGn. 01640 unsigned Binary = getBinaryCodeForInstr(MI); 01641 01642 // Set the conditional execution predicate 01643 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01644 01645 switch (Form) { 01646 default: break; 01647 case ARMII::VFPConv1Frm: 01648 case ARMII::VFPConv2Frm: 01649 case ARMII::VFPConv3Frm: 01650 // Encode Dd / Sd. 01651 Binary |= encodeVFPRd(MI, 0); 01652 break; 01653 case ARMII::VFPConv4Frm: 01654 // Encode Dn / Sn. 01655 Binary |= encodeVFPRn(MI, 0); 01656 break; 01657 case ARMII::VFPConv5Frm: 01658 // Encode Dm / Sm. 01659 Binary |= encodeVFPRm(MI, 0); 01660 break; 01661 } 01662 01663 switch (Form) { 01664 default: break; 01665 case ARMII::VFPConv1Frm: 01666 // Encode Dm / Sm. 01667 Binary |= encodeVFPRm(MI, 1); 01668 break; 01669 case ARMII::VFPConv2Frm: 01670 case ARMII::VFPConv3Frm: 01671 // Encode Dn / Sn. 01672 Binary |= encodeVFPRn(MI, 1); 01673 break; 01674 case ARMII::VFPConv4Frm: 01675 case ARMII::VFPConv5Frm: 01676 // Encode Dd / Sd. 01677 Binary |= encodeVFPRd(MI, 1); 01678 break; 01679 } 01680 01681 if (Form == ARMII::VFPConv5Frm) 01682 // Encode Dn / Sn. 01683 Binary |= encodeVFPRn(MI, 2); 01684 else if (Form == ARMII::VFPConv3Frm) 01685 // Encode Dm / Sm. 01686 Binary |= encodeVFPRm(MI, 2); 01687 01688 emitWordLE(Binary); 01689 } 01690 01691 void ARMCodeEmitter::emitVFPLoadStoreInstruction(const MachineInstr &MI) { 01692 // Part of binary is determined by TableGn. 01693 unsigned Binary = getBinaryCodeForInstr(MI); 01694 01695 // Set the conditional execution predicate 01696 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01697 01698 unsigned OpIdx = 0; 01699 01700 // Encode Dd / Sd. 01701 Binary |= encodeVFPRd(MI, OpIdx++); 01702 01703 // Encode address base. 01704 const MachineOperand &Base = MI.getOperand(OpIdx++); 01705 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift; 01706 01707 // If there is a non-zero immediate offset, encode it. 01708 if (Base.isReg()) { 01709 const MachineOperand &Offset = MI.getOperand(OpIdx); 01710 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) { 01711 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add) 01712 Binary |= 1 << ARMII::U_BitShift; 01713 Binary |= ImmOffs; 01714 emitWordLE(Binary); 01715 return; 01716 } 01717 } 01718 01719 // If immediate offset is omitted, default to +0. 01720 Binary |= 1 << ARMII::U_BitShift; 01721 01722 emitWordLE(Binary); 01723 } 01724 01725 void 01726 ARMCodeEmitter::emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI) { 01727 const MCInstrDesc &MCID = MI.getDesc(); 01728 bool IsUpdating = (MCID.TSFlags & ARMII::IndexModeMask) != 0; 01729 01730 // Part of binary is determined by TableGn. 01731 unsigned Binary = getBinaryCodeForInstr(MI); 01732 01733 // Set the conditional execution predicate 01734 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01735 01736 // Skip operand 0 of an instruction with base register update. 01737 unsigned OpIdx = 0; 01738 if (IsUpdating) 01739 ++OpIdx; 01740 01741 // Set base address operand 01742 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; 01743 01744 // Set addressing mode by modifying bits U(23) and P(24) 01745 ARM_AM::AMSubMode Mode = ARM_AM::getLoadStoreMultipleSubMode(MI.getOpcode()); 01746 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(Mode)); 01747 01748 // Set bit W(21) 01749 if (IsUpdating) 01750 Binary |= 0x1 << ARMII::W_BitShift; 01751 01752 // First register is encoded in Dd. 01753 Binary |= encodeVFPRd(MI, OpIdx+2); 01754 01755 // Count the number of registers. 01756 unsigned NumRegs = 1; 01757 for (unsigned i = OpIdx+3, e = MI.getNumOperands(); i != e; ++i) { 01758 const MachineOperand &MO = MI.getOperand(i); 01759 if (!MO.isReg() || MO.isImplicit()) 01760 break; 01761 ++NumRegs; 01762 } 01763 // Bit 8 will be set if <list> is consecutive 64-bit registers (e.g., D0) 01764 // Otherwise, it will be 0, in the case of 32-bit registers. 01765 if(Binary & 0x100) 01766 Binary |= NumRegs * 2; 01767 else 01768 Binary |= NumRegs; 01769 01770 emitWordLE(Binary); 01771 } 01772 01773 unsigned ARMCodeEmitter::encodeNEONRd(const MachineInstr &MI, 01774 unsigned OpIdx) const { 01775 unsigned RegD = MI.getOperand(OpIdx).getReg(); 01776 unsigned Binary = 0; 01777 RegD = II->getRegisterInfo().getEncodingValue(RegD); 01778 Binary |= (RegD & 0xf) << ARMII::RegRdShift; 01779 Binary |= ((RegD >> 4) & 1) << ARMII::D_BitShift; 01780 return Binary; 01781 } 01782 01783 unsigned ARMCodeEmitter::encodeNEONRn(const MachineInstr &MI, 01784 unsigned OpIdx) const { 01785 unsigned RegN = MI.getOperand(OpIdx).getReg(); 01786 unsigned Binary = 0; 01787 RegN = II->getRegisterInfo().getEncodingValue(RegN); 01788 Binary |= (RegN & 0xf) << ARMII::RegRnShift; 01789 Binary |= ((RegN >> 4) & 1) << ARMII::N_BitShift; 01790 return Binary; 01791 } 01792 01793 unsigned ARMCodeEmitter::encodeNEONRm(const MachineInstr &MI, 01794 unsigned OpIdx) const { 01795 unsigned RegM = MI.getOperand(OpIdx).getReg(); 01796 unsigned Binary = 0; 01797 RegM = II->getRegisterInfo().getEncodingValue(RegM); 01798 Binary |= (RegM & 0xf); 01799 Binary |= ((RegM >> 4) & 1) << ARMII::M_BitShift; 01800 return Binary; 01801 } 01802 01803 /// convertNEONDataProcToThumb - Convert the ARM mode encoding for a NEON 01804 /// data-processing instruction to the corresponding Thumb encoding. 01805 static unsigned convertNEONDataProcToThumb(unsigned Binary) { 01806 assert((Binary & 0xfe000000) == 0xf2000000 && 01807 "not an ARM NEON data-processing instruction"); 01808 unsigned UBit = (Binary >> 24) & 1; 01809 return 0xef000000 | (UBit << 28) | (Binary & 0xffffff); 01810 } 01811 01812 void ARMCodeEmitter::emitNEONLaneInstruction(const MachineInstr &MI) { 01813 unsigned Binary = getBinaryCodeForInstr(MI); 01814 01815 unsigned RegTOpIdx, RegNOpIdx, LnOpIdx; 01816 const MCInstrDesc &MCID = MI.getDesc(); 01817 if ((MCID.TSFlags & ARMII::FormMask) == ARMII::NGetLnFrm) { 01818 RegTOpIdx = 0; 01819 RegNOpIdx = 1; 01820 LnOpIdx = 2; 01821 } else { // ARMII::NSetLnFrm 01822 RegTOpIdx = 2; 01823 RegNOpIdx = 0; 01824 LnOpIdx = 3; 01825 } 01826 01827 // Set the conditional execution predicate 01828 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift; 01829 01830 unsigned RegT = MI.getOperand(RegTOpIdx).getReg(); 01831 RegT = II->getRegisterInfo().getEncodingValue(RegT); 01832 Binary |= (RegT << ARMII::RegRdShift); 01833 Binary |= encodeNEONRn(MI, RegNOpIdx); 01834 01835 unsigned LaneShift; 01836 if ((Binary & (1 << 22)) != 0) 01837 LaneShift = 0; // 8-bit elements 01838 else if ((Binary & (1 << 5)) != 0) 01839 LaneShift = 1; // 16-bit elements 01840 else 01841 LaneShift = 2; // 32-bit elements 01842 01843 unsigned Lane = MI.getOperand(LnOpIdx).getImm() << LaneShift; 01844 unsigned Opc1 = Lane >> 2; 01845 unsigned Opc2 = Lane & 3; 01846 assert((Opc1 & 3) == 0 && "out-of-range lane number operand"); 01847 Binary |= (Opc1 << 21); 01848 Binary |= (Opc2 << 5); 01849 01850 emitWordLE(Binary); 01851 } 01852 01853 void ARMCodeEmitter::emitNEONDupInstruction(const MachineInstr &MI) { 01854 unsigned Binary = getBinaryCodeForInstr(MI); 01855 01856 // Set the conditional execution predicate 01857 Binary |= (IsThumb ? ARMCC::AL : II->getPredicate(&MI)) << ARMII::CondShift; 01858 01859 unsigned RegT = MI.getOperand(1).getReg(); 01860 RegT = II->getRegisterInfo().getEncodingValue(RegT); 01861 Binary |= (RegT << ARMII::RegRdShift); 01862 Binary |= encodeNEONRn(MI, 0); 01863 emitWordLE(Binary); 01864 } 01865 01866 void ARMCodeEmitter::emitNEON1RegModImmInstruction(const MachineInstr &MI) { 01867 unsigned Binary = getBinaryCodeForInstr(MI); 01868 // Destination register is encoded in Dd. 01869 Binary |= encodeNEONRd(MI, 0); 01870 // Immediate fields: Op, Cmode, I, Imm3, Imm4 01871 unsigned Imm = MI.getOperand(1).getImm(); 01872 unsigned Op = (Imm >> 12) & 1; 01873 unsigned Cmode = (Imm >> 8) & 0xf; 01874 unsigned I = (Imm >> 7) & 1; 01875 unsigned Imm3 = (Imm >> 4) & 0x7; 01876 unsigned Imm4 = Imm & 0xf; 01877 Binary |= (I << 24) | (Imm3 << 16) | (Cmode << 8) | (Op << 5) | Imm4; 01878 if (IsThumb) 01879 Binary = convertNEONDataProcToThumb(Binary); 01880 emitWordLE(Binary); 01881 } 01882 01883 void ARMCodeEmitter::emitNEON2RegInstruction(const MachineInstr &MI) { 01884 const MCInstrDesc &MCID = MI.getDesc(); 01885 unsigned Binary = getBinaryCodeForInstr(MI); 01886 // Destination register is encoded in Dd; source register in Dm. 01887 unsigned OpIdx = 0; 01888 Binary |= encodeNEONRd(MI, OpIdx++); 01889 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01890 ++OpIdx; 01891 Binary |= encodeNEONRm(MI, OpIdx); 01892 if (IsThumb) 01893 Binary = convertNEONDataProcToThumb(Binary); 01894 // FIXME: This does not handle VDUPfdf or VDUPfqf. 01895 emitWordLE(Binary); 01896 } 01897 01898 void ARMCodeEmitter::emitNEON3RegInstruction(const MachineInstr &MI) { 01899 const MCInstrDesc &MCID = MI.getDesc(); 01900 unsigned Binary = getBinaryCodeForInstr(MI); 01901 // Destination register is encoded in Dd; source registers in Dn and Dm. 01902 unsigned OpIdx = 0; 01903 Binary |= encodeNEONRd(MI, OpIdx++); 01904 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01905 ++OpIdx; 01906 Binary |= encodeNEONRn(MI, OpIdx++); 01907 if (MCID.getOperandConstraint(OpIdx, MCOI::TIED_TO) != -1) 01908 ++OpIdx; 01909 Binary |= encodeNEONRm(MI, OpIdx); 01910 if (IsThumb) 01911 Binary = convertNEONDataProcToThumb(Binary); 01912 // FIXME: This does not handle VMOVDneon or VMOVQ. 01913 emitWordLE(Binary); 01914 } 01915 01916 #include "ARMGenCodeEmitter.inc"