blob: 8cbf55e840f3e9fc1814c9e589664ef784916c39 [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
22//************************ Class Implementations **************************/
23
Vikram S. Adve1885da42001-07-31 21:49:28 +000024// Constructor for instructions with fixed #operands (nearly all)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000025MachineInstr::MachineInstr(MachineOpCode _opCode,
26 OpCodeMask _opCodeMask)
27 : opCode(_opCode),
28 opCodeMask(_opCodeMask),
Vikram S. Adve6a175e02001-07-28 04:06:37 +000029 operands(TargetInstrDescriptors[_opCode].numOperands)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000030{
Vikram S. Adve1885da42001-07-31 21:49:28 +000031 assert(TargetInstrDescriptors[_opCode].numOperands >= 0);
32}
33
34// Constructor for instructions with variable #operands
35MachineInstr::MachineInstr(MachineOpCode _opCode,
36 unsigned numOperands,
37 OpCodeMask _opCodeMask)
38 : opCode(_opCode),
39 opCodeMask(_opCodeMask),
40 operands(numOperands)
41{
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000042}
43
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000044void
45MachineInstr::SetMachineOperand(unsigned int i,
46 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000047 Value* _val, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000048{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000049 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000050 operands[i].Initialize(operandType, _val);
Vikram S. Adve149977b2001-08-13 16:32:45 +000051 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000052 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000053}
54
55void
56MachineInstr::SetMachineOperand(unsigned int i,
57 MachineOperand::MachineOperandType operandType,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000058 int64_t intValue, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000059{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000060 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000061 operands[i].InitializeConst(operandType, intValue);
Vikram S. Adve149977b2001-08-13 16:32:45 +000062 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000063 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000064}
65
66void
67MachineInstr::SetMachineOperand(unsigned int i,
Ruchira Sasanka45c171e2001-08-07 20:16:52 +000068 unsigned int regNum, bool isdef=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000069{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000070 assert(i < operands.size());
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000071 operands[i].InitializeReg(regNum);
Vikram S. Adve149977b2001-08-13 16:32:45 +000072 operands[i].isDef = isdef ||
Vikram S. Adve6e447182001-09-18 12:56:28 +000073 TargetInstrDescriptors[opCode].resultPos == (int) i;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000074}
75
76void
Ruchira Sasanka0b03c6a2001-08-07 21:01:23 +000077MachineInstr::dump(unsigned int indent) const
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000078{
79 for (unsigned i=0; i < indent; i++)
80 cout << " ";
81
82 cout << *this;
83}
84
85ostream&
86operator<< (ostream& os, const MachineInstr& minstr)
87{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000088 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000089
90 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++)
91 os << "\t" << minstr.getOperand(i);
92
Vikram S. Adve6a175e02001-07-28 04:06:37 +000093#undef DEBUG_VAL_OP_ITERATOR
94#ifdef DEBUG_VAL_OP_ITERATOR
95 os << endl << "\tValue operands are: ";
96 for (MachineInstr::val_op_const_iterator vo(&minstr); ! vo.done(); ++vo)
97 {
98 const Value* val = *vo;
99 os << val << (vo.isDef()? "(def), " : ", ");
100 }
101 os << endl;
102#endif
103
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000104 return os;
105}
106
Vikram S. Adve6e447182001-09-18 12:56:28 +0000107static inline ostream&
108OutputOperand(ostream &os, const MachineOperand &mop)
109{
110 switch (mop.getOperandType())
111 {
112 case MachineOperand::MO_CCRegister:
113 case MachineOperand::MO_VirtualRegister:
114 return os << "(val " << mop.getVRegValue() << ")";
115 case MachineOperand::MO_MachineRegister:
116 return os << "(" << mop.getMachineRegNum() << ")";
117 default:
118 assert(0 && "Unknown operand type");
119 return os;
120 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000121}
122
123
Vikram S. Adve6e447182001-09-18 12:56:28 +0000124ostream&
125operator<<(ostream &os, const MachineOperand &mop)
126{
127 switch(mop.opType)
128 {
129 case MachineOperand::MO_VirtualRegister:
130 case MachineOperand::MO_MachineRegister:
131 os << "%reg";
132 return OutputOperand(os, mop);
133 case MachineOperand::MO_CCRegister:
134 os << "%ccreg";
135 return OutputOperand(os, mop);
136 case MachineOperand::MO_SignExtendedImmed:
137 return os << mop.immedVal;
138 case MachineOperand::MO_UnextendedImmed:
139 return os << mop.immedVal;
140 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000141 {
142 const Value* opVal = mop.getVRegValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000143 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Advee949da52001-09-30 23:44:19 +0000144 return os << "%disp("
145 << (isLabel? "label " : "addr-of-val ")
146 << opVal << ")";
147 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000148 default:
149 assert(0 && "Unrecognized operand type");
150 break;
151 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000152
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000153 return os;
154}
155
156
Vikram S. Adve5b795912001-08-28 23:02:39 +0000157void
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000158PrintMachineInstructions(const Method *const method)
159{
160 cout << "\n" << method->getReturnType()
161 << " \"" << method->getName() << "\"" << endl;
162
163 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
164 {
165 BasicBlock* bb = *BI;
166 cout << "\n"
167 << (bb->hasName()? bb->getName() : "Label")
168 << " (" << bb << ")" << ":"
169 << endl;
170
171 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
172 for (unsigned i=0; i < mvec.size(); i++)
173 cout << "\t" << *mvec[i] << endl;
174 }
175 cout << endl << "End method \"" << method->getName() << "\""
176 << endl << endl;
177}