8cadbd0562292f61d240e41bdccc4efbee3d1e5f
[oota-llvm.git] / lib / Target / Alpha / AlphaCodeEmitter.cpp
1 //===-- Alpha/AlphaCodeEmitter.cpp - Convert Alpha code to machine code ---===//
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 contains the pass that transforms the Alpha machine instructions
11 // into relocatable machine code.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "AlphaTargetMachine.h"
16 #include "AlphaRelocations.h"
17 #include "Alpha.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/CodeGen/MachineFunctionPass.h"
21 #include "llvm/CodeGen/MachineInstr.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Function.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/ADT/Statistic.h"
26 #include <iostream>
27 using namespace llvm;
28
29 namespace {
30   Statistic<>
31   NumEmitted("alpha-emitter", "Number of machine instructions emitted");
32 }
33
34 namespace {
35   class AlphaCodeEmitter : public MachineFunctionPass {
36     const AlphaInstrInfo  *II;
37     MachineCodeEmitter  &MCE;
38     std::vector<unsigned*> BasicBlockAddrs;
39     std::vector<std::pair<const MachineBasicBlock *, unsigned*> > BBRefs;
40
41     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
42     ///
43     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
44
45   public:
46     explicit AlphaCodeEmitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
47     AlphaCodeEmitter(MachineCodeEmitter &mce, const AlphaInstrInfo& ii)
48         : II(&ii), MCE(mce) {}
49
50     bool runOnMachineFunction(MachineFunction &MF);
51
52     virtual const char *getPassName() const {
53       return "Alpha Machine Code Emitter";
54     }
55
56     void emitInstruction(const MachineInstr &MI);
57
58     /// getBinaryCodeForInstr - This function, generated by the
59     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
60     /// machine instructions.
61     ///
62     unsigned getBinaryCodeForInstr(MachineInstr &MI);
63
64   private:
65     void emitBasicBlock(MachineBasicBlock &MBB);
66
67   };
68 }
69
70 /// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
71 /// to the specified MCE object.
72 FunctionPass *llvm::createAlphaCodeEmitterPass(MachineCodeEmitter &MCE) {
73   return new AlphaCodeEmitter(MCE);
74 }
75
76 bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
77   II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
78
79   do {
80     BBRefs.clear();
81     BasicBlockAddrs.clear();
82     
83     MCE.startFunction(MF);
84     for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
85       emitBasicBlock(*I);
86   } while (MCE.finishFunction(MF));
87
88   // Resolve all forward branches now...
89   for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
90     unsigned* Location = BasicBlockAddrs[BBRefs[i].first->getNumber()];
91     unsigned* Ref = (unsigned*)BBRefs[i].second;
92     intptr_t BranchTargetDisp = 
93       (((unsigned char*)Location  - (unsigned char*)Ref) >> 2) - 1;
94     DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
95           << " Disp " << BranchTargetDisp 
96           << " using " <<  (BranchTargetDisp & ((1 << 22)-1)) << "\n");
97     *Ref |= (BranchTargetDisp & ((1 << 21)-1));
98   }
99   BBRefs.clear();
100   BasicBlockAddrs.clear();
101
102   return false;
103 }
104
105 void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
106   if (BasicBlockAddrs.size() <= (unsigned)MBB.getNumber())
107     BasicBlockAddrs.resize((MBB.getNumber()+1)*2);
108
109   BasicBlockAddrs[MBB.getNumber()] = (unsigned*)MCE.getCurrentPCValue();
110
111   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
112        I != E; ++I) {
113     MachineInstr &MI = *I;
114     unsigned Opcode = MI.getOpcode();
115     switch(MI.getOpcode()) {
116     default:
117       MCE.emitWordLE(getBinaryCodeForInstr(*I));
118       break;
119     case Alpha::ALTENT:
120     case Alpha::PCLABEL:
121     case Alpha::MEMLABEL:
122     case Alpha::IDEF_I:
123     case Alpha::IDEF_F32:
124     case Alpha::IDEF_F64:
125       break; //skip these
126     }
127   }
128 }
129
130 static unsigned getAlphaRegNumber(unsigned Reg) {
131   switch (Reg) {
132   case Alpha::R0  : case Alpha::F0  : return 0;
133   case Alpha::R1  : case Alpha::F1  : return 1;
134   case Alpha::R2  : case Alpha::F2  : return 2;
135   case Alpha::R3  : case Alpha::F3  : return 3;
136   case Alpha::R4  : case Alpha::F4  : return 4;
137   case Alpha::R5  : case Alpha::F5  : return 5;
138   case Alpha::R6  : case Alpha::F6  : return 6;
139   case Alpha::R7  : case Alpha::F7  : return 7;
140   case Alpha::R8  : case Alpha::F8  : return 8;
141   case Alpha::R9  : case Alpha::F9  : return 9;
142   case Alpha::R10 : case Alpha::F10 : return 10;
143   case Alpha::R11 : case Alpha::F11 : return 11;
144   case Alpha::R12 : case Alpha::F12 : return 12;
145   case Alpha::R13 : case Alpha::F13 : return 13;
146   case Alpha::R14 : case Alpha::F14 : return 14;
147   case Alpha::R15 : case Alpha::F15 : return 15;
148   case Alpha::R16 : case Alpha::F16 : return 16;
149   case Alpha::R17 : case Alpha::F17 : return 17;
150   case Alpha::R18 : case Alpha::F18 : return 18;
151   case Alpha::R19 : case Alpha::F19 : return 19;
152   case Alpha::R20 : case Alpha::F20 : return 20;
153   case Alpha::R21 : case Alpha::F21 : return 21;
154   case Alpha::R22 : case Alpha::F22 : return 22;
155   case Alpha::R23 : case Alpha::F23 : return 23;
156   case Alpha::R24 : case Alpha::F24 : return 24;
157   case Alpha::R25 : case Alpha::F25 : return 25;
158   case Alpha::R26 : case Alpha::F26 : return 26;
159   case Alpha::R27 : case Alpha::F27 : return 27;
160   case Alpha::R28 : case Alpha::F28 : return 28;
161   case Alpha::R29 : case Alpha::F29 : return 29;
162   case Alpha::R30 : case Alpha::F30 : return 30;
163   case Alpha::R31 : case Alpha::F31 : return 31;
164   default:
165     assert(0 && "Unhandled reg");
166     abort();
167   }
168 }
169
170 int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
171
172   int rv = 0; // Return value; defaults to 0 for unhandled cases
173               // or things that get fixed up later by the JIT.
174
175   if (MO.isRegister()) {
176     rv = getAlphaRegNumber(MO.getReg());
177   } else if (MO.isImmediate()) {
178     rv = MO.getImmedValue();
179   } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
180              || MO.isConstantPoolIndex()) {
181     DEBUG(std::cerr << MO << " is a relocated op for " << MI << "\n";);
182     bool isExternal = MO.isExternalSymbol() ||
183       (MO.isGlobalAddress() &&
184        ( MO.getGlobal()->hasWeakLinkage() ||
185          MO.getGlobal()->isExternal()) );
186     unsigned Reloc = 0;
187     int Offset = 0;
188     bool useGOT = false;
189     switch (MI.getOpcode()) {
190     case Alpha::BSR:
191       Reloc = Alpha::reloc_bsr;
192       break;
193     case Alpha::LDLr:
194     case Alpha::LDQr:
195     case Alpha::LDBUr:
196     case Alpha::LDWUr:
197     case Alpha::LDSr:
198     case Alpha::LDTr:
199     case Alpha::LDAr:
200     case Alpha::STQr:
201     case Alpha::STLr:
202     case Alpha::STWr:
203     case Alpha::STBr:
204     case Alpha::STSr:
205     case Alpha::STTr:
206       Reloc = Alpha::reloc_gprellow;
207       break;
208     case Alpha::LDAHr:
209       Reloc = Alpha::reloc_gprelhigh;
210       break;
211     case Alpha::LDQl:
212       Reloc = Alpha::reloc_literal;
213       useGOT = true;
214       break;
215     case Alpha::LDAg:
216     case Alpha::LDAHg:
217       Reloc = Alpha::reloc_gpdist;
218       Offset = MI.getOperand(3).getImmedValue();
219       break;
220     default:
221       assert(0 && "unknown relocatable instruction");
222       abort();
223     }
224     if (MO.isGlobalAddress())
225       MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
226                                           Reloc, MO.getGlobal(), Offset,
227                                           false, useGOT));
228     else if (MO.isExternalSymbol())
229       MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
230                                           Reloc, MO.getSymbolName(), Offset,
231                                           true));
232     else
233       MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
234                                           Reloc, MO.getConstantPoolIndex(),
235                                           Offset));
236   } else if (MO.isMachineBasicBlock()) {
237     unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
238     BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
239   }else {
240     std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
241     abort();
242   }
243
244   return rv;
245 }
246
247
248 #include "AlphaGenCodeEmitter.inc"
249