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"
22 //************************ Class Implementations **************************/
24 // Constructor for instructions with fixed #operands (nearly all)
25 MachineInstr::MachineInstr(MachineOpCode _opCode,
26 OpCodeMask _opCodeMask)
28 opCodeMask(_opCodeMask),
29 operands(TargetInstrDescriptors[_opCode].numOperands)
31 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
34 // Constructor for instructions with variable #operands
35 MachineInstr::MachineInstr(MachineOpCode _opCode,
37 OpCodeMask _opCodeMask)
39 opCodeMask(_opCodeMask),
45 MachineInstr::SetMachineOperand(unsigned int i,
46 MachineOperand::MachineOperandType operandType,
47 Value* _val, bool isdef=false)
49 assert(i < operands.size());
50 operands[i].Initialize(operandType, _val);
51 operands[i].isDef = isdef ||
52 TargetInstrDescriptors[opCode].resultPos == (int) i;
56 MachineInstr::SetMachineOperand(unsigned int i,
57 MachineOperand::MachineOperandType operandType,
58 int64_t intValue, bool isdef=false)
60 assert(i < operands.size());
61 operands[i].InitializeConst(operandType, intValue);
62 operands[i].isDef = isdef ||
63 TargetInstrDescriptors[opCode].resultPos == (int) i;
67 MachineInstr::SetMachineOperand(unsigned int i,
68 unsigned int regNum, bool isdef=false)
70 assert(i < operands.size());
71 operands[i].InitializeReg(regNum);
72 operands[i].isDef = isdef ||
73 TargetInstrDescriptors[opCode].resultPos == (int) i;
77 MachineInstr::dump(unsigned int indent) const
79 for (unsigned i=0; i < indent; i++)
86 operator<< (ostream& os, const MachineInstr& minstr)
88 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
90 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
91 os << "\t" << minstr.getOperand(i);
93 #undef DEBUG_VAL_OP_ITERATOR
94 #ifdef DEBUG_VAL_OP_ITERATOR
95 os << endl << "\tValue operands are: ";
96 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
98 const Value* val = *vo;
99 os << val << (vo.isDef()? "(def), " : ", ");
107 static inline ostream&
108 OutputOperand(ostream &os, const MachineOperand &mop)
110 switch (mop.getOperandType())
112 case MachineOperand::MO_CCRegister:
113 case MachineOperand::MO_VirtualRegister:
114 return os << "(val " << mop.getVRegValue() << ")";
115 case MachineOperand::MO_MachineRegister:
116 return os << "(" << mop.getMachineRegNum() << ")";
118 assert(0 && "Unknown operand type");
125 operator<<(ostream &os, const MachineOperand &mop)
129 case MachineOperand::MO_VirtualRegister:
130 case MachineOperand::MO_MachineRegister:
132 return OutputOperand(os, mop);
133 case MachineOperand::MO_CCRegister:
135 return OutputOperand(os, mop);
136 case MachineOperand::MO_SignExtendedImmed:
137 return os << mop.immedVal;
138 case MachineOperand::MO_UnextendedImmed:
139 return os << mop.immedVal;
140 case MachineOperand::MO_PCRelativeDisp:
142 const Value* opVal = mop.getVRegValue();
143 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
144 return os << "%disp("
145 << (isLabel? "label " : "addr-of-val ")
149 assert(0 && "Unrecognized operand type");
158 PrintMachineInstructions(const Method *const method)
160 cout << "\n" << method->getReturnType()
161 << " \"" << method->getName() << "\"" << endl;
163 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
165 BasicBlock* bb = *BI;
167 << (bb->hasName()? bb->getName() : "Label")
168 << " (" << bb << ")" << ":"
171 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
172 for (unsigned i=0; i < mvec.size(); i++)
173 cout << "\t" << *mvec[i] << endl;
175 cout << endl << "End method \"" << method->getName() << "\""