Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===// |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 8b1bf43 | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 10 | // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to |
Gabor Greif | e16561c | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 11 | // JIT-compile bitcode to native PowerPC. |
Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | bfca1ab | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 15 | #include "PPC.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "PPCRelocations.h" |
| 17 | #include "PPCTargetMachine.h" |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Misha Brukman | 9ce0da9 | 2004-10-23 23:47:34 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Nicolas Geoffray | 21ad494 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Module.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/PassManager.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Evan Cheng | 5f99760 | 2006-02-18 00:08:58 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | f242979 | 2004-11-16 04:47:33 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 28 | |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 29 | namespace { |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 30 | class PPCCodeEmitter : public MachineFunctionPass { |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 31 | TargetMachine &TM; |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 32 | JITCodeEmitter &MCE; |
Chris Lattner | 34adc8d | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 33 | MachineModuleInfo *MMI; |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 34 | |
| 35 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 36 | AU.addRequired<MachineModuleInfo>(); |
| 37 | MachineFunctionPass::getAnalysisUsage(AU); |
| 38 | } |
| 39 | |
| 40 | static char ID; |
| 41 | |
| 42 | /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record |
| 43 | /// its address in the function into this pointer. |
| 44 | void *MovePCtoLROffset; |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 45 | public: |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 46 | |
| 47 | PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 48 | : MachineFunctionPass(ID), TM(tm), MCE(mce) {} |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 49 | |
| 50 | /// getBinaryCodeForInstr - This function, generated by the |
| 51 | /// CodeEmitterGenerator using TableGen, produces the binary encoding for |
| 52 | /// machine instructions. |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 53 | uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const; |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 55 | |
| 56 | MachineRelocation GetRelocation(const MachineOperand &MO, |
| 57 | unsigned RelocID) const; |
| 58 | |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 59 | /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr |
Bruno Cardoso Lopes | 9fd794b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 60 | unsigned getMachineOpValue(const MachineInstr &MI, |
Jim Grosbach | a7b6d58 | 2010-10-08 00:21:28 +0000 | [diff] [blame] | 61 | const MachineOperand &MO) const; |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 62 | |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 63 | unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const; |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 64 | unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const; |
| 65 | unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 66 | unsigned getAbsDirectBrEncoding(const MachineInstr &MI, |
| 67 | unsigned OpNo) const; |
| 68 | unsigned getAbsCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 69 | |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 70 | unsigned getImm16Encoding(const MachineInstr &MI, unsigned OpNo) const; |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 71 | unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 72 | unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 73 | unsigned getTLSRegEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 74 | unsigned getTLSCallEncoding(const MachineInstr &MI, unsigned OpNo) const; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 75 | |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 76 | const char *getPassName() const { return "PowerPC Machine Code Emitter"; } |
| 77 | |
| 78 | /// runOnMachineFunction - emits the given MachineFunction to memory |
| 79 | /// |
| 80 | bool runOnMachineFunction(MachineFunction &MF); |
| 81 | |
| 82 | /// emitBasicBlock - emits the given MachineBasicBlock to memory |
| 83 | /// |
| 84 | void emitBasicBlock(MachineBasicBlock &MBB); |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 85 | }; |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 86 | } |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 88 | char PPCCodeEmitter::ID = 0; |
| 89 | |
Nate Begeman | 3cb3921 | 2006-08-23 21:08:52 +0000 | [diff] [blame] | 90 | /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code |
| 91 | /// to the specified MCE object. |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 92 | FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM, |
Bruno Cardoso Lopes | 9fd794b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 93 | JITCodeEmitter &JCE) { |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 94 | return new PPCCodeEmitter(TM, JCE); |
Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 97 | bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Evan Cheng | 73136df | 2006-02-22 20:19:42 +0000 | [diff] [blame] | 98 | assert((MF.getTarget().getRelocationModel() != Reloc::Default || |
| 99 | MF.getTarget().getRelocationModel() != Reloc::Static) && |
| 100 | "JIT relocation model must be set to static or default!"); |
Nicolas Geoffray | 21ad494 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 34adc8d | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 102 | MMI = &getAnalysis<MachineModuleInfo>(); |
| 103 | MCE.setModuleInfo(MMI); |
Chris Lattner | c9aa371 | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 104 | do { |
Chris Lattner | 09fecf9 | 2006-12-08 04:54:03 +0000 | [diff] [blame] | 105 | MovePCtoLROffset = 0; |
Chris Lattner | c9aa371 | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 106 | MCE.startFunction(MF); |
Chris Lattner | c9aa371 | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 107 | for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) |
| 108 | emitBasicBlock(*BB); |
Chris Lattner | c9aa371 | 2006-05-02 18:27:26 +0000 | [diff] [blame] | 109 | } while (MCE.finishFunction(MF)); |
Misha Brukman | 2beb63a | 2004-10-21 01:42:02 +0000 | [diff] [blame] | 110 | |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
Chris Lattner | 308acc4 | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 114 | void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Chris Lattner | 1d8ee1f | 2006-05-03 17:10:41 +0000 | [diff] [blame] | 115 | MCE.StartMachineBasicBlock(&MBB); |
Daniel Dunbar | 7d6781b | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 116 | |
Misha Brukman | 421c3c1 | 2004-10-23 18:28:01 +0000 | [diff] [blame] | 117 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){ |
Evan Cheng | 34f3a96 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 118 | const MachineInstr &MI = *I; |
Devang Patel | 051454a | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 119 | MCE.processDebugLoc(MI.getDebugLoc(), true); |
Chris Lattner | 743a434 | 2004-11-23 05:59:53 +0000 | [diff] [blame] | 120 | switch (MI.getOpcode()) { |
| 121 | default: |
Evan Cheng | 34f3a96 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 122 | MCE.emitWordBE(getBinaryCodeForInstr(MI)); |
Chris Lattner | 743a434 | 2004-11-23 05:59:53 +0000 | [diff] [blame] | 123 | break; |
Rafael Espindola | b1f25f1 | 2014-03-07 06:08:31 +0000 | [diff] [blame] | 124 | case TargetOpcode::CFI_INSTRUCTION: |
| 125 | break; |
Chris Lattner | ee2fbbc | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 126 | case TargetOpcode::EH_LABEL: |
| 127 | MCE.emitLabel(MI.getOperand(0).getMCSymbol()); |
| 128 | break; |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 129 | case TargetOpcode::IMPLICIT_DEF: |
| 130 | case TargetOpcode::KILL: |
Evan Cheng | 24bc123 | 2008-03-17 06:56:52 +0000 | [diff] [blame] | 131 | break; // pseudo opcode, no side effects |
Chris Lattner | 743a434 | 2004-11-23 05:59:53 +0000 | [diff] [blame] | 132 | case PPC::MovePCtoLR: |
Chris Lattner | 44dbdbe | 2006-11-14 18:44:47 +0000 | [diff] [blame] | 133 | case PPC::MovePCtoLR8: |
Chris Lattner | 09fecf9 | 2006-12-08 04:54:03 +0000 | [diff] [blame] | 134 | assert(TM.getRelocationModel() == Reloc::PIC_); |
| 135 | MovePCtoLROffset = (void*)MCE.getCurrentPCValue(); |
| 136 | MCE.emitWordBE(0x48000005); // bl 1 |
Chris Lattner | 743a434 | 2004-11-23 05:59:53 +0000 | [diff] [blame] | 137 | break; |
| 138 | } |
Devang Patel | 051454a | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 139 | MCE.processDebugLoc(MI.getDebugLoc(), false); |
Misha Brukman | 421c3c1 | 2004-10-23 18:28:01 +0000 | [diff] [blame] | 140 | } |
Misha Brukman | 1892291 | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 143 | unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI, |
| 144 | unsigned OpNo) const { |
| 145 | const MachineOperand &MO = MI.getOperand(OpNo); |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 146 | assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 || |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 147 | MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) && |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 148 | (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 149 | return 0x80 >> TM.getRegisterInfo()->getEncodingValue(MO.getReg()); |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 152 | MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO, |
| 153 | unsigned RelocID) const { |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 154 | // If in PIC mode, we need to encode the negated address of the |
| 155 | // 'movepctolr' into the unrelocated field. After relocation, we'll have |
| 156 | // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm |
| 157 | // field, we get &gv. This doesn't happen for branch relocations, which are |
| 158 | // always implicitly pc relative. |
| 159 | intptr_t Cst = 0; |
| 160 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 161 | assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); |
| 162 | Cst = -(intptr_t)MovePCtoLROffset - 4; |
| 163 | } |
| 164 | |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 165 | if (MO.isGlobal()) |
| 166 | return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID, |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 167 | const_cast<GlobalValue *>(MO.getGlobal()), |
| 168 | Cst, isa<Function>(MO.getGlobal())); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 169 | if (MO.isSymbol()) |
| 170 | return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 171 | RelocID, MO.getSymbolName(), Cst); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 172 | if (MO.isCPI()) |
| 173 | return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 174 | RelocID, MO.getIndex(), Cst); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 175 | |
| 176 | if (MO.isMBB()) |
Chris Lattner | bf9f2f2 | 2010-11-15 22:50:50 +0000 | [diff] [blame] | 177 | return MachineRelocation::getBB(MCE.getCurrentPCOffset(), |
| 178 | RelocID, MO.getMBB()); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 179 | |
| 180 | assert(MO.isJTI()); |
| 181 | return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 182 | RelocID, MO.getIndex(), Cst); |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 185 | unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI, |
| 186 | unsigned OpNo) const { |
Chris Lattner | 79fa371 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 187 | const MachineOperand &MO = MI.getOperand(OpNo); |
| 188 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO); |
| 189 | |
| 190 | MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx)); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Chris Lattner | 0e3461e | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 194 | unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI, |
| 195 | unsigned OpNo) const { |
| 196 | const MachineOperand &MO = MI.getOperand(OpNo); |
| 197 | MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx)); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Ulrich Weigand | b6a30d1 | 2013-06-24 11:03:33 +0000 | [diff] [blame] | 201 | unsigned PPCCodeEmitter::getAbsDirectBrEncoding(const MachineInstr &MI, |
| 202 | unsigned OpNo) const { |
| 203 | const MachineOperand &MO = MI.getOperand(OpNo); |
| 204 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO); |
| 205 | |
| 206 | llvm_unreachable("Absolute branch relocations unsupported on the old JIT."); |
| 207 | } |
| 208 | |
| 209 | unsigned PPCCodeEmitter::getAbsCondBrEncoding(const MachineInstr &MI, |
| 210 | unsigned OpNo) const { |
| 211 | llvm_unreachable("Absolute branch relocations unsupported on the old JIT."); |
| 212 | } |
| 213 | |
Ulrich Weigand | fd3ad69 | 2013-06-26 13:49:15 +0000 | [diff] [blame] | 214 | unsigned PPCCodeEmitter::getImm16Encoding(const MachineInstr &MI, |
| 215 | unsigned OpNo) const { |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 216 | const MachineOperand &MO = MI.getOperand(OpNo); |
| 217 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO); |
| 218 | |
Ulrich Weigand | 2dbe06a | 2013-05-17 14:14:12 +0000 | [diff] [blame] | 219 | unsigned RelocID; |
| 220 | switch (MO.getTargetFlags() & PPCII::MO_ACCESS_MASK) { |
| 221 | default: llvm_unreachable("Unsupported target operand flags!"); |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 222 | case PPCII::MO_LO: RelocID = PPC::reloc_absolute_low; break; |
| 223 | case PPCII::MO_HA: RelocID = PPC::reloc_absolute_high; break; |
Ulrich Weigand | 2dbe06a | 2013-05-17 14:14:12 +0000 | [diff] [blame] | 224 | } |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 225 | |
Ulrich Weigand | 2dbe06a | 2013-05-17 14:14:12 +0000 | [diff] [blame] | 226 | MCE.addRelocation(GetRelocation(MO, RelocID)); |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 230 | unsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI, |
| 231 | unsigned OpNo) const { |
| 232 | // Encode (imm, reg) as a memri, which has the low 16-bits as the |
| 233 | // displacement and the next 5 bits as the register #. |
| 234 | assert(MI.getOperand(OpNo+1).isReg()); |
| 235 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16; |
| 236 | |
| 237 | const MachineOperand &MO = MI.getOperand(OpNo); |
| 238 | if (MO.isImm()) |
| 239 | return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits; |
| 240 | |
| 241 | // Add a fixup for the displacement field. |
| 242 | MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low)); |
| 243 | return RegBits; |
| 244 | } |
| 245 | |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 246 | unsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI, |
| 247 | unsigned OpNo) const { |
| 248 | // Encode (imm, reg) as a memrix, which has the low 14-bits as the |
| 249 | // displacement and the next 5 bits as the register #. |
| 250 | assert(MI.getOperand(OpNo+1).isReg()); |
| 251 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14; |
| 252 | |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 253 | const MachineOperand &MO = MI.getOperand(OpNo); |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 254 | if (MO.isImm()) |
Ulrich Weigand | 9d980cb | 2013-05-16 17:58:02 +0000 | [diff] [blame] | 255 | return ((getMachineOpValue(MI, MO) >> 2) & 0x3FFF) | RegBits; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 256 | |
| 257 | MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix)); |
Chris Lattner | 8f4444d | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 258 | return RegBits; |
Chris Lattner | 6566112 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 261 | |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 262 | unsigned PPCCodeEmitter::getTLSRegEncoding(const MachineInstr &MI, |
| 263 | unsigned OpNo) const { |
| 264 | llvm_unreachable("TLS not supported on the old JIT."); |
| 265 | return 0; |
| 266 | } |
| 267 | |
Ulrich Weigand | 5143bab | 2013-07-02 21:31:04 +0000 | [diff] [blame] | 268 | unsigned PPCCodeEmitter::getTLSCallEncoding(const MachineInstr &MI, |
| 269 | unsigned OpNo) const { |
| 270 | llvm_unreachable("TLS not supported on the old JIT."); |
| 271 | return 0; |
| 272 | } |
Bill Schmidt | ca4a0c9 | 2012-12-04 16:18:08 +0000 | [diff] [blame] | 273 | |
Evan Cheng | 34f3a96 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 274 | unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI, |
Jim Grosbach | a7b6d58 | 2010-10-08 00:21:28 +0000 | [diff] [blame] | 275 | const MachineOperand &MO) const { |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 276 | |
Dan Gohman | 0d1e9a8 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 277 | if (MO.isReg()) { |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 278 | // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand. |
Chris Lattner | 73716a6 | 2010-11-16 00:55:51 +0000 | [diff] [blame] | 279 | // The GPR operand should come through here though. |
Ulrich Weigand | 49f487e | 2013-07-03 17:59:07 +0000 | [diff] [blame] | 280 | assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 && |
Ulrich Weigand | d5ebc62 | 2013-07-03 17:05:42 +0000 | [diff] [blame] | 281 | MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) || |
Chris Lattner | 73716a6 | 2010-11-16 00:55:51 +0000 | [diff] [blame] | 282 | MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7); |
Hal Finkel | feea653 | 2013-03-26 20:08:20 +0000 | [diff] [blame] | 283 | return TM.getRegisterInfo()->getEncodingValue(MO.getReg()); |
Chris Lattner | d6a07cc | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Chris Lattner | efacb9e | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 286 | assert(MO.isImm() && |
| 287 | "Relocation required in an instruction that we cannot encode!"); |
| 288 | return MO.getImm(); |
Misha Brukman | e05203f | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | 0921e3b | 2005-10-14 23:37:35 +0000 | [diff] [blame] | 291 | #include "PPCGenCodeEmitter.inc" |