blob: b620fa4bfaa15cffd4af75ca09762a3248a6ab32 [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
Vikram S. Adve5b795912001-08-28 23:02:39 +000015
Chris Lattner822b4fb2001-09-07 17:18:30 +000016#include "llvm/CodeGen/MachineInstr.h"
Vikram S. Adve6e447182001-09-18 12:56:28 +000017#include "llvm/Target/MachineRegInfo.h"
Vikram S. Adve5b795912001-08-28 23:02:39 +000018#include "llvm/Method.h"
Chris Lattner68498ce2001-07-21 23:24:48 +000019#include "llvm/Instruction.h"
Vikram S. Adve5b795912001-08-28 23:02:39 +000020
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000021
Ruchira Sasanka69917e22001-10-18 22:40:02 +000022
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000023//************************ Class Implementations **************************/
24
Vikram S. Adve1885da42001-07-31 21:49:28 +000025// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000026MachineInstr::MachineInstr(MachineOpCode _opCode,
27 OpCodeMask _opCodeMask)
28 : opCode(_opCode),
29 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000030 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000031{
Vikram S. Adve1885da42001-07-31 21:49:28 +000032 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
33}
34
35// Constructor for instructions with variable #operands
36MachineInstr::MachineInstr(MachineOpCode _opCode,
37 unsigned numOperands,
38 OpCodeMask _opCodeMask)
39 : opCode(_opCode),
40 opCodeMask(_opCodeMask),
41 operands(numOperands)
42{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000043}
44
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000045void
46MachineInstr::SetMachineOperand(unsigned int i,
47 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000048 Value* _val, bool isdef=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. Adve70bc4b52001-07-21 12:41:50 +000051 operands[i].Initialize(operandType, _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. Adve70bc4b52001-07-21 12:41:50 +000054}
55
56void
57MachineInstr::SetMachineOperand(unsigned int i,
58 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000059 int64_t intValue, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000060{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000061 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000062 operands[i].InitializeConst(operandType, intValue);
Vikram S. Adve149977b2001-08-13 16:32:45 +000063 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000064 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000065}
66
67void
68MachineInstr::SetMachineOperand(unsigned int i,
Vikram S. Advedf1c3b82001-11-05 03:56:02 +000069 int regNum, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000070{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000071 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000072 operands[i].InitializeReg(regNum);
Vikram S. Adve149977b2001-08-13 16:32:45 +000073 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000074 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000075}
76
77void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000078MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000079{
80 for (unsigned i=0; i < indent; i++)
81 cout << " ";
82
83 cout << *this;
84}
85
86ostream&
87operator<< (ostream& os, const MachineInstr& minstr)
88{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000089 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000090
91 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
92 os << "\t" << minstr.getOperand(i);
93
Vikram S. Adve6a175e02001-07-28 04:06:37 +000094#undef DEBUG_VAL_OP_ITERATOR
95#ifdef DEBUG_VAL_OP_ITERATOR
96 os << endl << "\tValue operands are: ";
97 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
98 {
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);
116 cout << "\t";
117 }
118 }
119
120#endif
121
122
Vikram S. Adve6d353262001-10-17 23:57:50 +0000123 os << endl;
124
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000125 return os;
126}
127
Vikram S. Adve6e447182001-09-18 12:56:28 +0000128static inline ostream&
129OutputOperand(ostream &os, const MachineOperand &mop)
130{
131 switch (mop.getOperandType())
132 {
133 case MachineOperand::MO_CCRegister:
134 case MachineOperand::MO_VirtualRegister:
135 return os << "(val " << mop.getVRegValue() << ")";
136 case MachineOperand::MO_MachineRegister:
137 return os << "(" << mop.getMachineRegNum() << ")";
138 default:
139 assert(0 && "Unknown operand type");
140 return os;
141 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000142}
143
144
Vikram S. Adve6e447182001-09-18 12:56:28 +0000145ostream&
146operator<<(ostream &os, const MachineOperand &mop)
147{
148 switch(mop.opType)
149 {
150 case MachineOperand::MO_VirtualRegister:
151 case MachineOperand::MO_MachineRegister:
152 os << "%reg";
153 return OutputOperand(os, mop);
154 case MachineOperand::MO_CCRegister:
155 os << "%ccreg";
156 return OutputOperand(os, mop);
157 case MachineOperand::MO_SignExtendedImmed:
158 return os << mop.immedVal;
159 case MachineOperand::MO_UnextendedImmed:
160 return os << mop.immedVal;
161 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000162 {
163 const Value* opVal = mop.getVRegValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000164 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Advee949da52001-09-30 23:44:19 +0000165 return os << "%disp("
166 << (isLabel? "label " : "addr-of-val ")
167 << opVal << ")";
168 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000169 default:
170 assert(0 && "Unrecognized operand type");
171 break;
172 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000173
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000174 return os;
175}
176
177
Vikram S. Adve5b795912001-08-28 23:02:39 +0000178void
Vikram S. Adve1d6158f2001-10-22 13:51:33 +0000179MachineCodeForMethod::putLocalVarAtOffsetFromFP(const Value* local,
180 int offset,
181 unsigned int size)
182{
183 offsetsFromFP[local] = offset;
184 incrementAutomaticVarsSize(size);
185}
186
187
188void
189MachineCodeForMethod::putLocalVarAtOffsetFromSP(const Value* local,
190 int offset,
191 unsigned int size)
192{
193 offsetsFromSP[local] = offset;
194 incrementAutomaticVarsSize(size);
195}
196
197
198int
199MachineCodeForMethod::getOffsetFromFP(const Value* local) const
200{
201 hash_map<const Value*, int>::const_iterator pair = offsetsFromFP.find(local);
202 assert(pair != offsetsFromFP.end() && "Offset from FP unknown for Value");
203 return (*pair).second;
204}
205
206
207int
208MachineCodeForMethod::getOffsetFromSP(const Value* local) const
209{
210 hash_map<const Value*, int>::const_iterator pair = offsetsFromSP.find(local);
211 assert(pair != offsetsFromSP.end() && "Offset from SP unknown for Value");
212 return (*pair).second;
213}
214
215
216void
217MachineCodeForMethod::dump() const
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000218{
219 cout << "\n" << method->getReturnType()
220 << " \"" << method->getName() << "\"" << endl;
221
222 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
223 {
224 BasicBlock* bb = *BI;
225 cout << "\n"
226 << (bb->hasName()? bb->getName() : "Label")
227 << " (" << bb << ")" << ":"
228 << endl;
229
230 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
231 for (unsigned i=0; i < mvec.size(); i++)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000232 cout << "\t" << *mvec[i];
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000233 }
234 cout << endl << "End method \"" << method->getName() << "\""
235 << endl << endl;
236}