Chris Lattner | 556d89d | 2003-08-01 22:19:03 +0000 | [diff] [blame] | 1 | //===-- SparcV9CodeEmitter.cpp --------------------------------------------===// |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 9 | // |
Brian Gaeke | 4296088 | 2003-10-13 19:51:20 +0000 | [diff] [blame] | 10 | // SPARC-specific backend for emitting machine code to memory. |
| 11 | // |
Chris Lattner | 42c7a16 | 2004-12-16 16:47:56 +0000 | [diff] [blame] | 12 | // This module also contains the code for lazily resolving the targets of call |
| 13 | // instructions, including the callback used to redirect calls to functions for |
| 14 | // which the code has not yet been generated into the JIT compiler. |
Brian Gaeke | 4296088 | 2003-10-13 19:51:20 +0000 | [diff] [blame] | 15 | // |
Chris Lattner | 42c7a16 | 2004-12-16 16:47:56 +0000 | [diff] [blame] | 16 | // This file #includes SparcV9GenCodeEmitter.inc, which contains the code for |
| 17 | // getBinaryCodeForInstr(), a method that converts a MachineInstr into the |
| 18 | // corresponding binary machine code word. |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/Function.h" |
| 24 | #include "llvm/GlobalVariable.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 25 | #include "llvm/PassManager.h" |
| 26 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineConstantPool.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 29 | #include "llvm/CodeGen/MachineInstr.h" |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetMachine.h" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 33 | #include "SparcV9Internals.h" |
| 34 | #include "SparcV9TargetMachine.h" |
| 35 | #include "SparcV9RegInfo.h" |
Misha Brukman | 0cc640e | 2003-05-27 21:45:05 +0000 | [diff] [blame] | 36 | #include "SparcV9CodeEmitter.h" |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 37 | #include "SparcV9Relocations.h" |
Chris Lattner | 85015a0 | 2004-08-16 21:55:02 +0000 | [diff] [blame] | 38 | #include "MachineFunctionInfo.h" |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 39 | using namespace llvm; |
Misha Brukman | 103f0c3 | 2003-09-05 22:59:31 +0000 | [diff] [blame] | 40 | |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 41 | bool SparcV9TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM, |
Misha Brukman | 7a750e1 | 2004-08-04 21:48:00 +0000 | [diff] [blame] | 42 | MachineCodeEmitter &MCE) { |
Brian Gaeke | eb6c29b | 2004-07-27 19:37:37 +0000 | [diff] [blame] | 43 | PM.add(new SparcV9CodeEmitter(*this, MCE)); |
Misha Brukman | 7a750e1 | 2004-08-04 21:48:00 +0000 | [diff] [blame] | 44 | PM.add(createSparcV9MachineCodeDestructionPass()); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 48 | SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm, |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 49 | MachineCodeEmitter &M): TM(tm), MCE(M) {} |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 50 | |
| 51 | void SparcV9CodeEmitter::emitWord(unsigned Val) { |
Brian Gaeke | 62c6f87 | 2004-04-23 17:11:15 +0000 | [diff] [blame] | 52 | MCE.emitWord(Val); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 55 | unsigned |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 56 | SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg, |
Misha Brukman | fad4929 | 2003-08-15 00:26:50 +0000 | [diff] [blame] | 57 | MachineInstr &MI) { |
Brian Gaeke | 498231b | 2004-06-03 02:45:09 +0000 | [diff] [blame] | 58 | const SparcV9RegInfo &RI = *TM.getRegInfo(); |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 59 | unsigned regClass, regType = RI.getRegType(fakeReg); |
| 60 | // At least map fakeReg into its class |
| 61 | fakeReg = RI.getClassRegNum(fakeReg, regClass); |
| 62 | |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 63 | switch (regClass) { |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 64 | case SparcV9RegInfo::IntRegClassID: { |
| 65 | // SparcV9 manual, p31 |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 66 | static const unsigned IntRegMap[] = { |
| 67 | // "o0", "o1", "o2", "o3", "o4", "o5", "o7", |
| 68 | 8, 9, 10, 11, 12, 13, 15, |
| 69 | // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", |
| 70 | 16, 17, 18, 19, 20, 21, 22, 23, |
| 71 | // "i0", "i1", "i2", "i3", "i4", "i5", "i6", "i7", |
| 72 | 24, 25, 26, 27, 28, 29, 30, 31, |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 73 | // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 74 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 75 | // "o6" |
| 76 | 14 |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 77 | }; |
| 78 | |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 79 | return IntRegMap[fakeReg]; |
| 80 | break; |
| 81 | } |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 82 | case SparcV9RegInfo::FloatRegClassID: { |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 83 | DEBUG(std::cerr << "FP reg: " << fakeReg << "\n"); |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 84 | if (regType == SparcV9RegInfo::FPSingleRegType) { |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 85 | // only numbered 0-31, hence can already fit into 5 bits (and 6) |
| 86 | DEBUG(std::cerr << "FP single reg, returning: " << fakeReg << "\n"); |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 87 | } else if (regType == SparcV9RegInfo::FPDoubleRegType) { |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 88 | // FIXME: This assumes that we only have 5-bit register fields! |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 89 | // From SparcV9 Manual, page 40. |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 90 | // The bit layout becomes: b[4], b[3], b[2], b[1], b[5] |
| 91 | fakeReg |= (fakeReg >> 5) & 1; |
| 92 | fakeReg &= 0x1f; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 93 | DEBUG(std::cerr << "FP double reg, returning: " << fakeReg << "\n"); |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 94 | } |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 95 | return fakeReg; |
| 96 | } |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 97 | case SparcV9RegInfo::IntCCRegClassID: { |
Misha Brukman | dfbfc57 | 2003-07-16 20:30:40 +0000 | [diff] [blame] | 98 | /* xcc, icc, ccr */ |
| 99 | static const unsigned IntCCReg[] = { 6, 4, 2 }; |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 100 | |
Misha Brukman | dfbfc57 | 2003-07-16 20:30:40 +0000 | [diff] [blame] | 101 | assert(fakeReg < sizeof(IntCCReg)/sizeof(IntCCReg[0]) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 102 | && "CC register out of bounds for IntCCReg map"); |
Misha Brukman | dfbfc57 | 2003-07-16 20:30:40 +0000 | [diff] [blame] | 103 | DEBUG(std::cerr << "IntCC reg: " << IntCCReg[fakeReg] << "\n"); |
| 104 | return IntCCReg[fakeReg]; |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 105 | } |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 106 | case SparcV9RegInfo::FloatCCRegClassID: { |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 107 | /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */ |
| 108 | DEBUG(std::cerr << "FP CC reg: " << fakeReg << "\n"); |
| 109 | return fakeReg; |
| 110 | } |
Brian Gaeke | 7fa84b7 | 2004-06-09 21:54:59 +0000 | [diff] [blame] | 111 | case SparcV9RegInfo::SpecialRegClassID: { |
| 112 | // Currently only "special" reg is %fsr, which is encoded as 1 in |
| 113 | // instructions and 0 in SparcV9SpecialRegClass. |
| 114 | static const unsigned SpecialReg[] = { 1 }; |
| 115 | assert(fakeReg < sizeof(SpecialReg)/sizeof(SpecialReg[0]) |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 116 | && "Special register out of bounds for SpecialReg map"); |
Brian Gaeke | 7fa84b7 | 2004-06-09 21:54:59 +0000 | [diff] [blame] | 117 | DEBUG(std::cerr << "Special reg: " << SpecialReg[fakeReg] << "\n"); |
| 118 | return SpecialReg[fakeReg]; |
| 119 | } |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 120 | default: |
Brian Gaeke | e8a6bee | 2004-06-09 20:44:42 +0000 | [diff] [blame] | 121 | assert(0 && "Invalid unified register number in getRealRegNum"); |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 122 | return fakeReg; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
Misha Brukman | 07d4516 | 2003-07-15 19:09:43 +0000 | [diff] [blame] | 127 | |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 128 | int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI, |
| 129 | MachineOperand &MO) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 130 | int64_t rv = 0; // Return value; defaults to 0 for unhandled cases |
| 131 | // or things that get fixed up later by the JIT. |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 132 | if (MO.isPCRelativeDisp() || MO.isGlobalAddress()) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 133 | DEBUG(std::cerr << "PCRelativeDisp: "); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 134 | Value *V = MO.getVRegValue(); |
| 135 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 136 | DEBUG(std::cerr << "Saving reference to BB (VReg)\n"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 137 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 138 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Brian Gaeke | a900452 | 2004-05-19 21:30:01 +0000 | [diff] [blame] | 139 | } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
Brian Gaeke | ceabd97 | 2004-05-20 07:43:40 +0000 | [diff] [blame] | 140 | // The real target of the branch is CI = PC + (rv * 4) |
| 141 | // So undo that: give the instruction (CI - PC) / 4 |
| 142 | rv = (CI->getRawValue() - MCE.getCurrentPCValue()) / 4; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 143 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
Chris Lattner | adbcc10 | 2004-11-23 15:57:01 +0000 | [diff] [blame] | 144 | unsigned Reloc = 0; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 145 | if (MI.getOpcode() == V9::CALL) { |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 146 | Reloc = V9::reloc_pcrel_call; |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 147 | } else if (MI.getOpcode() == V9::SETHI) { |
| 148 | if (MO.isHiBits64()) |
| 149 | Reloc = V9::reloc_sethi_hh; |
| 150 | else if (MO.isHiBits32()) |
| 151 | Reloc = V9::reloc_sethi_lm; |
| 152 | else |
| 153 | assert(0 && "Unknown relocation!"); |
| 154 | } else if (MI.getOpcode() == V9::ORi) { |
| 155 | if (MO.isLoBits32()) |
| 156 | Reloc = V9::reloc_or_lo; |
| 157 | else if (MO.isLoBits64()) |
| 158 | Reloc = V9::reloc_or_hm; |
| 159 | else |
| 160 | assert(0 && "Unknown relocation!"); |
| 161 | } else { |
| 162 | assert(0 && "Unknown relocation!"); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 163 | } |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 2eeda6e | 2004-11-22 21:42:40 +0000 | [diff] [blame] | 165 | MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(), Reloc, GV)); |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 166 | rv = 0; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 167 | } else { |
| 168 | std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n"; |
| 169 | abort(); |
| 170 | } |
Alkis Evlogimenos | af86211 | 2004-02-11 05:55:00 +0000 | [diff] [blame] | 171 | } else if (MO.isRegister() || MO.getType() == MachineOperand::MO_CCRegister) |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame] | 172 | { |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 173 | // This is necessary because the SparcV9 backend doesn't actually lay out |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 174 | // registers in the real fashion -- it skips those that it chooses not to |
| 175 | // allocate, i.e. those that are the FP, SP, etc. |
Alkis Evlogimenos | be766c7 | 2004-02-13 21:01:20 +0000 | [diff] [blame] | 176 | unsigned fakeReg = MO.getReg(); |
Misha Brukman | 173e250 | 2003-07-14 23:26:03 +0000 | [diff] [blame] | 177 | unsigned realRegByClass = getRealRegNum(fakeReg, MI); |
| 178 | DEBUG(std::cerr << MO << ": Reg[" << std::dec << fakeReg << "] => " |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 179 | << realRegByClass << " (LLC: " |
Chris Lattner | d029cd2 | 2004-06-02 05:55:25 +0000 | [diff] [blame] | 180 | << TM.getRegInfo()->getUnifiedRegName(fakeReg) << ")\n"); |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 181 | rv = realRegByClass; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 182 | } else if (MO.isImmediate()) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 183 | rv = MO.getImmedValue(); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 184 | DEBUG(std::cerr << "immed: " << rv << "\n"); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 185 | } else if (MO.isMachineBasicBlock()) { |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 186 | // Duplicate code of the above case for VirtualRegister, BasicBlock... |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 187 | // It should really hit this case, but SparcV9 backend uses VRegs instead |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 188 | DEBUG(std::cerr << "Saving reference to MBB\n"); |
Chris Lattner | 6856d11 | 2003-07-26 23:04:00 +0000 | [diff] [blame] | 189 | const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 190 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 191 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 192 | } else if (MO.isExternalSymbol()) { |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 193 | // SparcV9 backend doesn't generate this (yet...) |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 194 | std::cerr << "ERROR: External symbol unhandled: " << MO << "\n"; |
| 195 | abort(); |
| 196 | } else if (MO.isFrameIndex()) { |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 197 | // SparcV9 backend doesn't generate this (yet...) |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 198 | int FrameIndex = MO.getFrameIndex(); |
| 199 | std::cerr << "ERROR: Frame index unhandled.\n"; |
| 200 | abort(); |
| 201 | } else if (MO.isConstantPoolIndex()) { |
Misha Brukman | e5ad815 | 2003-11-07 18:06:26 +0000 | [diff] [blame] | 202 | unsigned Index = MO.getConstantPoolIndex(); |
| 203 | rv = MCE.getConstantPoolEntryAddress(Index); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 204 | } else { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 205 | std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 206 | abort(); |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Finally, deal with the various bitfield-extracting functions that |
| 210 | // are used in SPARC assembly. (Some of these make no sense in combination |
| 211 | // with some of the above; we'll trust that the instruction selector |
| 212 | // will not produce nonsense, and not check for valid combinations here.) |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 213 | if (MO.isLoBits32()) { // %lo(val) == %lo() in SparcV9 ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 214 | return rv & 0x03ff; |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 215 | } else if (MO.isHiBits32()) { // %lm(val) == %hi() in SparcV9 ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 216 | return (rv >> 10) & 0x03fffff; |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 217 | } else if (MO.isLoBits64()) { // %hm(val) == %ulo() in SparcV9 ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 218 | return (rv >> 32) & 0x03ff; |
Brian Gaeke | e3d6807 | 2004-02-25 18:44:15 +0000 | [diff] [blame] | 219 | } else if (MO.isHiBits64()) { // %hh(val) == %uhi() in SparcV9 ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 220 | return rv >> 42; |
| 221 | } else { // (unadorned) val |
| 222 | return rv; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) { |
| 227 | Val >>= bit; |
| 228 | return (Val & 1); |
| 229 | } |
| 230 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 231 | bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 232 | MCE.startFunction(MF); |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 233 | DEBUG(std::cerr << "Starting function " << MF.getFunction()->getName() |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 234 | << ", address: " << "0x" << std::hex |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 235 | << (long)MCE.getCurrentPCValue() << "\n"); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 236 | |
Misha Brukman | e5ad815 | 2003-11-07 18:06:26 +0000 | [diff] [blame] | 237 | MCE.emitConstantPool(MF.getConstantPool()); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 238 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 239 | emitBasicBlock(*I); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 240 | MCE.finishFunction(MF); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 241 | |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 242 | DEBUG(std::cerr << "Finishing fn " << MF.getFunction()->getName() << "\n"); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 243 | |
| 244 | // Resolve branches to BasicBlocks for the entire function |
| 245 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 246 | long Location = BBLocations[BBRefs[i].first]; |
| 247 | unsigned *Ref = BBRefs[i].second.first; |
| 248 | MachineInstr *MI = BBRefs[i].second.second; |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 249 | DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location |
| 250 | << " in instr: " << std::dec << *MI); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 251 | for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) { |
| 252 | MachineOperand &op = MI->getOperand(ii); |
| 253 | if (op.isPCRelativeDisp()) { |
| 254 | // the instruction's branch target is made such that it branches to |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 255 | // PC + (branchTarget * 4), so undo that arithmetic here: |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 256 | // Location is the target of the branch |
| 257 | // Ref is the location of the instruction, and hence the PC |
Misha Brukman | 9cedd43 | 2003-07-03 18:36:47 +0000 | [diff] [blame] | 258 | int64_t branchTarget = (Location - (long)Ref) >> 2; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 259 | // Save the flags. |
Misha Brukman | b5f662f | 2005-04-21 23:30:14 +0000 | [diff] [blame^] | 260 | bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false; |
Alkis Evlogimenos | 4d7af65 | 2003-12-14 13:24:17 +0000 | [diff] [blame] | 261 | if (op.isLoBits32()) { loBits32=true; } |
| 262 | if (op.isHiBits32()) { hiBits32=true; } |
| 263 | if (op.isLoBits64()) { loBits64=true; } |
| 264 | if (op.isHiBits64()) { hiBits64=true; } |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 265 | MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed, |
| 266 | branchTarget); |
Chris Lattner | f4fc36e | 2004-07-19 07:52:35 +0000 | [diff] [blame] | 267 | if (loBits32) { MI->getOperand(ii).markLo32(); } |
| 268 | else if (hiBits32) { MI->getOperand(ii).markHi32(); } |
| 269 | else if (loBits64) { MI->getOperand(ii).markLo64(); } |
| 270 | else if (hiBits64) { MI->getOperand(ii).markHi64(); } |
Misha Brukman | 8f12222 | 2003-06-06 00:26:11 +0000 | [diff] [blame] | 271 | DEBUG(std::cerr << "Rewrote BB ref: "); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 272 | unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI); |
Brian Gaeke | 62c6f87 | 2004-04-23 17:11:15 +0000 | [diff] [blame] | 273 | MCE.emitWordAt (fixedInstr, Ref); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | BBRefs.clear(); |
| 279 | BBLocations.clear(); |
| 280 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Misha Brukman | 0d60345 | 2003-05-27 22:41:44 +0000 | [diff] [blame] | 285 | currBB = MBB.getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 286 | BBLocations[currBB] = MCE.getCurrentPCValue(); |
Chris Lattner | 54a4d6a3 | 2004-11-22 20:25:10 +0000 | [diff] [blame] | 287 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I) |
Chris Lattner | befc374 | 2004-11-22 21:25:10 +0000 | [diff] [blame] | 288 | if (I->getOpcode() != V9::RDCCR) { |
| 289 | emitWord(getBinaryCodeForInstr(*I)); |
| 290 | } else { |
| 291 | // FIXME: The tblgen produced code emitter cannot deal with the fact that |
| 292 | // machine operand #0 of the RDCCR instruction should be ignored. This is |
| 293 | // really a bug in the representation of the RDCCR instruction (which has |
| 294 | // no need to explicitly represent the CCR dest), but we hack around it |
| 295 | // here. |
| 296 | unsigned RegNo = getMachineOpValue(*I, I->getOperand(1)); |
| 297 | RegNo &= (1<<5)-1; |
| 298 | emitWord((RegNo << 25) | 2168487936U); |
| 299 | } |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Chris Lattner | 42c7a16 | 2004-12-16 16:47:56 +0000 | [diff] [blame] | 302 | #include "SparcV9GenCodeEmitter.inc" |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 303 | |