blob: 4fc373065881b8da2ff355099dcd77d5cbfccd98 [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. Adve3bc9ef92002-07-10 21:45:04 +000052 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
53 operands[i].markDef();
54 if (isDefAndUse)
55 operands[i].markDefAndUse();
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000056}
57
58void
Vikram S. Advec356e562002-03-18 03:35:24 +000059MachineInstr::SetMachineOperandConst(unsigned int i,
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000060 MachineOperand::MachineOperandType operandType,
Vikram S. Advec356e562002-03-18 03:35:24 +000061 int64_t intValue)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000062{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000063 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000064 assert(TargetInstrDescriptors[opCode].resultPos != (int) i &&
65 "immed. constant cannot be defined");
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000066 operands[i].InitializeConst(operandType, intValue);
67}
68
69void
Vikram S. Advec356e562002-03-18 03:35:24 +000070MachineInstr::SetMachineOperandReg(unsigned int i,
71 int regNum,
72 bool isdef=false,
Vikram S. Adve7a4be952002-07-08 22:38:45 +000073 bool isDefAndUse=false,
Vikram S. Advec356e562002-03-18 03:35:24 +000074 bool isCCReg=false)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +000075{
Vikram S. Adve6a175e02001-07-28 04:06:37 +000076 assert(i < operands.size());
Vikram S. Advec356e562002-03-18 03:35:24 +000077 operands[i].InitializeReg(regNum, isCCReg);
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +000078 if (isdef || TargetInstrDescriptors[opCode].resultPos == (int) i)
79 operands[i].markDef();
80 if (isDefAndUse)
81 operands[i].markDefAndUse();
Vikram S. Adve7a4be952002-07-08 22:38:45 +000082 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())
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000104 return os << val->getName() << ")";
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000105 else
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000106 return os << (void*) val << ")"; // print address only
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000107}
108
Chris Lattner697954c2002-01-20 22:54:45 +0000109std::ostream &operator<<(std::ostream& os, const MachineInstr& minstr)
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000110{
Vikram S. Adve6a175e02001-07-28 04:06:37 +0000111 os << TargetInstrDescriptors[minstr.opCode].opCodeString;
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000112
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000113 for (unsigned i=0, N=minstr.getNumOperands(); i < N; i++) {
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000114 os << "\t" << minstr.getOperand(i);
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000115 if( minstr.operandIsDefined(i) )
116 os << "*";
117 if( minstr.operandIsDefinedAndUsed(i) )
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000118 os << "*";
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000119 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000120
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000121 // code for printing implict references
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000122 unsigned NumOfImpRefs = minstr.getNumImplicitRefs();
123 if( NumOfImpRefs > 0 ) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000124 os << "\tImplicit: ";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000125 for(unsigned z=0; z < NumOfImpRefs; z++) {
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000126 OutputValue(os, minstr.getImplicitRef(z));
Ruchira Sasanka8d243372001-11-14 20:05:23 +0000127 if( minstr.implicitRefIsDefined(z)) os << "*";
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000128 if( minstr.implicitRefIsDefinedAndUsed(z)) os << "*";
Ruchira Sasanka07c70862001-11-15 20:46:40 +0000129 os << "\t";
Ruchira Sasanka69917e22001-10-18 22:40:02 +0000130 }
131 }
Vikram S. Adve93240fe2002-04-25 04:31:18 +0000132
Chris Lattner697954c2002-01-20 22:54:45 +0000133 return os << "\n";
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000134}
135
Chris Lattner697954c2002-01-20 22:54:45 +0000136std::ostream &operator<<(std::ostream &os, const MachineOperand &mop)
Vikram S. Adve6e447182001-09-18 12:56:28 +0000137{
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000138 if (mop.opHiBits32())
139 os << "%lm(";
140 else if (mop.opLoBits32())
141 os << "%lo(";
142 else if (mop.opHiBits64())
143 os << "%hh(";
144 else if (mop.opLoBits64())
145 os << "%hm(";
146
Vikram S. Adve6e447182001-09-18 12:56:28 +0000147 switch(mop.opType)
148 {
149 case MachineOperand::MO_VirtualRegister:
Vikram S. Adve6e447182001-09-18 12:56:28 +0000150 os << "%reg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000151 OutputValue(os, mop.getVRegValue());
152 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000153 case MachineOperand::MO_CCRegister:
154 os << "%ccreg";
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000155 OutputValue(os, mop.getVRegValue());
156 break;
157 case MachineOperand::MO_MachineRegister:
158 os << "%reg";
159 os << "(" << mop.getMachineRegNum() << ")";
160 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000161 case MachineOperand::MO_SignExtendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000162 os << (long)mop.immedVal;
163 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000164 case MachineOperand::MO_UnextendedImmed:
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000165 os << (long)mop.immedVal;
166 break;
Vikram S. Adve6e447182001-09-18 12:56:28 +0000167 case MachineOperand::MO_PCRelativeDisp:
Vikram S. Advee949da52001-09-30 23:44:19 +0000168 {
169 const Value* opVal = mop.getVRegValue();
Chris Lattner4d669b52002-04-08 22:01:15 +0000170 bool isLabel = isa<Function>(opVal) || isa<BasicBlock>(opVal);
Vikram S. Adved9beb972001-11-12 14:19:47 +0000171 os << "%disp(" << (isLabel? "label " : "addr-of-val ");
172 if (opVal->hasName())
Chris Lattner697954c2002-01-20 22:54:45 +0000173 os << opVal->getName();
Vikram S. Adved9beb972001-11-12 14:19:47 +0000174 else
Vikram S. Adve7a4be952002-07-08 22:38:45 +0000175 os << (const void*) opVal;
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000176 os << ")";
177 break;
Vikram S. Advee949da52001-09-30 23:44:19 +0000178 }
Vikram S. Adve6e447182001-09-18 12:56:28 +0000179 default:
180 assert(0 && "Unrecognized operand type");
181 break;
182 }
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000183
Vikram S. Adve3bc9ef92002-07-10 21:45:04 +0000184 if (mop.flags &
185 (MachineOperand::HIFLAG32 | MachineOperand::LOFLAG32 |
186 MachineOperand::HIFLAG64 | MachineOperand::LOFLAG64))
187 os << ")";
188
Vikram S. Adve70bc4b52001-07-21 12:41:50 +0000189 return os;
190}