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 | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 57 | inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr); |
| 58 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 59 | private: |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 60 | uint64_t emitStubForFunction(Function *F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 61 | static void CompilationCallback(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 62 | uint64_t resolveFunctionReference(uint64_t RetAddr); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 63 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | JITResolver *TheJITResolver; |
| 67 | } |
| 68 | |
| 69 | /// addFunctionReference - This method is called when we need to emit the |
| 70 | /// address of a function that has not yet been emitted, so we don't know the |
| 71 | /// address. Instead, we emit a call to the CompilationCallback method, and |
| 72 | /// keep track of where we are. |
| 73 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 74 | uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) { |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 75 | LazyCodeGenMap[Address] = F; |
| 76 | return (intptr_t)&JITResolver::CompilationCallback; |
| 77 | } |
| 78 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 79 | uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) { |
| 80 | std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 81 | assert(I != LazyCodeGenMap.end() && "Not in map!"); |
| 82 | Function *F = I->second; |
| 83 | LazyCodeGenMap.erase(I); |
| 84 | return MCE.forceCompilationOf(F); |
| 85 | } |
| 86 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 87 | uint64_t JITResolver::getLazyResolver(Function *F) { |
| 88 | std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 89 | if (I != LazyResolverMap.end() && I->first == F) return I->second; |
| 90 | |
| 91 | //std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n"; |
| 92 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 93 | uint64_t Stub = emitStubForFunction(F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 94 | LazyResolverMap.insert(I, std::make_pair(F, Stub)); |
| 95 | return Stub; |
| 96 | } |
| 97 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 98 | uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) { |
| 99 | |
| 100 | static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2, |
| 101 | i7 = SparcIntRegClass::i7, |
| 102 | o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0; |
| 103 | |
| 104 | // |
| 105 | // Save %i1, %i2 to the stack so we can form a 64-bit constant in %i2 |
| 106 | // |
| 107 | |
| 108 | // stx %i1, [%sp + 2119] ;; save %i1 to the stack, used as temp |
| 109 | MachineInstr *STX = BuildMI(V9::STXi, 3).addReg(i1).addReg(o6).addSImm(2119); |
| 110 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*STX); |
| 111 | delete STX; |
| 112 | Addr += 4; |
| 113 | |
| 114 | // stx %i2, [%sp + 2127] ;; save %i2 to the stack |
| 115 | STX = BuildMI(V9::STXi, 3).addReg(i2).addReg(o6).addSImm(2127); |
| 116 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*STX); |
| 117 | delete STX; |
| 118 | Addr += 4; |
| 119 | |
| 120 | // |
| 121 | // Get address to branch into %i2, using %i1 as a temporary |
| 122 | // |
| 123 | |
| 124 | // sethi %uhi(Target), %i1 ;; get upper 22 bits of Target into %i1 |
| 125 | MachineInstr *SH = BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(i1); |
| 126 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*SH); |
| 127 | delete SH; |
| 128 | Addr += 4; |
| 129 | |
| 130 | // or %i1, %ulo(Target), %i1 ;; get 10 lower bits of upper word into %1 |
| 131 | MachineInstr *OR = BuildMI(V9::ORi, 3) |
| 132 | .addReg(i1).addSImm((Target >> 32) & 0x03ff).addReg(i1); |
| 133 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*OR); |
| 134 | delete OR; |
| 135 | Addr += 4; |
| 136 | |
| 137 | // sllx %i1, 32, %i1 ;; shift those 10 bits to the upper word |
| 138 | MachineInstr *SL = BuildMI(V9::SLLXi6, 3).addReg(i1).addSImm(32).addReg(i1); |
| 139 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*SL); |
| 140 | delete SL; |
| 141 | Addr += 4; |
| 142 | |
| 143 | // sethi %hi(Target), %i2 ;; extract bits 10-31 into the dest reg |
| 144 | SH = BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(i2); |
| 145 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*SH); |
| 146 | delete SH; |
| 147 | Addr += 4; |
| 148 | |
| 149 | // or %i1, %i2, %i2 ;; get upper word (in %i1) into %i2 |
| 150 | OR = BuildMI(V9::ORr, 3).addReg(i1).addReg(i2).addReg(i2); |
| 151 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*OR); |
| 152 | delete OR; |
| 153 | Addr += 4; |
| 154 | |
| 155 | // or %i2, %lo(Target), %i2 ;; get lowest 10 bits of Target into %i2 |
| 156 | OR = BuildMI(V9::ORi, 3).addReg(i2).addSImm(Target & 0x03ff).addReg(i2); |
| 157 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*OR); |
| 158 | delete OR; |
| 159 | Addr += 4; |
| 160 | |
| 161 | // ldx [%sp + 2119], %i1 ;; restore %i1 -> 2119 = BIAS(2047) + 72 |
| 162 | MachineInstr *LDX = BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2119).addReg(i1); |
| 163 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*LDX); |
| 164 | delete LDX; |
| 165 | Addr += 4; |
| 166 | |
| 167 | // jmpl %i2, %g0, %g0 ;; indirect branch on %i2 |
| 168 | MachineInstr *J = BuildMI(V9::JMPLRETr, 3).addReg(i2).addReg(g0).addReg(g0); |
| 169 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*J); |
| 170 | delete J; |
| 171 | Addr += 4; |
| 172 | |
| 173 | // ldx [%sp + 2127], %i2 ;; restore %i2 -> 2127 = BIAS(2047) + 80 |
| 174 | LDX = BuildMI(V9::LDXi, 3).addReg(o6).addSImm(2127).addReg(i2); |
| 175 | *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*LDX); |
| 176 | delete LDX; |
| 177 | Addr += 4; |
| 178 | |
| 179 | return Addr; |
| 180 | } |
| 181 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 182 | void JITResolver::CompilationCallback() { |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 183 | uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0); |
| 184 | int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom); |
| 185 | std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 186 | |
| 187 | // Rewrite the call target... so that we don't fault every time we execute |
| 188 | // the call. |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 189 | #if 0 |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 190 | int64_t RealCallTarget = (int64_t) |
| 191 | ((NewVal - TheJITResolver->getCurrentPCValue()) >> 4); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 192 | if (RealCallTarget >= (1<<22) || RealCallTarget <= -(1<<22)) { |
| 193 | std::cerr << "Address out of bounds for 22bit BA: " << RealCallTarget<<"\n"; |
| 194 | abort(); |
| 195 | } |
| 196 | #endif |
| 197 | |
| 198 | //uint64_t CurrPC = TheJITResolver->getCurrentPCValue(); |
| 199 | // we will insert 9 instructions before we do the actual jump |
| 200 | //int64_t NewTarget = (NewVal - 9*4 - InstAddr) >> 2; |
| 201 | |
| 202 | static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2, |
| 203 | i7 = SparcIntRegClass::i7, o6 = SparcIntRegClass::o6, |
| 204 | o7 = SparcIntRegClass::o7, g0 = SparcIntRegClass::g0; |
| 205 | |
| 206 | // Subtract 4 to overwrite the 'save' that's there now |
| 207 | uint64_t InstAddr = CameFrom-4; |
| 208 | |
| 209 | InstAddr = TheJITResolver->insertFarJumpAtAddr(Target, InstAddr); |
| 210 | |
| 211 | // CODE SHOULD NEVER GO PAST THIS LOAD!! The real function should return to |
| 212 | // the original caller, not here!! |
| 213 | |
| 214 | // FIXME: add call 0 to make sure?!? |
| 215 | |
| 216 | // =============== THE REAL STUB ENDS HERE ========================= |
| 217 | |
| 218 | // What follows below is one-time restore code, because this callback may be |
| 219 | // changing registers in unpredictible ways. However, since it is executed |
| 220 | // only once per function (after the function is resolved, the callback is no |
| 221 | // longer in the path), this has to be done only once. |
| 222 | // |
| 223 | // Thus, it is after the regular stub code. The call back returns to THIS |
| 224 | // point, but every other call to the target function will execute the code |
| 225 | // above. Hence, this code is one-time use. |
| 226 | |
| 227 | uint64_t OneTimeRestore = InstAddr; |
| 228 | |
| 229 | // restore %g0, 0, %g0 |
| 230 | //MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0) |
| 231 | // .addMReg(g0, MOTy::Def); |
| 232 | //*((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*R); |
| 233 | //delete R; |
| 234 | |
| 235 | // FIXME: BuildMI() above crashes. Encode the instruction directly. |
| 236 | // restore %g0, 0, %g0 |
| 237 | *((unsigned*)(intptr_t)InstAddr) = 0x81e82000U; |
| 238 | InstAddr += 4; |
| 239 | |
| 240 | InstAddr = TheJITResolver->insertFarJumpAtAddr(Target, InstAddr); |
| 241 | |
| 242 | // FIXME: if the target function is close enough to fit into the 19bit disp of |
| 243 | // BA, we should use this version, as its much cheaper to generate. |
| 244 | /* |
| 245 | MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(RealCallTarget); |
| 246 | *((unsigned*)(intptr_t)InstAddr) = TheJITResolver->getBinaryCodeForInstr(*MI); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 247 | delete MI; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 248 | InstAddr += 4; |
| 249 | |
| 250 | // Add another NOP |
| 251 | MachineInstr *Nop = BuildMI(V9::NOP, 0); |
| 252 | *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*Nop); |
| 253 | delete Nop; |
| 254 | InstAddr += 4; |
| 255 | |
| 256 | MachineInstr *BA = BuildMI(V9::BA, 1).addSImm(RealCallTarget-2); |
| 257 | *((unsigned*)(intptr_t)InstAddr) = TheJITResolver->getBinaryCodeForInstr(*BA); |
| 258 | delete BA; |
| 259 | */ |
| 260 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 261 | // Change the return address to reexecute the call instruction... |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 262 | // The return address is really %o7, but will disappear after this function |
| 263 | // returns, and the register windows are rotated away. |
| 264 | #if defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
| 265 | __asm__ __volatile__ ("or %%g0, %0, %%i7" : : "r" (OneTimeRestore-8)); |
| 266 | #endif |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /// emitStubForFunction - This method is used by the JIT when it needs to emit |
| 270 | /// the address of a function for a function whose code has not yet been |
| 271 | /// generated. In order to do this, it generates a stub which jumps to the lazy |
| 272 | /// function compiler, which will eventually get fixed to call the function |
| 273 | /// directly. |
| 274 | /// |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 275 | uint64_t JITResolver::emitStubForFunction(Function *F) { |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 276 | MCE.startFunctionStub(*F, 6); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 277 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 278 | std::cerr << "Emitting stub at addr: 0x" |
| 279 | << std::hex << MCE.getCurrentPCValue() << "\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 280 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 281 | unsigned o6 = SparcIntRegClass::o6; |
| 282 | // save %sp, -192, %sp |
| 283 | MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6); |
| 284 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV)); |
| 285 | delete SV; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 286 | |
| 287 | int64_t CurrPC = MCE.getCurrentPCValue(); |
| 288 | int64_t Addr = (int64_t)addFunctionReference(CurrPC, F); |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 289 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 290 | int64_t CallTarget = (Addr-CurrPC) >> 2; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 291 | if (CallTarget >= (1 << 30) || CallTarget <= -(1 << 30)) { |
| 292 | std::cerr << "Call target beyond 30 bit limit of CALL: " <<CallTarget<<"\n"; |
| 293 | abort(); |
| 294 | } |
| 295 | // call CallTarget ;; invoke the callback |
| 296 | MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 297 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call)); |
| 298 | delete Call; |
| 299 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 300 | // nop ;; call delay slot |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 301 | MachineInstr *Nop = BuildMI(V9::NOP, 0); |
| 302 | SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop)); |
| 303 | delete Nop; |
| 304 | |
| 305 | SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub |
| 306 | return (intptr_t)MCE.finishFunctionStub(*F); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 310 | SparcV9CodeEmitter::SparcV9CodeEmitter(TargetMachine &tm, |
| 311 | MachineCodeEmitter &M): TM(tm), MCE(M) |
| 312 | { |
| 313 | TheJITResolver = new JITResolver(*this, M); |
| 314 | } |
| 315 | |
| 316 | SparcV9CodeEmitter::~SparcV9CodeEmitter() { |
| 317 | delete TheJITResolver; |
| 318 | } |
| 319 | |
| 320 | void SparcV9CodeEmitter::emitWord(unsigned Val) { |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 321 | // Output the constant in big endian byte order... |
| 322 | unsigned byteVal; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 323 | for (int i = 3; i >= 0; --i) { |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 324 | byteVal = Val >> 8*i; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 325 | MCE.emitByte(byteVal & 255); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 326 | } |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 329 | bool SparcV9CodeEmitter::isFPInstr(MachineInstr &MI) { |
| 330 | for (unsigned i = 0, e = MI.getNumOperands(); i < e; ++i) { |
| 331 | const MachineOperand &MO = MI.getOperand(i); |
| 332 | if (MO.isPhysicalRegister()) { |
| 333 | unsigned fakeReg = MO.getReg(), realReg, regClass, regType; |
| 334 | regType = TM.getRegInfo().getRegType(fakeReg); |
| 335 | // At least map fakeReg into its class |
| 336 | fakeReg = TM.getRegInfo().getClassRegNum(fakeReg, regClass); |
| 337 | if (regClass == UltraSparcRegInfo::FPSingleRegType || |
| 338 | regClass == UltraSparcRegInfo::FPDoubleRegType) |
| 339 | return true; |
| 340 | } |
| 341 | } |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | unsigned |
| 346 | SparcV9CodeEmitter::getRealRegNum(unsigned fakeReg, unsigned regClass, |
| 347 | MachineInstr &MI) { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 348 | switch (regClass) { |
| 349 | case UltraSparcRegInfo::IntRegType: { |
| 350 | // Sparc manual, p31 |
| 351 | static const unsigned IntRegMap[] = { |
| 352 | // "o0", "o1", "o2", "o3", "o4", "o5", "o7", |
| 353 | 8, 9, 10, 11, 12, 13, 15, |
| 354 | // "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", |
| 355 | 16, 17, 18, 19, 20, 21, 22, 23, |
| 356 | // "i0", "i1", "i2", "i3", "i4", "i5", |
| 357 | 24, 25, 26, 27, 28, 29, |
| 358 | // "i6", "i7", |
| 359 | 30, 31, |
| 360 | // "g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7", |
| 361 | 0, 1, 2, 3, 4, 5, 6, 7, |
| 362 | // "o6" |
| 363 | 14 |
| 364 | }; |
| 365 | |
| 366 | return IntRegMap[fakeReg]; |
| 367 | break; |
| 368 | } |
| 369 | case UltraSparcRegInfo::FPSingleRegType: { |
| 370 | return fakeReg; |
| 371 | } |
| 372 | case UltraSparcRegInfo::FPDoubleRegType: { |
| 373 | return fakeReg; |
| 374 | } |
| 375 | case UltraSparcRegInfo::FloatCCRegType: { |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 376 | /* These are laid out %fcc0 - %fcc3 => 0 - 3, so are correct */ |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 377 | return fakeReg; |
| 378 | |
| 379 | } |
| 380 | case UltraSparcRegInfo::IntCCRegType: { |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 381 | static const unsigned FPInstrIntCCReg[] = { 6 /* xcc */, 4 /* icc */ }; |
| 382 | static const unsigned IntInstrIntCCReg[] = { 2 /* xcc */, 0 /* icc */ }; |
| 383 | |
| 384 | if (isFPInstr(MI)) { |
| 385 | assert(fakeReg < sizeof(FPInstrIntCCReg)/sizeof(FPInstrIntCCReg[0]) |
| 386 | && "Int CC register out of bounds for FPInstr IntCCReg map"); |
| 387 | return FPInstrIntCCReg[fakeReg]; |
| 388 | } else { |
| 389 | assert(fakeReg < sizeof(IntInstrIntCCReg)/sizeof(IntInstrIntCCReg[0]) |
| 390 | && "Int CC register out of bounds for IntInstr IntCCReg map"); |
| 391 | return IntInstrIntCCReg[fakeReg]; |
| 392 | } |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 393 | } |
| 394 | default: |
| 395 | assert(0 && "Invalid unified register number in getRegType"); |
| 396 | return fakeReg; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 400 | int64_t SparcV9CodeEmitter::getMachineOpValue(MachineInstr &MI, |
| 401 | MachineOperand &MO) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 402 | int64_t rv = 0; // Return value; defaults to 0 for unhandled cases |
| 403 | // or things that get fixed up later by the JIT. |
| 404 | |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 405 | if (MO.isVirtualRegister()) { |
| 406 | std::cerr << "ERROR: virtual register found in machine code.\n"; |
| 407 | abort(); |
| 408 | } else if (MO.isPCRelativeDisp()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 409 | std::cerr << "PCRelativeDisp: "; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 410 | Value *V = MO.getVRegValue(); |
| 411 | if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { |
| 412 | std::cerr << "Saving reference to BB (VReg)\n"; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 413 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 414 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 415 | } else if (const Constant *C = dyn_cast<Constant>(V)) { |
| 416 | if (ConstantMap.find(C) != ConstantMap.end()) { |
| 417 | rv = (int64_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
| 418 | std::cerr << "const: 0x" << std::hex << rv |
| 419 | << "\n" << std::dec; |
| 420 | } else { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 421 | std::cerr << "ERROR: constant not in map:" << MO << "\n"; |
| 422 | abort(); |
| 423 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 424 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { |
| 425 | // same as MO.isGlobalAddress() |
| 426 | std::cerr << "GlobalValue: "; |
| 427 | // external function calls, etc.? |
| 428 | if (Function *F = dyn_cast<Function>(GV)) { |
| 429 | std::cerr << "Function: "; |
| 430 | if (F->isExternal()) { |
| 431 | // Sparc backend broken: this MO should be `ExternalSymbol' |
| 432 | rv = (int64_t)MCE.getGlobalValueAddress(F->getName()); |
| 433 | } else { |
| 434 | rv = (int64_t)MCE.getGlobalValueAddress(F); |
| 435 | } |
| 436 | if (rv == 0) { |
| 437 | std::cerr << "not yet generated\n"; |
| 438 | // Function has not yet been code generated! |
| 439 | TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), F); |
| 440 | // Delayed resolution... |
| 441 | rv = TheJITResolver->getLazyResolver(F); |
| 442 | } else { |
| 443 | std::cerr << "already generated: 0x" << std::hex << rv << "\n" |
| 444 | << std::dec; |
| 445 | } |
| 446 | } else { |
| 447 | std::cerr << "not a function: " << *GV << "\n"; |
| 448 | abort(); |
| 449 | } |
| 450 | // The real target of the call is Addr = PC + (rv * 4) |
| 451 | // So undo that: give the instruction (Addr - PC) / 4 |
| 452 | if (MI.getOpcode() == V9::CALL) { |
| 453 | int64_t CurrPC = MCE.getCurrentPCValue(); |
| 454 | std::cerr << "rv addr: 0x" << std::hex << rv << "\n"; |
| 455 | std::cerr << "curr PC: 0x" << CurrPC << "\n"; |
| 456 | rv = (rv - CurrPC) >> 2; |
| 457 | if (rv >= (1<<29) || rv <= -(1<<29)) { |
| 458 | std::cerr << "addr out of bounds for the 30-bit call: " << rv << "\n"; |
| 459 | abort(); |
| 460 | } |
| 461 | std::cerr << "returning addr: 0x" << rv << "\n" << std::dec; |
| 462 | } |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 463 | } else { |
| 464 | std::cerr << "ERROR: PC relative disp unhandled:" << MO << "\n"; |
| 465 | abort(); |
| 466 | } |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 467 | } else if (MO.isPhysicalRegister() || |
| 468 | MO.getType() == MachineOperand::MO_CCRegister) |
| 469 | { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 470 | // This is necessary because the Sparc doesn't actually lay out registers |
| 471 | // in the real fashion -- it skips those that it chooses not to allocate, |
| 472 | // i.e. those that are the SP, etc. |
| 473 | unsigned fakeReg = MO.getReg(), realReg, regClass, regType; |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 474 | regType = TM.getRegInfo().getRegType(fakeReg); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 475 | // At least map fakeReg into its class |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 476 | fakeReg = TM.getRegInfo().getClassRegNum(fakeReg, regClass); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 477 | // Find the real register number for use in an instruction |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 478 | realReg = getRealRegNum(fakeReg, regClass, MI); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 479 | std::cerr << "Reg[" << std::dec << fakeReg << "] = " << realReg << "\n"; |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 480 | rv = realReg; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 481 | } else if (MO.isImmediate()) { |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 482 | rv = MO.getImmedValue(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 483 | std::cerr << "immed: " << rv << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 484 | } else if (MO.isGlobalAddress()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 485 | std::cerr << "GlobalAddress: not PC-relative\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 486 | rv = (int64_t) |
| 487 | (intptr_t)getGlobalAddress(cast<GlobalValue>(MO.getVRegValue()), |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 488 | MI, MO.isPCRelative()); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 489 | } else if (MO.isMachineBasicBlock()) { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 490 | // Duplicate code of the above case for VirtualRegister, BasicBlock... |
| 491 | // It should really hit this case, but Sparc backend uses VRegs instead |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 492 | std::cerr << "Saving reference to MBB\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 493 | BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 494 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 495 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 496 | } else if (MO.isExternalSymbol()) { |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 497 | // Sparc backend doesn't generate this (yet...) |
| 498 | std::cerr << "ERROR: External symbol unhandled: " << MO << "\n"; |
| 499 | abort(); |
| 500 | } else if (MO.isFrameIndex()) { |
| 501 | // Sparc backend doesn't generate this (yet...) |
| 502 | int FrameIndex = MO.getFrameIndex(); |
| 503 | std::cerr << "ERROR: Frame index unhandled.\n"; |
| 504 | abort(); |
| 505 | } else if (MO.isConstantPoolIndex()) { |
| 506 | // Sparc backend doesn't generate this (yet...) |
| 507 | std::cerr << "ERROR: Constant Pool index unhandled.\n"; |
| 508 | abort(); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 509 | } else { |
Misha Brukman | a9f7f6e | 2003-05-30 20:17:33 +0000 | [diff] [blame] | 510 | std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 511 | abort(); |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | // Finally, deal with the various bitfield-extracting functions that |
| 515 | // are used in SPARC assembly. (Some of these make no sense in combination |
| 516 | // with some of the above; we'll trust that the instruction selector |
| 517 | // will not produce nonsense, and not check for valid combinations here.) |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 518 | if (MO.opLoBits32()) { // %lo(val) == %lo() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 519 | return rv & 0x03ff; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 520 | } else if (MO.opHiBits32()) { // %lm(val) == %hi() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 521 | return (rv >> 10) & 0x03fffff; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 522 | } else if (MO.opLoBits64()) { // %hm(val) == %ulo() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 523 | return (rv >> 32) & 0x03ff; |
Misha Brukman | f47d9c2 | 2003-06-05 20:52:06 +0000 | [diff] [blame^] | 524 | } else if (MO.opHiBits64()) { // %hh(val) == %uhi() in Sparc ABI doc |
Brian Gaeke | c3eaa89 | 2003-06-02 02:13:26 +0000 | [diff] [blame] | 525 | return rv >> 42; |
| 526 | } else { // (unadorned) val |
| 527 | return rv; |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) { |
| 532 | Val >>= bit; |
| 533 | return (Val & 1); |
| 534 | } |
| 535 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 536 | bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 537 | MCE.startFunction(MF); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 538 | std::cerr << "Starting function " << MF.getFunction()->getName() |
| 539 | << ", address: " << "0x" << std::hex |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 540 | << (long)MCE.getCurrentPCValue() << "\n"; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 541 | |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 542 | // The Sparc backend does not use MachineConstantPool; |
| 543 | // instead, it has its own constant pool implementation. |
| 544 | // We create a new MachineConstantPool here to be compatible with the emitter. |
| 545 | MachineConstantPool MCP; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 546 | const hash_set<const Constant*> &pool = MF.getInfo()->getConstantPoolValues(); |
| 547 | for (hash_set<const Constant*>::const_iterator I = pool.begin(), |
| 548 | E = pool.end(); I != E; ++I) |
| 549 | { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 550 | Constant *C = (Constant*)*I; |
| 551 | unsigned idx = MCP.getConstantPoolIndex(C); |
| 552 | std::cerr << "Mapping constant 0x" << (intptr_t)C << " to " << idx << "\n"; |
| 553 | ConstantMap[C] = idx; |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 554 | } |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 555 | MCE.emitConstantPool(&MCP); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 556 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 557 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 558 | emitBasicBlock(*I); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 559 | MCE.finishFunction(MF); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 560 | |
| 561 | std::cerr << "Finishing function " << MF.getFunction()->getName() << "\n"; |
| 562 | ConstantMap.clear(); |
| 563 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 564 | long Location = BBLocations[BBRefs[i].first]; |
| 565 | unsigned *Ref = BBRefs[i].second.first; |
| 566 | MachineInstr *MI = BBRefs[i].second.second; |
| 567 | std::cerr << "Fixup @" << std::hex << Ref << " to " << Location |
| 568 | << " in instr: " << std::dec << *MI << "\n"; |
| 569 | } |
| 570 | |
| 571 | // Resolve branches to BasicBlocks for the entire function |
| 572 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 573 | long Location = BBLocations[BBRefs[i].first]; |
| 574 | unsigned *Ref = BBRefs[i].second.first; |
| 575 | MachineInstr *MI = BBRefs[i].second.second; |
| 576 | std::cerr << "attempting to resolve BB: " << i << "\n"; |
| 577 | for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) { |
| 578 | MachineOperand &op = MI->getOperand(ii); |
| 579 | if (op.isPCRelativeDisp()) { |
| 580 | // the instruction's branch target is made such that it branches to |
| 581 | // PC + (br target * 4), so undo that arithmetic here: |
| 582 | // Location is the target of the branch |
| 583 | // Ref is the location of the instruction, and hence the PC |
| 584 | unsigned branchTarget = (Location - (long)Ref) >> 2; |
| 585 | // Save the flags. |
| 586 | bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false; |
| 587 | if (op.opLoBits32()) { loBits32=true; } |
| 588 | if (op.opHiBits32()) { hiBits32=true; } |
| 589 | if (op.opLoBits64()) { loBits64=true; } |
| 590 | if (op.opHiBits64()) { hiBits64=true; } |
| 591 | MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed, |
| 592 | branchTarget); |
| 593 | if (loBits32) { MI->setOperandLo32(ii); } |
| 594 | else if (hiBits32) { MI->setOperandHi32(ii); } |
| 595 | else if (loBits64) { MI->setOperandLo64(ii); } |
| 596 | else if (hiBits64) { MI->setOperandHi64(ii); } |
| 597 | std::cerr << "Rewrote BB ref: "; |
| 598 | unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI); |
| 599 | *Ref = fixedInstr; |
| 600 | break; |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | BBRefs.clear(); |
| 605 | BBLocations.clear(); |
| 606 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 607 | return false; |
| 608 | } |
| 609 | |
| 610 | void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Misha Brukman | 0d60345 | 2003-05-27 22:41:44 +0000 | [diff] [blame] | 611 | currBB = MBB.getBasicBlock(); |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 612 | BBLocations[currBB] = MCE.getCurrentPCValue(); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 613 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I) |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 614 | emitWord(getBinaryCodeForInstr(**I)); |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 617 | void* SparcV9CodeEmitter::getGlobalAddress(GlobalValue *V, MachineInstr &MI, |
| 618 | bool isPCRelative) |
| 619 | { |
| 620 | if (isPCRelative) { // must be a call, this is a major hack! |
| 621 | // Try looking up the function to see if it is already compiled! |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 622 | if (void *Addr = (void*)(intptr_t)MCE.getGlobalValueAddress(V)) { |
| 623 | intptr_t CurByte = MCE.getCurrentPCValue(); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 624 | // The real target of the call is Addr = PC + (target * 4) |
| 625 | // CurByte is the PC, Addr we just received |
| 626 | return (void*) (((long)Addr - (long)CurByte) >> 2); |
| 627 | } else { |
| 628 | if (Function *F = dyn_cast<Function>(V)) { |
| 629 | // Function has not yet been code generated! |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 630 | TheJITResolver->addFunctionReference(MCE.getCurrentPCValue(), |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 631 | cast<Function>(V)); |
| 632 | // Delayed resolution... |
Misha Brukman | eaaf8ad | 2003-06-02 05:24:46 +0000 | [diff] [blame] | 633 | return |
| 634 | (void*)(intptr_t)TheJITResolver->getLazyResolver(cast<Function>(V)); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 635 | |
| 636 | } else if (Constant *C = ConstantPointerRef::get(V)) { |
| 637 | if (ConstantMap.find(C) != ConstantMap.end()) { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 638 | return (void*) |
| 639 | (intptr_t)MCE.getConstantPoolEntryAddress(ConstantMap[C]); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 640 | } else { |
| 641 | std::cerr << "Constant: 0x" << std::hex << &*C << std::dec |
| 642 | << ", " << *V << " not found in ConstantMap!\n"; |
| 643 | abort(); |
| 644 | } |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 645 | } else { |
| 646 | std::cerr << "Unhandled global: " << *V << "\n"; |
| 647 | abort(); |
| 648 | } |
| 649 | } |
| 650 | } else { |
Misha Brukman | a2196c1 | 2003-06-04 20:01:13 +0000 | [diff] [blame] | 651 | return (void*)(intptr_t)MCE.getGlobalValueAddress(V); |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
| 655 | |
Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame] | 656 | #include "SparcV9CodeEmitter.inc" |
Misha Brukman | f86aaa8 | 2003-06-02 04:12:39 +0000 | [diff] [blame] | 657 | |