LLVM API Documentation

AlphaCodeEmitter.cpp

Go to the documentation of this file.
00001 //===-- Alpha/AlphaCodeEmitter.cpp - Convert Alpha code to machine code ---===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the pass that transforms the Alpha machine instructions
00011 // into relocatable machine code.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "alpha-emitter"
00016 #include "AlphaTargetMachine.h"
00017 #include "AlphaRelocations.h"
00018 #include "Alpha.h"
00019 #include "llvm/PassManager.h"
00020 #include "llvm/CodeGen/MachineCodeEmitter.h"
00021 #include "llvm/CodeGen/JITCodeEmitter.h"
00022 #include "llvm/CodeGen/ObjectCodeEmitter.h"
00023 #include "llvm/CodeGen/MachineFunctionPass.h"
00024 #include "llvm/CodeGen/MachineInstr.h"
00025 #include "llvm/CodeGen/Passes.h"
00026 #include "llvm/Function.h"
00027 #include "llvm/Support/Compiler.h"
00028 #include "llvm/Support/Debug.h"
00029 #include "llvm/Support/ErrorHandling.h"
00030 #include "llvm/Support/raw_ostream.h"
00031 using namespace llvm;
00032 
00033 namespace {
00034     
00035   class AlphaCodeEmitter {
00036     MachineCodeEmitter &MCE;
00037   public:
00038     AlphaCodeEmitter(MachineCodeEmitter &mce) : MCE(mce) {}
00039 
00040     /// getBinaryCodeForInstr - This function, generated by the
00041     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
00042     /// machine instructions.
00043 
00044     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
00045 
00046     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
00047 
00048     unsigned getMachineOpValue(const MachineInstr &MI,
00049                                const MachineOperand &MO);
00050   };
00051 
00052   template <class CodeEmitter>
00053   class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
00054       public AlphaCodeEmitter
00055   {
00056     const AlphaInstrInfo  *II;
00057     TargetMachine         &TM;
00058     CodeEmitter           &MCE;
00059 
00060   public:
00061     static char ID;
00062     explicit Emitter(TargetMachine &tm, CodeEmitter &mce)
00063       : MachineFunctionPass(&ID), AlphaCodeEmitter(mce), 
00064         II(0), TM(tm), MCE(mce) {}
00065     Emitter(TargetMachine &tm, CodeEmitter &mce, const AlphaInstrInfo& ii)
00066       : MachineFunctionPass(&ID), AlphaCodeEmitter(mce),
00067         II(&ii), TM(tm), MCE(mce) {}
00068 
00069     bool runOnMachineFunction(MachineFunction &MF);
00070 
00071     virtual const char *getPassName() const {
00072       return "Alpha Machine Code Emitter";
00073     }
00074 
00075   private:
00076     void emitBasicBlock(MachineBasicBlock &MBB);
00077   };
00078 
00079   template <class CodeEmitter>
00080     char Emitter<CodeEmitter>::ID = 0;
00081 }
00082 
00083 /// createAlphaCodeEmitterPass - Return a pass that emits the collected Alpha
00084 /// code to the specified MCE object.
00085 
00086 FunctionPass *llvm::createAlphaCodeEmitterPass(AlphaTargetMachine &TM,
00087                                                MachineCodeEmitter &MCE) {
00088   return new Emitter<MachineCodeEmitter>(TM, MCE);
00089 }
00090 
00091 FunctionPass *llvm::createAlphaJITCodeEmitterPass(AlphaTargetMachine &TM,
00092                                                   JITCodeEmitter &JCE) {
00093   return new Emitter<JITCodeEmitter>(TM, JCE);
00094 }
00095 FunctionPass *llvm::createAlphaObjectCodeEmitterPass(AlphaTargetMachine &TM,
00096                                                      ObjectCodeEmitter &OCE) {
00097   return new Emitter<ObjectCodeEmitter>(TM, OCE);
00098 }
00099 
00100 template <class CodeEmitter>
00101 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
00102   II = ((AlphaTargetMachine&)MF.getTarget()).getInstrInfo();
00103 
00104   do {
00105     MCE.startFunction(MF);
00106     for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
00107       emitBasicBlock(*I);
00108   } while (MCE.finishFunction(MF));
00109 
00110   return false;
00111 }
00112 
00113 template <class CodeEmitter>
00114 void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
00115   MCE.StartMachineBasicBlock(&MBB);
00116   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
00117        I != E; ++I) {
00118     const MachineInstr &MI = *I;
00119     MCE.processDebugLoc(MI.getDebugLoc());
00120     switch(MI.getOpcode()) {
00121     default:
00122       MCE.emitWordLE(getBinaryCodeForInstr(*I));
00123       break;
00124     case Alpha::ALTENT:
00125     case Alpha::PCLABEL:
00126     case Alpha::MEMLABEL:
00127     case TargetInstrInfo::IMPLICIT_DEF:
00128       break; //skip these
00129     }
00130   }
00131 }
00132 
00133 static unsigned getAlphaRegNumber(unsigned Reg) {
00134   switch (Reg) {
00135   case Alpha::R0  : case Alpha::F0  : return 0;
00136   case Alpha::R1  : case Alpha::F1  : return 1;
00137   case Alpha::R2  : case Alpha::F2  : return 2;
00138   case Alpha::R3  : case Alpha::F3  : return 3;
00139   case Alpha::R4  : case Alpha::F4  : return 4;
00140   case Alpha::R5  : case Alpha::F5  : return 5;
00141   case Alpha::R6  : case Alpha::F6  : return 6;
00142   case Alpha::R7  : case Alpha::F7  : return 7;
00143   case Alpha::R8  : case Alpha::F8  : return 8;
00144   case Alpha::R9  : case Alpha::F9  : return 9;
00145   case Alpha::R10 : case Alpha::F10 : return 10;
00146   case Alpha::R11 : case Alpha::F11 : return 11;
00147   case Alpha::R12 : case Alpha::F12 : return 12;
00148   case Alpha::R13 : case Alpha::F13 : return 13;
00149   case Alpha::R14 : case Alpha::F14 : return 14;
00150   case Alpha::R15 : case Alpha::F15 : return 15;
00151   case Alpha::R16 : case Alpha::F16 : return 16;
00152   case Alpha::R17 : case Alpha::F17 : return 17;
00153   case Alpha::R18 : case Alpha::F18 : return 18;
00154   case Alpha::R19 : case Alpha::F19 : return 19;
00155   case Alpha::R20 : case Alpha::F20 : return 20;
00156   case Alpha::R21 : case Alpha::F21 : return 21;
00157   case Alpha::R22 : case Alpha::F22 : return 22;
00158   case Alpha::R23 : case Alpha::F23 : return 23;
00159   case Alpha::R24 : case Alpha::F24 : return 24;
00160   case Alpha::R25 : case Alpha::F25 : return 25;
00161   case Alpha::R26 : case Alpha::F26 : return 26;
00162   case Alpha::R27 : case Alpha::F27 : return 27;
00163   case Alpha::R28 : case Alpha::F28 : return 28;
00164   case Alpha::R29 : case Alpha::F29 : return 29;
00165   case Alpha::R30 : case Alpha::F30 : return 30;
00166   case Alpha::R31 : case Alpha::F31 : return 31;
00167   default:
00168     llvm_unreachable("Unhandled reg");
00169   }
00170 }
00171 
00172 unsigned AlphaCodeEmitter::getMachineOpValue(const MachineInstr &MI,
00173                                            const MachineOperand &MO) {
00174 
00175   unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
00176                    // or things that get fixed up later by the JIT.
00177 
00178   if (MO.isReg()) {
00179     rv = getAlphaRegNumber(MO.getReg());
00180   } else if (MO.isImm()) {
00181     rv = MO.getImm();
00182   } else if (MO.isGlobal() || MO.isSymbol() || MO.isCPI()) {
00183     DOUT << MO << " is a relocated op for " << MI << "\n";
00184     unsigned Reloc = 0;
00185     int Offset = 0;
00186     bool useGOT = false;
00187     switch (MI.getOpcode()) {
00188     case Alpha::BSR:
00189       Reloc = Alpha::reloc_bsr;
00190       break;
00191     case Alpha::LDLr:
00192     case Alpha::LDQr:
00193     case Alpha::LDBUr:
00194     case Alpha::LDWUr:
00195     case Alpha::LDSr:
00196     case Alpha::LDTr:
00197     case Alpha::LDAr:
00198     case Alpha::STQr:
00199     case Alpha::STLr:
00200     case Alpha::STWr:
00201     case Alpha::STBr:
00202     case Alpha::STSr:
00203     case Alpha::STTr:
00204       Reloc = Alpha::reloc_gprellow;
00205       break;
00206     case Alpha::LDAHr:
00207       Reloc = Alpha::reloc_gprelhigh;
00208       break;
00209     case Alpha::LDQl:
00210       Reloc = Alpha::reloc_literal;
00211       useGOT = true;
00212       break;
00213     case Alpha::LDAg:
00214     case Alpha::LDAHg:
00215       Reloc = Alpha::reloc_gpdist;
00216       Offset = MI.getOperand(3).getImm();
00217       break;
00218     default:
00219       llvm_unreachable("unknown relocatable instruction");
00220     }
00221     if (MO.isGlobal())
00222       MCE.addRelocation(MachineRelocation::getGV(MCE.getCurrentPCOffset(),
00223                                                  Reloc, MO.getGlobal(), Offset,
00224                                                  isa<Function>(MO.getGlobal()),
00225                                                  useGOT));
00226     else if (MO.isSymbol())
00227       MCE.addRelocation(MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
00228                                                      Reloc, MO.getSymbolName(),
00229                                                      Offset, true));
00230     else
00231      MCE.addRelocation(MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
00232                                           Reloc, MO.getIndex(), Offset));
00233   } else if (MO.isMBB()) {
00234     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
00235                                                Alpha::reloc_bsr, MO.getMBB()));
00236   } else {
00237 #ifndef NDEBUG
00238     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
00239 #endif
00240     llvm_unreachable(0);
00241   }
00242 
00243   return rv;
00244 }
00245 
00246 #include "AlphaGenCodeEmitter.inc"