2001-07-21 20:41:50 +08:00
|
|
|
// $Id$
|
|
|
|
//***************************************************************************
|
|
|
|
// File:
|
|
|
|
// MachineInstr.cpp
|
|
|
|
//
|
|
|
|
// Purpose:
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Strategy:
|
|
|
|
//
|
|
|
|
// History:
|
|
|
|
// 7/2/01 - Vikram Adve - Created
|
|
|
|
//**************************************************************************/
|
|
|
|
|
2001-09-08 01:18:30 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-03 15:46:01 +08:00
|
|
|
#include "llvm/Value.h"
|
2002-01-21 06:54:45 +08:00
|
|
|
#include <iostream>
|
|
|
|
using std::cerr;
|
2001-08-29 07:02:39 +08:00
|
|
|
|
2001-10-19 06:40:02 +08:00
|
|
|
|
2001-07-21 20:41:50 +08:00
|
|
|
//************************ Class Implementations **************************/
|
|
|
|
|
2001-08-01 05:49:28 +08:00
|
|
|
// Constructor for instructions with fixed #operands (nearly all)
|
2001-07-21 20:41:50 +08:00
|
|
|
MachineInstr::MachineInstr(MachineOpCode _opCode,
|
|
|
|
OpCodeMask _opCodeMask)
|
|
|
|
: opCode(_opCode),
|
|
|
|
opCodeMask(_opCodeMask),
|
2001-07-28 12:06:37 +08:00
|
|
|
operands(TargetInstrDescriptors[_opCode].numOperands)
|
2001-08-01 05:49:28 +08:00
|
|
|
{
|
|
|
|
assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor for instructions with variable #operands
|
|
|
|
MachineInstr::MachineInstr(MachineOpCode _opCode,
|
|
|
|
unsigned numOperands,
|
|
|
|
OpCodeMask _opCodeMask)
|
|
|
|
: opCode(_opCode),
|
|
|
|
opCodeMask(_opCodeMask),
|
|
|
|
operands(numOperands)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-18 11:35:24 +08:00
|
|
|
MachineInstr::SetMachineOperandVal(unsigned int i,
|
2001-07-21 20:41:50 +08:00
|
|
|
MachineOperand::MachineOperandType operandType,
|
2001-08-08 04:16:52 +08:00
|
|
|
Value* _val, bool isdef=false)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2001-07-28 12:06:37 +08:00
|
|
|
assert(i < operands.size());
|
2001-07-21 20:41:50 +08:00
|
|
|
operands[i].Initialize(operandType, _val);
|
2001-08-14 00:32:45 +08:00
|
|
|
operands[i].isDef = isdef ||
|
2001-09-18 20:56:28 +08:00
|
|
|
TargetInstrDescriptors[opCode].resultPos == (int) i;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-18 11:35:24 +08:00
|
|
|
MachineInstr::SetMachineOperandConst(unsigned int i,
|
2001-07-21 20:41:50 +08:00
|
|
|
MachineOperand::MachineOperandType operandType,
|
2002-03-18 11:35:24 +08:00
|
|
|
int64_t intValue)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2001-07-28 12:06:37 +08:00
|
|
|
assert(i < operands.size());
|
2002-03-18 11:35:24 +08:00
|
|
|
assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
|
|
|
|
"immed. constant cannot be defined");
|
2001-07-21 20:41:50 +08:00
|
|
|
operands[i].InitializeConst(operandType, intValue);
|
2002-03-18 11:35:24 +08:00
|
|
|
operands[i].isDef = false;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-03-18 11:35:24 +08:00
|
|
|
MachineInstr::SetMachineOperandReg(unsigned int i,
|
|
|
|
int regNum,
|
|
|
|
bool isdef=false,
|
|
|
|
bool isCCReg=false)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2001-07-28 12:06:37 +08:00
|
|
|
assert(i < operands.size());
|
2002-03-18 11:35:24 +08:00
|
|
|
operands[i].InitializeReg(regNum, isCCReg);
|
2001-08-14 00:32:45 +08:00
|
|
|
operands[i].isDef = isdef ||
|
2001-09-18 20:56:28 +08:00
|
|
|
TargetInstrDescriptors[opCode].resultPos == (int) i;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-08-08 05:01:23 +08:00
|
|
|
MachineInstr::dump(unsigned int indent) const
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
|
|
|
for (unsigned i=0; i < indent; i++)
|
2002-01-21 06:54:45 +08:00
|
|
|
cerr << " ";
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
cerr << *this;
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
2002-04-25 12:31:18 +08:00
|
|
|
static inline std::ostream &OutputValue(std::ostream &os,
|
|
|
|
const Value* val)
|
|
|
|
{
|
|
|
|
os << "(val ";
|
|
|
|
if (val && val->hasName())
|
|
|
|
return os << val->getName();
|
|
|
|
else
|
|
|
|
return os << (void*) val; // print address only
|
|
|
|
os << ")";
|
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
|
2001-07-21 20:41:50 +08:00
|
|
|
{
|
2001-07-28 12:06:37 +08:00
|
|
|
os << TargetInstrDescriptors[minstr.opCode].opCodeString;
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2001-11-15 04:05:23 +08:00
|
|
|
for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
|
2001-07-21 20:41:50 +08:00
|
|
|
os << "\t" << minstr.getOperand(i);
|
2001-11-16 04:46:40 +08:00
|
|
|
if( minstr.getOperand(i).opIsDef() )
|
|
|
|
os << "*";
|
2001-11-15 04:05:23 +08:00
|
|
|
}
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2001-10-19 06:40:02 +08:00
|
|
|
// code for printing implict references
|
|
|
|
unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
|
|
|
|
if( NumOfImpRefs > 0 ) {
|
2002-04-25 12:31:18 +08:00
|
|
|
os << "\tImplicit: ";
|
2001-10-19 06:40:02 +08:00
|
|
|
for(unsigned z=0; z < NumOfImpRefs; z++) {
|
2002-04-25 12:31:18 +08:00
|
|
|
OutputValue(os, minstr.getImplicitRef(z));
|
2001-11-15 04:05:23 +08:00
|
|
|
if( minstr.implicitRefIsDefined(z)) os << "*";
|
2001-11-16 04:46:40 +08:00
|
|
|
os << "\t";
|
2001-10-19 06:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
2002-04-25 12:31:18 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
return os << "\n";
|
2001-07-21 20:41:50 +08:00
|
|
|
}
|
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
static inline std::ostream &OutputOperand(std::ostream &os,
|
|
|
|
const MachineOperand &mop)
|
2001-09-18 20:56:28 +08:00
|
|
|
{
|
2001-11-12 22:19:47 +08:00
|
|
|
Value* val;
|
2001-09-18 20:56:28 +08:00
|
|
|
switch (mop.getOperandType())
|
|
|
|
{
|
|
|
|
case MachineOperand::MO_CCRegister:
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
2002-04-25 12:31:18 +08:00
|
|
|
return OutputValue(os, mop.getVRegValue());
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_MachineRegister:
|
2002-04-25 12:31:18 +08:00
|
|
|
return os << "(" << mop.getMachineRegNum() << ")";
|
2001-09-18 20:56:28 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Unknown operand type");
|
|
|
|
return os;
|
|
|
|
}
|
2001-09-10 06:26:29 +08:00
|
|
|
}
|
2001-07-21 20:41:50 +08:00
|
|
|
|
2001-09-10 06:26:29 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
|
2001-09-18 20:56:28 +08:00
|
|
|
{
|
|
|
|
switch(mop.opType)
|
|
|
|
{
|
|
|
|
case MachineOperand::MO_VirtualRegister:
|
|
|
|
case MachineOperand::MO_MachineRegister:
|
|
|
|
os << "%reg";
|
|
|
|
return OutputOperand(os, mop);
|
|
|
|
case MachineOperand::MO_CCRegister:
|
|
|
|
os << "%ccreg";
|
|
|
|
return OutputOperand(os, mop);
|
|
|
|
case MachineOperand::MO_SignExtendedImmed:
|
2002-01-21 06:54:45 +08:00
|
|
|
return os << (long)mop.immedVal;
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_UnextendedImmed:
|
2002-01-21 06:54:45 +08:00
|
|
|
return os << (long)mop.immedVal;
|
2001-09-18 20:56:28 +08:00
|
|
|
case MachineOperand::MO_PCRelativeDisp:
|
2001-10-01 07:44:19 +08:00
|
|
|
{
|
|
|
|
const Value* opVal = mop.getVRegValue();
|
2002-04-09 06:01:15 +08:00
|
|
|
bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
|
2001-11-12 22:19:47 +08:00
|
|
|
os << "%disp(" << (isLabel? "label " : "addr-of-val ");
|
|
|
|
if (opVal->hasName())
|
2002-01-21 06:54:45 +08:00
|
|
|
os << opVal->getName();
|
2001-11-12 22:19:47 +08:00
|
|
|
else
|
|
|
|
os << opVal;
|
|
|
|
return os << ")";
|
2001-10-01 07:44:19 +08:00
|
|
|
}
|
2001-09-18 20:56:28 +08:00
|
|
|
default:
|
|
|
|
assert(0 && "Unrecognized operand type");
|
|
|
|
break;
|
|
|
|
}
|
2001-09-10 06:26:29 +08:00
|
|
|
|
2001-07-21 20:41:50 +08:00
|
|
|
return os;
|
|
|
|
}
|