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 "ARMAddressingModes.h" 00018 #include "ARMConstantPoolValue.h" 00019 #include "ARMInstrInfo.h" 00020 #include "ARMRelocations.h" 00021 #include "ARMSubtarget.h" 00022 #include "ARMTargetMachine.h" 00023 #include "llvm/Constants.h" 00024 #include "llvm/DerivedTypes.h" 00025 #include "llvm/Function.h" 00026 #include "llvm/PassManager.h" 00027 #include "llvm/CodeGen/MachineCodeEmitter.h" 00028 #include "llvm/CodeGen/JITCodeEmitter.h" 00029 #include "llvm/CodeGen/ObjectCodeEmitter.h" 00030 #include "llvm/CodeGen/MachineConstantPool.h" 00031 #include "llvm/CodeGen/MachineFunctionPass.h" 00032 #include "llvm/CodeGen/MachineInstr.h" 00033 #include "llvm/CodeGen/MachineJumpTableInfo.h" 00034 #include "llvm/CodeGen/Passes.h" 00035 #include "llvm/ADT/Statistic.h" 00036 #include "llvm/Support/Compiler.h" 00037 #include "llvm/Support/Debug.h" 00038 #include "llvm/Support/ErrorHandling.h" 00039 #include "llvm/Support/raw_ostream.h" 00040 #ifndef NDEBUG 00041 #include <iomanip> 00042 #endif 00043 using namespace llvm; 00044 00045 STATISTIC(NumEmitted, "Number of machine instructions emitted"); 00046 00047 namespace { 00048 00049 class ARMCodeEmitter { 00050 public: 00051 /// getBinaryCodeForInstr - This function, generated by the 00052 /// CodeEmitterGenerator using TableGen, produces the binary encoding for 00053 /// machine instructions. 00054 unsigned getBinaryCodeForInstr(const MachineInstr &MI); 00055 }; 00056 00057 template<class CodeEmitter> 00058 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass, 00059 public ARMCodeEmitter { 00060 ARMJITInfo *JTI; 00061 const ARMInstrInfo *II; 00062 const TargetData *TD; 00063 const ARMSubtarget *Subtarget; 00064 TargetMachine &TM; 00065 CodeEmitter &MCE; 00066 const std::vector<MachineConstantPoolEntry> *MCPEs; 00067 const std::vector<MachineJumpTableEntry> *MJTEs; 00068 bool IsPIC; 00069 00070 public: 00071 static char ID; 00072 explicit Emitter(TargetMachine &tm, CodeEmitter &mce) 00073 : MachineFunctionPass(&ID), JTI(0), II(0), TD(0), TM(tm), 00074 MCE(mce), MCPEs(0), MJTEs(0), 00075 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {} 00076 Emitter(TargetMachine &tm, CodeEmitter &mce, 00077 const ARMInstrInfo &ii, const TargetData &td) 00078 : MachineFunctionPass(&ID), JTI(0), II(&ii), TD(&td), TM(tm), 00079 MCE(mce), MCPEs(0), MJTEs(0), 00080 IsPIC(TM.getRelocationModel() == Reloc::PIC_) {} 00081 00082 bool runOnMachineFunction(MachineFunction &MF); 00083 00084 virtual const char *getPassName() const { 00085 return "ARM Machine Code Emitter"; 00086 } 00087 00088 void emitInstruction(const MachineInstr &MI); 00089 00090 private: 00091 00092 void emitWordLE(unsigned Binary); 00093 00094 void emitDWordLE(uint64_t Binary); 00095 00096 void emitConstPoolInstruction(const MachineInstr &MI); 00097 00098 void emitMOVi2piecesInstruction(const MachineInstr &MI); 00099 00100 void emitLEApcrelJTInstruction(const MachineInstr &MI); 00101 00102 void emitPseudoMoveInstruction(const MachineInstr &MI); 00103 00104 void addPCLabel(unsigned LabelID); 00105 00106 void emitPseudoInstruction(const MachineInstr &MI); 00107 00108 unsigned getMachineSoRegOpValue(const MachineInstr &MI, 00109 const TargetInstrDesc &TID, 00110 const MachineOperand &MO, 00111 unsigned OpIdx); 00112 00113 unsigned getMachineSoImmOpValue(unsigned SoImm); 00114 00115 unsigned getAddrModeSBit(const MachineInstr &MI, 00116 const TargetInstrDesc &TID) const; 00117 00118 void emitDataProcessingInstruction(const MachineInstr &MI, 00119 unsigned ImplicitRd = 0, 00120 unsigned ImplicitRn = 0); 00121 00122 void emitLoadStoreInstruction(const MachineInstr &MI, 00123 unsigned ImplicitRd = 0, 00124 unsigned ImplicitRn = 0); 00125 00126 void emitMiscLoadStoreInstruction(const MachineInstr &MI, 00127 unsigned ImplicitRn = 0); 00128 00129 void emitLoadStoreMultipleInstruction(const MachineInstr &MI); 00130 00131 void emitMulFrmInstruction(const MachineInstr &MI); 00132 00133 void emitExtendInstruction(const MachineInstr &MI); 00134 00135 void emitMiscArithInstruction(const MachineInstr &MI); 00136 00137 void emitBranchInstruction(const MachineInstr &MI); 00138 00139 void emitInlineJumpTable(unsigned JTIndex); 00140 00141 void emitMiscBranchInstruction(const MachineInstr &MI); 00142 00143 void emitVFPArithInstruction(const MachineInstr &MI); 00144 00145 void emitVFPConversionInstruction(const MachineInstr &MI); 00146 00147 void emitVFPLoadStoreInstruction(const MachineInstr &MI); 00148 00149 void emitVFPLoadStoreMultipleInstruction(const MachineInstr &MI); 00150 00151 void emitMiscInstruction(const MachineInstr &MI); 00152 00153 /// getMachineOpValue - Return binary encoding of operand. If the machine 00154 /// operand requires relocation, record the relocation and return zero. 00155 unsigned getMachineOpValue(const MachineInstr &MI,const MachineOperand &MO); 00156 unsigned getMachineOpValue(const MachineInstr &MI, unsigned OpIdx) { 00157 return getMachineOpValue(MI, MI.getOperand(OpIdx)); 00158 } 00159 00160 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. 00161 /// 00162 unsigned getShiftOp(unsigned Imm) const ; 00163 00164 /// Routines that handle operands which add machine relocations which are 00165 /// fixed up by the relocation stage. 00166 void emitGlobalAddress(GlobalValue *GV, unsigned Reloc, 00167 bool NeedStub, bool Indirect, intptr_t ACPV = 0); 00168 void emitExternalSymbolAddress(const char *ES, unsigned Reloc); 00169 void emitConstPoolAddress(unsigned CPI, unsigned Reloc); 00170 void emitJumpTableAddress(unsigned JTIndex, unsigned Reloc); 00171 void emitMachineBasicBlock(MachineBasicBlock *BB, unsigned Reloc, 00172 intptr_t JTBase = 0); 00173 }; 00174 template <class CodeEmitter> 00175 char Emitter<CodeEmitter>::ID = 0; 00176 } 00177 00178 /// createARMCodeEmitterPass - Return a pass that emits the collected ARM code 00179 /// to the specified MCE object. 00180 00181 FunctionPass *llvm::createARMCodeEmitterPass(ARMBaseTargetMachine &TM, 00182 MachineCodeEmitter &MCE) { 00183 return new Emitter<MachineCodeEmitter>(TM, MCE); 00184 } 00185 FunctionPass *llvm::createARMJITCodeEmitterPass(ARMBaseTargetMachine &TM, 00186 JITCodeEmitter &JCE) { 00187 return new Emitter<JITCodeEmitter>(TM, JCE); 00188 } 00189 FunctionPass *llvm::createARMObjectCodeEmitterPass(ARMBaseTargetMachine &TM, 00190 ObjectCodeEmitter &OCE) { 00191 return new Emitter<ObjectCodeEmitter>(TM, OCE); 00192 } 00193 00194 template<class CodeEmitter> 00195 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) { 00196 assert((MF.getTarget().getRelocationModel() != Reloc::Default || 00197 MF.getTarget().getRelocationModel() != Reloc::Static) && 00198 "JIT relocation model must be set to static or default!"); 00199 JTI = ((ARMTargetMachine&)MF.getTarget()).getJITInfo(); 00200 II = ((ARMTargetMachine&)MF.getTarget()).getInstrInfo(); 00201 TD = ((ARMTargetMachine&)MF.getTarget()).getTargetData(); 00202 Subtarget = &TM.getSubtarget<ARMSubtarget>(); 00203 MCPEs = &MF.getConstantPool()->getConstants(); 00204 MJTEs = &MF.getJumpTableInfo()->getJumpTables(); 00205 IsPIC = TM.getRelocationModel() == Reloc::PIC_; 00206 JTI->Initialize(MF, IsPIC); 00207 00208 do { 00209 DEBUG(errs() << "JITTing function '" 00210 << MF.getFunction()->getName() << "'\n"); 00211 MCE.startFunction(MF); 00212 for (MachineFunction::iterator MBB = MF.begin(), E = MF.end(); 00213 MBB != E; ++MBB) { 00214 MCE.StartMachineBasicBlock(MBB); 00215 for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end(); 00216 I != E; ++I) 00217 emitInstruction(*I); 00218 } 00219 } while (MCE.finishFunction(MF)); 00220 00221 return false; 00222 } 00223 00224 /// getShiftOp - Return the shift opcode (bit[6:5]) of the immediate value. 00225 /// 00226 template<class CodeEmitter> 00227 unsigned Emitter<CodeEmitter>::getShiftOp(unsigned Imm) const { 00228 switch (ARM_AM::getAM2ShiftOpc(Imm)) { 00229 default: llvm_unreachable("Unknown shift opc!"); 00230 case ARM_AM::asr: return 2; 00231 case ARM_AM::lsl: return 0; 00232 case ARM_AM::lsr: return 1; 00233 case ARM_AM::ror: 00234 case ARM_AM::rrx: return 3; 00235 } 00236 return 0; 00237 } 00238 00239 /// getMachineOpValue - Return binary encoding of operand. If the machine 00240 /// operand requires relocation, record the relocation and return zero. 00241 template<class CodeEmitter> 00242 unsigned Emitter<CodeEmitter>::getMachineOpValue(const MachineInstr &MI, 00243 const MachineOperand &MO) { 00244 if (MO.isReg()) 00245 return ARMRegisterInfo::getRegisterNumbering(MO.getReg()); 00246 else if (MO.isImm()) 00247 return static_cast<unsigned>(MO.getImm()); 00248 else if (MO.isGlobal()) 00249 emitGlobalAddress(MO.getGlobal(), ARM::reloc_arm_branch, true, false); 00250 else if (MO.isSymbol()) 00251 emitExternalSymbolAddress(MO.getSymbolName(), ARM::reloc_arm_branch); 00252 else if (MO.isCPI()) { 00253 const TargetInstrDesc &TID = MI.getDesc(); 00254 // For VFP load, the immediate offset is multiplied by 4. 00255 unsigned Reloc = ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPLdStFrm) 00256 ? ARM::reloc_arm_vfp_cp_entry : ARM::reloc_arm_cp_entry; 00257 emitConstPoolAddress(MO.getIndex(), Reloc); 00258 } else if (MO.isJTI()) 00259 emitJumpTableAddress(MO.getIndex(), ARM::reloc_arm_relative); 00260 else if (MO.isMBB()) 00261 emitMachineBasicBlock(MO.getMBB(), ARM::reloc_arm_branch); 00262 else { 00263 #ifndef NDEBUG 00264 cerr << MO; 00265 #endif 00266 llvm_unreachable(0); 00267 } 00268 return 0; 00269 } 00270 00271 /// emitGlobalAddress - Emit the specified address to the code stream. 00272 /// 00273 template<class CodeEmitter> 00274 void Emitter<CodeEmitter>::emitGlobalAddress(GlobalValue *GV, unsigned Reloc, 00275 bool NeedStub, bool Indirect, 00276 intptr_t ACPV) { 00277 MachineRelocation MR = Indirect 00278 ? MachineRelocation::getIndirectSymbol(MCE.getCurrentPCOffset(), Reloc, 00279 GV, ACPV, NeedStub) 00280 : MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 00281 GV, ACPV, NeedStub); 00282 MCE.addRelocation(MR); 00283 } 00284 00285 /// emitExternalSymbolAddress - Arrange for the address of an external symbol to 00286 /// be emitted to the current location in the function, and allow it to be PC 00287 /// relative. 00288 template<class CodeEmitter> 00289 void Emitter<CodeEmitter>::emitExternalSymbolAddress(const char *ES, 00290 unsigned Reloc) { 00291 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 00292 Reloc, ES)); 00293 } 00294 00295 /// emitConstPoolAddress - Arrange for the address of an constant pool 00296 /// to be emitted to the current location in the function, and allow it to be PC 00297 /// relative. 00298 template<class CodeEmitter> 00299 void Emitter<CodeEmitter>::emitConstPoolAddress(unsigned CPI, 00300 unsigned Reloc) { 00301 // Tell JIT emitter we'll resolve the address. 00302 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 00303 Reloc, CPI, 0, true)); 00304 } 00305 00306 /// emitJumpTableAddress - Arrange for the address of a jump table to 00307 /// be emitted to the current location in the function, and allow it to be PC 00308 /// relative. 00309 template<class CodeEmitter> 00310 void Emitter<CodeEmitter>::emitJumpTableAddress(unsigned JTIndex, 00311 unsigned Reloc) { 00312 MCE.addRelocation(MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 00313 Reloc, JTIndex, 0, true)); 00314 } 00315 00316 /// emitMachineBasicBlock - Emit the specified address basic block. 00317 template<class CodeEmitter> 00318 void Emitter<CodeEmitter>::emitMachineBasicBlock(MachineBasicBlock *BB, 00319 unsigned Reloc, intptr_t JTBase) { 00320 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 00321 Reloc, BB, JTBase)); 00322 } 00323 00324 template<class CodeEmitter> 00325 void Emitter<CodeEmitter>::emitWordLE(unsigned Binary) { 00326 #ifndef NDEBUG 00327 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0') 00328 << Binary << std::dec << "\n"; 00329 #endif 00330 MCE.emitWordLE(Binary); 00331 } 00332 00333 template<class CodeEmitter> 00334 void Emitter<CodeEmitter>::emitDWordLE(uint64_t Binary) { 00335 #ifndef NDEBUG 00336 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0') 00337 << (unsigned)Binary << std::dec << "\n"; 00338 DOUT << " 0x" << std::hex << std::setw(8) << std::setfill('0') 00339 << (unsigned)(Binary >> 32) << std::dec << "\n"; 00340 #endif 00341 MCE.emitDWordLE(Binary); 00342 } 00343 00344 template<class CodeEmitter> 00345 void Emitter<CodeEmitter>::emitInstruction(const MachineInstr &MI) { 00346 DOUT << "JIT: " << (void*)MCE.getCurrentPCValue() << ":\t" << MI; 00347 00348 MCE.processDebugLoc(MI.getDebugLoc()); 00349 00350 NumEmitted++; // Keep track of the # of mi's emitted 00351 switch (MI.getDesc().TSFlags & ARMII::FormMask) { 00352 default: { 00353 llvm_unreachable("Unhandled instruction encoding format!"); 00354 break; 00355 } 00356 case ARMII::Pseudo: 00357 emitPseudoInstruction(MI); 00358 break; 00359 case ARMII::DPFrm: 00360 case ARMII::DPSoRegFrm: 00361 emitDataProcessingInstruction(MI); 00362 break; 00363 case ARMII::LdFrm: 00364 case ARMII::StFrm: 00365 emitLoadStoreInstruction(MI); 00366 break; 00367 case ARMII::LdMiscFrm: 00368 case ARMII::StMiscFrm: 00369 emitMiscLoadStoreInstruction(MI); 00370 break; 00371 case ARMII::LdStMulFrm: 00372 emitLoadStoreMultipleInstruction(MI); 00373 break; 00374 case ARMII::MulFrm: 00375 emitMulFrmInstruction(MI); 00376 break; 00377 case ARMII::ExtFrm: 00378 emitExtendInstruction(MI); 00379 break; 00380 case ARMII::ArithMiscFrm: 00381 emitMiscArithInstruction(MI); 00382 break; 00383 case ARMII::BrFrm: 00384 emitBranchInstruction(MI); 00385 break; 00386 case ARMII::BrMiscFrm: 00387 emitMiscBranchInstruction(MI); 00388 break; 00389 // VFP instructions. 00390 case ARMII::VFPUnaryFrm: 00391 case ARMII::VFPBinaryFrm: 00392 emitVFPArithInstruction(MI); 00393 break; 00394 case ARMII::VFPConv1Frm: 00395 case ARMII::VFPConv2Frm: 00396 case ARMII::VFPConv3Frm: 00397 case ARMII::VFPConv4Frm: 00398 case ARMII::VFPConv5Frm: 00399 emitVFPConversionInstruction(MI); 00400 break; 00401 case ARMII::VFPLdStFrm: 00402 emitVFPLoadStoreInstruction(MI); 00403 break; 00404 case ARMII::VFPLdStMulFrm: 00405 emitVFPLoadStoreMultipleInstruction(MI); 00406 break; 00407 case ARMII::VFPMiscFrm: 00408 emitMiscInstruction(MI); 00409 break; 00410 } 00411 } 00412 00413 template<class CodeEmitter> 00414 void Emitter<CodeEmitter>::emitConstPoolInstruction(const MachineInstr &MI) { 00415 unsigned CPI = MI.getOperand(0).getImm(); // CP instruction index. 00416 unsigned CPIndex = MI.getOperand(1).getIndex(); // Actual cp entry index. 00417 const MachineConstantPoolEntry &MCPE = (*MCPEs)[CPIndex]; 00418 00419 // Remember the CONSTPOOL_ENTRY address for later relocation. 00420 JTI->addConstantPoolEntryAddr(CPI, MCE.getCurrentPCValue()); 00421 00422 // Emit constpool island entry. In most cases, the actual values will be 00423 // resolved and relocated after code emission. 00424 if (MCPE.isMachineConstantPoolEntry()) { 00425 ARMConstantPoolValue *ACPV = 00426 static_cast<ARMConstantPoolValue*>(MCPE.Val.MachineCPVal); 00427 00428 DOUT << " ** ARM constant pool #" << CPI << " @ " 00429 << (void*)MCE.getCurrentPCValue() << " " << *ACPV << '\n'; 00430 00431 GlobalValue *GV = ACPV->getGV(); 00432 if (GV) { 00433 Reloc::Model RelocM = TM.getRelocationModel(); 00434 emitGlobalAddress(GV, ARM::reloc_arm_machine_cp_entry, 00435 isa<Function>(GV), 00436 Subtarget->GVIsIndirectSymbol(GV, RelocM), 00437 (intptr_t)ACPV); 00438 } else { 00439 emitExternalSymbolAddress(ACPV->getSymbol(), ARM::reloc_arm_absolute); 00440 } 00441 emitWordLE(0); 00442 } else { 00443 Constant *CV = MCPE.Val.ConstVal; 00444 00445 DEBUG({ 00446 errs() << " ** Constant pool #" << CPI << " @ " 00447 << (void*)MCE.getCurrentPCValue() << " "; 00448 if (const Function *F = dyn_cast<Function>(CV)) 00449 errs() << F->getName(); 00450 else 00451 errs() << *CV; 00452 errs() << '\n'; 00453 }); 00454 00455 if (GlobalValue *GV = dyn_cast<GlobalValue>(CV)) { 00456 emitGlobalAddress(GV, ARM::reloc_arm_absolute, isa<Function>(GV), false); 00457 emitWordLE(0); 00458 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 00459 uint32_t Val = *(uint32_t*)CI->getValue().getRawData(); 00460 emitWordLE(Val); 00461 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 00462 if (CFP->getType() == Type::getFloatTy(CFP->getContext())) 00463 emitWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 00464 else if (CFP->getType() == Type::getDoubleTy(CFP->getContext())) 00465 emitDWordLE(CFP->getValueAPF().bitcastToAPInt().getZExtValue()); 00466 else { 00467 llvm_unreachable("Unable to handle this constantpool entry!"); 00468 } 00469 } else { 00470 llvm_unreachable("Unable to handle this constantpool entry!"); 00471 } 00472 } 00473 } 00474 00475 template<class CodeEmitter> 00476 void Emitter<CodeEmitter>::emitMOVi2piecesInstruction(const MachineInstr &MI) { 00477 const MachineOperand &MO0 = MI.getOperand(0); 00478 const MachineOperand &MO1 = MI.getOperand(1); 00479 assert(MO1.isImm() && ARM_AM::getSOImmVal(MO1.isImm()) != -1 && 00480 "Not a valid so_imm value!"); 00481 unsigned V1 = ARM_AM::getSOImmTwoPartFirst(MO1.getImm()); 00482 unsigned V2 = ARM_AM::getSOImmTwoPartSecond(MO1.getImm()); 00483 00484 // Emit the 'mov' instruction. 00485 unsigned Binary = 0xd << 21; // mov: Insts{24-21} = 0b1101 00486 00487 // Set the conditional execution predicate. 00488 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00489 00490 // Encode Rd. 00491 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; 00492 00493 // Encode so_imm. 00494 // Set bit I(25) to identify this is the immediate form of <shifter_op> 00495 Binary |= 1 << ARMII::I_BitShift; 00496 Binary |= getMachineSoImmOpValue(V1); 00497 emitWordLE(Binary); 00498 00499 // Now the 'orr' instruction. 00500 Binary = 0xc << 21; // orr: Insts{24-21} = 0b1100 00501 00502 // Set the conditional execution predicate. 00503 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00504 00505 // Encode Rd. 00506 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRdShift; 00507 00508 // Encode Rn. 00509 Binary |= getMachineOpValue(MI, MO0) << ARMII::RegRnShift; 00510 00511 // Encode so_imm. 00512 // Set bit I(25) to identify this is the immediate form of <shifter_op> 00513 Binary |= 1 << ARMII::I_BitShift; 00514 Binary |= getMachineSoImmOpValue(V2); 00515 emitWordLE(Binary); 00516 } 00517 00518 template<class CodeEmitter> 00519 void Emitter<CodeEmitter>::emitLEApcrelJTInstruction(const MachineInstr &MI) { 00520 // It's basically add r, pc, (LJTI - $+8) 00521 00522 const TargetInstrDesc &TID = MI.getDesc(); 00523 00524 // Emit the 'add' instruction. 00525 unsigned Binary = 0x4 << 21; // add: Insts{24-31} = 0b0100 00526 00527 // Set the conditional execution predicate 00528 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00529 00530 // Encode S bit if MI modifies CPSR. 00531 Binary |= getAddrModeSBit(MI, TID); 00532 00533 // Encode Rd. 00534 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; 00535 00536 // Encode Rn which is PC. 00537 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift; 00538 00539 // Encode the displacement. 00540 Binary |= 1 << ARMII::I_BitShift; 00541 emitJumpTableAddress(MI.getOperand(1).getIndex(), ARM::reloc_arm_jt_base); 00542 00543 emitWordLE(Binary); 00544 } 00545 00546 template<class CodeEmitter> 00547 void Emitter<CodeEmitter>::emitPseudoMoveInstruction(const MachineInstr &MI) { 00548 unsigned Opcode = MI.getDesc().Opcode; 00549 00550 // Part of binary is determined by TableGn. 00551 unsigned Binary = getBinaryCodeForInstr(MI); 00552 00553 // Set the conditional execution predicate 00554 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00555 00556 // Encode S bit if MI modifies CPSR. 00557 if (Opcode == ARM::MOVsrl_flag || Opcode == ARM::MOVsra_flag) 00558 Binary |= 1 << ARMII::S_BitShift; 00559 00560 // Encode register def if there is one. 00561 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRdShift; 00562 00563 // Encode the shift operation. 00564 switch (Opcode) { 00565 default: break; 00566 case ARM::MOVrx: 00567 // rrx 00568 Binary |= 0x6 << 4; 00569 break; 00570 case ARM::MOVsrl_flag: 00571 // lsr #1 00572 Binary |= (0x2 << 4) | (1 << 7); 00573 break; 00574 case ARM::MOVsra_flag: 00575 // asr #1 00576 Binary |= (0x4 << 4) | (1 << 7); 00577 break; 00578 } 00579 00580 // Encode register Rm. 00581 Binary |= getMachineOpValue(MI, 1); 00582 00583 emitWordLE(Binary); 00584 } 00585 00586 template<class CodeEmitter> 00587 void Emitter<CodeEmitter>::addPCLabel(unsigned LabelID) { 00588 DOUT << " ** LPC" << LabelID << " @ " 00589 << (void*)MCE.getCurrentPCValue() << '\n'; 00590 JTI->addPCLabelAddr(LabelID, MCE.getCurrentPCValue()); 00591 } 00592 00593 template<class CodeEmitter> 00594 void Emitter<CodeEmitter>::emitPseudoInstruction(const MachineInstr &MI) { 00595 unsigned Opcode = MI.getDesc().Opcode; 00596 switch (Opcode) { 00597 default: 00598 llvm_unreachable("ARMCodeEmitter::emitPseudoInstruction");//FIXME: 00599 case TargetInstrInfo::INLINEASM: { 00600 // We allow inline assembler nodes with empty bodies - they can 00601 // implicitly define registers, which is ok for JIT. 00602 if (MI.getOperand(0).getSymbolName()[0]) { 00603 llvm_report_error("JIT does not support inline asm!"); 00604 } 00605 break; 00606 } 00607 case TargetInstrInfo::DBG_LABEL: 00608 case TargetInstrInfo::EH_LABEL: 00609 MCE.emitLabel(MI.getOperand(0).getImm()); 00610 break; 00611 case TargetInstrInfo::IMPLICIT_DEF: 00612 case TargetInstrInfo::DECLARE: 00613 case ARM::DWARF_LOC: 00614 // Do nothing. 00615 break; 00616 case ARM::CONSTPOOL_ENTRY: 00617 emitConstPoolInstruction(MI); 00618 break; 00619 case ARM::PICADD: { 00620 // Remember of the address of the PC label for relocation later. 00621 addPCLabel(MI.getOperand(2).getImm()); 00622 // PICADD is just an add instruction that implicitly read pc. 00623 emitDataProcessingInstruction(MI, 0, ARM::PC); 00624 break; 00625 } 00626 case ARM::PICLDR: 00627 case ARM::PICLDRB: 00628 case ARM::PICSTR: 00629 case ARM::PICSTRB: { 00630 // Remember of the address of the PC label for relocation later. 00631 addPCLabel(MI.getOperand(2).getImm()); 00632 // These are just load / store instructions that implicitly read pc. 00633 emitLoadStoreInstruction(MI, 0, ARM::PC); 00634 break; 00635 } 00636 case ARM::PICLDRH: 00637 case ARM::PICLDRSH: 00638 case ARM::PICLDRSB: 00639 case ARM::PICSTRH: { 00640 // Remember of the address of the PC label for relocation later. 00641 addPCLabel(MI.getOperand(2).getImm()); 00642 // These are just load / store instructions that implicitly read pc. 00643 emitMiscLoadStoreInstruction(MI, ARM::PC); 00644 break; 00645 } 00646 case ARM::MOVi2pieces: 00647 // Two instructions to materialize a constant. 00648 emitMOVi2piecesInstruction(MI); 00649 break; 00650 case ARM::LEApcrelJT: 00651 // Materialize jumptable address. 00652 emitLEApcrelJTInstruction(MI); 00653 break; 00654 case ARM::MOVrx: 00655 case ARM::MOVsrl_flag: 00656 case ARM::MOVsra_flag: 00657 emitPseudoMoveInstruction(MI); 00658 break; 00659 } 00660 } 00661 00662 template<class CodeEmitter> 00663 unsigned Emitter<CodeEmitter>::getMachineSoRegOpValue( 00664 const MachineInstr &MI, 00665 const TargetInstrDesc &TID, 00666 const MachineOperand &MO, 00667 unsigned OpIdx) { 00668 unsigned Binary = getMachineOpValue(MI, MO); 00669 00670 const MachineOperand &MO1 = MI.getOperand(OpIdx + 1); 00671 const MachineOperand &MO2 = MI.getOperand(OpIdx + 2); 00672 ARM_AM::ShiftOpc SOpc = ARM_AM::getSORegShOp(MO2.getImm()); 00673 00674 // Encode the shift opcode. 00675 unsigned SBits = 0; 00676 unsigned Rs = MO1.getReg(); 00677 if (Rs) { 00678 // Set shift operand (bit[7:4]). 00679 // LSL - 0001 00680 // LSR - 0011 00681 // ASR - 0101 00682 // ROR - 0111 00683 // RRX - 0110 and bit[11:8] clear. 00684 switch (SOpc) { 00685 default: llvm_unreachable("Unknown shift opc!"); 00686 case ARM_AM::lsl: SBits = 0x1; break; 00687 case ARM_AM::lsr: SBits = 0x3; break; 00688 case ARM_AM::asr: SBits = 0x5; break; 00689 case ARM_AM::ror: SBits = 0x7; break; 00690 case ARM_AM::rrx: SBits = 0x6; break; 00691 } 00692 } else { 00693 // Set shift operand (bit[6:4]). 00694 // LSL - 000 00695 // LSR - 010 00696 // ASR - 100 00697 // ROR - 110 00698 switch (SOpc) { 00699 default: llvm_unreachable("Unknown shift opc!"); 00700 case ARM_AM::lsl: SBits = 0x0; break; 00701 case ARM_AM::lsr: SBits = 0x2; break; 00702 case ARM_AM::asr: SBits = 0x4; break; 00703 case ARM_AM::ror: SBits = 0x6; break; 00704 } 00705 } 00706 Binary |= SBits << 4; 00707 if (SOpc == ARM_AM::rrx) 00708 return Binary; 00709 00710 // Encode the shift operation Rs or shift_imm (except rrx). 00711 if (Rs) { 00712 // Encode Rs bit[11:8]. 00713 assert(ARM_AM::getSORegOffset(MO2.getImm()) == 0); 00714 return Binary | 00715 (ARMRegisterInfo::getRegisterNumbering(Rs) << ARMII::RegRsShift); 00716 } 00717 00718 // Encode shift_imm bit[11:7]. 00719 return Binary | ARM_AM::getSORegOffset(MO2.getImm()) << 7; 00720 } 00721 00722 template<class CodeEmitter> 00723 unsigned Emitter<CodeEmitter>::getMachineSoImmOpValue(unsigned SoImm) { 00724 int SoImmVal = ARM_AM::getSOImmVal(SoImm); 00725 assert(SoImmVal != -1 && "Not a valid so_imm value!"); 00726 00727 // Encode rotate_imm. 00728 unsigned Binary = (ARM_AM::getSOImmValRot((unsigned)SoImmVal) >> 1) 00729 << ARMII::SoRotImmShift; 00730 00731 // Encode immed_8. 00732 Binary |= ARM_AM::getSOImmValImm((unsigned)SoImmVal); 00733 return Binary; 00734 } 00735 00736 template<class CodeEmitter> 00737 unsigned Emitter<CodeEmitter>::getAddrModeSBit(const MachineInstr &MI, 00738 const TargetInstrDesc &TID) const { 00739 for (unsigned i = MI.getNumOperands(), e = TID.getNumOperands(); i != e; --i){ 00740 const MachineOperand &MO = MI.getOperand(i-1); 00741 if (MO.isReg() && MO.isDef() && MO.getReg() == ARM::CPSR) 00742 return 1 << ARMII::S_BitShift; 00743 } 00744 return 0; 00745 } 00746 00747 template<class CodeEmitter> 00748 void Emitter<CodeEmitter>::emitDataProcessingInstruction( 00749 const MachineInstr &MI, 00750 unsigned ImplicitRd, 00751 unsigned ImplicitRn) { 00752 const TargetInstrDesc &TID = MI.getDesc(); 00753 00754 if (TID.Opcode == ARM::BFC) { 00755 llvm_report_error("ARMv6t2 JIT is not yet supported."); 00756 } 00757 00758 // Part of binary is determined by TableGn. 00759 unsigned Binary = getBinaryCodeForInstr(MI); 00760 00761 // Set the conditional execution predicate 00762 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00763 00764 // Encode S bit if MI modifies CPSR. 00765 Binary |= getAddrModeSBit(MI, TID); 00766 00767 // Encode register def if there is one. 00768 unsigned NumDefs = TID.getNumDefs(); 00769 unsigned OpIdx = 0; 00770 if (NumDefs) 00771 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 00772 else if (ImplicitRd) 00773 // Special handling for implicit use (e.g. PC). 00774 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd) 00775 << ARMII::RegRdShift); 00776 00777 // If this is a two-address operand, skip it. e.g. MOVCCr operand 1. 00778 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) 00779 ++OpIdx; 00780 00781 // Encode first non-shifter register operand if there is one. 00782 bool isUnary = TID.TSFlags & ARMII::UnaryDP; 00783 if (!isUnary) { 00784 if (ImplicitRn) 00785 // Special handling for implicit use (e.g. PC). 00786 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn) 00787 << ARMII::RegRnShift); 00788 else { 00789 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRnShift; 00790 ++OpIdx; 00791 } 00792 } 00793 00794 // Encode shifter operand. 00795 const MachineOperand &MO = MI.getOperand(OpIdx); 00796 if ((TID.TSFlags & ARMII::FormMask) == ARMII::DPSoRegFrm) { 00797 // Encode SoReg. 00798 emitWordLE(Binary | getMachineSoRegOpValue(MI, TID, MO, OpIdx)); 00799 return; 00800 } 00801 00802 if (MO.isReg()) { 00803 // Encode register Rm. 00804 emitWordLE(Binary | ARMRegisterInfo::getRegisterNumbering(MO.getReg())); 00805 return; 00806 } 00807 00808 // Encode so_imm. 00809 Binary |= getMachineSoImmOpValue((unsigned)MO.getImm()); 00810 00811 emitWordLE(Binary); 00812 } 00813 00814 template<class CodeEmitter> 00815 void Emitter<CodeEmitter>::emitLoadStoreInstruction( 00816 const MachineInstr &MI, 00817 unsigned ImplicitRd, 00818 unsigned ImplicitRn) { 00819 const TargetInstrDesc &TID = MI.getDesc(); 00820 unsigned Form = TID.TSFlags & ARMII::FormMask; 00821 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0; 00822 00823 // Part of binary is determined by TableGn. 00824 unsigned Binary = getBinaryCodeForInstr(MI); 00825 00826 // Set the conditional execution predicate 00827 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00828 00829 unsigned OpIdx = 0; 00830 00831 // Operand 0 of a pre- and post-indexed store is the address base 00832 // writeback. Skip it. 00833 bool Skipped = false; 00834 if (IsPrePost && Form == ARMII::StFrm) { 00835 ++OpIdx; 00836 Skipped = true; 00837 } 00838 00839 // Set first operand 00840 if (ImplicitRd) 00841 // Special handling for implicit use (e.g. PC). 00842 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRd) 00843 << ARMII::RegRdShift); 00844 else 00845 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 00846 00847 // Set second operand 00848 if (ImplicitRn) 00849 // Special handling for implicit use (e.g. PC). 00850 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn) 00851 << ARMII::RegRnShift); 00852 else 00853 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; 00854 00855 // If this is a two-address operand, skip it. e.g. LDR_PRE. 00856 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) 00857 ++OpIdx; 00858 00859 const MachineOperand &MO2 = MI.getOperand(OpIdx); 00860 unsigned AM2Opc = (ImplicitRn == ARM::PC) 00861 ? 0 : MI.getOperand(OpIdx+1).getImm(); 00862 00863 // Set bit U(23) according to sign of immed value (positive or negative). 00864 Binary |= ((ARM_AM::getAM2Op(AM2Opc) == ARM_AM::add ? 1 : 0) << 00865 ARMII::U_BitShift); 00866 if (!MO2.getReg()) { // is immediate 00867 if (ARM_AM::getAM2Offset(AM2Opc)) 00868 // Set the value of offset_12 field 00869 Binary |= ARM_AM::getAM2Offset(AM2Opc); 00870 emitWordLE(Binary); 00871 return; 00872 } 00873 00874 // Set bit I(25), because this is not in immediate enconding. 00875 Binary |= 1 << ARMII::I_BitShift; 00876 assert(TargetRegisterInfo::isPhysicalRegister(MO2.getReg())); 00877 // Set bit[3:0] to the corresponding Rm register 00878 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg()); 00879 00880 // If this instr is in scaled register offset/index instruction, set 00881 // shift_immed(bit[11:7]) and shift(bit[6:5]) fields. 00882 if (unsigned ShImm = ARM_AM::getAM2Offset(AM2Opc)) { 00883 Binary |= getShiftOp(AM2Opc) << ARMII::ShiftImmShift; // shift 00884 Binary |= ShImm << ARMII::ShiftShift; // shift_immed 00885 } 00886 00887 emitWordLE(Binary); 00888 } 00889 00890 template<class CodeEmitter> 00891 void Emitter<CodeEmitter>::emitMiscLoadStoreInstruction(const MachineInstr &MI, 00892 unsigned ImplicitRn) { 00893 const TargetInstrDesc &TID = MI.getDesc(); 00894 unsigned Form = TID.TSFlags & ARMII::FormMask; 00895 bool IsPrePost = (TID.TSFlags & ARMII::IndexModeMask) != 0; 00896 00897 // Part of binary is determined by TableGn. 00898 unsigned Binary = getBinaryCodeForInstr(MI); 00899 00900 // Set the conditional execution predicate 00901 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00902 00903 unsigned OpIdx = 0; 00904 00905 // Operand 0 of a pre- and post-indexed store is the address base 00906 // writeback. Skip it. 00907 bool Skipped = false; 00908 if (IsPrePost && Form == ARMII::StMiscFrm) { 00909 ++OpIdx; 00910 Skipped = true; 00911 } 00912 00913 // Set first operand 00914 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 00915 00916 // Skip LDRD and STRD's second operand. 00917 if (TID.Opcode == ARM::LDRD || TID.Opcode == ARM::STRD) 00918 ++OpIdx; 00919 00920 // Set second operand 00921 if (ImplicitRn) 00922 // Special handling for implicit use (e.g. PC). 00923 Binary |= (ARMRegisterInfo::getRegisterNumbering(ImplicitRn) 00924 << ARMII::RegRnShift); 00925 else 00926 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRnShift; 00927 00928 // If this is a two-address operand, skip it. e.g. LDRH_POST. 00929 if (!Skipped && TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) 00930 ++OpIdx; 00931 00932 const MachineOperand &MO2 = MI.getOperand(OpIdx); 00933 unsigned AM3Opc = (ImplicitRn == ARM::PC) 00934 ? 0 : MI.getOperand(OpIdx+1).getImm(); 00935 00936 // Set bit U(23) according to sign of immed value (positive or negative) 00937 Binary |= ((ARM_AM::getAM3Op(AM3Opc) == ARM_AM::add ? 1 : 0) << 00938 ARMII::U_BitShift); 00939 00940 // If this instr is in register offset/index encoding, set bit[3:0] 00941 // to the corresponding Rm register. 00942 if (MO2.getReg()) { 00943 Binary |= ARMRegisterInfo::getRegisterNumbering(MO2.getReg()); 00944 emitWordLE(Binary); 00945 return; 00946 } 00947 00948 // This instr is in immediate offset/index encoding, set bit 22 to 1. 00949 Binary |= 1 << ARMII::AM3_I_BitShift; 00950 if (unsigned ImmOffs = ARM_AM::getAM3Offset(AM3Opc)) { 00951 // Set operands 00952 Binary |= (ImmOffs >> 4) << ARMII::ImmHiShift; // immedH 00953 Binary |= (ImmOffs & 0xF); // immedL 00954 } 00955 00956 emitWordLE(Binary); 00957 } 00958 00959 static unsigned getAddrModeUPBits(unsigned Mode) { 00960 unsigned Binary = 0; 00961 00962 // Set addressing mode by modifying bits U(23) and P(24) 00963 // IA - Increment after - bit U = 1 and bit P = 0 00964 // IB - Increment before - bit U = 1 and bit P = 1 00965 // DA - Decrement after - bit U = 0 and bit P = 0 00966 // DB - Decrement before - bit U = 0 and bit P = 1 00967 switch (Mode) { 00968 default: llvm_unreachable("Unknown addressing sub-mode!"); 00969 case ARM_AM::da: break; 00970 case ARM_AM::db: Binary |= 0x1 << ARMII::P_BitShift; break; 00971 case ARM_AM::ia: Binary |= 0x1 << ARMII::U_BitShift; break; 00972 case ARM_AM::ib: Binary |= 0x3 << ARMII::U_BitShift; break; 00973 } 00974 00975 return Binary; 00976 } 00977 00978 template<class CodeEmitter> 00979 void Emitter<CodeEmitter>::emitLoadStoreMultipleInstruction( 00980 const MachineInstr &MI) { 00981 // Part of binary is determined by TableGn. 00982 unsigned Binary = getBinaryCodeForInstr(MI); 00983 00984 // Set the conditional execution predicate 00985 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 00986 00987 // Set base address operand 00988 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift; 00989 00990 // Set addressing mode by modifying bits U(23) and P(24) 00991 const MachineOperand &MO = MI.getOperand(1); 00992 Binary |= getAddrModeUPBits(ARM_AM::getAM4SubMode(MO.getImm())); 00993 00994 // Set bit W(21) 00995 if (ARM_AM::getAM4WBFlag(MO.getImm())) 00996 Binary |= 0x1 << ARMII::W_BitShift; 00997 00998 // Set registers 00999 for (unsigned i = 4, e = MI.getNumOperands(); i != e; ++i) { 01000 const MachineOperand &MO = MI.getOperand(i); 01001 if (!MO.isReg() || MO.isImplicit()) 01002 break; 01003 unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(MO.getReg()); 01004 assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) && 01005 RegNum < 16); 01006 Binary |= 0x1 << RegNum; 01007 } 01008 01009 emitWordLE(Binary); 01010 } 01011 01012 template<class CodeEmitter> 01013 void Emitter<CodeEmitter>::emitMulFrmInstruction(const MachineInstr &MI) { 01014 const TargetInstrDesc &TID = MI.getDesc(); 01015 01016 // Part of binary is determined by TableGn. 01017 unsigned Binary = getBinaryCodeForInstr(MI); 01018 01019 // Set the conditional execution predicate 01020 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01021 01022 // Encode S bit if MI modifies CPSR. 01023 Binary |= getAddrModeSBit(MI, TID); 01024 01025 // 32x32->64bit operations have two destination registers. The number 01026 // of register definitions will tell us if that's what we're dealing with. 01027 unsigned OpIdx = 0; 01028 if (TID.getNumDefs() == 2) 01029 Binary |= getMachineOpValue (MI, OpIdx++) << ARMII::RegRdLoShift; 01030 01031 // Encode Rd 01032 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdHiShift; 01033 01034 // Encode Rm 01035 Binary |= getMachineOpValue(MI, OpIdx++); 01036 01037 // Encode Rs 01038 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRsShift; 01039 01040 // Many multiple instructions (e.g. MLA) have three src operands. Encode 01041 // it as Rn (for multiply, that's in the same offset as RdLo. 01042 if (TID.getNumOperands() > OpIdx && 01043 !TID.OpInfo[OpIdx].isPredicate() && 01044 !TID.OpInfo[OpIdx].isOptionalDef()) 01045 Binary |= getMachineOpValue(MI, OpIdx) << ARMII::RegRdLoShift; 01046 01047 emitWordLE(Binary); 01048 } 01049 01050 template<class CodeEmitter> 01051 void Emitter<CodeEmitter>::emitExtendInstruction(const MachineInstr &MI) { 01052 const TargetInstrDesc &TID = MI.getDesc(); 01053 01054 // Part of binary is determined by TableGn. 01055 unsigned Binary = getBinaryCodeForInstr(MI); 01056 01057 // Set the conditional execution predicate 01058 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01059 01060 unsigned OpIdx = 0; 01061 01062 // Encode Rd 01063 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01064 01065 const MachineOperand &MO1 = MI.getOperand(OpIdx++); 01066 const MachineOperand &MO2 = MI.getOperand(OpIdx); 01067 if (MO2.isReg()) { 01068 // Two register operand form. 01069 // Encode Rn. 01070 Binary |= getMachineOpValue(MI, MO1) << ARMII::RegRnShift; 01071 01072 // Encode Rm. 01073 Binary |= getMachineOpValue(MI, MO2); 01074 ++OpIdx; 01075 } else { 01076 Binary |= getMachineOpValue(MI, MO1); 01077 } 01078 01079 // Encode rot imm (0, 8, 16, or 24) if it has a rotate immediate operand. 01080 if (MI.getOperand(OpIdx).isImm() && 01081 !TID.OpInfo[OpIdx].isPredicate() && 01082 !TID.OpInfo[OpIdx].isOptionalDef()) 01083 Binary |= (getMachineOpValue(MI, OpIdx) / 8) << ARMII::ExtRotImmShift; 01084 01085 emitWordLE(Binary); 01086 } 01087 01088 template<class CodeEmitter> 01089 void Emitter<CodeEmitter>::emitMiscArithInstruction(const MachineInstr &MI) { 01090 const TargetInstrDesc &TID = MI.getDesc(); 01091 01092 // Part of binary is determined by TableGn. 01093 unsigned Binary = getBinaryCodeForInstr(MI); 01094 01095 // Set the conditional execution predicate 01096 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01097 01098 unsigned OpIdx = 0; 01099 01100 // Encode Rd 01101 Binary |= getMachineOpValue(MI, OpIdx++) << ARMII::RegRdShift; 01102 01103 const MachineOperand &MO = MI.getOperand(OpIdx++); 01104 if (OpIdx == TID.getNumOperands() || 01105 TID.OpInfo[OpIdx].isPredicate() || 01106 TID.OpInfo[OpIdx].isOptionalDef()) { 01107 // Encode Rm and it's done. 01108 Binary |= getMachineOpValue(MI, MO); 01109 emitWordLE(Binary); 01110 return; 01111 } 01112 01113 // Encode Rn. 01114 Binary |= getMachineOpValue(MI, MO) << ARMII::RegRnShift; 01115 01116 // Encode Rm. 01117 Binary |= getMachineOpValue(MI, OpIdx++); 01118 01119 // Encode shift_imm. 01120 unsigned ShiftAmt = MI.getOperand(OpIdx).getImm(); 01121 assert(ShiftAmt < 32 && "shift_imm range is 0 to 31!"); 01122 Binary |= ShiftAmt << ARMII::ShiftShift; 01123 01124 emitWordLE(Binary); 01125 } 01126 01127 template<class CodeEmitter> 01128 void Emitter<CodeEmitter>::emitBranchInstruction(const MachineInstr &MI) { 01129 const TargetInstrDesc &TID = MI.getDesc(); 01130 01131 if (TID.Opcode == ARM::TPsoft) { 01132 llvm_unreachable("ARM::TPsoft FIXME"); // FIXME 01133 } 01134 01135 // Part of binary is determined by TableGn. 01136 unsigned Binary = getBinaryCodeForInstr(MI); 01137 01138 // Set the conditional execution predicate 01139 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01140 01141 // Set signed_immed_24 field 01142 Binary |= getMachineOpValue(MI, 0); 01143 01144 emitWordLE(Binary); 01145 } 01146 01147 template<class CodeEmitter> 01148 void Emitter<CodeEmitter>::emitInlineJumpTable(unsigned JTIndex) { 01149 // Remember the base address of the inline jump table. 01150 uintptr_t JTBase = MCE.getCurrentPCValue(); 01151 JTI->addJumpTableBaseAddr(JTIndex, JTBase); 01152 DOUT << " ** Jump Table #" << JTIndex << " @ " << (void*)JTBase << '\n'; 01153 01154 // Now emit the jump table entries. 01155 const std::vector<MachineBasicBlock*> &MBBs = (*MJTEs)[JTIndex].MBBs; 01156 for (unsigned i = 0, e = MBBs.size(); i != e; ++i) { 01157 if (IsPIC) 01158 // DestBB address - JT base. 01159 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_pic_jt, JTBase); 01160 else 01161 // Absolute DestBB address. 01162 emitMachineBasicBlock(MBBs[i], ARM::reloc_arm_absolute); 01163 emitWordLE(0); 01164 } 01165 } 01166 01167 template<class CodeEmitter> 01168 void Emitter<CodeEmitter>::emitMiscBranchInstruction(const MachineInstr &MI) { 01169 const TargetInstrDesc &TID = MI.getDesc(); 01170 01171 // Handle jump tables. 01172 if (TID.Opcode == ARM::BR_JTr || TID.Opcode == ARM::BR_JTadd) { 01173 // First emit a ldr pc, [] instruction. 01174 emitDataProcessingInstruction(MI, ARM::PC); 01175 01176 // Then emit the inline jump table. 01177 unsigned JTIndex = 01178 (TID.Opcode == ARM::BR_JTr) 01179 ? MI.getOperand(1).getIndex() : MI.getOperand(2).getIndex(); 01180 emitInlineJumpTable(JTIndex); 01181 return; 01182 } else if (TID.Opcode == ARM::BR_JTm) { 01183 // First emit a ldr pc, [] instruction. 01184 emitLoadStoreInstruction(MI, ARM::PC); 01185 01186 // Then emit the inline jump table. 01187 emitInlineJumpTable(MI.getOperand(3).getIndex()); 01188 return; 01189 } 01190 01191 // Part of binary is determined by TableGn. 01192 unsigned Binary = getBinaryCodeForInstr(MI); 01193 01194 // Set the conditional execution predicate 01195 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01196 01197 if (TID.Opcode == ARM::BX_RET) 01198 // The return register is LR. 01199 Binary |= ARMRegisterInfo::getRegisterNumbering(ARM::LR); 01200 else 01201 // otherwise, set the return register 01202 Binary |= getMachineOpValue(MI, 0); 01203 01204 emitWordLE(Binary); 01205 } 01206 01207 static unsigned encodeVFPRd(const MachineInstr &MI, unsigned OpIdx) { 01208 unsigned RegD = MI.getOperand(OpIdx).getReg(); 01209 unsigned Binary = 0; 01210 bool isSPVFP = false; 01211 RegD = ARMRegisterInfo::getRegisterNumbering(RegD, &isSPVFP); 01212 if (!isSPVFP) 01213 Binary |= RegD << ARMII::RegRdShift; 01214 else { 01215 Binary |= ((RegD & 0x1E) >> 1) << ARMII::RegRdShift; 01216 Binary |= (RegD & 0x01) << ARMII::D_BitShift; 01217 } 01218 return Binary; 01219 } 01220 01221 static unsigned encodeVFPRn(const MachineInstr &MI, unsigned OpIdx) { 01222 unsigned RegN = MI.getOperand(OpIdx).getReg(); 01223 unsigned Binary = 0; 01224 bool isSPVFP = false; 01225 RegN = ARMRegisterInfo::getRegisterNumbering(RegN, &isSPVFP); 01226 if (!isSPVFP) 01227 Binary |= RegN << ARMII::RegRnShift; 01228 else { 01229 Binary |= ((RegN & 0x1E) >> 1) << ARMII::RegRnShift; 01230 Binary |= (RegN & 0x01) << ARMII::N_BitShift; 01231 } 01232 return Binary; 01233 } 01234 01235 static unsigned encodeVFPRm(const MachineInstr &MI, unsigned OpIdx) { 01236 unsigned RegM = MI.getOperand(OpIdx).getReg(); 01237 unsigned Binary = 0; 01238 bool isSPVFP = false; 01239 RegM = ARMRegisterInfo::getRegisterNumbering(RegM, &isSPVFP); 01240 if (!isSPVFP) 01241 Binary |= RegM; 01242 else { 01243 Binary |= ((RegM & 0x1E) >> 1); 01244 Binary |= (RegM & 0x01) << ARMII::M_BitShift; 01245 } 01246 return Binary; 01247 } 01248 01249 template<class CodeEmitter> 01250 void Emitter<CodeEmitter>::emitVFPArithInstruction(const MachineInstr &MI) { 01251 const TargetInstrDesc &TID = MI.getDesc(); 01252 01253 // Part of binary is determined by TableGn. 01254 unsigned Binary = getBinaryCodeForInstr(MI); 01255 01256 // Set the conditional execution predicate 01257 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01258 01259 unsigned OpIdx = 0; 01260 assert((Binary & ARMII::D_BitShift) == 0 && 01261 (Binary & ARMII::N_BitShift) == 0 && 01262 (Binary & ARMII::M_BitShift) == 0 && "VFP encoding bug!"); 01263 01264 // Encode Dd / Sd. 01265 Binary |= encodeVFPRd(MI, OpIdx++); 01266 01267 // If this is a two-address operand, skip it, e.g. FMACD. 01268 if (TID.getOperandConstraint(OpIdx, TOI::TIED_TO) != -1) 01269 ++OpIdx; 01270 01271 // Encode Dn / Sn. 01272 if ((TID.TSFlags & ARMII::FormMask) == ARMII::VFPBinaryFrm) 01273 Binary |= encodeVFPRn(MI, OpIdx++); 01274 01275 if (OpIdx == TID.getNumOperands() || 01276 TID.OpInfo[OpIdx].isPredicate() || 01277 TID.OpInfo[OpIdx].isOptionalDef()) { 01278 // FCMPEZD etc. has only one operand. 01279 emitWordLE(Binary); 01280 return; 01281 } 01282 01283 // Encode Dm / Sm. 01284 Binary |= encodeVFPRm(MI, OpIdx); 01285 01286 emitWordLE(Binary); 01287 } 01288 01289 template<class CodeEmitter> 01290 void Emitter<CodeEmitter>::emitVFPConversionInstruction( 01291 const MachineInstr &MI) { 01292 const TargetInstrDesc &TID = MI.getDesc(); 01293 unsigned Form = TID.TSFlags & ARMII::FormMask; 01294 01295 // Part of binary is determined by TableGn. 01296 unsigned Binary = getBinaryCodeForInstr(MI); 01297 01298 // Set the conditional execution predicate 01299 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01300 01301 switch (Form) { 01302 default: break; 01303 case ARMII::VFPConv1Frm: 01304 case ARMII::VFPConv2Frm: 01305 case ARMII::VFPConv3Frm: 01306 // Encode Dd / Sd. 01307 Binary |= encodeVFPRd(MI, 0); 01308 break; 01309 case ARMII::VFPConv4Frm: 01310 // Encode Dn / Sn. 01311 Binary |= encodeVFPRn(MI, 0); 01312 break; 01313 case ARMII::VFPConv5Frm: 01314 // Encode Dm / Sm. 01315 Binary |= encodeVFPRm(MI, 0); 01316 break; 01317 } 01318 01319 switch (Form) { 01320 default: break; 01321 case ARMII::VFPConv1Frm: 01322 // Encode Dm / Sm. 01323 Binary |= encodeVFPRm(MI, 1); 01324 break; 01325 case ARMII::VFPConv2Frm: 01326 case ARMII::VFPConv3Frm: 01327 // Encode Dn / Sn. 01328 Binary |= encodeVFPRn(MI, 1); 01329 break; 01330 case ARMII::VFPConv4Frm: 01331 case ARMII::VFPConv5Frm: 01332 // Encode Dd / Sd. 01333 Binary |= encodeVFPRd(MI, 1); 01334 break; 01335 } 01336 01337 if (Form == ARMII::VFPConv5Frm) 01338 // Encode Dn / Sn. 01339 Binary |= encodeVFPRn(MI, 2); 01340 else if (Form == ARMII::VFPConv3Frm) 01341 // Encode Dm / Sm. 01342 Binary |= encodeVFPRm(MI, 2); 01343 01344 emitWordLE(Binary); 01345 } 01346 01347 template<class CodeEmitter> 01348 void Emitter<CodeEmitter>::emitVFPLoadStoreInstruction(const MachineInstr &MI) { 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 Dd / Sd. 01358 Binary |= encodeVFPRd(MI, OpIdx++); 01359 01360 // Encode address base. 01361 const MachineOperand &Base = MI.getOperand(OpIdx++); 01362 Binary |= getMachineOpValue(MI, Base) << ARMII::RegRnShift; 01363 01364 // If there is a non-zero immediate offset, encode it. 01365 if (Base.isReg()) { 01366 const MachineOperand &Offset = MI.getOperand(OpIdx); 01367 if (unsigned ImmOffs = ARM_AM::getAM5Offset(Offset.getImm())) { 01368 if (ARM_AM::getAM5Op(Offset.getImm()) == ARM_AM::add) 01369 Binary |= 1 << ARMII::U_BitShift; 01370 Binary |= ImmOffs; 01371 emitWordLE(Binary); 01372 return; 01373 } 01374 } 01375 01376 // If immediate offset is omitted, default to +0. 01377 Binary |= 1 << ARMII::U_BitShift; 01378 01379 emitWordLE(Binary); 01380 } 01381 01382 template<class CodeEmitter> 01383 void Emitter<CodeEmitter>::emitVFPLoadStoreMultipleInstruction( 01384 const MachineInstr &MI) { 01385 // Part of binary is determined by TableGn. 01386 unsigned Binary = getBinaryCodeForInstr(MI); 01387 01388 // Set the conditional execution predicate 01389 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01390 01391 // Set base address operand 01392 Binary |= getMachineOpValue(MI, 0) << ARMII::RegRnShift; 01393 01394 // Set addressing mode by modifying bits U(23) and P(24) 01395 const MachineOperand &MO = MI.getOperand(1); 01396 Binary |= getAddrModeUPBits(ARM_AM::getAM5SubMode(MO.getImm())); 01397 01398 // Set bit W(21) 01399 if (ARM_AM::getAM5WBFlag(MO.getImm())) 01400 Binary |= 0x1 << ARMII::W_BitShift; 01401 01402 // First register is encoded in Dd. 01403 Binary |= encodeVFPRd(MI, 4); 01404 01405 // Number of registers are encoded in offset field. 01406 unsigned NumRegs = 1; 01407 for (unsigned i = 5, e = MI.getNumOperands(); i != e; ++i) { 01408 const MachineOperand &MO = MI.getOperand(i); 01409 if (!MO.isReg() || MO.isImplicit()) 01410 break; 01411 ++NumRegs; 01412 } 01413 Binary |= NumRegs * 2; 01414 01415 emitWordLE(Binary); 01416 } 01417 01418 template<class CodeEmitter> 01419 void Emitter<CodeEmitter>::emitMiscInstruction(const MachineInstr &MI) { 01420 // Part of binary is determined by TableGn. 01421 unsigned Binary = getBinaryCodeForInstr(MI); 01422 01423 // Set the conditional execution predicate 01424 Binary |= II->getPredicate(&MI) << ARMII::CondShift; 01425 01426 emitWordLE(Binary); 01427 } 01428 01429 #include "ARMGenCodeEmitter.inc"