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



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.