2 //***************************************************************************
12 // 7/2/01 - Vikram Adve - Created
13 //**************************************************************************/
16 #include "llvm/CodeGen/MachineInstr.h"
17 #include "llvm/Target/MachineRegInfo.h"
18 #include "llvm/Method.h"
19 #include "llvm/Instruction.h"
23 //************************ Class Implementations **************************/
25 // Constructor for instructions with fixed #operands (nearly all)
26 MachineInstr::MachineInstr(MachineOpCode _opCode,
27 OpCodeMask _opCodeMask)
29 opCodeMask(_opCodeMask),
30 operands(TargetInstrDescriptors[_opCode].numOperands)
32 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
35 // Constructor for instructions with variable #operands
36 MachineInstr::MachineInstr(MachineOpCode _opCode,
38 OpCodeMask _opCodeMask)
40 opCodeMask(_opCodeMask),
46 MachineInstr::SetMachineOperand(unsigned int i,
47 MachineOperand::MachineOperandType operandType,
48 Value* _val, bool isdef=false)
50 assert(i < operands.size());
51 operands[i].Initialize(operandType, _val);
52 operands[i].isDef = isdef ||
53 TargetInstrDescriptors[opCode].resultPos == (int) i;
57 MachineInstr::SetMachineOperand(unsigned int i,
58 MachineOperand::MachineOperandType operandType,
59 int64_t intValue, bool isdef=false)
61 assert(i < operands.size());
62 operands[i].InitializeConst(operandType, intValue);
63 operands[i].isDef = isdef ||
64 TargetInstrDescriptors[opCode].resultPos == (int) i;
68 MachineInstr::SetMachineOperand(unsigned int i,
69 unsigned int regNum, bool isdef=false)
71 assert(i < operands.size());
72 operands[i].InitializeReg(regNum);
73 operands[i].isDef = isdef ||
74 TargetInstrDescriptors[opCode].resultPos == (int) i;
78 MachineInstr::dump(unsigned int indent) const
80 for (unsigned i=0; i < indent; i++)
87 operator<< (ostream& os, const MachineInstr& minstr)
89 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
91 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
92 os << "\t" << minstr.getOperand(i);
94 #undef DEBUG_VAL_OP_ITERATOR
95 #ifdef DEBUG_VAL_OP_ITERATOR
96 os << endl << "\tValue operands are: ";
97 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
99 const Value* val = *vo;
100 os << val << (vo.isDef()? "(def), " : ", ");
107 // code for printing implict references
109 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
110 if( NumOfImpRefs > 0 ) {
114 for(unsigned z=0; z < NumOfImpRefs; z++) {
115 os << minstr.getImplicitRef(z);
128 static inline ostream&
129 OutputOperand(ostream &os, const MachineOperand &mop)
131 switch (mop.getOperandType())
133 case MachineOperand::MO_CCRegister:
134 case MachineOperand::MO_VirtualRegister:
135 return os << "(val " << mop.getVRegValue() << ")";
136 case MachineOperand::MO_MachineRegister:
137 return os << "(" << mop.getMachineRegNum() << ")";
139 assert(0 && "Unknown operand type");
146 operator<<(ostream &os, const MachineOperand &mop)
150 case MachineOperand::MO_VirtualRegister:
151 case MachineOperand::MO_MachineRegister:
153 return OutputOperand(os, mop);
154 case MachineOperand::MO_CCRegister:
156 return OutputOperand(os, mop);
157 case MachineOperand::MO_SignExtendedImmed:
158 return os << mop.immedVal;
159 case MachineOperand::MO_UnextendedImmed:
160 return os << mop.immedVal;
161 case MachineOperand::MO_PCRelativeDisp:
163 const Value* opVal = mop.getVRegValue();
164 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
165 return os << "%disp("
166 << (isLabel? "label " : "addr-of-val ")
170 assert(0 && "Unrecognized operand type");
179 PrintMachineInstructions(const Method *const method)
181 cout << "\n" << method->getReturnType()
182 << " \"" << method->getName() << "\"" << endl;
184 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
186 BasicBlock* bb = *BI;
188 << (bb->hasName()? bb->getName() : "Label")
189 << " (" << bb << ")" << ":"
192 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
193 for (unsigned i=0; i < mvec.size(); i++)
194 cout << "\t" << *mvec[i];
196 cout << endl << "End method \"" << method->getName() << "\""