blob: 6b4c3a2bf50dd86f35f7458c250677cbd1d8bea4 [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"
26using namespace llvm;
27
28namespace {
29 Statistic<>
30 NumEmitted("alpha-emitter", "Number of machine instructions emitted");
31}
32
33namespace {
34 class AlphaCodeEmitter : public MachineFunctionPass {
35 const AlphaInstrInfo *II;
36 MachineCodeEmitter &MCE;
37 std::map<const MachineBasicBlock*, unsigned*> BasicBlockAddrs;
38 std::vector<std::pair<const MachineBasicBlock *, unsigned*> > BBRefs;
39
40 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
41 ///
42 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
43
44 public:
45 explicit AlphaCodeEmitter(MachineCodeEmitter &mce) : II(0), MCE(mce) {}
46 AlphaCodeEmitter(MachineCodeEmitter &mce, const AlphaInstrInfo& ii)
47 : II(&ii), MCE(mce) {}
48
49 bool runOnMachineFunction(MachineFunction &MF);
50
51 virtual const char *getPassName() const {
52 return "Alpha Machine Code Emitter";
53 }
54
55 void emitInstruction(const MachineInstr &MI);
56
57 /// emitWord - write a 32-bit word to memory at the current PC
58 ///
59 void emitWord(unsigned w) { MCE.emitWord(w); }
60
61 /// getBinaryCodeForInstr - This function, generated by the
62 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
63 /// machine instructions.
64 ///
65 unsigned getBinaryCodeForInstr(MachineInstr &MI);
66
67 private:
68 void emitBasicBlock(MachineBasicBlock &MBB);
69
70 };
71}
72
73/// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
74/// to the specified MCE object.
75FunctionPass *llvm::createAlphaCodeEmitterPass(MachineCodeEmitter &MCE) {
76 return new AlphaCodeEmitter(MCE);
77}
78
79bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
80 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
81
82 MCE.startFunction(MF);
83 MCE.emitConstantPool(MF.getConstantPool());
84 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
85 emitBasicBlock(*I);
86 MCE.finishFunction(MF);
87
88 // Resolve all forward branches now...
89 for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) {
90 unsigned* Location = (unsigned*)BasicBlockAddrs[BBRefs[i].first];
91 unsigned* Ref = (unsigned*)BBRefs[i].second;
Andrew Lenharth3f55a4f2005-08-04 15:32:36 +000092 intptr_t BranchTargetDisp =
93 (((unsigned char*)Location - (unsigned char*)Ref) >> 2) - 1;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000094 DEBUG(std::cerr << "Fixup @ " << (void*)Ref << " to " << (void*)Location
Andrew Lenharth3f55a4f2005-08-04 15:32:36 +000095 << " Disp " << BranchTargetDisp
96 << " using " << (BranchTargetDisp & ((1 << 22)-1)) << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +000097 *Ref |= (BranchTargetDisp & ((1 << 21)-1));
98 }
99 BBRefs.clear();
100 BasicBlockAddrs.clear();
101
102 return false;
103}
104
105void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner45ff4fa2005-07-27 05:58:01 +0000106 uintptr_t Addr = MCE.getCurrentPCValue();
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000107 BasicBlockAddrs[&MBB] = (unsigned*)Addr;
108
109 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
110 I != E; ++I) {
111 MachineInstr &MI = *I;
112 unsigned Opcode = MI.getOpcode();
113 switch(MI.getOpcode()) {
114 default:
115 emitWord(getBinaryCodeForInstr(*I));
116 break;
117 case Alpha::ALTENT:
118 case Alpha::PCLABEL:
119 case Alpha::MEMLABEL:
Andrew Lenharth3f55a4f2005-08-04 15:32:36 +0000120 case Alpha::IDEF:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000121 break; //skip these
122 }
123 }
124}
125
126static unsigned getAlphaRegNumber(unsigned Reg) {
127 switch (Reg) {
128 case Alpha::R0 : case Alpha::F0 : return 0;
129 case Alpha::R1 : case Alpha::F1 : return 1;
130 case Alpha::R2 : case Alpha::F2 : return 2;
131 case Alpha::R3 : case Alpha::F3 : return 3;
132 case Alpha::R4 : case Alpha::F4 : return 4;
133 case Alpha::R5 : case Alpha::F5 : return 5;
134 case Alpha::R6 : case Alpha::F6 : return 6;
135 case Alpha::R7 : case Alpha::F7 : return 7;
136 case Alpha::R8 : case Alpha::F8 : return 8;
137 case Alpha::R9 : case Alpha::F9 : return 9;
138 case Alpha::R10 : case Alpha::F10 : return 10;
139 case Alpha::R11 : case Alpha::F11 : return 11;
140 case Alpha::R12 : case Alpha::F12 : return 12;
141 case Alpha::R13 : case Alpha::F13 : return 13;
142 case Alpha::R14 : case Alpha::F14 : return 14;
143 case Alpha::R15 : case Alpha::F15 : return 15;
144 case Alpha::R16 : case Alpha::F16 : return 16;
145 case Alpha::R17 : case Alpha::F17 : return 17;
146 case Alpha::R18 : case Alpha::F18 : return 18;
147 case Alpha::R19 : case Alpha::F19 : return 19;
148 case Alpha::R20 : case Alpha::F20 : return 20;
149 case Alpha::R21 : case Alpha::F21 : return 21;
150 case Alpha::R22 : case Alpha::F22 : return 22;
151 case Alpha::R23 : case Alpha::F23 : return 23;
152 case Alpha::R24 : case Alpha::F24 : return 24;
153 case Alpha::R25 : case Alpha::F25 : return 25;
154 case Alpha::R26 : case Alpha::F26 : return 26;
155 case Alpha::R27 : case Alpha::F27 : return 27;
156 case Alpha::R28 : case Alpha::F28 : return 28;
157 case Alpha::R29 : case Alpha::F29 : return 29;
158 case Alpha::R30 : case Alpha::F30 : return 30;
159 case Alpha::R31 : case Alpha::F31 : return 31;
160 default:
161 assert(0 && "Unhandled reg");
162 abort();
163 }
164}
165
166int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
167
168 int rv = 0; // Return value; defaults to 0 for unhandled cases
169 // or things that get fixed up later by the JIT.
170
171 if (MO.isRegister()) {
172 rv = getAlphaRegNumber(MO.getReg());
173 } else if (MO.isImmediate()) {
174 rv = MO.getImmedValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000175 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000176 || MO.isConstantPoolIndex()) {
177 DEBUG(std::cerr << MO << " is a relocated op for " << MI << "\n";);
Jeff Cohen00b168892005-07-27 06:12:32 +0000178 bool isExternal = MO.isExternalSymbol() ||
179 (MO.isGlobalAddress() &&
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000180 ( MO.getGlobal()->hasWeakLinkage() ||
181 MO.getGlobal()->isExternal()) );
182 unsigned Reloc = 0;
183 int Offset = 0;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000184 bool useGOT = false;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000185 switch (MI.getOpcode()) {
Andrew Lenharth98169be2005-07-28 18:14:47 +0000186 case Alpha::BSR:
187 Reloc = Alpha::reloc_bsr;
188 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000189 case Alpha::LDLr:
190 case Alpha::LDQr:
191 case Alpha::LDBUr:
192 case Alpha::LDWUr:
193 case Alpha::LDSr:
194 case Alpha::LDTr:
195 case Alpha::LDAr:
196 Reloc = Alpha::reloc_gprellow;
197 break;
198 case Alpha::LDAHr:
199 Reloc = Alpha::reloc_gprelhigh;
200 break;
201 case Alpha::LDQl:
202 Reloc = Alpha::reloc_literal;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000203 useGOT = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000204 break;
205 case Alpha::LDAg:
206 case Alpha::LDAHg:
207 Reloc = Alpha::reloc_gpdist;
208 Offset = MI.getOperand(3).getImmedValue();
209 break;
210 default:
211 assert(0 && "unknown relocatable instruction");
212 abort();
213 }
214 if (MO.isGlobalAddress())
215 MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
216 Reloc, MO.getGlobal(), Offset,
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000217 false, useGOT));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000218 else if (MO.isExternalSymbol())
219 MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
220 Reloc, MO.getSymbolName(), Offset,
221 true));
222 else
223 MCE.addRelocation(MachineRelocation((unsigned)MCE.getCurrentPCOffset(),
Jeff Cohen00b168892005-07-27 06:12:32 +0000224 Reloc, MO.getConstantPoolIndex(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000225 Offset));
226 } else if (MO.isMachineBasicBlock()) {
Chris Lattner45ff4fa2005-07-27 05:58:01 +0000227 unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue();
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000228 BBRefs.push_back(std::make_pair(MO.getMachineBasicBlock(), CurrPC));
229 }else {
230 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
231 abort();
232 }
233
234 return rv;
235}
236
237
238#include "AlphaGenCodeEmitter.inc"
239