Misha Brukman | 3de36f5 | 2003-05-27 20:07:58 +0000 | [diff] [blame^] | 1 | #include "llvm/PassManager.h" |
| 2 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 3 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 4 | #include "llvm/CodeGen/MachineInstr.h" |
| 5 | #include "SparcInternals.h" |
| 6 | |
| 7 | namespace { |
| 8 | class SparcV9CodeEmitter : public MachineFunctionPass { |
| 9 | MachineCodeEmitter &MCE; |
| 10 | |
| 11 | public: |
| 12 | SparcV9CodeEmitter(MachineCodeEmitter &M) : MCE(M) {} |
| 13 | |
| 14 | bool runOnMachineFunction(MachineFunction &F); |
| 15 | |
| 16 | private: |
| 17 | int64_t getMachineOpValue(MachineOperand &MO); |
| 18 | unsigned getValueBit(int64_t Val, unsigned bit); |
| 19 | |
| 20 | void emitConstant(unsigned Val, unsigned Size); |
| 21 | |
| 22 | void emitBasicBlock(MachineBasicBlock &MBB); |
| 23 | void emitInstruction(MachineInstr &MI); |
| 24 | |
| 25 | /// Function generated by the CodeEmitterGenerator using TableGen |
| 26 | /// |
| 27 | unsigned getBinaryCodeForInstr(MachineInstr &MI); |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | bool UltraSparc::addPassesToEmitMachineCode(PassManager &PM, |
| 32 | MachineCodeEmitter &MCE) { |
| 33 | //PM.add(new SparcV9CodeEmitter(MCE)); |
| 34 | //MachineCodeEmitter *M = MachineCodeEmitter::createDebugMachineCodeEmitter(); |
| 35 | MachineCodeEmitter *M = |
| 36 | MachineCodeEmitter::createFilePrinterMachineCodeEmitter(); |
| 37 | PM.add(new SparcV9CodeEmitter(*M)); |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | void SparcV9CodeEmitter::emitConstant(unsigned Val, unsigned Size) { |
| 42 | // Output the constant in big endian byte order... |
| 43 | unsigned byteVal; |
| 44 | for (int i = Size-1; i >= 0; --i) { |
| 45 | byteVal = Val >> 8*i; |
| 46 | MCE.emitByte(byteVal & 255); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | int64_t SparcV9CodeEmitter::getMachineOpValue(MachineOperand &MO) { |
| 51 | if (MO.isPhysicalRegister()) { |
| 52 | return MO.getReg(); |
| 53 | } else if (MO.isImmediate()) { |
| 54 | return MO.getImmedValue(); |
| 55 | } else if (MO.isPCRelativeDisp()) { |
| 56 | // FIXME!!! |
| 57 | //return MO.getPCRelativeDisp(); |
| 58 | return 0; |
| 59 | } else { |
| 60 | assert(0 && "Unknown type of MachineOperand"); |
| 61 | return 0; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | unsigned SparcV9CodeEmitter::getValueBit(int64_t Val, unsigned bit) { |
| 66 | Val >>= bit; |
| 67 | return (Val & 1); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | bool SparcV9CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
| 72 | MCE.startFunction(MF); |
| 73 | MCE.emitConstantPool(MF.getConstantPool()); |
| 74 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 75 | emitBasicBlock(*I); |
| 76 | MCE.finishFunction(MF); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | void SparcV9CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
| 81 | MCE.startBasicBlock(MBB); |
| 82 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I) |
| 83 | emitInstruction(**I); |
| 84 | } |
| 85 | |
| 86 | void SparcV9CodeEmitter::emitInstruction(MachineInstr &MI) { |
| 87 | emitConstant(getBinaryCodeForInstr(MI), 4); |
| 88 | } |
| 89 | |
| 90 | #include "SparcV9CodeEmitter.inc" |