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