blob: 884215e79309d9d594082ba95b94d4d54c1e165e [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 {
Chris Lattnerac0b6ae2006-12-06 17:46:33 +000030 Statistic
Andrew Lenharth0934ae02005-07-22 20:52:16 +000031 NumEmitted("alpha-emitter", "Number of machine instructions emitted");
32}
33
34namespace {
35 class AlphaCodeEmitter : public MachineFunctionPass {
36 const AlphaInstrInfo *II;
Evan Cheng55fc2802006-07-25 20:40:54 +000037 TargetMachine &TM;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000038 MachineCodeEmitter &MCE;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000039
40 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
41 ///
42 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
43
44 public:
Evan Cheng55fc2802006-07-25 20:40:54 +000045 explicit AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
46 : II(0), TM(tm), MCE(mce) {}
47 AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
48 const AlphaInstrInfo& ii)
49 : II(&ii), TM(tm), MCE(mce) {}
Andrew Lenharth0934ae02005-07-22 20:52:16 +000050
51 bool runOnMachineFunction(MachineFunction &MF);
52
53 virtual const char *getPassName() const {
54 return "Alpha Machine Code Emitter";
55 }
56
57 void emitInstruction(const MachineInstr &MI);
58
Andrew Lenharth0934ae02005-07-22 20:52:16 +000059 /// getBinaryCodeForInstr - This function, generated by the
60 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
61 /// machine instructions.
62 ///
63 unsigned getBinaryCodeForInstr(MachineInstr &MI);
64
65 private:
66 void emitBasicBlock(MachineBasicBlock &MBB);
67
68 };
69}
70
71/// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
72/// to the specified MCE object.
Evan Cheng55fc2802006-07-25 20:40:54 +000073FunctionPass *llvm::createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
74 MachineCodeEmitter &MCE) {
75 return new AlphaCodeEmitter(TM, MCE);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000076}
77
78bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
79 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
80
Chris Lattner43b429b2006-05-02 18:27:26 +000081 do {
Chris Lattner43b429b2006-05-02 18:27:26 +000082 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +000083 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
84 emitBasicBlock(*I);
85 } while (MCE.finishFunction(MF));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000086
Andrew Lenharth0934ae02005-07-22 20:52:16 +000087 return false;
88}
89
90void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +000091 MCE.StartMachineBasicBlock(&MBB);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000092 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
93 I != E; ++I) {
94 MachineInstr &MI = *I;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000095 switch(MI.getOpcode()) {
96 default:
Chris Lattnerd3f0aef2006-05-02 19:14:47 +000097 MCE.emitWordLE(getBinaryCodeForInstr(*I));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000098 break;
99 case Alpha::ALTENT:
100 case Alpha::PCLABEL:
101 case Alpha::MEMLABEL:
Andrew Lenharth50b37842005-11-22 04:20:06 +0000102 case Alpha::IDEF_I:
103 case Alpha::IDEF_F32:
104 case Alpha::IDEF_F64:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000105 break; //skip these
106 }
107 }
108}
109
110static unsigned getAlphaRegNumber(unsigned Reg) {
111 switch (Reg) {
112 case Alpha::R0 : case Alpha::F0 : return 0;
113 case Alpha::R1 : case Alpha::F1 : return 1;
114 case Alpha::R2 : case Alpha::F2 : return 2;
115 case Alpha::R3 : case Alpha::F3 : return 3;
116 case Alpha::R4 : case Alpha::F4 : return 4;
117 case Alpha::R5 : case Alpha::F5 : return 5;
118 case Alpha::R6 : case Alpha::F6 : return 6;
119 case Alpha::R7 : case Alpha::F7 : return 7;
120 case Alpha::R8 : case Alpha::F8 : return 8;
121 case Alpha::R9 : case Alpha::F9 : return 9;
122 case Alpha::R10 : case Alpha::F10 : return 10;
123 case Alpha::R11 : case Alpha::F11 : return 11;
124 case Alpha::R12 : case Alpha::F12 : return 12;
125 case Alpha::R13 : case Alpha::F13 : return 13;
126 case Alpha::R14 : case Alpha::F14 : return 14;
127 case Alpha::R15 : case Alpha::F15 : return 15;
128 case Alpha::R16 : case Alpha::F16 : return 16;
129 case Alpha::R17 : case Alpha::F17 : return 17;
130 case Alpha::R18 : case Alpha::F18 : return 18;
131 case Alpha::R19 : case Alpha::F19 : return 19;
132 case Alpha::R20 : case Alpha::F20 : return 20;
133 case Alpha::R21 : case Alpha::F21 : return 21;
134 case Alpha::R22 : case Alpha::F22 : return 22;
135 case Alpha::R23 : case Alpha::F23 : return 23;
136 case Alpha::R24 : case Alpha::F24 : return 24;
137 case Alpha::R25 : case Alpha::F25 : return 25;
138 case Alpha::R26 : case Alpha::F26 : return 26;
139 case Alpha::R27 : case Alpha::F27 : return 27;
140 case Alpha::R28 : case Alpha::F28 : return 28;
141 case Alpha::R29 : case Alpha::F29 : return 29;
142 case Alpha::R30 : case Alpha::F30 : return 30;
143 case Alpha::R31 : case Alpha::F31 : return 31;
144 default:
145 assert(0 && "Unhandled reg");
146 abort();
147 }
148}
149
150int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
151
152 int rv = 0; // Return value; defaults to 0 for unhandled cases
153 // or things that get fixed up later by the JIT.
154
155 if (MO.isRegister()) {
156 rv = getAlphaRegNumber(MO.getReg());
157 } else if (MO.isImmediate()) {
158 rv = MO.getImmedValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000159 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000160 || MO.isConstantPoolIndex()) {
161 DEBUG(std::cerr << MO << " is a relocated op for " << MI << "\n";);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000162 unsigned Reloc = 0;
163 int Offset = 0;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000164 bool useGOT = false;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000165 switch (MI.getOpcode()) {
Andrew Lenharth98169be2005-07-28 18:14:47 +0000166 case Alpha::BSR:
167 Reloc = Alpha::reloc_bsr;
168 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000169 case Alpha::LDLr:
170 case Alpha::LDQr:
171 case Alpha::LDBUr:
172 case Alpha::LDWUr:
173 case Alpha::LDSr:
174 case Alpha::LDTr:
175 case Alpha::LDAr:
Andrew Lenharth81b5a3c2005-11-16 21:15:53 +0000176 case Alpha::STQr:
177 case Alpha::STLr:
178 case Alpha::STWr:
179 case Alpha::STBr:
180 case Alpha::STSr:
181 case Alpha::STTr:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000182 Reloc = Alpha::reloc_gprellow;
183 break;
184 case Alpha::LDAHr:
185 Reloc = Alpha::reloc_gprelhigh;
186 break;
187 case Alpha::LDQl:
188 Reloc = Alpha::reloc_literal;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000189 useGOT = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000190 break;
191 case Alpha::LDAg:
192 case Alpha::LDAHg:
193 Reloc = Alpha::reloc_gpdist;
194 Offset = MI.getOperand(3).getImmedValue();
195 break;
196 default:
197 assert(0 && "unknown relocatable instruction");
198 abort();
199 }
200 if (MO.isGlobalAddress())
Chris Lattner5a032de2006-05-03 20:30:20 +0000201 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000202 Reloc, MO.getGlobal(), Offset,
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000203 false, useGOT));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000204 else if (MO.isExternalSymbol())
Chris Lattner5a032de2006-05-03 20:30:20 +0000205 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000206 Reloc, MO.getSymbolName(), Offset,
207 true));
208 else
Chris Lattner5a032de2006-05-03 20:30:20 +0000209 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Jeff Cohen00b168892005-07-27 06:12:32 +0000210 Reloc, MO.getConstantPoolIndex(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000211 Offset));
212 } else if (MO.isMachineBasicBlock()) {
Evan Chengf141cc42006-07-27 18:21:10 +0000213 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
214 Alpha::reloc_bsr,
215 MO.getMachineBasicBlock()));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000216 }else {
217 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
218 abort();
219 }
220
221 return rv;
222}
223
224
225#include "AlphaGenCodeEmitter.inc"
226