What should be the last unnecessary <iostream>s in the library.
[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 using namespace llvm;
27
28 namespace {
29   Statistic
30   NumEmitted("alpha-emitter", "Number of machine instructions emitted");
31 }
32
33 namespace {
34   class AlphaCodeEmitter : public MachineFunctionPass {
35     const AlphaInstrInfo  *II;
36     TargetMachine &TM;
37     MachineCodeEmitter  &MCE;
38
39     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
40     ///
41     int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
42
43   public:
44     explicit AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
45       : II(0), TM(tm), MCE(mce) {}
46     AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
47                      const AlphaInstrInfo& ii)
48       : II(&ii), TM(tm), 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(AlphaTargetMachine &TM,
73                                                MachineCodeEmitter &MCE) {
74   return new AlphaCodeEmitter(TM, MCE);
75 }
76
77 bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
78   II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
79
80   do {
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   return false;
87 }
88
89 void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
90   MCE.StartMachineBasicBlock(&MBB);
91   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
92        I != E; ++I) {
93     MachineInstr &MI = *I;
94     switch(MI.getOpcode()) {
95     default:
96       MCE.emitWordLE(getBinaryCodeForInstr(*I));
97       break;
98     case Alpha::ALTENT:
99     case Alpha::PCLABEL:
100     case Alpha::MEMLABEL:
101     case Alpha::IDEF_I:
102     case Alpha::IDEF_F32:
103     case Alpha::IDEF_F64:
104       break; //skip these
105     }
106   }
107 }
108
109 static unsigned getAlphaRegNumber(unsigned Reg) {
110   switch (Reg) {
111   case Alpha::R0  : case Alpha::F0  : return 0;
112   case Alpha::R1  : case Alpha::F1  : return 1;
113   case Alpha::R2  : case Alpha::F2  : return 2;
114   case Alpha::R3  : case Alpha::F3  : return 3;
115   case Alpha::R4  : case Alpha::F4  : return 4;
116   case Alpha::R5  : case Alpha::F5  : return 5;
117   case Alpha::R6  : case Alpha::F6  : return 6;
118   case Alpha::R7  : case Alpha::F7  : return 7;
119   case Alpha::R8  : case Alpha::F8  : return 8;
120   case Alpha::R9  : case Alpha::F9  : return 9;
121   case Alpha::R10 : case Alpha::F10 : return 10;
122   case Alpha::R11 : case Alpha::F11 : return 11;
123   case Alpha::R12 : case Alpha::F12 : return 12;
124   case Alpha::R13 : case Alpha::F13 : return 13;
125   case Alpha::R14 : case Alpha::F14 : return 14;
126   case Alpha::R15 : case Alpha::F15 : return 15;
127   case Alpha::R16 : case Alpha::F16 : return 16;
128   case Alpha::R17 : case Alpha::F17 : return 17;
129   case Alpha::R18 : case Alpha::F18 : return 18;
130   case Alpha::R19 : case Alpha::F19 : return 19;
131   case Alpha::R20 : case Alpha::F20 : return 20;
132   case Alpha::R21 : case Alpha::F21 : return 21;
133   case Alpha::R22 : case Alpha::F22 : return 22;
134   case Alpha::R23 : case Alpha::F23 : return 23;
135   case Alpha::R24 : case Alpha::F24 : return 24;
136   case Alpha::R25 : case Alpha::F25 : return 25;
137   case Alpha::R26 : case Alpha::F26 : return 26;
138   case Alpha::R27 : case Alpha::F27 : return 27;
139   case Alpha::R28 : case Alpha::F28 : return 28;
140   case Alpha::R29 : case Alpha::F29 : return 29;
141   case Alpha::R30 : case Alpha::F30 : return 30;
142   case Alpha::R31 : case Alpha::F31 : return 31;
143   default:
144     assert(0 && "Unhandled reg");
145     abort();
146   }
147 }
148
149 int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
150
151   int rv = 0; // Return value; defaults to 0 for unhandled cases
152               // or things that get fixed up later by the JIT.
153
154   if (MO.isRegister()) {
155     rv = getAlphaRegNumber(MO.getReg());
156   } else if (MO.isImmediate()) {
157     rv = MO.getImmedValue();
158   } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
159              || MO.isConstantPoolIndex()) {
160     DOUT << MO << " is a relocated op for " << MI << "\n";
161     unsigned Reloc = 0;
162     int Offset = 0;
163     bool useGOT = false;
164     switch (MI.getOpcode()) {
165     case Alpha::BSR:
166       Reloc = Alpha::reloc_bsr;
167       break;
168     case Alpha::LDLr:
169     case Alpha::LDQr:
170     case Alpha::LDBUr:
171     case Alpha::LDWUr:
172     case Alpha::LDSr:
173     case Alpha::LDTr:
174     case Alpha::LDAr:
175     case Alpha::STQr:
176     case Alpha::STLr:
177     case Alpha::STWr:
178     case Alpha::STBr:
179     case Alpha::STSr:
180     case Alpha::STTr:
181       Reloc = Alpha::reloc_gprellow;
182       break;
183     case Alpha::LDAHr:
184       Reloc = Alpha::reloc_gprelhigh;
185       break;
186     case Alpha::LDQl:
187       Reloc = Alpha::reloc_literal;
188       useGOT = true;
189       break;
190     case Alpha::LDAg:
191     case Alpha::LDAHg:
192       Reloc = Alpha::reloc_gpdist;
193       Offset = MI.getOperand(3).getImmedValue();
194       break;
195     default:
196       assert(0 && "unknown relocatable instruction");
197       abort();
198     }
199     if (MO.isGlobalAddress())
200       MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
201                                           Reloc, MO.getGlobal(), Offset,
202                                           false, useGOT));
203     else if (MO.isExternalSymbol())
204       MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
205                                           Reloc, MO.getSymbolName(), Offset,
206                                           true));
207     else
208     MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
209                                           Reloc, MO.getConstantPoolIndex(),
210                                           Offset));
211   } else if (MO.isMachineBasicBlock()) {
212     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
213                                                Alpha::reloc_bsr,
214                                                MO.getMachineBasicBlock()));
215   }else {
216     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
217     abort();
218   }
219
220   return rv;
221 }
222
223
224 #include "AlphaGenCodeEmitter.inc"
225