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