182e664e675f700a130e0a51859519029e5c0638
[oota-llvm.git] / lib / CodeGen / MachineInstr.cpp
1 //===-- MachineInstr.cpp --------------------------------------------------===//
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 //
10 // Methods common to all machine instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/Target/TargetMachine.h"
17 #include "llvm/Target/TargetInstrInfo.h"
18 #include "llvm/Target/MRegisterInfo.h"
19 #include "llvm/Support/LeakDetector.h"
20 #include <iostream>
21
22 using namespace llvm;
23
24 // Global variable holding an array of descriptors for machine instructions.
25 // The actual object needs to be created separately for each target machine.
26 // This variable is initialized and reset by class TargetInstrInfo.
27 //
28 // FIXME: This should be a property of the target so that more than one target
29 // at a time can be active...
30 //
31 namespace llvm {
32   extern const TargetInstrDescriptor *TargetInstrDescriptors;
33 }
34
35 /// MachineInstr ctor - This constructor only does a _reserve_ of the operands,
36 /// not a resize for them.  It is expected that if you use this that you call
37 /// add* methods below to fill up the operands, instead of the Set methods.
38 /// Eventually, the "resizing" ctors will be phased out.
39 ///
40 MachineInstr::MachineInstr(short opcode, unsigned numOperands)
41   : Opcode(opcode), parent(0) {
42   Operands.reserve(numOperands);
43   // Make sure that we get added to a machine basicblock
44   LeakDetector::addGarbageObject(this);
45 }
46
47 /// MachineInstr ctor - Work exactly the same as the ctor above, except that the
48 /// MachineInstr is created and added to the end of the specified basic block.
49 ///
50 MachineInstr::MachineInstr(MachineBasicBlock *MBB, short opcode,
51                            unsigned numOperands)
52   : Opcode(opcode), parent(0) {
53   assert(MBB && "Cannot use inserting ctor with null basic block!");
54   Operands.reserve(numOperands);
55   // Make sure that we get added to a machine basicblock
56   LeakDetector::addGarbageObject(this);
57   MBB->push_back(this);  // Add instruction to end of basic block!
58 }
59
60 /// MachineInstr ctor - Copies MachineInstr arg exactly
61 ///
62 MachineInstr::MachineInstr(const MachineInstr &MI) {
63   Opcode = MI.getOpcode();
64   Operands.reserve(MI.getNumOperands());
65
66   // Add operands
67   for (unsigned i = 0; i != MI.getNumOperands(); ++i)
68     Operands.push_back(MI.getOperand(i));
69
70   // Set parent, next, and prev to null
71   parent = 0;
72   prev = 0;
73   next = 0;
74 }
75
76
77 MachineInstr::~MachineInstr() {
78   LeakDetector::removeGarbageObject(this);
79 }
80
81 /// removeFromParent - This method unlinks 'this' from the containing basic
82 /// block, and returns it, but does not delete it.
83 MachineInstr *MachineInstr::removeFromParent() {
84   assert(getParent() && "Not embedded in a basic block!");
85   getParent()->remove(this);
86   return this;
87 }
88
89
90 /// OperandComplete - Return true if it's illegal to add a new operand
91 ///
92 bool MachineInstr::OperandsComplete() const {
93   int NumOperands = TargetInstrDescriptors[Opcode].numOperands;
94   if ((TargetInstrDescriptors[Opcode].Flags & M_VARIABLE_OPS) == 0 &&
95       getNumOperands() >= (unsigned)NumOperands)
96     return true;  // Broken: we have all the operands of this instruction!
97   return false;
98 }
99
100 void MachineInstr::dump() const {
101   std::cerr << "  " << *this;
102 }
103
104 static inline void OutputReg(std::ostream &os, unsigned RegNo,
105                              const MRegisterInfo *MRI = 0) {
106   if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) {
107     if (MRI)
108       os << "%" << MRI->get(RegNo).Name;
109     else
110       os << "%mreg(" << RegNo << ")";
111   } else
112     os << "%reg" << RegNo;
113 }
114
115 static void print(const MachineOperand &MO, std::ostream &OS,
116                   const TargetMachine *TM) {
117   const MRegisterInfo *MRI = 0;
118
119   if (TM) MRI = TM->getRegisterInfo();
120
121   switch (MO.getType()) {
122   case MachineOperand::MO_Register:
123     OutputReg(OS, MO.getReg(), MRI);
124     break;
125   case MachineOperand::MO_Immediate:
126     OS << MO.getImmedValue();
127     break;
128   case MachineOperand::MO_MachineBasicBlock:
129     OS << "mbb<"
130        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
131        << "," << (void*)MO.getMachineBasicBlock() << ">";
132     break;
133   case MachineOperand::MO_FrameIndex:
134     OS << "<fi#" << MO.getFrameIndex() << ">";
135     break;
136   case MachineOperand::MO_ConstantPoolIndex:
137     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
138     break;
139   case MachineOperand::MO_JumpTableIndex:
140     OS << "<jt#" << MO.getJumpTableIndex() << ">";
141     break;
142   case MachineOperand::MO_GlobalAddress:
143     OS << "<ga:" << ((Value*)MO.getGlobal())->getName();
144     if (MO.getOffset()) OS << "+" << MO.getOffset();
145     OS << ">";
146     break;
147   case MachineOperand::MO_ExternalSymbol:
148     OS << "<es:" << MO.getSymbolName();
149     if (MO.getOffset()) OS << "+" << MO.getOffset();
150     OS << ">";
151     break;
152   default:
153     assert(0 && "Unrecognized operand type");
154   }
155 }
156
157 void MachineInstr::print(std::ostream &OS, const TargetMachine *TM) const {
158   unsigned StartOp = 0;
159
160    // Specialize printing if op#0 is definition
161   if (getNumOperands() && getOperand(0).isReg() && getOperand(0).isDef()) {
162     ::print(getOperand(0), OS, TM);
163     OS << " = ";
164     ++StartOp;   // Don't print this operand again!
165   }
166
167   // Must check if Target machine is not null because machine BB could not
168   // be attached to a Machine function yet
169   if (TM)
170     OS << TM->getInstrInfo()->getName(getOpcode());
171
172   for (unsigned i = StartOp, e = getNumOperands(); i != e; ++i) {
173     const MachineOperand& mop = getOperand(i);
174     if (i != StartOp)
175       OS << ",";
176     OS << " ";
177     ::print(mop, OS, TM);
178
179     if (mop.isReg() && mop.isDef())
180       OS << "<def>";
181   }
182
183   OS << "\n";
184 }
185
186 std::ostream &llvm::operator<<(std::ostream &os, const MachineInstr &MI) {
187   // If the instruction is embedded into a basic block, we can find the target
188   // info for the instruction.
189   if (const MachineBasicBlock *MBB = MI.getParent()) {
190     const MachineFunction *MF = MBB->getParent();
191     if (MF)
192       MI.print(os, &MF->getTarget());
193     else
194       MI.print(os, 0);
195     return os;
196   }
197
198   // Otherwise, print it out in the "raw" format without symbolic register names
199   // and such.
200   os << TargetInstrDescriptors[MI.getOpcode()].Name;
201
202   for (unsigned i = 0, N = MI.getNumOperands(); i < N; i++) {
203     os << "\t" << MI.getOperand(i);
204     if (MI.getOperand(i).isReg() && MI.getOperand(i).isDef())
205       os << "<d>";
206   }
207
208   return os << "\n";
209 }
210
211 std::ostream &llvm::operator<<(std::ostream &OS, const MachineOperand &MO) {
212   switch (MO.getType()) {
213   case MachineOperand::MO_Register:
214     OutputReg(OS, MO.getReg());
215     break;
216   case MachineOperand::MO_Immediate:
217     OS << (long)MO.getImmedValue();
218     break;
219   case MachineOperand::MO_MachineBasicBlock:
220     OS << "<mbb:"
221        << ((Value*)MO.getMachineBasicBlock()->getBasicBlock())->getName()
222        << "@" << (void*)MO.getMachineBasicBlock() << ">";
223     break;
224   case MachineOperand::MO_FrameIndex:
225     OS << "<fi#" << MO.getFrameIndex() << ">";
226     break;
227   case MachineOperand::MO_ConstantPoolIndex:
228     OS << "<cp#" << MO.getConstantPoolIndex() << ">";
229     break;
230   case MachineOperand::MO_JumpTableIndex:
231     OS << "<jt#" << MO.getJumpTableIndex() << ">";
232     break;
233   case MachineOperand::MO_GlobalAddress:
234     OS << "<ga:" << ((Value*)MO.getGlobal())->getName() << ">";
235     break;
236   case MachineOperand::MO_ExternalSymbol:
237     OS << "<es:" << MO.getSymbolName() << ">";
238     break;
239   default:
240     assert(0 && "Unrecognized operand type");
241     break;
242   }
243
244   return OS;
245 }