blob: 09992728574956a145e0018323ed1650bd7bd348 [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. Adve7a4be952002-07-08 22:38:45 +000045 MachineOperand::MachineOperandType opType,
46 Value* _val,
47 bool isdef=false,
48 bool isDefAndUse=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000049{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000050 assert(i < operands.size());
Vikram S. Adve7a4be952002-07-08 22:38:45 +000051 operands[i].Initialize(opType, _val);
Vikram S. Adve149977b2001-08-13 16:32:45 +000052 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000053 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve7a4be952002-07-08 22:38:45 +000054 operands[i].isDefAndUse = isDefAndUse;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000055}
56
57void
Vikram S. Advec356e562002-03-18 03:35:24 +000058MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000059 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000060 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000061{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000062 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000063 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
64 "immed. constant cannot be defined");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000065 operands[i].InitializeConst(operandType, intValue);
Vikram S. Advec356e562002-03-18 03:35:24 +000066 operands[i].isDef = false;
Vikram S. Adve7a4be952002-07-08 22:38:45 +000067 operands[i].isDefAndUse = false;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000068}
69
70void
Vikram S. Advec356e562002-03-18 03:35:24 +000071MachineInstr::SetMachineOperandReg(unsigned int i,
72 int regNum,
73 bool isdef=false,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000074 bool isDefAndUse=false,
Vikram S. Advec356e562002-03-18 03:35:24 +000075 bool isCCReg=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000076{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000077 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000078 operands[i].InitializeReg(regNum, isCCReg);
Vikram S. Adve149977b2001-08-13 16:32:45 +000079 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000080 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve7a4be952002-07-08 22:38:45 +000081 operands[i].isDefAndUse = isDefAndUse;
82 regsUsed.insert(regNum);
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000083}
84
85void
Vikram S. Adve7a4be952002-07-08 22:38:45 +000086MachineInstr::SetRegForOperand(unsigned i, int regNum)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000087{
Vikram S. Adve7a4be952002-07-08 22:38:45 +000088 operands[i].setRegForValue(regNum);
89 regsUsed.insert(regNum);
90}
91
92
93void
94MachineInstr::dump() const
95{
96 cerr << " " << *this;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000097}
98
Vikram S. Adve93240fe2002-04-25 04:31:18 +000099static inline std::ostream &OutputValue(std::ostream &os,
100 const Value* val)
101{
102 os << "(val ";
103 if (val && val->hasName())
104 return os << val->getName();
105 else
106 return os << (void*) val; // print address only
107 os << ")";
108}
109
Chris Lattner697954c2002-01-20 22:54:45 +0000110std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000111{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000112 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000113
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000114 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000115 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000116 if( minstr.operandIsDefined(i) )
117 os << "*";
118 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000119 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000120 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000121
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000122 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000123 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
124 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000125 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000126 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000127 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000128 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000129 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000130 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000131 }
132 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000133
Chris Lattner697954c2002-01-20 22:54:45 +0000134 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000135}
136
Chris Lattner697954c2002-01-20 22:54:45 +0000137static inline std::ostream &OutputOperand(std::ostream &os,
138 const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000139{
Vikram S. Adved9beb972001-11-12 14:19:47 +0000140 Value* val;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000141 switch (mop.getOperandType())
142 {
143 case MachineOperand::MO_CCRegister:
144 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000145 return OutputValue(os, mop.getVRegValue());
Vikram S. Adve6e447182001-09-18 12:56:28 +0000146 case MachineOperand::MO_MachineRegister:
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000147 return os << "(" << mop.getMachineRegNum() << ")";
Vikram S. Adve6e447182001-09-18 12:56:28 +0000148 default:
149 assert(0 && "Unknown operand type");
150 return os;
151 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000152}
153
Chris Lattner697954c2002-01-20 22:54:45 +0000154std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000155{
156 switch(mop.opType)
157 {
158 case MachineOperand::MO_VirtualRegister:
159 case MachineOperand::MO_MachineRegister:
160 os << "%reg";
161 return OutputOperand(os, mop);
162 case MachineOperand::MO_CCRegister:
163 os << "%ccreg";
164 return OutputOperand(os, mop);
165 case MachineOperand::MO_SignExtendedImmed:
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_UnextendedImmed:
Chris Lattner697954c2002-01-20 22:54:45 +0000168 return os << (long)mop.immedVal;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000169 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000170 {
171 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000172 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000173 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
174 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000175 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000176 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000177 os << (const void*) opVal;
Vikram S. Adved9beb972001-11-12 14:19:47 +0000178 return os << ")";
Vikram S. Advee949da52001-09-30 23:44:19 +0000179 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000180 default:
181 assert(0 && "Unrecognized operand type");
182 break;
183 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000184
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000185 return os;
186}