Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 1 | //===-- SparcV9CodeEmitter.cpp - --------===// |
| 2 | // |
| 3 | // |
| 4 | //===----------------------------------------------------------------------===// |
| 5 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 6 | #include "llvm/Constants.h" |
| 7 | #include "llvm/Function.h" |
| 8 | #include "llvm/GlobalVariable.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 9 | #include "llvm/PassManager.h" |
| 10 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 11 | #include "llvm/CodeGen/MachineConstantPool.h" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineFunctionInfo.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 14 | #include "llvm/CodeGen/MachineInstr.h" |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 15 | #include "llvm/Target/TargetMachine.h" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 16 | #include "llvm/Target/TargetData.h" |
| 17 | #include "Support/hash_set" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 18 | #include "SparcInternals.h" |
Misha Brukman | 0cc640e | 2003-05-27 21:45:05 +0000 | [diff] [blame] | 19 | #include "SparcV9CodeEmitter.h" |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 20 | |
| 21 | bool UltraSparc::addPassesToEmitMachineCode(PassManager &PM, |
| 22 | MachineCodeEmitter &MCE) { |
| 23 | //PM.add(new SparcV9CodeEmitter(MCE)); |
| 24 | //MachineCodeEmitter *M = MachineCodeEmitter::createDebugMachineCodeEmitter(); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 25 | MachineCodeEmitter *M = MachineCodeEmitter::createFilePrinterEmitter(MCE); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 26 | PM.add(new SparcV9CodeEmitter(*this, *M)); |
Misha Brukman | dcbe712 | 2003-05-31 06:26:06 +0000 | [diff] [blame] | 27 | PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 28 | return false; |
| 29 | } |
| 30 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class JITResolver { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 33 | SparcV9CodeEmitter &SparcV9; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 34 | MachineCodeEmitter &MCE; |
| 35 | |
| 36 | // LazyCodeGenMap - Keep track of call sites for functions that are to be |
| 37 | // lazily resolved. |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 38 | std::map<uint64_t, Function*> LazyCodeGenMap; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 39 | |
| 40 | // LazyResolverMap - Keep track of the lazy resolver created for a |
| 41 | // particular function so that we can reuse them if necessary. |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 42 | std::map<Function*, uint64_t> LazyResolverMap; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 43 | public: |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 44 | JITResolver(SparcV9CodeEmitter &V9, |
| 45 | MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {} |
| 46 | uint64_t getLazyResolver(Function *F); |
| 47 | uint64_t addFunctionReference(uint64_t Address, Function *F); |
| 48 | |
| 49 | // Utility functions for accessing data from static callback |
| 50 | uint64_t getCurrentPCValue() { |
| 51 | return MCE.getCurrentPCValue(); |
| 52 | } |
| 53 | unsigned getBinaryCodeForInstr(MachineInstr &MI) { |
| 54 | return SparcV9.getBinaryCodeForInstr(MI); |
| 55 | } |
| 56 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 57 | private: |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 58 | uint64_t emitStubForFunction(Function *F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 59 | static void CompilationCallback(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 60 | uint64_t resolveFunctionReference(uint64_t RetAddr); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | JITResolver *TheJITResolver; |
| 64 | } |
| 65 | |
| 66 | /// addFunctionReference - This method is called when we need to emit the |
| 67 | /// address of a function that has not yet been emitted, so we don't know the |
| 68 | /// address. Instead, we emit a call to the CompilationCallback method, and |
| 69 | /// keep track of where we are. |
| 70 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 71 | uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) { |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 72 | LazyCodeGenMap[Address] = F; |
| 73 | return (intptr_t)&JITResolver::CompilationCallback; |
| 74 | } |
| 75 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 76 | uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) { |
| 77 | std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 78 | assert(I != LazyCodeGenMap.end() && "Not in map!"); |
| 79 | Function *F = I->second; |
| 80 | LazyCodeGenMap.erase(I); |
| 81 | return MCE.forceCompilationOf(F); |
| 82 | } |
| 83 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 84 | uint64_t JITResolver::getLazyResolver(Function *F) { |
| 85 | std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 86 | if (I != LazyResolverMap.end() && I->first == F) return I->second; |
| 87 | |
| 88 | //std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n"; |
| 89 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 90 | uint64_t Stub = emitStubForFunction(F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 91 | LazyResolverMap.insert(I, std::make_pair(F, Stub)); |
| 92 | return Stub; |
| 93 | } |
| 94 | |
| 95 | void JITResolver::CompilationCallback() { |
| 96 | uint64_t *StackPtr = (uint64_t*)__builtin_frame_address(0); |
| 97 | uint64_t RetAddr = (uint64_t)(intptr_t)__builtin_return_address(0); |
| 98 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 99 | std::cerr << "In callback! Addr=0x" << std::hex << RetAddr |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 100 | << " SP=0x" << (uint64_t)(intptr_t)StackPtr << std::dec << "\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 101 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 102 | int64_t NewVal = (int64_t)TheJITResolver->resolveFunctionReference(RetAddr); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 103 | |
| 104 | // Rewrite the call target... so that we don't fault every time we execute |
| 105 | // the call. |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 106 | int64_t RealCallTarget = (int64_t) |
| 107 | ((NewVal - TheJITResolver->getCurrentPCValue()) >> 4); |
| 108 | MachineInstr *MI = BuildMI(V9::CALL, 1); |
| 109 | MI->addSignExtImmOperand(RealCallTarget); |
| 110 | // FIXME: this could be in the wrong byte order!! |
| 111 | *((unsigned*)(intptr_t)RetAddr) = TheJITResolver->getBinaryCodeForInstr(*MI); |
| 112 | delete MI; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 113 | |
| 114 | // Change the return address to reexecute the call instruction... |
| 115 | StackPtr[1] -= 4; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /// emitStubForFunction - This method is used by the JIT when it needs to emit |
| 119 | /// the address of a function for a function whose code has not yet been |
| 120 | /// generated. In order to do this, it generates a stub which jumps to the lazy |
| 121 | /// function compiler, which will eventually get fixed to call the function |
| 122 | /// directly. |
| 123 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 124 | uint64_t JITResolver::emitStubForFunction(Function *F) { |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 125 | #if 0 |
| 126 | MCE.startFunctionStub(*F, 6); |
| 127 | MCE.emitByte(0xE8); // Call with 32 bit pc-rel destination... |
| 128 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 129 | uint64_t Address = addFunctionReference(MCE.getCurrentPCValue(), F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 130 | MCE.emitWord(Address-MCE.getCurrentPCValue()-4); |
| 131 | |
| 132 | MCE.emitByte(0xCD); // Interrupt - Just a marker identifying the stub! |
| 133 | return (intptr_t)MCE.finishFunctionStub(*F); |
| 134 | #endif |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 135 | MCE.startFunctionStub(*F, 6); |
| 136 | |
| 137 | int64_t CurrPC = MCE.getCurrentPCValue(); |
| 138 | int64_t Addr = (int64_t)addFunctionReference(CurrPC, F); |
| 139 | int64_t CallTarget = (Addr-CurrPC) >> 2; |
| 140 | MachineInstr *Call = BuildMI(V9::CALL, 1); |
| 141 | Call->addSignExtImmOperand(CallTarget); |
| 142 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call)); |
| 143 | delete Call; |
| 144 | |
| 145 | MachineInstr *Nop = BuildMI(V9::NOP, 0); |
| 146 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop)); |
| 147 | delete Nop; |
| 148 | |
| 149 | SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub |
| 150 | return (intptr_t)MCE.finishFunctionStub(*F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 154 | SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm, |
| 155 | MachineCodeEmitter &M): TM(tm), MCE(M) |
| 156 | { |
| 157 | TheJITResolver = new JITResolver(*this, M); |
| 158 | } |
| 159 | |
| 160 | SparcV9CodeEmitter::~SparcV9CodeEmitter() { |
| 161 | delete TheJITResolver; |
| 162 | } |
| 163 | |
| 164 | void SparcV9CodeEmitter::emitWord(unsigned Val) { |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 165 | // Output the constant in big endian byte order... |
| 166 | unsigned byteVal; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 167 | for (int i = 3; i >= 0; --i) { |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 168 | byteVal = Val >> 8*i; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 169 | MCE.emitByte(byteVal & 255); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 170 | } |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | unsigned getRealRegNum(unsigned fakeReg, unsigned regClass) { |
| 174 | switch (regClass) { |
| 175 | case UltraSparcRegInfo::IntRegType: { |
| 176 | // Sparc manual, p31 |
| 177 | static const unsigned IntRegMap[] = { |
| 178 | // "o0", "o1", "o2", "o3", "o4", "o5", "o7", |
| 179 | 8, 9, 10, 11, 12, 13, 15, |
| 180 | // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", |
| 181 | 16, 17, 18, 19, 20, 21, 22, 23, |
| 182 | // "i0", "i1", "i2", "i3", "i4", "i5", |
| 183 | 24, 25, 26, 27, 28, 29, |
| 184 | // "i6", "i7", |
| 185 | 30, 31, |
| 186 | // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", |
| 187 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 188 | // "o6" |
| 189 | 14 |
| 190 | }; |
| 191 | |
| 192 | return IntRegMap[fakeReg]; |
| 193 | break; |
| 194 | } |
| 195 | case UltraSparcRegInfo::FPSingleRegType: { |
| 196 | return fakeReg; |
| 197 | } |
| 198 | case UltraSparcRegInfo::FPDoubleRegType: { |
| 199 | return fakeReg; |
| 200 | } |
| 201 | case UltraSparcRegInfo::FloatCCRegType: { |
| 202 | return fakeReg; |
| 203 | |
| 204 | } |
| 205 | case UltraSparcRegInfo::IntCCRegType: { |
| 206 | return fakeReg; |
| 207 | } |
| 208 | default: |
| 209 | assert(0 && "Invalid unified register number in getRegType"); |
| 210 | return fakeReg; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 214 | int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI, |
| 215 | MachineOperand &MO) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 216 | int64_t rv = 0; // Return value; defaults to 0 for unhandled cases |
| 217 | // or things that get fixed up later by the JIT. |
| 218 | |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 219 | if (MO.isVirtualRegister()) { |
| 220 | std::cerr << "ERROR: virtual register found in machine code.\n"; |
| 221 | abort(); |
| 222 | } else if (MO.isPCRelativeDisp()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 223 | std::cerr << "PCRelativeDisp: "; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 224 | Value *V = MO.getVRegValue(); |
| 225 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
| 226 | std::cerr << "Saving reference to BB (VReg)\n"; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 227 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 228 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 229 | } else if (const Constant *C = dyn_cast<Constant>(V)) { |
| 230 | if (ConstantMap.find(C) != ConstantMap.end()) { |
| 231 | rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
| 232 | std::cerr << "const: 0x" << std::hex << rv |
| 233 | << "\n" << std::dec; |
| 234 | } else { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 235 | std::cerr << "ERROR: constant not in map:" << MO << "\n"; |
| 236 | abort(); |
| 237 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 238 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 239 | // same as MO.isGlobalAddress() |
| 240 | std::cerr << "GlobalValue: "; |
| 241 | // external function calls, etc.? |
| 242 | if (Function *F = dyn_cast<Function>(GV)) { |
| 243 | std::cerr << "Function: "; |
| 244 | if (F->isExternal()) { |
| 245 | // Sparc backend broken: this MO should be `ExternalSymbol' |
| 246 | rv = (int64_t)MCE.getGlobalValueAddress(F->getName()); |
| 247 | } else { |
| 248 | rv = (int64_t)MCE.getGlobalValueAddress(F); |
| 249 | } |
| 250 | if (rv == 0) { |
| 251 | std::cerr << "not yet generated\n"; |
| 252 | // Function has not yet been code generated! |
| 253 | TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F); |
| 254 | // Delayed resolution... |
| 255 | rv = TheJITResolver->getLazyResolver(F); |
| 256 | } else { |
| 257 | std::cerr << "already generated: 0x" << std::hex << rv << "\n" |
| 258 | << std::dec; |
| 259 | } |
| 260 | } else { |
| 261 | std::cerr << "not a function: " << *GV << "\n"; |
| 262 | abort(); |
| 263 | } |
| 264 | // The real target of the call is Addr = PC + (rv * 4) |
| 265 | // So undo that: give the instruction (Addr - PC) / 4 |
| 266 | if (MI.getOpcode() == V9::CALL) { |
| 267 | int64_t CurrPC = MCE.getCurrentPCValue(); |
| 268 | std::cerr << "rv addr: 0x" << std::hex << rv << "\n"; |
| 269 | std::cerr << "curr PC: 0x" << CurrPC << "\n"; |
| 270 | rv = (rv - CurrPC) >> 2; |
| 271 | if (rv >= (1<<29) || rv <= -(1<<29)) { |
| 272 | std::cerr << "addr out of bounds for the 30-bit call: " << rv << "\n"; |
| 273 | abort(); |
| 274 | } |
| 275 | std::cerr << "returning addr: 0x" << rv << "\n" << std::dec; |
| 276 | } |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 277 | } else { |
| 278 | std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n"; |
| 279 | abort(); |
| 280 | } |
| 281 | } else if (MO.isPhysicalRegister()) { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 282 | // This is necessary because the Sparc doesn't actually lay out registers |
| 283 | // in the real fashion -- it skips those that it chooses not to allocate, |
| 284 | // i.e. those that are the SP, etc. |
| 285 | unsigned fakeReg = MO.getReg(), realReg, regClass, regType; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 286 | regType = TM.getRegInfo().getRegType(fakeReg); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 287 | // At least map fakeReg into its class |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 288 | fakeReg = TM.getRegInfo().getClassRegNum(fakeReg, regClass); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 289 | // Find the real register number for use in an instruction |
| 290 | realReg = getRealRegNum(fakeReg, regClass); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 291 | std::cerr << "Reg[" << std::dec << fakeReg << "] = " << realReg << "\n"; |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 292 | rv = realReg; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 293 | } else if (MO.isImmediate()) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 294 | rv = MO.getImmedValue(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 295 | std::cerr << "immed: " << rv << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 296 | } else if (MO.isGlobalAddress()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 297 | std::cerr << "GlobalAddress: not PC-relative\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 298 | rv = (int64_t) |
| 299 | (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()), |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 300 | MI, MO.isPCRelative()); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 301 | } else if (MO.isMachineBasicBlock()) { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 302 | // Duplicate code of the above case for VirtualRegister, BasicBlock... |
| 303 | // It should really hit this case, but Sparc backend uses VRegs instead |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 304 | std::cerr << "Saving reference to MBB\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 305 | BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 306 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 307 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 308 | } else if (MO.isExternalSymbol()) { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 309 | // Sparc backend doesn't generate this (yet...) |
| 310 | std::cerr << "ERROR: External symbol unhandled: " << MO << "\n"; |
| 311 | abort(); |
| 312 | } else if (MO.isFrameIndex()) { |
| 313 | // Sparc backend doesn't generate this (yet...) |
| 314 | int FrameIndex = MO.getFrameIndex(); |
| 315 | std::cerr << "ERROR: Frame index unhandled.\n"; |
| 316 | abort(); |
| 317 | } else if (MO.isConstantPoolIndex()) { |
| 318 | // Sparc backend doesn't generate this (yet...) |
| 319 | std::cerr << "ERROR: Constant Pool index unhandled.\n"; |
| 320 | abort(); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 321 | } else { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 322 | std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 323 | abort(); |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // Finally, deal with the various bitfield-extracting functions that |
| 327 | // are used in SPARC assembly. (Some of these make no sense in combination |
| 328 | // with some of the above; we'll trust that the instruction selector |
| 329 | // will not produce nonsense, and not check for valid combinations here.) |
| 330 | if (MO.opLoBits32()) { // %lo(val) |
| 331 | return rv & 0x03ff; |
| 332 | } else if (MO.opHiBits32()) { // %lm(val) |
| 333 | return (rv >> 10) & 0x03fffff; |
| 334 | } else if (MO.opLoBits64()) { // %hm(val) |
| 335 | return (rv >> 32) & 0x03ff; |
| 336 | } else if (MO.opHiBits64()) { // %hh(val) |
| 337 | return rv >> 42; |
| 338 | } else { // (unadorned) val |
| 339 | return rv; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
| 343 | unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) { |
| 344 | Val >>= bit; |
| 345 | return (Val & 1); |
| 346 | } |
| 347 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 348 | bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 349 | MCE.startFunction(MF); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 350 | std::cerr << "Starting function " << MF.getFunction()->getName() |
| 351 | << ", address: " << "0x" << std::hex |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 352 | << (long)MCE.getCurrentPCValue() << "\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 353 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 354 | // The Sparc backend does not use MachineConstantPool; |
| 355 | // instead, it has its own constant pool implementation. |
| 356 | // We create a new MachineConstantPool here to be compatible with the emitter. |
| 357 | MachineConstantPool MCP; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 358 | const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues(); |
| 359 | for (hash_set<const Constant*>::const_iterator I = pool.begin(), |
| 360 | E = pool.end(); I != E; ++I) |
| 361 | { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 362 | Constant *C = (Constant*)*I; |
| 363 | unsigned idx = MCP.getConstantPoolIndex(C); |
| 364 | std::cerr << "Mapping constant 0x" << (intptr_t)C << " to " << idx << "\n"; |
| 365 | ConstantMap[C] = idx; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 366 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 367 | MCE.emitConstantPool(&MCP); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 368 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 369 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 370 | emitBasicBlock(*I); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 371 | MCE.finishFunction(MF); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 372 | |
| 373 | std::cerr << "Finishing function " << MF.getFunction()->getName() << "\n"; |
| 374 | ConstantMap.clear(); |
| 375 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 376 | long Location = BBLocations[BBRefs[i].first]; |
| 377 | unsigned *Ref = BBRefs[i].second.first; |
| 378 | MachineInstr *MI = BBRefs[i].second.second; |
| 379 | std::cerr << "Fixup @" << std::hex << Ref << " to " << Location |
| 380 | << " in instr: " << std::dec << *MI << "\n"; |
| 381 | } |
| 382 | |
| 383 | // Resolve branches to BasicBlocks for the entire function |
| 384 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 385 | long Location = BBLocations[BBRefs[i].first]; |
| 386 | unsigned *Ref = BBRefs[i].second.first; |
| 387 | MachineInstr *MI = BBRefs[i].second.second; |
| 388 | std::cerr << "attempting to resolve BB: " << i << "\n"; |
| 389 | for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) { |
| 390 | MachineOperand &op = MI->getOperand(ii); |
| 391 | if (op.isPCRelativeDisp()) { |
| 392 | // the instruction's branch target is made such that it branches to |
| 393 | // PC + (br target * 4), so undo that arithmetic here: |
| 394 | // Location is the target of the branch |
| 395 | // Ref is the location of the instruction, and hence the PC |
| 396 | unsigned branchTarget = (Location - (long)Ref) >> 2; |
| 397 | // Save the flags. |
| 398 | bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false; |
| 399 | if (op.opLoBits32()) { loBits32=true; } |
| 400 | if (op.opHiBits32()) { hiBits32=true; } |
| 401 | if (op.opLoBits64()) { loBits64=true; } |
| 402 | if (op.opHiBits64()) { hiBits64=true; } |
| 403 | MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed, |
| 404 | branchTarget); |
| 405 | if (loBits32) { MI->setOperandLo32(ii); } |
| 406 | else if (hiBits32) { MI->setOperandHi32(ii); } |
| 407 | else if (loBits64) { MI->setOperandLo64(ii); } |
| 408 | else if (hiBits64) { MI->setOperandHi64(ii); } |
| 409 | std::cerr << "Rewrote BB ref: "; |
| 410 | unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI); |
| 411 | *Ref = fixedInstr; |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | BBRefs.clear(); |
| 417 | BBLocations.clear(); |
| 418 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | |
| 422 | void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Misha Brukman | 0d60345 | 2003-05-27 22:41:44 +0000 | [diff] [blame] | 423 | currBB = MBB.getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 424 | BBLocations[currBB] = MCE.getCurrentPCValue(); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 425 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I) |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 426 | emitWord(getBinaryCodeForInstr(**I)); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 429 | void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI, |
| 430 | bool isPCRelative) |
| 431 | { |
| 432 | if (isPCRelative) { // must be a call, this is a major hack! |
| 433 | // Try looking up the function to see if it is already compiled! |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 434 | if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) { |
| 435 | intptr_t CurByte = MCE.getCurrentPCValue(); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 436 | // The real target of the call is Addr = PC + (target * 4) |
| 437 | // CurByte is the PC, Addr we just received |
| 438 | return (void*) (((long)Addr - (long)CurByte) >> 2); |
| 439 | } else { |
| 440 | if (Function *F = dyn_cast<Function>(V)) { |
| 441 | // Function has not yet been code generated! |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 442 | TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 443 | cast<Function>(V)); |
| 444 | // Delayed resolution... |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 445 | return |
| 446 | (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V)); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 447 | |
| 448 | } else if (Constant *C = ConstantPointerRef::get(V)) { |
| 449 | if (ConstantMap.find(C) != ConstantMap.end()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 450 | return (void*) |
| 451 | (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 452 | } else { |
| 453 | std::cerr << "Constant: 0x" << std::hex << &*C << std::dec |
| 454 | << ", " << *V << " not found in ConstantMap!\n"; |
| 455 | abort(); |
| 456 | } |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 457 | } else { |
| 458 | std::cerr << "Unhandled global: " << *V << "\n"; |
| 459 | abort(); |
| 460 | } |
| 461 | } |
| 462 | } else { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame^] | 463 | return (void*)(intptr_t)MCE.getGlobalValueAddress(V); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 468 | #include "SparcV9CodeEmitter.inc" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 469 | |