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