blob: d362f3550e477a8570e74b1a254d08d540d551c3 [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth0934ae02005-07-22 20:52:16 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the pass that transforms the Alpha machine instructions
11// into relocatable machine code.
12//
13//===----------------------------------------------------------------------===//
14
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "alpha-emitter"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000016#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"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000026using namespace llvm;
27
28namespace {
Andrew Lenharth0934ae02005-07-22 20:52:16 +000029 class AlphaCodeEmitter : public MachineFunctionPass {
30 const AlphaInstrInfo *II;
Evan Cheng55fc2802006-07-25 20:40:54 +000031 TargetMachine &TM;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000032 MachineCodeEmitter &MCE;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000033
34 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
35 ///
36 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
37
38 public:
Devang Patel19974732007-05-03 01:11:54 +000039 static char ID;
Evan Cheng55fc2802006-07-25 20:40:54 +000040 explicit AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce)
Devang Patel794fd752007-05-01 21:15:47 +000041 : MachineFunctionPass((intptr_t)&ID), II(0), TM(tm), MCE(mce) {}
Evan Cheng55fc2802006-07-25 20:40:54 +000042 AlphaCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce,
43 const AlphaInstrInfo& ii)
Devang Patel794fd752007-05-01 21:15:47 +000044 : MachineFunctionPass((intptr_t)&ID), II(&ii), TM(tm), MCE(mce) {}
Andrew Lenharth0934ae02005-07-22 20:52:16 +000045
46 bool runOnMachineFunction(MachineFunction &MF);
47
48 virtual const char *getPassName() const {
49 return "Alpha Machine Code Emitter";
50 }
51
52 void emitInstruction(const MachineInstr &MI);
53
Andrew Lenharth0934ae02005-07-22 20:52:16 +000054 /// getBinaryCodeForInstr - This function, generated by the
55 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
56 /// machine instructions.
57 ///
58 unsigned getBinaryCodeForInstr(MachineInstr &MI);
59
60 private:
61 void emitBasicBlock(MachineBasicBlock &MBB);
62
63 };
Devang Patel19974732007-05-03 01:11:54 +000064 char AlphaCodeEmitter::ID = 0;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000065}
66
67/// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
68/// to the specified MCE object.
Evan Cheng55fc2802006-07-25 20:40:54 +000069FunctionPass *llvm::createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
70 MachineCodeEmitter &MCE) {
71 return new AlphaCodeEmitter(TM, MCE);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000072}
73
74bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
75 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
76
Chris Lattner43b429b2006-05-02 18:27:26 +000077 do {
Chris Lattner43b429b2006-05-02 18:27:26 +000078 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +000079 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
80 emitBasicBlock(*I);
81 } while (MCE.finishFunction(MF));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000082
Andrew Lenharth0934ae02005-07-22 20:52:16 +000083 return false;
84}
85
86void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +000087 MCE.StartMachineBasicBlock(&MBB);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000088 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
89 I != E; ++I) {
90 MachineInstr &MI = *I;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000091 switch(MI.getOpcode()) {
92 default:
Chris Lattnerd3f0aef2006-05-02 19:14:47 +000093 MCE.emitWordLE(getBinaryCodeForInstr(*I));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000094 break;
95 case Alpha::ALTENT:
96 case Alpha::PCLABEL:
97 case Alpha::MEMLABEL:
Andrew Lenharth50b37842005-11-22 04:20:06 +000098 case Alpha::IDEF_I:
99 case Alpha::IDEF_F32:
100 case Alpha::IDEF_F64:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000101 break; //skip these
102 }
103 }
104}
105
106static unsigned getAlphaRegNumber(unsigned Reg) {
107 switch (Reg) {
108 case Alpha::R0 : case Alpha::F0 : return 0;
109 case Alpha::R1 : case Alpha::F1 : return 1;
110 case Alpha::R2 : case Alpha::F2 : return 2;
111 case Alpha::R3 : case Alpha::F3 : return 3;
112 case Alpha::R4 : case Alpha::F4 : return 4;
113 case Alpha::R5 : case Alpha::F5 : return 5;
114 case Alpha::R6 : case Alpha::F6 : return 6;
115 case Alpha::R7 : case Alpha::F7 : return 7;
116 case Alpha::R8 : case Alpha::F8 : return 8;
117 case Alpha::R9 : case Alpha::F9 : return 9;
118 case Alpha::R10 : case Alpha::F10 : return 10;
119 case Alpha::R11 : case Alpha::F11 : return 11;
120 case Alpha::R12 : case Alpha::F12 : return 12;
121 case Alpha::R13 : case Alpha::F13 : return 13;
122 case Alpha::R14 : case Alpha::F14 : return 14;
123 case Alpha::R15 : case Alpha::F15 : return 15;
124 case Alpha::R16 : case Alpha::F16 : return 16;
125 case Alpha::R17 : case Alpha::F17 : return 17;
126 case Alpha::R18 : case Alpha::F18 : return 18;
127 case Alpha::R19 : case Alpha::F19 : return 19;
128 case Alpha::R20 : case Alpha::F20 : return 20;
129 case Alpha::R21 : case Alpha::F21 : return 21;
130 case Alpha::R22 : case Alpha::F22 : return 22;
131 case Alpha::R23 : case Alpha::F23 : return 23;
132 case Alpha::R24 : case Alpha::F24 : return 24;
133 case Alpha::R25 : case Alpha::F25 : return 25;
134 case Alpha::R26 : case Alpha::F26 : return 26;
135 case Alpha::R27 : case Alpha::F27 : return 27;
136 case Alpha::R28 : case Alpha::F28 : return 28;
137 case Alpha::R29 : case Alpha::F29 : return 29;
138 case Alpha::R30 : case Alpha::F30 : return 30;
139 case Alpha::R31 : case Alpha::F31 : return 31;
140 default:
141 assert(0 && "Unhandled reg");
142 abort();
143 }
144}
145
146int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
147
148 int rv = 0; // Return value; defaults to 0 for unhandled cases
149 // or things that get fixed up later by the JIT.
150
151 if (MO.isRegister()) {
152 rv = getAlphaRegNumber(MO.getReg());
153 } else if (MO.isImmediate()) {
154 rv = MO.getImmedValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000155 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000156 || MO.isConstantPoolIndex()) {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000157 DOUT << MO << " is a relocated op for " << MI << "\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000158 unsigned Reloc = 0;
159 int Offset = 0;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000160 bool useGOT = false;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000161 switch (MI.getOpcode()) {
Andrew Lenharth98169be2005-07-28 18:14:47 +0000162 case Alpha::BSR:
163 Reloc = Alpha::reloc_bsr;
164 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000165 case Alpha::LDLr:
166 case Alpha::LDQr:
167 case Alpha::LDBUr:
168 case Alpha::LDWUr:
169 case Alpha::LDSr:
170 case Alpha::LDTr:
171 case Alpha::LDAr:
Andrew Lenharth81b5a3c2005-11-16 21:15:53 +0000172 case Alpha::STQr:
173 case Alpha::STLr:
174 case Alpha::STWr:
175 case Alpha::STBr:
176 case Alpha::STSr:
177 case Alpha::STTr:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000178 Reloc = Alpha::reloc_gprellow;
179 break;
180 case Alpha::LDAHr:
181 Reloc = Alpha::reloc_gprelhigh;
182 break;
183 case Alpha::LDQl:
184 Reloc = Alpha::reloc_literal;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000185 useGOT = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000186 break;
187 case Alpha::LDAg:
188 case Alpha::LDAHg:
189 Reloc = Alpha::reloc_gpdist;
190 Offset = MI.getOperand(3).getImmedValue();
191 break;
192 default:
193 assert(0 && "unknown relocatable instruction");
194 abort();
195 }
196 if (MO.isGlobalAddress())
Chris Lattner5a032de2006-05-03 20:30:20 +0000197 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000198 Reloc, MO.getGlobal(), Offset,
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000199 false, useGOT));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000200 else if (MO.isExternalSymbol())
Chris Lattner5a032de2006-05-03 20:30:20 +0000201 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000202 Reloc, MO.getSymbolName(), Offset,
203 true));
204 else
Chris Lattner5a032de2006-05-03 20:30:20 +0000205 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Jeff Cohen00b168892005-07-27 06:12:32 +0000206 Reloc, MO.getConstantPoolIndex(),
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000207 Offset));
208 } else if (MO.isMachineBasicBlock()) {
Evan Chengf141cc42006-07-27 18:21:10 +0000209 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
210 Alpha::reloc_bsr,
211 MO.getMachineBasicBlock()));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000212 }else {
Bill Wendlingf5da1332006-12-07 22:21:48 +0000213 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000214 abort();
215 }
216
217 return rv;
218}
219
220
221#include "AlphaGenCodeEmitter.inc"
222