blob: ccce154afb385cd599a764cf597e47b3ce8731fb [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 {
Daniel Dunbara279bc32009-09-20 02:20:51 +000031
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000032 class AlphaCodeEmitter {
33 MachineCodeEmitter &MCE;
34 public:
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000035 AlphaCodeEmitter(MachineCodeEmitter &mce) : MCE(mce) {}
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000036
37 /// getBinaryCodeForInstr - This function, generated by the
38 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
39 /// machine instructions.
40
41 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000042
43 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000044
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000045 unsigned getMachineOpValue(const MachineInstr &MI,
46 const MachineOperand &MO);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000047 };
48
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000049 template <class CodeEmitter>
Nick Lewycky6726b6d2009-10-25 06:33:48 +000050 class Emitter : public MachineFunctionPass, public AlphaCodeEmitter
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000051 {
52 const AlphaInstrInfo *II;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000053 TargetMachine &TM;
54 CodeEmitter &MCE;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000055
56 public:
Devang Patel19974732007-05-03 01:11:54 +000057 static char ID;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000058 explicit Emitter(TargetMachine &tm, CodeEmitter &mce)
Daniel Dunbara279bc32009-09-20 02:20:51 +000059 : MachineFunctionPass(&ID), AlphaCodeEmitter(mce),
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000060 II(0), TM(tm), MCE(mce) {}
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000061 Emitter(TargetMachine &tm, CodeEmitter &mce, const AlphaInstrInfo& ii)
62 : MachineFunctionPass(&ID), AlphaCodeEmitter(mce),
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000063 II(&ii), TM(tm), MCE(mce) {}
Andrew Lenharth0934ae02005-07-22 20:52:16 +000064
65 bool runOnMachineFunction(MachineFunction &MF);
66
67 virtual const char *getPassName() const {
68 return "Alpha Machine Code Emitter";
69 }
70
Andrew Lenharth0934ae02005-07-22 20:52:16 +000071 private:
72 void emitBasicBlock(MachineBasicBlock &MBB);
Andrew Lenharth0934ae02005-07-22 20:52:16 +000073 };
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000074
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000075 template <class CodeEmitter>
76 char Emitter<CodeEmitter>::ID = 0;
Andrew Lenharth0934ae02005-07-22 20:52:16 +000077}
78
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000079/// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha
80/// code to the specified MCE object.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000081
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000082FunctionPass *llvm::createAlphaJITCodeEmitterPass(AlphaTargetMachine &TM,
83 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000084 return new Emitter<JITCodeEmitter>(TM, JCE);
85}
86
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000087template <class CodeEmitter>
88bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +000089 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
90
Chris Lattner43b429b2006-05-02 18:27:26 +000091 do {
Chris Lattner43b429b2006-05-02 18:27:26 +000092 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +000093 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
94 emitBasicBlock(*I);
95 } while (MCE.finishFunction(MF));
Andrew Lenharth0934ae02005-07-22 20:52:16 +000096
Andrew Lenharth0934ae02005-07-22 20:52:16 +000097 return false;
98}
99
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000100template <class CodeEmitter>
101void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +0000102 MCE.StartMachineBasicBlock(&MBB);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000103 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
104 I != E; ++I) {
Evan Chengacff3392008-09-02 06:51:36 +0000105 const MachineInstr &MI = *I;
Devang Patelaf0e2722009-10-06 02:19:11 +0000106 MCE.processDebugLoc(MI.getDebugLoc(), true);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000107 switch(MI.getOpcode()) {
108 default:
Chris Lattnerd3f0aef2006-05-02 19:14:47 +0000109 MCE.emitWordLE(getBinaryCodeForInstr(*I));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000110 break;
111 case Alpha::ALTENT:
112 case Alpha::PCLABEL:
113 case Alpha::MEMLABEL:
Evan Chengd1833072008-03-17 06:56:52 +0000114 case TargetInstrInfo::IMPLICIT_DEF:
Jakob Stoklund Olesen26207e52009-09-28 20:32:26 +0000115 case TargetInstrInfo::KILL:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000116 break; //skip these
117 }
Devang Patelaf0e2722009-10-06 02:19:11 +0000118 MCE.processDebugLoc(MI.getDebugLoc(), false);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000119 }
120}
121
122static unsigned getAlphaRegNumber(unsigned Reg) {
123 switch (Reg) {
124 case Alpha::R0 : case Alpha::F0 : return 0;
125 case Alpha::R1 : case Alpha::F1 : return 1;
126 case Alpha::R2 : case Alpha::F2 : return 2;
127 case Alpha::R3 : case Alpha::F3 : return 3;
128 case Alpha::R4 : case Alpha::F4 : return 4;
129 case Alpha::R5 : case Alpha::F5 : return 5;
130 case Alpha::R6 : case Alpha::F6 : return 6;
131 case Alpha::R7 : case Alpha::F7 : return 7;
132 case Alpha::R8 : case Alpha::F8 : return 8;
133 case Alpha::R9 : case Alpha::F9 : return 9;
134 case Alpha::R10 : case Alpha::F10 : return 10;
135 case Alpha::R11 : case Alpha::F11 : return 11;
136 case Alpha::R12 : case Alpha::F12 : return 12;
137 case Alpha::R13 : case Alpha::F13 : return 13;
138 case Alpha::R14 : case Alpha::F14 : return 14;
139 case Alpha::R15 : case Alpha::F15 : return 15;
140 case Alpha::R16 : case Alpha::F16 : return 16;
141 case Alpha::R17 : case Alpha::F17 : return 17;
142 case Alpha::R18 : case Alpha::F18 : return 18;
143 case Alpha::R19 : case Alpha::F19 : return 19;
144 case Alpha::R20 : case Alpha::F20 : return 20;
145 case Alpha::R21 : case Alpha::F21 : return 21;
146 case Alpha::R22 : case Alpha::F22 : return 22;
147 case Alpha::R23 : case Alpha::F23 : return 23;
148 case Alpha::R24 : case Alpha::F24 : return 24;
149 case Alpha::R25 : case Alpha::F25 : return 25;
150 case Alpha::R26 : case Alpha::F26 : return 26;
151 case Alpha::R27 : case Alpha::F27 : return 27;
152 case Alpha::R28 : case Alpha::F28 : return 28;
153 case Alpha::R29 : case Alpha::F29 : return 29;
154 case Alpha::R30 : case Alpha::F30 : return 30;
155 case Alpha::R31 : case Alpha::F31 : return 31;
156 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000157 llvm_unreachable("Unhandled reg");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000158 }
159}
160
Evan Chengacff3392008-09-02 06:51:36 +0000161unsigned AlphaCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Chris Lattner705e07f2009-08-23 03:41:05 +0000162 const MachineOperand &MO) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000163
Evan Chengacff3392008-09-02 06:51:36 +0000164 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
165 // or things that get fixed up later by the JIT.
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000166
Dan Gohmand735b802008-10-03 15:45:36 +0000167 if (MO.isReg()) {
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000168 rv = getAlphaRegNumber(MO.getReg());
Dan Gohmand735b802008-10-03 15:45:36 +0000169 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000170 rv = MO.getImm();
Dan Gohmand735b802008-10-03 15:45:36 +0000171 } else if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
Chris Lattner705e07f2009-08-23 03:41:05 +0000172 DEBUG(errs() << MO << " is a relocated op for " << MI << "\n");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000173 unsigned Reloc = 0;
174 int Offset = 0;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000175 bool useGOT = false;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000176 switch (MI.getOpcode()) {
Andrew Lenharth98169be2005-07-28 18:14:47 +0000177 case Alpha::BSR:
178 Reloc = Alpha::reloc_bsr;
179 break;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000180 case Alpha::LDLr:
181 case Alpha::LDQr:
182 case Alpha::LDBUr:
183 case Alpha::LDWUr:
184 case Alpha::LDSr:
185 case Alpha::LDTr:
186 case Alpha::LDAr:
Andrew Lenharth81b5a3c2005-11-16 21:15:53 +0000187 case Alpha::STQr:
188 case Alpha::STLr:
189 case Alpha::STWr:
190 case Alpha::STBr:
191 case Alpha::STSr:
192 case Alpha::STTr:
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000193 Reloc = Alpha::reloc_gprellow;
194 break;
195 case Alpha::LDAHr:
196 Reloc = Alpha::reloc_gprelhigh;
197 break;
198 case Alpha::LDQl:
199 Reloc = Alpha::reloc_literal;
Andrew Lenhartha4433e12005-07-28 12:45:20 +0000200 useGOT = true;
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000201 break;
202 case Alpha::LDAg:
203 case Alpha::LDAHg:
204 Reloc = Alpha::reloc_gpdist;
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000205 Offset = MI.getOperand(3).getImm();
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000206 break;
207 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000208 llvm_unreachable("unknown relocatable instruction");
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000209 }
Dan Gohmand735b802008-10-03 15:45:36 +0000210 if (MO.isGlobal())
Chris Lattner5a032de2006-05-03 20:30:20 +0000211 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000212 Reloc, MO.getGlobal(), Offset,
Evan Cheng02aabbf2008-01-03 02:56:28 +0000213 isa<Function>(MO.getGlobal()),
214 useGOT));
Dan Gohmand735b802008-10-03 15:45:36 +0000215 else if (MO.isSymbol())
Chris Lattner5a032de2006-05-03 20:30:20 +0000216 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000217 Reloc, MO.getSymbolName(),
218 Offset, true));
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000219 else
Chris Lattner8aa797a2007-12-30 23:10:15 +0000220 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
221 Reloc, MO.getIndex(), Offset));
Dan Gohmand735b802008-10-03 15:45:36 +0000222 } else if (MO.isMBB()) {
Evan Chengf141cc42006-07-27 18:21:10 +0000223 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner8aa797a2007-12-30 23:10:15 +0000224 Alpha::reloc_bsr, MO.getMBB()));
Torok Edwin804e0fe2009-07-08 19:04:27 +0000225 } else {
Torok Edwindac237e2009-07-08 20:53:28 +0000226#ifndef NDEBUG
Chris Lattner705e07f2009-08-23 03:41:05 +0000227 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000228#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000229 llvm_unreachable(0);
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000230 }
231
232 return rv;
233}
234
Andrew Lenharth0934ae02005-07-22 20:52:16 +0000235#include "AlphaGenCodeEmitter.inc"