Ensure that dump calls that are associated with asserts are removed from
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
2 //
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 //
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bytecode to native PowerPC.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "PPCTargetMachine.h"
16 #include "PPCRelocations.h"
17 #include "PPC.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/Passes.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Visibility.h"
26 #include "llvm/Target/TargetOptions.h"
27 #include <iostream>
28 using namespace llvm;
29
30 namespace {
31   class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
32     TargetMachine &TM;
33     MachineCodeEmitter &MCE;
34
35     // Tracks which instruction references which BasicBlock
36     std::vector<std::pair<MachineBasicBlock*, unsigned*> > BBRefs;
37     
38     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
39     ///
40     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
41
42   public:
43     PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
44       : TM(T), MCE(M) {}
45
46     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
47
48     /// runOnMachineFunction - emits the given MachineFunction to memory
49     ///
50     bool runOnMachineFunction(MachineFunction &MF);
51
52     /// emitBasicBlock - emits the given MachineBasicBlock to memory
53     ///
54     void emitBasicBlock(MachineBasicBlock &MBB);
55
56     /// getValueBit - return the particular bit of Val
57     ///
58     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
59
60     /// getBinaryCodeForInstr - This function, generated by the
61     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
62     /// machine instructions.
63     ///
64     unsigned getBinaryCodeForInstr(MachineInstr &MI);
65   };
66 }
67
68 /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get
69 /// machine code emitted.  This uses a MachineCodeEmitter object to handle
70 /// actually outputting the machine code and resolving things like the address
71 /// of functions.  This method should returns true if machine code emission is
72 /// not supported.
73 ///
74 bool PPCTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
75                                                   MachineCodeEmitter &MCE) {
76   // Machine code emitter pass for PowerPC
77   PM.add(new PPCCodeEmitter(*this, MCE));
78   // Delete machine code for this function after emitting it
79   PM.add(createMachineCodeDeleter());
80   return false;
81 }
82
83 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
84   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
85           MF.getTarget().getRelocationModel() != Reloc::Static) &&
86          "JIT relocation model must be set to static or default!");
87   do {
88     BBRefs.clear();
89
90     MCE.startFunction(MF);
91     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
92       emitBasicBlock(*BB);
93   } while (MCE.finishFunction(MF));
94
95   // Resolve branches to BasicBlocks for the entire function
96   for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
97     intptr_t Location = MCE.getMachineBasicBlockAddress(BBRefs[i].first);
98     unsigned *Ref = BBRefs[i].second;
99     DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
100                     << "\n");
101     unsigned Instr = *Ref;
102     intptr_t BranchTargetDisp = (Location - (intptr_t)Ref) >> 2;
103
104     switch (Instr >> 26) {
105     default: assert(0 && "Unknown branch user!");
106     case 18:  // This is B or BL
107       *Ref |= (BranchTargetDisp & ((1 << 24)-1)) << 2;
108       break;
109     case 16:  // This is BLT,BLE,BEQ,BGE,BGT,BNE, or other bcx instruction
110       *Ref |= (BranchTargetDisp & ((1 << 14)-1)) << 2;
111       break;
112     }
113   }
114   BBRefs.clear();
115
116   return false;
117 }
118
119 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
120   MCE.StartMachineBasicBlock(&MBB);
121   
122   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
123     MachineInstr &MI = *I;
124     unsigned Opcode = MI.getOpcode();
125     switch (MI.getOpcode()) {
126     default:
127       MCE.emitWordBE(getBinaryCodeForInstr(*I));
128       break;
129     case PPC::IMPLICIT_DEF_GPRC:
130     case PPC::IMPLICIT_DEF_G8RC:
131     case PPC::IMPLICIT_DEF_F8:
132     case PPC::IMPLICIT_DEF_F4:
133     case PPC::IMPLICIT_DEF_VRRC:
134       break; // pseudo opcode, no side effects
135     case PPC::MovePCtoLR:
136       assert(0 && "CodeEmitter does not support MovePCtoLR instruction");
137       break;
138     }
139   }
140 }
141
142 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
143
144   intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
145                    // or things that get fixed up later by the JIT.
146   if (MO.isRegister()) {
147     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
148
149     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
150     // register, not the register number directly.
151     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
152         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
153       rv = 0x80 >> rv;
154     }
155   } else if (MO.isImmediate()) {
156     rv = MO.getImmedValue();
157   } else if (MO.isGlobalAddress() || MO.isExternalSymbol()) {
158     unsigned Reloc = 0;
159     if (MI.getOpcode() == PPC::BL)
160       Reloc = PPC::reloc_pcrel_bx;
161     else {
162       switch (MI.getOpcode()) {
163       default: DEBUG(MI.dump()); assert(0 && "Unknown instruction for relocation!");
164       case PPC::LIS:
165         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
166         break;
167       case PPC::LI:
168       case PPC::LA:
169       case PPC::LBZ:
170       case PPC::LHA:
171       case PPC::LHZ:
172       case PPC::LWZ:
173       case PPC::LFS:
174       case PPC::LFD:
175       case PPC::STB:
176       case PPC::STH:
177       case PPC::STW:
178       case PPC::STFS:
179       case PPC::STFD:
180         Reloc = PPC::reloc_absolute_low;
181         break;
182       }
183     }
184     if (MO.isGlobalAddress())
185       MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
186                                           Reloc, MO.getGlobal(), 0));
187     else
188       MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
189                                           Reloc, MO.getSymbolName(), 0));
190   } else if (MO.isMachineBasicBlock()) {
191     unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
192     BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
193   } else if (MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
194     if (MO.isConstantPoolIndex())
195       rv = MCE.getConstantPoolEntryAddress(MO.getConstantPoolIndex());
196     else
197       rv = MCE.getJumpTableEntryAddress(MO.getJumpTableIndex());
198
199     unsigned Opcode = MI.getOpcode();
200     if (Opcode == PPC::LIS || Opcode == PPC::ADDIS) {
201       // lis wants hi16(addr)
202       if ((short)rv < 0) rv += 1 << 16;
203       rv >>= 16;
204     } else if (Opcode == PPC::LWZ || Opcode == PPC::LA ||
205                Opcode == PPC::LI ||
206                Opcode == PPC::LFS || Opcode == PPC::LFD) {
207       // These load opcodes want lo16(addr)
208       rv &= 0xffff;
209     } else {
210       assert(0 && "Unknown constant pool or jump table using instruction!");
211     }
212   } else {
213     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
214     abort();
215   }
216
217   return rv;
218 }
219
220 #include "PPCGenCodeEmitter.inc"
221