blob: bb22008f952e8b5e4d47ebef21ed57fd12b8b91c [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
44MachineInstr::SetMachineOperand(unsigned int i,
45 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
55MachineInstr::SetMachineOperand(unsigned int i,
56 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000057 int64_t intValue, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000058{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000059 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000060 operands[i].InitializeConst(operandType, intValue);
Vikram S. Adve149977b2001-08-13 16:32:45 +000061 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000062 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000063}
64
65void
66MachineInstr::SetMachineOperand(unsigned int i,
Vikram S. Advedf1c3b82001-11-05 03:56:02 +000067 int regNum, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000068{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000069 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000070 operands[i].InitializeReg(regNum);
Vikram S. Adve149977b2001-08-13 16:32:45 +000071 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000072 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000073}
74
75void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000076MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000077{
78 for (unsigned i=0; i < indent; i++)
Chris Lattner697954c2002-01-20 22:54:45 +000079 cerr << " ";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000080
Chris Lattner697954c2002-01-20 22:54:45 +000081 cerr << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000082}
83
Chris Lattner697954c2002-01-20 22:54:45 +000084std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000085{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000086 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000087
Ruchira Sasanka8d243372001-11-14 20:05:23 +000088 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000089 os << "\t" << minstr.getOperand(i);
Ruchira Sasanka07c70862001-11-15 20:46:40 +000090 if( minstr.getOperand(i).opIsDef() )
91 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +000092 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000093
Vikram S. Adve6a175e02001-07-28 04:06:37 +000094#undef DEBUG_VAL_OP_ITERATOR
95#ifdef DEBUG_VAL_OP_ITERATOR
Chris Lattner697954c2002-01-20 22:54:45 +000096 os << "\n\tValue operands are: ";
Chris Lattner7a176752001-12-04 00:03:30 +000097 for (MachineInstr::val_const_op_iterator vo(&minstr); ! vo.done(); ++vo)
Vikram S. Adve6a175e02001-07-28 04:06:37 +000098 {
99 const Value* val = *vo;
100 os << val << (vo.isDef()? "(def), " : ", ");
101 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000102#endif
103
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000104
105
106#if 1
107 // code for printing implict references
108
109 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
110 if( NumOfImpRefs > 0 ) {
111
112 os << "\tImplicit:";
113
114 for(unsigned z=0; z < NumOfImpRefs; z++) {
115 os << minstr.getImplicitRef(z);
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000116 if( minstr.implicitRefIsDefined(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000117 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000118 }
119 }
120
121#endif
Chris Lattner697954c2002-01-20 22:54:45 +0000122 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000123}
124
Chris Lattner697954c2002-01-20 22:54:45 +0000125static inline std::ostream &OutputOperand(std::ostream &os,
126 const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000127{
Vikram S. Adved9beb972001-11-12 14:19:47 +0000128 Value* val;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000129 switch (mop.getOperandType())
130 {
131 case MachineOperand::MO_CCRegister:
132 case MachineOperand::MO_VirtualRegister:
Vikram S. Adved9beb972001-11-12 14:19:47 +0000133 val = mop.getVRegValue();
134 os << "(val ";
135 if (val && val->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000136 os << val->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000137 else
138 os << val;
139 return os << ")";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000140 case MachineOperand::MO_MachineRegister:
141 return os << "(" << mop.getMachineRegNum() << ")";
142 default:
143 assert(0 && "Unknown operand type");
144 return os;
145 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000146}
147
148
Chris Lattner697954c2002-01-20 22:54:45 +0000149std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000150{
151 switch(mop.opType)
152 {
153 case MachineOperand::MO_VirtualRegister:
154 case MachineOperand::MO_MachineRegister:
155 os << "%reg";
156 return OutputOperand(os, mop);
157 case MachineOperand::MO_CCRegister:
158 os << "%ccreg";
159 return OutputOperand(os, mop);
160 case MachineOperand::MO_SignExtendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000161 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000162 case MachineOperand::MO_UnextendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000163 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000164 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000165 {
166 const Value* opVal = mop.getVRegValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000167 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000168 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
169 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000170 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000171 else
172 os << opVal;
173 return os << ")";
Vikram S. Advee949da52001-09-30 23:44:19 +0000174 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000175 default:
176 assert(0 && "Unrecognized operand type");
177 break;
178 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000179
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000180 return os;
181}