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