blob: 1409e0c6443442a42fb488a38baa49a98ac0651f [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"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000020#include "llvm/CodeGen/JITCodeEmitter.h"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000021#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"
Torok Edwin804e0fe2009-07-08 19:04:27 +000026#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
Andrew Lenharth0934ae02005-07-22 20:52:16 +000028using namespace llvm;
29
30namespace {
Chris Lattner782ebe42010-02-02 21:35:47 +000031 class AlphaCodeEmitter : public MachineFunctionPass {
32 JITCodeEmitter &MCE;
33 const AlphaInstrInfo *II;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000034 public:
Chris Lattner782ebe42010-02-02 21:35:47 +000035 static char ID;
36
37 AlphaCodeEmitter(JITCodeEmitter &mce) : MachineFunctionPass(&ID),
38 MCE(mce) {}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000039
40 /// getBinaryCodeForInstr - This function, generated by the
41 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
42 /// machine instructions.
43
44 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000045
46 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000047
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000048 unsigned getMachineOpValue(const MachineInstr &MI,
49 const MachineOperand &MO);
Chris Lattner782ebe42010-02-02 21:35:47 +000050
Andrew Lenharth0934ae02005-07-22 20:52:16 +000051 bool runOnMachineFunction(MachineFunction &MF);
Chris Lattner782ebe42010-02-02 21:35:47 +000052
Andrew Lenharth0934ae02005-07-22 20:52:16 +000053 virtual const char *getPassName() const {
54 return "Alpha Machine Code Emitter";
55 }
Chris Lattner782ebe42010-02-02 21:35:47 +000056
Andrew Lenharth0934ae02005-07-22 20:52:16 +000057 private:
58 void emitBasicBlock(MachineBasicBlock &MBB);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000059 };
60}
61
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000062/// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha
63/// code to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000064
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000065FunctionPass *llvm::createAlphaJITCodeEmitterPass(AlphaTargetMachine &TM,
66 JITCodeEmitter &JCE) {
Chris Lattner782ebe42010-02-02 21:35:47 +000067 return new AlphaCodeEmitter(JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000068}
69
Chris Lattner782ebe42010-02-02 21:35:47 +000070bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +000071 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
72
Chris Lattner43b429b2006-05-02 18:27:26 +000073 do {
Chris Lattner43b429b2006-05-02 18:27:26 +000074 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +000075 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
76 emitBasicBlock(*I);
77 } while (MCE.finishFunction(MF));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000078
Andrew Lenharth0934ae02005-07-22 20:52:16 +000079 return false;
80}
81
Chris Lattner782ebe42010-02-02 21:35:47 +000082void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +000083 MCE.StartMachineBasicBlock(&MBB);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000084 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
85 I != E; ++I) {
Evan Chengacff3392008-09-02 06:51:36 +000086 const MachineInstr &MI = *I;
Devang Patelaf0e2722009-10-06 02:19:11 +000087 MCE.processDebugLoc(MI.getDebugLoc(), true);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000088 switch(MI.getOpcode()) {
89 default:
Chris Lattnerd3f0aef2006-05-02 19:14:47 +000090 MCE.emitWordLE(getBinaryCodeForInstr(*I));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000091 break;
92 case Alpha::ALTENT:
93 case Alpha::PCLABEL:
94 case Alpha::MEMLABEL:
Evan Chengd1833072008-03-17 06:56:52 +000095 case TargetInstrInfo::IMPLICIT_DEF:
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +000096 case TargetInstrInfo::KILL:
Andrew Lenharth0934ae02005-07-22 20:52:16 +000097 break; //skip these
98 }
Devang Patelaf0e2722009-10-06 02:19:11 +000099 MCE.processDebugLoc(MI.getDebugLoc(), false);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000100 }
101}
102
103static unsigned getAlphaRegNumber(unsigned Reg) {
104 switch (Reg) {
105 case Alpha::R0 : case Alpha::F0 : return 0;
106 case Alpha::R1 : case Alpha::F1 : return 1;
107 case Alpha::R2 : case Alpha::F2 : return 2;
108 case Alpha::R3 : case Alpha::F3 : return 3;
109 case Alpha::R4 : case Alpha::F4 : return 4;
110 case Alpha::R5 : case Alpha::F5 : return 5;
111 case Alpha::R6 : case Alpha::F6 : return 6;
112 case Alpha::R7 : case Alpha::F7 : return 7;
113 case Alpha::R8 : case Alpha::F8 : return 8;
114 case Alpha::R9 : case Alpha::F9 : return 9;
115 case Alpha::R10 : case Alpha::F10 : return 10;
116 case Alpha::R11 : case Alpha::F11 : return 11;
117 case Alpha::R12 : case Alpha::F12 : return 12;
118 case Alpha::R13 : case Alpha::F13 : return 13;
119 case Alpha::R14 : case Alpha::F14 : return 14;
120 case Alpha::R15 : case Alpha::F15 : return 15;
121 case Alpha::R16 : case Alpha::F16 : return 16;
122 case Alpha::R17 : case Alpha::F17 : return 17;
123 case Alpha::R18 : case Alpha::F18 : return 18;
124 case Alpha::R19 : case Alpha::F19 : return 19;
125 case Alpha::R20 : case Alpha::F20 : return 20;
126 case Alpha::R21 : case Alpha::F21 : return 21;
127 case Alpha::R22 : case Alpha::F22 : return 22;
128 case Alpha::R23 : case Alpha::F23 : return 23;
129 case Alpha::R24 : case Alpha::F24 : return 24;
130 case Alpha::R25 : case Alpha::F25 : return 25;
131 case Alpha::R26 : case Alpha::F26 : return 26;
132 case Alpha::R27 : case Alpha::F27 : return 27;
133 case Alpha::R28 : case Alpha::F28 : return 28;
134 case Alpha::R29 : case Alpha::F29 : return 29;
135 case Alpha::R30 : case Alpha::F30 : return 30;
136 case Alpha::R31 : case Alpha::F31 : return 31;
137 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000138 llvm_unreachable("Unhandled reg");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000139 }
140}
141
Evan Chengacff3392008-09-02 06:51:36 +0000142unsigned AlphaCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Chris Lattner705e07f2009-08-23 03:41:05 +0000143 const MachineOperand &MO) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000144
Evan Chengacff3392008-09-02 06:51:36 +0000145 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
146 // or things that get fixed up later by the JIT.
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000147
Dan Gohmand735b802008-10-03 15:45:36 +0000148 if (MO.isReg()) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000149 rv = getAlphaRegNumber(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000150 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000151 rv = MO.getImm();
Dan Gohmand735b802008-10-03 15:45:36 +0000152 } else if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000153 DEBUG(errs() << MO << " is a relocated op for " << MI << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000154 unsigned Reloc = 0;
155 int Offset = 0;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000156 bool useGOT = false;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000157 switch (MI.getOpcode()) {
Andrew Lenharth98169be2005-07-28 18:14:47 +0000158 case Alpha::BSR:
159 Reloc = Alpha::reloc_bsr;
160 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000161 case Alpha::LDLr:
162 case Alpha::LDQr:
163 case Alpha::LDBUr:
164 case Alpha::LDWUr:
165 case Alpha::LDSr:
166 case Alpha::LDTr:
167 case Alpha::LDAr:
Andrew Lenharth81b5a3c2005-11-16 21:15:53 +0000168 case Alpha::STQr:
169 case Alpha::STLr:
170 case Alpha::STWr:
171 case Alpha::STBr:
172 case Alpha::STSr:
173 case Alpha::STTr:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000174 Reloc = Alpha::reloc_gprellow;
175 break;
176 case Alpha::LDAHr:
177 Reloc = Alpha::reloc_gprelhigh;
178 break;
179 case Alpha::LDQl:
180 Reloc = Alpha::reloc_literal;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000181 useGOT = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000182 break;
183 case Alpha::LDAg:
184 case Alpha::LDAHg:
185 Reloc = Alpha::reloc_gpdist;
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000186 Offset = MI.getOperand(3).getImm();
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000187 break;
188 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000189 llvm_unreachable("unknown relocatable instruction");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000190 }
Dan Gohmand735b802008-10-03 15:45:36 +0000191 if (MO.isGlobal())
Chris Lattner5a032de2006-05-03 20:30:20 +0000192 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000193 Reloc, MO.getGlobal(), Offset,
Evan Cheng02aabbf2008-01-03 02:56:28 +0000194 isa<Function>(MO.getGlobal()),
195 useGOT));
Dan Gohmand735b802008-10-03 15:45:36 +0000196 else if (MO.isSymbol())
Chris Lattner5a032de2006-05-03 20:30:20 +0000197 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000198 Reloc, MO.getSymbolName(),
199 Offset, true));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000200 else
Chris Lattner8aa797a2007-12-30 23:10:15 +0000201 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
202 Reloc, MO.getIndex(), Offset));
Dan Gohmand735b802008-10-03 15:45:36 +0000203 } else if (MO.isMBB()) {
Evan Chengf141cc42006-07-27 18:21:10 +0000204 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner8aa797a2007-12-30 23:10:15 +0000205 Alpha::reloc_bsr, MO.getMBB()));
Torok Edwin804e0fe2009-07-08 19:04:27 +0000206 } else {
Torok Edwindac237e2009-07-08 20:53:28 +0000207#ifndef NDEBUG
Chris Lattner705e07f2009-08-23 03:41:05 +0000208 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000209#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000210 llvm_unreachable(0);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000211 }
212
213 return rv;
214}
215
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000216#include "AlphaGenCodeEmitter.inc"