1 //===-- Alpha/AlphaCodeEmitter.cpp - Convert Alpha code to machine code ---===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the pass that transforms the Alpha machine instructions
11 // into relocatable machine code.
13 //===----------------------------------------------------------------------===//
15 #include "AlphaTargetMachine.h"
16 #include "AlphaRelocations.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"
31 NumEmitted("alpha-emitter", "Number of machine instructions emitted");
35 class AlphaCodeEmitter : public MachineFunctionPass {
36 const AlphaInstrInfo *II;
38 MachineCodeEmitter &MCE;
40 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
42 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
45 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) {}
51 bool runOnMachineFunction(MachineFunction &MF);
53 virtual const char *getPassName() const {
54 return "Alpha Machine Code Emitter";
57 void emitInstruction(const MachineInstr &MI);
59 /// getBinaryCodeForInstr - This function, generated by the
60 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
61 /// machine instructions.
63 unsigned getBinaryCodeForInstr(MachineInstr &MI);
66 void emitBasicBlock(MachineBasicBlock &MBB);
71 /// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha code
72 /// to the specified MCE object.
73 FunctionPass *llvm::createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
74 MachineCodeEmitter &MCE) {
75 return new AlphaCodeEmitter(TM, MCE);
78 bool AlphaCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
79 II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
82 MCE.startFunction(MF);
83 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
85 } while (MCE.finishFunction(MF));
90 void AlphaCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
91 MCE.StartMachineBasicBlock(&MBB);
92 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
94 MachineInstr &MI = *I;
95 unsigned Opcode = MI.getOpcode();
96 switch(MI.getOpcode()) {
98 MCE.emitWordLE(getBinaryCodeForInstr(*I));
102 case Alpha::MEMLABEL:
104 case Alpha::IDEF_F32:
105 case Alpha::IDEF_F64:
111 static unsigned getAlphaRegNumber(unsigned Reg) {
113 case Alpha::R0 : case Alpha::F0 : return 0;
114 case Alpha::R1 : case Alpha::F1 : return 1;
115 case Alpha::R2 : case Alpha::F2 : return 2;
116 case Alpha::R3 : case Alpha::F3 : return 3;
117 case Alpha::R4 : case Alpha::F4 : return 4;
118 case Alpha::R5 : case Alpha::F5 : return 5;
119 case Alpha::R6 : case Alpha::F6 : return 6;
120 case Alpha::R7 : case Alpha::F7 : return 7;
121 case Alpha::R8 : case Alpha::F8 : return 8;
122 case Alpha::R9 : case Alpha::F9 : return 9;
123 case Alpha::R10 : case Alpha::F10 : return 10;
124 case Alpha::R11 : case Alpha::F11 : return 11;
125 case Alpha::R12 : case Alpha::F12 : return 12;
126 case Alpha::R13 : case Alpha::F13 : return 13;
127 case Alpha::R14 : case Alpha::F14 : return 14;
128 case Alpha::R15 : case Alpha::F15 : return 15;
129 case Alpha::R16 : case Alpha::F16 : return 16;
130 case Alpha::R17 : case Alpha::F17 : return 17;
131 case Alpha::R18 : case Alpha::F18 : return 18;
132 case Alpha::R19 : case Alpha::F19 : return 19;
133 case Alpha::R20 : case Alpha::F20 : return 20;
134 case Alpha::R21 : case Alpha::F21 : return 21;
135 case Alpha::R22 : case Alpha::F22 : return 22;
136 case Alpha::R23 : case Alpha::F23 : return 23;
137 case Alpha::R24 : case Alpha::F24 : return 24;
138 case Alpha::R25 : case Alpha::F25 : return 25;
139 case Alpha::R26 : case Alpha::F26 : return 26;
140 case Alpha::R27 : case Alpha::F27 : return 27;
141 case Alpha::R28 : case Alpha::F28 : return 28;
142 case Alpha::R29 : case Alpha::F29 : return 29;
143 case Alpha::R30 : case Alpha::F30 : return 30;
144 case Alpha::R31 : case Alpha::F31 : return 31;
146 assert(0 && "Unhandled reg");
151 int AlphaCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
153 int rv = 0; // Return value; defaults to 0 for unhandled cases
154 // or things that get fixed up later by the JIT.
156 if (MO.isRegister()) {
157 rv = getAlphaRegNumber(MO.getReg());
158 } else if (MO.isImmediate()) {
159 rv = MO.getImmedValue();
160 } else if (MO.isGlobalAddress() || MO.isExternalSymbol()
161 || MO.isConstantPoolIndex()) {
162 DEBUG(std::cerr << MO << " is a relocated op for " << MI << "\n";);
163 bool isExternal = MO.isExternalSymbol() ||
164 (MO.isGlobalAddress() &&
165 ( MO.getGlobal()->hasWeakLinkage() ||
166 MO.getGlobal()->isExternal()) );
170 switch (MI.getOpcode()) {
172 Reloc = Alpha::reloc_bsr;
187 Reloc = Alpha::reloc_gprellow;
190 Reloc = Alpha::reloc_gprelhigh;
193 Reloc = Alpha::reloc_literal;
198 Reloc = Alpha::reloc_gpdist;
199 Offset = MI.getOperand(3).getImmedValue();
202 assert(0 && "unknown relocatable instruction");
205 if (MO.isGlobalAddress())
206 MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
207 Reloc, MO.getGlobal(), Offset,
209 else if (MO.isExternalSymbol())
210 MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
211 Reloc, MO.getSymbolName(), Offset,
214 MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
215 Reloc, MO.getConstantPoolIndex(),
217 } else if (MO.isMachineBasicBlock()) {
218 TM.getJITInfo()->addBBRef(MO.getMachineBasicBlock(),
219 MCE.getCurrentPCValue());
221 std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
229 #include "AlphaGenCodeEmitter.inc"