blob: 227d5ba846937139fb8b4dfdce3fd7d0794282d2 [file] [log] [blame]
Misha Brukman3d9a6c22004-08-11 00:09:42 +00001//===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// 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 Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00009//
Misha Brukmand37faba2004-10-14 06:07:25 +000010// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11// JIT-compile bytecode to native PowerPC.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000012//
13//===----------------------------------------------------------------------===//
14
Misha Brukman3d9a6c22004-08-11 00:09:42 +000015#include "PPC32TargetMachine.h"
Chris Lattnere94c5172004-11-23 05:59:53 +000016#include "PPC32Relocations.h"
Misha Brukmana4df3502004-10-23 18:28:01 +000017#include "PowerPC.h"
Misha Brukman3070e2f2004-10-21 01:42:02 +000018#include "llvm/Module.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000019#include "llvm/CodeGen/MachineCodeEmitter.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmand4b4a992004-10-23 23:47:34 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000022#include "llvm/CodeGen/Passes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/Support/Debug.h"
Chris Lattnereea9b132004-11-16 04:47:33 +000024using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000025
Misha Brukmanb05daff2004-08-09 23:03:59 +000026namespace {
Misha Brukman3d9a6c22004-08-11 00:09:42 +000027 class PPC32CodeEmitter : public MachineFunctionPass {
Misha Brukmanb05daff2004-08-09 23:03:59 +000028 TargetMachine &TM;
29 MachineCodeEmitter &MCE;
30
Misha Brukman3070e2f2004-10-21 01:42:02 +000031 // Tracks which instruction references which BasicBlock
Chris Lattnerb752a972004-11-25 00:33:57 +000032 std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
Misha Brukman3070e2f2004-10-21 01:42:02 +000033 // Tracks where each BasicBlock starts
Chris Lattnerb752a972004-11-25 00:33:57 +000034 std::map<MachineBasicBlock*, long> BBLocations;
Misha Brukman3070e2f2004-10-21 01:42:02 +000035
36 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
37 ///
Chris Lattnere94c5172004-11-23 05:59:53 +000038 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
Misha Brukman3070e2f2004-10-21 01:42:02 +000039
Misha Brukmanb05daff2004-08-09 23:03:59 +000040 public:
Misha Brukmanb5f662f2005-04-21 23:30:14 +000041 PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
Misha Brukmanb05daff2004-08-09 23:03:59 +000042 : TM(T), MCE(M) {}
43
44 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
45
46 /// runOnMachineFunction - emits the given MachineFunction to memory
47 ///
48 bool runOnMachineFunction(MachineFunction &MF);
49
50 /// emitBasicBlock - emits the given MachineBasicBlock to memory
51 ///
52 void emitBasicBlock(MachineBasicBlock &MBB);
53
54 /// emitWord - write a 32-bit word to memory at the current PC
55 ///
56 void emitWord(unsigned w) { MCE.emitWord(w); }
Misha Brukmanb5f662f2005-04-21 23:30:14 +000057
Misha Brukmand37faba2004-10-14 06:07:25 +000058 /// getValueBit - return the particular bit of Val
59 ///
60 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Misha Brukmanb05daff2004-08-09 23:03:59 +000061
Misha Brukman3070e2f2004-10-21 01:42:02 +000062 /// getBinaryCodeForInstr - This function, generated by the
63 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
64 /// machine instructions.
Misha Brukmanb05daff2004-08-09 23:03:59 +000065 ///
Misha Brukmand37faba2004-10-14 06:07:25 +000066 unsigned getBinaryCodeForInstr(MachineInstr &MI);
Misha Brukmanb05daff2004-08-09 23:03:59 +000067 };
68}
69
Misha Brukman5dfe3a92004-06-21 16:55:25 +000070/// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
71/// machine code emitted. This uses a MachineCodeEmitter object to handle
72/// actually outputting the machine code and resolving things like the address
73/// of functions. This method should returns true if machine code emission is
74/// not supported.
75///
Misha Brukman3d9a6c22004-08-11 00:09:42 +000076bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
77 MachineCodeEmitter &MCE) {
Misha Brukmanb05daff2004-08-09 23:03:59 +000078 // Machine code emitter pass for PowerPC
Misha Brukmanb5f662f2005-04-21 23:30:14 +000079 PM.add(new PPC32CodeEmitter(*this, MCE));
Misha Brukmand37faba2004-10-14 06:07:25 +000080 // Delete machine code for this function after emitting it
Misha Brukmanb05daff2004-08-09 23:03:59 +000081 PM.add(createMachineCodeDeleter());
Misha Brukmanc982cfa2004-10-14 06:39:56 +000082 return false;
Misha Brukmanb05daff2004-08-09 23:03:59 +000083}
84
Misha Brukman3d9a6c22004-08-11 00:09:42 +000085bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmanb05daff2004-08-09 23:03:59 +000086 MCE.startFunction(MF);
87 MCE.emitConstantPool(MF.getConstantPool());
Misha Brukman3070e2f2004-10-21 01:42:02 +000088 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
89 emitBasicBlock(*BB);
Misha Brukmanb05daff2004-08-09 23:03:59 +000090 MCE.finishFunction(MF);
Misha Brukman3070e2f2004-10-21 01:42:02 +000091
92 // Resolve branches to BasicBlocks for the entire function
93 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
Chris Lattnere94c5172004-11-23 05:59:53 +000094 intptr_t Location = BBLocations[BBRefs[i].first];
Chris Lattner8599d382004-11-24 01:35:12 +000095 unsigned *Ref = BBRefs[i].second;
Chris Lattnere94c5172004-11-23 05:59:53 +000096 DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
Chris Lattner8599d382004-11-24 01:35:12 +000097 << "\n");
98 unsigned Instr = *Ref;
99 intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000100
Chris Lattner8599d382004-11-24 01:35:12 +0000101 switch (Instr >> 26) {
102 default: assert(0 && "Unknown branch user!");
103 case 18: // This is B or BL
104 *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
105 break;
106 case 16: // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
107 *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
108 break;
Misha Brukman3070e2f2004-10-21 01:42:02 +0000109 }
110 }
111 BBRefs.clear();
112 BBLocations.clear();
113
Misha Brukmanb05daff2004-08-09 23:03:59 +0000114 return false;
115}
116
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000117void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Nate Begeman2497e632005-07-21 20:44:43 +0000118 assert(!PICEnabled && "CodeEmitter does not support PIC!");
Chris Lattnerb752a972004-11-25 00:33:57 +0000119 BBLocations[&MBB] = MCE.getCurrentPCValue();
Misha Brukmana4df3502004-10-23 18:28:01 +0000120 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
121 MachineInstr &MI = *I;
122 unsigned Opcode = MI.getOpcode();
Chris Lattnere94c5172004-11-23 05:59:53 +0000123 switch (MI.getOpcode()) {
124 default:
Misha Brukmand4b4a992004-10-23 23:47:34 +0000125 emitWord(getBinaryCodeForInstr(*I));
Chris Lattnere94c5172004-11-23 05:59:53 +0000126 break;
127 case PPC::IMPLICIT_DEF:
128 break; // pseudo opcode, no side effects
129 case PPC::MovePCtoLR:
Nate Begeman2497e632005-07-21 20:44:43 +0000130 assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
Chris Lattnere94c5172004-11-23 05:59:53 +0000131 break;
132 }
Misha Brukmana4df3502004-10-23 18:28:01 +0000133 }
Misha Brukmanb05daff2004-08-09 23:03:59 +0000134}
135
Misha Brukmana4df3502004-10-23 18:28:01 +0000136static unsigned enumRegToMachineReg(unsigned enumReg) {
137 switch (enumReg) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000138 case PPC::R0 : case PPC::F0 : case PPC::CR0: return 0;
139 case PPC::R1 : case PPC::F1 : case PPC::CR1: return 1;
Chris Lattner9ba12352004-11-23 18:59:59 +0000140 case PPC::R2 : case PPC::F2 : case PPC::CR2: return 2;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000141 case PPC::R3 : case PPC::F3 : case PPC::CR3: return 3;
142 case PPC::R4 : case PPC::F4 : case PPC::CR4: return 4;
Chris Lattner9ba12352004-11-23 18:59:59 +0000143 case PPC::R5 : case PPC::F5 : case PPC::CR5: return 5;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000144 case PPC::R6 : case PPC::F6 : case PPC::CR6: return 6;
145 case PPC::R7 : case PPC::F7 : case PPC::CR7: return 7;
Misha Brukmana4df3502004-10-23 18:28:01 +0000146 case PPC::R8 : case PPC::F8 : return 8;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000147 case PPC::R9 : case PPC::F9 : return 9;
148 case PPC::R10: case PPC::F10: return 10;
Misha Brukmana4df3502004-10-23 18:28:01 +0000149 case PPC::R11: case PPC::F11: return 11;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000150 case PPC::R12: case PPC::F12: return 12;
151 case PPC::R13: case PPC::F13: return 13;
Misha Brukmana4df3502004-10-23 18:28:01 +0000152 case PPC::R14: case PPC::F14: return 14;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000153 case PPC::R15: case PPC::F15: return 15;
154 case PPC::R16: case PPC::F16: return 16;
Misha Brukmana4df3502004-10-23 18:28:01 +0000155 case PPC::R17: case PPC::F17: return 17;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000156 case PPC::R18: case PPC::F18: return 18;
157 case PPC::R19: case PPC::F19: return 19;
Misha Brukmana4df3502004-10-23 18:28:01 +0000158 case PPC::R20: case PPC::F20: return 20;
159 case PPC::R21: case PPC::F21: return 21;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000160 case PPC::R22: case PPC::F22: return 22;
161 case PPC::R23: case PPC::F23: return 23;
Misha Brukmana4df3502004-10-23 18:28:01 +0000162 case PPC::R24: case PPC::F24: return 24;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000163 case PPC::R25: case PPC::F25: return 25;
164 case PPC::R26: case PPC::F26: return 26;
Misha Brukmana4df3502004-10-23 18:28:01 +0000165 case PPC::R27: case PPC::F27: return 27;
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000166 case PPC::R28: case PPC::F28: return 28;
167 case PPC::R29: case PPC::F29: return 29;
Misha Brukmana4df3502004-10-23 18:28:01 +0000168 case PPC::R30: case PPC::F30: return 30;
169 case PPC::R31: case PPC::F31: return 31;
170 default:
171 std::cerr << "Unhandled reg in enumRegToRealReg!\n";
172 abort();
173 }
174}
175
Chris Lattnere94c5172004-11-23 05:59:53 +0000176int PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000177
Chris Lattnere94c5172004-11-23 05:59:53 +0000178 int rv = 0; // Return value; defaults to 0 for unhandled cases
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000179 // or things that get fixed up later by the JIT.
Misha Brukman3070e2f2004-10-21 01:42:02 +0000180 if (MO.isRegister()) {
Misha Brukmana4df3502004-10-23 18:28:01 +0000181 rv = enumRegToMachineReg(MO.getReg());
Chris Lattnerf577c612005-04-19 05:41:52 +0000182
Nate Begemanadeb43d2005-07-20 22:42:00 +0000183 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
Chris Lattnerf577c612005-04-19 05:41:52 +0000184 // register, not the register number directly.
Nate Begemanadeb43d2005-07-20 22:42:00 +0000185 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
Chris Lattnerf577c612005-04-19 05:41:52 +0000186 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
187 rv = 0x80 >> rv;
188 }
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000189 } else if (MO.isImmediate()) {
190 rv = MO.getImmedValue();
Chris Lattner477d1de2005-04-18 00:46:10 +0000191 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
192 bool isExternal = MO.isExternalSymbol() ||
193 MO.getGlobal()->hasWeakLinkage() ||
194 MO.getGlobal()->isExternal();
Chris Lattnerd7fa35c2004-11-23 15:56:38 +0000195 unsigned Reloc = 0;
Chris Lattnere94c5172004-11-23 05:59:53 +0000196 if (MI.getOpcode() == PPC::CALLpcrel)
197 Reloc = PPC::reloc_pcrel_bx;
Chris Lattner5efb75d2004-11-24 22:30:08 +0000198 else {
Nate Begeman2497e632005-07-21 20:44:43 +0000199 switch (MI.getOpcode()) {
200 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
201 case PPC::LIS:
Chris Lattner477d1de2005-04-18 00:46:10 +0000202 if (isExternal)
Chris Lattner5efb75d2004-11-24 22:30:08 +0000203 Reloc = PPC::reloc_absolute_ptr_high; // Pointer to stub
Nate Begeman2497e632005-07-21 20:44:43 +0000204 else
Chris Lattner5efb75d2004-11-24 22:30:08 +0000205 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
Nate Begeman2497e632005-07-21 20:44:43 +0000206 break;
207 case PPC::LA:
Chris Lattner477d1de2005-04-18 00:46:10 +0000208 assert(!isExternal && "Something in the ISEL changed\n");
Chris Lattner5efb75d2004-11-24 22:30:08 +0000209 Reloc = PPC::reloc_absolute_low;
Nate Begeman2497e632005-07-21 20:44:43 +0000210 break;
211 case PPC::LBZ:
212 case PPC::LHA:
213 case PPC::LHZ:
214 case PPC::LWZ:
215 case PPC::LFS:
216 case PPC::LFD:
217 case PPC::STB:
218 case PPC::STH:
219 case PPC::STW:
220 case PPC::STFS:
221 case PPC::STFD:
222 if (isExternal)
223 Reloc = PPC::reloc_absolute_ptr_low;
224 else
225 Reloc = PPC::reloc_absolute_low;
226 break;
Chris Lattner5efb75d2004-11-24 22:30:08 +0000227 }
Chris Lattnere94c5172004-11-23 05:59:53 +0000228 }
Chris Lattner477d1de2005-04-18 00:46:10 +0000229 if (MO.isGlobalAddress())
230 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
Nate Begeman2497e632005-07-21 20:44:43 +0000231 Reloc, MO.getGlobal(), 0));
Chris Lattner477d1de2005-04-18 00:46:10 +0000232 else
233 MCE.addRelocation(MachineRelocation(MCE.getCurrentPCOffset(),
Nate Begeman2497e632005-07-21 20:44:43 +0000234 Reloc, MO.getSymbolName(), 0));
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000235 } else if (MO.isMachineBasicBlock()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000236 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Chris Lattnerb752a972004-11-25 00:33:57 +0000237 BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000238 } else if (MO.isConstantPoolIndex()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000239 unsigned index = MO.getConstantPoolIndex();
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000240 unsigned Opcode = MI.getOpcode();
Nate Begeman2497e632005-07-21 20:44:43 +0000241 rv = MCE.getConstantPoolEntryAddress(index);
242 if (Opcode == PPC::LIS) {
243 // lis wants hi16(addr)
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000244 if ((short)rv < 0) rv += 1 << 16;
Chris Lattnere94c5172004-11-23 05:59:53 +0000245 rv >>= 16;
Misha Brukmana4df3502004-10-23 18:28:01 +0000246 } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
247 Opcode == PPC::LFS || Opcode == PPC::LFD) {
Nate Begeman2497e632005-07-21 20:44:43 +0000248 // These load opcodes want lo16(addr)
Chris Lattnere94c5172004-11-23 05:59:53 +0000249 rv &= 0xffff;
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000250 } else {
251 assert(0 && "Unknown constant pool using instruction!");
Misha Brukmana4df3502004-10-23 18:28:01 +0000252 }
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000253 } else {
254 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
255 abort();
Misha Brukmana4df3502004-10-23 18:28:01 +0000256 }
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000257
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000258 return rv;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000259}
260
Misha Brukmand37faba2004-10-14 06:07:25 +0000261#include "PPC32GenCodeEmitter.inc"
Misha Brukmanb05daff2004-08-09 23:03:59 +0000262