blob: 76fd4e7c8357b5a10a852e77dba0bc0cac4ef190 [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 }
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000101#endif
102
Vikram S. Adve6d353262001-10-17 23:57:50 +0000103 os << endl;
104
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000105 return os;
106}
107
Vikram S. Adve6e447182001-09-18 12:56:28 +0000108static inline ostream&
109OutputOperand(ostream &os, const MachineOperand &mop)
110{
111 switch (mop.getOperandType())
112 {
113 case MachineOperand::MO_CCRegister:
114 case MachineOperand::MO_VirtualRegister:
115 return os << "(val " << mop.getVRegValue() << ")";
116 case MachineOperand::MO_MachineRegister:
117 return os << "(" << mop.getMachineRegNum() << ")";
118 default:
119 assert(0 && "Unknown operand type");
120 return os;
121 }
Chris Lattnere6fdb112001-09-09 22:26:29 +0000122}
123
124
Vikram S. Adve6e447182001-09-18 12:56:28 +0000125ostream&
126operator<<(ostream &os, const MachineOperand &mop)
127{
128 switch(mop.opType)
129 {
130 case MachineOperand::MO_VirtualRegister:
131 case MachineOperand::MO_MachineRegister:
132 os << "%reg";
133 return OutputOperand(os, mop);
134 case MachineOperand::MO_CCRegister:
135 os << "%ccreg";
136 return OutputOperand(os, mop);
137 case MachineOperand::MO_SignExtendedImmed:
138 return os << mop.immedVal;
139 case MachineOperand::MO_UnextendedImmed:
140 return os << mop.immedVal;
141 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000142 {
143 const Value* opVal = mop.getVRegValue();
Chris Lattner1d87bcf2001-10-01 20:11:19 +0000144 bool isLabel = isa<Method>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Advee949da52001-09-30 23:44:19 +0000145 return os << "%disp("
146 << (isLabel? "label " : "addr-of-val ")
147 << opVal << ")";
148 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000149 default:
150 assert(0 && "Unrecognized operand type");
151 break;
152 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000153
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000154 return os;
155}
156
157
Vikram S. Adve5b795912001-08-28 23:02:39 +0000158void
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000159PrintMachineInstructions(const Method *const method)
160{
161 cout << "\n" << method->getReturnType()
162 << " \"" << method->getName() << "\"" << endl;
163
164 for (Method::const_iterator BI = method->begin(); BI != method->end(); ++BI)
165 {
166 BasicBlock* bb = *BI;
167 cout << "\n"
168 << (bb->hasName()? bb->getName() : "Label")
169 << " (" << bb << ")" << ":"
170 << endl;
171
172 MachineCodeForBasicBlock& mvec = bb->getMachineInstrVec();
173 for (unsigned i=0; i < mvec.size(); i++)
Vikram S. Adve6d353262001-10-17 23:57:50 +0000174 cout << "\t" << *mvec[i];
Ruchira Sasankaed8f6742001-09-15 19:07:45 +0000175 }
176 cout << endl << "End method \"" << method->getName() << "\""
177 << endl << endl;
178}