1 //===-- MachineInstr.cpp --------------------------------------------------===//
3 //===----------------------------------------------------------------------===//
5 #include "llvm/CodeGen/MachineInstr.h"
6 #include "llvm/Value.h"
10 // Constructor for instructions with fixed #operands (nearly all)
11 MachineInstr::MachineInstr(MachineOpCode _opCode,
12 OpCodeMask _opCodeMask)
14 opCodeMask(_opCodeMask),
15 operands(TargetInstrDescriptors[_opCode].numOperands)
17 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
20 // Constructor for instructions with variable #operands
21 MachineInstr::MachineInstr(MachineOpCode _opCode,
23 OpCodeMask _opCodeMask)
25 opCodeMask(_opCodeMask),
31 MachineInstr::SetMachineOperandVal(unsigned int i,
32 MachineOperand::MachineOperandType opType,
37 assert(i < operands.size());
38 operands[i].Initialize(opType, _val);
39 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
40 operands[i].markDef();
42 operands[i].markDefAndUse();
46 MachineInstr::SetMachineOperandConst(unsigned int i,
47 MachineOperand::MachineOperandType operandType,
50 assert(i < operands.size());
51 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
52 "immed. constant cannot be defined");
53 operands[i].InitializeConst(operandType, intValue);
57 MachineInstr::SetMachineOperandReg(unsigned int i,
63 assert(i < operands.size());
64 operands[i].InitializeReg(regNum, isCCReg);
65 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
66 operands[i].markDef();
68 operands[i].markDefAndUse();
69 regsUsed.insert(regNum);
73 MachineInstr::SetRegForOperand(unsigned i, int regNum)
75 operands[i].setRegForValue(regNum);
76 regsUsed.insert(regNum);
81 MachineInstr::dump() const
86 static inline std::ostream &OutputValue(std::ostream &os,
90 if (val && val->hasName())
91 return os << val->getName() << ")";
93 return os << (void*) val << ")"; // print address only
96 std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
98 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
100 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
101 os << "\t" << minstr.getOperand(i);
102 if( minstr.operandIsDefined(i) )
104 if( minstr.operandIsDefinedAndUsed(i) )
108 // code for printing implict references
109 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
110 if( NumOfImpRefs > 0 ) {
111 os << "\tImplicit: ";
112 for(unsigned z=0; z < NumOfImpRefs; z++) {
113 OutputValue(os, minstr.getImplicitRef(z));
114 if( minstr.implicitRefIsDefined(z)) os << "*";
115 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
123 std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
125 if (mop.opHiBits32())
127 else if (mop.opLoBits32())
129 else if (mop.opHiBits64())
131 else if (mop.opLoBits64())
136 case MachineOperand::MO_VirtualRegister:
138 OutputValue(os, mop.getVRegValue());
140 case MachineOperand::MO_CCRegister:
142 OutputValue(os, mop.getVRegValue());
144 case MachineOperand::MO_MachineRegister:
146 os << "(" << mop.getMachineRegNum() << ")";
148 case MachineOperand::MO_SignExtendedImmed:
149 os << (long)mop.immedVal;
151 case MachineOperand::MO_UnextendedImmed:
152 os << (long)mop.immedVal;
154 case MachineOperand::MO_PCRelativeDisp:
156 const Value* opVal = mop.getVRegValue();
157 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
158 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
159 if (opVal->hasName())
160 os << opVal->getName();
162 os << (const void*) opVal;
167 assert(0 && "Unrecognized operand type");
172 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
173 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))