2 //***************************************************************************
12 // 7/2/01 - Vikram Adve - Created
13 //**************************************************************************/
15 #include "llvm/CodeGen/MachineInstr.h"
16 #include "llvm/Value.h"
21 //************************ Class Implementations **************************/
23 // Constructor for instructions with fixed #operands (nearly all)
24 MachineInstr::MachineInstr(MachineOpCode _opCode,
25 OpCodeMask _opCodeMask)
27 opCodeMask(_opCodeMask),
28 operands(TargetInstrDescriptors[_opCode].numOperands)
30 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
33 // Constructor for instructions with variable #operands
34 MachineInstr::MachineInstr(MachineOpCode _opCode,
36 OpCodeMask _opCodeMask)
38 opCodeMask(_opCodeMask),
44 MachineInstr::SetMachineOperandVal(unsigned int i,
45 MachineOperand::MachineOperandType operandType,
46 Value* _val, bool isdef=false)
48 assert(i < operands.size());
49 operands[i].Initialize(operandType, _val);
50 operands[i].isDef = isdef ||
51 TargetInstrDescriptors[opCode].resultPos == (int) i;
55 MachineInstr::SetMachineOperandConst(unsigned int i,
56 MachineOperand::MachineOperandType operandType,
59 assert(i < operands.size());
60 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
61 "immed. constant cannot be defined");
62 operands[i].InitializeConst(operandType, intValue);
63 operands[i].isDef = false;
67 MachineInstr::SetMachineOperandReg(unsigned int i,
72 assert(i < operands.size());
73 operands[i].InitializeReg(regNum, isCCReg);
74 operands[i].isDef = isdef ||
75 TargetInstrDescriptors[opCode].resultPos == (int) i;
79 MachineInstr::dump(unsigned int indent) const
81 for (unsigned i=0; i < indent; i++)
87 std::ostream &operator<<(std::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);
93 if( minstr.getOperand(i).opIsDef() )
97 #undef DEBUG_VAL_OP_ITERATOR
98 #ifdef DEBUG_VAL_OP_ITERATOR
99 os << "\n\tValue operands are: ";
100 for (MachineInstr::val_const_op_iterator vo(&minstr); ! vo.done(); ++vo)
102 const Value* val = *vo;
103 os << val << (vo.isDef()? "(def), " : ", ");
110 // code for printing implict references
112 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
113 if( NumOfImpRefs > 0 ) {
117 for(unsigned z=0; z < NumOfImpRefs; z++) {
118 os << minstr.getImplicitRef(z);
119 if( minstr.implicitRefIsDefined(z)) os << "*";
128 static inline std::ostream &OutputOperand(std::ostream &os,
129 const MachineOperand &mop)
132 switch (mop.getOperandType())
134 case MachineOperand::MO_CCRegister:
135 case MachineOperand::MO_VirtualRegister:
136 val = mop.getVRegValue();
138 if (val && val->hasName())
139 os << val->getName();
143 case MachineOperand::MO_MachineRegister:
144 return os << "(" << mop.getMachineRegNum() << ")";
146 assert(0 && "Unknown operand type");
152 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
156 case MachineOperand::MO_VirtualRegister:
157 case MachineOperand::MO_MachineRegister:
159 return OutputOperand(os, mop);
160 case MachineOperand::MO_CCRegister:
162 return OutputOperand(os, mop);
163 case MachineOperand::MO_SignExtendedImmed:
164 return os << (long)mop.immedVal;
165 case MachineOperand::MO_UnextendedImmed:
166 return os << (long)mop.immedVal;
167 case MachineOperand::MO_PCRelativeDisp:
169 const Value* opVal = mop.getVRegValue();
170 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
171 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
172 if (opVal->hasName())
173 os << opVal->getName();
179 assert(0 && "Unrecognized operand type");