blob: 1d6bfb8033f04aa623ac5f4d2228c0bafcfe42f1 [file] [log] [blame]
Vikram S. Adve70bc4b52001-07-21 12:41:50 +00001// $Id$
2//***************************************************************************
3// File:
4// MachineInstr.cpp
5//
6// Purpose:
7//
8//
9// Strategy:
10//
11// History:
12// 7/2/01 - Vikram Adve - Created
13//**************************************************************************/
14
Chris Lattner822b4fb2001-09-07 17:18:30 +000015#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner3801f6d2002-02-03 07:46:01 +000016#include "llvm/Value.h"
Chris Lattner697954c2002-01-20 22:54:45 +000017#include <iostream>
18using std::cerr;
Vikram S. Adve5b795912001-08-28 23:02:39 +000019
Ruchira Sasanka69917e22001-10-18 22:40:02 +000020
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000021//************************ Class Implementations **************************/
22
Vikram S. Adve1885da42001-07-31 21:49:28 +000023// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000024MachineInstr::MachineInstr(MachineOpCode _opCode,
25 OpCodeMask _opCodeMask)
26 : opCode(_opCode),
27 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000028 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000029{
Vikram S. Adve1885da42001-07-31 21:49:28 +000030 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
31}
32
33// Constructor for instructions with variable #operands
34MachineInstr::MachineInstr(MachineOpCode _opCode,
35 unsigned numOperands,
36 OpCodeMask _opCodeMask)
37 : opCode(_opCode),
38 opCodeMask(_opCodeMask),
39 operands(numOperands)
40{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000041}
42
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000043void
Vikram S. Advec356e562002-03-18 03:35:24 +000044MachineInstr::SetMachineOperandVal(unsigned int i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000045 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000046 Value* _val, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000047{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000048 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000049 operands[i].Initialize(operandType, _val);
Vikram S. Adve149977b2001-08-13 16:32:45 +000050 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000051 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000052}
53
54void
Vikram S. Advec356e562002-03-18 03:35:24 +000055MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000056 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000057 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000058{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000059 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000060 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
61 "immed. constant cannot be defined");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000062 operands[i].InitializeConst(operandType, intValue);
Vikram S. Advec356e562002-03-18 03:35:24 +000063 operands[i].isDef = false;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064}
65
66void
Vikram S. Advec356e562002-03-18 03:35:24 +000067MachineInstr::SetMachineOperandReg(unsigned int i,
68 int regNum,
69 bool isdef=false,
70 bool isCCReg=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000071{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000072 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000073 operands[i].InitializeReg(regNum, isCCReg);
Vikram S. Adve149977b2001-08-13 16:32:45 +000074 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000075 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000076}
77
78void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000079MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000080{
81 for (unsigned i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000082 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000083
Chris Lattner697954c2002-01-20 22:54:45 +000084 cerr << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000085}
86
Chris Lattner697954c2002-01-20 22:54:45 +000087std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000088{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000089 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000090
Ruchira Sasanka8d243372001-11-14 20:05:23 +000091 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000092 os << "\t" << minstr.getOperand(i);
Ruchira Sasanka07c70862001-11-15 20:46:40 +000093 if( minstr.getOperand(i).opIsDef() )
94 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +000095 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000096
Vikram S. Adve6a175e02001-07-28 04:06:37 +000097#undef DEBUG_VAL_OP_ITERATOR
98#ifdef DEBUG_VAL_OP_ITERATOR
Chris Lattner697954c2002-01-20 22:54:45 +000099 os << "\n\tValue operands are: ";
Chris Lattner7a176752001-12-04 00:03:30 +0000100 for (MachineInstr::val_const_op_iterator vo(&minstr); ! vo.done(); ++vo)
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000101 {
102 const Value* val = *vo;
103 os << val << (vo.isDef()? "(def), " : ", ");
104 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000105#endif
106
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000107
108
109#if 1
110 // code for printing implict references
111
112 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
113 if( NumOfImpRefs > 0 ) {
114
115 os << "\tImplicit:";
116
117 for(unsigned z=0; z < NumOfImpRefs; z++) {
118 os << minstr.getImplicitRef(z);
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000119 if( minstr.implicitRefIsDefined(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000120 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000121 }
122 }
123
124#endif
Chris Lattner697954c2002-01-20 22:54:45 +0000125 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000126}
127
Chris Lattner697954c2002-01-20 22:54:45 +0000128static inline std::ostream &OutputOperand(std::ostream &os,
129 const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000130{
Vikram S. Adved9beb972001-11-12 14:19:47 +0000131 Value* val;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000132 switch (mop.getOperandType())
133 {
134 case MachineOperand::MO_CCRegister:
135 case MachineOperand::MO_VirtualRegister:
Vikram S. Adved9beb972001-11-12 14:19:47 +0000136 val = mop.getVRegValue();
137 os << "(val ";
138 if (val && val->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000139 os << val->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000140 else
141 os << val;
142 return os << ")";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000143 case MachineOperand::MO_MachineRegister:
144 return os << "(" << mop.getMachineRegNum() << ")";
145 default:
146 assert(0 && "Unknown operand type");
147 return os;
148 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000149}
150
151
Chris Lattner697954c2002-01-20 22:54:45 +0000152std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000153{
154 switch(mop.opType)
155 {
156 case MachineOperand::MO_VirtualRegister:
157 case MachineOperand::MO_MachineRegister:
158 os << "%reg";
159 return OutputOperand(os, mop);
160 case MachineOperand::MO_CCRegister:
161 os << "%ccreg";
162 return OutputOperand(os, mop);
163 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000164 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000165 case MachineOperand::MO_UnextendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000166 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000167 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000168 {
169 const Value* opVal = mop.getVRegValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000170 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000171 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
172 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000173 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000174 else
175 os << opVal;
176 return os << ")";
Vikram S. Advee949da52001-09-30 23:44:19 +0000177 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000178 default:
179 assert(0 && "Unrecognized operand type");
180 break;
181 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000182
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000183 return os;
184}