1 //===-- MachineInstr.cpp --------------------------------------------------===//
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 // Methods common to all machine instructions.
12 // FIXME: Now that MachineInstrs have parent pointers, they should always
13 // print themselves using their MachineFunction's TargetMachine.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/Value.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/Target/TargetInstrInfo.h"
22 #include "llvm/Target/MRegisterInfo.h"
23 #include "llvm/Support/LeakDetector.h"
28 // Global variable holding an array of descriptors for machine instructions.
29 // The actual object needs to be created separately for each target machine.
30 // This variable is initialized and reset by class TargetInstrInfo.
32 // FIXME: This should be a property of the target so that more than one target
33 // at a time can be active...
36 extern const TargetInstrDescriptor *TargetInstrDescriptors;
39 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
40 /// not a resize for them. It is expected that if you use this that you call
41 /// add* methods below to fill up the operands, instead of the Set methods.
42 /// Eventually, the "resizing" ctors will be phased out.
44 MachineInstr::MachineInstr(short opcode, unsigned numOperands)
45 : Opcode(opcode), parent(0) {
46 Operands.reserve(numOperands);
47 // Make sure that we get added to a machine basicblock
48 LeakDetector::addGarbageObject(this);
51 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
52 /// MachineInstr is created and added to the end of the specified basic block.
54 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
56 : Opcode(opcode), parent(0) {
57 assert(MBB && "Cannot use inserting ctor with null basic block!");
58 Operands.reserve(numOperands);
59 // Make sure that we get added to a machine basicblock
60 LeakDetector::addGarbageObject(this);
61 MBB->push_back(this); // Add instruction to end of basic block!
64 /// MachineInstr ctor - Copies MachineInstr arg exactly
66 MachineInstr::MachineInstr(const MachineInstr &MI) {
67 Opcode = MI.getOpcode();
68 Operands.reserve(MI.getNumOperands());
71 for (unsigned i = 0; i != MI.getNumOperands(); ++i)
72 Operands.push_back(MI.getOperand(i));
74 // Set parent, next, and prev to null
81 MachineInstr::~MachineInstr() {
82 LeakDetector::removeGarbageObject(this);
85 /// removeFromParent - This method unlinks 'this' from the containing basic
86 /// block, and returns it, but does not delete it.
87 MachineInstr *MachineInstr::removeFromParent() {
88 assert(getParent() && "Not embedded in a basic block!");
89 getParent()->remove(this);
94 /// OperandComplete - Return true if it's illegal to add a new operand
96 bool MachineInstr::OperandsComplete() const {
97 int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
98 if (NumOperands >= 0 && getNumOperands() >= (unsigned)NumOperands)
99 return true; // Broken: we have all the operands of this instruction!
103 void MachineInstr::dump() const {
104 std::cerr << " " << *this;
107 static inline void OutputReg(std::ostream &os, unsigned RegNo,
108 const MRegisterInfo *MRI = 0) {
109 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
111 os << "%" << MRI->get(RegNo).Name;
113 os << "%mreg(" << RegNo << ")";
115 os << "%reg" << RegNo;
118 static void print(const MachineOperand &MO, std::ostream &OS,
119 const TargetMachine *TM) {
120 const MRegisterInfo *MRI = 0;
122 if (TM) MRI = TM->getRegisterInfo();
124 switch (MO.getType()) {
125 case MachineOperand::MO_Register:
126 OutputReg(OS, MO.getReg(), MRI);
128 case MachineOperand::MO_Immediate:
129 OS << (long)MO.getImmedValue();
131 case MachineOperand::MO_MachineBasicBlock:
133 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
134 << "," << (void*)MO.getMachineBasicBlock() << ">";
136 case MachineOperand::MO_FrameIndex:
137 OS << "<fi#" << MO.getFrameIndex() << ">";
139 case MachineOperand::MO_ConstantPoolIndex:
140 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
142 case MachineOperand::MO_JumpTableIndex:
143 OS << "<jt#" << MO.getJumpTableIndex() << ">";
145 case MachineOperand::MO_GlobalAddress:
146 OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
147 if (MO.getOffset()) OS << "+" << MO.getOffset();
150 case MachineOperand::MO_ExternalSymbol:
151 OS << "<es:" << MO.getSymbolName();
152 if (MO.getOffset()) OS << "+" << MO.getOffset();
156 assert(0 && "Unrecognized operand type");
160 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
161 unsigned StartOp = 0;
163 // Specialize printing if op#0 is definition
164 if (getNumOperands() && getOperand(0).isDef() && !getOperand(0).isUse()) {
165 ::print(getOperand(0), OS, TM);
167 ++StartOp; // Don't print this operand again!
170 // Must check if Target machine is not null because machine BB could not
171 // be attached to a Machine function yet
173 OS << TM->getInstrInfo()->getName(getOpcode());
175 for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
176 const MachineOperand& mop = getOperand(i);
180 ::print(mop, OS, TM);
192 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
193 // If the instruction is embedded into a basic block, we can find the target
194 // info for the instruction.
195 if (const MachineBasicBlock *MBB = MI.getParent()) {
196 const MachineFunction *MF = MBB->getParent();
198 MI.print(os, &MF->getTarget());
204 // Otherwise, print it out in the "raw" format without symbolic register names
206 os << TargetInstrDescriptors[MI.getOpcode()].Name;
208 for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
209 os << "\t" << MI.getOperand(i);
210 if (MI.getOperand(i).isDef())
211 if (MI.getOperand(i).isUse())
220 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
221 switch (MO.getType()) {
222 case MachineOperand::MO_Register:
223 OutputReg(OS, MO.getReg());
225 case MachineOperand::MO_Immediate:
226 OS << (long)MO.getImmedValue();
228 case MachineOperand::MO_MachineBasicBlock:
230 << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
231 << "@" << (void*)MO.getMachineBasicBlock() << ">";
233 case MachineOperand::MO_FrameIndex:
234 OS << "<fi#" << MO.getFrameIndex() << ">";
236 case MachineOperand::MO_ConstantPoolIndex:
237 OS << "<cp#" << MO.getConstantPoolIndex() << ">";
239 case MachineOperand::MO_JumpTableIndex:
240 OS << "<jt#" << MO.getJumpTableIndex() << ">";
242 case MachineOperand::MO_GlobalAddress:
243 OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
245 case MachineOperand::MO_ExternalSymbol:
246 OS << "<es:" << MO.getSymbolName() << ">";
249 assert(0 && "Unrecognized operand type");