blob: 57f30c53bdcac471bf49f5de94404c976305d426 [file] [log] [blame]
Misha Brukman3d9a6c22004-08-11 00:09:42 +00001//===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00002//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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 "PPC32JITInfo.h"
16#include "PPC32TargetMachine.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
32 std::vector<std::pair<const BasicBlock*,
33 std::pair<unsigned*,MachineInstr*> > > BBRefs;
34 // Tracks where each BasicBlock starts
35 std::map<const BasicBlock*, long> BBLocations;
36
37 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
38 ///
Misha Brukmand37faba2004-10-14 06:07:25 +000039 int64_t getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
40
Misha Brukman3070e2f2004-10-21 01:42:02 +000041 unsigned getAddressOfExternalFunction(Function *F);
42
Misha Brukmanb05daff2004-08-09 23:03:59 +000043 public:
Misha Brukman3d9a6c22004-08-11 00:09:42 +000044 PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
Misha Brukmanb05daff2004-08-09 23:03:59 +000045 : TM(T), MCE(M) {}
46
47 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
48
49 /// runOnMachineFunction - emits the given MachineFunction to memory
50 ///
51 bool runOnMachineFunction(MachineFunction &MF);
52
53 /// emitBasicBlock - emits the given MachineBasicBlock to memory
54 ///
55 void emitBasicBlock(MachineBasicBlock &MBB);
56
57 /// emitWord - write a 32-bit word to memory at the current PC
58 ///
59 void emitWord(unsigned w) { MCE.emitWord(w); }
Misha Brukmand37faba2004-10-14 06:07:25 +000060
61 /// getValueBit - return the particular bit of Val
62 ///
63 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Misha Brukmanb05daff2004-08-09 23:03:59 +000064
Misha Brukman3070e2f2004-10-21 01:42:02 +000065 /// getBinaryCodeForInstr - This function, generated by the
66 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
67 /// machine instructions.
Misha Brukmanb05daff2004-08-09 23:03:59 +000068 ///
Misha Brukmand37faba2004-10-14 06:07:25 +000069 unsigned getBinaryCodeForInstr(MachineInstr &MI);
Misha Brukmanb05daff2004-08-09 23:03:59 +000070 };
71}
72
Misha Brukman5dfe3a92004-06-21 16:55:25 +000073/// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
74/// machine code emitted. This uses a MachineCodeEmitter object to handle
75/// actually outputting the machine code and resolving things like the address
76/// of functions. This method should returns true if machine code emission is
77/// not supported.
78///
Misha Brukman3d9a6c22004-08-11 00:09:42 +000079bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
80 MachineCodeEmitter &MCE) {
Misha Brukman3070e2f2004-10-21 01:42:02 +000081 // Keep as `true' until this is a functional JIT to allow llvm-gcc to build
82 return true;
83
Misha Brukmanb05daff2004-08-09 23:03:59 +000084 // Machine code emitter pass for PowerPC
Misha Brukman9691a892004-10-21 03:07:38 +000085 PM.add(new PPC32CodeEmitter(*this, MCE));
Misha Brukmand37faba2004-10-14 06:07:25 +000086 // Delete machine code for this function after emitting it
Misha Brukmanb05daff2004-08-09 23:03:59 +000087 PM.add(createMachineCodeDeleter());
Misha Brukmanc982cfa2004-10-14 06:39:56 +000088 return false;
Misha Brukmanb05daff2004-08-09 23:03:59 +000089}
90
Misha Brukman3d9a6c22004-08-11 00:09:42 +000091bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Misha Brukmanb05daff2004-08-09 23:03:59 +000092 MCE.startFunction(MF);
93 MCE.emitConstantPool(MF.getConstantPool());
Misha Brukman3070e2f2004-10-21 01:42:02 +000094 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
95 emitBasicBlock(*BB);
Misha Brukmanb05daff2004-08-09 23:03:59 +000096 MCE.finishFunction(MF);
Misha Brukman3070e2f2004-10-21 01:42:02 +000097
98 // Resolve branches to BasicBlocks for the entire function
99 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
100 long Location = BBLocations[BBRefs[i].first];
101 unsigned *Ref = BBRefs[i].second.first;
102 MachineInstr *MI = BBRefs[i].second.second;
103 DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location
104 << " in instr: " << std::dec << *MI);
105 for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) {
106 MachineOperand &op = MI->getOperand(ii);
107 if (op.isPCRelativeDisp()) {
108 // the instruction's branch target is made such that it branches to
109 // PC + (branchTarget * 4), so undo that arithmetic here:
110 // Location is the target of the branch
111 // Ref is the location of the instruction, and hence the PC
112 int64_t branchTarget = (Location - (long)Ref) >> 2;
113 MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed,
114 branchTarget);
115 unsigned fixedInstr = PPC32CodeEmitter::getBinaryCodeForInstr(*MI);
116 MCE.emitWordAt(fixedInstr, Ref);
117 break;
118 }
119 }
120 }
121 BBRefs.clear();
122 BBLocations.clear();
123
Misha Brukmanb05daff2004-08-09 23:03:59 +0000124 return false;
125}
126
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000127void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Misha Brukmana4df3502004-10-23 18:28:01 +0000128 BBLocations[MBB.getBasicBlock()] = MCE.getCurrentPCValue();
129 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
130 MachineInstr &MI = *I;
131 unsigned Opcode = MI.getOpcode();
Misha Brukmand4b4a992004-10-23 23:47:34 +0000132 if (Opcode == PPC::IMPLICIT_DEF)
133 continue; // pseudo opcode, no side effects
134 else if (Opcode == PPC::MovePCtoLR) {
135 // This can be simplified: the resulting 32-bit code is 0x48000005
136 MachineInstr *MI = BuildMI(PPC::BL, 1).addImm(1);
137 emitWord(getBinaryCodeForInstr(*MI));
138 delete MI;
139 } else
140 emitWord(getBinaryCodeForInstr(*I));
Misha Brukmana4df3502004-10-23 18:28:01 +0000141 }
Misha Brukmanb05daff2004-08-09 23:03:59 +0000142}
143
Misha Brukman3070e2f2004-10-21 01:42:02 +0000144unsigned PPC32CodeEmitter::getAddressOfExternalFunction(Function *F) {
145 static std::map<Function*, unsigned> ExternalFn2Addr;
146 std::map<Function*, unsigned>::iterator Addr = ExternalFn2Addr.find(F);
147
Chris Lattner213c9692004-11-22 21:51:40 +0000148 // FIXME: this needs to be rewritten.
Misha Brukman3070e2f2004-10-21 01:42:02 +0000149 if (Addr == ExternalFn2Addr.end())
Chris Lattner213c9692004-11-22 21:51:40 +0000150 ExternalFn2Addr[F] = 0; //MCE.forceCompilationOf(F);
Misha Brukman3070e2f2004-10-21 01:42:02 +0000151 return ExternalFn2Addr[F];
152}
153
Misha Brukmana4df3502004-10-23 18:28:01 +0000154static unsigned enumRegToMachineReg(unsigned enumReg) {
155 switch (enumReg) {
156 case PPC::R0 : case PPC::F0 : return 0;
157 case PPC::R1 : case PPC::F1 : return 1;
158 case PPC::R2 : case PPC::F2 : return 2;
159 case PPC::R3 : case PPC::F3 : return 3;
160 case PPC::R4 : case PPC::F4 : return 4;
161 case PPC::R5 : case PPC::F5 : return 5;
162 case PPC::R6 : case PPC::F6 : return 6;
163 case PPC::R7 : case PPC::F7 : return 7;
164 case PPC::R8 : case PPC::F8 : return 8;
165 case PPC::R9 : case PPC::F9 : return 9;
166 case PPC::R10: case PPC::F10: return 10;
167 case PPC::R11: case PPC::F11: return 11;
168 case PPC::R12: case PPC::F12: return 12;
169 case PPC::R13: case PPC::F13: return 13;
170 case PPC::R14: case PPC::F14: return 14;
171 case PPC::R15: case PPC::F15: return 15;
172 case PPC::R16: case PPC::F16: return 16;
173 case PPC::R17: case PPC::F17: return 17;
174 case PPC::R18: case PPC::F18: return 18;
175 case PPC::R19: case PPC::F19: return 19;
176 case PPC::R20: case PPC::F20: return 20;
177 case PPC::R21: case PPC::F21: return 21;
178 case PPC::R22: case PPC::F22: return 22;
179 case PPC::R23: case PPC::F23: return 23;
180 case PPC::R24: case PPC::F24: return 24;
181 case PPC::R25: case PPC::F25: return 25;
182 case PPC::R26: case PPC::F26: return 26;
183 case PPC::R27: case PPC::F27: return 27;
184 case PPC::R28: case PPC::F28: return 28;
185 case PPC::R29: case PPC::F29: return 29;
186 case PPC::R30: case PPC::F30: return 30;
187 case PPC::R31: case PPC::F31: return 31;
188 default:
189 std::cerr << "Unhandled reg in enumRegToRealReg!\n";
190 abort();
191 }
192}
193
Misha Brukmand37faba2004-10-14 06:07:25 +0000194int64_t PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI,
195 MachineOperand &MO) {
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000196 int64_t rv = 0; // Return value; defaults to 0 for unhandled cases
197 // or things that get fixed up later by the JIT.
Misha Brukman3070e2f2004-10-21 01:42:02 +0000198 if (MO.isRegister()) {
Misha Brukmana4df3502004-10-23 18:28:01 +0000199 rv = enumRegToMachineReg(MO.getReg());
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000200 } else if (MO.isImmediate()) {
201 rv = MO.getImmedValue();
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000202 } else if (MO.isGlobalAddress()) {
Chris Lattner99b394d2004-11-22 21:45:54 +0000203 //GlobalValue *GV = MO.getGlobal();
204 // FIXME: Emit a relocation here.
205 rv = 0;
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000206 } else if (MO.isMachineBasicBlock()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000207 const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock();
208 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
209 BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI)));
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000210 } else if (MO.isConstantPoolIndex()) {
Misha Brukman3070e2f2004-10-21 01:42:02 +0000211 unsigned index = MO.getConstantPoolIndex();
212 rv = MCE.getConstantPoolEntryAddress(index);
213 } else if (MO.isFrameIndex()) {
214 std::cerr << "PPC32CodeEmitter: error: Frame index unhandled!\n";
215 abort();
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000216 } else {
217 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
218 abort();
219 }
220
Misha Brukmana4df3502004-10-23 18:28:01 +0000221 // Special treatment for global symbols: constants and vars
222 if (MO.isConstantPoolIndex() || MO.isGlobalAddress()) {
223 unsigned Opcode = MI.getOpcode();
224 int64_t MBBLoc = BBLocations[MI.getParent()->getBasicBlock()];
225 if (Opcode == PPC::LOADHiAddr) {
226 // LoadHiAddr wants hi16(addr - mbb)
227 rv = (rv - MBBLoc) >> 16;
228 } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
229 Opcode == PPC::LFS || Opcode == PPC::LFD) {
230 // These load opcodes want lo16(addr - mbb)
231 rv = (rv - MBBLoc) & 0xffff;
232 }
233 }
234
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000235 return rv;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000236}
237
Misha Brukman3d9a6c22004-08-11 00:09:42 +0000238void PPC32JITInfo::replaceMachineCodeForFunction (void *Old, void *New) {
Misha Brukmand37faba2004-10-14 06:07:25 +0000239 std::cerr << "PPC32JITInfo::replaceMachineCodeForFunction not implemented\n";
240 abort();
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000241}
242
Misha Brukmand37faba2004-10-14 06:07:25 +0000243#include "PPC32GenCodeEmitter.inc"
Misha Brukmanb05daff2004-08-09 23:03:59 +0000244