blob: dfb7820cae859027f0ce1f3056f900ec7973852d [file] [log] [blame]
Andrew Lenharth0934ae02005-07-22 20:52:16 +00001//===-- 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"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000026#include <iostream>
Andrew Lenharth0934ae02005-07-22 20:52:16 +000027using namespace llvm;
28
29namespace {
30 Statistic<>
31 NumEmitted("alpha-emitter", "Number of machine instructions emitted");
32}
33
34namespace {
35 class AlphaCodeEmitter : public MachineFunctionPass {
36 const AlphaInstrInfo *II;
37 MachineCodeEmitter &MCE;
38 std::map<const MachineBasicBlock*, unsigned*> BasicBlockAddrs;
39 std::vector<std::pair<const MachineBasicBlock *, unsigned*> > BBRefs;
40
41 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
42 ///
43 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
44
45 public:
46 explicit AlphaCodeEmitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
47 AlphaCodeEmitter(MachineCodeEmitter &mce, const AlphaInstrInfo& ii)
48 : II(&ii), 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 /// emitWord - write a 32-bit word to memory at the current PC
59 ///
60 void emitWord(unsigned w) { MCE.emitWord(w); }
61
62 /// getBinaryCodeForInstr - This function, generated by the
63 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
64 /// machine instructions.
65 ///
66 unsigned getBinaryCodeForInstr(MachineInstr &MI);
67
68 private:
69 void emitBasicBlock(MachineBasicBlock &MBB);
70
71 };
72}
73
74/// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
75/// to the specified MCE object.
76FunctionPass *llvm::createAlphaCodeEmitterPass(MachineCodeEmitter &MCE) {
77 return new AlphaCodeEmitter(MCE);
78}
79
80bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
81 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
82
Chris Lattner43b429b2006-05-02 18:27:26 +000083 do {
84 BBRefs.clear();
85 BasicBlockAddrs.clear();
86
87 MCE.startFunction(MF);
88 MCE.emitConstantPool(MF.getConstantPool());
89 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
90 emitBasicBlock(*I);
91 } while (MCE.finishFunction(MF));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000092
93 // Resolve all forward branches now...
94 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
95 unsigned* Location = (unsigned*)BasicBlockAddrs[BBRefs[i].first];
96 unsigned* Ref = (unsigned*)BBRefs[i].second;
Andrew Lenharth3f55a4f2005-08-04 15:32:36 +000097 intptr_t BranchTargetDisp =
98 (((unsigned char*)Location - (unsigned char*)Ref) >> 2) - 1;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000099 DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
Andrew Lenharth3f55a4f2005-08-04 15:32:36 +0000100 << " Disp " << BranchTargetDisp
101 << " using " << (BranchTargetDisp & ((1 << 22)-1)) << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000102 *Ref |= (BranchTargetDisp & ((1 << 21)-1));
103 }
104 BBRefs.clear();
105 BasicBlockAddrs.clear();
106
107 return false;
108}
109
110void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner45ff4fa2005-07-27 05:58:01 +0000111 uintptr_t Addr = MCE.getCurrentPCValue();
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000112 BasicBlockAddrs[&MBB] = (unsigned*)Addr;
113
114 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
115 I != E; ++I) {
116 MachineInstr &MI = *I;
117 unsigned Opcode = MI.getOpcode();
118 switch(MI.getOpcode()) {
119 default:
120 emitWord(getBinaryCodeForInstr(*I));
121 break;
122 case Alpha::ALTENT:
123 case Alpha::PCLABEL:
124 case Alpha::MEMLABEL:
Andrew Lenharth50b37842005-11-22 04:20:06 +0000125 case Alpha::IDEF_I:
126 case Alpha::IDEF_F32:
127 case Alpha::IDEF_F64:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000128 break; //skip these
129 }
130 }
131}
132
133static unsigned getAlphaRegNumber(unsigned Reg) {
134 switch (Reg) {
135 case Alpha::R0 : case Alpha::F0 : return 0;
136 case Alpha::R1 : case Alpha::F1 : return 1;
137 case Alpha::R2 : case Alpha::F2 : return 2;
138 case Alpha::R3 : case Alpha::F3 : return 3;
139 case Alpha::R4 : case Alpha::F4 : return 4;
140 case Alpha::R5 : case Alpha::F5 : return 5;
141 case Alpha::R6 : case Alpha::F6 : return 6;
142 case Alpha::R7 : case Alpha::F7 : return 7;
143 case Alpha::R8 : case Alpha::F8 : return 8;
144 case Alpha::R9 : case Alpha::F9 : return 9;
145 case Alpha::R10 : case Alpha::F10 : return 10;
146 case Alpha::R11 : case Alpha::F11 : return 11;
147 case Alpha::R12 : case Alpha::F12 : return 12;
148 case Alpha::R13 : case Alpha::F13 : return 13;
149 case Alpha::R14 : case Alpha::F14 : return 14;
150 case Alpha::R15 : case Alpha::F15 : return 15;
151 case Alpha::R16 : case Alpha::F16 : return 16;
152 case Alpha::R17 : case Alpha::F17 : return 17;
153 case Alpha::R18 : case Alpha::F18 : return 18;
154 case Alpha::R19 : case Alpha::F19 : return 19;
155 case Alpha::R20 : case Alpha::F20 : return 20;
156 case Alpha::R21 : case Alpha::F21 : return 21;
157 case Alpha::R22 : case Alpha::F22 : return 22;
158 case Alpha::R23 : case Alpha::F23 : return 23;
159 case Alpha::R24 : case Alpha::F24 : return 24;
160 case Alpha::R25 : case Alpha::F25 : return 25;
161 case Alpha::R26 : case Alpha::F26 : return 26;
162 case Alpha::R27 : case Alpha::F27 : return 27;
163 case Alpha::R28 : case Alpha::F28 : return 28;
164 case Alpha::R29 : case Alpha::F29 : return 29;
165 case Alpha::R30 : case Alpha::F30 : return 30;
166 case Alpha::R31 : case Alpha::F31 : return 31;
167 default:
168 assert(0 && "Unhandled reg");
169 abort();
170 }
171}
172
173int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
174
175 int rv = 0; // Return value; defaults to 0 for unhandled cases
176 // or things that get fixed up later by the JIT.
177
178 if (MO.isRegister()) {
179 rv = getAlphaRegNumber(MO.getReg());
180 } else if (MO.isImmediate()) {
181 rv = MO.getImmedValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000182 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000183 || MO.isConstantPoolIndex()) {
184 DEBUG(std::cerr << MO << " is a relocated op for " << MI << "\n";);
Jeff Cohen00b168892005-07-27 06:12:32 +0000185 bool isExternal = MO.isExternalSymbol() ||
186 (MO.isGlobalAddress() &&
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000187 ( MO.getGlobal()->hasWeakLinkage() ||
188 MO.getGlobal()->isExternal()) );
189 unsigned Reloc = 0;
190 int Offset = 0;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000191 bool useGOT = false;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000192 switch (MI.getOpcode()) {
Andrew Lenharth98169be2005-07-28 18:14:47 +0000193 case Alpha::BSR:
194 Reloc = Alpha::reloc_bsr;
195 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000196 case Alpha::LDLr:
197 case Alpha::LDQr:
198 case Alpha::LDBUr:
199 case Alpha::LDWUr:
200 case Alpha::LDSr:
201 case Alpha::LDTr:
202 case Alpha::LDAr:
Andrew Lenharth81b5a3c2005-11-16 21:15:53 +0000203 case Alpha::STQr:
204 case Alpha::STLr:
205 case Alpha::STWr:
206 case Alpha::STBr:
207 case Alpha::STSr:
208 case Alpha::STTr:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000209 Reloc = Alpha::reloc_gprellow;
210 break;
211 case Alpha::LDAHr:
212 Reloc = Alpha::reloc_gprelhigh;
213 break;
214 case Alpha::LDQl:
215 Reloc = Alpha::reloc_literal;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000216 useGOT = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000217 break;
218 case Alpha::LDAg:
219 case Alpha::LDAHg:
220 Reloc = Alpha::reloc_gpdist;
221 Offset = MI.getOperand(3).getImmedValue();
222 break;
223 default:
224 assert(0 && "unknown relocatable instruction");
225 abort();
226 }
227 if (MO.isGlobalAddress())
228 MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
229 Reloc, MO.getGlobal(), Offset,
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000230 false, useGOT));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000231 else if (MO.isExternalSymbol())
232 MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
233 Reloc, MO.getSymbolName(), Offset,
234 true));
235 else
236 MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
Jeff Cohen00b168892005-07-27 06:12:32 +0000237 Reloc, MO.getConstantPoolIndex(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000238 Offset));
239 } else if (MO.isMachineBasicBlock()) {
Chris Lattner45ff4fa2005-07-27 05:58:01 +0000240 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000241 BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
242 }else {
243 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
244 abort();
245 }
246
247 return rv;
248}
249
250
251#include "AlphaGenCodeEmitter.inc"
252