Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 1 | //===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 10 | // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to |
| 11 | // JIT-compile bytecode to native PowerPC. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 15 | #include "PPC32JITInfo.h" |
| 16 | #include "PPC32TargetMachine.h" |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 18 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 19 | #include "llvm/CodeGen/Passes.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 21 | |
| 22 | namespace llvm { |
| 23 | |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 24 | namespace { |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 25 | class PPC32CodeEmitter : public MachineFunctionPass { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 26 | TargetMachine &TM; |
| 27 | MachineCodeEmitter &MCE; |
| 28 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 29 | int64_t getMachineOpValue(MachineInstr &MI, MachineOperand &MO); |
| 30 | |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 31 | public: |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 32 | PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M) |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 33 | : TM(T), MCE(M) {} |
| 34 | |
| 35 | const char *getPassName() const { return "PowerPC Machine Code Emitter"; } |
| 36 | |
| 37 | /// runOnMachineFunction - emits the given MachineFunction to memory |
| 38 | /// |
| 39 | bool runOnMachineFunction(MachineFunction &MF); |
| 40 | |
| 41 | /// emitBasicBlock - emits the given MachineBasicBlock to memory |
| 42 | /// |
| 43 | void emitBasicBlock(MachineBasicBlock &MBB); |
| 44 | |
| 45 | /// emitWord - write a 32-bit word to memory at the current PC |
| 46 | /// |
| 47 | void emitWord(unsigned w) { MCE.emitWord(w); } |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 48 | |
| 49 | /// getValueBit - return the particular bit of Val |
| 50 | /// |
| 51 | unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; } |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 52 | |
| 53 | /// getBinaryCodeForInstr - returns the assembled code for an instruction |
| 54 | /// |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 55 | unsigned getBinaryCodeForInstr(MachineInstr &MI); |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 56 | }; |
| 57 | } |
| 58 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 59 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get |
| 60 | /// machine code emitted. This uses a MachineCodeEmitter object to handle |
| 61 | /// actually outputting the machine code and resolving things like the address |
| 62 | /// of functions. This method should returns true if machine code emission is |
| 63 | /// not supported. |
| 64 | /// |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 65 | bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM, |
| 66 | MachineCodeEmitter &MCE) { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 67 | // Machine code emitter pass for PowerPC |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 68 | PM.add(new PPC32CodeEmitter(*this, MCE)); |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 69 | // Delete machine code for this function after emitting it |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 70 | PM.add(createMachineCodeDeleter()); |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame^] | 71 | return false; |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 74 | bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 75 | MCE.startFunction(MF); |
| 76 | MCE.emitConstantPool(MF.getConstantPool()); |
| 77 | for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) |
| 78 | emitBasicBlock(*I); |
| 79 | MCE.finishFunction(MF); |
| 80 | return false; |
| 81 | } |
| 82 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 83 | void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 84 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I) |
| 85 | emitWord(getBinaryCodeForInstr(*I)); |
| 86 | } |
| 87 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 88 | int64_t PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI, |
| 89 | MachineOperand &MO) { |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame^] | 90 | int64_t rv = 0; // Return value; defaults to 0 for unhandled cases |
| 91 | // or things that get fixed up later by the JIT. |
| 92 | if (MO.isPCRelativeDisp()) { |
| 93 | std::cerr << "PPC32CodeEmitter: PC-relative disp unhandled\n"; |
| 94 | abort(); |
| 95 | } else if (MO.isRegister()) { |
| 96 | rv = MO.getReg(); |
| 97 | } else if (MO.isImmediate()) { |
| 98 | rv = MO.getImmedValue(); |
| 99 | #if 0 |
| 100 | } else if (MO.isGlobalAddress()) { |
| 101 | } else if (MO.isMachineBasicBlock()) { |
| 102 | MachineBasicBlock *MBB = MO.getMachineBasicBlock(); |
| 103 | } else if (MO.isExternalSymbol()) { |
| 104 | } else if (MO.isFrameIndex()) { |
| 105 | unsigned index = MO.getFrameIndex(); |
| 106 | } else if (MO.isConstantPoolIndex()) { |
| 107 | unsigned index = MO.getCosntantPoolIndex(); |
| 108 | #endif |
| 109 | } else { |
| 110 | std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
| 111 | abort(); |
| 112 | } |
| 113 | |
| 114 | return rv; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 117 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 118 | void *PPC32JITInfo::getJITStubForFunction(Function *F, |
| 119 | MachineCodeEmitter &MCE) { |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 120 | std::cerr << "PPC32JITInfo::getJITStubForFunction not implemented\n"; |
| 121 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 125 | void PPC32JITInfo::replaceMachineCodeForFunction (void *Old, void *New) { |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 126 | std::cerr << "PPC32JITInfo::replaceMachineCodeForFunction not implemented\n"; |
| 127 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 130 | #include "PPC32GenCodeEmitter.inc" |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 131 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 132 | } // end llvm namespace |
| 133 | |